Constructing trees
The @ast
macro can be used to construct complex trees with relatively little effort, without having to construct and push all the Node
instances yourself.
MarkdownAST.@ast
— Macro@ast markdown-node-expression
A macro that implements a simple domain specific language to easily and explicitly construct a Markdown AST.
The markdown-node-expression
must be either:
A Markdown element (i.e. some
AbstractElement
object), such as a constructor call (e.g.Paragraph()
), function call returning an element, or a variable pointing to an element.A variable or an expression returning or pointing to a
Node
object, which will then get unlinked from its current tree and pushed as a child (together with all of its descendents).A
do
-block, with the function call part being an element (as above), and the contents of thedo
-block a sequence of other node expressions, i.e.element do child-node-expression-1 child-node-expression-2 ... end
In practice, a simple example might look something like
@ast Document() do
Heading(1) do
"Top-level heading"
end
Paragraph() do
"Some paragraph text"
end
end
Strings are interpreted as Text(s)
elements.