Iteration over trees
The Node
type implements the AbstractTrees interface which provides various general tree-iteration algorithms.
Using the following MarkdownAST tree as an example:
md = @ast Document() do
Heading(1) do; "Iteration example"; end
Paragraph() do
"MarkdownAST trees can be iterated over with "
Strong() do; "AbstractTrees"; end
"."
end
Paragraph() do; "The use it, load the package with"; end
CodeBlock("julia", "using AbstractTrees")
end
The different AbstractTrees iterators, such as PostOrderDFS
, PreOrderDFS
, or Leaves
, can be used to construct iterators from the md
variable (which is an instance of Node
). Each algorithm provides a way to iterate through the trees in a different way, as can be seen in the following examples:
using AbstractTrees
for node in PostOrderDFS(md)
println(node.element)
end
MarkdownAST.Text("Iteration example")
MarkdownAST.Heading(1)
MarkdownAST.Text("MarkdownAST trees can be iterated over with ")
MarkdownAST.Text("AbstractTrees")
MarkdownAST.Strong()
MarkdownAST.Text(".")
MarkdownAST.Paragraph()
MarkdownAST.Text("The use it, load the package with")
MarkdownAST.Paragraph()
MarkdownAST.CodeBlock("julia", "using AbstractTrees")
MarkdownAST.Document()
for node in PreOrderDFS(md)
println(node.element)
end
MarkdownAST.Document()
MarkdownAST.Heading(1)
MarkdownAST.Text("Iteration example")
MarkdownAST.Paragraph()
MarkdownAST.Text("MarkdownAST trees can be iterated over with ")
MarkdownAST.Strong()
MarkdownAST.Text("AbstractTrees")
MarkdownAST.Text(".")
MarkdownAST.Paragraph()
MarkdownAST.Text("The use it, load the package with")
MarkdownAST.CodeBlock("julia", "using AbstractTrees")
using AbstractTrees
for node in Leaves(md)
println(node.element)
end
MarkdownAST.Text("Iteration example")
MarkdownAST.Text("MarkdownAST trees can be iterated over with ")
MarkdownAST.Text("AbstractTrees")
MarkdownAST.Text(".")
MarkdownAST.Text("The use it, load the package with")
MarkdownAST.CodeBlock("julia", "using AbstractTrees")