Parser: MyST Markdown to AST Parsing#
Parser collects the classmethods that hook MyST-specific block and inline rules (directives,
roles, line comments, attribute lines, block breaks, targets) into a markdown-it engine. Each
rule is registered declaratively through the ParserEntry.register() decorator, which records
where in the rule chain the parser is inserted; Parser.setup() then replays the registry onto an
engine alongside the default mdformat extensions.
MyST Markdown to AST Parsing.
Parser collects the classmethods that hook MyST-specific block and inline rules (directives,
roles, line comments, attribute lines, block breaks, targets) into a markdown-it engine. Each
rule is registered declaratively through the ParserEntry.register() decorator, which records
where in the rule chain the parser is inserted; Parser.setup() then replays the registry onto an
engine alongside the default mdformat extensions.
- class myform.Parser.ParserEntry(name: str, func: Interface, rule: str = '', mode: 'before' | 'after' | 'at' = 'before', inline: bool = False, alt: list[str] = <factory>)#
One registered parser rule and its placement, kept free of import-time plugins.
- func: Interface#
The rule callable; on success it increments
state.line, adds tokens, and returns True.
- model_dump(**kwargs: Any) dict[str, Any]#
Return the markdown-it registration keyword arguments.
Accepts (and ignores) the standard
BaseModel.model_dumpkeywords (mode,include,exclude, …): the pre-dataclass Pydantic model serialized through a plain-modemodel_serializer, which produced this same fixed shape regardless of those options, so keyword callers keep working unchanged.
- classmethod model_validate(data: Any) ParserEntry#
Build an entry from a mapping of constructor fields (or pass an entry through).
Restores the one
BaseModelconstructor-style API the dataclass conversion dropped; validation itself still happens in__post_init__.
- property options: RuleOptionsType | None#
Get the rule options for this parser.
- classmethod register(rule: str, name: str = '', mode: 'before' | 'after' | 'at' = 'before', inline: bool = False, alt: list[str] | None = None, extra_alt: list[str] | None = None) Callable[[M], M]#
Decorator factory that registers a parser method into the class registry.
- Parameters:
rule – The markdown-it rule name this parser hooks into (e.g.
'fence').name – Explicit registration name; inferred from the function name if omitted.
mode – Where to insert relative to the existing rule (
'before','after','at').inline – Whether this is an inline (vs. block) rule.
alt – Alternative block rules under which this parser may also fire.
extra_alt – Additional alternative rules appended to
alt(block only).
- Returns:
A decorator that registers the method and returns it unchanged.
- class myform.Parser.Parser#
Parser for MyST-specific syntax nodes (i.e. MD -> AST).
- classmethod setup(engine: MarkdownIt, *additional_plugins: str | ParserExtensionInterface) None#
Set up the given engine with the default mdformat extensions and our custom parsers.
- Parameters:
engine – The markdown-it engine to configure.
*additional_plugins – Extra mdformat parser extensions (or their names) to enable.
- classmethod parse_myst_role(state: StateInline, silent: bool) bool#
Parse MyST roles – inline
{name}markers followed by backtick-fenced content.
- classmethod parse_safe_setext_heading(state: StateBlock, l0: int, le: int, silent: bool) bool#
Preserve an ambiguous unary nested dash before Setext parsing.
- classmethod parse_myst_directive(state: StateBlock, l0: int, le: int, silent: bool) bool#
Parse MyST directives (fenced code blocks identified by {bracketed} info strings).
On success: increment
state.line, add tostate.tokens, and returnTrue.
- classmethod parse_myst_line_comment(state: StateBlock, l0: int, le: int, silent: bool) bool#
Parse MyST line comments (e.g.
% comment text).
- classmethod parse_myst_block_break(state: StateBlock, l0: int, le: int, silent: bool) bool#
Parse MyST thematic breaks (e.g.
+++ {"json": "data"}).
- classmethod parse_myst_target(state: StateBlock, l0: int, le: int, silent: bool) bool#
Parse MyST targets (e.g.
(target_name)=).
- classmethod parse_myst_attrs_block(state: StateBlock, l0: int, le: int, silent: bool) bool#
Parse a block attribute line (e.g.
{.glossary}), as theattrs_blockextension.The line holds one attribute list and nothing else; on its own it is a block of its own, so the block below it (a definition list, a quote, a heading) parses as it would without the line above. Without this rule the line is the first line of a paragraph and swallows the term of a definition list beneath it. The token keeps the line verbatim, and its
tightflag records whether the next line was the block it applies to, so the writer can leave the two adjacent. Like the extension’s own rule it never interrupts a paragraph, list, or quote.