Guidedoc AST contract
=====================
This is the normative mapping between source constructs and Guidedoc nodes (GDS 0003). Readers produce exactly these shapes; renderers may rely on them. ``crates/guidedoc/src/kinds.rs`` (``child_allowed``) enforces the parent-child rules during building and validation.
General rules
-------------
- Nodes are emitted in preorder through the builder (``Builder::begin_node``, ``end_node``,
``add_leaf``, ``with_node``). A node's attributes are added right after ``begin_node``,
before children.
- All node text lives in the snapshot text pool (``Builder::add_text``,
``text_open``/``text_push``/``text_close``, ``build_text``). Text is final: escapes
processed, entities decoded, markup removed.
- Spans are byte ranges in the node's source (``SourceSpan::source`` is the source table
index; included files have their own index).
- ``Node.value`` is a small per-kind scalar documented below; otherwise zero.
- ``Node.payload`` is ``NONE`` unless the kind uses a payload row (List uses lists, Cell
uses cells, link-like kinds use links).
- ``LinkInfo.resolved`` is meaningful only when ``kind == LinkKind::Internal``.
- Adjacent Text nodes are allowed but readers should merge runs they produce together.
Shared block kinds
------------------
+---------------+------------------------------------+------------------------------------+
| Kind | Children | Fields |
+===============+====================================+====================================+
| Document | blocks | root; span covers the main source; |
| | | ``value`` = DocumentReading: |
| | | Sphinx (1) when the Sphinx layer |
| | | read it, whose transforms give ids |
| | | of their own (see Renderer |
| | | obligations) |
+---------------+------------------------------------+------------------------------------+
| Title | inlines | document title (RST promotion); |
| | | also first child of Admonition |
| | | (generic), Topic, Sidebar, Table |
| | | (caption), Directive |
+---------------+------------------------------------+------------------------------------+
| Subtitle | inlines | document or sidebar subtitle |
+---------------+------------------------------------+------------------------------------+
| Section | Heading, then blocks | ``value`` = level (1 = top); |
| | | parent Document, Section, Only, or |
| | | DescContent |
+---------------+------------------------------------+------------------------------------+
| Heading | inlines | ``value`` = level 1..6 |
| | | (CommonMark) or section level |
| | | (RST) |
+---------------+------------------------------------+------------------------------------+
| Paragraph | inlines | |
+---------------+------------------------------------+------------------------------------+
| BlockQuote | blocks, optional trailing | class attribute ``epigraph``, |
| | Attribution | ``highlights``, ``pull-quote`` for |
| | | those RST directives |
+---------------+------------------------------------+------------------------------------+
| Attribution | inlines | |
+---------------+------------------------------------+------------------------------------+
| List | ListItem | payload ListInfo{style, delimiter, |
| | | start}; flag Tight |
+---------------+------------------------------------+------------------------------------+
| ListItem | blocks | |
+---------------+------------------------------------+------------------------------------+
| CodeBlock | none; Caption in the Sphinx layer | ``text`` = code, ending in "\\n" |
| | | when non-empty; attribute |
| | | Language; ``value`` > 0: line |
| | | numbers start there |
+---------------+------------------------------------+------------------------------------+
| Preformatted | inlines | RST parsed-literal; renderers keep |
| | | white space |
+---------------+------------------------------------+------------------------------------+
| DoctestBlock | none | ``text`` = content |
+---------------+------------------------------------+------------------------------------+
| MathBlock | none | ``text`` = LaTeX source (one |
| | | equation group) |
+---------------+------------------------------------+------------------------------------+
| RawBlock | none | ``text`` = raw content; attribute |
| | | Format (``html``, ``typst``, |
| | | ``latex``, ...) |
+---------------+------------------------------------+------------------------------------+
| ThematicBreak | none | CommonMark thematic break, RST |
| | | transition |
+---------------+------------------------------------+------------------------------------+
| Comment | none | ``text`` = comment content; |
| | | renderers emit nothing |
+---------------+------------------------------------+------------------------------------+
CommonMark tight lists: paragraphs inside a Tight list's items are still Paragraph nodes;
renderers omit their ``
`` wrappers (reference profile) exactly as CommonMark says.
CommonMark mapping
------------------
+-----------------------------+-----------------------------------------------------------+
| Construct | Node |
+=============================+===========================================================+
| ATX / setext heading | Heading{value = level} |
+-----------------------------+-----------------------------------------------------------+
| indented / fenced code | CodeBlock; Language = first word of the info string |
| | (escapes and entities processed); attribute Custom "info" |
| | holds the full info string when it has more than one word |
+-----------------------------+-----------------------------------------------------------+
| HTML block | RawBlock{Format "html"}; ``text`` = the lines verbatim, |
| | each ending "\\n" |
+-----------------------------+-----------------------------------------------------------+
| block quote | BlockQuote |
+-----------------------------+-----------------------------------------------------------+
| bullet list | List{Bullet, delimiter = '-', '+', or '\*'} |
+-----------------------------+-----------------------------------------------------------+
| ordered list | List{Arabic, delimiter = '.' or ')', start} |
+-----------------------------+-----------------------------------------------------------+
| link reference definition | nothing (consumed) |
+-----------------------------+-----------------------------------------------------------+
| text | Text |
+-----------------------------+-----------------------------------------------------------+
| soft / hard line break | SoftBreak / HardBreak |
+-----------------------------+-----------------------------------------------------------+
| code span | Code{text} |
+-----------------------------+-----------------------------------------------------------+
| emphasis / strong | Emphasis / Strong |
+-----------------------------+-----------------------------------------------------------+
| inline link, reference link | Link; links row {destination, title, kind External}; |
| | children = link text |
+-----------------------------+-----------------------------------------------------------+
| image | Image; links row {destination, title}; children = |
| | description inlines |
+-----------------------------+-----------------------------------------------------------+
| URI autolink | Link{kind External, destination = URI as written}; child |
| | Text |
+-----------------------------+-----------------------------------------------------------+
| email autolink | Link{kind Email, destination = address}; child Text |
+-----------------------------+-----------------------------------------------------------+
| raw HTML | RawInline{Format "html"} |
+-----------------------------+-----------------------------------------------------------+
Destinations are stored after backslash-escape and entity processing, *not* percent-encoded;
the HTML renderer normalizes them as CommonMark's reference does.
The table above is the tree of ``commonmark::Settings::strict`` (pure CommonMark 0.31.2).
Markdown (MyST) mapping
-----------------------
By default the ``commonmark`` reader reads the Markdown that Sphinx projects write with
myst-parser 0.16.1 (:doc:`commonmark-ledger`). Its tree is reStructuredText's
for the equivalent construct, so renderers and the project host need nothing new:
+--------------------------------------------+--------------------------------------------+
| Construct | Node |
+============================================+============================================+
| heading at the top level | Section{value = depth} > Heading{value = |
| | depth}, as an RST section; a skipped level |
| | nests one deeper; attribute Custom |
| | ``slug`` with ``heading_anchors`` |
+--------------------------------------------+--------------------------------------------+
| heading in a quote, list, or directive | Rubric (warning ``myst.header``) |
+--------------------------------------------+--------------------------------------------+
| ``(name)=`` | Target{text = normalized name}; internal |
| | link row, resolved to the next element |
+--------------------------------------------+--------------------------------------------+
| ``% comment`` lines | Comment{text} (consecutive lines join) |
+--------------------------------------------+--------------------------------------------+
| ``+++ text`` | Comment{text; Class ``block_break``} |
+--------------------------------------------+--------------------------------------------+
| front matter | FieldList of its keys (bibliographic keys' |
| | values are Markdown), which resolution |
| | turns into bibliographic fields; |
| | ``html_meta`` becomes Directive{Name |
| | "meta"} as the meta directive's; |
| | ``substitutions`` configure the document |
+--------------------------------------------+--------------------------------------------+
| :literal:`\```{name} arg` , ``:::{name}`` | whatever the directive's handler builds |
| | (Sphinx layer); nothing, with a warning, |
| | without handlers |
+--------------------------------------------+--------------------------------------------+
| ``{name}`` role | whatever the role's handler builds; |
| | Role{text = name} > Text without handlers |
+--------------------------------------------+--------------------------------------------+
| ``[^1]``, ``[^label]`` | FootnoteReference as ``[1]_`` and |
| | ``[#label]_``: text "1" or "#label", flag |
| | Auto for labels |
+--------------------------------------------+--------------------------------------------+
| footnote definitions | Footnote nodes at the end of the document |
| | (inside the last open section), in order |
| | of first reference, after |
| | ThematicBreak{Class ``footnotes``}; |
| | unreferenced ones are dropped |
+--------------------------------------------+--------------------------------------------+
| GFM table | Table{value = columns; Class |
| | ``colwidths-auto``} > TableHead > |
| | Row{Header} > Cell{Header; Class |
| | ``text-left``/``-center``/``-right``} > |
| | Paragraph; TableBody likewise |
+--------------------------------------------+--------------------------------------------+
| fenced code | CodeBlock{Language = first word; |
| | ``default`` without one, ``none`` for |
| | indented code, when a directive extension |
| | is present} |
+--------------------------------------------+--------------------------------------------+
| ``$x$``, ``$$x$$ (label)`` | Math; MathBlock through the math directive |
| | (Id ``equation-