Classes
Highlighter
2
▼
Protocol for syntax highlighters.
Aligned with Bengal's HighlightService for seamless integration.…
Highlighter
2
▼
Protocol for syntax highlighters.
Aligned with Bengal's HighlightService for seamless integration. Highlighters take code and language and return HTML markup with syntax highlighting applied.
Thread Safety:
Implementations must be thread-safe. The highlight() method
may be called concurrently from multiple render threads.
Methods
highlight
2
str
▼
Highlight code with syntax colors.
**Contract:**
- MUST return valid HTML (nev…
highlight
2
str
▼
def highlight(self, code: str, language: str) -> str
Highlight code with syntax colors.
Contract:
- MUST return valid HTML (never raise for bad input)
- MUST escape HTML entities in code
- MUST use CSS classes (not inline styles)
- SHOULD fall back to plain text for unknown languages
Parameters
| Name | Type | Description |
|---|---|---|
code |
— |
Source code to highlight |
language |
— |
Language identifier (e.g., "python", "javascript") |
Returns
str
HTML markup with highlighting
supports_language
1
bool
▼
Check if highlighter supports the given language.
**Contract:**
- MUST NOT rai…
supports_language
1
bool
▼
def supports_language(self, language: str) -> bool
Check if highlighter supports the given language.
Contract:
- MUST NOT raise exceptions
- SHOULD handle common aliases (js -> javascript)
Parameters
| Name | Type | Description |
|---|---|---|
language |
— |
Language identifier or alias |
Returns
bool
True if highlighting is available
Functions
set_highlighter
1
None
▼
Set the global syntax highlighter.
set_highlighter
1
None
▼
def set_highlighter(highlighter: Highlighter | SimpleHighlighter | None) -> None
Parameters
| Name | Type | Description |
|---|---|---|
highlighter |
Highlighter | SimpleHighlighter | None |
A Highlighter protocol implementation, or a simple function that takes (code, language) and returns HTML. Pass None to clear the highlighter. |
_try_import_rosettes
0
bool
▼
Try to import and configure Rosettes highlighter.
_try_import_rosettes
0
bool
▼
def _try_import_rosettes() -> bool
Returns
bool
highlight
2
str
▼
Highlight code using the configured highlighter.
Falls back to plain code bloc…
highlight
2
str
▼
def highlight(code: str, language: str) -> str
Highlight code using the configured highlighter.
Falls back to plain code block if no highlighter is available. Automatically tries to use Rosettes if installed.
Parameters
| Name | Type | Description |
|---|---|---|
code |
str |
Source code to highlight |
language |
str |
Language identifier |
Returns
str
has_highlighter
0
bool
▼
Check if a syntax highlighter is available.
has_highlighter
0
bool
▼
def has_highlighter() -> bool
Returns
bool
get_highlighter
0
Highlighter | SimpleHigh…
▼
Get the current highlighter instance.
get_highlighter
0
Highlighter | SimpleHigh…
▼
def get_highlighter() -> Highlighter | SimpleHighlighter | None
Returns
Highlighter | SimpleHighlighter | None