Skip to main content

rustgments

A Rust engine for Pygments. It runs Pygments' own lexer tables and produces the same tokens and the same HTML, byte for byte. A lexer or option it can't reproduce exactly goes to Pygments itself, so output never changes.

import rustgments

lexer = rustgments.get_lexer_by_name('python')
html = rustgments.highlight(code, lexer, rustgments.HtmlFormatter())

Or leave your code alone and make Pygments itself use it:

import rustgments
rustgments.install()   # pygments.highlight() and friends now use the native path

When it helps

It helps when one process highlights a lot of code. Python startup and imports are the same either way, so a one-shot script on a small file gains little.

Workload Pygments rustgments
highlight() called again in the same process, 20 KB - 1.2 MB files 5 - 1,600 ms 0.2 - 53 ms 12 - 75x
First highlight() of a process, same files 14 - 1,588 ms 0.3 - 52 ms 7 - 54x
4,226 files up to 20 KB through pygments.highlight after install(), highlight loop 32.55 s 2.23 s 14.6x
Same, whole process 33.41 s 3.31 s 10.1x
One file, whole process from start to exit, 20 KB - 1.2 MB 0.14 - 1.7 s 0.09 - 0.18 s 1.1 - 9.5x
Django docs with Sphinx (4,199 code blocks) 109.5 s 100.7 s within noise

The Sphinx row is the honest one for documentation builds: highlighting was 3.8 s of a 113 s build, and it dropped to 1.8 s. Sphinx spends its time elsewhere.

The first use of each lexer costs a little more than in Pygments (median 3.8 ms against 2.7 ms over 361 lexers; a few very large lexers take 100-300 ms), so a short run that touches many languages a few times each can come out slower.

Windows 11, i7-9700, CPython 3.11-3.12, base and rustgments alternating. Raw results are in bench/results.

What runs natively

363 of Pygments' 602 lexers, including Python, JavaScript, TypeScript, JSON, Bash, Rust, Go, Java, Kotlin, HTML, CSS, XML, TOML, INI, Dockerfile, PowerShell, Perl and diff. C, C++, YAML, Markdown, SQL, Ruby, PHP and Swift still run on Pygments for now.

A lexer stays on Pygments when it isn't a plain RegexLexer table (hand-written lexers, custom callbacks, overridden tokenizing), when Pygments has no test files to check it against, or when lexer options change its behaviour. lexer.backend tells you which one you got, and lexer.reason says why:

>>> rustgments.get_lexer_by_name('c').backend
'fallback'

HtmlFormatter runs natively with the options Sphinx and pymdownx use (cssclass, nowrap, linenos variants, hl_lines, wrapcode, linespans, lineanchors, noclasses, style, ...). Other formatters, and tagsfile, are plain Pygments.

Requirements

Python 3.9 - 3.14 and Pygments 2.21.0. The tables are generated from that exact release, and Python's \w, \d, \s follow the interpreter's Unicode version, so there is one table set per Unicode version. With any other Pygments, or a Python it has no tables for, everything runs on Pygments: correct, just not faster.

Install

pip install rustgments

Wheels are built for Windows, Linux (glibc and musl, x86_64 and aarch64) and macOS (x86_64 and arm64), for any Python from 3.9. Elsewhere pip builds from source, which needs a Rust toolchain.

Using it with existing tools

Your own code rustgments.install(); rustgments.uninstall() restores Pygments
Sphinx extensions = ['rustgments.sphinxext']
MkDocs plugins: [rustgments]
Anything else set RUSTGMENTS_INSTALL=1

The environment variable works through a one-line .pth file in the wheel. It runs at every Python startup, and all it does is check that variable. Delete rustgments-autoinstall.pth from site-packages if you'd rather not have it.

install() patches get_tokens, get_tokens_unprocessed of the native lexers and the per-token loop of HtmlFormatter. Line numbers, highlighted lines, anchors and wrapping still run Pygments' own code.

How parity is checked

Parity is the point of this project, so a lexer only runs natively after passing a gate on every supported Python:

  • tests/gate.py: each lexer's own Pygments test files (tests/examplefiles, tests/snippets) plus 30 generated mutants, comparing preprocessing, every (offset, token, value) and the final HTML. Any mismatch, engine error or timeout keeps that lexer on Pygments. The result is build_ir/native_gate.json.
  • tests/parity.py: the same comparisons over a corpus of real code (5,171 files, 88 MB, for the five core lexers).
  • tests/independent_check.py: a second checker using only the public API, on other files and random mutants.
  • tests/dropin_check.py: Pygments' API before and after install(), with 23 formatter and lexer call shapes.
  • tests/unicode_check.py: the 9,917 characters whose classes changed between Unicode versions, inserted into real code.

The corpus isn't in this repository. Point RUSTGMENTS_CORPUS at a directory with python/, json/, js/, bash/ and rust/ subdirectories, and RUSTGMENTS_PYGMENTS_SRC at a checkout of Pygments 2.21.0.

CI builds every wheel and runs Pygments' own test inputs against it.

How it works

A build step imports Pygments and serializes each lexer's processed token tables, so include, inherit, words() and friends are already resolved. Every pattern is re-emitted from Python's own regex parse tree into Rust syntax, with Python's semantics spelled out (\w, \d, \s, \Z, $, flags). At run time the engine matches with DFAs where it can and falls back to fancy-regex for look-around and back-references, following RegexLexer.get_tokens_unprocessed step by step.

build_ir/ regenerates the tables in ir/, which are checked in.

Status

Early. Version 0.1.0 tracks Pygments 2.21.0 only, and the API is the small part of Pygments listed above. Please open an issue if the output ever differs from Pygments: that's the bug this project cares about most.

Prior art: the pygmentsrs crate inside westurner/dsport takes a similar approach for a docutils port. No code from it is used here.

License

BSD-2-Clause, see LICENSE. The generated tables are derived from Pygments (LICENSE-pygments). Rust crate licenses are in LICENSE-third-party.txt.

Metadata

Release files for rustgments 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rustgments 0.1.0
File Size Uploaded Provenance Actions
rustgments-0.1.0.tar.gz 2.6 MB With provenance Details for rustgments-0.1.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for rustgments 0.1.0
File Actions
rustgments-0.1.0-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details for rustgments-0.1.0-cp39-abi3-win_amd64.whl
rustgments-0.1.0-cp39-abi3-musllinux_1_2_x86_64.whl CPython 3.9 abi3 Linux musl 1.2+ x86-64 Details for rustgments-0.1.0-cp39-abi3-musllinux_1_2_x86_64.whl
rustgments-0.1.0-cp39-abi3-musllinux_1_2_aarch64.whl CPython 3.9 abi3 Linux musl 1.2+ ARM64 Details for rustgments-0.1.0-cp39-abi3-musllinux_1_2_aarch64.whl
rustgments-0.1.0-cp39-abi3-manylinux_2_28_x86_64.whl CPython 3.9 abi3 Linux glibc 2.28+ x86-64 Details for rustgments-0.1.0-cp39-abi3-manylinux_2_28_x86_64.whl
rustgments-0.1.0-cp39-abi3-manylinux_2_28_aarch64.whl CPython 3.9 abi3 Linux glibc 2.28+ ARM64 Details for rustgments-0.1.0-cp39-abi3-manylinux_2_28_aarch64.whl
rustgments-0.1.0-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details for rustgments-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
rustgments-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl CPython 3.9 abi3 macOS 10.12+ x86-64 Details for rustgments-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl

Total release size: 30.4 MB

Release files / rustgments-0.1.0.tar.gz

Download URL rustgments-0.1.0.tar.gz
Size 2.6 MB
Tags Source
SHA-256 checksum
How to use checksums
f6d1ff442c00cc4a6d54f0c597c3fbd5ee611a9c3bf5c011211c1449bed09382
BLAKE2b-256 checksum
How to use checksums
4f72eb23eaeaf189093586a3a5c79e40b17e99bebf9c2b66c3c35f51aabe7672
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Release files / rustgments-0.1.0-cp39-abi3-win_amd64.whl

Download URL rustgments-0.1.0-cp39-abi3-win_amd64.whl
Size 3.7 MB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
a73ba8c000c551ce10d70f8cc3395e0375899369368a9016ea1e8f6a9180c4e4
BLAKE2b-256 checksum
How to use checksums
e782c11f367764afd79bf57515fca5637b2a38b24b70b748518b37bcc1e996b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Release files / rustgments-0.1.0-cp39-abi3-musllinux_1_2_x86_64.whl

Download URL rustgments-0.1.0-cp39-abi3-musllinux_1_2_x86_64.whl
Size 4.2 MB
Tags CPython 3.9 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
cefa8c75b88b7075237de2cf63597d8863b5ac90c031cfd1d2413d75e9a4ea4c
BLAKE2b-256 checksum
How to use checksums
d958d116bcc7ca59e1daeb3786efdd2c4659802ee934cf086956baf16140655a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Release files / rustgments-0.1.0-cp39-abi3-musllinux_1_2_aarch64.whl

Download URL rustgments-0.1.0-cp39-abi3-musllinux_1_2_aarch64.whl
Size 4.1 MB
Tags CPython 3.9 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
fd2aeb68c306efe4d1a6fb5817b18a227c42c167d2303efe39b8d31e098c134d
BLAKE2b-256 checksum
How to use checksums
2103926c49f63b1de647ed52a1daf0d5e844c346e6147ede743a6f1c7851572e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Release files / rustgments-0.1.0-cp39-abi3-manylinux_2_28_x86_64.whl

Download URL rustgments-0.1.0-cp39-abi3-manylinux_2_28_x86_64.whl
Size 4.0 MB
Tags CPython 3.9 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
dbfc83a0d1e031d29949e0f85121ec9e891cb3f014580e211ceb026802c84b93
BLAKE2b-256 checksum
How to use checksums
0e326e8088f9d205a789437e80d5c2eccbab41563b332dd4551f5b6fd425ff90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Release files / rustgments-0.1.0-cp39-abi3-manylinux_2_28_aarch64.whl

Download URL rustgments-0.1.0-cp39-abi3-manylinux_2_28_aarch64.whl
Size 3.9 MB
Tags CPython 3.9 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
e6a381ea1d0efea064621362811ef1857bcda16b1a467ef8170673bd5968f7d3
BLAKE2b-256 checksum
How to use checksums
0c6901db7c1cadf6ce254603384221d06088b8abe42be3e91e2939ad3263f242
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Release files / rustgments-0.1.0-cp39-abi3-macosx_11_0_arm64.whl

Download URL rustgments-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
Size 4.0 MB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
7808b2d49f3d5163a397c644d5859f8ae47f53acff51a0bcafce7917cee57262
BLAKE2b-256 checksum
How to use checksums
564f52367da875d5a128c8e3167008de9761c409cd046e3f35a6795d28f7b085
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Release files / rustgments-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl

Download URL rustgments-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl
Size 3.8 MB
Tags CPython 3.9 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
348b0b74ea0eeabf87838543995737232a191b83baae15fc3dacee01e711f9b7
BLAKE2b-256 checksum
How to use checksums
3f2d09db23ac4498e5d8b2f4f71b41d05a49d7682250a4d5d4471509490c3bbb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Security for rustgments 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 8 files in this release have been uploaded with provenance metadata.

Select from 8 files

Attestation for rustgments-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

8 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page