all2md.parsers.ast_json

JSON AST to Document converter.

This module provides a parser for converting JSON-serialized AST back into Document objects. This enables programmatic document generation and manipulation through the AST JSON format.

class all2md.parsers.ast_json.AstJsonParser

Bases: BaseParser

Convert JSON AST format to Document objects.

This parser reads JSON-serialized AST and converts it back to Document objects using the ast.serialization module.

Parameters:

Examples

Parse AST JSON from file:
>>> from all2md.parsers.ast_json import AstJsonParser
>>> parser = AstJsonParser()
>>> doc = parser.parse("document.ast")
Parse AST JSON from string:
>>> import json
>>> ast_json = json.dumps({
...     "schema_version": 1,
...     "node_type": "Document",
...     "children": [],
...     "metadata": {}
... })
>>> doc = parser.parse(ast_json.encode('utf-8'))

Initialize the AST JSON parser.

__init__(options: AstJsonParserOptions | None = None, progress_callback: Callable[[ProgressEvent], None] | None = None)

Initialize the AST JSON parser.

parse(input_data: str | Path | IO[bytes] | bytes) Document

Parse JSON AST input into a Document.

Parameters:

input_data (str, Path, IO[bytes], or bytes) – The input JSON AST to parse

Returns:

AST Document node

Return type:

Document

Raises:

ParsingError – If parsing fails due to invalid JSON or AST structure

extract_metadata(document: Any) DocumentMetadata

Extract metadata from AST Document.

Parameters:

document (Document) – The parsed Document node

Returns:

Extracted metadata from Document.metadata

Return type:

DocumentMetadata