Make ForEachNode func a Walk method on Node

This commit is contained in:
Vytautas Šaltenis 2016-04-01 12:36:56 +03:00
parent 04673c9f28
commit f1361aa0da
3 changed files with 13 additions and 13 deletions

View File

@ -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()

View File

@ -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("</div>")))
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)

18
node.go
View File

@ -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))
}