Make formatter a bit more convenient

This commit is contained in:
Roberto Alsina 2024-08-19 09:59:01 -03:00
parent bb952a44b8
commit ad92929a10
3 changed files with 7 additions and 3 deletions

View File

@ -12,7 +12,7 @@ module Tartrazine
property theme : Theme = Tartrazine.theme("default-dark") property theme : Theme = Tartrazine.theme("default-dark")
# Format the text using the given lexer. # Format the text using the given lexer.
def format(text : String, lexer : Lexer) : String def format(text : String, lexer : Lexer, io : IO?) : String?
raise Exception.new("Not implemented") raise Exception.new("Not implemented")
end end

View File

@ -11,7 +11,8 @@ module Tartrazine
"#{i + 1}".rjust(4).ljust(5) "#{i + 1}".rjust(4).ljust(5)
end end
def format(text : String, lexer : Lexer, outp : IO) : Nil def format(text : String, lexer : Lexer, io : IO?) : String?
outp = io.nil? ? String::Builder.new("") : io
tokenizer = Tokenizer.new(lexer, text) tokenizer = Tokenizer.new(lexer, text)
i = 0 i = 0
outp << line_label(i) if line_numbers? outp << line_label(i) if line_numbers?
@ -22,6 +23,7 @@ module Tartrazine
outp << line_label(i) if line_numbers? outp << line_label(i) if line_numbers?
end end
end end
return outp.to_s if io.nil?
end end
def colorize(text : String, token : String) : String def colorize(text : String, token : String) : String

View File

@ -58,7 +58,8 @@ module Tartrazine
"<span #{line_id} #{line_class} style=\"user-select: none;\">#{line_label} </span>" "<span #{line_id} #{line_class} style=\"user-select: none;\">#{line_label} </span>"
end end
def format_text(text : String, lexer : Lexer, outp : IO) : Nil def format_text(text : String, lexer : Lexer, io : IO?) : String?
outp = io.nil? ? String::Builder.new("") : io
tokenizer = Tokenizer.new(lexer, text) tokenizer = Tokenizer.new(lexer, text)
i = 0 i = 0
if surrounding_pre? if surrounding_pre?
@ -75,6 +76,7 @@ module Tartrazine
end end
end end
outp << "</code></pre>" outp << "</code></pre>"
return outp.to_s if io.nil?
end end
# ameba:disable Metrics/CyclomaticComplexity # ameba:disable Metrics/CyclomaticComplexity