all2md.renderers.ast_json

JSON AST rendering from Document.

This module provides the AstJsonRenderer class which converts Document nodes to JSON-serialized AST format. This is useful for: - Debugging and inspecting document structure - Programmatic document generation and manipulation - Interoperability with other tools and languages - Testing transforms with JSON fixtures

The renderer uses the ast.serialization module for conversion.

class all2md.renderers.ast_json.AstJsonRenderer

Bases: BaseRenderer

Render Document nodes to JSON AST format.

This class serializes AST Document nodes to JSON format using the ast.serialization module. The output includes schema versioning and preserves all node structure and metadata.

Parameters:

options (AstJsonRendererOptions or None, default = None) – JSON rendering options

Examples

Basic usage:
>>> from all2md.ast import Document, Paragraph, Text
>>> from all2md.renderers.ast_json import AstJsonRenderer
>>> doc = Document(children=[
...     Paragraph(content=[Text(content="Hello, world!")])
... ])
>>> renderer = AstJsonRenderer()
>>> json_str = renderer.render_to_string(doc)
>>> print(json_str)
Compact JSON output:
>>> from all2md.options.ast_json import AstJsonRendererOptions
>>> renderer = AstJsonRenderer(AstJsonRendererOptions(indent=None))
>>> json_str = renderer.render_to_string(doc)

Initialize the AST JSON renderer with options.

__init__(options: AstJsonRendererOptions | None = None)

Initialize the AST JSON renderer with options.

render_to_string(document: Document) str

Render a Document to JSON AST string.

Parameters:

document (Document) – The document node to render

Returns:

JSON AST output

Return type:

str

Examples

>>> from all2md.ast import Document
>>> doc = Document(children=[])
>>> renderer = AstJsonRenderer()
>>> json_str = renderer.render_to_string(doc)
render(doc: Document, output: str | Path | IO[bytes] | IO[str]) None

Render AST to JSON and write to output.

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

  • output (str, Path, IO[bytes], or IO[str]) – Output destination (file path or file-like object)