all2md.renderers.toml

TOML rendering from AST.

This module provides the TomlRenderer class which extracts structured data from AST documents and converts it to TOML format. The primary use case is extracting tables from markdown documentation as TOML structures.

Examples

Extract tables from markdown:

Input markdown:

# Users
| name | age | active |
|------|-----|--------|
| Alice | 30 | true |
| Bob | 25 | false |

Output TOML:

[[Users]]
name = "Alice"
age = 30
active = true

[[Users]]
name = "Bob"
age = 25
active = false
class all2md.renderers.toml.TomlRenderer

Bases: BaseRenderer

Render AST to TOML format by extracting structured data.

This renderer extracts tables and lists from AST documents and converts them to TOML structures. Tables become arrays of tables, with column headers as keys. Preceding headings are used as structure keys.

Note: TOML does not support null values. Null values will be omitted or converted to empty strings.

Parameters:

options (TomlRendererOptions or None, default = None) – TOML rendering options

Examples

Basic usage (extract tables):

>>> from all2md.ast import Document, Table, TableRow, TableCell, Text, Heading
>>> from all2md.renderers.toml import TomlRenderer
>>> table = Table(
...     header=TableRow(cells=[
...         TableCell(content=[Text(content="name")]),
...         TableCell(content=[Text(content="value")])
...     ]),
...     rows=[
...         TableRow(cells=[
...             TableCell(content=[Text(content="timeout")]),
...             TableCell(content=[Text(content="30")])
...         ])
...     ]
... )
>>> heading = Heading(level=1, content=[Text(content="Configuration")])
>>> doc = Document(children=[heading, table])
>>> renderer = TomlRenderer()
>>> toml_text = renderer.render_to_string(doc)

Extract lists as arrays:

>>> from all2md.options.toml import TomlRendererOptions
>>> options = TomlRendererOptions(extract_mode="both")
>>> renderer = TomlRenderer(options)

Initialize the TOML renderer with options.

__init__(options: TomlRendererOptions | None = None)

Initialize the TOML renderer with options.

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

Render AST document to TOML file.

Parameters:
  • doc (Document) – AST Document node to render

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

render_to_string(doc: Document) str

Render AST document to TOML string.

Parameters:

doc (Document) – AST Document node to render

Returns:

TOML string

Return type:

str

class all2md.renderers.toml.DataExtractor

Bases: object

Extractor that walks AST and extracts structured data.

This class walks the AST and extracts tables and lists into structured data according to the renderer options.

Parameters:

options (TomlRendererOptions) – Rendering options

Initialize the data extractor.

__init__(options: TomlRendererOptions)

Initialize the data extractor.

extract(doc: Document) dict[str, Any]

Extract data from document.

Parameters:

doc (Document) – Document to extract from

Returns:

Extracted data

Return type:

dict