# highlighting

URL: /patitas/api/highlighting/
Section: api
Description: 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)

---

> For a complete page index, fetch /patitas/llms.txt.

Open LLM text
(/patitas/api/highlighting/index.txt)

Share with AI

Ask Claude
(https://claude.ai/new?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fpatitas%2Fapi%2Fhighlighting%2Findex.txt)

Ask ChatGPT
(https://chatgpt.com/?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fpatitas%2Fapi%2Fhighlighting%2Findex.txt)

Ask Gemini
(https://gemini.google.com/app?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fpatitas%2Fapi%2Fhighlighting%2Findex.txt)

Ask Copilot
(https://copilot.microsoft.com/?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fpatitas%2Fapi%2Fhighlighting%2Findex.txt)

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)
```

1Class5Functions

## Classes

`Highlighter`

2

▼

Protocol for syntax highlighters.

Aligned with Bengal's HighlightService for seamless integration.…

Bases:

`Protocol`

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`

4

`str`

▼

Highlight code with syntax colors.

**Contract:**
- MUST return valid HTML (nev…

`def highlight(self, code: str, language: str, *, hl_lines: list[int] | None = None, show_linenos: bool = False) -> 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")

`hl_lines`
`—`

1-indexed line numbers to emphasize (optional)

Default:`None`

`show_linenos`
`—`

Include line numbers in output

Default:`False`

##### 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 (/patitas/api/highlighting/#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`

4

`str`

▼

Highlight code using the configured highlighter.

Falls back to plain code bloc…

`def highlight(code: str, language: str, *, hl_lines: list[int] | None = None, show_linenos: bool = False) -> 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

`hl_lines`
`list[int] | None`

1-indexed line numbers to emphasize (optional)

Default:`None`

`show_linenos`
`bool`

Include line numbers in output

Default:`False`

##### Returns

`str`

`has_highlighter`

0

`bool`

▼

Check if a syntax highlighter is available.

`def has_highlighter() -> bool`

##### Returns

`bool`

`get_highlighter`

0

`Highlighter (/patitas/api/highlighting/#Highlighter) | SimpleHigh…`

▼

Get the current highlighter instance.

`def get_highlighter() -> Highlighter | SimpleHighlighter | None`

##### Returns

`Highlighter (/patitas/api/highlighting/#Highlighter) | SimpleHighlighter | None`
