Module

highlighting

Syntax highlighting protocol and injection for Patitas.

Provides optional syntax highlighting for code blocks. When patitas[syntax] is installed, Rosettes is used automatically.

Protocol Alignment:

The Highlighter protocol is aligned with Bengal's HighlightService
for seamless integration. Both interfaces support:
  • highlight(code, language, hl_lines, show_linenos) -> str
  • supports_language(language) -> bool

Usage:

# Automatic with patitas[syntax]
from patitas import Markdown
md = Markdown()  # Highlighting enabled if rosettes is installed

# Manual injection
from patitas.highlighting import set_highlighter

def my_highlighter(code: str, language: str) -> str:
    return f'<pre class="language-{language}"><code>{code}</code></pre>'

set_highlighter(my_highlighter)

Classes

Highlighter 2
Protocol for syntax highlighters. Aligned with Bengal's HighlightService for seamless integration.…

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…
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…
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.
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.
def _try_import_rosettes() -> bool
Returns
bool
highlight 2 str
Highlight code using the configured highlighter. Falls back to plain code bloc…
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.
def has_highlighter() -> bool
Returns
bool
get_highlighter 0 Highlighter | SimpleHigh…
Get the current highlighter instance.
def get_highlighter() -> Highlighter | SimpleHighlighter | None
Returns
Highlighter | SimpleHighlighter | None