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:
Roberto Alsina 2024-08-13 10:57:02 -03:00
parent 88f5674917
commit 2e87762f1b
4 changed files with 47 additions and 22 deletions

View File

@ -9,12 +9,15 @@ module Tartrazine
# This is the base class for all formatters. # This is the base class for all formatters.
abstract class Formatter abstract class Formatter
property name : String = "" property name : String = ""
property theme : Theme = Tartrazine.theme("default-dark")
def format(text : String, lexer : Lexer, theme : Theme) : String # Format the text using the given lexer.
def format(text : String, lexer : Lexer) : String
raise Exception.new("Not implemented") raise Exception.new("Not implemented")
end end
def get_style_defs(theme : Theme) : String # Return the styles, if the formatter supports it.
def style_defs : String
raise Exception.new("Not implemented") raise Exception.new("Not implemented")
end end
end end

View File

@ -4,20 +4,23 @@ module Tartrazine
class Ansi < Formatter class Ansi < Formatter
property? line_numbers : Bool = false property? line_numbers : Bool = false
def format(text : String, lexer : Lexer, theme : Theme) : String def initialize(@theme : Theme = Tartrazine.theme("default-dark"), @line_numbers : Bool = false)
end
def format(text : String, lexer : Lexer) : String
output = String.build do |outp| output = String.build do |outp|
lexer.group_tokens_in_lines(lexer.tokenize(text)).each_with_index do |line, i| lexer.group_tokens_in_lines(lexer.tokenize(text)).each_with_index do |line, i|
label = line_numbers? ? "#{i + 1}".rjust(4).ljust(5) : "" label = line_numbers? ? "#{i + 1}".rjust(4).ljust(5) : ""
outp << label outp << label
line.each do |token| line.each do |token|
outp << colorize(token[:value], token[:type], theme) outp << colorize(token[:value], token[:type])
end end
end end
end end
output output
end end
def colorize(text : String, token : String, theme : Theme) : String def colorize(text : String, token : String) : String
style = theme.styles.fetch(token, nil) style = theme.styles.fetch(token, nil)
return text if style.nil? return text if style.nil?
if theme.styles.has_key?(token) if theme.styles.has_key?(token)

View File

@ -17,19 +17,35 @@ module Tartrazine
property? wrap_long_lines : Bool = false property? wrap_long_lines : Bool = false
property weight_of_bold : Int32 = 600 property weight_of_bold : Int32 = 600
def format(text : String, lexer : Lexer, theme : Theme) : String property theme : Theme
text = format_text(text, lexer, 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? if standalone?
text = wrap_standalone(text, theme) text = wrap_standalone(text)
end end
text text
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
def wrap_standalone(text, theme) : String def wrap_standalone(text) : String
output = String.build do |outp| output = String.build do |outp|
outp << "<!DOCTYPE html><html><head><style>" outp << "<!DOCTYPE html><html><head><style>"
outp << get_style_defs(theme) outp << style_defs
outp << "</style></head><body>" outp << "</style></head><body>"
outp << text outp << text
outp << "</body></html>" outp << "</body></html>"
@ -37,21 +53,21 @@ module Tartrazine
output output
end 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)) lines = lexer.group_tokens_in_lines(lexer.tokenize(text))
output = String.build do |outp| output = String.build do |outp|
if surrounding_pre? if surrounding_pre?
pre_style = wrap_long_lines? ? "style=\"white-space: pre-wrap; word-break: break-word;\"" : "" 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 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| lines.each_with_index(offset: line_number_start - 1) do |line, i|
line_label = line_numbers? ? "#{i + 1}".rjust(4).ljust(5) : "" 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}\"" : "" 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>" outp << "<span #{line_id} #{line_class} style=\"user-select: none;\">#{line_label} </span>"
line.each do |token| 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 outp << fragment
end end
end end
@ -61,10 +77,10 @@ module Tartrazine
end end
# ameba:disable Metrics/CyclomaticComplexity # ameba:disable Metrics/CyclomaticComplexity
def get_style_defs(theme : Theme) : String def style_defs : String
output = String.build do |outp| output = String.build do |outp|
theme.styles.each do |token, style| theme.styles.each do |token, style|
outp << ".#{get_css_class(token, theme)} {" outp << ".#{get_css_class(token)} {"
# These are set or nil # These are set or nil
outp << "color: ##{style.color.try &.hex};" if style.color outp << "color: ##{style.color.try &.hex};" if style.color
outp << "background-color: ##{style.background.try &.hex};" if style.background outp << "background-color: ##{style.background.try &.hex};" if style.background
@ -87,7 +103,7 @@ module Tartrazine
end end
# Given a token type, return the CSS class to use. # 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) return class_prefix + Abbreviations[token] if theme.styles.has_key?(token)
# Themes don't contain information for each specific # Themes don't contain information for each specific
@ -99,6 +115,7 @@ module Tartrazine
}] }]
end end
# Is this line in the highlighted ranges?
def highlighted?(line : Int) : Bool def highlighted?(line : Int) : Bool
highlight_lines.any?(&.includes?(line)) highlight_lines.any?(&.includes?(line))
end end

View File

@ -54,6 +54,8 @@ if options["--list-formatters"]
exit 0 exit 0
end end
theme = Tartrazine.theme(options["-t"].as(String))
if options["-f"] if options["-f"]
formatter = options["-f"].as(String) formatter = options["-f"].as(String)
case formatter case formatter
@ -61,9 +63,11 @@ if options["-f"]
formatter = Tartrazine::Html.new formatter = Tartrazine::Html.new
formatter.standalone = options["--standalone"] != nil formatter.standalone = options["--standalone"] != nil
formatter.line_numbers = options["--line-numbers"] != nil formatter.line_numbers = options["--line-numbers"] != nil
formatter.theme = theme
when "terminal" when "terminal"
formatter = Tartrazine::Ansi.new formatter = Tartrazine::Ansi.new
formatter.line_numbers = options["--line-numbers"] != nil formatter.line_numbers = options["--line-numbers"] != nil
formatter.theme = theme
when "json" when "json"
formatter = Tartrazine::Json.new formatter = Tartrazine::Json.new
else else
@ -71,11 +75,9 @@ if options["-f"]
exit 1 exit 1
end end
theme = Tartrazine.theme(options["-t"].as(String))
if formatter.is_a?(Tartrazine::Html) && options["--css"] if formatter.is_a?(Tartrazine::Html) && options["--css"]
File.open("#{options["-t"].as(String)}.css", "w") do |outf| File.open("#{options["-t"].as(String)}.css", "w") do |outf|
outf.puts formatter.get_style_defs(theme) outf.puts formatter.style_defs
end end
exit 0 exit 0
end end
@ -83,7 +85,7 @@ if options["-f"]
lexer = Tartrazine.lexer(name: options["-l"].as(String), filename: options["FILE"].as(String)) lexer = Tartrazine.lexer(name: options["-l"].as(String), filename: options["FILE"].as(String))
input = File.open(options["FILE"].as(String)).gets_to_end input = File.open(options["FILE"].as(String)).gets_to_end
output = formatter.format(input, lexer, theme) output = formatter.format(input, lexer)
if options["-o"].nil? if options["-o"].nil?
puts output puts output