diff --git a/README.md b/README.md index ca812dc..a2a5130 100644 --- a/README.md +++ b/README.md @@ -45,18 +45,35 @@ $ tartrazine whatever.c -t catppuccin-macchiato --line-numbers \ ## Usage as a Library -This works: +This is the high level API: ```crystal require "tartrazine" -lexer = Tartrazine.lexer("crystal") -theme = Tartrazine.theme("catppuccin-macchiato") -formatter = Tartrazine::Html.new -formatter.theme = theme -puts formatter.format(File.read(ARGV[0]), lexer) +html = Tartrazine.to_html( + "puts \"Hello, world!\"", + language: "crystal", + theme: "catppuccin-macchiato", + standalone: true, + line_numbers: true +) ``` +This does more or less the same thing, but more manually: + +```crystal +lexer = Tartrazine.lexer("crystal") +formatter = Tartrazine::Html.new ( + theme: Tartrazine.theme("catppuccin-macchiato"), + line_numbers: true, + standalone: true, +) +puts formatter.format("puts \"Hello, world!\"", lexer) +``` + +The reason you may want to use the manual version is to reuse +the lexer and formatter objects for performance reasons. + ## Contributing 1. Fork it () diff --git a/spec/tartrazine_spec.cr b/spec/tartrazine_spec.cr index f768a9b..7e8d8c7 100644 --- a/spec/tartrazine_spec.cr +++ b/spec/tartrazine_spec.cr @@ -69,6 +69,31 @@ describe Tartrazine do end end end + + describe "to_html" do + it "should do basic highlighting" do + html = Tartrazine.to_html("puts 'Hello, World!'", "ruby", standalone: false) + html.should eq( + "
puts 'Hello, World!'
" + ) + end + end + describe "to_ansi" do + it "should do basic highlighting" do + ansi = Tartrazine.to_ansi("puts 'Hello, World!'", "ruby") + if ENV.fetch("CI", nil) + # In Github Actions there is no terminal so these don't + # really work + ansi.should eq( + "puts 'Hello, World!'" + ) + else + ansi.should eq( + "\e[38;2;171;70;66mputs\e[0m\e[38;2;216;216;216m \e[0m'Hello, World!'" + ) + end + end + end end # Helper that creates lexer and tokenizes diff --git a/src/formatters/ansi.cr b/src/formatters/ansi.cr index a2bbc60..4273906 100644 --- a/src/formatters/ansi.cr +++ b/src/formatters/ansi.cr @@ -1,6 +1,15 @@ require "../formatter" module Tartrazine + def self.to_ansi(text : String, language : String, + theme : String = "default-dark", + line_numbers : Bool = false) : String + Tartrazine::Ansi.new( + theme: Tartrazine.theme(theme), + line_numbers: line_numbers + ).format(text, Tartrazine.lexer(name: language)) + end + class Ansi < Formatter property? line_numbers : Bool = false diff --git a/src/formatters/html.cr b/src/formatters/html.cr index 05079b4..002b567 100644 --- a/src/formatters/html.cr +++ b/src/formatters/html.cr @@ -3,6 +3,17 @@ require "../formatter" require "html" module Tartrazine + def self.to_html(text : String, language : String, + theme : String = "default-dark", + standalone : Bool = true, + line_numbers : Bool = false) : String + Tartrazine::Html.new( + theme: Tartrazine.theme(theme), + standalone: standalone, + line_numbers: line_numbers + ).format(text, Tartrazine.lexer(name: language)) + end + class Html < Formatter # property line_number_in_table : Bool = false # property with_classes : Bool = true