Skip to main content

Module doc

Module doc 

Source
Expand description

Documents and fragments: the parsed source a document is made of, how its trees are placed, and the walk that defines document order.

A fragment is one piece of source as the parser delivered it (its tree, its source, its parse diagnostics); a tree is one placement of a fragment’s node tree in a document, numbered by the document’s arena; a document is its content (the tree arena, the traversal list and the resolution map) and the option values bound on it, and nothing else. See documents, fragments, and the corpus.

A Fragment also records how the content at its top level is laid out (FragmentLayout). It is shared as Arc<Fragment> and has no identifier of its own: its identity is the shared pointer, so one fragment may be used by several documents at once.

A document (Doc) holds its content (DocContent) and the option values set on it (OptionValues). The content has exactly three parts:

  • the tree arena, which numbers every tree added to the document with a TreeId in the order the trees are added; adding one Arc<Fragment> twice makes two trees, each with its own computed data;
  • the traversal list, the trees the walk visits in their own right, in order;
  • the resolution map, which records the node at which each placed tree is entered: the node places that tree.

A document has no structure beyond that: no front matter, no chapters, no named parts. The structure of a document comes from the headings in its content and is computed by a stage, so that there is only one account of how a document nests.

The usual way to build a document’s content is assemble, which parses a primary file and the files it includes; DocContent::place, DocContent::add_tree and DocContent::resolve build one tree by tree.

§The walk

DocContent::walk defines document order, and it is the only definition the library has. The walk visits the trees of the traversal list in order, enters each tree at most once, and enters a tree that a node places at that node: the placed tree’s nodes come right after the placing node, before the placing node’s own children. Everything that comes before the placed tree in document order is therefore the placing node and everything that comes before it. Every notion of “earlier in the document” (numbering, the order of notes, the order of citations) uses this one operation, so a placed tree needs no special handling anywhere else. DocContent::walk_nodes and DocContent::walk_trees keep the nodes alone and the trees alone.

The walk (Walk) runs over any WalkSource: something that gives trees by identifier, the trees of its traversal list, and which node places which tree. DocContent is the source of the trees as parsed; a run is the source of its frozen trees, what each tree became after the rewrites (StageCx::walk). Both use Walk, so document order is the same for both.

Document order is neither the order of the arena nor the order in which node addresses sort; see crate::model::addr.

§What a document does not hold

The data that stages compute, and the diagnostics they report, belong to the run that computed them, not to the document: one document may be rendered to several formats, or checked and then rendered, and nothing about it may depend on which plan runs. The diagnostics of the parse are kept in the fragment (Fragment::diags). Synthesized values (small trees a stage builds while a run is under way) are not part of the arena either; the run numbers them (see TreeId).

Structs§

Doc
A document: its content together with the option values set on it.
DocContent
The content of a document: its tree arena, its traversal list and its resolution map.
Fragment
A fragment: one piece of source as the parser delivered it, with its tree, the source it came from, the diagnostics of the parse, and the layout of its top level (FragmentLayout).
Walk
The walk: an iterator over the steps of the traversal of a WalkSource in document order.

Enums§

FragmentLayout
How the content at the top level of a fragment is laid out when a run builds block structure: as blocks, as inline content, or as blocks only when it holds a block.
PlacementError
The error of DocContent::resolve: why a placement was not recorded.
WalkStep
One step of the walk, yielded by Walk.

Traits§

WalkSource
The trees a Walk visits: trees by identifier, the trees of the traversal list in order, and which node places which tree.