1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
package parser
import (
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
type linkReferenceParagraphTransformer struct {
}
// LinkReferenceParagraphTransformer is a ParagraphTransformer implementation
// that parses and extracts link reference from paragraphs.
var LinkReferenceParagraphTransformer = &linkReferenceParagraphTransformer{}
func (p *linkReferenceParagraphTransformer) Transform(node *ast.Paragraph, reader text.Reader, pc Context) {
lines := node.Lines()
block := text.NewBlockReader(reader.Source(), lines)
removes := [][2]int{}
for {
start, end := parseLinkReferenceDefinition(block, pc)
if start > -1 {
if start == end {
end++
}
removes = append(removes, [2]int{start, end})
continue
}
break
}
offset := 0
for _, remove := range removes {
if lines.Len() == 0 {
break
}
s := lines.Sliced(remove[1]-offset, lines.Len())
lines.SetSliced(0, remove[0]-offset)
lines.AppendAll(s)
offset = remove[1]
}
if lines.Len() == 0 {
t := ast.NewTextBlock()
t.SetBlankPreviousLines(node.HasBlankPreviousLines())
node.Parent().ReplaceChild(node.Parent(), node, t)
return
}
node.SetLines(lines)
}
func parseLinkReferenceDefinition(block text.Reader, pc Context) (int, int) {
block.SkipSpaces()
line, _ := block.PeekLine()
if line == nil {
return -1, -1
}
startLine, _ := block.Position()
width, pos := util.IndentWidth(line, 0)
if width > 3 {
return -1, -1
}
if width != 0 {
pos++
}
if line[pos] != '[' {
return -1, -1
}
block.Advance(pos + 1)
segments, found := block.FindClosure('[', ']', linkFindClosureOptions)
if !found {
return -1, -1
}
var label []byte
if segments.Len() == 1 {
label = block.Value(segments.At(0))
} else {
for i := 0; i < segments.Len(); i++ {
s := segments.At(i)
label = append(label, block.Value(s)...)
}
}
if util.IsBlank(label) {
return -1, -1
}
if block.Peek() != ':' {
return -1, -1
}
block.Advance(1)
block.SkipSpaces()
destination, ok := parseLinkDestination(block)
if !ok {
return -1, -1
}
line, _ = block.PeekLine()
isNewLine := line == nil || util.IsBlank(line)
endLine, _ := block.Position()
_, spaces, _ := block.SkipSpaces()
opener := block.Peek()
if opener != '"' && opener != '\'' && opener != '(' {
if !isNewLine {
return -1, -1
}
ref := NewReference(label, destination, nil)
pc.AddReference(ref)
return startLine, endLine + 1
}
if spaces == 0 {
return -1, -1
}
block.Advance(1)
closer := opener
if opener == '(' {
closer = ')'
}
segments, found = block.FindClosure(opener, closer, linkFindClosureOptions)
if !found {
if !isNewLine {
return -1, -1
}
ref := NewReference(label, destination, nil)
pc.AddReference(ref)
block.AdvanceLine()
return startLine, endLine + 1
}
var title []byte
if segments.Len() == 1 {
title = block.Value(segments.At(0))
} else {
for i := 0; i < segments.Len(); i++ {
s := segments.At(i)
title = append(title, block.Value(s)...)
}
}
line, _ = block.PeekLine()
if line != nil && !util.IsBlank(line) {
if !isNewLine {
return -1, -1
}
ref := NewReference(label, destination, title)
pc.AddReference(ref)
return startLine, endLine
}
endLine, _ = block.Position()
ref := NewReference(label, destination, title)
pc.AddReference(ref)
return startLine, endLine + 1
}
|