From 3a37f8bdc7fe484a9fbef4d93b1d995b7d6ffe56 Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Tue, 6 Aug 2024 12:18:03 -0300 Subject: [PATCH] Basic theme parser --- src/styles.cr | 46 +++++++++++++++++++++++++-- styles/api.go | 65 -------------------------------------- styles/compat.go | 66 --------------------------------------- styles/onesenterprise.xml | 10 ------ styles/pygments.xml | 42 ------------------------- 5 files changed, 43 insertions(+), 186 deletions(-) delete mode 100644 styles/api.go delete mode 100644 styles/compat.go delete mode 100644 styles/onesenterprise.xml delete mode 100644 styles/pygments.xml diff --git a/src/styles.cr b/src/styles.cr index 4d6ab36..ac0d206 100644 --- a/src/styles.cr +++ b/src/styles.cr @@ -7,9 +7,7 @@ module Tartrazine # false means it's not set # nil means inherit from parent style property bold : Bool? - property nobold : Bool? property italic : Bool? - property notalic : Bool? property underline : Bool? # These properties are either set or nil @@ -17,6 +15,27 @@ module Tartrazine property background : String? property border : String? property color : String? + + # Styles are incomplete by default and inherit + # from parents. If this is true, this style + # is already complete and should not inherit + # anything + property? complete : Bool = false + + macro merge_prop(prop) + new.{{prop}} = other.{{prop}}.nil? ? self.{{prop}} : other.{{prop}} + end + + def +(other : Style) + new = Style.new + merge_prop bold + merge_prop italic + merge_prop underline + merge_prop background + merge_prop border + merge_prop color + new + end end class Theme @@ -26,6 +45,27 @@ module Tartrazine # Get the style for a token. def style(token) + styles[token] = Style.new unless styles.has_key?(token) + s = styles[token] + + # We already got the data from the style hierarchy + return s if s.complete? + + # Form the hierarchy of parent styles + parents = ["Background"] + parts = token.underscore.split("_").map(&.capitalize) + parts.each_with_index do |_, i| + parents << parts[..i].join("") + end + + s = parents.map do |parent| + styles[parent] + end.reduce(s) do |acc, style| + acc + style + end + s.complete = true + styles[token] = s + s end # Load from a Chroma XML file @@ -64,4 +104,4 @@ end t = Tartrazine::Theme.from_xml(File.read("styles/catppuccin-frappe.xml")) -pp! t.styles["Name"] +pp! t.style("CommentPreprocFile") diff --git a/styles/api.go b/styles/api.go deleted file mode 100644 index e26d6f0..0000000 --- a/styles/api.go +++ /dev/null @@ -1,65 +0,0 @@ -package styles - -import ( - "embed" - "io/fs" - "sort" - - "github.com/alecthomas/chroma/v2" -) - -//go:embed *.xml -var embedded embed.FS - -// Registry of Styles. -var Registry = func() map[string]*chroma.Style { - registry := map[string]*chroma.Style{} - // Register all embedded styles. - files, err := fs.ReadDir(embedded, ".") - if err != nil { - panic(err) - } - for _, file := range files { - if file.IsDir() { - continue - } - r, err := embedded.Open(file.Name()) - if err != nil { - panic(err) - } - style, err := chroma.NewXMLStyle(r) - if err != nil { - panic(err) - } - registry[style.Name] = style - _ = r.Close() - } - return registry -}() - -// Fallback style. Reassign to change the default fallback style. -var Fallback = Registry["swapoff"] - -// Register a chroma.Style. -func Register(style *chroma.Style) *chroma.Style { - Registry[style.Name] = style - return style -} - -// Names of all available styles. -func Names() []string { - out := []string{} - for name := range Registry { - out = append(out, name) - } - sort.Strings(out) - return out -} - -// Get named style, or Fallback. -func Get(name string) *chroma.Style { - if style, ok := Registry[name]; ok { - return style - } - return Fallback -} diff --git a/styles/compat.go b/styles/compat.go deleted file mode 100644 index 4a6aaa6..0000000 --- a/styles/compat.go +++ /dev/null @@ -1,66 +0,0 @@ -package styles - -// Present for backwards compatibility. -// -// Deprecated: use styles.Get(name) instead. -var ( - Abap = Registry["abap"] - Algol = Registry["algol"] - AlgolNu = Registry["algol_nu"] - Arduino = Registry["arduino"] - Autumn = Registry["autumn"] - Average = Registry["average"] - Base16Snazzy = Registry["base16-snazzy"] - Borland = Registry["borland"] - BlackWhite = Registry["bw"] - CatppuccinFrappe = Registry["catppuccin-frappe"] - CatppuccinLatte = Registry["catppuccin-latte"] - CatppuccinMacchiato = Registry["catppuccin-macchiato"] - CatppuccinMocha = Registry["catppuccin-mocha"] - Colorful = Registry["colorful"] - DoomOne = Registry["doom-one"] - DoomOne2 = Registry["doom-one2"] - Dracula = Registry["dracula"] - Emacs = Registry["emacs"] - Friendly = Registry["friendly"] - Fruity = Registry["fruity"] - GitHubDark = Registry["github-dark"] - GitHub = Registry["github"] - GruvboxLight = Registry["gruvbox-light"] - Gruvbox = Registry["gruvbox"] - HrDark = Registry["hrdark"] - HrHighContrast = Registry["hr_high_contrast"] - Igor = Registry["igor"] - Lovelace = Registry["lovelace"] - Manni = Registry["manni"] - ModusOperandi = Registry["modus-operandi"] - ModusVivendi = Registry["modus-vivendi"] - Monokai = Registry["monokai"] - MonokaiLight = Registry["monokailight"] - Murphy = Registry["murphy"] - Native = Registry["native"] - Nord = Registry["nord"] - OnesEnterprise = Registry["onesenterprise"] - ParaisoDark = Registry["paraiso-dark"] - ParaisoLight = Registry["paraiso-light"] - Pastie = Registry["pastie"] - Perldoc = Registry["perldoc"] - Pygments = Registry["pygments"] - RainbowDash = Registry["rainbow_dash"] - RosePineDawn = Registry["rose-pine-dawn"] - RosePineMoon = Registry["rose-pine-moon"] - RosePine = Registry["rose-pine"] - Rrt = Registry["rrt"] - SolarizedDark = Registry["solarized-dark"] - SolarizedDark256 = Registry["solarized-dark256"] - SolarizedLight = Registry["solarized-light"] - SwapOff = Registry["swapoff"] - Tango = Registry["tango"] - Trac = Registry["trac"] - Vim = Registry["vim"] - VisualStudio = Registry["vs"] - Vulcan = Registry["vulcan"] - WitchHazel = Registry["witchhazel"] - XcodeDark = Registry["xcode-dark"] - Xcode = Registry["xcode"] -) diff --git a/styles/onesenterprise.xml b/styles/onesenterprise.xml deleted file mode 100644 index ce86db3..0000000 --- a/styles/onesenterprise.xml +++ /dev/null @@ -1,10 +0,0 @@ - \ No newline at end of file diff --git a/styles/pygments.xml b/styles/pygments.xml deleted file mode 100644 index a3d0d8b..0000000 --- a/styles/pygments.xml +++ /dev/null @@ -1,42 +0,0 @@ - \ No newline at end of file