Rosettes

A Python syntax highlighter and Pygments alternative for secure code highlighting and existing CSS themes

Python Syntax Highlighter for Secure Code Highlighting

Pygments alternative. O(n) guaranteed. Safe for untrusted input.

Rosettes is a pure-Python syntax highlighter for HTML output, documentation sites, and web applications. Every lexer is a hand-written state machine with no regex backtracking, making it a good fit for secure code highlighting and Pygments-style migration paths.

from rosettes import highlight

html = highlight("def hello(): print('world')", "python")

Why Use Rosettes

O(n) Guaranteed

Every lexer processes input in linear time. No regex patterns that can be exploited for denial-of-service attacks.

55 Languages

Hand-written state machines for Python, JavaScript, Rust, Go, and 51 more languages. Full syntax support, not just keywords.

Free-Threading Ready

Optimized for Python 3.14t (PEP 703). True parallelism withhighlight_many()for multi-core systems.

Pygments Compatible

Drop-in CSS class compatibility. Use your existing Pygments themes or Rosettes' semantic classes.

Common Use Cases

  • Highlighting code blocks for documentation sites and blogs
  • Replacing or evaluating Pygments in Python applications
  • Rendering untrusted code snippets in web applications
  • Processing many code blocks in parallel withhighlight_many()
  • Keeping existing themes while moving to a Python-native highlighter

Quick Example

from rosettes import highlight, highlight_many

# Single block
html = highlight("const x = 1;", "javascript")

# Parallel processing (optimal for 8+ blocks)
blocks = [
    ("def foo(): pass", "python"),
    ("fn main() {}", "rust"),
    ("let x = 1;", "javascript"),
]
results = highlight_many(blocks)  # 1.5-2x speedup on 3.14t

Performance

File Size Time
~50 lines ~0.5ms
~500 lines ~5ms
10,000 lines ~220ms

Benchmark source


The Bengal Ecosystem

A structured reactive stack — every layer written in pure Python for 3.14t free-threading.

ᓚᘏᗢ Bengal Static site generator Docs
∿∿ Purr Content runtime
⌁⌁ Chirp Web framework Docs
=^..^= Pounce ASGI server Docs
)彡 Kida Template engine Docs
ฅᨐฅ Patitas Markdown parser Docs
⌾⌾⌾ Rosettes Syntax highlighter ← You are here Docs

Python-native. Free-threading ready. No npm required.