4 Commits

Author SHA1 Message Date
92a97490f1 bump: Release v0.9.1 2024-09-21 21:08:41 -03:00
22decedf3a test: added minimal tests for svg and png formatters
Some checks are pending
Tests / build (push) Waiting to run
2024-09-21 21:08:03 -03:00
8b34a1659d fix: Bug in high-level API for png formatter 2024-09-21 21:07:44 -03:00
3bf8172b89 fix: Terminal formatter was skipping things that it could highlight 2024-09-21 20:57:24 -03:00
5 changed files with 39 additions and 6 deletions

View File

@ -2,6 +2,17 @@
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
## [0.9.1] - 2024-09-22
### 🐛 Bug Fixes
- Terminal formatter was skipping things that it could highlight
- Bug in high-level API for png formatter
### 🧪 Testing
- Added minimal tests for svg and png formatters
## [0.9.0] - 2024-09-21 ## [0.9.0] - 2024-09-21
### 🚀 Features ### 🚀 Features

View File

@ -1,5 +1,5 @@
name: tartrazine name: tartrazine
version: 0.9.0 version: 0.9.1
authors: authors:
- Roberto Alsina <roberto.alsina@gmail.com> - Roberto Alsina <roberto.alsina@gmail.com>

View File

@ -1,4 +1,5 @@
require "./spec_helper" require "./spec_helper"
require "digest/sha1"
# These are the testcases from Pygments # These are the testcases from Pygments
testcases = Dir.glob("#{__DIR__}/tests/**/*txt").sort testcases = Dir.glob("#{__DIR__}/tests/**/*txt").sort
@ -103,6 +104,7 @@ describe Tartrazine do
) )
end end
end end
describe "to_ansi" do describe "to_ansi" do
it "should do basic highlighting" do it "should do basic highlighting" do
ansi = Tartrazine.to_ansi("puts 'Hello, World!'", "ruby") ansi = Tartrazine.to_ansi("puts 'Hello, World!'", "ruby")
@ -114,11 +116,29 @@ describe Tartrazine do
) )
else else
ansi.should eq( ansi.should eq(
"\e[38;2;171;70;66mputs\e[0m\e[38;2;216;216;216m \e[0m'Hello, World!'" "\e[38;2;171;70;66mputs\e[0m\e[38;2;216;216;216m \e[0m\e[38;2;161;181;108m'Hello, World!'\e[0m"
) )
end end
end end
end end
describe "to_svg" do
it "should do basic highlighting" do
svg = Tartrazine.to_svg("puts 'Hello, World!'", "ruby", standalone: false)
svg.should eq(
"<text x=\"0\" y=\"19\" xml:space=\"preserve\"><tspan fill=\"#ab4642\">puts</tspan><tspan fill=\"#d8d8d8\"> </tspan><tspan fill=\"#a1b56c\">&#39;Hello, World!&#39;</tspan></text>"
)
end
end
describe "to_png" do
it "should do basic highlighting" do
png = Digest::SHA1.hexdigest(Tartrazine.to_png("puts 'Hello, World!'", "ruby"))
png.should eq(
"62d419dcd263fffffc265a0f04c156dc2530c362"
)
end
end
end end
# Helper that creates lexer and tokenizes # Helper that creates lexer and tokenizes

View File

@ -34,8 +34,6 @@ module Tartrazine
end end
def colorize(text : String, token : String) : String def colorize(text : String, token : String) : String
style = theme.styles.fetch(token, nil)
return text if style.nil?
if theme.styles.has_key?(token) if theme.styles.has_key?(token)
s = theme.styles[token] s = theme.styles[token]
else else

View File

@ -1,16 +1,20 @@
require "../formatter" require "../formatter"
require "compress/gzip"
require "digest/sha1"
require "stumpy_png" require "stumpy_png"
require "stumpy_utils" require "stumpy_utils"
require "compress/gzip"
module Tartrazine module Tartrazine
def self.to_png(text : String, language : String, def self.to_png(text : String, language : String,
theme : String = "default-dark", theme : String = "default-dark",
line_numbers : Bool = false) : String line_numbers : Bool = false) : String
buf = IO::Memory.new
Tartrazine::Png.new( Tartrazine::Png.new(
theme: Tartrazine.theme(theme), theme: Tartrazine.theme(theme),
line_numbers: line_numbers line_numbers: line_numbers
).format(text, Tartrazine.lexer(name: language)) ).format(text, Tartrazine.lexer(name: language), buf)
buf.to_s
end end
class FontFiles class FontFiles