inline.go (view raw)
1//
2// Blackfriday Markdown Processor
3// Available at http://github.com/russross/blackfriday
4//
5// Copyright © 2011 Russ Ross <russ@russross.com>.
6// Distributed under the Simplified BSD License.
7// See README.md for details.
8//
9
10//
11// Functions to parse inline elements.
12//
13
14package blackfriday
15
16import (
17 "bytes"
18 "regexp"
19 "strconv"
20)
21
22var (
23 urlRe = `((https?|ftp):\/\/|\/)[-A-Za-z0-9+&@#\/%?=~_|!:,.;\(\)]+`
24 anchorRe = regexp.MustCompile(`^(<a\shref="` + urlRe + `"(\stitle="[^"<>]+")?\s?>` + urlRe + `<\/a>)`)
25
26 // TODO: improve this regexp to catch all possible entities:
27 htmlEntityRe = regexp.MustCompile(`&[a-z]{2,5};`)
28)
29
30// Functions to parse text within a block
31// Each function returns the number of chars taken care of
32// data is the complete block being rendered
33// offset is the number of valid chars before the current cursor
34
35func (p *parser) inline(currBlock *Node, data []byte) {
36 // handlers might call us recursively: enforce a maximum depth
37 if p.nesting >= p.maxNesting || len(data) == 0 {
38 return
39 }
40 p.nesting++
41 beg, end := 0, 0
42 for end < len(data) {
43 handler := p.inlineCallback[data[end]]
44 if handler != nil {
45 if consumed, node := handler(p, data, end); consumed == 0 {
46 // No action from the callback.
47 end++
48 } else {
49 // Copy inactive chars into the output.
50 currBlock.AppendChild(text(data[beg:end]))
51 if node != nil {
52 currBlock.AppendChild(node)
53 }
54 // Skip past whatever the callback used.
55 beg = end + consumed
56 end = beg
57 }
58 } else {
59 end++
60 }
61 }
62 if beg < len(data) {
63 if data[end-1] == '\n' {
64 end--
65 }
66 currBlock.AppendChild(text(data[beg:end]))
67 }
68 p.nesting--
69}
70
71// single and double emphasis parsing
72func emphasis(p *parser, data []byte, offset int) (int, *Node) {
73 data = data[offset:]
74 c := data[0]
75
76 if len(data) > 2 && data[1] != c {
77 // whitespace cannot follow an opening emphasis;
78 // strikethrough only takes two characters '~~'
79 if c == '~' || isspace(data[1]) {
80 return 0, nil
81 }
82 ret, node := helperEmphasis(p, data[1:], c)
83 if ret == 0 {
84 return 0, nil
85 }
86
87 return ret + 1, node
88 }
89
90 if len(data) > 3 && data[1] == c && data[2] != c {
91 if isspace(data[2]) {
92 return 0, nil
93 }
94 ret, node := helperDoubleEmphasis(p, data[2:], c)
95 if ret == 0 {
96 return 0, nil
97 }
98
99 return ret + 2, node
100 }
101
102 if len(data) > 4 && data[1] == c && data[2] == c && data[3] != c {
103 if c == '~' || isspace(data[3]) {
104 return 0, nil
105 }
106 ret, node := helperTripleEmphasis(p, data, 3, c)
107 if ret == 0 {
108 return 0, nil
109 }
110
111 return ret + 3, node
112 }
113
114 return 0, nil
115}
116
117func codeSpan(p *parser, data []byte, offset int) (int, *Node) {
118 data = data[offset:]
119
120 nb := 0
121
122 // count the number of backticks in the delimiter
123 for nb < len(data) && data[nb] == '`' {
124 nb++
125 }
126
127 // find the next delimiter
128 i, end := 0, 0
129 for end = nb; end < len(data) && i < nb; end++ {
130 if data[end] == '`' {
131 i++
132 } else {
133 i = 0
134 }
135 }
136
137 // no matching delimiter?
138 if i < nb && end >= len(data) {
139 return 0, nil
140 }
141
142 // trim outside whitespace
143 fBegin := nb
144 for fBegin < end && data[fBegin] == ' ' {
145 fBegin++
146 }
147
148 fEnd := end - nb
149 for fEnd > fBegin && data[fEnd-1] == ' ' {
150 fEnd--
151 }
152
153 // render the code span
154 if fBegin != fEnd {
155 code := NewNode(Code)
156 code.Literal = data[fBegin:fEnd]
157 return end, code
158 }
159
160 return end, nil
161}
162
163// newline preceded by two spaces becomes <br>
164func maybeLineBreak(p *parser, data []byte, offset int) (int, *Node) {
165 origOffset := offset
166 for offset < len(data) && data[offset] == ' ' {
167 offset++
168 }
169
170 if offset < len(data) && data[offset] == '\n' {
171 if offset-origOffset >= 2 {
172 return offset - origOffset + 1, NewNode(Hardbreak)
173 }
174 return offset - origOffset, nil
175 }
176 return 0, nil
177}
178
179// newline without two spaces works when HardLineBreak is enabled
180func lineBreak(p *parser, data []byte, offset int) (int, *Node) {
181 if p.flags&HardLineBreak != 0 {
182 return 1, NewNode(Hardbreak)
183 }
184 return 0, nil
185}
186
187type linkType int
188
189const (
190 linkNormal linkType = iota
191 linkImg
192 linkDeferredFootnote
193 linkInlineFootnote
194)
195
196func isReferenceStyleLink(data []byte, pos int, t linkType) bool {
197 if t == linkDeferredFootnote {
198 return false
199 }
200 return pos < len(data)-1 && data[pos] == '[' && data[pos+1] != '^'
201}
202
203func maybeImage(p *parser, data []byte, offset int) (int, *Node) {
204 if offset < len(data)-1 && data[offset+1] == '[' {
205 return link(p, data, offset)
206 }
207 return 0, nil
208}
209
210func maybeInlineFootnote(p *parser, data []byte, offset int) (int, *Node) {
211 if offset < len(data)-1 && data[offset+1] == '[' {
212 return link(p, data, offset)
213 }
214 return 0, nil
215}
216
217// '[': parse a link or an image or a footnote
218func link(p *parser, data []byte, offset int) (int, *Node) {
219 // no links allowed inside regular links, footnote, and deferred footnotes
220 if p.insideLink && (offset > 0 && data[offset-1] == '[' || len(data)-1 > offset && data[offset+1] == '^') {
221 return 0, nil
222 }
223
224 var t linkType
225 switch {
226 // special case: ![^text] == deferred footnote (that follows something with
227 // an exclamation point)
228 case p.flags&Footnotes != 0 && len(data)-1 > offset && data[offset+1] == '^':
229 t = linkDeferredFootnote
230 // ![alt] == image
231 case offset >= 0 && data[offset] == '!':
232 t = linkImg
233 offset++
234 // ^[text] == inline footnote
235 // [^refId] == deferred footnote
236 case p.flags&Footnotes != 0:
237 if offset >= 0 && data[offset] == '^' {
238 t = linkInlineFootnote
239 offset++
240 } else if len(data)-1 > offset && data[offset+1] == '^' {
241 t = linkDeferredFootnote
242 }
243 // [text] == regular link
244 default:
245 t = linkNormal
246 }
247
248 data = data[offset:]
249
250 var (
251 i = 1
252 noteID int
253 title, link, altContent []byte
254 textHasNl = false
255 )
256
257 if t == linkDeferredFootnote {
258 i++
259 }
260
261 // look for the matching closing bracket
262 for level := 1; level > 0 && i < len(data); i++ {
263 switch {
264 case data[i] == '\n':
265 textHasNl = true
266
267 case data[i-1] == '\\':
268 continue
269
270 case data[i] == '[':
271 level++
272
273 case data[i] == ']':
274 level--
275 if level <= 0 {
276 i-- // compensate for extra i++ in for loop
277 }
278 }
279 }
280
281 if i >= len(data) {
282 return 0, nil
283 }
284
285 txtE := i
286 i++
287
288 // skip any amount of whitespace or newline
289 // (this is much more lax than original markdown syntax)
290 for i < len(data) && isspace(data[i]) {
291 i++
292 }
293
294 // inline style link
295 switch {
296 case i < len(data) && data[i] == '(':
297 // skip initial whitespace
298 i++
299
300 for i < len(data) && isspace(data[i]) {
301 i++
302 }
303
304 linkB := i
305
306 // look for link end: ' " )
307 findlinkend:
308 for i < len(data) {
309 switch {
310 case data[i] == '\\':
311 i += 2
312
313 case data[i] == ')' || data[i] == '\'' || data[i] == '"':
314 break findlinkend
315
316 default:
317 i++
318 }
319 }
320
321 if i >= len(data) {
322 return 0, nil
323 }
324 linkE := i
325
326 // look for title end if present
327 titleB, titleE := 0, 0
328 if data[i] == '\'' || data[i] == '"' {
329 i++
330 titleB = i
331
332 findtitleend:
333 for i < len(data) {
334 switch {
335 case data[i] == '\\':
336 i += 2
337
338 case data[i] == ')':
339 break findtitleend
340
341 default:
342 i++
343 }
344 }
345
346 if i >= len(data) {
347 return 0, nil
348 }
349
350 // skip whitespace after title
351 titleE = i - 1
352 for titleE > titleB && isspace(data[titleE]) {
353 titleE--
354 }
355
356 // check for closing quote presence
357 if data[titleE] != '\'' && data[titleE] != '"' {
358 titleB, titleE = 0, 0
359 linkE = i
360 }
361 }
362
363 // remove whitespace at the end of the link
364 for linkE > linkB && isspace(data[linkE-1]) {
365 linkE--
366 }
367
368 // remove optional angle brackets around the link
369 if data[linkB] == '<' {
370 linkB++
371 }
372 if data[linkE-1] == '>' {
373 linkE--
374 }
375
376 // build escaped link and title
377 if linkE > linkB {
378 link = data[linkB:linkE]
379 }
380
381 if titleE > titleB {
382 title = data[titleB:titleE]
383 }
384
385 i++
386
387 // reference style link
388 case isReferenceStyleLink(data, i, t):
389 var id []byte
390 altContentConsidered := false
391
392 // look for the id
393 i++
394 linkB := i
395 for i < len(data) && data[i] != ']' {
396 i++
397 }
398 if i >= len(data) {
399 return 0, nil
400 }
401 linkE := i
402
403 // find the reference
404 if linkB == linkE {
405 if textHasNl {
406 var b bytes.Buffer
407
408 for j := 1; j < txtE; j++ {
409 switch {
410 case data[j] != '\n':
411 b.WriteByte(data[j])
412 case data[j-1] != ' ':
413 b.WriteByte(' ')
414 }
415 }
416
417 id = b.Bytes()
418 } else {
419 id = data[1:txtE]
420 altContentConsidered = true
421 }
422 } else {
423 id = data[linkB:linkE]
424 }
425
426 // find the reference with matching id
427 lr, ok := p.getRef(string(id))
428 if !ok {
429 return 0, nil
430 }
431
432 // keep link and title from reference
433 link = lr.link
434 title = lr.title
435 if altContentConsidered {
436 altContent = lr.text
437 }
438 i++
439
440 // shortcut reference style link or reference or inline footnote
441 default:
442 var id []byte
443
444 // craft the id
445 if textHasNl {
446 var b bytes.Buffer
447
448 for j := 1; j < txtE; j++ {
449 switch {
450 case data[j] != '\n':
451 b.WriteByte(data[j])
452 case data[j-1] != ' ':
453 b.WriteByte(' ')
454 }
455 }
456
457 id = b.Bytes()
458 } else {
459 if t == linkDeferredFootnote {
460 id = data[2:txtE] // get rid of the ^
461 } else {
462 id = data[1:txtE]
463 }
464 }
465
466 if t == linkInlineFootnote {
467 // create a new reference
468 noteID = len(p.notes) + 1
469
470 var fragment []byte
471 if len(id) > 0 {
472 if len(id) < 16 {
473 fragment = make([]byte, len(id))
474 } else {
475 fragment = make([]byte, 16)
476 }
477 copy(fragment, slugify(id))
478 } else {
479 fragment = append([]byte("footnote-"), []byte(strconv.Itoa(noteID))...)
480 }
481
482 ref := &reference{
483 noteID: noteID,
484 hasBlock: false,
485 link: fragment,
486 title: id,
487 }
488
489 p.notes = append(p.notes, ref)
490
491 link = ref.link
492 title = ref.title
493 } else {
494 // find the reference with matching id
495 lr, ok := p.getRef(string(id))
496 if !ok {
497 return 0, nil
498 }
499
500 if t == linkDeferredFootnote {
501 lr.noteID = len(p.notes) + 1
502 p.notes = append(p.notes, lr)
503 }
504
505 // keep link and title from reference
506 link = lr.link
507 // if inline footnote, title == footnote contents
508 title = lr.title
509 noteID = lr.noteID
510 }
511
512 // rewind the whitespace
513 i = txtE + 1
514 }
515
516 var uLink []byte
517 if t == linkNormal || t == linkImg {
518 if len(link) > 0 {
519 var uLinkBuf bytes.Buffer
520 unescapeText(&uLinkBuf, link)
521 uLink = uLinkBuf.Bytes()
522 }
523
524 // links need something to click on and somewhere to go
525 if len(uLink) == 0 || (t == linkNormal && txtE <= 1) {
526 return 0, nil
527 }
528 }
529
530 // call the relevant rendering function
531 var linkNode *Node
532 switch t {
533 case linkNormal:
534 linkNode = NewNode(Link)
535 linkNode.Destination = normalizeURI(uLink)
536 linkNode.Title = title
537 if len(altContent) > 0 {
538 linkNode.AppendChild(text(altContent))
539 } else {
540 // links cannot contain other links, so turn off link parsing
541 // temporarily and recurse
542 insideLink := p.insideLink
543 p.insideLink = true
544 p.inline(linkNode, data[1:txtE])
545 p.insideLink = insideLink
546 }
547
548 case linkImg:
549 linkNode = NewNode(Image)
550 linkNode.Destination = uLink
551 linkNode.Title = title
552 linkNode.AppendChild(text(data[1:txtE]))
553 i++
554
555 case linkInlineFootnote, linkDeferredFootnote:
556 linkNode = NewNode(Link)
557 linkNode.Destination = link
558 linkNode.Title = title
559 linkNode.NoteID = noteID
560 if t == linkInlineFootnote {
561 i++
562 }
563
564 default:
565 return 0, nil
566 }
567
568 return i, linkNode
569}
570
571func (p *parser) inlineHTMLComment(data []byte) int {
572 if len(data) < 5 {
573 return 0
574 }
575 if data[0] != '<' || data[1] != '!' || data[2] != '-' || data[3] != '-' {
576 return 0
577 }
578 i := 5
579 // scan for an end-of-comment marker, across lines if necessary
580 for i < len(data) && !(data[i-2] == '-' && data[i-1] == '-' && data[i] == '>') {
581 i++
582 }
583 // no end-of-comment marker
584 if i >= len(data) {
585 return 0
586 }
587 return i + 1
588}
589
590func stripMailto(link []byte) []byte {
591 if bytes.HasPrefix(link, []byte("mailto://")) {
592 return link[9:]
593 } else if bytes.HasPrefix(link, []byte("mailto:")) {
594 return link[7:]
595 } else {
596 return link
597 }
598}
599
600// autolinkType specifies a kind of autolink that gets detected.
601type autolinkType int
602
603// These are the possible flag values for the autolink renderer.
604const (
605 notAutolink autolinkType = iota
606 normalAutolink
607 emailAutolink
608)
609
610// '<' when tags or autolinks are allowed
611func leftAngle(p *parser, data []byte, offset int) (int, *Node) {
612 data = data[offset:]
613 altype, end := tagLength(data)
614 if size := p.inlineHTMLComment(data); size > 0 {
615 end = size
616 }
617 if end > 2 {
618 if altype != notAutolink {
619 var uLink bytes.Buffer
620 unescapeText(&uLink, data[1:end+1-2])
621 if uLink.Len() > 0 {
622 link := uLink.Bytes()
623 node := NewNode(Link)
624 node.Destination = link
625 if altype == emailAutolink {
626 node.Destination = append([]byte("mailto:"), link...)
627 }
628 node.AppendChild(text(stripMailto(link)))
629 return end, node
630 }
631 } else {
632 htmlTag := NewNode(HTMLSpan)
633 htmlTag.Literal = data[:end]
634 return end, htmlTag
635 }
636 }
637
638 return end, nil
639}
640
641// '\\' backslash escape
642var escapeChars = []byte("\\`*_{}[]()#+-.!:|&<>~")
643
644func escape(p *parser, data []byte, offset int) (int, *Node) {
645 data = data[offset:]
646
647 if len(data) > 1 {
648 if p.flags&BackslashLineBreak != 0 && data[1] == '\n' {
649 return 2, NewNode(Hardbreak)
650 }
651 if bytes.IndexByte(escapeChars, data[1]) < 0 {
652 return 0, nil
653 }
654
655 return 2, text(data[1:2])
656 }
657
658 return 2, nil
659}
660
661func unescapeText(ob *bytes.Buffer, src []byte) {
662 i := 0
663 for i < len(src) {
664 org := i
665 for i < len(src) && src[i] != '\\' {
666 i++
667 }
668
669 if i > org {
670 ob.Write(src[org:i])
671 }
672
673 if i+1 >= len(src) {
674 break
675 }
676
677 ob.WriteByte(src[i+1])
678 i += 2
679 }
680}
681
682// '&' escaped when it doesn't belong to an entity
683// valid entities are assumed to be anything matching &#?[A-Za-z0-9]+;
684func entity(p *parser, data []byte, offset int) (int, *Node) {
685 data = data[offset:]
686
687 end := 1
688
689 if end < len(data) && data[end] == '#' {
690 end++
691 }
692
693 for end < len(data) && isalnum(data[end]) {
694 end++
695 }
696
697 if end < len(data) && data[end] == ';' {
698 end++ // real entity
699 } else {
700 return 0, nil // lone '&'
701 }
702
703 ent := data[:end]
704 // undo & escaping or it will be converted to &amp; by another
705 // escaper in the renderer
706 if bytes.Equal(ent, []byte("&")) {
707 ent = []byte{'&'}
708 }
709
710 return end, text(ent)
711}
712
713func linkEndsWithEntity(data []byte, linkEnd int) bool {
714 entityRanges := htmlEntityRe.FindAllIndex(data[:linkEnd], -1)
715 return entityRanges != nil && entityRanges[len(entityRanges)-1][1] == linkEnd
716}
717
718var prefixes = [][]byte{
719 []byte("http://"),
720 []byte("https://"),
721 []byte("ftp://"),
722 []byte("file://"),
723 []byte("mailto:"),
724}
725
726func hasPrefixCaseInsensitive(s, prefix []byte) bool {
727 if len(s) < len(prefix) {
728 return false
729 }
730 delta := byte('a' - 'A')
731 for i, b := range prefix {
732 if b != s[i] && b != s[i]+delta {
733 return false
734 }
735 }
736 return true
737}
738
739func maybeAutoLink(p *parser, data []byte, offset int) (int, *Node) {
740 // quick check to rule out most false hits
741 if p.insideLink || len(data) < offset+6 { // 6 is the len() of the shortest of the prefixes
742 return 0, nil
743 }
744 for _, prefix := range prefixes {
745 endOfHead := offset + 8 // 8 is the len() of the longest prefix
746 if endOfHead > len(data) {
747 endOfHead = len(data)
748 }
749 if hasPrefixCaseInsensitive(data[offset:endOfHead], prefix) {
750 return autoLink(p, data, offset)
751 }
752 }
753 return 0, nil
754}
755
756func autoLink(p *parser, data []byte, offset int) (int, *Node) {
757 // Now a more expensive check to see if we're not inside an anchor element
758 anchorStart := offset
759 offsetFromAnchor := 0
760 for anchorStart > 0 && data[anchorStart] != '<' {
761 anchorStart--
762 offsetFromAnchor++
763 }
764
765 anchorStr := anchorRe.Find(data[anchorStart:])
766 if anchorStr != nil {
767 anchorClose := NewNode(HTMLSpan)
768 anchorClose.Literal = anchorStr[offsetFromAnchor:]
769 return len(anchorStr) - offsetFromAnchor, anchorClose
770 }
771
772 // scan backward for a word boundary
773 rewind := 0
774 for offset-rewind > 0 && rewind <= 7 && isletter(data[offset-rewind-1]) {
775 rewind++
776 }
777 if rewind > 6 { // longest supported protocol is "mailto" which has 6 letters
778 return 0, nil
779 }
780
781 origData := data
782 data = data[offset-rewind:]
783
784 if !isSafeLink(data) {
785 return 0, nil
786 }
787
788 linkEnd := 0
789 for linkEnd < len(data) && !isEndOfLink(data[linkEnd]) {
790 linkEnd++
791 }
792
793 // Skip punctuation at the end of the link
794 if (data[linkEnd-1] == '.' || data[linkEnd-1] == ',') && data[linkEnd-2] != '\\' {
795 linkEnd--
796 }
797
798 // But don't skip semicolon if it's a part of escaped entity:
799 if data[linkEnd-1] == ';' && data[linkEnd-2] != '\\' && !linkEndsWithEntity(data, linkEnd) {
800 linkEnd--
801 }
802
803 // See if the link finishes with a punctuation sign that can be closed.
804 var copen byte
805 switch data[linkEnd-1] {
806 case '"':
807 copen = '"'
808 case '\'':
809 copen = '\''
810 case ')':
811 copen = '('
812 case ']':
813 copen = '['
814 case '}':
815 copen = '{'
816 default:
817 copen = 0
818 }
819
820 if copen != 0 {
821 bufEnd := offset - rewind + linkEnd - 2
822
823 openDelim := 1
824
825 /* Try to close the final punctuation sign in this same line;
826 * if we managed to close it outside of the URL, that means that it's
827 * not part of the URL. If it closes inside the URL, that means it
828 * is part of the URL.
829 *
830 * Examples:
831 *
832 * foo http://www.pokemon.com/Pikachu_(Electric) bar
833 * => http://www.pokemon.com/Pikachu_(Electric)
834 *
835 * foo (http://www.pokemon.com/Pikachu_(Electric)) bar
836 * => http://www.pokemon.com/Pikachu_(Electric)
837 *
838 * foo http://www.pokemon.com/Pikachu_(Electric)) bar
839 * => http://www.pokemon.com/Pikachu_(Electric))
840 *
841 * (foo http://www.pokemon.com/Pikachu_(Electric)) bar
842 * => foo http://www.pokemon.com/Pikachu_(Electric)
843 */
844
845 for bufEnd >= 0 && origData[bufEnd] != '\n' && openDelim != 0 {
846 if origData[bufEnd] == data[linkEnd-1] {
847 openDelim++
848 }
849
850 if origData[bufEnd] == copen {
851 openDelim--
852 }
853
854 bufEnd--
855 }
856
857 if openDelim == 0 {
858 linkEnd--
859 }
860 }
861
862 var uLink bytes.Buffer
863 unescapeText(&uLink, data[:linkEnd])
864
865 if uLink.Len() > 0 {
866 node := NewNode(Link)
867 node.Destination = uLink.Bytes()
868 node.AppendChild(text(uLink.Bytes()))
869 return linkEnd, node
870 }
871
872 return linkEnd, nil
873}
874
875func isEndOfLink(char byte) bool {
876 return isspace(char) || char == '<'
877}
878
879var validUris = [][]byte{[]byte("http://"), []byte("https://"), []byte("ftp://"), []byte("mailto://")}
880var validPaths = [][]byte{[]byte("/"), []byte("./"), []byte("../")}
881
882func isSafeLink(link []byte) bool {
883 for _, path := range validPaths {
884 if len(link) >= len(path) && bytes.Equal(link[:len(path)], path) {
885 if len(link) == len(path) {
886 return true
887 } else if isalnum(link[len(path)]) {
888 return true
889 }
890 }
891 }
892
893 for _, prefix := range validUris {
894 // TODO: handle unicode here
895 // case-insensitive prefix test
896 if len(link) > len(prefix) && bytes.Equal(bytes.ToLower(link[:len(prefix)]), prefix) && isalnum(link[len(prefix)]) {
897 return true
898 }
899 }
900
901 return false
902}
903
904// return the length of the given tag, or 0 is it's not valid
905func tagLength(data []byte) (autolink autolinkType, end int) {
906 var i, j int
907
908 // a valid tag can't be shorter than 3 chars
909 if len(data) < 3 {
910 return notAutolink, 0
911 }
912
913 // begins with a '<' optionally followed by '/', followed by letter or number
914 if data[0] != '<' {
915 return notAutolink, 0
916 }
917 if data[1] == '/' {
918 i = 2
919 } else {
920 i = 1
921 }
922
923 if !isalnum(data[i]) {
924 return notAutolink, 0
925 }
926
927 // scheme test
928 autolink = notAutolink
929
930 // try to find the beginning of an URI
931 for i < len(data) && (isalnum(data[i]) || data[i] == '.' || data[i] == '+' || data[i] == '-') {
932 i++
933 }
934
935 if i > 1 && i < len(data) && data[i] == '@' {
936 if j = isMailtoAutoLink(data[i:]); j != 0 {
937 return emailAutolink, i + j
938 }
939 }
940
941 if i > 2 && i < len(data) && data[i] == ':' {
942 autolink = normalAutolink
943 i++
944 }
945
946 // complete autolink test: no whitespace or ' or "
947 switch {
948 case i >= len(data):
949 autolink = notAutolink
950 case autolink != notAutolink:
951 j = i
952
953 for i < len(data) {
954 if data[i] == '\\' {
955 i += 2
956 } else if data[i] == '>' || data[i] == '\'' || data[i] == '"' || isspace(data[i]) {
957 break
958 } else {
959 i++
960 }
961
962 }
963
964 if i >= len(data) {
965 return autolink, 0
966 }
967 if i > j && data[i] == '>' {
968 return autolink, i + 1
969 }
970
971 // one of the forbidden chars has been found
972 autolink = notAutolink
973 }
974 i += bytes.IndexByte(data[i:], '>')
975 if i < 0 {
976 return autolink, 0
977 }
978 return autolink, i + 1
979}
980
981// look for the address part of a mail autolink and '>'
982// this is less strict than the original markdown e-mail address matching
983func isMailtoAutoLink(data []byte) int {
984 nb := 0
985
986 // address is assumed to be: [-@._a-zA-Z0-9]+ with exactly one '@'
987 for i := 0; i < len(data); i++ {
988 if isalnum(data[i]) {
989 continue
990 }
991
992 switch data[i] {
993 case '@':
994 nb++
995
996 case '-', '.', '_':
997 break
998
999 case '>':
1000 if nb == 1 {
1001 return i + 1
1002 }
1003 return 0
1004 default:
1005 return 0
1006 }
1007 }
1008
1009 return 0
1010}
1011
1012// look for the next emph char, skipping other constructs
1013func helperFindEmphChar(data []byte, c byte) int {
1014 i := 0
1015
1016 for i < len(data) {
1017 for i < len(data) && data[i] != c && data[i] != '`' && data[i] != '[' {
1018 i++
1019 }
1020 if i >= len(data) {
1021 return 0
1022 }
1023 // do not count escaped chars
1024 if i != 0 && data[i-1] == '\\' {
1025 i++
1026 continue
1027 }
1028 if data[i] == c {
1029 return i
1030 }
1031
1032 if data[i] == '`' {
1033 // skip a code span
1034 tmpI := 0
1035 i++
1036 for i < len(data) && data[i] != '`' {
1037 if tmpI == 0 && data[i] == c {
1038 tmpI = i
1039 }
1040 i++
1041 }
1042 if i >= len(data) {
1043 return tmpI
1044 }
1045 i++
1046 } else if data[i] == '[' {
1047 // skip a link
1048 tmpI := 0
1049 i++
1050 for i < len(data) && data[i] != ']' {
1051 if tmpI == 0 && data[i] == c {
1052 tmpI = i
1053 }
1054 i++
1055 }
1056 i++
1057 for i < len(data) && (data[i] == ' ' || data[i] == '\n') {
1058 i++
1059 }
1060 if i >= len(data) {
1061 return tmpI
1062 }
1063 if data[i] != '[' && data[i] != '(' { // not a link
1064 if tmpI > 0 {
1065 return tmpI
1066 }
1067 continue
1068 }
1069 cc := data[i]
1070 i++
1071 for i < len(data) && data[i] != cc {
1072 if tmpI == 0 && data[i] == c {
1073 return i
1074 }
1075 i++
1076 }
1077 if i >= len(data) {
1078 return tmpI
1079 }
1080 i++
1081 }
1082 }
1083 return 0
1084}
1085
1086func helperEmphasis(p *parser, data []byte, c byte) (int, *Node) {
1087 i := 0
1088
1089 // skip one symbol if coming from emph3
1090 if len(data) > 1 && data[0] == c && data[1] == c {
1091 i = 1
1092 }
1093
1094 for i < len(data) {
1095 length := helperFindEmphChar(data[i:], c)
1096 if length == 0 {
1097 return 0, nil
1098 }
1099 i += length
1100 if i >= len(data) {
1101 return 0, nil
1102 }
1103
1104 if i+1 < len(data) && data[i+1] == c {
1105 i++
1106 continue
1107 }
1108
1109 if data[i] == c && !isspace(data[i-1]) {
1110
1111 if p.flags&NoIntraEmphasis != 0 {
1112 if !(i+1 == len(data) || isspace(data[i+1]) || ispunct(data[i+1])) {
1113 continue
1114 }
1115 }
1116
1117 emph := NewNode(Emph)
1118 p.inline(emph, data[:i])
1119 return i + 1, emph
1120 }
1121 }
1122
1123 return 0, nil
1124}
1125
1126func helperDoubleEmphasis(p *parser, data []byte, c byte) (int, *Node) {
1127 i := 0
1128
1129 for i < len(data) {
1130 length := helperFindEmphChar(data[i:], c)
1131 if length == 0 {
1132 return 0, nil
1133 }
1134 i += length
1135
1136 if i+1 < len(data) && data[i] == c && data[i+1] == c && i > 0 && !isspace(data[i-1]) {
1137 nodeType := Strong
1138 if c == '~' {
1139 nodeType = Del
1140 }
1141 node := NewNode(nodeType)
1142 p.inline(node, data[:i])
1143 return i + 2, node
1144 }
1145 i++
1146 }
1147 return 0, nil
1148}
1149
1150func helperTripleEmphasis(p *parser, data []byte, offset int, c byte) (int, *Node) {
1151 i := 0
1152 origData := data
1153 data = data[offset:]
1154
1155 for i < len(data) {
1156 length := helperFindEmphChar(data[i:], c)
1157 if length == 0 {
1158 return 0, nil
1159 }
1160 i += length
1161
1162 // skip whitespace preceded symbols
1163 if data[i] != c || isspace(data[i-1]) {
1164 continue
1165 }
1166
1167 switch {
1168 case i+2 < len(data) && data[i+1] == c && data[i+2] == c:
1169 // triple symbol found
1170 strong := NewNode(Strong)
1171 em := NewNode(Emph)
1172 strong.AppendChild(em)
1173 p.inline(em, data[:i])
1174 return i + 3, strong
1175 case (i+1 < len(data) && data[i+1] == c):
1176 // double symbol found, hand over to emph1
1177 length, node := helperEmphasis(p, origData[offset-2:], c)
1178 if length == 0 {
1179 return 0, nil
1180 }
1181 return length - 2, node
1182 default:
1183 // single symbol found, hand over to emph2
1184 length, node := helperDoubleEmphasis(p, origData[offset-1:], c)
1185 if length == 0 {
1186 return 0, nil
1187 }
1188 return length - 1, node
1189 }
1190 }
1191 return 0, nil
1192}
1193
1194func text(s []byte) *Node {
1195 node := NewNode(Text)
1196 node.Literal = s
1197 return node
1198}
1199
1200func normalizeURI(s []byte) []byte {
1201 return s // TODO: implement
1202}