mirror of
https://github.com/ralsina/tartrazine.git
synced 2024-11-10 05:22:23 +00:00
HTML formatter option: line_number_start / line_number_id_prefix
This commit is contained in:
parent
e6a292ade0
commit
d0c2b1764a
@ -13,10 +13,12 @@ module Tartrazine
|
|||||||
# property surrounding_pre : Bool = true
|
# property surrounding_pre : Bool = true
|
||||||
# property wrap_long_lines : Bool = false
|
# property wrap_long_lines : Bool = false
|
||||||
# property line_numbers : Bool = false
|
# property line_numbers : Bool = false
|
||||||
|
property line_number_start : Int32 = 1
|
||||||
|
property line_number_id_prefix : String = "line-"
|
||||||
|
|
||||||
# property line_number_in_table : Bool = false
|
# property line_number_in_table : Bool = false
|
||||||
# property linkable_line_numbers : Bool = false
|
# property linkable_line_numbers : Bool = false
|
||||||
# property highlight_lines : Array(Range(Int32, Int32)) = [] of Range(Int32, Int32)
|
# property highlight_lines : Array(Range(Int32, Int32)) = [] of Range(Int32, Int32)
|
||||||
# property base_line_number : Int32 = 1
|
|
||||||
|
|
||||||
def format(text : String, lexer : Lexer, theme : Theme) : String
|
def format(text : String, lexer : Lexer, theme : Theme) : String
|
||||||
text = format_text(text, lexer, theme)
|
text = format_text(text, lexer, theme)
|
||||||
@ -39,11 +41,16 @@ module Tartrazine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def format_text(text : String, lexer : Lexer, theme : Theme) : String
|
def format_text(text : String, lexer : Lexer, theme : Theme) : String
|
||||||
|
lines = group_tokens_in_lines(lexer.tokenize(text))
|
||||||
output = String.build do |outp|
|
output = String.build do |outp|
|
||||||
outp << "<pre class=\"#{get_css_class("Background", theme)}\"><code class=\"#{get_css_class("Background", theme)}\">"
|
outp << "<pre class=\"#{get_css_class("Background", theme)}\"><code class=\"#{get_css_class("Background", theme)}\">"
|
||||||
lexer.tokenize(text).each do |token|
|
lines.each_with_index(offset: line_number_start - 1) do |line, i|
|
||||||
fragment = "<span class=\"#{get_css_class(token[:type], theme)}\">#{token[:value]}</span>"
|
outp << "<span id=\"#{line_number_id_prefix}#{i + 1}\">"
|
||||||
outp << fragment
|
line.each do |token|
|
||||||
|
fragment = "<span class=\"#{get_css_class(token[:type], theme)}\">#{token[:value]}</span>"
|
||||||
|
outp << fragment
|
||||||
|
end
|
||||||
|
outp << "</span>"
|
||||||
end
|
end
|
||||||
outp << "</code></pre>"
|
outp << "</code></pre>"
|
||||||
end
|
end
|
||||||
@ -88,5 +95,28 @@ module Tartrazine
|
|||||||
theme.styles.has_key?(parent)
|
theme.styles.has_key?(parent)
|
||||||
}]
|
}]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def group_tokens_in_lines(tokens : Array(Token)) : Array(Array(Token))
|
||||||
|
split_tokens = [] of Token
|
||||||
|
tokens.each do |token|
|
||||||
|
if token[:value].includes?("\n")
|
||||||
|
values = token[:value].split("\n")
|
||||||
|
values.each_with_index do |value, index|
|
||||||
|
value += "\n" if index < values.size - 1
|
||||||
|
split_tokens << {type: token[:type], value: value}
|
||||||
|
end
|
||||||
|
else
|
||||||
|
split_tokens << token
|
||||||
|
end
|
||||||
|
end
|
||||||
|
lines = [Array(Token).new]
|
||||||
|
split_tokens.each do |token|
|
||||||
|
lines.last << token
|
||||||
|
if token[:value].includes?("\n")
|
||||||
|
lines << Array(Token).new
|
||||||
|
end
|
||||||
|
end
|
||||||
|
lines
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -4,5 +4,7 @@ lexer = Tartrazine.lexer("crystal")
|
|||||||
theme = Tartrazine.theme(ARGV[1])
|
theme = Tartrazine.theme(ARGV[1])
|
||||||
formatter = Tartrazine::Html.new
|
formatter = Tartrazine::Html.new
|
||||||
formatter.standalone = true
|
formatter.standalone = true
|
||||||
formatter.class_prefix = "tartrazine-"
|
formatter.class_prefix = "hl-"
|
||||||
|
formatter.line_number_id_prefix = "ln-"
|
||||||
|
formatter.line_number_start = 3
|
||||||
puts formatter.format(File.read(ARGV[0]), lexer, theme)
|
puts formatter.format(File.read(ARGV[0]), lexer, theme)
|
||||||
|
Loading…
Reference in New Issue
Block a user