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">&quot;Hello, world!&quot;</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 fancyvrb and color standard 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 TerminalFormatter color 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 TerminalFormatter color sequences are terminated at newlines, so that paging the output works correctly.

Enums

Traits

Functions