mirror of
https://github.com/ralsina/tartrazine.git
synced 2025-07-01 20:37:08 -03:00
Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
c8de4c2194 | |||
d1ce83a9c8 | |||
aabe028767 | |||
ed61a84553 | |||
b7e4aaa1f9 | |||
0f6b9b0117 | |||
b81e9c4405 |
2
.github/workflows/coverage.yml
vendored
2
.github/workflows/coverage.yml
vendored
@ -15,7 +15,7 @@ jobs:
|
|||||||
uses: crystal-lang/install-crystal@v1
|
uses: crystal-lang/install-crystal@v1
|
||||||
- name: Run tests using kcov
|
- name: Run tests using kcov
|
||||||
run: |
|
run: |
|
||||||
sudo apt update && sudo apt install kcov
|
sudo apt update && sudo apt upgrade && sudo apt install -y kcov
|
||||||
wget https://github.com/alecthomas/chroma/releases/download/v2.14.0/chroma-2.14.0-linux-amd64.tar.gz
|
wget https://github.com/alecthomas/chroma/releases/download/v2.14.0/chroma-2.14.0-linux-amd64.tar.gz
|
||||||
tar xzvf chroma-2.14.0*gz
|
tar xzvf chroma-2.14.0*gz
|
||||||
mkdir ~/.local/bin -p
|
mkdir ~/.local/bin -p
|
||||||
|
17
CHANGELOG.md
17
CHANGELOG.md
@ -2,6 +2,23 @@
|
|||||||
|
|
||||||
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.13.0] - 2025-03-10
|
||||||
|
|
||||||
|
### 🚀 Features
|
||||||
|
|
||||||
|
- Support custom template for HTML standalone output
|
||||||
|
|
||||||
|
### 🐛 Bug Fixes
|
||||||
|
|
||||||
|
- Better error message when loading a XML theme
|
||||||
|
- When the internal crystal highlighter fails, fallback to ruby. Fixes #13
|
||||||
|
- Don't log when falling back to ruby, it breaks stuff
|
||||||
|
|
||||||
|
### ⚙️ Miscellaneous Tasks
|
||||||
|
|
||||||
|
- Upgrade ci image
|
||||||
|
- Typo
|
||||||
|
|
||||||
## [0.12.0] - 2025-01-21
|
## [0.12.0] - 2025-01-21
|
||||||
|
|
||||||
### 🚀 Features
|
### 🚀 Features
|
||||||
|
27
README.md
27
README.md
@ -127,6 +127,33 @@ pico
|
|||||||
|
|
||||||
Be careful not to build without any themes at all, nothing will work.
|
Be careful not to build without any themes at all, nothing will work.
|
||||||
|
|
||||||
|
## Templates for standalone HTML output
|
||||||
|
|
||||||
|
If you are using the HTML formatter, you can pass a template to use for the output. The template is a string where the following placeholders will be replaced:
|
||||||
|
|
||||||
|
* `{{style_defs}}` will be replaced by the CSS styles needed for the theme
|
||||||
|
* `{{code}}` will be replaced by the highlighted code
|
||||||
|
|
||||||
|
This is an example template that changes the padding around the code:
|
||||||
|
|
||||||
|
```jinja2
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<style>
|
||||||
|
{{style_defs}}
|
||||||
|
pre {
|
||||||
|
padding: 1em;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
{{body}}
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
1. Fork it (<https://github.com/ralsina/tartrazine/fork>)
|
1. Fork it (<https://github.com/ralsina/tartrazine/fork>)
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
name: tartrazine
|
name: tartrazine
|
||||||
version: 0.12.0
|
version: 0.13.0
|
||||||
|
|
||||||
authors:
|
authors:
|
||||||
- Roberto Alsina <roberto.alsina@gmail.com>
|
- Roberto Alsina <roberto.alsina@gmail.com>
|
||||||
|
@ -28,6 +28,13 @@ module Tartrazine
|
|||||||
property? surrounding_pre : Bool = true
|
property? surrounding_pre : Bool = true
|
||||||
property? wrap_long_lines : Bool = false
|
property? wrap_long_lines : Bool = false
|
||||||
property weight_of_bold : Int32 = 600
|
property weight_of_bold : Int32 = 600
|
||||||
|
property template : String = <<-TEMPLATE
|
||||||
|
<!DOCTYPE html><html><head><style>
|
||||||
|
{{style_defs}}
|
||||||
|
</style></head><body>
|
||||||
|
{{body}}
|
||||||
|
</body></html>
|
||||||
|
TEMPLATE
|
||||||
|
|
||||||
property theme : Theme
|
property theme : Theme
|
||||||
|
|
||||||
@ -42,7 +49,8 @@ module Tartrazine
|
|||||||
@standalone : Bool = false,
|
@standalone : Bool = false,
|
||||||
@surrounding_pre : Bool = true,
|
@surrounding_pre : Bool = true,
|
||||||
@wrap_long_lines : Bool = false,
|
@wrap_long_lines : Bool = false,
|
||||||
@weight_of_bold : Int32 = 600)
|
@weight_of_bold : Int32 = 600,
|
||||||
|
@template : String = @template)
|
||||||
end
|
end
|
||||||
|
|
||||||
def format(text : String, lexer : Lexer) : String
|
def format(text : String, lexer : Lexer) : String
|
||||||
@ -61,11 +69,15 @@ module Tartrazine
|
|||||||
# Wrap text into a full HTML document, including the CSS for the theme
|
# Wrap text into a full HTML document, including the CSS for the theme
|
||||||
def wrap_standalone
|
def wrap_standalone
|
||||||
output = String.build do |outp|
|
output = String.build do |outp|
|
||||||
outp << "<!DOCTYPE html><html><head><style>"
|
if @template.includes? "{{style_defs}}"
|
||||||
outp << style_defs
|
outp << @template.split("{{style_defs}}")[0]
|
||||||
outp << "</style></head><body>"
|
outp << style_defs
|
||||||
|
outp << @template.split("{{style_defs}}")[1].split("{{body}}")[0]
|
||||||
|
else
|
||||||
|
outp << @template.split("{{body}}")[0]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
{output.to_s, "</body></html>"}
|
{output.to_s, @template.split("{{body}}")[1]}
|
||||||
end
|
end
|
||||||
|
|
||||||
private def line_label(i : Int32) : String
|
private def line_label(i : Int32) : String
|
||||||
|
@ -350,6 +350,13 @@ module Tartrazine
|
|||||||
class CustomCrystalHighlighter < Crystal::SyntaxHighlighter
|
class CustomCrystalHighlighter < Crystal::SyntaxHighlighter
|
||||||
@tokens = [] of Token
|
@tokens = [] of Token
|
||||||
|
|
||||||
|
def highlight(text)
|
||||||
|
super
|
||||||
|
rescue ex : Crystal::SyntaxException
|
||||||
|
# Fallback to Ruby highlighting
|
||||||
|
@tokens = Tartrazine.lexer("ruby").tokenizer(text).to_a
|
||||||
|
end
|
||||||
|
|
||||||
def render_delimiter(&block)
|
def render_delimiter(&block)
|
||||||
@tokens << {type: "LiteralString", value: block.call.to_s}
|
@tokens << {type: "LiteralString", value: block.call.to_s}
|
||||||
end
|
end
|
||||||
|
12
src/main.cr
12
src/main.cr
@ -10,8 +10,8 @@ Keep in mind that not all formatters support all features.
|
|||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
tartrazine (-h, --help)
|
tartrazine (-h, --help)
|
||||||
tartrazine FILE -f html [-t theme][--standalone][--line-numbers]
|
tartrazine FILE -f html [-t theme][--standalone [--template file]]
|
||||||
[-l lexer][-o output]
|
[--line-numbers][-l lexer][-o output]
|
||||||
tartrazine -f html -t theme --css
|
tartrazine -f html -t theme --css
|
||||||
tartrazine FILE -f terminal [-t theme][-l lexer][--line-numbers]
|
tartrazine FILE -f terminal [-t theme][-l lexer][--line-numbers]
|
||||||
[-o output]
|
[-o output]
|
||||||
@ -35,6 +35,7 @@ Options:
|
|||||||
all style information. If not given, it will generate just
|
all style information. If not given, it will generate just
|
||||||
a HTML fragment ready to include in your own page.
|
a HTML fragment ready to include in your own page.
|
||||||
--css Generate a CSS file for the theme called <theme>.css
|
--css Generate a CSS file for the theme called <theme>.css
|
||||||
|
--template <file> Use a custom template for the HTML output [default: none]
|
||||||
--line-numbers Include line numbers in the output
|
--line-numbers Include line numbers in the output
|
||||||
-h, --help Show this screen
|
-h, --help Show this screen
|
||||||
-v, --version Show version number
|
-v, --version Show version number
|
||||||
@ -64,6 +65,12 @@ if options["--list-formatters"]
|
|||||||
end
|
end
|
||||||
|
|
||||||
theme = Tartrazine.theme(options["-t"].as(String))
|
theme = Tartrazine.theme(options["-t"].as(String))
|
||||||
|
template = options["--template"].as(String)
|
||||||
|
if template != "none" # Otherwise we will use the default template
|
||||||
|
template = File.open(template).gets_to_end
|
||||||
|
else
|
||||||
|
template = nil
|
||||||
|
end
|
||||||
|
|
||||||
if options["-f"]
|
if options["-f"]
|
||||||
formatter = options["-f"].as(String)
|
formatter = options["-f"].as(String)
|
||||||
@ -73,6 +80,7 @@ if options["-f"]
|
|||||||
formatter.standalone = options["--standalone"] != nil
|
formatter.standalone = options["--standalone"] != nil
|
||||||
formatter.line_numbers = options["--line-numbers"] != nil
|
formatter.line_numbers = options["--line-numbers"] != nil
|
||||||
formatter.theme = theme
|
formatter.theme = theme
|
||||||
|
formatter.template = template if template
|
||||||
when "terminal"
|
when "terminal"
|
||||||
formatter = Tartrazine::Ansi.new
|
formatter = Tartrazine::Ansi.new
|
||||||
formatter.line_numbers = options["--line-numbers"] != nil
|
formatter.line_numbers = options["--line-numbers"] != nil
|
||||||
|
@ -35,8 +35,8 @@ module Tartrazine
|
|||||||
end
|
end
|
||||||
begin
|
begin
|
||||||
Theme.from_xml(ThemeFiles.get("/#{name}.xml").gets_to_end)
|
Theme.from_xml(ThemeFiles.get("/#{name}.xml").gets_to_end)
|
||||||
rescue
|
rescue ex : Exception
|
||||||
raise Exception.new("Theme #{name} not found")
|
raise Exception.new("Error loading theme #{name}: #{ex.message}")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user