Compare commits

...

2 Commits

Author SHA1 Message Date
5c074344d5 Added helper files 2024-08-09 10:30:20 -03:00
d3439563f2 Use new sixteen api 2024-08-09 10:25:24 -03:00
6 changed files with 63 additions and 47 deletions

7
Makefile Normal file
View File

@ -0,0 +1,7 @@
build: $(wildcard src/**/*.cr) $(wildcard lexers/*xml) $(wildcard styles/*xml) shard.yml
shards build -Dstrict_multi_assign -Dno_number_autocast
release: $(wildcard src/**/*.cr) $(wildcard lexers/*xml) $(wildcard styles/*xml) shard.yml
shards build --release
static: $(wildcard src/**/*.cr) $(wildcard lexers/*xml) $(wildcard styles/*xml) shard.yml
shards build --release --static
strip bin/tartrazine

16
build_static.sh Executable file
View File

@ -0,0 +1,16 @@
#!/bin/bash
set -e
docker run --rm --privileged \
multiarch/qemu-user-static \
--reset -p yes
# Build for AMD64
docker build . -f Dockerfile.static -t tartrazine-builder
docker run -ti --rm -v "$PWD":/app --user="$UID" tartrazine-builder /bin/sh -c "cd /app && rm -rf lib shard.lock && make static"
mv bin/tartrazine bin/tartrazine-static-linux-amd64
# Build for ARM64
docker build . -f Dockerfile.static --platform linux/arm64 -t tartrazine-builder
docker run -ti --rm -v "$PWD":/app --platform linux/arm64 --user="$UID" tartrazine-builder /bin/sh -c "cd /app && rm -rf lib shard.lock && make static"
mv bin/tartrazine bin/tartrazine-static-linux-arm64

View File

@ -15,7 +15,6 @@ dependencies:
github: crystal-china/base58.cr github: crystal-china/base58.cr
sixteen: sixteen:
github: ralsina/sixteen github: ralsina/sixteen
branch: main
crystal: ">= 1.13.0" crystal: ">= 1.13.0"

View File

@ -44,21 +44,13 @@ module Tartrazine
theme.styles.has_key?(parent) theme.styles.has_key?(parent)
}] }]
end end
colorized = text.colorize(*rgb(s.color)) colorized = text.colorize(s.color.try &.colorize)
# Intentionally not setting background color # Intentionally not setting background color
colorized.mode(:bold) if s.bold colorized.mode(:bold) if s.bold
colorized.mode(:italic) if s.italic colorized.mode(:italic) if s.italic
colorized.mode(:underline) if s.underline colorized.mode(:underline) if s.underline
colorized.to_s colorized.to_s
end end
def rgb(c : String?)
return {0_u8, 0_u8, 0_u8} unless c
r = c[0..1].to_u8(16)
g = c[2..3].to_u8(16)
b = c[4..5].to_u8(16)
{r, g, b}
end
end end
class Html < Formatter class Html < Formatter
@ -83,9 +75,9 @@ module Tartrazine
theme.styles.each do |token, style| theme.styles.each do |token, style|
outp << ".#{get_css_class(token, theme)} {" outp << ".#{get_css_class(token, theme)} {"
# These are set or nil # These are set or nil
outp << "color: #{style.color};" if style.color outp << "color: #{style.color.try &.hex};" if style.color
outp << "background-color: #{style.background};" if style.background outp << "background-color: #{style.background.try &.hex};" if style.background
outp << "border: 1px solid #{style.border};" if style.border outp << "border: 1px solid #{style.border.try &.hex};" if style.border
# These are true/false/nil # These are true/false/nil
outp << "border: none;" if style.border == false outp << "border: none;" if style.border == false

View File

@ -8,6 +8,8 @@ require "sixteen"
require "xml" require "xml"
module Tartrazine module Tartrazine
alias Color = Sixteen::Color
def self.theme(name : String) : Theme def self.theme(name : String) : Theme
return Theme.from_base16(name[7..]) if name.starts_with? "base16_" return Theme.from_base16(name[7..]) if name.starts_with? "base16_"
Theme.from_xml(ThemeFiles.get("/#{name}.xml").gets_to_end) Theme.from_xml(ThemeFiles.get("/#{name}.xml").gets_to_end)
@ -29,9 +31,9 @@ module Tartrazine
# These properties are either set or nil # These properties are either set or nil
# (inherit from parent style) # (inherit from parent style)
property background : String? property background : Color?
property border : String? property border : Color?
property color : String? property color : Color?
# Styles are incomplete by default and inherit # Styles are incomplete by default and inherit
# from parents. If this is true, this style # from parents. If this is true, this style
@ -101,33 +103,33 @@ module Tartrazine
# 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/
theme.styles["Background"] = Style.new(color: t.palette["base05"], background: t.palette["base00"]) theme.styles["Background"] = Style.new(color: t["base05"], background: t["base00"])
theme.styles["Text"] = Style.new(color: t.palette["base05"]) theme.styles["Text"] = Style.new(color: t["base05"])
theme.styles["Error"] = Style.new(color: t.palette["base08"]) theme.styles["Error"] = Style.new(color: t["base08"])
theme.styles["Comment"] = Style.new(color: t.palette["base03"]) theme.styles["Comment"] = Style.new(color: t["base03"])
theme.styles["CommentPreproc"] = Style.new(color: t.palette["base0F"]) theme.styles["CommentPreproc"] = Style.new(color: t["base0F"])
theme.styles["CommentPreprocFile"] = Style.new(color: t.palette["base0B"]) theme.styles["CommentPreprocFile"] = Style.new(color: t["base0B"])
theme.styles["Keyword"] = Style.new(color: t.palette["base0E"]) theme.styles["Keyword"] = Style.new(color: t["base0E"])
theme.styles["KeywordType"] = Style.new(color: t.palette["base08"]) theme.styles["KeywordType"] = Style.new(color: t["base08"])
theme.styles["NameAttribute"] = Style.new(color: t.palette["base0D"]) theme.styles["NameAttribute"] = Style.new(color: t["base0D"])
theme.styles["NameBuiltin"] = Style.new(color: t.palette["base08"]) theme.styles["NameBuiltin"] = Style.new(color: t["base08"])
theme.styles["NameBuiltinPseudo"] = Style.new(color: t.palette["base08"]) theme.styles["NameBuiltinPseudo"] = Style.new(color: t["base08"])
theme.styles["NameClass"] = Style.new(color: t.palette["base0D"]) theme.styles["NameClass"] = Style.new(color: t["base0D"])
theme.styles["NameConstant"] = Style.new(color: t.palette["base09"]) theme.styles["NameConstant"] = Style.new(color: t["base09"])
theme.styles["NameDecorator"] = Style.new(color: t.palette["base09"]) theme.styles["NameDecorator"] = Style.new(color: t["base09"])
theme.styles["NameFunction"] = Style.new(color: t.palette["base0D"]) theme.styles["NameFunction"] = Style.new(color: t["base0D"])
theme.styles["NameNamespace"] = Style.new(color: t.palette["base0D"]) theme.styles["NameNamespace"] = Style.new(color: t["base0D"])
theme.styles["NameTag"] = Style.new(color: t.palette["base0E"]) theme.styles["NameTag"] = Style.new(color: t["base0E"])
theme.styles["NameVariable"] = Style.new(color: t.palette["base0D"]) theme.styles["NameVariable"] = Style.new(color: t["base0D"])
theme.styles["NameVariableInstance"] = Style.new(color: t.palette["base08"]) theme.styles["NameVariableInstance"] = Style.new(color: t["base08"])
theme.styles["LiteralNumber"] = Style.new(color: t.palette["base09"]) theme.styles["LiteralNumber"] = Style.new(color: t["base09"])
theme.styles["Operator"] = Style.new(color: t.palette["base0C"]) theme.styles["Operator"] = Style.new(color: t["base0C"])
theme.styles["OperatorWord"] = Style.new(color: t.palette["base0E"]) theme.styles["OperatorWord"] = Style.new(color: t["base0E"])
theme.styles["Literal"] = Style.new(color: t.palette["base0B"]) theme.styles["Literal"] = Style.new(color: t["base0B"])
theme.styles["LiteralString"] = Style.new(color: t.palette["base0B"]) theme.styles["LiteralString"] = Style.new(color: t["base0B"])
theme.styles["LiteralStringInterpol"] = Style.new(color: t.palette["base0F"]) theme.styles["LiteralStringInterpol"] = Style.new(color: t["base0F"])
theme.styles["LiteralStringRegex"] = Style.new(color: t.palette["base0C"]) theme.styles["LiteralStringRegex"] = Style.new(color: t["base0C"])
theme.styles["LiteralStringSymbol"] = Style.new(color: t.palette["base09"]) theme.styles["LiteralStringSymbol"] = Style.new(color: t["base09"])
theme theme
end end
@ -154,9 +156,9 @@ module Tartrazine
s.underline = true if style.includes?("underline") s.underline = true if style.includes?("underline")
s.underline = false if style.includes?("nounderline") s.underline = false if style.includes?("nounderline")
s.color = style.find(&.starts_with?("#")).try &.split("#").last s.color = style.find(&.starts_with?("#")).try { |v| Color.new v.split("#").last }
s.background = style.find(&.starts_with?("bg:#")).try &.split("#").last s.background = style.find(&.starts_with?("bg:#")).try { |v| Color.new v.split("#").last }
s.border = style.find(&.starts_with?("border:#")).try &.split("#").last s.border = style.find(&.starts_with?("border:#")).try { |v| Color.new v.split("#").last }
theme.styles[node["type"]] = s theme.styles[node["type"]] = s
end end