block.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 block-level elements.
12//
13
14package blackfriday
15
16import (
17 "bytes"
18)
19
20// Parse block-level data.
21// Note: this function and many that it calls assume that
22// the input buffer ends with a newline.
23func (parser *Parser) parseBlock(out *bytes.Buffer, data []byte) {
24 if len(data) == 0 || data[len(data)-1] != '\n' {
25 panic("parseBlock input is missing terminating newline")
26 }
27
28 // this is called recursively: enforce a maximum depth
29 if parser.nesting >= parser.maxNesting {
30 return
31 }
32 parser.nesting++
33
34 // parse out one block-level construct at a time
35 for len(data) > 0 {
36 // prefixed header:
37 //
38 // # Header 1
39 // ## Header 2
40 // ...
41 // ###### Header 6
42 if parser.isPrefixHeader(data) {
43 data = data[parser.blockPrefixHeader(out, data):]
44 continue
45 }
46
47 // block of preformatted HTML:
48 //
49 // <div>
50 // ...
51 // </div>
52 if data[0] == '<' {
53 if i := parser.blockHtml(out, data, true); i > 0 {
54 data = data[i:]
55 continue
56 }
57 }
58
59 // blank lines. note: returns the # of bytes to skip
60 if i := parser.isEmpty(data); i > 0 {
61 data = data[i:]
62 continue
63 }
64
65 // horizontal rule:
66 //
67 // ------
68 // or
69 // ******
70 // or
71 // ______
72 if parser.isHRule(data) {
73 parser.r.HRule(out)
74 var i int
75 for i = 0; data[i] != '\n'; i++ {
76 }
77 data = data[i:]
78 continue
79 }
80
81 // fenced code block:
82 //
83 // ``` go
84 // func fact(n int) int {
85 // if n <= 1 {
86 // return n
87 // }
88 // return n * fact(n-1)
89 // }
90 // ```
91 if parser.flags&EXTENSION_FENCED_CODE != 0 {
92 if i := parser.blockFencedCode(out, data); i > 0 {
93 data = data[i:]
94 continue
95 }
96 }
97
98 // table:
99 //
100 // Name | Age | Phone
101 // ------|-----|---------
102 // Bob | 31 | 555-1234
103 // Alice | 27 | 555-4321
104 if parser.flags&EXTENSION_TABLES != 0 {
105 if i := parser.blockTable(out, data); i > 0 {
106 data = data[i:]
107 continue
108 }
109 }
110
111 // block quote:
112 //
113 // > A big quote I found somewhere
114 // > on the web
115 if parser.blockQuotePrefix(data) > 0 {
116 data = data[parser.blockQuote(out, data):]
117 continue
118 }
119
120 // indented code block:
121 //
122 // func max(a, b int) int {
123 // if a > b {
124 // return a
125 // }
126 // return b
127 // }
128 if parser.blockCodePrefix(data) > 0 {
129 data = data[parser.blockCode(out, data):]
130 continue
131 }
132
133 // an itemized/unordered list:
134 //
135 // * Item 1
136 // * Item 2
137 //
138 // also works with + or -
139 if parser.blockUliPrefix(data) > 0 {
140 data = data[parser.blockList(out, data, 0):]
141 continue
142 }
143
144 // a numbered/ordered list:
145 //
146 // 1. Item 1
147 // 2. Item 2
148 if parser.blockOliPrefix(data) > 0 {
149 data = data[parser.blockList(out, data, LIST_TYPE_ORDERED):]
150 continue
151 }
152
153 // anything else must look like a normal paragraph
154 // note: this finds underlined headers, too
155 data = data[parser.blockParagraph(out, data):]
156 }
157
158 parser.nesting--
159}
160
161func (parser *Parser) isPrefixHeader(data []byte) bool {
162 if data[0] != '#' {
163 return false
164 }
165
166 if parser.flags&EXTENSION_SPACE_HEADERS != 0 {
167 level := 0
168 for level < 6 && data[level] == '#' {
169 level++
170 }
171 if data[level] != ' ' && data[level] != '\t' {
172 return false
173 }
174 }
175 return true
176}
177
178func (parser *Parser) blockPrefixHeader(out *bytes.Buffer, data []byte) int {
179 level := 0
180 for level < 6 && data[level] == '#' {
181 level++
182 }
183 i, end := 0, 0
184 for i = level; data[i] == ' ' || data[i] == '\t'; i++ {
185 }
186 for end = i; data[end] != '\n'; end++ {
187 }
188 skip := end
189 for end > 0 && data[end-1] == '#' {
190 end--
191 }
192 for end > 0 && (data[end-1] == ' ' || data[end-1] == '\t') {
193 end--
194 }
195 if end > i {
196 work := func() bool {
197 parser.parseInline(out, data[i:end])
198 return true
199 }
200 parser.r.Header(out, work, level)
201 }
202 return skip
203}
204
205func (parser *Parser) isUnderlinedHeader(data []byte) int {
206 // test of level 1 header
207 if data[0] == '=' {
208 i := 1
209 for data[i] == '=' {
210 i++
211 }
212 for data[i] == ' ' || data[i] == '\t' {
213 i++
214 }
215 if data[i] == '\n' {
216 return 1
217 } else {
218 return 0
219 }
220 }
221
222 // test of level 2 header
223 if data[0] == '-' {
224 i := 1
225 for data[i] == '-' {
226 i++
227 }
228 for data[i] == ' ' || data[i] == '\t' {
229 i++
230 }
231 if data[i] == '\n' {
232 return 2
233 } else {
234 return 0
235 }
236 }
237
238 return 0
239}
240
241func (parser *Parser) blockHtml(out *bytes.Buffer, data []byte, doRender bool) int {
242 var i, j int
243
244 // identify the opening tag
245 if data[0] != '<' {
246 return 0
247 }
248 curtag, tagfound := parser.blockHtmlFindTag(data[1:])
249
250 // handle special cases
251 if !tagfound {
252 // check for an HTML comment
253 if size := parser.blockHtmlComment(out, data, doRender); size > 0 {
254 return size
255 }
256
257 // check for an <hr> tag
258 if size := parser.blockHtmlHr(out, data, doRender); size > 0 {
259 return size
260 }
261
262 // no special case recognized
263 return 0
264 }
265
266 // look for an unindented matching closing tag
267 // followed by a blank line
268 found := false
269 /*
270 closetag := []byte("\n</" + curtag + ">")
271 j = len(curtag) + 1
272 for !found {
273 // scan for a closing tag at the beginning of a line
274 if skip := bytes.Index(data[j:], closetag); skip >= 0 {
275 j += skip + len(closetag)
276 } else {
277 break
278 }
279
280 // see if it is the only thing on the line
281 if skip := parser.isEmpty(data[j:]); skip > 0 {
282 // see if it is followed by a blank line/eof
283 j += skip
284 if j >= len(data) {
285 found = true
286 i = j
287 } else {
288 if skip := parser.isEmpty(data[j:]); skip > 0 {
289 j += skip
290 found = true
291 i = j
292 }
293 }
294 }
295 }
296 */
297
298 // if not found, try a second pass looking for indented match
299 // but not if tag is "ins" or "del" (following original Markdown.pl)
300 if !found && curtag != "ins" && curtag != "del" {
301 i = 1
302 for i < len(data) {
303 i++
304 for i < len(data) && !(data[i-1] == '<' && data[i] == '/') {
305 i++
306 }
307
308 if i+2+len(curtag) >= len(data) {
309 break
310 }
311
312 j = parser.blockHtmlFindEnd(curtag, data[i-1:])
313
314 if j > 0 {
315 i += j - 1
316 found = true
317 break
318 }
319 }
320 }
321
322 if !found {
323 return 0
324 }
325
326 // the end of the block has been found
327 if doRender {
328 // trim newlines
329 end := i
330 for end > 0 && data[end-1] == '\n' {
331 end--
332 }
333 parser.r.BlockHtml(out, data[:end])
334 }
335
336 return i
337}
338
339// HTML comment, lax form
340func (parser *Parser) blockHtmlComment(out *bytes.Buffer, data []byte, doRender bool) int {
341 if data[0] != '<' || data[1] != '!' || data[2] != '-' || data[3] != '-' {
342 return 0
343 }
344
345 i := 5
346
347 // scan for an end-of-comment marker, across lines if necessary
348 for i < len(data) && !(data[i-2] == '-' && data[i-1] == '-' && data[i] == '>') {
349 i++
350 }
351 i++
352
353 // no end-of-comment marker
354 if i >= len(data) {
355 return 0
356 }
357
358 // needs to end with a blank line
359 if j := parser.isEmpty(data[i:]); j > 0 {
360 size := i + j
361 if doRender {
362 // trim trailing newlines
363 end := size
364 for end > 0 && data[end-1] == '\n' {
365 end--
366 }
367 parser.r.BlockHtml(out, data[:end])
368 }
369 return size
370 }
371
372 return 0
373}
374
375// HR, which is the only self-closing block tag considered
376func (parser *Parser) blockHtmlHr(out *bytes.Buffer, data []byte, doRender bool) int {
377 if data[0] != '<' || (data[1] != 'h' && data[1] != 'H') || (data[2] != 'r' && data[2] != 'R') {
378 return 0
379 }
380 if data[3] != ' ' && data[3] != '\t' && data[3] != '/' && data[3] != '>' {
381 // not an <hr> tag after all; at least not a valid one
382 return 0
383 }
384
385 i := 3
386 for data[i] != '>' && data[i] != '\n' {
387 i++
388 }
389
390 if data[i] == '>' {
391 i++
392 if j := parser.isEmpty(data[i:]); j > 0 {
393 size := i + j
394 if doRender {
395 // trim newlines
396 end := size
397 for end > 0 && data[end-1] == '\n' {
398 end--
399 }
400 parser.r.BlockHtml(out, data[:end])
401 }
402 return size
403 }
404 }
405
406 return 0
407}
408
409func (parser *Parser) blockHtmlFindTag(data []byte) (string, bool) {
410 i := 0
411 for isalnum(data[i]) {
412 i++
413 }
414 key := string(data[:i])
415 if blockTags[key] {
416 return key, true
417 }
418 return "", false
419}
420
421func (parser *Parser) blockHtmlFindEnd(tag string, data []byte) int {
422 // assume data[0] == '<' && data[1] == '/' already tested
423
424 // check if tag is a match
425 closetag := []byte("</" + tag + ">")
426 if !bytes.HasPrefix(data, closetag) {
427 return 0
428 }
429 i := len(closetag)
430
431 // check that the rest of the line is blank
432 skip := 0
433 if skip = parser.isEmpty(data[i:]); skip == 0 {
434 return 0
435 }
436 i += skip
437 skip = 0
438
439 if i >= len(data) {
440 return i
441 }
442
443 if parser.flags&EXTENSION_LAX_HTML_BLOCKS != 0 {
444 return i
445 }
446 if skip = parser.isEmpty(data[i:]); skip == 0 {
447 // following line must be blank
448 return 0
449 }
450
451 return i + skip
452}
453
454func (parser *Parser) isEmpty(data []byte) int {
455 // it is okay to call isEmpty on an empty buffer
456 if len(data) == 0 {
457 return 0
458 }
459
460 var i int
461 for i = 0; data[i] != '\n'; i++ {
462 if data[i] != ' ' && data[i] != '\t' {
463 return 0
464 }
465 }
466 return i + 1
467}
468
469func (parser *Parser) isHRule(data []byte) bool {
470 i := 0
471
472 // skip up to three spaces
473 for i < 3 && data[i] == ' ' {
474 i++
475 }
476
477 // look at the hrule char
478 if data[i] != '*' && data[i] != '-' && data[i] != '_' {
479 return false
480 }
481 c := data[i]
482
483 // the whole line must be the char or whitespace
484 n := 0
485 for data[i] != '\n' {
486 switch {
487 case data[i] == c:
488 n++
489 case data[i] != ' ' && data[i] != '\t':
490 return false
491 }
492 i++
493 }
494
495 return n >= 3
496}
497
498func (parser *Parser) isFencedCode(data []byte, syntax **string, oldmarker string) (skip int, marker string) {
499 i, size := 0, 0
500 skip = 0
501
502 // skip up to three spaces
503 for i < 3 && data[i] == ' ' {
504 i++
505 }
506
507 // check for the marker characters: ~ or `
508 if data[i] != '~' && data[i] != '`' {
509 return
510 }
511
512 c := data[i]
513
514 // the whole line must be the same char or whitespace
515 for data[i] == c {
516 size++
517 i++
518 }
519
520 // the marker char must occur at least 3 times
521 if size < 3 {
522 return
523 }
524 marker = string(data[i-size : i])
525
526 // if this is the end marker, it must match the beginning marker
527 if oldmarker != "" && marker != oldmarker {
528 return
529 }
530
531 if syntax != nil {
532 syn := 0
533
534 for data[i] == ' ' || data[i] == '\t' {
535 i++
536 }
537
538 syntaxStart := i
539
540 if data[i] == '{' {
541 i++
542 syntaxStart++
543
544 for data[i] != '}' && data[i] != '\n' {
545 syn++
546 i++
547 }
548
549 if data[i] != '}' {
550 return
551 }
552
553 // strip all whitespace at the beginning and the end
554 // of the {} block
555 for syn > 0 && isspace(data[syntaxStart]) {
556 syntaxStart++
557 syn--
558 }
559
560 for syn > 0 && isspace(data[syntaxStart+syn-1]) {
561 syn--
562 }
563
564 i++
565 } else {
566 for !isspace(data[i]) {
567 syn++
568 i++
569 }
570 }
571
572 language := string(data[syntaxStart : syntaxStart+syn])
573 *syntax = &language
574 }
575
576 for ; data[i] != '\n'; i++ {
577 if !isspace(data[i]) {
578 return
579 }
580 }
581
582 skip = i + 1
583 return
584}
585
586func (parser *Parser) blockFencedCode(out *bytes.Buffer, data []byte) int {
587 var lang *string
588 beg, marker := parser.isFencedCode(data, &lang, "")
589 if beg == 0 {
590 return 0
591 }
592
593 var work bytes.Buffer
594
595 for beg < len(data) {
596 fenceEnd, _ := parser.isFencedCode(data[beg:], nil, marker)
597 if fenceEnd != 0 {
598 beg += fenceEnd
599 break
600 }
601
602 var end int
603 for end = beg + 1; end < len(data) && data[end-1] != '\n'; end++ {
604 }
605
606 if beg < end {
607 // verbatim copy to the working buffer
608 if parser.isEmpty(data[beg:]) > 0 {
609 work.WriteByte('\n')
610 } else {
611 work.Write(data[beg:end])
612 }
613 }
614 beg = end
615
616 // did we find the end of the buffer without a closing marker?
617 if beg >= len(data) {
618 return 0
619 }
620 }
621
622 if work.Len() > 0 && work.Bytes()[work.Len()-1] != '\n' {
623 work.WriteByte('\n')
624 }
625
626 syntax := ""
627 if lang != nil {
628 syntax = *lang
629 }
630
631 parser.r.BlockCode(out, work.Bytes(), syntax)
632
633 return beg
634}
635
636func (parser *Parser) blockTable(out *bytes.Buffer, data []byte) int {
637 var headerWork bytes.Buffer
638 i, columns, colData := parser.blockTableHeader(&headerWork, data)
639 if i == 0 {
640 return 0
641 }
642
643 var bodyWork bytes.Buffer
644
645 for i < len(data) {
646 pipes, rowStart := 0, i
647 for ; i < len(data) && data[i] != '\n'; i++ {
648 if data[i] == '|' {
649 pipes++
650 }
651 }
652
653 if pipes == 0 || i == len(data) {
654 i = rowStart
655 break
656 }
657
658 parser.blockTableRow(&bodyWork, data[rowStart:i], columns, colData)
659 i++
660 }
661
662 parser.r.Table(out, headerWork.Bytes(), bodyWork.Bytes(), colData)
663
664 return i
665}
666
667func (parser *Parser) blockTableHeader(out *bytes.Buffer, data []byte) (size int, columns int, columnData []int) {
668 i, pipes := 0, 0
669 columnData = []int{}
670 for i = 0; i < len(data) && data[i] != '\n'; i++ {
671 if data[i] == '|' {
672 pipes++
673 }
674 }
675
676 if i == len(data) || pipes == 0 {
677 return 0, 0, columnData
678 }
679
680 headerEnd := i
681
682 if data[0] == '|' {
683 pipes--
684 }
685
686 if i > 2 && data[i-1] == '|' {
687 pipes--
688 }
689
690 columns = pipes + 1
691 columnData = make([]int, columns)
692
693 // parse the header underline
694 i++
695 if i < len(data) && data[i] == '|' {
696 i++
697 }
698
699 underEnd := i
700 for underEnd < len(data) && data[underEnd] != '\n' {
701 underEnd++
702 }
703
704 col := 0
705 for ; col < columns && i < underEnd; col++ {
706 dashes := 0
707
708 for i < underEnd && (data[i] == ' ' || data[i] == '\t') {
709 i++
710 }
711
712 if data[i] == ':' {
713 i++
714 columnData[col] |= TABLE_ALIGNMENT_LEFT
715 dashes++
716 }
717
718 for i < underEnd && data[i] == '-' {
719 i++
720 dashes++
721 }
722
723 if i < underEnd && data[i] == ':' {
724 i++
725 columnData[col] |= TABLE_ALIGNMENT_RIGHT
726 dashes++
727 }
728
729 for i < underEnd && (data[i] == ' ' || data[i] == '\t') {
730 i++
731 }
732
733 if i < underEnd && data[i] != '|' {
734 break
735 }
736
737 if dashes < 3 {
738 break
739 }
740
741 i++
742 }
743
744 if col < columns {
745 return 0, 0, columnData
746 }
747
748 parser.blockTableRow(out, data[:headerEnd], columns, columnData)
749 size = underEnd + 1
750 return
751}
752
753func (parser *Parser) blockTableRow(out *bytes.Buffer, data []byte, columns int, colData []int) {
754 i, col := 0, 0
755 var rowWork bytes.Buffer
756
757 if i < len(data) && data[i] == '|' {
758 i++
759 }
760
761 for col = 0; col < columns && i < len(data); col++ {
762 for i < len(data) && isspace(data[i]) {
763 i++
764 }
765
766 cellStart := i
767
768 for i < len(data) && data[i] != '|' {
769 i++
770 }
771
772 cellEnd := i - 1
773
774 for cellEnd > cellStart && isspace(data[cellEnd]) {
775 cellEnd--
776 }
777
778 var cellWork bytes.Buffer
779 parser.parseInline(&cellWork, data[cellStart:cellEnd+1])
780
781 cdata := 0
782 if col < len(colData) {
783 cdata = colData[col]
784 }
785 parser.r.TableCell(&rowWork, cellWork.Bytes(), cdata)
786
787 i++
788 }
789
790 for ; col < columns; col++ {
791 emptyCell := []byte{}
792 cdata := 0
793 if col < len(colData) {
794 cdata = colData[col]
795 }
796 parser.r.TableCell(&rowWork, emptyCell, cdata)
797 }
798
799 parser.r.TableRow(out, rowWork.Bytes())
800}
801
802// returns blockquote prefix length
803func (parser *Parser) blockQuotePrefix(data []byte) int {
804 i := 0
805 for i < 3 && data[i] == ' ' {
806 i++
807 }
808 if data[i] == '>' {
809 if data[i+1] == ' ' || data[i+1] == '\t' {
810 return i + 2
811 }
812 return i + 1
813 }
814 return 0
815}
816
817// parse a blockquote fragment
818func (parser *Parser) blockQuote(out *bytes.Buffer, data []byte) int {
819 var raw bytes.Buffer
820 beg, end := 0, 0
821 for beg < len(data) {
822 for end = beg + 1; data[end-1] != '\n'; end++ {
823 }
824
825 if pre := parser.blockQuotePrefix(data[beg:]); pre > 0 {
826 // string the prefix
827 beg += pre
828 } else {
829 // blockquote ends with at least one blank line
830 // followed by something without a blockquote prefix
831 if parser.isEmpty(data[beg:]) > 0 &&
832 (end >= len(data) ||
833 (parser.blockQuotePrefix(data[end:]) == 0 && parser.isEmpty(data[end:]) == 0)) {
834 break
835 }
836 }
837
838 // this line is part of the blockquote
839 raw.Write(data[beg:end])
840 beg = end
841 }
842
843 var cooked bytes.Buffer
844 parser.parseBlock(&cooked, raw.Bytes())
845 parser.r.BlockQuote(out, cooked.Bytes())
846 return end
847}
848
849// returns prefix length for block code
850func (parser *Parser) blockCodePrefix(data []byte) int {
851 if len(data) > 0 && data[0] == '\t' {
852 return 1
853 }
854 if len(data) > 3 && data[0] == ' ' && data[1] == ' ' && data[2] == ' ' && data[3] == ' ' {
855 return 4
856 }
857 return 0
858}
859
860func (parser *Parser) blockCode(out *bytes.Buffer, data []byte) int {
861 var work bytes.Buffer
862
863 beg, end := 0, 0
864 for beg < len(data) {
865 for end = beg + 1; end < len(data) && data[end-1] != '\n'; end++ {
866 }
867
868 if pre := parser.blockCodePrefix(data[beg:end]); pre > 0 {
869 beg += pre
870 } else {
871 if parser.isEmpty(data[beg:end]) == 0 {
872 // non-empty non-prefixed line breaks the pre
873 break
874 }
875 }
876
877 if beg < end {
878 // verbatim copy to the working buffer, escaping entities
879 if parser.isEmpty(data[beg:end]) > 0 {
880 work.WriteByte('\n')
881 } else {
882 work.Write(data[beg:end])
883 }
884 }
885 beg = end
886 }
887
888 // trim all the \n off the end of work
889 workbytes := work.Bytes()
890 n := 0
891 for len(workbytes) > n && workbytes[len(workbytes)-n-1] == '\n' {
892 n++
893 }
894 if n > 0 {
895 work.Truncate(len(workbytes) - n)
896 }
897
898 work.WriteByte('\n')
899
900 parser.r.BlockCode(out, work.Bytes(), "")
901
902 return beg
903}
904
905// returns unordered list item prefix
906func (parser *Parser) blockUliPrefix(data []byte) int {
907 i := 0
908
909 // start with up to 3 spaces
910 for i < len(data) && i < 3 && data[i] == ' ' {
911 i++
912 }
913
914 // need a *, +, or - followed by a space/tab
915 if i+1 >= len(data) ||
916 (data[i] != '*' && data[i] != '+' && data[i] != '-') ||
917 (data[i+1] != ' ' && data[i+1] != '\t') {
918 return 0
919 }
920 return i + 2
921}
922
923// returns ordered list item prefix
924func (parser *Parser) blockOliPrefix(data []byte) int {
925 i := 0
926
927 // start with up to 3 spaces
928 for i < len(data) && i < 3 && data[i] == ' ' {
929 i++
930 }
931
932 // count the digits
933 start := i
934 for i < len(data) && data[i] >= '0' && data[i] <= '9' {
935 i++
936 }
937
938 // we need >= 1 digits followed by a dot and a space/tab
939 if start == i || data[i] != '.' || i+1 >= len(data) ||
940 (data[i+1] != ' ' && data[i+1] != '\t') {
941 return 0
942 }
943 return i + 2
944}
945
946// parse ordered or unordered list block
947func (parser *Parser) blockList(out *bytes.Buffer, data []byte, flags int) int {
948 i := 0
949 work := func() bool {
950 j := 0
951 for i < len(data) {
952 j = parser.blockListItem(out, data[i:], &flags)
953 i += j
954
955 if j == 0 || flags&LIST_ITEM_END_OF_LIST != 0 {
956 break
957 }
958 }
959 return true
960 }
961
962 parser.r.List(out, work, flags)
963 return i
964}
965
966// parse a single list item
967// assumes initial prefix is already removed
968func (parser *Parser) blockListItem(out *bytes.Buffer, data []byte, flags *int) int {
969 // keep track of the first indentation prefix
970 beg, end, pre, sublist, orgpre, i := 0, 0, 0, 0, 0, 0
971
972 for orgpre < 3 && orgpre < len(data) && data[orgpre] == ' ' {
973 orgpre++
974 }
975
976 beg = parser.blockUliPrefix(data)
977 if beg == 0 {
978 beg = parser.blockOliPrefix(data)
979 }
980 if beg == 0 {
981 return 0
982 }
983
984 // skip leading whitespace on first line
985 for beg < len(data) && (data[beg] == ' ' || data[beg] == '\t') {
986 beg++
987 }
988
989 // skip to the beginning of the following line
990 end = beg
991 for end < len(data) && data[end-1] != '\n' {
992 end++
993 }
994
995 // get working buffers
996 var rawItem bytes.Buffer
997 var parsed bytes.Buffer
998
999 // put the first line into the working buffer
1000 rawItem.Write(data[beg:end])
1001 beg = end
1002
1003 // process the following lines
1004 containsBlankLine, containsBlock := false, false
1005 for beg < len(data) {
1006 end++
1007
1008 for end < len(data) && data[end-1] != '\n' {
1009 end++
1010 }
1011
1012 // process an empty line
1013 if parser.isEmpty(data[beg:end]) > 0 {
1014 containsBlankLine = true
1015 beg = end
1016 continue
1017 }
1018
1019 // calculate the indentation
1020 i = 0
1021 for i < 4 && beg+i < end && data[beg+i] == ' ' {
1022 i++
1023 }
1024
1025 pre = i
1026 if data[beg] == '\t' {
1027 i = 1
1028 pre = TAB_SIZE_DEFAULT
1029 if parser.flags&EXTENSION_TAB_SIZE_EIGHT != 0 {
1030 pre = TAB_SIZE_EIGHT
1031 }
1032 }
1033
1034 chunk := data[beg+i : end]
1035
1036 // check for a nested list item
1037 if (parser.blockUliPrefix(chunk) > 0 && !parser.isHRule(chunk)) ||
1038 parser.blockOliPrefix(chunk) > 0 {
1039 if containsBlankLine {
1040 containsBlock = true
1041 }
1042
1043 // the following item must have the same indentation
1044 if pre == orgpre {
1045 break
1046 }
1047
1048 if sublist == 0 {
1049 sublist = rawItem.Len()
1050 }
1051 } else {
1052 // how about a nested prefix header?
1053 if parser.isPrefixHeader(chunk) {
1054 // only nest headers that are indented
1055 if containsBlankLine && i < 4 && data[beg] != '\t' {
1056 *flags |= LIST_ITEM_END_OF_LIST
1057 break
1058 }
1059 containsBlock = true
1060 } else {
1061 // only join stuff after empty lines when indented
1062 if containsBlankLine && i < 4 && data[beg] != '\t' {
1063 *flags |= LIST_ITEM_END_OF_LIST
1064 break
1065 } else {
1066 if containsBlankLine {
1067 rawItem.WriteByte('\n')
1068 containsBlock = true
1069 }
1070 }
1071 }
1072 }
1073
1074 containsBlankLine = false
1075
1076 // add the line into the working buffer without prefix
1077 rawItem.Write(data[beg+i : end])
1078 beg = end
1079 }
1080
1081 // render li contents
1082 if containsBlock {
1083 *flags |= LIST_ITEM_CONTAINS_BLOCK
1084 }
1085
1086 rawItemBytes := rawItem.Bytes()
1087 if *flags&LIST_ITEM_CONTAINS_BLOCK != 0 {
1088 // intermediate render of block li
1089 if sublist > 0 && sublist < len(rawItemBytes) {
1090 parser.parseBlock(&parsed, rawItemBytes[:sublist])
1091 parser.parseBlock(&parsed, rawItemBytes[sublist:])
1092 } else {
1093 parser.parseBlock(&parsed, rawItemBytes)
1094 }
1095 } else {
1096 // intermediate render of inline li
1097 if sublist > 0 && sublist < len(rawItemBytes) {
1098 parser.parseInline(&parsed, rawItemBytes[:sublist])
1099 parser.parseBlock(&parsed, rawItemBytes[sublist:])
1100 } else {
1101 parser.parseInline(&parsed, rawItemBytes)
1102 }
1103 }
1104
1105 // render li itself
1106 parsedBytes := parsed.Bytes()
1107 parsedEnd := len(parsedBytes)
1108 for parsedEnd > 0 && parsedBytes[parsedEnd-1] == '\n' {
1109 parsedEnd--
1110 }
1111 parser.r.ListItem(out, parsedBytes[:parsedEnd], *flags)
1112
1113 return beg
1114}
1115
1116// render a single paragraph that has already been parsed out
1117func (parser *Parser) renderParagraph(out *bytes.Buffer, data []byte) {
1118 // trim leading whitespace
1119 beg := 0
1120 for beg < len(data) && isspace(data[beg]) {
1121 beg++
1122 }
1123
1124 // trim trailing whitespace
1125 end := len(data)
1126 for end > beg && isspace(data[end-1]) {
1127 end--
1128 }
1129 if end == beg {
1130 return
1131 }
1132
1133 work := func() bool {
1134 parser.parseInline(out, data[beg:end])
1135 return true
1136 }
1137 parser.r.Paragraph(out, work)
1138}
1139
1140func (parser *Parser) blockParagraph(out *bytes.Buffer, data []byte) int {
1141 // prev: index of 1st char of previous line
1142 // line: index of 1st char of current line
1143 // i: index of cursor/end of current line
1144 var prev, line, i int
1145
1146 // keep going until we find something to mark the end of the paragraph
1147 for i < len(data) {
1148 // mark the beginning of the current line
1149 prev = line
1150 current := data[i:]
1151 line = i
1152
1153 // did we find a blank line marking the end of the paragraph?
1154 if n := parser.isEmpty(current); n > 0 {
1155 parser.renderParagraph(out, data[:i])
1156 return i + n
1157 }
1158
1159 // an underline under some text marks a header, so our paragraph ended on prev line
1160 if i > 0 {
1161 if level := parser.isUnderlinedHeader(current); level > 0 {
1162 // render the paragraph
1163 parser.renderParagraph(out, data[:prev])
1164
1165 // ignore leading and trailing whitespace
1166 eol := i - 1
1167 for prev < eol && (data[prev] == ' ' || data[prev] == '\t') {
1168 prev++
1169 }
1170 for eol > prev && (data[eol-1] == ' ' || data[eol-1] == '\t') {
1171 eol--
1172 }
1173
1174 // render the header
1175 // this ugly double closure avoids forcing variables onto the heap
1176 work := func(o *bytes.Buffer, p *Parser, d []byte) func() bool {
1177 return func() bool {
1178 p.parseInline(o, d)
1179 return true
1180 }
1181 }(out, parser, data[prev:eol])
1182 parser.r.Header(out, work, level)
1183
1184 // find the end of the underline
1185 for ; i < len(data) && data[i] != '\n'; i++ {
1186 }
1187 return i
1188 }
1189 }
1190
1191 // if the next line starts a block of HTML, then the paragraph ends here
1192 if parser.flags&EXTENSION_LAX_HTML_BLOCKS != 0 {
1193 if data[i] == '<' && parser.blockHtml(out, current, false) > 0 {
1194 // rewind to before the HTML block
1195 parser.renderParagraph(out, data[:i])
1196 return i
1197 }
1198 }
1199
1200 // if there's a prefixed header or a horizontal rule after this, paragraph is over
1201 if parser.isPrefixHeader(current) || parser.isHRule(current) {
1202 parser.renderParagraph(out, data[:i])
1203 return i
1204 }
1205
1206 // otherwise, scan to the beginning of the next line
1207 i++
1208 for i < len(data) && data[i-1] != '\n' {
1209 i++
1210 }
1211 }
1212
1213 parser.renderParagraph(out, data[:i])
1214 return i
1215}