The In-Progress Zipper #
While identifying the block structure of a document, lines are checked one at a time and added in order. A zipper-like structure makes it easy to see the surrounding block context and to modify it at the right-hand edge.
A container that is currently open and may still receive child blocks.
- blockquote {s : String.Slice} (markers : Array s.Subslice) : OpenContainer s
A blockquote.
markersaccumulates the>markers from every continuation line that has been seen so far. - listItem {s : String.Slice} (list : OpenList s) (marker : s.Subslice) (contentColumn : Columns) : OpenContainer s
A list item that is open, which makes it the last item of its list so far.
listis the list that the item belongs to, with the items that closed before this one.markeris this item's bullet or ordered marker.contentColumnis the column at which the item's content begins. Subsequent lines must be indented to at least this column to count as continuations of this item (CommonMark §5.2).
Instances For
Equations
A frame of the zipper's spine, which is an open container plus the children that have already been closed inside it, in source order.
- container : OpenContainer s
The container in this frame. Subsequent frames in the stack represent the last child of this open container.
- children : Children s
The children that have already been closed inside this frame's container.
Instances For
Equations
A leaf block that is currently open and may still accept additional source lines.
- paragraph {s : String.Slice} (lines : Lines s) : OpenLeaf s
An open paragraph that is being extended line by line. Closing the paragraph (because of a blank line, a new block opener, or the parent container closing) finalizes it as
Block.Raw.paragraph. - fencedCode {s : String.Slice} (info : FenceInfo s) (openFence : s.Subslice) (lines : Array (VerbatimLine s)) : OpenLeaf s
An open fenced code block.
linesaccumulates content lines until either a matching closing fence appears (yieldingBlock.Raw.fencedCodewithcloseFence? := some _) or the parent container closes at EOF (yieldingcloseFence? := none). - indentedCode {s : String.Slice} (lines pendingBlanks : Array (VerbatimLine s)) : OpenLeaf s
An open indented code block.
linesare the content lines that have been committed so far, after the four columns of indentation.pendingBlanksrecords blank lines that may belong to this block. They are promoted tolineswhen a subsequent indented line continues the block, and discarded when an unindented non-blank line closes the block. This matches CommonMark's rule that blank lines between indented chunks are part of the code block, but blank lines that follow the final chunk are not.
- html {s : String.Slice} (kind : HtmlBlockKind) (lines : Array (VerbatimLine s)) : OpenLeaf s
An open HTML block.
kindselects the end condition, andlinesaccumulates the block's lines, starting with the one that met the start condition.
Instances For
Equations
The block parsing state. Maintains the spine of currently-open containers plus an optional open leaf at the bottom.
An empty spine means that only the document is open.
Invariants:
- If
openLeaf?issome _, that leaf logically lives inside the deepest spine frame, or directly under the document if the spine is empty. Extending the leaf adds to its content, and closing the leaf pushes aBlock.Rawonto that container's children. - A list contains only list items. An
OpenListhas items and nothing else, and every open list is either the list of an item frame or the pending list of the container around it.Children.push, the one operation that appends a block, finishes the pending list first.
- document : Children s
The children of the document root.
- spine : Array (ZipperFrame s)
Open containers below the document root, deepest last.
An open leaf node, if one is present.
The position in
spineof the outermost list that the most recent blank line could still make loose (CommonMark §5.3), ornonewhen there is no such list or the next block has begun. A list is at the position of its open item's frame, or one past the deepest frame when it is the pending list of the deepest container.A blank line makes a list loose when the next block begins in one of the list's items. The lists it could make loose are therefore the open lists around it, from this position inward. The outermost of them is inside the nearest enclosing blockquote, because a blank line inside a blockquote separates blocks of the blockquote, not blocks of a list that contains the blockquote.
Instances For
Equations
A zipper with only the document root open, no children, and no open leaf.
Equations
Instances For
The deepest currently-open container, or none if only the document is open.
Equations
- z.top? = Option.map (fun (x : Lean.Markdown.ZipperFrame s) => x.container) z.spine.back?
Instances For
Converts an open leaf into its closed Block.Raw form. Fenced code blocks are not terminated with a
fence.
Equations
- Lean.Markdown.BlockZipper.leafToBlock (Lean.Markdown.OpenLeaf.paragraph lines) = Lean.Markdown.Block.Raw.paragraph (Lean.Markdown.InlineSource.ofLines lines)
- Lean.Markdown.BlockZipper.leafToBlock (Lean.Markdown.OpenLeaf.fencedCode info openFence lines) = Lean.Markdown.Block.Raw.fencedCode info openFence none lines
- Lean.Markdown.BlockZipper.leafToBlock (Lean.Markdown.OpenLeaf.indentedCode lines _pendingBlanks) = Lean.Markdown.Block.Raw.indentedCode lines
- Lean.Markdown.BlockZipper.leafToBlock (Lean.Markdown.OpenLeaf.html kind lines) = Lean.Markdown.Block.Raw.html lines
Instances For
Closes any open leaf, attaching it to the deepest container.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The least spine index from which every deeper frame is a list item.
It is one past the deepest blockquote, or 0 when there is none.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Records a blank line in the deepest open container.
A blank line between two blocks of a list item, or between two items, makes the list loose (CommonMark §5.3). Which list that is depends on where the next block begins, so the blank line is remembered until then, along with the outermost list it could affect. A blank line inside a blockquote affects only the blockquote's own content.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Records a line that continues the open block. Any blank line before it was inside that block instead of between two blocks, so it doesn't make any list loose.
Equations
Instances For
Closes the deepest container. A blockquote becomes the last child of the container around it. A list item is added to its list, which becomes the pending list of the container around it, since the next line may add another item. Does nothing when only the document is open.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Opens a blockquote as the last child of the deepest open container, closing any open leaf first.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Opens a list item as the last child of the deepest open container, closing any open leaf first. The item joins the container's pending list when that list is of the item's kind; otherwise the item begins a new list. A blank line before the item makes the list that it joins loose.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Replaces the open leaf at the bottom of the spine, closing any prior one.
Equations
- z.openLeaf l = have z := Lean.Markdown.BlockZipper.startBlock✝ z.closeLeaf; { document := z.document, spine := z.spine, openLeaf? := some l, blankLineFloor? := z.blankLineFloor? }
Instances For
Adds a finalized block as a child of the deepest open container, closing any open leaf first. Used
for blocks like ATX headings that don't go through OpenLeaf.
Equations
Instances For
Appends a source line to the open paragraph, if any. Otherwise, does nothing.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Appends a verbatim line to the open fenced code block, if any. Otherwise, does nothing.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Appends a verbatim line to the open HTML block, if any. Otherwise, does nothing.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Closes the open fenced code block with a matching closing fence.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Appends an indented content line to the open indented code block. Any pending blanks that were recorded since the last content line are promoted to real content lines first.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Records a blank line provisionally inside the open indented code block. It is incorporated into the block if a later indented line continues the block; otherwise, it is discarded when the block closes.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Appends m to the markers of the blockquote frame at spine index i. A frame of another kind is
left as it is.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Closes every container down to the document root and returns its children.
Equations
- One or more equations did not get rendered due to their size.