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
sixteen:
github: ralsina/sixteen
branch: main
crystal: ">= 1.13.0"

View File

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

View File

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

View File

@ -29,7 +29,7 @@ module Tartrazine
# anything
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
macro merge_prop(prop)
@ -95,8 +95,8 @@ module Tartrazine
theme.styles["Text"] = Style.new(color: t.palette["base05"])
theme.styles["Error"] = Style.new(color: t.palette["base08"])
theme.styles["Comment"] = Style.new(color: t.palette["base03"])
theme.styles["CommentPreProc"] = Style.new(color: t.palette["base0F"])
theme.styles["CommentPreProcFile"] = Style.new(color: t.palette["base0B"])
theme.styles["CommentPreproc"] = Style.new(color: t.palette["base0F"])
theme.styles["CommentPreprocFile"] = Style.new(color: t.palette["base0B"])
theme.styles["Keyword"] = Style.new(color: t.palette["base0E"])
theme.styles["KeywordType"] = Style.new(color: t.palette["base08"])
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["NameVariable"] = Style.new(color: t.palette["base0D"])
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["OperatorWord"] = Style.new(color: t.palette["base0E"])
theme.styles["Literal"] = Style.new(color: t.palette["base0B"])
theme.styles["String"] = Style.new(color: t.palette["base0B"])
theme.styles["StringInterpol"] = Style.new(color: t.palette["base0F"])
theme.styles["StringRegex"] = Style.new(color: t.palette["base0C"])
theme.styles["StringSymbol"] = Style.new(color: t.palette["base09"])
theme.styles["LiteralString"] = Style.new(color: t.palette["base0B"])
theme.styles["LiteralStringInterpol"] = Style.new(color: t.palette["base0F"])
theme.styles["LiteralStringRegex"] = Style.new(color: t.palette["base0C"])
theme.styles["LiteralStringSymbol"] = Style.new(color: t.palette["base09"])
theme
end