2 unstable releases

0.2.0 May 14, 2023
0.1.0 May 1, 2023

#1454 in Text processing

Download history 12/week @ 2024-07-19 128/week @ 2024-07-26 113/week @ 2024-08-02 8/week @ 2024-08-30 32/week @ 2024-09-06 59/week @ 2024-09-13 121/week @ 2024-09-20 13/week @ 2024-09-27 17/week @ 2024-10-04 2/week @ 2024-10-11 77/week @ 2024-10-18 87/week @ 2024-10-25 70/week @ 2024-11-01

240 downloads per month

MIT license

20KB
315 lines

pygmentize

Latest Version Docs Pygmentize Version License

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).

Rendered HTML Output

Rendered example of examples/html.rs.

Rendered Example

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>

Rendered

(with the Dracula theme)

Rendered Example 2

Override Pygmentize Path

The path to the pygmentize binary, can be overridden using pygmentize::set_bin_path(). The default path is "pygmentize".

If pygmentize is installed in a virtual environment, within your crate directory, i.e. Cargo.lock and env/ being within the same directory. Then assuming that the current directory is the same. Then the path can be overridden by doing:

pygmentize::set_bin_path("./env/Scripts/pygmentize");

Install

The library is a wrapper around the pygmentize CLI, and as such it must be available in the system PATH. The easiest way to install pygmentize is through Python.

pip install Pygments

Dependencies

~0–6.5MB
~36K SLoC