all2md.renderers.jinja

Jinja2 template-based rendering from AST.

This module provides a generic Jinja2 template-based renderer that allows users to define custom output formats using templates. Templates have full access to the document AST and can produce any text-based format.

all2md.renderers.jinja.escape_xml(text: str) str

Escape text for XML/HTML output.

Parameters:

text (str) – Text to escape

Returns:

XML-escaped text

Return type:

str

all2md.renderers.jinja.escape_html(text: str) str

Escape text for HTML output (alias for escape_xml).

Parameters:

text (str) – Text to escape

Returns:

HTML-escaped text

Return type:

str

all2md.renderers.jinja.escape_latex(text: str) str

Escape text for LaTeX output.

Parameters:

text (str) – Text to escape

Returns:

LaTeX-escaped text

Return type:

str

all2md.renderers.jinja.escape_yaml(text: str) str

Escape text for YAML string values.

Parameters:

text (str) – Text to escape

Returns:

YAML-escaped text (returns quoted string if needed)

Return type:

str

all2md.renderers.jinja.escape_markdown(text: str, context: str = 'text') str

Escape text for Markdown output.

Parameters:
  • text (str) – Text to escape

  • context ({"text", "table", "link", "image_alt"}, default = "text") – Context where text will be used

Returns:

Markdown-escaped text

Return type:

str

all2md.renderers.jinja.get_all_headings(document: Document) list[dict[str, Any]]

Extract all headings from document.

Parameters:

document (Document) – Document to extract headings from

Returns:

List of heading dictionaries with keys: level, text, node

Return type:

list[dict[str, Any]]

Extract all links from document.

Parameters:

document (Document) – Document to extract links from

Returns:

List of link dictionaries with keys: url, title, text, node

Return type:

list[dict[str, Any]]

all2md.renderers.jinja.get_all_images(document: Document) list[dict[str, Any]]

Extract all images from document.

Parameters:

document (Document) – Document to extract images from

Returns:

List of image dictionaries with keys: url, alt_text, title, width, height, node

Return type:

list[dict[str, Any]]

all2md.renderers.jinja.get_all_footnotes(document: Document) list[dict[str, Any]]

Extract all footnote definitions from document.

Parameters:

document (Document) – Document to extract footnotes from

Returns:

List of footnote dictionaries with keys: identifier, node

Return type:

list[dict[str, Any]]

all2md.renderers.jinja.find_nodes_by_type(document: Document, node_type: str) list[Node]

Find all nodes of a specific type.

Parameters:
  • document (Document) – Document to search

  • node_type (str) – Node type name (e.g., “Heading”, “Paragraph”, “CodeBlock”)

Returns:

List of matching nodes

Return type:

list[Node]

all2md.renderers.jinja.node_type_name(node: Node | dict) str

Get the type name of a node.

Parameters:

node (Node or dict) – Node object or dictionary representation

Returns:

Node type name

Return type:

str

class all2md.renderers.jinja.JinjaRenderer

Bases: BaseRenderer

Render AST using Jinja2 templates.

This renderer provides maximum flexibility by allowing users to define custom output formats using Jinja2 templates. Templates have full access to the document AST and can use helper filters and functions for common operations.

Parameters:

options (JinjaRendererOptions or None, default = None) – Jinja2 rendering options including template path and configuration

Examples

Render using a template file:
>>> from all2md.renderers.jinja import JinjaRenderer
>>> from all2md.options import JinjaRendererOptions
>>> options = JinjaRendererOptions(
...     template_file="templates/docbook.xml.jinja2",
...     escape_strategy="xml"
... )
>>> renderer = JinjaRenderer(options)
>>> output = renderer.render_to_string(document)
Render using an inline template:
>>> template = "Title: {{ metadata.title }}"
>>> options = JinjaRendererOptions(template_string=template)
>>> renderer = JinjaRenderer(options)
>>> output = renderer.render_to_string(document)

Initialize the Jinja2 renderer with options.

__init__(options: JinjaRendererOptions | None = None)

Initialize the Jinja2 renderer with options.

render_to_string(document: Document) str

Render document to string using Jinja2 template.

Parameters:

document (Document) – Document to render

Returns:

Rendered output

Return type:

str

render(doc: Document, output: str | Path | IO[bytes]) None

Render document to file or file-like object.

Parameters:
  • doc (Document) – Document to render

  • output (str, Path, or IO[bytes]) – Output destination