|
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 |
|
14 package blackfriday |
|
15 |
|
16 import ( |
|
17 "bytes" |
|
18 "strings" |
|
19 "unicode" |
|
20 ) |
|
21 |
|
22 // Parse block-level data. |
|
23 // Note: this function and many that it calls assume that |
|
24 // the input buffer ends with a newline. |
|
25 func (p *parser) block(out *bytes.Buffer, data []byte) { |
|
26 if len(data) == 0 || data[len(data)-1] != '\n' { |
|
27 panic("block input is missing terminating newline") |
|
28 } |
|
29 |
|
30 // this is called recursively: enforce a maximum depth |
|
31 if p.nesting >= p.maxNesting { |
|
32 return |
|
33 } |
|
34 p.nesting++ |
|
35 |
|
36 // parse out one block-level construct at a time |
|
37 for len(data) > 0 { |
|
38 // prefixed header: |
|
39 // |
|
40 // # Header 1 |
|
41 // ## Header 2 |
|
42 // ... |
|
43 // ###### Header 6 |
|
44 if p.isPrefixHeader(data) { |
|
45 data = data[p.prefixHeader(out, data):] |
|
46 continue |
|
47 } |
|
48 |
|
49 // block of preformatted HTML: |
|
50 // |
|
51 // <div> |
|
52 // ... |
|
53 // </div> |
|
54 if data[0] == '<' { |
|
55 if i := p.html(out, data, true); i > 0 { |
|
56 data = data[i:] |
|
57 continue |
|
58 } |
|
59 } |
|
60 |
|
61 // title block |
|
62 // |
|
63 // % stuff |
|
64 // % more stuff |
|
65 // % even more stuff |
|
66 if p.flags&EXTENSION_TITLEBLOCK != 0 { |
|
67 if data[0] == '%' { |
|
68 if i := p.titleBlock(out, data, true); i > 0 { |
|
69 data = data[i:] |
|
70 continue |
|
71 } |
|
72 } |
|
73 } |
|
74 |
|
75 // blank lines. note: returns the # of bytes to skip |
|
76 if i := p.isEmpty(data); i > 0 { |
|
77 data = data[i:] |
|
78 continue |
|
79 } |
|
80 |
|
81 // indented code block: |
|
82 // |
|
83 // func max(a, b int) int { |
|
84 // if a > b { |
|
85 // return a |
|
86 // } |
|
87 // return b |
|
88 // } |
|
89 if p.codePrefix(data) > 0 { |
|
90 data = data[p.code(out, data):] |
|
91 continue |
|
92 } |
|
93 |
|
94 // fenced code block: |
|
95 // |
|
96 // ``` go info string here |
|
97 // func fact(n int) int { |
|
98 // if n <= 1 { |
|
99 // return n |
|
100 // } |
|
101 // return n * fact(n-1) |
|
102 // } |
|
103 // ``` |
|
104 if p.flags&EXTENSION_FENCED_CODE != 0 { |
|
105 if i := p.fencedCodeBlock(out, data, true); i > 0 { |
|
106 data = data[i:] |
|
107 continue |
|
108 } |
|
109 } |
|
110 |
|
111 // horizontal rule: |
|
112 // |
|
113 // ------ |
|
114 // or |
|
115 // ****** |
|
116 // or |
|
117 // ______ |
|
118 if p.isHRule(data) { |
|
119 p.r.HRule(out) |
|
120 var i int |
|
121 for i = 0; data[i] != '\n'; i++ { |
|
122 } |
|
123 data = data[i:] |
|
124 continue |
|
125 } |
|
126 |
|
127 // block quote: |
|
128 // |
|
129 // > A big quote I found somewhere |
|
130 // > on the web |
|
131 if p.quotePrefix(data) > 0 { |
|
132 data = data[p.quote(out, data):] |
|
133 continue |
|
134 } |
|
135 |
|
136 // table: |
|
137 // |
|
138 // Name | Age | Phone |
|
139 // ------|-----|--------- |
|
140 // Bob | 31 | 555-1234 |
|
141 // Alice | 27 | 555-4321 |
|
142 if p.flags&EXTENSION_TABLES != 0 { |
|
143 if i := p.table(out, data); i > 0 { |
|
144 data = data[i:] |
|
145 continue |
|
146 } |
|
147 } |
|
148 |
|
149 // an itemized/unordered list: |
|
150 // |
|
151 // * Item 1 |
|
152 // * Item 2 |
|
153 // |
|
154 // also works with + or - |
|
155 if p.uliPrefix(data) > 0 { |
|
156 data = data[p.list(out, data, 0):] |
|
157 continue |
|
158 } |
|
159 |
|
160 // a numbered/ordered list: |
|
161 // |
|
162 // 1. Item 1 |
|
163 // 2. Item 2 |
|
164 if p.oliPrefix(data) > 0 { |
|
165 data = data[p.list(out, data, LIST_TYPE_ORDERED):] |
|
166 continue |
|
167 } |
|
168 |
|
169 // definition lists: |
|
170 // |
|
171 // Term 1 |
|
172 // : Definition a |
|
173 // : Definition b |
|
174 // |
|
175 // Term 2 |
|
176 // : Definition c |
|
177 if p.flags&EXTENSION_DEFINITION_LISTS != 0 { |
|
178 if p.dliPrefix(data) > 0 { |
|
179 data = data[p.list(out, data, LIST_TYPE_DEFINITION):] |
|
180 continue |
|
181 } |
|
182 } |
|
183 |
|
184 // anything else must look like a normal paragraph |
|
185 // note: this finds underlined headers, too |
|
186 data = data[p.paragraph(out, data):] |
|
187 } |
|
188 |
|
189 p.nesting-- |
|
190 } |
|
191 |
|
192 func (p *parser) isPrefixHeader(data []byte) bool { |
|
193 if data[0] != '#' { |
|
194 return false |
|
195 } |
|
196 |
|
197 if p.flags&EXTENSION_SPACE_HEADERS != 0 { |
|
198 level := 0 |
|
199 for level < 6 && data[level] == '#' { |
|
200 level++ |
|
201 } |
|
202 if data[level] != ' ' { |
|
203 return false |
|
204 } |
|
205 } |
|
206 return true |
|
207 } |
|
208 |
|
209 func (p *parser) prefixHeader(out *bytes.Buffer, data []byte) int { |
|
210 level := 0 |
|
211 for level < 6 && data[level] == '#' { |
|
212 level++ |
|
213 } |
|
214 i := skipChar(data, level, ' ') |
|
215 end := skipUntilChar(data, i, '\n') |
|
216 skip := end |
|
217 id := "" |
|
218 if p.flags&EXTENSION_HEADER_IDS != 0 { |
|
219 j, k := 0, 0 |
|
220 // find start/end of header id |
|
221 for j = i; j < end-1 && (data[j] != '{' || data[j+1] != '#'); j++ { |
|
222 } |
|
223 for k = j + 1; k < end && data[k] != '}'; k++ { |
|
224 } |
|
225 // extract header id iff found |
|
226 if j < end && k < end { |
|
227 id = string(data[j+2 : k]) |
|
228 end = j |
|
229 skip = k + 1 |
|
230 for end > 0 && data[end-1] == ' ' { |
|
231 end-- |
|
232 } |
|
233 } |
|
234 } |
|
235 for end > 0 && data[end-1] == '#' { |
|
236 if isBackslashEscaped(data, end-1) { |
|
237 break |
|
238 } |
|
239 end-- |
|
240 } |
|
241 for end > 0 && data[end-1] == ' ' { |
|
242 end-- |
|
243 } |
|
244 if end > i { |
|
245 if id == "" && p.flags&EXTENSION_AUTO_HEADER_IDS != 0 { |
|
246 id = SanitizedAnchorName(string(data[i:end])) |
|
247 } |
|
248 work := func() bool { |
|
249 p.inline(out, data[i:end]) |
|
250 return true |
|
251 } |
|
252 p.r.Header(out, work, level, id) |
|
253 } |
|
254 return skip |
|
255 } |
|
256 |
|
257 func (p *parser) isUnderlinedHeader(data []byte) int { |
|
258 // test of level 1 header |
|
259 if data[0] == '=' { |
|
260 i := skipChar(data, 1, '=') |
|
261 i = skipChar(data, i, ' ') |
|
262 if data[i] == '\n' { |
|
263 return 1 |
|
264 } else { |
|
265 return 0 |
|
266 } |
|
267 } |
|
268 |
|
269 // test of level 2 header |
|
270 if data[0] == '-' { |
|
271 i := skipChar(data, 1, '-') |
|
272 i = skipChar(data, i, ' ') |
|
273 if data[i] == '\n' { |
|
274 return 2 |
|
275 } else { |
|
276 return 0 |
|
277 } |
|
278 } |
|
279 |
|
280 return 0 |
|
281 } |
|
282 |
|
283 func (p *parser) titleBlock(out *bytes.Buffer, data []byte, doRender bool) int { |
|
284 if data[0] != '%' { |
|
285 return 0 |
|
286 } |
|
287 splitData := bytes.Split(data, []byte("\n")) |
|
288 var i int |
|
289 for idx, b := range splitData { |
|
290 if !bytes.HasPrefix(b, []byte("%")) { |
|
291 i = idx // - 1 |
|
292 break |
|
293 } |
|
294 } |
|
295 |
|
296 data = bytes.Join(splitData[0:i], []byte("\n")) |
|
297 p.r.TitleBlock(out, data) |
|
298 |
|
299 return len(data) |
|
300 } |
|
301 |
|
302 func (p *parser) html(out *bytes.Buffer, data []byte, doRender bool) int { |
|
303 var i, j int |
|
304 |
|
305 // identify the opening tag |
|
306 if data[0] != '<' { |
|
307 return 0 |
|
308 } |
|
309 curtag, tagfound := p.htmlFindTag(data[1:]) |
|
310 |
|
311 // handle special cases |
|
312 if !tagfound { |
|
313 // check for an HTML comment |
|
314 if size := p.htmlComment(out, data, doRender); size > 0 { |
|
315 return size |
|
316 } |
|
317 |
|
318 // check for an <hr> tag |
|
319 if size := p.htmlHr(out, data, doRender); size > 0 { |
|
320 return size |
|
321 } |
|
322 |
|
323 // check for HTML CDATA |
|
324 if size := p.htmlCDATA(out, data, doRender); size > 0 { |
|
325 return size |
|
326 } |
|
327 |
|
328 // no special case recognized |
|
329 return 0 |
|
330 } |
|
331 |
|
332 // look for an unindented matching closing tag |
|
333 // followed by a blank line |
|
334 found := false |
|
335 /* |
|
336 closetag := []byte("\n</" + curtag + ">") |
|
337 j = len(curtag) + 1 |
|
338 for !found { |
|
339 // scan for a closing tag at the beginning of a line |
|
340 if skip := bytes.Index(data[j:], closetag); skip >= 0 { |
|
341 j += skip + len(closetag) |
|
342 } else { |
|
343 break |
|
344 } |
|
345 |
|
346 // see if it is the only thing on the line |
|
347 if skip := p.isEmpty(data[j:]); skip > 0 { |
|
348 // see if it is followed by a blank line/eof |
|
349 j += skip |
|
350 if j >= len(data) { |
|
351 found = true |
|
352 i = j |
|
353 } else { |
|
354 if skip := p.isEmpty(data[j:]); skip > 0 { |
|
355 j += skip |
|
356 found = true |
|
357 i = j |
|
358 } |
|
359 } |
|
360 } |
|
361 } |
|
362 */ |
|
363 |
|
364 // if not found, try a second pass looking for indented match |
|
365 // but not if tag is "ins" or "del" (following original Markdown.pl) |
|
366 if !found && curtag != "ins" && curtag != "del" { |
|
367 i = 1 |
|
368 for i < len(data) { |
|
369 i++ |
|
370 for i < len(data) && !(data[i-1] == '<' && data[i] == '/') { |
|
371 i++ |
|
372 } |
|
373 |
|
374 if i+2+len(curtag) >= len(data) { |
|
375 break |
|
376 } |
|
377 |
|
378 j = p.htmlFindEnd(curtag, data[i-1:]) |
|
379 |
|
380 if j > 0 { |
|
381 i += j - 1 |
|
382 found = true |
|
383 break |
|
384 } |
|
385 } |
|
386 } |
|
387 |
|
388 if !found { |
|
389 return 0 |
|
390 } |
|
391 |
|
392 // the end of the block has been found |
|
393 if doRender { |
|
394 // trim newlines |
|
395 end := i |
|
396 for end > 0 && data[end-1] == '\n' { |
|
397 end-- |
|
398 } |
|
399 p.r.BlockHtml(out, data[:end]) |
|
400 } |
|
401 |
|
402 return i |
|
403 } |
|
404 |
|
405 func (p *parser) renderHTMLBlock(out *bytes.Buffer, data []byte, start int, doRender bool) int { |
|
406 // html block needs to end with a blank line |
|
407 if i := p.isEmpty(data[start:]); i > 0 { |
|
408 size := start + i |
|
409 if doRender { |
|
410 // trim trailing newlines |
|
411 end := size |
|
412 for end > 0 && data[end-1] == '\n' { |
|
413 end-- |
|
414 } |
|
415 p.r.BlockHtml(out, data[:end]) |
|
416 } |
|
417 return size |
|
418 } |
|
419 return 0 |
|
420 } |
|
421 |
|
422 // HTML comment, lax form |
|
423 func (p *parser) htmlComment(out *bytes.Buffer, data []byte, doRender bool) int { |
|
424 i := p.inlineHTMLComment(out, data) |
|
425 return p.renderHTMLBlock(out, data, i, doRender) |
|
426 } |
|
427 |
|
428 // HTML CDATA section |
|
429 func (p *parser) htmlCDATA(out *bytes.Buffer, data []byte, doRender bool) int { |
|
430 const cdataTag = "<![cdata[" |
|
431 const cdataTagLen = len(cdataTag) |
|
432 if len(data) < cdataTagLen+1 { |
|
433 return 0 |
|
434 } |
|
435 if !bytes.Equal(bytes.ToLower(data[:cdataTagLen]), []byte(cdataTag)) { |
|
436 return 0 |
|
437 } |
|
438 i := cdataTagLen |
|
439 // scan for an end-of-comment marker, across lines if necessary |
|
440 for i < len(data) && !(data[i-2] == ']' && data[i-1] == ']' && data[i] == '>') { |
|
441 i++ |
|
442 } |
|
443 i++ |
|
444 // no end-of-comment marker |
|
445 if i >= len(data) { |
|
446 return 0 |
|
447 } |
|
448 return p.renderHTMLBlock(out, data, i, doRender) |
|
449 } |
|
450 |
|
451 // HR, which is the only self-closing block tag considered |
|
452 func (p *parser) htmlHr(out *bytes.Buffer, data []byte, doRender bool) int { |
|
453 if data[0] != '<' || (data[1] != 'h' && data[1] != 'H') || (data[2] != 'r' && data[2] != 'R') { |
|
454 return 0 |
|
455 } |
|
456 if data[3] != ' ' && data[3] != '/' && data[3] != '>' { |
|
457 // not an <hr> tag after all; at least not a valid one |
|
458 return 0 |
|
459 } |
|
460 |
|
461 i := 3 |
|
462 for data[i] != '>' && data[i] != '\n' { |
|
463 i++ |
|
464 } |
|
465 |
|
466 if data[i] == '>' { |
|
467 return p.renderHTMLBlock(out, data, i+1, doRender) |
|
468 } |
|
469 |
|
470 return 0 |
|
471 } |
|
472 |
|
473 func (p *parser) htmlFindTag(data []byte) (string, bool) { |
|
474 i := 0 |
|
475 for isalnum(data[i]) { |
|
476 i++ |
|
477 } |
|
478 key := string(data[:i]) |
|
479 if _, ok := blockTags[key]; ok { |
|
480 return key, true |
|
481 } |
|
482 return "", false |
|
483 } |
|
484 |
|
485 func (p *parser) htmlFindEnd(tag string, data []byte) int { |
|
486 // assume data[0] == '<' && data[1] == '/' already tested |
|
487 |
|
488 // check if tag is a match |
|
489 closetag := []byte("</" + tag + ">") |
|
490 if !bytes.HasPrefix(data, closetag) { |
|
491 return 0 |
|
492 } |
|
493 i := len(closetag) |
|
494 |
|
495 // check that the rest of the line is blank |
|
496 skip := 0 |
|
497 if skip = p.isEmpty(data[i:]); skip == 0 { |
|
498 return 0 |
|
499 } |
|
500 i += skip |
|
501 skip = 0 |
|
502 |
|
503 if i >= len(data) { |
|
504 return i |
|
505 } |
|
506 |
|
507 if p.flags&EXTENSION_LAX_HTML_BLOCKS != 0 { |
|
508 return i |
|
509 } |
|
510 if skip = p.isEmpty(data[i:]); skip == 0 { |
|
511 // following line must be blank |
|
512 return 0 |
|
513 } |
|
514 |
|
515 return i + skip |
|
516 } |
|
517 |
|
518 func (*parser) isEmpty(data []byte) int { |
|
519 // it is okay to call isEmpty on an empty buffer |
|
520 if len(data) == 0 { |
|
521 return 0 |
|
522 } |
|
523 |
|
524 var i int |
|
525 for i = 0; i < len(data) && data[i] != '\n'; i++ { |
|
526 if data[i] != ' ' && data[i] != '\t' { |
|
527 return 0 |
|
528 } |
|
529 } |
|
530 return i + 1 |
|
531 } |
|
532 |
|
533 func (*parser) isHRule(data []byte) bool { |
|
534 i := 0 |
|
535 |
|
536 // skip up to three spaces |
|
537 for i < 3 && data[i] == ' ' { |
|
538 i++ |
|
539 } |
|
540 |
|
541 // look at the hrule char |
|
542 if data[i] != '*' && data[i] != '-' && data[i] != '_' { |
|
543 return false |
|
544 } |
|
545 c := data[i] |
|
546 |
|
547 // the whole line must be the char or whitespace |
|
548 n := 0 |
|
549 for data[i] != '\n' { |
|
550 switch { |
|
551 case data[i] == c: |
|
552 n++ |
|
553 case data[i] != ' ': |
|
554 return false |
|
555 } |
|
556 i++ |
|
557 } |
|
558 |
|
559 return n >= 3 |
|
560 } |
|
561 |
|
562 // isFenceLine checks if there's a fence line (e.g., ``` or ``` go) at the beginning of data, |
|
563 // and returns the end index if so, or 0 otherwise. It also returns the marker found. |
|
564 // If syntax is not nil, it gets set to the syntax specified in the fence line. |
|
565 // A final newline is mandatory to recognize the fence line, unless newlineOptional is true. |
|
566 func isFenceLine(data []byte, info *string, oldmarker string, newlineOptional bool) (end int, marker string) { |
|
567 i, size := 0, 0 |
|
568 |
|
569 // skip up to three spaces |
|
570 for i < len(data) && i < 3 && data[i] == ' ' { |
|
571 i++ |
|
572 } |
|
573 |
|
574 // check for the marker characters: ~ or ` |
|
575 if i >= len(data) { |
|
576 return 0, "" |
|
577 } |
|
578 if data[i] != '~' && data[i] != '`' { |
|
579 return 0, "" |
|
580 } |
|
581 |
|
582 c := data[i] |
|
583 |
|
584 // the whole line must be the same char or whitespace |
|
585 for i < len(data) && data[i] == c { |
|
586 size++ |
|
587 i++ |
|
588 } |
|
589 |
|
590 // the marker char must occur at least 3 times |
|
591 if size < 3 { |
|
592 return 0, "" |
|
593 } |
|
594 marker = string(data[i-size : i]) |
|
595 |
|
596 // if this is the end marker, it must match the beginning marker |
|
597 if oldmarker != "" && marker != oldmarker { |
|
598 return 0, "" |
|
599 } |
|
600 |
|
601 // TODO(shurcooL): It's probably a good idea to simplify the 2 code paths here |
|
602 // into one, always get the info string, and discard it if the caller doesn't care. |
|
603 if info != nil { |
|
604 infoLength := 0 |
|
605 i = skipChar(data, i, ' ') |
|
606 |
|
607 if i >= len(data) { |
|
608 if newlineOptional && i == len(data) { |
|
609 return i, marker |
|
610 } |
|
611 return 0, "" |
|
612 } |
|
613 |
|
614 infoStart := i |
|
615 |
|
616 if data[i] == '{' { |
|
617 i++ |
|
618 infoStart++ |
|
619 |
|
620 for i < len(data) && data[i] != '}' && data[i] != '\n' { |
|
621 infoLength++ |
|
622 i++ |
|
623 } |
|
624 |
|
625 if i >= len(data) || data[i] != '}' { |
|
626 return 0, "" |
|
627 } |
|
628 |
|
629 // strip all whitespace at the beginning and the end |
|
630 // of the {} block |
|
631 for infoLength > 0 && isspace(data[infoStart]) { |
|
632 infoStart++ |
|
633 infoLength-- |
|
634 } |
|
635 |
|
636 for infoLength > 0 && isspace(data[infoStart+infoLength-1]) { |
|
637 infoLength-- |
|
638 } |
|
639 |
|
640 i++ |
|
641 } else { |
|
642 for i < len(data) && !isverticalspace(data[i]) { |
|
643 infoLength++ |
|
644 i++ |
|
645 } |
|
646 } |
|
647 |
|
648 *info = strings.TrimSpace(string(data[infoStart : infoStart+infoLength])) |
|
649 } |
|
650 |
|
651 i = skipChar(data, i, ' ') |
|
652 if i >= len(data) || data[i] != '\n' { |
|
653 if newlineOptional && i == len(data) { |
|
654 return i, marker |
|
655 } |
|
656 return 0, "" |
|
657 } |
|
658 |
|
659 return i + 1, marker // Take newline into account. |
|
660 } |
|
661 |
|
662 // fencedCodeBlock returns the end index if data contains a fenced code block at the beginning, |
|
663 // or 0 otherwise. It writes to out if doRender is true, otherwise it has no side effects. |
|
664 // If doRender is true, a final newline is mandatory to recognize the fenced code block. |
|
665 func (p *parser) fencedCodeBlock(out *bytes.Buffer, data []byte, doRender bool) int { |
|
666 var infoString string |
|
667 beg, marker := isFenceLine(data, &infoString, "", false) |
|
668 if beg == 0 || beg >= len(data) { |
|
669 return 0 |
|
670 } |
|
671 |
|
672 var work bytes.Buffer |
|
673 |
|
674 for { |
|
675 // safe to assume beg < len(data) |
|
676 |
|
677 // check for the end of the code block |
|
678 newlineOptional := !doRender |
|
679 fenceEnd, _ := isFenceLine(data[beg:], nil, marker, newlineOptional) |
|
680 if fenceEnd != 0 { |
|
681 beg += fenceEnd |
|
682 break |
|
683 } |
|
684 |
|
685 // copy the current line |
|
686 end := skipUntilChar(data, beg, '\n') + 1 |
|
687 |
|
688 // did we reach the end of the buffer without a closing marker? |
|
689 if end >= len(data) { |
|
690 return 0 |
|
691 } |
|
692 |
|
693 // verbatim copy to the working buffer |
|
694 if doRender { |
|
695 work.Write(data[beg:end]) |
|
696 } |
|
697 beg = end |
|
698 } |
|
699 |
|
700 if doRender { |
|
701 p.r.BlockCode(out, work.Bytes(), infoString) |
|
702 } |
|
703 |
|
704 return beg |
|
705 } |
|
706 |
|
707 func (p *parser) table(out *bytes.Buffer, data []byte) int { |
|
708 var header bytes.Buffer |
|
709 i, columns := p.tableHeader(&header, data) |
|
710 if i == 0 { |
|
711 return 0 |
|
712 } |
|
713 |
|
714 var body bytes.Buffer |
|
715 |
|
716 for i < len(data) { |
|
717 pipes, rowStart := 0, i |
|
718 for ; data[i] != '\n'; i++ { |
|
719 if data[i] == '|' { |
|
720 pipes++ |
|
721 } |
|
722 } |
|
723 |
|
724 if pipes == 0 { |
|
725 i = rowStart |
|
726 break |
|
727 } |
|
728 |
|
729 // include the newline in data sent to tableRow |
|
730 i++ |
|
731 p.tableRow(&body, data[rowStart:i], columns, false) |
|
732 } |
|
733 |
|
734 p.r.Table(out, header.Bytes(), body.Bytes(), columns) |
|
735 |
|
736 return i |
|
737 } |
|
738 |
|
739 // check if the specified position is preceded by an odd number of backslashes |
|
740 func isBackslashEscaped(data []byte, i int) bool { |
|
741 backslashes := 0 |
|
742 for i-backslashes-1 >= 0 && data[i-backslashes-1] == '\\' { |
|
743 backslashes++ |
|
744 } |
|
745 return backslashes&1 == 1 |
|
746 } |
|
747 |
|
748 func (p *parser) tableHeader(out *bytes.Buffer, data []byte) (size int, columns []int) { |
|
749 i := 0 |
|
750 colCount := 1 |
|
751 for i = 0; data[i] != '\n'; i++ { |
|
752 if data[i] == '|' && !isBackslashEscaped(data, i) { |
|
753 colCount++ |
|
754 } |
|
755 } |
|
756 |
|
757 // doesn't look like a table header |
|
758 if colCount == 1 { |
|
759 return |
|
760 } |
|
761 |
|
762 // include the newline in the data sent to tableRow |
|
763 header := data[:i+1] |
|
764 |
|
765 // column count ignores pipes at beginning or end of line |
|
766 if data[0] == '|' { |
|
767 colCount-- |
|
768 } |
|
769 if i > 2 && data[i-1] == '|' && !isBackslashEscaped(data, i-1) { |
|
770 colCount-- |
|
771 } |
|
772 |
|
773 columns = make([]int, colCount) |
|
774 |
|
775 // move on to the header underline |
|
776 i++ |
|
777 if i >= len(data) { |
|
778 return |
|
779 } |
|
780 |
|
781 if data[i] == '|' && !isBackslashEscaped(data, i) { |
|
782 i++ |
|
783 } |
|
784 i = skipChar(data, i, ' ') |
|
785 |
|
786 // each column header is of form: / *:?-+:? *|/ with # dashes + # colons >= 3 |
|
787 // and trailing | optional on last column |
|
788 col := 0 |
|
789 for data[i] != '\n' { |
|
790 dashes := 0 |
|
791 |
|
792 if data[i] == ':' { |
|
793 i++ |
|
794 columns[col] |= TABLE_ALIGNMENT_LEFT |
|
795 dashes++ |
|
796 } |
|
797 for data[i] == '-' { |
|
798 i++ |
|
799 dashes++ |
|
800 } |
|
801 if data[i] == ':' { |
|
802 i++ |
|
803 columns[col] |= TABLE_ALIGNMENT_RIGHT |
|
804 dashes++ |
|
805 } |
|
806 for data[i] == ' ' { |
|
807 i++ |
|
808 } |
|
809 |
|
810 // end of column test is messy |
|
811 switch { |
|
812 case dashes < 3: |
|
813 // not a valid column |
|
814 return |
|
815 |
|
816 case data[i] == '|' && !isBackslashEscaped(data, i): |
|
817 // marker found, now skip past trailing whitespace |
|
818 col++ |
|
819 i++ |
|
820 for data[i] == ' ' { |
|
821 i++ |
|
822 } |
|
823 |
|
824 // trailing junk found after last column |
|
825 if col >= colCount && data[i] != '\n' { |
|
826 return |
|
827 } |
|
828 |
|
829 case (data[i] != '|' || isBackslashEscaped(data, i)) && col+1 < colCount: |
|
830 // something else found where marker was required |
|
831 return |
|
832 |
|
833 case data[i] == '\n': |
|
834 // marker is optional for the last column |
|
835 col++ |
|
836 |
|
837 default: |
|
838 // trailing junk found after last column |
|
839 return |
|
840 } |
|
841 } |
|
842 if col != colCount { |
|
843 return |
|
844 } |
|
845 |
|
846 p.tableRow(out, header, columns, true) |
|
847 size = i + 1 |
|
848 return |
|
849 } |
|
850 |
|
851 func (p *parser) tableRow(out *bytes.Buffer, data []byte, columns []int, header bool) { |
|
852 i, col := 0, 0 |
|
853 var rowWork bytes.Buffer |
|
854 |
|
855 if data[i] == '|' && !isBackslashEscaped(data, i) { |
|
856 i++ |
|
857 } |
|
858 |
|
859 for col = 0; col < len(columns) && i < len(data); col++ { |
|
860 for data[i] == ' ' { |
|
861 i++ |
|
862 } |
|
863 |
|
864 cellStart := i |
|
865 |
|
866 for (data[i] != '|' || isBackslashEscaped(data, i)) && data[i] != '\n' { |
|
867 i++ |
|
868 } |
|
869 |
|
870 cellEnd := i |
|
871 |
|
872 // skip the end-of-cell marker, possibly taking us past end of buffer |
|
873 i++ |
|
874 |
|
875 for cellEnd > cellStart && data[cellEnd-1] == ' ' { |
|
876 cellEnd-- |
|
877 } |
|
878 |
|
879 var cellWork bytes.Buffer |
|
880 p.inline(&cellWork, data[cellStart:cellEnd]) |
|
881 |
|
882 if header { |
|
883 p.r.TableHeaderCell(&rowWork, cellWork.Bytes(), columns[col]) |
|
884 } else { |
|
885 p.r.TableCell(&rowWork, cellWork.Bytes(), columns[col]) |
|
886 } |
|
887 } |
|
888 |
|
889 // pad it out with empty columns to get the right number |
|
890 for ; col < len(columns); col++ { |
|
891 if header { |
|
892 p.r.TableHeaderCell(&rowWork, nil, columns[col]) |
|
893 } else { |
|
894 p.r.TableCell(&rowWork, nil, columns[col]) |
|
895 } |
|
896 } |
|
897 |
|
898 // silently ignore rows with too many cells |
|
899 |
|
900 p.r.TableRow(out, rowWork.Bytes()) |
|
901 } |
|
902 |
|
903 // returns blockquote prefix length |
|
904 func (p *parser) quotePrefix(data []byte) int { |
|
905 i := 0 |
|
906 for i < 3 && data[i] == ' ' { |
|
907 i++ |
|
908 } |
|
909 if data[i] == '>' { |
|
910 if data[i+1] == ' ' { |
|
911 return i + 2 |
|
912 } |
|
913 return i + 1 |
|
914 } |
|
915 return 0 |
|
916 } |
|
917 |
|
918 // blockquote ends with at least one blank line |
|
919 // followed by something without a blockquote prefix |
|
920 func (p *parser) terminateBlockquote(data []byte, beg, end int) bool { |
|
921 if p.isEmpty(data[beg:]) <= 0 { |
|
922 return false |
|
923 } |
|
924 if end >= len(data) { |
|
925 return true |
|
926 } |
|
927 return p.quotePrefix(data[end:]) == 0 && p.isEmpty(data[end:]) == 0 |
|
928 } |
|
929 |
|
930 // parse a blockquote fragment |
|
931 func (p *parser) quote(out *bytes.Buffer, data []byte) int { |
|
932 var raw bytes.Buffer |
|
933 beg, end := 0, 0 |
|
934 for beg < len(data) { |
|
935 end = beg |
|
936 // Step over whole lines, collecting them. While doing that, check for |
|
937 // fenced code and if one's found, incorporate it altogether, |
|
938 // irregardless of any contents inside it |
|
939 for data[end] != '\n' { |
|
940 if p.flags&EXTENSION_FENCED_CODE != 0 { |
|
941 if i := p.fencedCodeBlock(out, data[end:], false); i > 0 { |
|
942 // -1 to compensate for the extra end++ after the loop: |
|
943 end += i - 1 |
|
944 break |
|
945 } |
|
946 } |
|
947 end++ |
|
948 } |
|
949 end++ |
|
950 |
|
951 if pre := p.quotePrefix(data[beg:]); pre > 0 { |
|
952 // skip the prefix |
|
953 beg += pre |
|
954 } else if p.terminateBlockquote(data, beg, end) { |
|
955 break |
|
956 } |
|
957 |
|
958 // this line is part of the blockquote |
|
959 raw.Write(data[beg:end]) |
|
960 beg = end |
|
961 } |
|
962 |
|
963 var cooked bytes.Buffer |
|
964 p.block(&cooked, raw.Bytes()) |
|
965 p.r.BlockQuote(out, cooked.Bytes()) |
|
966 return end |
|
967 } |
|
968 |
|
969 // returns prefix length for block code |
|
970 func (p *parser) codePrefix(data []byte) int { |
|
971 if data[0] == ' ' && data[1] == ' ' && data[2] == ' ' && data[3] == ' ' { |
|
972 return 4 |
|
973 } |
|
974 return 0 |
|
975 } |
|
976 |
|
977 func (p *parser) code(out *bytes.Buffer, data []byte) int { |
|
978 var work bytes.Buffer |
|
979 |
|
980 i := 0 |
|
981 for i < len(data) { |
|
982 beg := i |
|
983 for data[i] != '\n' { |
|
984 i++ |
|
985 } |
|
986 i++ |
|
987 |
|
988 blankline := p.isEmpty(data[beg:i]) > 0 |
|
989 if pre := p.codePrefix(data[beg:i]); pre > 0 { |
|
990 beg += pre |
|
991 } else if !blankline { |
|
992 // non-empty, non-prefixed line breaks the pre |
|
993 i = beg |
|
994 break |
|
995 } |
|
996 |
|
997 // verbatim copy to the working buffeu |
|
998 if blankline { |
|
999 work.WriteByte('\n') |
|
1000 } else { |
|
1001 work.Write(data[beg:i]) |
|
1002 } |
|
1003 } |
|
1004 |
|
1005 // trim all the \n off the end of work |
|
1006 workbytes := work.Bytes() |
|
1007 eol := len(workbytes) |
|
1008 for eol > 0 && workbytes[eol-1] == '\n' { |
|
1009 eol-- |
|
1010 } |
|
1011 if eol != len(workbytes) { |
|
1012 work.Truncate(eol) |
|
1013 } |
|
1014 |
|
1015 work.WriteByte('\n') |
|
1016 |
|
1017 p.r.BlockCode(out, work.Bytes(), "") |
|
1018 |
|
1019 return i |
|
1020 } |
|
1021 |
|
1022 // returns unordered list item prefix |
|
1023 func (p *parser) uliPrefix(data []byte) int { |
|
1024 i := 0 |
|
1025 |
|
1026 // start with up to 3 spaces |
|
1027 for i < 3 && data[i] == ' ' { |
|
1028 i++ |
|
1029 } |
|
1030 |
|
1031 // need a *, +, or - followed by a space |
|
1032 if (data[i] != '*' && data[i] != '+' && data[i] != '-') || |
|
1033 data[i+1] != ' ' { |
|
1034 return 0 |
|
1035 } |
|
1036 return i + 2 |
|
1037 } |
|
1038 |
|
1039 // returns ordered list item prefix |
|
1040 func (p *parser) oliPrefix(data []byte) int { |
|
1041 i := 0 |
|
1042 |
|
1043 // start with up to 3 spaces |
|
1044 for i < 3 && data[i] == ' ' { |
|
1045 i++ |
|
1046 } |
|
1047 |
|
1048 // count the digits |
|
1049 start := i |
|
1050 for data[i] >= '0' && data[i] <= '9' { |
|
1051 i++ |
|
1052 } |
|
1053 |
|
1054 // we need >= 1 digits followed by a dot and a space |
|
1055 if start == i || data[i] != '.' || data[i+1] != ' ' { |
|
1056 return 0 |
|
1057 } |
|
1058 return i + 2 |
|
1059 } |
|
1060 |
|
1061 // returns definition list item prefix |
|
1062 func (p *parser) dliPrefix(data []byte) int { |
|
1063 i := 0 |
|
1064 |
|
1065 // need a : followed by a spaces |
|
1066 if data[i] != ':' || data[i+1] != ' ' { |
|
1067 return 0 |
|
1068 } |
|
1069 for data[i] == ' ' { |
|
1070 i++ |
|
1071 } |
|
1072 return i + 2 |
|
1073 } |
|
1074 |
|
1075 // parse ordered or unordered list block |
|
1076 func (p *parser) list(out *bytes.Buffer, data []byte, flags int) int { |
|
1077 i := 0 |
|
1078 flags |= LIST_ITEM_BEGINNING_OF_LIST |
|
1079 work := func() bool { |
|
1080 for i < len(data) { |
|
1081 skip := p.listItem(out, data[i:], &flags) |
|
1082 i += skip |
|
1083 |
|
1084 if skip == 0 || flags&LIST_ITEM_END_OF_LIST != 0 { |
|
1085 break |
|
1086 } |
|
1087 flags &= ^LIST_ITEM_BEGINNING_OF_LIST |
|
1088 } |
|
1089 return true |
|
1090 } |
|
1091 |
|
1092 p.r.List(out, work, flags) |
|
1093 return i |
|
1094 } |
|
1095 |
|
1096 // Parse a single list item. |
|
1097 // Assumes initial prefix is already removed if this is a sublist. |
|
1098 func (p *parser) listItem(out *bytes.Buffer, data []byte, flags *int) int { |
|
1099 // keep track of the indentation of the first line |
|
1100 itemIndent := 0 |
|
1101 for itemIndent < 3 && data[itemIndent] == ' ' { |
|
1102 itemIndent++ |
|
1103 } |
|
1104 |
|
1105 i := p.uliPrefix(data) |
|
1106 if i == 0 { |
|
1107 i = p.oliPrefix(data) |
|
1108 } |
|
1109 if i == 0 { |
|
1110 i = p.dliPrefix(data) |
|
1111 // reset definition term flag |
|
1112 if i > 0 { |
|
1113 *flags &= ^LIST_TYPE_TERM |
|
1114 } |
|
1115 } |
|
1116 if i == 0 { |
|
1117 // if in defnition list, set term flag and continue |
|
1118 if *flags&LIST_TYPE_DEFINITION != 0 { |
|
1119 *flags |= LIST_TYPE_TERM |
|
1120 } else { |
|
1121 return 0 |
|
1122 } |
|
1123 } |
|
1124 |
|
1125 // skip leading whitespace on first line |
|
1126 for data[i] == ' ' { |
|
1127 i++ |
|
1128 } |
|
1129 |
|
1130 // find the end of the line |
|
1131 line := i |
|
1132 for i > 0 && data[i-1] != '\n' { |
|
1133 i++ |
|
1134 } |
|
1135 |
|
1136 // get working buffer |
|
1137 var raw bytes.Buffer |
|
1138 |
|
1139 // put the first line into the working buffer |
|
1140 raw.Write(data[line:i]) |
|
1141 line = i |
|
1142 |
|
1143 // process the following lines |
|
1144 containsBlankLine := false |
|
1145 sublist := 0 |
|
1146 codeBlockMarker := "" |
|
1147 |
|
1148 gatherlines: |
|
1149 for line < len(data) { |
|
1150 i++ |
|
1151 |
|
1152 // find the end of this line |
|
1153 for data[i-1] != '\n' { |
|
1154 i++ |
|
1155 } |
|
1156 |
|
1157 // if it is an empty line, guess that it is part of this item |
|
1158 // and move on to the next line |
|
1159 if p.isEmpty(data[line:i]) > 0 { |
|
1160 containsBlankLine = true |
|
1161 raw.Write(data[line:i]) |
|
1162 line = i |
|
1163 continue |
|
1164 } |
|
1165 |
|
1166 // calculate the indentation |
|
1167 indent := 0 |
|
1168 for indent < 4 && line+indent < i && data[line+indent] == ' ' { |
|
1169 indent++ |
|
1170 } |
|
1171 |
|
1172 chunk := data[line+indent : i] |
|
1173 |
|
1174 if p.flags&EXTENSION_FENCED_CODE != 0 { |
|
1175 // determine if in or out of codeblock |
|
1176 // if in codeblock, ignore normal list processing |
|
1177 _, marker := isFenceLine(chunk, nil, codeBlockMarker, false) |
|
1178 if marker != "" { |
|
1179 if codeBlockMarker == "" { |
|
1180 // start of codeblock |
|
1181 codeBlockMarker = marker |
|
1182 } else { |
|
1183 // end of codeblock. |
|
1184 *flags |= LIST_ITEM_CONTAINS_BLOCK |
|
1185 codeBlockMarker = "" |
|
1186 } |
|
1187 } |
|
1188 // we are in a codeblock, write line, and continue |
|
1189 if codeBlockMarker != "" || marker != "" { |
|
1190 raw.Write(data[line+indent : i]) |
|
1191 line = i |
|
1192 continue gatherlines |
|
1193 } |
|
1194 } |
|
1195 |
|
1196 // evaluate how this line fits in |
|
1197 switch { |
|
1198 // is this a nested list item? |
|
1199 case (p.uliPrefix(chunk) > 0 && !p.isHRule(chunk)) || |
|
1200 p.oliPrefix(chunk) > 0 || |
|
1201 p.dliPrefix(chunk) > 0: |
|
1202 |
|
1203 if containsBlankLine { |
|
1204 // end the list if the type changed after a blank line |
|
1205 if indent <= itemIndent && |
|
1206 ((*flags&LIST_TYPE_ORDERED != 0 && p.uliPrefix(chunk) > 0) || |
|
1207 (*flags&LIST_TYPE_ORDERED == 0 && p.oliPrefix(chunk) > 0)) { |
|
1208 |
|
1209 *flags |= LIST_ITEM_END_OF_LIST |
|
1210 break gatherlines |
|
1211 } |
|
1212 *flags |= LIST_ITEM_CONTAINS_BLOCK |
|
1213 } |
|
1214 |
|
1215 // to be a nested list, it must be indented more |
|
1216 // if not, it is the next item in the same list |
|
1217 if indent <= itemIndent { |
|
1218 break gatherlines |
|
1219 } |
|
1220 |
|
1221 // is this the first item in the nested list? |
|
1222 if sublist == 0 { |
|
1223 sublist = raw.Len() |
|
1224 } |
|
1225 |
|
1226 // is this a nested prefix header? |
|
1227 case p.isPrefixHeader(chunk): |
|
1228 // if the header is not indented, it is not nested in the list |
|
1229 // and thus ends the list |
|
1230 if containsBlankLine && indent < 4 { |
|
1231 *flags |= LIST_ITEM_END_OF_LIST |
|
1232 break gatherlines |
|
1233 } |
|
1234 *flags |= LIST_ITEM_CONTAINS_BLOCK |
|
1235 |
|
1236 // anything following an empty line is only part |
|
1237 // of this item if it is indented 4 spaces |
|
1238 // (regardless of the indentation of the beginning of the item) |
|
1239 case containsBlankLine && indent < 4: |
|
1240 if *flags&LIST_TYPE_DEFINITION != 0 && i < len(data)-1 { |
|
1241 // is the next item still a part of this list? |
|
1242 next := i |
|
1243 for data[next] != '\n' { |
|
1244 next++ |
|
1245 } |
|
1246 for next < len(data)-1 && data[next] == '\n' { |
|
1247 next++ |
|
1248 } |
|
1249 if i < len(data)-1 && data[i] != ':' && data[next] != ':' { |
|
1250 *flags |= LIST_ITEM_END_OF_LIST |
|
1251 } |
|
1252 } else { |
|
1253 *flags |= LIST_ITEM_END_OF_LIST |
|
1254 } |
|
1255 break gatherlines |
|
1256 |
|
1257 // a blank line means this should be parsed as a block |
|
1258 case containsBlankLine: |
|
1259 *flags |= LIST_ITEM_CONTAINS_BLOCK |
|
1260 } |
|
1261 |
|
1262 containsBlankLine = false |
|
1263 |
|
1264 // add the line into the working buffer without prefix |
|
1265 raw.Write(data[line+indent : i]) |
|
1266 |
|
1267 line = i |
|
1268 } |
|
1269 |
|
1270 // If reached end of data, the Renderer.ListItem call we're going to make below |
|
1271 // is definitely the last in the list. |
|
1272 if line >= len(data) { |
|
1273 *flags |= LIST_ITEM_END_OF_LIST |
|
1274 } |
|
1275 |
|
1276 rawBytes := raw.Bytes() |
|
1277 |
|
1278 // render the contents of the list item |
|
1279 var cooked bytes.Buffer |
|
1280 if *flags&LIST_ITEM_CONTAINS_BLOCK != 0 && *flags&LIST_TYPE_TERM == 0 { |
|
1281 // intermediate render of block item, except for definition term |
|
1282 if sublist > 0 { |
|
1283 p.block(&cooked, rawBytes[:sublist]) |
|
1284 p.block(&cooked, rawBytes[sublist:]) |
|
1285 } else { |
|
1286 p.block(&cooked, rawBytes) |
|
1287 } |
|
1288 } else { |
|
1289 // intermediate render of inline item |
|
1290 if sublist > 0 { |
|
1291 p.inline(&cooked, rawBytes[:sublist]) |
|
1292 p.block(&cooked, rawBytes[sublist:]) |
|
1293 } else { |
|
1294 p.inline(&cooked, rawBytes) |
|
1295 } |
|
1296 } |
|
1297 |
|
1298 // render the actual list item |
|
1299 cookedBytes := cooked.Bytes() |
|
1300 parsedEnd := len(cookedBytes) |
|
1301 |
|
1302 // strip trailing newlines |
|
1303 for parsedEnd > 0 && cookedBytes[parsedEnd-1] == '\n' { |
|
1304 parsedEnd-- |
|
1305 } |
|
1306 p.r.ListItem(out, cookedBytes[:parsedEnd], *flags) |
|
1307 |
|
1308 return line |
|
1309 } |
|
1310 |
|
1311 // render a single paragraph that has already been parsed out |
|
1312 func (p *parser) renderParagraph(out *bytes.Buffer, data []byte) { |
|
1313 if len(data) == 0 { |
|
1314 return |
|
1315 } |
|
1316 |
|
1317 // trim leading spaces |
|
1318 beg := 0 |
|
1319 for data[beg] == ' ' { |
|
1320 beg++ |
|
1321 } |
|
1322 |
|
1323 // trim trailing newline |
|
1324 end := len(data) - 1 |
|
1325 |
|
1326 // trim trailing spaces |
|
1327 for end > beg && data[end-1] == ' ' { |
|
1328 end-- |
|
1329 } |
|
1330 |
|
1331 work := func() bool { |
|
1332 p.inline(out, data[beg:end]) |
|
1333 return true |
|
1334 } |
|
1335 p.r.Paragraph(out, work) |
|
1336 } |
|
1337 |
|
1338 func (p *parser) paragraph(out *bytes.Buffer, data []byte) int { |
|
1339 // prev: index of 1st char of previous line |
|
1340 // line: index of 1st char of current line |
|
1341 // i: index of cursor/end of current line |
|
1342 var prev, line, i int |
|
1343 |
|
1344 // keep going until we find something to mark the end of the paragraph |
|
1345 for i < len(data) { |
|
1346 // mark the beginning of the current line |
|
1347 prev = line |
|
1348 current := data[i:] |
|
1349 line = i |
|
1350 |
|
1351 // did we find a blank line marking the end of the paragraph? |
|
1352 if n := p.isEmpty(current); n > 0 { |
|
1353 // did this blank line followed by a definition list item? |
|
1354 if p.flags&EXTENSION_DEFINITION_LISTS != 0 { |
|
1355 if i < len(data)-1 && data[i+1] == ':' { |
|
1356 return p.list(out, data[prev:], LIST_TYPE_DEFINITION) |
|
1357 } |
|
1358 } |
|
1359 |
|
1360 p.renderParagraph(out, data[:i]) |
|
1361 return i + n |
|
1362 } |
|
1363 |
|
1364 // an underline under some text marks a header, so our paragraph ended on prev line |
|
1365 if i > 0 { |
|
1366 if level := p.isUnderlinedHeader(current); level > 0 { |
|
1367 // render the paragraph |
|
1368 p.renderParagraph(out, data[:prev]) |
|
1369 |
|
1370 // ignore leading and trailing whitespace |
|
1371 eol := i - 1 |
|
1372 for prev < eol && data[prev] == ' ' { |
|
1373 prev++ |
|
1374 } |
|
1375 for eol > prev && data[eol-1] == ' ' { |
|
1376 eol-- |
|
1377 } |
|
1378 |
|
1379 // render the header |
|
1380 // this ugly double closure avoids forcing variables onto the heap |
|
1381 work := func(o *bytes.Buffer, pp *parser, d []byte) func() bool { |
|
1382 return func() bool { |
|
1383 pp.inline(o, d) |
|
1384 return true |
|
1385 } |
|
1386 }(out, p, data[prev:eol]) |
|
1387 |
|
1388 id := "" |
|
1389 if p.flags&EXTENSION_AUTO_HEADER_IDS != 0 { |
|
1390 id = SanitizedAnchorName(string(data[prev:eol])) |
|
1391 } |
|
1392 |
|
1393 p.r.Header(out, work, level, id) |
|
1394 |
|
1395 // find the end of the underline |
|
1396 for data[i] != '\n' { |
|
1397 i++ |
|
1398 } |
|
1399 return i |
|
1400 } |
|
1401 } |
|
1402 |
|
1403 // if the next line starts a block of HTML, then the paragraph ends here |
|
1404 if p.flags&EXTENSION_LAX_HTML_BLOCKS != 0 { |
|
1405 if data[i] == '<' && p.html(out, current, false) > 0 { |
|
1406 // rewind to before the HTML block |
|
1407 p.renderParagraph(out, data[:i]) |
|
1408 return i |
|
1409 } |
|
1410 } |
|
1411 |
|
1412 // if there's a prefixed header or a horizontal rule after this, paragraph is over |
|
1413 if p.isPrefixHeader(current) || p.isHRule(current) { |
|
1414 p.renderParagraph(out, data[:i]) |
|
1415 return i |
|
1416 } |
|
1417 |
|
1418 // if there's a fenced code block, paragraph is over |
|
1419 if p.flags&EXTENSION_FENCED_CODE != 0 { |
|
1420 if p.fencedCodeBlock(out, current, false) > 0 { |
|
1421 p.renderParagraph(out, data[:i]) |
|
1422 return i |
|
1423 } |
|
1424 } |
|
1425 |
|
1426 // if there's a definition list item, prev line is a definition term |
|
1427 if p.flags&EXTENSION_DEFINITION_LISTS != 0 { |
|
1428 if p.dliPrefix(current) != 0 { |
|
1429 return p.list(out, data[prev:], LIST_TYPE_DEFINITION) |
|
1430 } |
|
1431 } |
|
1432 |
|
1433 // if there's a list after this, paragraph is over |
|
1434 if p.flags&EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK != 0 { |
|
1435 if p.uliPrefix(current) != 0 || |
|
1436 p.oliPrefix(current) != 0 || |
|
1437 p.quotePrefix(current) != 0 || |
|
1438 p.codePrefix(current) != 0 { |
|
1439 p.renderParagraph(out, data[:i]) |
|
1440 return i |
|
1441 } |
|
1442 } |
|
1443 |
|
1444 // otherwise, scan to the beginning of the next line |
|
1445 for data[i] != '\n' { |
|
1446 i++ |
|
1447 } |
|
1448 i++ |
|
1449 } |
|
1450 |
|
1451 p.renderParagraph(out, data[:i]) |
|
1452 return i |
|
1453 } |
|
1454 |
|
1455 // SanitizedAnchorName returns a sanitized anchor name for the given text. |
|
1456 // |
|
1457 // It implements the algorithm specified in the package comment. |
|
1458 func SanitizedAnchorName(text string) string { |
|
1459 var anchorName []rune |
|
1460 futureDash := false |
|
1461 for _, r := range text { |
|
1462 switch { |
|
1463 case unicode.IsLetter(r) || unicode.IsNumber(r): |
|
1464 if futureDash && len(anchorName) > 0 { |
|
1465 anchorName = append(anchorName, '-') |
|
1466 } |
|
1467 futureDash = false |
|
1468 anchorName = append(anchorName, unicode.ToLower(r)) |
|
1469 default: |
|
1470 futureDash = true |
|
1471 } |
|
1472 } |
|
1473 return string(anchorName) |
|
1474 } |