mirror of
https://github.com/ralsina/tartrazine.git
synced 2024-11-10 05:22:23 +00:00
Starting to add options to HTML formatter: standalone
This commit is contained in:
parent
47237eecc3
commit
fd5af6ba3b
@ -2,17 +2,49 @@ require "../formatter"
|
||||
|
||||
module Tartrazine
|
||||
class Html < Formatter
|
||||
# Not all of these options are implemented
|
||||
|
||||
property? standalone : Bool = false
|
||||
|
||||
# property class_prefix : String = ""
|
||||
# property with_classes : Bool = true
|
||||
# property tab_width = 8
|
||||
# property surrounding_pre : Bool = true
|
||||
# property wrap_long_lines : Bool = false
|
||||
# property line_numbers : Bool = false
|
||||
# property line_number_in_table : Bool = false
|
||||
# property linkable_line_numbers : Bool = false
|
||||
# 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
|
||||
text = format_text(text, lexer, theme)
|
||||
if standalone?
|
||||
text = wrap_standalone(text, theme)
|
||||
end
|
||||
text
|
||||
end
|
||||
|
||||
# Wrap text into a full HTML document, including the CSS for the theme
|
||||
def wrap_standalone(text, theme) : String
|
||||
output = String.build do |outp|
|
||||
outp << "<html><head><style>"
|
||||
outp << "<!DOCTYPE html><html><head><style>"
|
||||
outp << get_style_defs(theme)
|
||||
outp << "</style></head><body>"
|
||||
outp << text
|
||||
outp << "</body></html>"
|
||||
end
|
||||
output
|
||||
end
|
||||
|
||||
def format_text(text : String, lexer : Lexer, theme : Theme) : String
|
||||
output = String.build do |outp|
|
||||
outp << "<pre class=\"#{get_css_class("Background", theme)}\"><code class=\"#{get_css_class("Background", theme)}\">"
|
||||
lexer.tokenize(text).each do |token|
|
||||
fragment = "<span class=\"#{get_css_class(token[:type], theme)}\">#{token[:value]}</span>"
|
||||
outp << fragment
|
||||
end
|
||||
outp << "</code></pre></body></html>"
|
||||
outp << "</code></pre>"
|
||||
end
|
||||
output
|
||||
end
|
||||
@ -23,9 +55,9 @@ module Tartrazine
|
||||
theme.styles.each do |token, style|
|
||||
outp << ".#{get_css_class(token, theme)} {"
|
||||
# These are set or nil
|
||||
outp << "color: #{style.color.try &.hex};" if style.color
|
||||
outp << "background-color: #{style.background.try &.hex};" if style.background
|
||||
outp << "border: 1px solid #{style.border.try &.hex};" if style.border
|
||||
outp << "color: ##{style.color.try &.hex};" if style.color
|
||||
outp << "background-color: ##{style.background.try &.hex};" if style.background
|
||||
outp << "border: 1px solid ##{style.border.try &.hex};" if style.border
|
||||
|
||||
# These are true/false/nil
|
||||
outp << "border: none;" if style.border == false
|
||||
@ -55,5 +87,4 @@ module Tartrazine
|
||||
}]
|
||||
end
|
||||
end
|
||||
|
||||
end
|
@ -2,4 +2,6 @@ require "./**"
|
||||
|
||||
lexer = Tartrazine.lexer("crystal")
|
||||
theme = Tartrazine.theme(ARGV[1])
|
||||
puts Tartrazine::Html.new.format(File.read(ARGV[0]), lexer, theme)
|
||||
formatter = Tartrazine::Html.new
|
||||
formatter.standalone = true
|
||||
puts formatter.format(File.read(ARGV[0]), lexer, theme)
|
||||
|
Loading…
Reference in New Issue
Block a user