Basic theme parser

This commit is contained in:
Roberto Alsina 2024-08-06 12:18:03 -03:00
parent 7392c8a74e
commit 3a37f8bdc7
5 changed files with 43 additions and 186 deletions

View File

@ -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")

View File

@ -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
}

View File

@ -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"]
)

View File

@ -1,10 +0,0 @@
<style name="onesenterprise">
<entry type="Keyword" style="#ff0000"/>
<entry type="Name" style="#0000ff"/>
<entry type="LiteralString" style="#000000"/>
<entry type="Operator" style="#ff0000"/>
<entry type="Punctuation" style="#ff0000"/>
<entry type="Comment" style="#008000"/>
<entry type="CommentPreproc" style="#963200"/>
<entry type="Text" style="#000000"/>
</style>

View File

@ -1,42 +0,0 @@
<style name="pygments">
<entry type="Error" style="border:#ff0000"/>
<entry type="Keyword" style="bold #008000"/>
<entry type="KeywordPseudo" style="nobold"/>
<entry type="KeywordType" style="nobold #b00040"/>
<entry type="NameAttribute" style="#7d9029"/>
<entry type="NameBuiltin" style="#008000"/>
<entry type="NameClass" style="bold #0000ff"/>
<entry type="NameConstant" style="#880000"/>
<entry type="NameDecorator" style="#aa22ff"/>
<entry type="NameEntity" style="bold #999999"/>
<entry type="NameException" style="bold #d2413a"/>
<entry type="NameFunction" style="#0000ff"/>
<entry type="NameLabel" style="#a0a000"/>
<entry type="NameNamespace" style="bold #0000ff"/>
<entry type="NameTag" style="bold #008000"/>
<entry type="NameVariable" style="#19177c"/>
<entry type="LiteralString" style="#ba2121"/>
<entry type="LiteralStringDoc" style="italic"/>
<entry type="LiteralStringEscape" style="bold #bb6622"/>
<entry type="LiteralStringInterpol" style="bold #bb6688"/>
<entry type="LiteralStringOther" style="#008000"/>
<entry type="LiteralStringRegex" style="#bb6688"/>
<entry type="LiteralStringSymbol" style="#19177c"/>
<entry type="LiteralNumber" style="#666666"/>
<entry type="Operator" style="#666666"/>
<entry type="OperatorWord" style="bold #aa22ff"/>
<entry type="Comment" style="italic #408080"/>
<entry type="CommentPreproc" style="noitalic #bc7a00"/>
<entry type="GenericDeleted" style="#a00000"/>
<entry type="GenericEmph" style="italic"/>
<entry type="GenericError" style="#ff0000"/>
<entry type="GenericHeading" style="bold #000080"/>
<entry type="GenericInserted" style="#00a000"/>
<entry type="GenericOutput" style="#888888"/>
<entry type="GenericPrompt" style="bold #000080"/>
<entry type="GenericStrong" style="bold"/>
<entry type="GenericSubheading" style="bold #800080"/>
<entry type="GenericTraceback" style="#0044dd"/>
<entry type="GenericUnderline" style="underline"/>
<entry type="TextWhitespace" style="#bbbbbb"/>
</style>