mirror of
https://github.com/ralsina/tartrazine.git
synced 2024-12-05 00:00:34 +00:00
feat: higher level API (to_html
and to_ansi
)
Some checks failed
Tests / build (push) Has been cancelled
Some checks failed
Tests / build (push) Has been cancelled
This commit is contained in:
parent
6a38f2f5fb
commit
c011bd8347
29
README.md
29
README.md
@ -45,18 +45,35 @@ $ tartrazine whatever.c -t catppuccin-macchiato --line-numbers \
|
|||||||
|
|
||||||
## Usage as a Library
|
## Usage as a Library
|
||||||
|
|
||||||
This works:
|
This is the high level API:
|
||||||
|
|
||||||
```crystal
|
```crystal
|
||||||
require "tartrazine"
|
require "tartrazine"
|
||||||
|
|
||||||
lexer = Tartrazine.lexer("crystal")
|
html = Tartrazine.to_html(
|
||||||
theme = Tartrazine.theme("catppuccin-macchiato")
|
"puts \"Hello, world!\"",
|
||||||
formatter = Tartrazine::Html.new
|
language: "crystal",
|
||||||
formatter.theme = theme
|
theme: "catppuccin-macchiato",
|
||||||
puts formatter.format(File.read(ARGV[0]), lexer)
|
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
|
## Contributing
|
||||||
|
|
||||||
1. Fork it (<https://github.com/ralsina/tartrazine/fork>)
|
1. Fork it (<https://github.com/ralsina/tartrazine/fork>)
|
||||||
|
@ -69,6 +69,31 @@ describe Tartrazine do
|
|||||||
end
|
end
|
||||||
end
|
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(
|
||||||
|
"<pre class=\"b\" ><code class=\"b\"><span class=\"nb\">puts</span><span class=\"t\"> </span><span class=\"lss\">'Hello, World!'</span></code></pre>"
|
||||||
|
)
|
||||||
|
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
|
end
|
||||||
|
|
||||||
# Helper that creates lexer and tokenizes
|
# Helper that creates lexer and tokenizes
|
||||||
|
@ -1,6 +1,15 @@
|
|||||||
require "../formatter"
|
require "../formatter"
|
||||||
|
|
||||||
module Tartrazine
|
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
|
class Ansi < Formatter
|
||||||
property? line_numbers : Bool = false
|
property? line_numbers : Bool = false
|
||||||
|
|
||||||
|
@ -3,6 +3,17 @@ require "../formatter"
|
|||||||
require "html"
|
require "html"
|
||||||
|
|
||||||
module Tartrazine
|
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
|
class Html < Formatter
|
||||||
# property line_number_in_table : Bool = false
|
# property line_number_in_table : Bool = false
|
||||||
# property with_classes : Bool = true
|
# property with_classes : Bool = true
|
||||||
|
Loading…
Reference in New Issue
Block a user