all2md.parsers.rst

reStructuredText to AST converter.

This module provides conversion from reStructuredText documents to AST representation using the docutils parser. It enables bidirectional transformation by parsing RST into the same AST structure used for other formats.

class all2md.parsers.rst.RestructuredTextParser

Bases: BaseParser

Convert reStructuredText to AST representation.

This converter uses docutils to parse RST and builds an AST that matches the structure used throughout all2md, enabling bidirectional conversion and transformation pipelines.

Parameters:

options (RstParserOptions or None, default = None) – Parser configuration options

Examples

Basic parsing:

>>> parser = RestructuredTextParser()
>>> doc = parser.parse("Title\\n=====\\n\\nThis is **bold**.")

With options:

>>> options = RstParserOptions(parse_directives=True, strict_mode=False)
>>> parser = RestructuredTextParser(options)
>>> doc = parser.parse(rst_text)

Initialize the RST parser with options and progress callback.

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

Initialize the RST parser with options and progress callback.

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

Parse RST input into AST Document.

Parameters:

input_data (str, Path, IO[bytes], or bytes) – RST input to parse. Can be: - File path (str or Path) - File-like object in binary mode - Raw RST bytes - RST string

Returns:

AST document node

Return type:

Document

Raises:
extract_metadata(document: Any) DocumentMetadata

Extract metadata from docutils document.

Parameters:

document (docutils.nodes.document) – Parsed docutils document

Returns:

Extracted metadata from docinfo if present

Return type:

DocumentMetadata

Notes

RST documents can have docinfo blocks that contain metadata fields. This method extracts those fields into DocumentMetadata.