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:
BaseRendererRender 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.
- class all2md.renderers.toml.DataExtractor
Bases:
objectExtractor 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.