-
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
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257
-
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
-
270
-
271
-
272
-
273
-
274
-
275
-
276
-
277
-
278
-
279
-
280
-
281
-
282
-
283
-
284
-
285
-
286
-
287
-
288
-
289
-
290
-
291
-
292
-
293
-
294
-
295
-
296
-
297
-
298
-
299
-
300
-
301
-
302
-
303
-
304
-
305
-
306
-
307
-
308
-
309
-
310
-
311
-
312
-
313
-
314
-
315
-
316
-
317
-
318
-
319
-
320
-
321
-
322
-
323
-
324
-
325
-
326
-
327
-
328
-
329
-
330
-
331
-
332
-
333
-
334
-
335
-
336
-
337
-
338
-
339
-
340
-
341
-
342
-
343
-
344
-
345
-
346
-
347
-
348
-
349
-
350
-
351
-
352
-
353
-
354
-
355
-
356
-
357
-
358
-
359
-
360
-
361
-
362
-
363
-
364
-
365
-
366
-
367
-
368
-
369
-
370
-
371
-
372
-
373
-
374
-
375
-
376
-
377
-
378
-
379
package parser
import (
"fmt"
"strings"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
var linkLabelStateKey = NewContextKey()
type linkLabelState struct {
ast.BaseInline
Segment text.Segment
IsImage bool
Prev *linkLabelState
Next *linkLabelState
First *linkLabelState
Last *linkLabelState
}
func newLinkLabelState(segment text.Segment, isImage bool) *linkLabelState {
return &linkLabelState{
Segment: segment,
IsImage: isImage,
}
}
func (s *linkLabelState) Text(source []byte) []byte {
return s.Segment.Value(source)
}
func (s *linkLabelState) Dump(source []byte, level int) {
fmt.Printf("%slinkLabelState: \"%s\"\n", strings.Repeat(" ", level), s.Text(source))
}
var kindLinkLabelState = ast.NewNodeKind("LinkLabelState")
func (s *linkLabelState) Kind() ast.NodeKind {
return kindLinkLabelState
}
func pushLinkLabelState(pc Context, v *linkLabelState) {
tlist := pc.Get(linkLabelStateKey)
var list *linkLabelState
if tlist == nil {
list = v
v.First = v
v.Last = v
pc.Set(linkLabelStateKey, list)
} else {
list = tlist.(*linkLabelState)
l := list.Last
list.Last = v
l.Next = v
v.Prev = l
}
}
func removeLinkLabelState(pc Context, d *linkLabelState) {
tlist := pc.Get(linkLabelStateKey)
var list *linkLabelState
if tlist == nil {
return
}
list = tlist.(*linkLabelState)
if d.Prev == nil {
list = d.Next
if list != nil {
list.First = d
list.Last = d.Last
list.Prev = nil
pc.Set(linkLabelStateKey, list)
} else {
pc.Set(linkLabelStateKey, nil)
}
} else {
d.Prev.Next = d.Next
if d.Next != nil {
d.Next.Prev = d.Prev
}
}
if list != nil && d.Next == nil {
list.Last = d.Prev
}
d.Next = nil
d.Prev = nil
d.First = nil
d.Last = nil
}
type linkParser struct {
}
var defaultLinkParser = &linkParser{}
// NewLinkParser return a new InlineParser that parses links.
func NewLinkParser() InlineParser {
return defaultLinkParser
}
func (s *linkParser) Trigger() []byte {
return []byte{'!', '[', ']'}
}
var linkBottom = NewContextKey()
func (s *linkParser) Parse(parent ast.Node, block text.Reader, pc Context) ast.Node {
line, segment := block.PeekLine()
if line[0] == '!' {
if len(line) > 1 && line[1] == '[' {
block.Advance(1)
pc.Set(linkBottom, pc.LastDelimiter())
return processLinkLabelOpen(block, segment.Start+1, true, pc)
}
return nil
}
if line[0] == '[' {
pc.Set(linkBottom, pc.LastDelimiter())
return processLinkLabelOpen(block, segment.Start, false, pc)
}
// line[0] == ']'
tlist := pc.Get(linkLabelStateKey)
if tlist == nil {
return nil
}
last := tlist.(*linkLabelState).Last
if last == nil {
return nil
}
block.Advance(1)
removeLinkLabelState(pc, last)
if s.containsLink(last) { // a link in a link text is not allowed
ast.MergeOrReplaceTextSegment(last.Parent(), last, last.Segment)
return nil
}
c := block.Peek()
l, pos := block.Position()
var link *ast.Link
var hasValue bool
if c == '(' { // normal link
link = s.parseLink(parent, last, block, pc)
} else if c == '[' { // reference link
link, hasValue = s.parseReferenceLink(parent, last, block, pc)
if link == nil && hasValue {
ast.MergeOrReplaceTextSegment(last.Parent(), last, last.Segment)
return nil
}
}
if link == nil {
// maybe shortcut reference link
block.SetPosition(l, pos)
ssegment := text.NewSegment(last.Segment.Stop, segment.Start)
maybeReference := block.Value(ssegment)
ref, ok := pc.Reference(util.ToLinkReference(maybeReference))
if !ok {
ast.MergeOrReplaceTextSegment(last.Parent(), last, last.Segment)
return nil
}
link = ast.NewLink()
s.processLinkLabel(parent, link, last, pc)
link.Title = ref.Title()
link.Destination = ref.Destination()
}
if last.IsImage {
last.Parent().RemoveChild(last.Parent(), last)
return ast.NewImage(link)
}
last.Parent().RemoveChild(last.Parent(), last)
return link
}
func (s *linkParser) containsLink(last *linkLabelState) bool {
if last.IsImage {
return false
}
var c ast.Node
for c = last; c != nil; c = c.NextSibling() {
if _, ok := c.(*ast.Link); ok {
return true
}
}
return false
}
func processLinkLabelOpen(block text.Reader, pos int, isImage bool, pc Context) *linkLabelState {
start := pos
if isImage {
start--
}
state := newLinkLabelState(text.NewSegment(start, pos+1), isImage)
pushLinkLabelState(pc, state)
block.Advance(1)
return state
}
func (s *linkParser) processLinkLabel(parent ast.Node, link *ast.Link, last *linkLabelState, pc Context) {
var bottom ast.Node
if v := pc.Get(linkBottom); v != nil {
bottom = v.(ast.Node)
}
pc.Set(linkBottom, nil)
ProcessDelimiters(bottom, pc)
for c := last.NextSibling(); c != nil; {
next := c.NextSibling()
parent.RemoveChild(parent, c)
link.AppendChild(link, c)
c = next
}
}
func (s *linkParser) parseReferenceLink(parent ast.Node, last *linkLabelState, block text.Reader, pc Context) (*ast.Link, bool) {
_, orgpos := block.Position()
block.Advance(1) // skip '['
line, segment := block.PeekLine()
endIndex := util.FindClosure(line, '[', ']', false, true)
if endIndex < 0 {
return nil, false
}
block.Advance(endIndex + 1)
ssegment := segment.WithStop(segment.Start + endIndex)
maybeReference := block.Value(ssegment)
if util.IsBlank(maybeReference) { // collapsed reference link
ssegment = text.NewSegment(last.Segment.Stop, orgpos.Start-1)
maybeReference = block.Value(ssegment)
}
ref, ok := pc.Reference(util.ToLinkReference(maybeReference))
if !ok {
return nil, true
}
link := ast.NewLink()
s.processLinkLabel(parent, link, last, pc)
link.Title = ref.Title()
link.Destination = ref.Destination()
return link, true
}
func (s *linkParser) parseLink(parent ast.Node, last *linkLabelState, block text.Reader, pc Context) *ast.Link {
block.Advance(1) // skip '('
block.SkipSpaces()
var title []byte
var destination []byte
var ok bool
if block.Peek() == ')' { // empty link like '[link]()'
block.Advance(1)
} else {
destination, ok = parseLinkDestination(block)
if !ok {
return nil
}
block.SkipSpaces()
if block.Peek() == ')' {
block.Advance(1)
} else {
title, ok = parseLinkTitle(block)
if !ok {
return nil
}
block.SkipSpaces()
if block.Peek() == ')' {
block.Advance(1)
} else {
return nil
}
}
}
link := ast.NewLink()
s.processLinkLabel(parent, link, last, pc)
link.Destination = destination
link.Title = title
return link
}
func parseLinkDestination(block text.Reader) ([]byte, bool) {
block.SkipSpaces()
line, _ := block.PeekLine()
if block.Peek() == '<' {
i := 1
for i < len(line) {
c := line[i]
if c == '\\' && i < len(line)-1 && util.IsPunct(line[i+1]) {
i += 2
continue
} else if c == '>' {
block.Advance(i + 1)
return line[1:i], true
}
i++
}
return nil, false
}
opened := 0
i := 0
for i < len(line) {
c := line[i]
if c == '\\' && i < len(line)-1 && util.IsPunct(line[i+1]) {
i += 2
continue
} else if c == '(' {
opened++
} else if c == ')' {
opened--
if opened < 0 {
break
}
} else if util.IsSpace(c) {
break
}
i++
}
block.Advance(i)
return line[:i], len(line[:i]) != 0
}
func parseLinkTitle(block text.Reader) ([]byte, bool) {
block.SkipSpaces()
opener := block.Peek()
if opener != '"' && opener != '\'' && opener != '(' {
return nil, false
}
closer := opener
if opener == '(' {
closer = ')'
}
savedLine, savedPosition := block.Position()
var title []byte
for i := 0; ; i++ {
line, _ := block.PeekLine()
if line == nil {
block.SetPosition(savedLine, savedPosition)
return nil, false
}
offset := 0
if i == 0 {
offset = 1
}
pos := util.FindClosure(line[offset:], opener, closer, false, true)
if pos < 0 {
title = append(title, line[offset:]...)
block.AdvanceLine()
continue
}
pos += offset + 1 // 1: closer
block.Advance(pos)
if i == 0 { // avoid allocating new slice
return line[offset : pos-1], true
}
return append(title, line[offset:pos-1]...), true
}
}
func (s *linkParser) CloseBlock(parent ast.Node, block text.Reader, pc Context) {
tlist := pc.Get(linkLabelStateKey)
if tlist == nil {
return
}
for s := tlist.(*linkLabelState); s != nil; {
next := s.Next
removeLinkLabelState(pc, s)
s.Parent().ReplaceChild(s.Parent(), s, ast.NewTextSegment(s.Segment))
s = next
}
}