Use abbreviated token names in css

This commit is contained in:
Roberto Alsina 2024-08-06 21:28:33 -03:00
parent 499cf7f623
commit 0c86e91b0b
5 changed files with 17 additions and 16 deletions

View File

@ -13,6 +13,7 @@ dependencies:
github: crystal-china/base58.cr github: crystal-china/base58.cr
sixteen: sixteen:
github: ralsina/sixteen github: ralsina/sixteen
branch: main
crystal: ">= 1.13.0" crystal: ">= 1.13.0"

View File

@ -1,5 +1,6 @@
module Tartrazine module Tartrazine
Abbreviations = { Abbreviations = {
"Background" => "b",
"Text" => "t", "Text" => "t",
"CommentSingle" => "cs", "CommentSingle" => "cs",
"CommentSpecial" => "cs", "CommentSpecial" => "cs",

View File

@ -1,4 +1,4 @@
require "./constantes.cr" require "./constants.cr"
require "./styles.cr" require "./styles.cr"
require "./tartrazine.cr" require "./tartrazine.cr"
@ -15,7 +15,7 @@ module Tartrazine
def get_style_defs(theme : Theme) : String def get_style_defs(theme : Theme) : String
output = String.build do |outp| output = String.build do |outp|
theme.styles.each do |token, style| theme.styles.each do |token, style|
outp << ".#{token} {" outp << ".#{get_css_class(token, theme)} {"
# These are set or nil # These are set or nil
outp << "color: #{style.color};" if style.color outp << "color: #{style.color};" if style.color
outp << "background-color: #{style.background};" if style.background outp << "background-color: #{style.background};" if style.background
@ -43,7 +43,7 @@ module Tartrazine
outp << "<html><head><style>" outp << "<html><head><style>"
outp << get_style_defs(theme) outp << get_style_defs(theme)
outp << "</style></head><body>" outp << "</style></head><body>"
outp << "<pre class=\"Background\"><code class=\"Background\">" outp << "<pre class=\"#{get_css_class("Background", theme)}\"><code class=\"#{get_css_class("Background", theme)}\">"
lexer.tokenize(text).each do |token| lexer.tokenize(text).each do |token|
fragment = "<span class=\"#{get_css_class(token[:type], theme)}\">#{token[:value]}</span>" fragment = "<span class=\"#{get_css_class(token[:type], theme)}\">#{token[:value]}</span>"
outp << fragment outp << fragment
@ -55,16 +55,15 @@ module Tartrazine
# 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, theme)
token = Abbreviations[token] return Abbreviations[token] if theme.styles.has_key?(token)
return token if theme.styles.has_key?(token)
# Themes don't contain information for each specific # Themes don't contain information for each specific
# token type. However, they may contain information # token type. However, they may contain information
# for a parent style. Worst case, we go to the root # for a parent style. Worst case, we go to the root
# (Background) style. # (Background) style.
theme.style_parents(token).reverse.find { |parent| Abbreviations[theme.style_parents(token).reverse.find { |parent|
theme.styles.has_key?(parent) theme.styles.has_key?(parent)
} }]
end end
end end
end end

View File

@ -1,4 +1,4 @@
require "./**" require "./**"
lexer = Tartrazine.lexer("crystal") lexer = Tartrazine.lexer("crystal")
theme = Tartrazine.theme(ARGV[1]) theme = Tartrazine.theme(ARGV[1])

View File

@ -29,7 +29,7 @@ module Tartrazine
# anything # anything
property? complete : Bool = false property? complete : Bool = false
def initialize(@color=nil, @background=nil, @border=nil, @bold=nil, @italic=nil, @underline=nil) def initialize(@color = nil, @background = nil, @border = nil, @bold = nil, @italic = nil, @underline = nil)
end end
macro merge_prop(prop) macro merge_prop(prop)
@ -95,8 +95,8 @@ module Tartrazine
theme.styles["Text"] = Style.new(color: t.palette["base05"]) theme.styles["Text"] = Style.new(color: t.palette["base05"])
theme.styles["Error"] = Style.new(color: t.palette["base08"]) theme.styles["Error"] = Style.new(color: t.palette["base08"])
theme.styles["Comment"] = Style.new(color: t.palette["base03"]) theme.styles["Comment"] = Style.new(color: t.palette["base03"])
theme.styles["CommentPreProc"] = Style.new(color: t.palette["base0F"]) theme.styles["CommentPreproc"] = Style.new(color: t.palette["base0F"])
theme.styles["CommentPreProcFile"] = Style.new(color: t.palette["base0B"]) theme.styles["CommentPreprocFile"] = Style.new(color: t.palette["base0B"])
theme.styles["Keyword"] = Style.new(color: t.palette["base0E"]) theme.styles["Keyword"] = Style.new(color: t.palette["base0E"])
theme.styles["KeywordType"] = Style.new(color: t.palette["base08"]) theme.styles["KeywordType"] = Style.new(color: t.palette["base08"])
theme.styles["NameAttribute"] = Style.new(color: t.palette["base0D"]) theme.styles["NameAttribute"] = Style.new(color: t.palette["base0D"])
@ -110,14 +110,14 @@ module Tartrazine
theme.styles["NameTag"] = Style.new(color: t.palette["base0E"]) theme.styles["NameTag"] = Style.new(color: t.palette["base0E"])
theme.styles["NameVariable"] = Style.new(color: t.palette["base0D"]) theme.styles["NameVariable"] = Style.new(color: t.palette["base0D"])
theme.styles["NameVariableInstance"] = Style.new(color: t.palette["base08"]) theme.styles["NameVariableInstance"] = Style.new(color: t.palette["base08"])
theme.styles["Number"] = Style.new(color: t.palette["base09"]) theme.styles["LiteralNumber"] = Style.new(color: t.palette["base09"])
theme.styles["Operator"] = Style.new(color: t.palette["base0C"]) theme.styles["Operator"] = Style.new(color: t.palette["base0C"])
theme.styles["OperatorWord"] = Style.new(color: t.palette["base0E"]) theme.styles["OperatorWord"] = Style.new(color: t.palette["base0E"])
theme.styles["Literal"] = Style.new(color: t.palette["base0B"]) theme.styles["Literal"] = Style.new(color: t.palette["base0B"])
theme.styles["String"] = Style.new(color: t.palette["base0B"]) theme.styles["LiteralString"] = Style.new(color: t.palette["base0B"])
theme.styles["StringInterpol"] = Style.new(color: t.palette["base0F"]) theme.styles["LiteralStringInterpol"] = Style.new(color: t.palette["base0F"])
theme.styles["StringRegex"] = Style.new(color: t.palette["base0C"]) theme.styles["LiteralStringRegex"] = Style.new(color: t.palette["base0C"])
theme.styles["StringSymbol"] = Style.new(color: t.palette["base09"]) theme.styles["LiteralStringSymbol"] = Style.new(color: t.palette["base09"])
theme theme
end end