mirror of
https://github.com/status-im/markdown.git
synced 2025-02-23 00:28:25 +00:00
Make ForEachNode func a Walk method on Node
This commit is contained in:
parent
04673c9f28
commit
f1361aa0da
4
html.go
4
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()
|
||||
|
@ -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
18
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))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user