all2md.parsers.toml
TOML to AST converter.
This module provides intelligent conversion from TOML structures to readable document format. By default, it transforms the structure into a hierarchical document with headings, tables, and lists. Alternatively, it can render TOML as a literal code block (via the literal_block option).
Examples
Smart Conversion (default):
Input TOML:
[server]
host = "localhost"
port = 8080
[[users]]
name = "Alice"
role = "admin"
[[users]]
name = "Bob"
role = "user"
Output (as markdown):
# server
* **host**: localhost
* **port**: 8,080
# users
| name | role |
|------|------|
| Alice | admin |
| Bob | user |
Literal Block Mode:
Input TOML (same as above)
Output (as markdown):
[server]
host = "localhost"
port = 8080
[[users]]
name = "Alice"
role = "admin"
[[users]]
name = "Bob"
role = "user"
- class all2md.parsers.toml.TomlParser
Bases:
BaseParserConvert TOML structures to AST representation.
This parser creates a readable document from TOML by converting: - Sections/tables → heading hierarchies - Arrays of tables → markdown tables - Arrays of primitives → lists - Nested structures → subsections
- Parameters:
options (TomlParserOptions or None, default = None) – Parser configuration options
progress_callback (ProgressCallback or None, default = None) – Optional callback for progress updates
Examples
Basic parsing:
>>> parser = TomlParser() >>> doc = parser.parse('[server]\\nhost = "localhost"\\nport = 8080')
With options:
>>> options = TomlParserOptions(max_heading_depth=3, sort_keys=True) >>> parser = TomlParser(options) >>> doc = parser.parse(toml_file_path)
Initialize the TOML parser with options and progress callback.
- __init__(options: TomlParserOptions | None = None, progress_callback: Callable[[ProgressEvent], None] | None = None)
Initialize the TOML parser with options and progress callback.
- parse(input_data: str | Path | IO[bytes] | bytes) Document
Parse TOML input into AST Document.
- Parameters:
input_data (str, Path, IO[bytes], or bytes) – TOML input to parse. Can be: - File path (str or Path) - File-like object in binary mode - Raw TOML bytes - TOML string
- Returns:
AST document node
- Return type:
- Raises:
ParsingError – If parsing fails
- extract_metadata(data: Any) DocumentMetadata
Extract metadata from TOML structure.
- Parameters:
data (Any) – TOML data structure
- Returns:
Extracted metadata
- Return type: