forked from ego008/go-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reference.go
157 lines (133 loc) · 2.59 KB
/
reference.go
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
// Copyright 2015 The Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package markdown
import "strings"
var referenceTerminatedBy []BlockRule
func ruleReference(s *StateBlock, startLine, _ int, silent bool) (_ bool) {
pos := s.BMarks[startLine] + s.TShift[startLine]
src := s.Src
if src[pos] != '[' {
return
}
pos++
max := s.EMarks[startLine]
for pos < max {
if src[pos] == ']' && src[pos-1] != '\\' {
if pos+1 == max {
return
}
if src[pos+1] != ':' {
return
}
break
}
pos++
}
nextLine := startLine + 1
endLine := s.LineMax
outer:
for ; nextLine < endLine && !s.IsLineEmpty(nextLine); nextLine++ {
if s.TShift[nextLine]-s.BlkIndent > 3 {
continue
}
for _, r := range referenceTerminatedBy {
if r(s, nextLine, endLine, true) {
break outer
}
}
}
str := strings.TrimSpace(s.Lines(startLine, nextLine, s.BlkIndent, false))
max = len(str)
lines := 0
var labelEnd int
for pos = 1; pos < max; pos++ {
b := str[pos]
if b == '[' {
return
} else if b == ']' {
labelEnd = pos
break
} else if b == '\n' {
lines++
} else if b == '\\' {
pos++
if pos < max && str[pos] == '\n' {
lines++
}
}
}
if labelEnd <= 0 || labelEnd+1 >= max || str[labelEnd+1] != ':' {
return
}
for pos = labelEnd + 2; pos < max; pos++ {
b := str[pos]
if b == '\n' {
lines++
} else if b != ' ' {
break
}
}
href, endpos, ok := parseLinkDestination(str, pos, max)
if !ok {
return
}
href = normalizeLink(href)
if !validateLink(href) {
return
}
pos = endpos
savedPos := pos
savedLineNo := lines
start := pos
for ; pos < max; pos++ {
b := str[pos]
if b == '\n' {
lines++
} else if b != ' ' {
break
}
}
title, nlines, endpos, ok := parseLinkTitle(str, pos, max)
if pos < max && start != pos && ok {
pos = endpos
lines += nlines
} else {
pos = savedPos
lines = savedLineNo
}
for pos < max && str[pos] == ' ' {
pos++
}
if pos < max && str[pos] != '\n' {
if title != "" {
title = ""
pos = savedPos
lines = savedLineNo
for pos < max && src[pos] == ' ' {
pos++
}
}
}
if pos < max && str[pos] != '\n' {
return
}
label := normalizeReference(str[1:labelEnd])
if label == "" {
return false
}
if silent {
return true
}
if s.Env.References == nil {
s.Env.References = make(map[string]map[string]string)
}
if _, ok := s.Env.References[label]; !ok {
s.Env.References[label] = map[string]string{
"title": title,
"href": href,
}
}
s.Line = startLine + lines + 1
return true
}