Module

parsing.backends.patitas.renderers.utils

Utility functions for HTML rendering.

Provides escape functions, URL encoding, and the HeadingInfo dataclass. Thread-safe: all functions are pure (no shared mutable state).

Classes

HeadingInfo 3
Heading metadata collected during rendering. Used to build TOC without post-render regex scanning.…

Heading metadata collected during rendering.

Used to build TOC without post-render regex scanning. Collected by HtmlRenderer during the AST walk.

Attributes

Name Type Description
level int
text str
slug str

Functions

escape_html 1 str
Escape HTML special characters for text content. Per CommonMark spec: - & mus…
def escape_html(text: str) -> str

Escape HTML special characters for text content.

Per CommonMark spec:

  • < > & must be escaped (XSS prevention)
  • " should be escaped to " (for safety)
  • ' should remain literal in text content (not ')

Also strips internal markers used for lazy continuation escaping.

Parameters
Name Type Description
text str
Returns
str
escape_attr 1 str
Escape HTML attribute value.
def escape_attr(text: str) -> str
Parameters
Name Type Description
text str
Returns
str
encode_url 1 str
Encode URL for use in href attribute per CommonMark spec. CommonMark requires:…
def encode_url(url: str) -> str

Encode URL for use in href attribute per CommonMark spec.

CommonMark requires:

  1. Percent-encoding of special characters in URLs (space, backslash, etc.)
  2. HTML escaping of characters that are special in HTML (& → &)

The final output goes in an HTML attribute, so we need both:

  • URL percent-encoding for URL-special characters
  • HTML escaping for HTML-special characters (&, <, >, ", ')
Parameters
Name Type Description
url str
Returns
str
escape_link_title 1 str
Escape link title for use in title attribute per CommonMark spec. Titles use H…
def escape_link_title(title: str) -> str

Escape link title for use in title attribute per CommonMark spec.

Titles use HTML escaping but must also decode HTML entities first. E.g., " in source becomes " which then becomes " in output.

Parameters
Name Type Description
title str
Returns
str
default_slugify 1 str
Default slugify function for heading IDs. Uses bengal.utils.text.slugify with …
def default_slugify(text: str) -> str

Default slugify function for heading IDs.

Uses bengal.utils.text.slugify with HTML unescaping enabled.

Parameters
Name Type Description
text str
Returns
str