Functions
highlight
3
str
▼
Highlight source code and return formatted output.
This is the primary high-le…
highlight
3
str
▼
def highlight(code: str, language: str, formatter: str | Formatter = 'html') -> str
Highlight source code and return formatted output.
This is the primary high-level API for syntax highlighting. Thread-safe and suitable for concurrent use.
All lexers are hand-written state machines with O(n) guaranteed performance and zero ReDoS vulnerability.
Parameters
| Name | Type | Description |
|---|---|---|
code |
str |
The source code to highlight. |
language |
str |
Language name or alias (e.g., 'python', 'py', 'js'). |
formatter |
str | Formatter |
Formatter name ('html', 'terminal', 'null') or instance. Default:'html'
|
Returns
str
tokenize
4
list[Token]
▼
Tokenize source code without formatting.
Useful for analysis, custom formattin…
tokenize
4
list[Token]
▼
def tokenize(code: str, language: str, start: int = 0, end: int | None = None) -> list[Token]
Tokenize source code without formatting.
Useful for analysis, custom formatting, or testing. Thread-safe.
All lexers are hand-written state machines with O(n) guaranteed performance and zero ReDoS vulnerability.
Parameters
| Name | Type | Description |
|---|---|---|
code |
str |
The source code to tokenize. |
language |
str |
Language name or alias. |
start |
int |
Starting index in the source string. Default:0
|
end |
int | None |
Optional ending index in the source string. Default:None
|
Returns
list[Token]
highlight_many
1
list[str]
▼
Highlight multiple code blocks in parallel.
This is the recommended way to hig…
highlight_many
1
list[str]
▼
def highlight_many(items: Iterable[tuple[str, str]]) -> list[str]
Highlight multiple code blocks in parallel.
This is the recommended way to highlight many code blocks concurrently. On Python 3.14t (free-threaded), this provides true parallelism. On GIL Python, it still provides benefits via I/O overlapping.
Thread-safe by design: each lexer uses only local variables.
Parameters
| Name | Type | Description |
|---|---|---|
items |
Iterable[tuple[str, str]] |
Iterable of (code, language) tuples. |
Returns
list[str]
tokenize_many
1
list[list[Token]]
▼
Tokenize multiple code blocks in parallel.
Similar to highlight_many() but ret…
tokenize_many
1
list[list[Token]]
▼
def tokenize_many(items: Iterable[tuple[str, str]]) -> list[list[Token]]
Tokenize multiple code blocks in parallel.
Similar to highlight_many() but returns raw tokens instead of HTML. Useful for analysis, custom formatting, or when you need token data.
Thread-safe by design: each lexer uses only local variables.
Parameters
| Name | Type | Description |
|---|---|---|
items |
Iterable[tuple[str, str]] |
Iterable of (code, language) tuples. |
Returns
list[list[Token]]
__getattr__
1
object
▼
Module-level getattr for free-threading declaration.
This allows Python to que…
__getattr__
1
object
▼
def __getattr__(name: str) -> object
Module-level getattr for free-threading declaration.
This allows Python to query whether this module is safe for free-threaded execution without enabling the GIL.
Parameters
| Name | Type | Description |
|---|---|---|
name |
str |
Returns
object