Module

formatters.html

HTML formatter for Rosettes.

Generates HTML output with semantic or Pygments-compatible CSS classes. Thread-safe by design — no mutable shared state.

Features:

  • Dual CSS class output: semantic (.syntax-function) or Pygments (.nf)
  • CSS custom properties for runtime theming
  • Line highlighting (hl_lines parameter)
  • Streaming output (generator-based)

Design Philosophy:

The HTML formatter is optimized for the common case while supporting advanced features when needed:

  1. Fast path:format_fast()for simple highlighting (~50µs/block)
  2. Slow path:format()for line highlighting, line numbers
  3. Immutable: Frozen dataclass ensures thread-safety
  4. Streaming: Yields chunks for memory-efficient processing

CSS Class Styles:

  • semantic (default): Human-readable class names like.syntax-function, .syntax-keyword. Better for custom themes and debugging.
  • pygments: Pygments-compatible names like.nf,.k. Works with existing Pygments CSS themes.

Performance Optimizations:

  1. Fast path when no line highlighting needed
  2. Pre-computed escape table (C-levelstr.translate)
  3. Pre-built span templates (avoid f-string in loop)
  4. Direct token type value access (StrEnum)
  5. Streaming output (generator, no intermediate list)

Benchmarks (100-line Python file):

  • format_fast(): ~50µs
  • format()with hl_lines: ~80µs
  • format()with line numbers: ~100µs

Common Mistakes:

# ❌ WRONG: Calling format() and ignoring streaming
html = ""
for chunk in formatter.format(tokens):
    html += chunk  # O(n²) string concatenation

# ✅ CORRECT: Use format_string() or join()
html = formatter.format_string(tokens)
# or
html = "".join(formatter.format(tokens))

See Also:

  • rosettes.formatters.terminal: ANSI terminal output formatter
  • rosettes.formatters.null: No-op formatter for testing
  • rosettes.themes: CSS generation for HTML themes
  • rosettes._escape: HTML entity escaping (used internally)

Classes

HtmlFormatter 8
HTML formatter with streaming output. Thread-safe: all state is immutable or local to method calls…

HTML formatter with streaming output.

Thread-safe: all state is immutable or local to method calls. Instances are frozen dataclasses and can be safely shared across threads.

Output Structure:


        def ...
      

Attributes

Name Type Description
config HighlightConfig

Highlight configuration for line highlighting, line numbers.

css_class_style CssClassStyle

"semantic" for .syntax-* or "pygments" for .k, .nf

Methods

name 0 str
property
def name(self) -> str
Returns
str
container_class 0 str
Get the container CSS class based on style.
property
def container_class(self) -> str
Returns
str
format_fast 2 Iterator[str]
Ultra-fast formatting without line highlighting. Uses role-based class names f…
def format_fast(self, tokens: Iterator[tuple[TokenType, str]], config: FormatConfig | None = None) -> Iterator[str]

Ultra-fast formatting without line highlighting.

Uses role-based class names for semantic mode.

Optimizations:

  • Pre-built span templates (no f-string per token)
  • Direct dict lookup (O(1))
  • Minimal branching in hot path
Parameters
Name Type Description
tokens
config Default:None
Returns
Iterator[str]
format 2 Iterator[str]
Format tokens as HTML with streaming output.
def format(self, tokens: Iterator[Token], config: FormatConfig | None = None) -> Iterator[str]
Parameters
Name Type Description
tokens
config Default:None
Returns
Iterator[str]
format_string 2 str
def format_string(self, tokens: Iterator[Token], config: FormatConfig | None = None) -> str
Parameters
Name Type Description
tokens
config Default:None
Returns
str
format_string_fast 2 str
def format_string_fast(self, tokens: Iterator[tuple[TokenType, str]], config: FormatConfig | None = None) -> str
Parameters
Name Type Description
tokens
config Default:None
Returns
str