From 94bc2215453aabaac75a5b8fb01926ed1a8abd2e Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Tue, 6 Aug 2024 18:03:05 -0300 Subject: [PATCH] Added test binary, code to read base16 themes --- shard.yml | 4 +++- src/formatter.cr | 4 ---- src/main.cr | 5 +++++ src/styles.cr | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 src/main.cr diff --git a/shard.yml b/shard.yml index 8e3d63b..2847f3b 100644 --- a/shard.yml +++ b/shard.yml @@ -6,11 +6,13 @@ authors: targets: tartrazine: - main: src/tartrazine.cr + main: src/main.cr dependencies: base58: github: crystal-china/base58.cr + sixteen: + github: ralsina/sixteen crystal: ">= 1.13.0" diff --git a/src/formatter.cr b/src/formatter.cr index 5f0fb8a..daecb05 100644 --- a/src/formatter.cr +++ b/src/formatter.cr @@ -66,7 +66,3 @@ module Tartrazine end end end - -lexer = Tartrazine.lexer("crystal") -theme = Tartrazine.theme("catppuccin-macchiato") -puts Tartrazine::Html.new.format(File.read(ARGV[0]), lexer, theme) diff --git a/src/main.cr b/src/main.cr new file mode 100644 index 0000000..f5fc33a --- /dev/null +++ b/src/main.cr @@ -0,0 +1,5 @@ +require "./**" + +lexer = Tartrazine.lexer("crystal") +theme = Tartrazine.theme("catppuccin-macchiato") +puts Tartrazine::Html.new.format(File.read(ARGV[0]), lexer, theme) diff --git a/src/styles.cr b/src/styles.cr index ea609dd..e0dd222 100644 --- a/src/styles.cr +++ b/src/styles.cr @@ -1,3 +1,4 @@ +require "sixteen" require "xml" module Tartrazine @@ -78,6 +79,41 @@ module Tartrazine parents end + # Load from a base16 theme name using Sixteen + def self.from_base16(name : String) : Theme + t = Sixteen.theme(name) + # The color assignments are adapted from + # https://github.com/mohd-akram/base16-pygments/ + + t.styles["Background"] = Style.new(color: t["base05"], background: t["base00"]) + t.styles["Text"] = Style.new(color: t["base05"]) + t.styles["Error"] = Style.new(color: t["base08"]) + t.styles["Comment"] = Style.new(color: t["base03"]) + t.styles["CommentPreProc"] = Style.new(color: t["base0f"]) + t.styles["CommentPreProcFile"] = Style.new(color: t["base0b"]) + t.styles["Keyword"] = Style.new(color: t["base0e"]) + t.styles["KeywordType"] = Style.new(color: t["base08"]) + t.styles["NameAttribute"] = Style.new(color: t["base0d"]) + t.styles["NameBuiltin"] = Style.new(color: t["base08"]) + t.styles["NameBuiltinPseudo"] = Style.new(color: t["base08"]) + t.styles["NameClass"] = Style.new(color: t["base0d"]) + t.styles["NameConstant"] = Style.new(color: t["base09"]) + t.styles["NameDecorator"] = Style.new(color: t["base09"]) + t.styles["NameFunction"] = Style.new(color: t["base0d"]) + t.styles["NameNamespace"] = Style.new(color: t["base0d"]) + t.styles["NameTag"] = Style.new(color: t["base0e"]) + t.styles["NameVariable"] = Style.new(color: t["base0d"]) + t.styles["NameVariableInstance"] = Style.new(color: t["base08"]) + t.styles["Number"] = Style.new(color: t["base09"]) + t.styles["Operator"] = Style.new(color: t["base0c"]) + t.styles["OperatorWord"] = Style.new(color: t["base0e"]) + t.styles["Literal"] = Style.new(color: t["base0b"]) + t.styles["String"] = Style.new(color: t["base0b"]) + t.styles["StringInterpol"] = Style.new(color: t["base0f"]) + t.styles["StringRegex"] = Style.new(color: t["base0c"]) + t.styles["StringSymbol"] = Style.new(color: t["base09"]) + end + # Load from a Chroma XML file def self.from_xml(xml : String) : Theme document = XML.parse(xml)