diff --git a/html.go b/html.go index b304305..0158ce7 100644 --- a/html.go +++ b/html.go @@ -1394,7 +1394,7 @@ func (r *HTML) Render(ast *Node) []byte { //dump(ast) // Run Smartypants if it's enabled or simply escape text if not sr := NewSmartypantsRenderer(r.extensions) - ForEachNode(ast, func(node *Node, entering bool) { + ast.Walk(func(node *Node, entering bool) { if node.Type == Text { if r.extensions&Smartypants != 0 { node.Literal = sr.Process(node.Literal) @@ -1404,7 +1404,7 @@ func (r *HTML) Render(ast *Node) []byte { } }) var buff bytes.Buffer - ForEachNode(ast, func(node *Node, entering bool) { + ast.Walk(func(node *Node, entering bool) { r.RenderNode(&buff, node, entering) }) return buff.Bytes() diff --git a/markdown.go b/markdown.go index 9e5a7aa..61c5468 100644 --- a/markdown.go +++ b/markdown.go @@ -450,7 +450,7 @@ func Parse(input []byte, opts Options) *Node { p.finalize(p.tip) } // Walk the tree again and process inline markdown in each block - ForEachNode(p.doc, func(node *Node, entering bool) { + p.doc.Walk(func(node *Node, entering bool) { if node.Type == Paragraph || node.Type == Header || node.Type == TableCell { p.currBlock = node p.inline(node.content) @@ -493,7 +493,7 @@ func (p *parser) parseRefsToAST() { finalizeList(block) p.tip = above finalizeHtmlBlock(p.addBlock(HtmlBlock, []byte(""))) - ForEachNode(block, func(node *Node, entering bool) { + block.Walk(func(node *Node, entering bool) { if node.Type == Paragraph || node.Type == Header { p.currBlock = node p.inline(node.content) diff --git a/node.go b/node.go index 58b8359..ff3069f 100644 --- a/node.go +++ b/node.go @@ -216,6 +216,15 @@ func (n *Node) canContain(t NodeType) bool { return false } +func (root *Node) Walk(visitor func(node *Node, entering bool)) { + walker := NewNodeWalker(root) + node, entering := walker.next() + for node != nil { + visitor(node, entering) + node, entering = walker.next() + } +} + type NodeWalker struct { current *Node root *Node @@ -263,15 +272,6 @@ func (nw *NodeWalker) resumeAt(node *Node, entering bool) { nw.entering = entering } -func ForEachNode(root *Node, f func(node *Node, entering bool)) { - walker := NewNodeWalker(root) - node, entering := walker.next() - for node != nil { - f(node, entering) - node, entering = walker.next() - } -} - func dump(ast *Node) { fmt.Println(dumpString(ast)) }