forked from russross/blackfriday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inline.go
1483 lines (1276 loc) · 29.3 KB
/
inline.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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Blackfriday Markdown Processor
// Available at http://github.com/russross/blackfriday
//
// Copyright © 2011 Russ Ross <[email protected]>.
// Distributed under the Simplified BSD License.
// See README.md for details.
//
//
// Functions to parse inline elements.
//
package blackthunder
import (
"bytes"
"errors"
"regexp"
"strconv"
)
var (
urlRe = `((https?|ftp):\/\/|\/)[-A-Za-z0-9+&@#\/%?=~_|!:,.;\(\)]+`
anchorRe = regexp.MustCompile(`^(<a\shref="` + urlRe + `"(\stitle="[^"<>]+")?\s?>` + urlRe + `<\/a>)`)
// TODO: improve this regexp to catch all possible entities:
htmlEntityRe = regexp.MustCompile(`&[a-z]{2,5};`)
)
type parsedCTagType int
const (
BEGIN parsedCTagType = iota + 1
TEXT
CLOSE
SINGLE
)
type parsedCTag struct {
end int
kind parsedCTagType
name string
attr map[string]string
args []string
content []byte
parseChildren bool
parseBlock bool
}
type cNode struct {
children []*cNode
this *parsedCTag
}
// Functions to parse text within a block
// Each function returns the number of chars taken care of
// data is the complete block being rendered
// offset is the number of valid chars before the current cursor
func (p *parser) inline(currBlock *Node, data []byte) {
// this is called recursively: enforce a maximum depth
if p.nesting >= p.maxNesting {
return
}
p.nesting++
i, end := 0, 0
for i < len(data) {
// Stop at EOL
if data[i] == '\n' && i+1 == len(data) {
break
}
for ; end < len(data); end++ {
if p.inlineCallback[data[end]] != nil {
break
}
}
if end >= len(data) {
if data[end-1] == '\n' {
currBlock.AppendChild(text(data[i : end-1]))
} else {
currBlock.AppendChild(text(data[i:end]))
}
break
}
// call the trigger
handler := p.inlineCallback[data[end]]
if consumed, node := handler(p, data, end); consumed == 0 {
// No action from the callback.
end++
} else {
// Copy inactive chars into the output.
currBlock.AppendChild(text(data[i:end]))
if node != nil {
currBlock.AppendChild(node)
}
// Skip past whatever the callback used.
i = end + consumed
end = i
}
}
p.nesting--
}
// single and double emphasis parsing
func emphasis(p *parser, data []byte, offset int) (int, *Node) {
data = data[offset:]
c := data[0]
if len(data) > 2 && data[1] != c {
// whitespace cannot follow an opening emphasis;
// strikethrough only takes two characters '~~'
if c == '~' || isspace(data[1]) {
return 0, nil
}
ret, node := helperEmphasis(p, data[1:], c)
if ret == 0 {
return 0, nil
}
return ret + 1, node
}
if len(data) > 3 && data[1] == c && data[2] != c {
if isspace(data[2]) {
return 0, nil
}
ret, node := helperDoubleEmphasis(p, data[2:], c)
if ret == 0 {
return 0, nil
}
return ret + 2, node
}
if len(data) > 4 && data[1] == c && data[2] == c && data[3] != c {
if c == '~' || isspace(data[3]) {
return 0, nil
}
ret, node := helperTripleEmphasis(p, data, 3, c)
if ret == 0 {
return 0, nil
}
return ret + 3, node
}
return 0, nil
}
func codeSpan(p *parser, data []byte, offset int) (int, *Node) {
data = data[offset:]
nb := 0
// count the number of backticks in the delimiter
for nb < len(data) && data[nb] == '`' {
nb++
}
// find the next delimiter
i, end := 0, 0
for end = nb; end < len(data) && i < nb; end++ {
if data[end] == '`' {
i++
} else {
i = 0
}
}
// no matching delimiter?
if i < nb && end >= len(data) {
return 0, nil
}
// trim outside whitespace
fBegin := nb
for fBegin < end && data[fBegin] == ' ' {
fBegin++
}
fEnd := end - nb
for fEnd > fBegin && data[fEnd-1] == ' ' {
fEnd--
}
// render the code span
if fBegin != fEnd {
code := NewNode(Code)
code.Literal = data[fBegin:fEnd]
return end, code
}
return end, nil
}
// newline preceded by two spaces becomes <br>
func maybeLineBreak(p *parser, data []byte, offset int) (int, *Node) {
origOffset := offset
for offset < len(data) && data[offset] == ' ' {
offset++
}
if offset < len(data) && data[offset] == '\n' {
if offset-origOffset >= 2 {
return offset - origOffset + 1, NewNode(Hardbreak)
}
return offset - origOffset, nil
}
return 0, nil
}
// newline without two spaces works when HardLineBreak is enabled
func lineBreak(p *parser, data []byte, offset int) (int, *Node) {
if p.flags&HardLineBreak != 0 {
return 1, NewNode(Hardbreak)
}
return 0, nil
}
type linkType int
const (
linkNormal linkType = iota
linkImg
linkDeferredFootnote
linkInlineFootnote
)
func isReferenceStyleLink(data []byte, pos int, t linkType) bool {
if t == linkDeferredFootnote {
return false
}
return pos < len(data)-1 && data[pos] == '[' && data[pos+1] != '^'
}
func maybeImage(p *parser, data []byte, offset int) (int, *Node) {
if offset < len(data)-1 && data[offset+1] == '[' {
return link(p, data, offset)
}
return 0, nil
}
func maybeInlineFootnote(p *parser, data []byte, offset int) (int, *Node) {
if offset < len(data)-1 && data[offset+1] == '[' {
return link(p, data, offset)
}
return 0, nil
}
// '[': parse a link or an image or a footnote
func link(p *parser, data []byte, offset int) (int, *Node) {
// no links allowed inside regular links, footnote, and deferred footnotes
if p.insideLink && (offset > 0 && data[offset-1] == '[' || len(data)-1 > offset && data[offset+1] == '^') {
return 0, nil
}
var t linkType
switch {
// special case: ![^text] == deferred footnote (that follows something with
// an exclamation point)
case p.flags&Footnotes != 0 && len(data)-1 > offset && data[offset+1] == '^':
t = linkDeferredFootnote
// ![alt] == image
case offset >= 0 && data[offset] == '!':
t = linkImg
offset++
// ^[text] == inline footnote
// [^refId] == deferred footnote
case p.flags&Footnotes != 0:
if offset >= 0 && data[offset] == '^' {
t = linkInlineFootnote
offset++
} else if len(data)-1 > offset && data[offset+1] == '^' {
t = linkDeferredFootnote
}
// [text] == regular link
default:
t = linkNormal
}
data = data[offset:]
var (
i = 1
noteID int
title, link, altContent []byte
textHasNl = false
)
if t == linkDeferredFootnote {
i++
}
// look for the matching closing bracket
for level := 1; level > 0 && i < len(data); i++ {
switch {
case data[i] == '\n':
textHasNl = true
case data[i-1] == '\\':
continue
case data[i] == '[':
level++
case data[i] == ']':
level--
if level <= 0 {
i-- // compensate for extra i++ in for loop
}
}
}
if i >= len(data) {
return 0, nil
}
txtE := i
i++
var footnoteNode *Node
// skip any amount of whitespace or newline
// (this is much more lax than original markdown syntax)
for i < len(data) && isspace(data[i]) {
i++
}
// inline style link
switch {
case i < len(data) && data[i] == '(':
// skip initial whitespace
i++
for i < len(data) && isspace(data[i]) {
i++
}
linkB := i
// look for link end: ' " )
findlinkend:
for i < len(data) {
switch {
case data[i] == '\\':
i += 2
case data[i] == ')' || data[i] == '\'' || data[i] == '"':
break findlinkend
default:
i++
}
}
if i >= len(data) {
return 0, nil
}
linkE := i
// look for title end if present
titleB, titleE := 0, 0
if data[i] == '\'' || data[i] == '"' {
i++
titleB = i
findtitleend:
for i < len(data) {
switch {
case data[i] == '\\':
i += 2
case data[i] == ')':
break findtitleend
default:
i++
}
}
if i >= len(data) {
return 0, nil
}
// skip whitespace after title
titleE = i - 1
for titleE > titleB && isspace(data[titleE]) {
titleE--
}
// check for closing quote presence
if data[titleE] != '\'' && data[titleE] != '"' {
titleB, titleE = 0, 0
linkE = i
}
}
// remove whitespace at the end of the link
for linkE > linkB && isspace(data[linkE-1]) {
linkE--
}
// remove optional angle brackets around the link
if data[linkB] == '<' {
linkB++
}
if data[linkE-1] == '>' {
linkE--
}
// build escaped link and title
if linkE > linkB {
link = data[linkB:linkE]
}
if titleE > titleB {
title = data[titleB:titleE]
}
i++
// reference style link
case isReferenceStyleLink(data, i, t):
var id []byte
altContentConsidered := false
// look for the id
i++
linkB := i
for i < len(data) && data[i] != ']' {
i++
}
if i >= len(data) {
return 0, nil
}
linkE := i
// find the reference
if linkB == linkE {
if textHasNl {
var b bytes.Buffer
for j := 1; j < txtE; j++ {
switch {
case data[j] != '\n':
b.WriteByte(data[j])
case data[j-1] != ' ':
b.WriteByte(' ')
}
}
id = b.Bytes()
} else {
id = data[1:txtE]
altContentConsidered = true
}
} else {
id = data[linkB:linkE]
}
// find the reference with matching id
lr, ok := p.getRef(string(id))
if !ok {
return 0, nil
}
// keep link and title from reference
link = lr.link
title = lr.title
if altContentConsidered {
altContent = lr.text
}
i++
// shortcut reference style link or reference or inline footnote
default:
var id []byte
// craft the id
if textHasNl {
var b bytes.Buffer
for j := 1; j < txtE; j++ {
switch {
case data[j] != '\n':
b.WriteByte(data[j])
case data[j-1] != ' ':
b.WriteByte(' ')
}
}
id = b.Bytes()
} else {
if t == linkDeferredFootnote {
id = data[2:txtE] // get rid of the ^
} else {
id = data[1:txtE]
}
}
footnoteNode = NewNode(Item)
if t == linkInlineFootnote {
// create a new reference
noteID = len(p.notes) + 1
var fragment []byte
if len(id) > 0 {
if len(id) < 16 {
fragment = make([]byte, len(id))
} else {
fragment = make([]byte, 16)
}
copy(fragment, slugify(id))
} else {
fragment = append([]byte("footnote-"), []byte(strconv.Itoa(noteID))...)
}
ref := &reference{
noteID: noteID,
hasBlock: false,
link: fragment,
title: id,
footnote: footnoteNode,
}
p.notes = append(p.notes, ref)
link = ref.link
title = ref.title
} else {
// find the reference with matching id
lr, ok := p.getRef(string(id))
if !ok {
return 0, nil
}
if t == linkDeferredFootnote {
lr.noteID = len(p.notes) + 1
lr.footnote = footnoteNode
p.notes = append(p.notes, lr)
}
// keep link and title from reference
link = lr.link
// if inline footnote, title == footnote contents
title = lr.title
noteID = lr.noteID
}
// rewind the whitespace
i = txtE + 1
}
var uLink []byte
if t == linkNormal || t == linkImg {
if len(link) > 0 {
var uLinkBuf bytes.Buffer
unescapeText(&uLinkBuf, link)
uLink = uLinkBuf.Bytes()
}
// links need something to click on and somewhere to go
if len(uLink) == 0 || (t == linkNormal && txtE <= 1) {
return 0, nil
}
}
// call the relevant rendering function
var linkNode *Node
switch t {
case linkNormal:
linkNode = NewNode(Link)
linkNode.Destination = normalizeURI(uLink)
linkNode.Title = title
if len(altContent) > 0 {
linkNode.AppendChild(text(altContent))
} else {
// links cannot contain other links, so turn off link parsing
// temporarily and recurse
insideLink := p.insideLink
p.insideLink = true
p.inline(linkNode, data[1:txtE])
p.insideLink = insideLink
}
case linkImg:
linkNode = NewNode(Image)
linkNode.Destination = uLink
linkNode.Title = title
linkNode.AppendChild(text(data[1:txtE]))
i++
case linkInlineFootnote, linkDeferredFootnote:
linkNode = NewNode(Link)
linkNode.Destination = link
linkNode.Title = title
linkNode.NoteID = noteID
linkNode.Footnote = footnoteNode
if t == linkInlineFootnote {
i++
}
default:
return 0, nil
}
return i, linkNode
}
func (p *parser) inlineHTMLComment(data []byte) int {
if len(data) < 5 {
return 0
}
if data[0] != '<' || data[1] != '!' || data[2] != '-' || data[3] != '-' {
return 0
}
i := 5
// scan for an end-of-comment marker, across lines if necessary
for i < len(data) && !(data[i-2] == '-' && data[i-1] == '-' && data[i] == '>') {
i++
}
// no end-of-comment marker
if i >= len(data) {
return 0
}
return i + 1
}
func stripMailto(link []byte) []byte {
if bytes.HasPrefix(link, []byte("mailto://")) {
return link[9:]
} else if bytes.HasPrefix(link, []byte("mailto:")) {
return link[7:]
} else {
return link
}
}
// autolinkType specifies a kind of autolink that gets detected.
type autolinkType int
// These are the possible flag values for the autolink renderer.
const (
notAutolink autolinkType = iota
normalAutolink
emailAutolink
)
// '<' when tags or autolinks are allowed
func leftAngle(p *parser, data []byte, offset int) (int, *Node) {
data = data[offset:]
altype, end := tagLength(data)
if size := p.inlineHTMLComment(data); size > 0 {
end = size
}
if end > 2 {
if altype != notAutolink {
var uLink bytes.Buffer
unescapeText(&uLink, data[1:end+1-2])
if uLink.Len() > 0 {
link := uLink.Bytes()
node := NewNode(Link)
node.Destination = link
if altype == emailAutolink {
node.Destination = append([]byte("mailto:"), link...)
}
node.AppendChild(text(stripMailto(link)))
return end, node
}
} else {
htmlTag := NewNode(HTMLSpan)
htmlTag.Literal = data[:end]
return end, htmlTag
}
}
return end, nil
}
// '{' customized tags
func leftBrace(p *parser, data []byte, offset int) (int, *Node) {
i, cNode := parseCustomizedTag(p, data, offset)
stack := newCTagStack()
stack.push(p.cTag)
return i, parseCNode(p, cNode, stack)
}
// Parse cNode to Node.
// The type of cNode must be SINGLE or BEGIN which has children.
func parseCNode(p *parser, cNode *cNode, stack *cTagStack) *Node {
switch cNode.this.kind {
case SINGLE:
tag := stack.get(cNode.this.name)
if tag != nil {
n := NewNode(CSingle)
if tag.Async {
p.wg.Add(1)
go func() {
n.cTag = tag.Parse(cNode.this.attr, cNode.this.args)
p.wg.Done()
}()
} else {
n.cTag = tag.Parse(cNode.this.attr, cNode.this.args)
}
return n
}
case BEGIN:
tag := stack.get(cNode.this.name)
if tag != nil {
n := NewNode(CParent)
if tag.Async {
p.wg.Add(1)
go func() {
parseCBlock(p, cNode, stack, tag, n)
p.wg.Done()
}()
} else {
parseCBlock(p, cNode, stack, tag, n)
}
return n
}
}
node := NewNode(HTMLSpan)
node.Literal = []byte{}
return node
}
// Parse children of cNode.
// cNode must have children.
func parseCBlock(p *parser, cNode *cNode, stack *cTagStack, tag *CustomizedTag, n *Node) {
ct := tag.Parse(cNode.this.attr, cNode.this.args)
n.cTag = ct
stack.push(ct.Child)
nodes := cNode.children
for i := 0; i < len(nodes); i++ {
switch nodes[i].this.kind {
case TEXT:
p.inline(n, nodes[i].this.content)
case SINGLE:
n.AppendChild(parseCNode(p, nodes[i], stack))
case BEGIN:
n.AppendChild(parseCNode(p, nodes[i], stack))
}
}
stack.pop()
}
type cTagStack struct {
s []map[string]CustomizedTag
}
func newCTagStack() *cTagStack {
return &cTagStack{[]map[string]CustomizedTag{}}
}
func (s *cTagStack) push(e map[string]CustomizedTag) {
s.s = append(s.s, e)
}
func (s *cTagStack) pop() (map[string]CustomizedTag, error) {
l := len(s.s)
if l == 0 {
return nil, errors.New("Empty Stack")
}
res := s.s[l-1]
s.s = s.s[:l-1]
return res, nil
}
func (s *cTagStack) get(name string) *CustomizedTag {
for i := len(s.s) - 1; i >= 0; i-- {
c, ok := s.s[i][name]
if ok {
return &c
}
}
return nil
}
// Parse a customized tag to tree.
// e.g. {big color=red}THIS IS **RED**{icon A/} TEXT!{/big}
// => BEGIN (big color=red)
// L TEXT (THIS IS **RED**)
// L SINGLE (icon A)
// L TEXT ( TEXT!!)
func parseCustomizedTag(p *parser, data []byte, offset int) (int, *cNode) {
data = data[offset:]
i := 0
tag := findCTag(p, data, i)
i = tag.end
if tag.kind == BEGIN {
end, child := parseCTBlock(p, data, i)
return end, &cNode{this: tag, children: child}
}
return i, &cNode{this: tag}
}
// Parse customized tags until CLOSE
func parseCTBlock(p *parser, data []byte, i int) (int, []*cNode) {
nodes := make([]*cNode, 0, 4)
for i < len(data) {
tag := findCTag(p, data, i)
i = tag.end
var n *cNode = &cNode{this: tag}
if tag.kind == BEGIN {
i, n.children = parseCTBlock(p, data, i)
}
if tag.kind == CLOSE {
break
}
nodes = append(nodes, n)
}
return i, nodes
}
// Parse a single customized tag.
func findCTag(p *parser, data []byte, i int) *parsedCTag {
if i+1 >= len(data) {
return &parsedCTag{end: len(data)}
}
if data[i] == '{' && data[i+1] == '/' {
i += 2
for i < len(data) && data[i] != '}' {
i++
}
i++ // points after }
return &parsedCTag{end: i, kind: CLOSE}
}
if data[i] == '{' {
i++
bgn := i
for i < len(data) && isalnum(data[i]) {
i++
}
name := string(data[bgn:i])
attributes := map[string]string{}
arguments := make([]string, 0, 4)
hasChild := false
for i < len(data) {
if isspace(data[i]) {
i++
continue
}
if data[i] == '}' {
i += 1
hasChild = true
break
}
if data[i] == '/' && i+1 < len(data) && data[i+1] == '}' {
i += 2
break
}
var key, value string
key, value, i = tagAttribute(data, i)
if value == "" {
arguments = append(arguments, key)
} else {
attributes[key] = value
}
}
kind := SINGLE
if hasChild {
kind = BEGIN
}
return &parsedCTag{
end: i,
name: name,
attr: attributes,
args: arguments,
kind: kind,
}
}
bgn := i
for i < len(data) && data[i-1] != '\\' && data[i] != '{' {
i++
}
return &parsedCTag{end: i, kind: TEXT, content: data[bgn:i]}
}
// Parse an attribute.
// Return key, value, end of data
func tagAttribute(data []byte, offset int) (string, string, int) {
i := offset
var key string
if data[i] == '"' {
i++
for i < len(data) && data[i] != '"' {
i++
}
key = string(data[offset+1 : i])
i++
} else if !isalnum(data[i]) {
return "", "", i + 1
} else {
for i < len(data) && isalnum(data[i]) {
i++
}
key = string(data[offset:i])
}
bgn := i
if bgn+1 >= len(data) {
return key, "", i
}
if data[bgn] == '=' && data[bgn+1] == '"' {
i += 2
for i < len(data) && data[i] != '"' {
i++
}
return key, string(data[bgn+2 : i]), i + 1
} else if data[bgn] == '=' {
i += 1
for i < len(data) && isalnum(data[i]) {
i++
}
return key, string(data[bgn+1 : i]), i
}
return key, "", i
}
// '\\' backslash escape
var escapeChars = []byte("\\`*_{}[]()#+-.!:|&<>~")
func escape(p *parser, data []byte, offset int) (int, *Node) {
data = data[offset:]
if len(data) > 1 {
if p.flags&BackslashLineBreak != 0 && data[1] == '\n' {
return 2, NewNode(Hardbreak)
}
if bytes.IndexByte(escapeChars, data[1]) < 0 {
return 0, nil
}
return 2, text(data[1:2])
}
return 2, nil
}
func unescapeText(ob *bytes.Buffer, src []byte) {
i := 0
for i < len(src) {
org := i
for i < len(src) && src[i] != '\\' {
i++
}
if i > org {
ob.Write(src[org:i])
}
if i+1 >= len(src) {
break
}
ob.WriteByte(src[i+1])
i += 2
}
}
// '&' escaped when it doesn't belong to an entity
// valid entities are assumed to be anything matching &#?[A-Za-z0-9]+;
func entity(p *parser, data []byte, offset int) (int, *Node) {
data = data[offset:]
end := 1
if end < len(data) && data[end] == '#' {
end++
}
for end < len(data) && isalnum(data[end]) {
end++
}
if end < len(data) && data[end] == ';' {
end++ // real entity
} else {
return 0, nil // lone '&'
}
ent := data[:end]
// undo & escaping or it will be converted to &amp; by another
// escaper in the renderer
if bytes.Equal(ent, []byte("&")) {