mirror of
https://github.com/ralsina/tartrazine.git
synced 2025-07-04 15:29:25 +00:00
API changes to make it nicer
These are incompatible, tho. * Theme is now a property of the formatter instead of passing it arounf * get_style_defs is now style_defs
This commit is contained in:
@ -17,19 +17,35 @@ module Tartrazine
|
||||
property? wrap_long_lines : Bool = false
|
||||
property weight_of_bold : Int32 = 600
|
||||
|
||||
def format(text : String, lexer : Lexer, theme : Theme) : String
|
||||
text = format_text(text, lexer, theme)
|
||||
property theme : Theme
|
||||
|
||||
def initialize(@theme : Theme = Tartrazine.theme("default-dark"), *,
|
||||
@highlight_lines = [] of Range(Int32, Int32),
|
||||
@class_prefix : String = "",
|
||||
@line_number_id_prefix = "line-",
|
||||
@line_number_start = 1,
|
||||
@tab_width = 8,
|
||||
@line_numbers : Bool = false,
|
||||
@linkable_line_numbers : Bool = true,
|
||||
@standalone : Bool = false,
|
||||
@surrounding_pre : Bool = true,
|
||||
@wrap_long_lines : Bool = false,
|
||||
@weight_of_bold : Int32 = 600,)
|
||||
end
|
||||
|
||||
def format(text : String, lexer : Lexer) : String
|
||||
text = format_text(text, lexer)
|
||||
if standalone?
|
||||
text = wrap_standalone(text, theme)
|
||||
text = wrap_standalone(text)
|
||||
end
|
||||
text
|
||||
end
|
||||
|
||||
# Wrap text into a full HTML document, including the CSS for the theme
|
||||
def wrap_standalone(text, theme) : String
|
||||
def wrap_standalone(text) : String
|
||||
output = String.build do |outp|
|
||||
outp << "<!DOCTYPE html><html><head><style>"
|
||||
outp << get_style_defs(theme)
|
||||
outp << style_defs
|
||||
outp << "</style></head><body>"
|
||||
outp << text
|
||||
outp << "</body></html>"
|
||||
@ -37,21 +53,21 @@ module Tartrazine
|
||||
output
|
||||
end
|
||||
|
||||
def format_text(text : String, lexer : Lexer, theme : Theme) : String
|
||||
def format_text(text : String, lexer : Lexer) : String
|
||||
lines = lexer.group_tokens_in_lines(lexer.tokenize(text))
|
||||
output = String.build do |outp|
|
||||
if surrounding_pre?
|
||||
pre_style = wrap_long_lines? ? "style=\"white-space: pre-wrap; word-break: break-word;\"" : ""
|
||||
outp << "<pre class=\"#{get_css_class("Background", theme)}\" #{pre_style}>"
|
||||
outp << "<pre class=\"#{get_css_class("Background")}\" #{pre_style}>"
|
||||
end
|
||||
outp << "<code class=\"#{get_css_class("Background", theme)}\">"
|
||||
outp << "<code class=\"#{get_css_class("Background")}\">"
|
||||
lines.each_with_index(offset: line_number_start - 1) do |line, i|
|
||||
line_label = line_numbers? ? "#{i + 1}".rjust(4).ljust(5) : ""
|
||||
line_class = highlighted?(i + 1) ? "class=\"#{get_css_class("LineHighlight", theme)}\"" : ""
|
||||
line_class = highlighted?(i + 1) ? "class=\"#{get_css_class("LineHighlight")}\"" : ""
|
||||
line_id = linkable_line_numbers? ? "id=\"#{line_number_id_prefix}#{i + 1}\"" : ""
|
||||
outp << "<span #{line_id} #{line_class} style=\"user-select: none;\">#{line_label} </span>"
|
||||
line.each do |token|
|
||||
fragment = "<span class=\"#{get_css_class(token[:type], theme)}\">#{token[:value]}</span>"
|
||||
fragment = "<span class=\"#{get_css_class(token[:type])}\">#{token[:value]}</span>"
|
||||
outp << fragment
|
||||
end
|
||||
end
|
||||
@ -61,10 +77,10 @@ module Tartrazine
|
||||
end
|
||||
|
||||
# ameba:disable Metrics/CyclomaticComplexity
|
||||
def get_style_defs(theme : Theme) : String
|
||||
def style_defs : String
|
||||
output = String.build do |outp|
|
||||
theme.styles.each do |token, style|
|
||||
outp << ".#{get_css_class(token, theme)} {"
|
||||
outp << ".#{get_css_class(token)} {"
|
||||
# These are set or nil
|
||||
outp << "color: ##{style.color.try &.hex};" if style.color
|
||||
outp << "background-color: ##{style.background.try &.hex};" if style.background
|
||||
@ -87,7 +103,7 @@ module Tartrazine
|
||||
end
|
||||
|
||||
# Given a token type, return the CSS class to use.
|
||||
def get_css_class(token, theme)
|
||||
def get_css_class(token : String) : String
|
||||
return class_prefix + Abbreviations[token] if theme.styles.has_key?(token)
|
||||
|
||||
# Themes don't contain information for each specific
|
||||
@ -99,6 +115,7 @@ module Tartrazine
|
||||
}]
|
||||
end
|
||||
|
||||
# Is this line in the highlighted ranges?
|
||||
def highlighted?(line : Int) : Bool
|
||||
highlight_lines.any?(&.includes?(line))
|
||||
end
|
||||
|
Reference in New Issue
Block a user