Crate pygmentize
source ·Expand description
Rust library and wrapper around the pygmentize CLI. Apply syntax highlighting to over 500 languages and other text formatted. Render into HTML, SVG, LaTeX, and Terminal (ANSI color sequences).
Example
use pygmentize::{HtmlFormatter, PygmentizeError};
let code = r#"fn main() {
println!("Hello, world!");
}"#;
let html = pygmentize::highlight(code, Some("rust"), &HtmlFormatter::default())?;
println!("{html}");Output
(whitespace added to improve clarity)
<div class="highlight">
<pre>
<span></span>
<span class="k">fn</span>
<span class="nf">main</span>
<span class="p">()</span>
<span class="w"> </span>
<span class="p">{</span>
<span class="w"> </span>
<span class="fm">println!</span>
<span class="p">(</span>
<span class="s">"Hello, world!"</span>
<span class="p">);</span>
<span class="p">}</span>
</pre>
</div>
Structs
- Format tokens as HTML 4
<span>tags. - Format tokens as LaTeX code. This needs the
fancyvrbandcolorstandard packages. - Format tokens as an SVG graphics file. This formatter is still experimental. Each line of code is a
<text>element with explicit x and y coordinates containing<tspan>elements with the individual token styles. - Format tokens with ANSI color sequences, for output in a 256-color terminal or console. Like in
TerminalFormattercolor sequences are terminated at newlines, so that paging the output works correctly. - Format tokens with ANSI color sequences, for output in a text console. Color sequences are terminated at newlines, so that paging the output works correctly.
- Format tokens with ANSI color sequences, for output in a true-color terminal or console. Like in
TerminalFormattercolor sequences are terminated at newlines, so that paging the output works correctly.
Enums
Traits
- Want to implement a formatter or add unsupported options?
Functions
- Applies syntax highlighting to
codewritten inlang, and outputs in the format ofF:PygmentizeFormatter.