Renderer: AST to MyST Markdown Rendering#

Renderer maps the token types produced by Parser back to normalized MyST text: directives with cleaned options and de-duplicated attributes, roles, math (dollar-delimited or a bare amsmath environment), targets, comments, attribute lines, and block breaks. Rendering functions self-register into REGISTRY via the register() decorator, and inter-block spacing is decided by the Spacer policies installed in setup().

AST to MyST Markdown Rendering.

Renderer maps the token types produced by Parser back to normalized MyST text: directives with cleaned options and de-duplicated attributes, roles, math (dollar-delimited or a bare amsmath environment), targets, comments, attribute lines, and block breaks. Rendering functions self-register into REGISTRY via the register() decorator, and inter-block spacing is decided by the Spacer policies installed in setup().

myform.Renderer.KEEP_INDENTATION = frozenset({'amsmath', 'bullet_list', 'code_block', 'fence', 'myform_verbatim', 'myst_directive', 'ordered_list'})#

TeX layout, list structure, a block carried as written, or the literal lines of code, which a directive may enclose. The quote never promotes those lines.

Type:

Block quote children whose rendered indentation is theirs

class myform.Renderer.Attribute(*, key: str, val: str | int | float | bool)#

Represents an HTML attribute for rendering as an inline attribute.

class myform.Renderer.Renderer#

Renderer for MyST-specific syntax nodes (i.e. AST -> MD).

REGISTRY: ClassVar[dict[str, Interface]] = {'amsmath': <function Renderer.render_amsmath>, 'blockquote': <function Renderer.render_blockquote>, 'inline': <function Renderer.render_inline>, 'math_block': <function Renderer.render_math_block>, 'math_block_label': <function Renderer.render_math_block_label>, 'math_inline': <function Renderer.render_math_inline>, 'myst_attrs_block': <function Renderer.render_myst_attrs_block>, 'myst_block_break': <function Renderer.render_myst_block_break>, 'myst_directive': <function Renderer.render_myst_directive>, 'myst_ignored': <function Renderer.render_myst_ignored>, 'myst_line_comment': <function Renderer.render_myst_line_comment>, 'myst_role': <function Renderer.render_myst_role>, 'myst_target': <function Renderer.render_myst_target>, 'root': <function Renderer.render_root>}#

Populated by register()

static register(fn: Interface, name: str = '') → Interface#

Decorator to register a method as a renderer for a given node type.

classmethod render_directive_options(options: dict[str, Any], *, style: str = 'auto', yaml_threshold: int = 3) → str#

Render directive options.

Options definitions are the equivalent of python keyword parameters, providing the writer the opportunity to customize the directive’s output in various preset ways.

Parameters:
  • options – A map of option(/parameter) names to serializable values.

  • style – auto (prefix, escalating to a YAML block past yaml_threshold), prefix (always :key: val), or yaml (always a --- block). See MystOptions.directive_options_style.

  • yaml_threshold – In auto style, switch to a YAML block once the rendered options exceed this many lines.

Returns:

Formatted markdown content describing the given options.

classmethod render_attributes(attributes: list[tuple[str, Any]]) → str#

Transform HTML attributes into string-separated ‘inline attributes’.

classmethod render_blocks(*nodes: Node, ctx: Context) → str#

Render multiple block-level nodes with appropriate spacing.

classmethod render_block_spacing(nodes: tuple[Node, Node], texts: tuple[str, str], ctx: Context) → int#

Choose the vertical spacing between two adjacent blocks, consulting the spacers.

Parameters:
  • nodes – The pair of block-level nodes between which we are rendering a space.

  • texts – The rendered text of each node.

  • ctx – The render context, used to resolve the option-aware spacer set (_spacers).

Returns:

The number of newlines to insert between the two nodes (2, the default, when no spacer matches).

static render_myst_line_comment(node: Node, _: Context) → str#

Render line comment nodes.

static render_myst_block_break(node: Node, _: Context) → str#

Render block break nodes.

static render_myst_target(node: Node, _: Context) → str#

Render target anchors.

static render_myst_attrs_block(node: Node, _: Context) → str#

Render a block attribute line verbatim; the spacers keep it above its block.

static render_myst_ignored(node: Node, _: Context) → str#

Emit a skip-formatting span’s captured source verbatim (see Ignore).

Unlike an html_block carrier – whose renderer calls lstrip on the first line and would silently de-indent a preserved code block – this returns the source untouched, so ignore comments truly preserve their span. Block spacing is added by the root joiner.

static render_math_inline(node: Node, _: Context) → str#

Render inline math.

static render_inline(node: Node, ctx: Context) → str#

Render inline content with its line layout settled before any escape reads it.

mdformat decides escapes against the layout it is shown: per text node (a soft break ends one) and per line (a paragraph line that starts like block syntax). A reflow that changes the layout afterwards leaves those decisions stale, and the next pass, which reads the new layout, decides them differently. So the layout is settled first. A heading joins its soft breaks, which its renderer turns into spaces anyway. A paragraph that the sembr option reflows joins them, then the rendered text is split into sentences, and mdformat’s paragraph renderer and the paragraph postprocessors escape the result line by line.

static render_math_block(node: Node, ctx: Context) → str#

Render block math.

static render_math_block_label(node: Node, ctx: Context) → str#

Render block math with label.

static render_amsmath(node: Node, _: Context) → str#

Render a bare amsmath environment, begin and end lines included, verbatim.

static render_blockquote(node: Node, ctx: Context) → str#

Render blockquotes while being safe with math blocks inside.

static render_myst_role(node: Node, ctx: Context) → str#

Render inline role nodes.

static render_myst_directive(node: Node, ctx: Context) → str#

Render directives.

Copied from upstream mdformat core and should be kept up-to-date if upstream introduces changes. Note that only two lines are added to the upstream implementation, i.e. the condition that calls render_directive_content function.

static render_root(root: Node, ctx: Context) → str#

Render the root node by rendering all children with appropriate spacing.

Parameters:
  • root – The root node of the render tree.

  • ctx – The current render context.

Returns:

The fully rendered Markdown string.

classmethod prerender_root(root: Node, ctx: Context) → None#

Pre-render hook – make sure to call superclass’ version if you override it.

classmethod postrender_root(rendered_nodes: list[tuple[Node, str]], ctx: Context) → str#

Post-render hook – make sure to call superclass’ version if you override it.