Classes
HtmlFormatter
10
▼
HTML formatter with streaming output.
Thread-safe: all state is immutable or local to method calls…
HtmlFormatter
10
▼
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
name
0
str
▼
def name(self) -> str
Returns
str
container_class
0
str
▼
Get the container CSS class based on style.
property
container_class
0
str
▼
def container_class(self) -> str
Returns
str
format_fast
2
Iterator[str]
▼
Ultra-fast formatting without line highlighting.
Uses role-based class names f…
format_fast
2
Iterator[str]
▼
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.
Dispatches to the fast path when …
format
2
Iterator[str]
▼
def format(self, tokens: Iterator[Token], config: FormatConfig | None = None) -> Iterator[str]
Format tokens as HTML with streaming output.
Dispatches to the fast path when no line highlighting is needed, otherwise delegates to _format_with_hl_lines().
Parameters
| Name | Type | Description |
|---|---|---|
tokens |
— |
|
config |
— |
Default:None
|
Returns
Iterator[str]
format_string
2
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
▼
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
Internal Methods 2 ▼
_resolve_span_table
1
tuple[dict[SyntaxRole, s…
▼
Resolve the correct span lookup table for the current CSS class style.
Returns…
_resolve_span_table
1
tuple[dict[SyntaxRole, s…
▼
def _resolve_span_table(self, prefix: str) -> tuple[dict[SyntaxRole, str] | None, dict[str, str] | None]
Resolve the correct span lookup table for the current CSS class style.
Returns a (semantic, pygments) tuple. For semantic-hybrid, semantic is None (handled dynamically in _format_with_hl_lines).
Parameters
| Name | Type | Description |
|---|---|---|
prefix |
— |
Returns
tuple[dict[SyntaxRole, str] | None, dict[str, str] | None]
_format_with_hl_lines
2
Iterator[str]
▼
Format tokens with line highlighting support.
Handles span open/close around h…
_format_with_hl_lines
2
Iterator[str]
▼
def _format_with_hl_lines(self, tokens: Iterator[Token], config: FormatConfig) -> Iterator[str]
Format tokens with line highlighting support.
Handles span open/close around highlighted lines, newline tracking, and token formatting. Called only when hl_lines is non-empty.
Parameters
| Name | Type | Description |
|---|---|---|
tokens |
— |
|
config |
— |
Returns
Iterator[str]