Export tree manipulation functions
Pierre Neidhardt ambrevar@gmail.com
Tue, 09 Aug 2016 09:06:55 +0530
4 files changed,
36 insertions(+),
30 deletions(-)
M
inline.go
→
inline.go
@@ -60,9 +60,9 @@ for end < len(data) {
if data[end] == ' ' { consumed, br := maybeLineBreak(p, data, end) if consumed > 0 { - p.currBlock.appendChild(text(data[i:end])) + p.currBlock.AppendChild(text(data[i:end])) if br { - p.currBlock.appendChild(NewNode(Hardbreak)) + p.currBlock.AppendChild(NewNode(Hardbreak)) } i = end i += consumed@@ -83,7 +83,7 @@ end++
} } - p.currBlock.appendChild(text(data[i:end])) + p.currBlock.AppendChild(text(data[i:end])) if end >= len(data) { break@@ -189,7 +189,7 @@ // render the code span
if fBegin != fEnd { code := NewNode(Code) code.Literal = data[fBegin:fEnd] - p.currBlock.appendChild(code) + p.currBlock.AppendChild(code) } return end@@ -214,7 +214,7 @@
// newline without two spaces works when HardLineBreak is enabled func lineBreak(p *parser, data []byte, offset int) int { if p.flags&HardLineBreak != 0 { - p.currBlock.appendChild(NewNode(Hardbreak)) + p.currBlock.AppendChild(NewNode(Hardbreak)) return 1 } return 0@@ -569,9 +569,9 @@ case linkNormal:
linkNode := NewNode(Link) linkNode.Destination = normalizeURI(uLink) linkNode.Title = title - p.currBlock.appendChild(linkNode) + p.currBlock.AppendChild(linkNode) if len(altContent) > 0 { - linkNode.appendChild(text(altContent)) + linkNode.AppendChild(text(altContent)) } else { // links cannot contain other links, so turn off link parsing // temporarily and recurse@@ -588,8 +588,8 @@ case linkImg:
linkNode := NewNode(Image) linkNode.Destination = uLink linkNode.Title = title - p.currBlock.appendChild(linkNode) - linkNode.appendChild(text(data[1:txtE])) + p.currBlock.AppendChild(linkNode) + linkNode.AppendChild(text(data[1:txtE])) i++ case linkInlineFootnote, linkDeferredFootnote:@@ -597,7 +597,7 @@ linkNode := NewNode(Link)
linkNode.Destination = link linkNode.Title = title linkNode.NoteID = noteID - p.currBlock.appendChild(linkNode) + p.currBlock.AppendChild(linkNode) if t == linkInlineFootnote { i++ }@@ -666,13 +666,13 @@ node.Destination = link
if altype == emailAutolink { node.Destination = append([]byte("mailto:"), link...) } - p.currBlock.appendChild(node) - node.appendChild(text(stripMailto(link))) + p.currBlock.AppendChild(node) + node.AppendChild(text(stripMailto(link))) } } else { htmlTag := NewNode(HTMLSpan) htmlTag.Literal = data[:end] - p.currBlock.appendChild(htmlTag) + p.currBlock.AppendChild(htmlTag) } }@@ -687,14 +687,14 @@ data = data[offset:]
if len(data) > 1 { if p.flags&BackslashLineBreak != 0 && data[1] == '\n' { - p.currBlock.appendChild(NewNode(Hardbreak)) + p.currBlock.AppendChild(NewNode(Hardbreak)) return 2 } if bytes.IndexByte(escapeChars, data[1]) < 0 { return 0 } - p.currBlock.appendChild(text(data[1:2])) + p.currBlock.AppendChild(text(data[1:2])) } return 2@@ -748,7 +748,7 @@ // escaper in the renderer
if bytes.Equal(ent, []byte("&")) { ent = []byte{'&'} } - p.currBlock.appendChild(text(ent)) + p.currBlock.AppendChild(text(ent)) return end }@@ -796,7 +796,7 @@ anchorStr := anchorRe.Find(data[anchorStart:])
if anchorStr != nil { anchorClose := NewNode(HTMLSpan) anchorClose.Literal = anchorStr[offsetFromAnchor:] - p.currBlock.appendChild(anchorClose) + p.currBlock.AppendChild(anchorClose) return len(anchorStr) - offsetFromAnchor }@@ -896,8 +896,8 @@
if uLink.Len() > 0 { node := NewNode(Link) node.Destination = uLink.Bytes() - p.currBlock.appendChild(node) - node.appendChild(text(uLink.Bytes())) + p.currBlock.AppendChild(node) + node.AppendChild(text(uLink.Bytes())) } return linkEnd@@ -1146,7 +1146,7 @@ }
} emph := NewNode(Emph) - p.currBlock.appendChild(emph) + p.currBlock.AppendChild(emph) tmp := p.currBlock p.currBlock = emph p.inline(data[:i])@@ -1174,7 +1174,7 @@ if c == '~' {
nodeType = Del } node := NewNode(nodeType) - p.currBlock.appendChild(node) + p.currBlock.AppendChild(node) tmp := p.currBlock p.currBlock = node p.inline(data[:i])@@ -1208,8 +1208,8 @@ case i+2 < len(data) && data[i+1] == c && data[i+2] == c:
// triple symbol found strong := NewNode(Strong) em := NewNode(Emph) - strong.appendChild(em) - p.currBlock.appendChild(strong) + strong.AppendChild(em) + p.currBlock.AppendChild(strong) tmp := p.currBlock p.currBlock = em p.inline(data[:i])
M
markdown.go
→
markdown.go
@@ -231,7 +231,7 @@ p.finalize(p.tip)
} newNode := NewNode(node) newNode.content = []byte{} - p.tip.appendChild(newNode) + p.tip.AppendChild(newNode) p.tip = newNode return newNode }
M
node.go
→
node.go
@@ -148,7 +148,9 @@ }
return fmt.Sprintf("%s: '%s%s'", n.Type, snippet, ellipsis) } -func (n *Node) unlink() { +// Unlink removes node 'n' from the tree. +// It panics if the node is nil. +func (n *Node) Unlink() { if n.Prev != nil { n.Prev.Next = n.Next } else if n.Parent != nil {@@ -164,8 +166,10 @@ n.Next = nil
n.Prev = nil } -func (n *Node) appendChild(child *Node) { - child.unlink() +// AppendChild adds a node 'child' as a child of 'n'. +// It panics if either node is nil. +func (n *Node) AppendChild(child *Node) { + child.Unlink() child.Parent = n if n.LastChild != nil { n.LastChild.Next = child@@ -177,8 +181,10 @@ n.LastChild = child
} } -func (n *Node) insertBefore(sibling *Node) { - sibling.unlink() +// InsertBefore inserts 'sibling' immediately before 'n'. +// It panics if either node is nil. +func (n *Node) InsertBefore(sibling *Node) { + sibling.Unlink() sibling.Prev = n.Prev if sibling.Prev != nil { sibling.Prev.Next = sibling