Made base16 work

This commit is contained in:
Roberto Alsina 2024-08-06 18:27:58 -03:00
parent 94bc221545
commit 420b68993c
2 changed files with 35 additions and 28 deletions

View File

@ -1,5 +1,5 @@
require "./**" require "./**"
lexer = Tartrazine.lexer("crystal") lexer = Tartrazine.lexer("crystal")
theme = Tartrazine.theme("catppuccin-macchiato") theme = Tartrazine.theme(ARGV[1])
puts Tartrazine::Html.new.format(File.read(ARGV[0]), lexer, theme) puts Tartrazine::Html.new.format(File.read(ARGV[0]), lexer, theme)

View File

@ -3,6 +3,7 @@ require "xml"
module Tartrazine module Tartrazine
def self.theme(name : String) : Theme def self.theme(name : String) : Theme
return Theme.from_base16(name[7..]) if name.starts_with? "base16_"
path = File.join("styles", "#{name}.xml") path = File.join("styles", "#{name}.xml")
Theme.from_xml(File.read(path)) Theme.from_xml(File.read(path))
end end
@ -28,6 +29,9 @@ 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)
end
macro merge_prop(prop) macro merge_prop(prop)
new.{{prop}} = other.{{prop}}.nil? ? self.{{prop}} : other.{{prop}} new.{{prop}} = other.{{prop}}.nil? ? self.{{prop}} : other.{{prop}}
end end
@ -82,36 +86,39 @@ module Tartrazine
# Load from a base16 theme name using Sixteen # Load from a base16 theme name using Sixteen
def self.from_base16(name : String) : Theme def self.from_base16(name : String) : Theme
t = Sixteen.theme(name) t = Sixteen.theme(name)
theme = Theme.new
theme.name = name
# The color assignments are adapted from # The color assignments are adapted from
# https://github.com/mohd-akram/base16-pygments/ # https://github.com/mohd-akram/base16-pygments/
t.styles["Background"] = Style.new(color: t["base05"], background: t["base00"]) theme.styles["Background"] = Style.new(color: t.palette["base05"], background: t.palette["base00"])
t.styles["Text"] = Style.new(color: t["base05"]) theme.styles["Text"] = Style.new(color: t.palette["base05"])
t.styles["Error"] = Style.new(color: t["base08"]) theme.styles["Error"] = Style.new(color: t.palette["base08"])
t.styles["Comment"] = Style.new(color: t["base03"]) theme.styles["Comment"] = Style.new(color: t.palette["base03"])
t.styles["CommentPreProc"] = Style.new(color: t["base0f"]) theme.styles["CommentPreProc"] = Style.new(color: t.palette["base0F"])
t.styles["CommentPreProcFile"] = Style.new(color: t["base0b"]) theme.styles["CommentPreProcFile"] = Style.new(color: t.palette["base0B"])
t.styles["Keyword"] = Style.new(color: t["base0e"]) theme.styles["Keyword"] = Style.new(color: t.palette["base0E"])
t.styles["KeywordType"] = Style.new(color: t["base08"]) theme.styles["KeywordType"] = Style.new(color: t.palette["base08"])
t.styles["NameAttribute"] = Style.new(color: t["base0d"]) theme.styles["NameAttribute"] = Style.new(color: t.palette["base0D"])
t.styles["NameBuiltin"] = Style.new(color: t["base08"]) theme.styles["NameBuiltin"] = Style.new(color: t.palette["base08"])
t.styles["NameBuiltinPseudo"] = Style.new(color: t["base08"]) theme.styles["NameBuiltinPseudo"] = Style.new(color: t.palette["base08"])
t.styles["NameClass"] = Style.new(color: t["base0d"]) theme.styles["NameClass"] = Style.new(color: t.palette["base0D"])
t.styles["NameConstant"] = Style.new(color: t["base09"]) theme.styles["NameConstant"] = Style.new(color: t.palette["base09"])
t.styles["NameDecorator"] = Style.new(color: t["base09"]) theme.styles["NameDecorator"] = Style.new(color: t.palette["base09"])
t.styles["NameFunction"] = Style.new(color: t["base0d"]) theme.styles["NameFunction"] = Style.new(color: t.palette["base0D"])
t.styles["NameNamespace"] = Style.new(color: t["base0d"]) theme.styles["NameNamespace"] = Style.new(color: t.palette["base0D"])
t.styles["NameTag"] = Style.new(color: t["base0e"]) theme.styles["NameTag"] = Style.new(color: t.palette["base0E"])
t.styles["NameVariable"] = Style.new(color: t["base0d"]) theme.styles["NameVariable"] = Style.new(color: t.palette["base0D"])
t.styles["NameVariableInstance"] = Style.new(color: t["base08"]) theme.styles["NameVariableInstance"] = Style.new(color: t.palette["base08"])
t.styles["Number"] = Style.new(color: t["base09"]) theme.styles["Number"] = Style.new(color: t.palette["base09"])
t.styles["Operator"] = Style.new(color: t["base0c"]) theme.styles["Operator"] = Style.new(color: t.palette["base0C"])
t.styles["OperatorWord"] = Style.new(color: t["base0e"]) theme.styles["OperatorWord"] = Style.new(color: t.palette["base0E"])
t.styles["Literal"] = Style.new(color: t["base0b"]) theme.styles["Literal"] = Style.new(color: t.palette["base0B"])
t.styles["String"] = Style.new(color: t["base0b"]) theme.styles["String"] = Style.new(color: t.palette["base0B"])
t.styles["StringInterpol"] = Style.new(color: t["base0f"]) theme.styles["StringInterpol"] = Style.new(color: t.palette["base0F"])
t.styles["StringRegex"] = Style.new(color: t["base0c"]) theme.styles["StringRegex"] = Style.new(color: t.palette["base0C"])
t.styles["StringSymbol"] = Style.new(color: t["base09"]) theme.styles["StringSymbol"] = Style.new(color: t.palette["base09"])
theme
end end
# Load from a Chroma XML file # Load from a Chroma XML file