Skip to content

HTML parsing

parse_html is the standalone Rust-backed HTML parser. It returns a ParsedHtml bundle containing the compiled node and a compiled-stylesheet tuple. The current parser ignores <style> elements, so that tuple is empty; compile document-level CSS explicitly. Pass both fields separately to a compiled renderer API:

from takumi_py import Renderer, parse_html

parsed = parse_html("<article>Hello</article>")
png = Renderer().render_compiled(
    parsed.node,
    stylesheets=parsed.stylesheets,
    width=320,
    height=160,
)

Use HtmlOptions to select parser presets, map Tailwind classes from another attribute, or cap nesting depth. Document-level CSS remains an explicit render input; compile it with Renderer.compile_stylesheet or Renderer.compile_stylesheet_lossy before passing it to a compiled API. HTML compilation is performed directly by the native parser; HTML entry points do not have a separate Python-side validate switch. See HTML and CSS for the full parsing behavior.

parse_html

takumi_py.html.parse_html

parse_html(
    markup: str, *, options: HtmlOptions | None = None
) -> ParsedHtml

Parse HTML without creating a public renderer.

Parameters:

Name Type Description Default
markup str

HTML fragment or document source.

required
options HtmlOptions | None

Parser presets, Tailwind attribute name, and maximum element depth. Defaults to HtmlOptions().

None

Returns:

Type Description
ParsedHtml

A compiled node and its (currently empty) stylesheet bundle.

Raises:

Type Description
HtmlParseError

If the HTML cannot be parsed or exceeds configured limits.

ParsedHtml

takumi_py.html.ParsedHtml dataclass

A compiled HTML node and any stylesheets produced with it.

The current parser keeps <style> elements out of the node tree and returns an empty stylesheets tuple. Compile document-level CSS explicitly with Renderer.compile_stylesheet or Renderer.compile_stylesheet_lossy.

Attributes:

Name Type Description
node CompiledNode

The compiled root node.

stylesheets tuple[CompiledStyleSheet, ...]

Compiled stylesheets returned by the parser.

node instance-attribute

node: CompiledNode

stylesheets class-attribute instance-attribute

stylesheets: tuple[CompiledStyleSheet, ...] = ()