This repository has been archived by the owner on Nov 10, 2020. It is now read-only.
forked from russross/blackfriday
-
Notifications
You must be signed in to change notification settings - Fork 24
/
quote.go
220 lines (197 loc) · 4.51 KB
/
quote.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
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
// Functions to parse quote-like elements.
package mmark
import "bytes"
// returns asidequote prefix length
func (p *parser) asidePrefix(data []byte) int {
i := 0
for i < 3 && data[i] == ' ' {
i++
}
if data[i] == 'A' && data[i+1] == '>' {
if data[i+2] == ' ' {
return i + 3
}
return i + 2
}
return 0
}
// parse an aside fragment
func (p *parser) aside(out *bytes.Buffer, data []byte) int {
var raw bytes.Buffer
beg, end := 0, 0
for beg < len(data) {
end = beg
for data[end] != '\n' {
end++
}
end++
if pre := p.asidePrefix(data[beg:]); pre > 0 {
// skip the prefix
beg += pre
} else if p.isEmpty(data[beg:]) > 0 &&
(end >= len(data) ||
(p.asidePrefix(data[end:]) == 0 && p.isEmpty(data[end:]) == 0)) {
break
}
raw.Write(data[beg:end])
beg = end
}
var cooked bytes.Buffer
p.block(&cooked, raw.Bytes())
p.r.SetAttr(p.ial)
p.ial = nil
p.r.Aside(out, cooked.Bytes())
return end
}
// returns blockquote prefix length
func (p *parser) quotePrefix(data []byte) int {
i := 0
for i < 3 && data[i] == ' ' {
i++
}
if data[i] == '>' {
if data[i+1] == ' ' {
return i + 2
}
return i + 1
}
return 0
}
// blockquote ends with at least one blank line
// followed by something without a blockquote prefix
func (p *parser) terminateBlockquote(data []byte, beg, end int) bool {
if p.isEmpty(data[beg:]) <= 0 {
return false
}
if end >= len(data) {
return true
}
return p.quotePrefix(data[end:]) == 0 && p.isEmpty(data[end:]) == 0
}
// parse a blockquote fragment
func (p *parser) quote(out *bytes.Buffer, data []byte) int {
var raw bytes.Buffer
beg, end := 0, 0
for beg < len(data) {
end = beg
// Step over whole lines, collecting them. While doing that, check for
// fenced code and if one's found, incorporate it altogether,
// irregardless of any contents inside it
for data[end] != '\n' {
if p.flags&EXTENSION_FENCED_CODE != 0 {
if i := p.fencedCode(out, data[end:], false); i > 0 {
// -1 to compensate for the extra end++ after the loop:
end += i - 1
break
}
}
end++
}
end++
if pre := p.quotePrefix(data[beg:]); pre > 0 {
// skip the prefix
beg += pre
} else if bytes.HasPrefix(data[beg:], []byte("Quote: ")) {
break
} else if p.terminateBlockquote(data, beg, end) {
break
}
// this line is part of the blockquote
raw.Write(data[beg:end])
beg = end
}
var attribution bytes.Buffer
line := beg
j := beg
if bytes.HasPrefix(data[j:], []byte("Quote: ")) {
for line < len(data) {
j++
// find the end of this line
for data[j-1] != '\n' {
j++
}
if p.isEmpty(data[line:j]) > 0 {
break
}
line = j
}
p.inline(&attribution, data[beg+7:j-1]) // +7 for 'Quote: '
}
ials := p.ial
p.ial = nil
// This set a new level of attributes, we could possible override what
// we have gotten above. TODO(miek): this might need to happen in more
// places.
var cooked bytes.Buffer
p.block(&cooked, raw.Bytes())
p.r.SetAttr(ials)
p.r.BlockQuote(out, cooked.Bytes(), attribution.Bytes())
return j
}
// returns figurequote prefix length
func (p *parser) figurePrefix(data []byte) int {
i := 0
for i < 3 && data[i] == ' ' {
i++
}
if data[i] == 'F' && data[i+1] == '>' {
if data[i+2] == ' ' {
return i + 3
}
return i + 2
}
return 0
}
// parse a figurequote fragment
func (p *parser) figure(out *bytes.Buffer, data []byte) int {
var raw bytes.Buffer
beg, end := 0, 0
for beg < len(data) {
end = beg
for data[end] != '\n' {
end++
}
end++
if pre := p.figurePrefix(data[beg:]); pre > 0 {
// skip the prefix
beg += pre
} else if bytes.HasPrefix(data[beg:], []byte("Figure: ")) {
break
} else if p.isEmpty(data[beg:]) > 0 &&
(end >= len(data) ||
(p.figurePrefix(data[end:]) == 0 && p.isEmpty(data[end:]) == 0)) {
// figurequote ends with at least one blank line
// followed by something without a figurequote prefix
break
}
// this line is part of the figurequote
raw.Write(data[beg:end])
beg = end
}
var caption bytes.Buffer
line := beg
j := beg
// this one must start on j
if bytes.HasPrefix(data[j:], []byte("Figure: ")) {
for line < len(data) {
j++
// find the end of this line
for data[j-1] != '\n' {
j++
}
if p.isEmpty(data[line:j]) > 0 {
break
}
line = j
}
p.inline(&caption, data[beg+8:j-1]) // +8 for 'Figure: '
}
p.insideFigure = true
var cooked bytes.Buffer
p.block(&cooked, raw.Bytes())
p.insideFigure = false
p.r.SetAttr(p.ial)
p.ial = nil
p.r.Figure(out, cooked.Bytes(), caption.Bytes())
return j
}