Make formatter a bit more convenient

This commit is contained in:
Roberto Alsina 2024-08-19 11:26:34 -03:00
parent 57e63f2308
commit 3ebedec6c1
4 changed files with 32 additions and 14 deletions

View File

@ -12,7 +12,11 @@ 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, io : IO? = nil) : String? def format(text : String, lexer : Lexer, io : IO = nil) : Nil
raise Exception.new("Not implemented")
end
def format(text : String, lexer : Lexer) : String
raise Exception.new("Not implemented") raise Exception.new("Not implemented")
end end

View File

@ -11,8 +11,13 @@ module Tartrazine
"#{i + 1}".rjust(4).ljust(5) "#{i + 1}".rjust(4).ljust(5)
end end
def format(text : String, lexer : Lexer, io : IO? = nil) : String? def format(text : String, lexer : Lexer) : String
outp = io.nil? ? String::Builder.new("") : io outp = String::Builder.new("")
format(text, lexer, outp)
return outp.to_s
end
def format(text : String, lexer : Lexer, outp : IO) : Nil
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?
@ -23,7 +28,6 @@ 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

@ -34,13 +34,18 @@ module Tartrazine
@weight_of_bold : Int32 = 600) @weight_of_bold : Int32 = 600)
end end
def format(text : String, lexer : Lexer, io : IO? = nil) : String? def format(text : String, lexer : Lexer) : String
outp = io.nil? ? String::Builder.new("") : io outp = String::Builder.new("")
format(text, lexer, outp)
return outp.to_s
end
def format(text : String, lexer : Lexer, io : IO) : Nil
pre, post = wrap_standalone pre, post = wrap_standalone
outp << pre if standalone? io << pre if standalone?
format_text(text, lexer, outp) format_text(text, lexer, io)
outp << post if standalone? io << post if standalone?
return outp.to_s if io.nil?
end end
# Wrap text into a full HTML document, including the CSS for the theme # Wrap text into a full HTML document, including the CSS for the theme

View File

@ -4,11 +4,16 @@ module Tartrazine
class Json < Formatter class Json < Formatter
property name = "json" property name = "json"
def format(text : String, lexer : Lexer, io : IO? = nil) : String? def format(text : String, lexer : Lexer) : String
outp = io.nil? ? String::Builder.new("") : io outp = String::Builder.new("")
format(text, lexer, outp)
return outp.to_s
end
def format(text : String, lexer : Lexer, io : IO) : Nil
tokenizer = Tokenizer.new(lexer, text) tokenizer = Tokenizer.new(lexer, text)
outp << Tartrazine::Lexer.collapse_tokens(tokenizer.to_a).to_json io << Tartrazine::Lexer.collapse_tokens(tokenizer.to_a).to_json
return outp.to_s if io.nil?
end end
end end
end end