A small library and command-line utility for rendering highlighted source code as Scalable Vector Graphics (SVG).
Built in Rust using Tree-sitter for parsing, and Pango and Cairo for text rendering.
Warning
Leafpress is currently unstable and a work in progress. APIs and behaviour may change without notice.
- Portable SVG output. Unicode text is rendered as paths without font dependencies.
- Compile-time parser integration.
- Runtime parser integration (dynamic shared library and scheme files).
- Large collection of built-in Base16 colour schemes sourced from tinted-theming.
- Custom user-defined themes.
- Custom user-defined highlight maps.
- Text decorations (bold, italic, underline, undercurl,
strikethrough). - Supports font ligatures.
- User-defined kerning and line spacing.
- User-defined image height and width
- Language injection
- More and better tests
- A better to do list
TBD
Clone this repository and run the following commands.
cargo build --release -p leafpressLeafpress cannot compile tree-sitter grammars. It is recommended that you also install tree-sitter-cli and its associated dependencies (incl. C compiler and JavaScript runtime).
cargo build --release -p leafpress-cli
cargo install --path crate/leafpress-cliLeafpress supports both compile-time and runtime parser loading. Compile-time loading is more convenient when the required grammar is known at build time. Runtime loading allows grammars and highlight queries to be selected dynamically, making it better suited to generic Tree-sitter tooling.
use std::path::Path;
use leafpress::{generate_svg, render::Format, theme};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let output_path = Path::new("./output.svg");
let source = br#"package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}
"#;
let language = tree_sitter_go::LANGUAGE.into();
let query = tree_sitter::Query::new(&language, tree_sitter_go::HIGHLIGHTS_QUERY)?;
let palette = theme::GITHUB_DARK;
let format = Format::default();
generate_svg(
output_path,
source,
&language,
&query,
&palette,
&format,
None,
)?;
Ok(())
}use std::path::Path;
use leafpress::{generate_svg, parser, render::Format, theme};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let output_path = Path::new("./output.svg");
let source = br#"package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}
"#;
let loaded = parser::load_dynamic("./go.so")?;
let query = parser::load_query("./highlights.scm", &loaded.language)?;
let palette = theme::GITHUB_DARK;
let format = Format::default();
generate_svg(
output_path,
source,
&loaded.language,
&query,
&palette,
&format,
None,
)?;
Ok(())
}Render a source file to SVG:
leafpress render main.go --lang goThe output is written to ./output.svg by default. Use -o to specify a different path:
leafpress render main.go --lang go -o main.svgFormatting can be optionally customised with --theme, --font, --size, and --padding:
leafpress render main.go \
--lang go \
--theme "Github Dark"
--font "Iosevka Term" \
--size 14 \
--padding 20List available themes:
leafpress list-themesList available Tree-sitter grammars:
leafpress list-grammarsBoth listing commands support JSON output with --json.
By default, grammars are located using the Tree-sitter configuration file. Use --grammar-dir to specify a parser directory directly:
leafpress render main.go --lang go --grammar-dir ~/src/tree-sitter-grammarsThe grammar directory and Tree-sitter configuration path can also be set with TREESITTER_GRAMMAR_DIR and TREESITTER_CONFIG_PATH, respectively.