html.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//
12// HTML rendering backend
13//
14//
15
16package blackfriday
17
18import (
19 "bytes"
20 "fmt"
21 "regexp"
22 "strconv"
23 "strings"
24)
25
26// Html renderer configuration options.
27const (
28 HTML_SKIP_HTML = 1 << iota // skip preformatted HTML blocks
29 HTML_SKIP_STYLE // skip embedded <style> elements
30 HTML_SKIP_IMAGES // skip embedded images
31 HTML_SKIP_LINKS // skip all links
32 HTML_SANITIZE_OUTPUT // strip output of everything that's not known to be safe
33 HTML_SAFELINK // only link to trusted protocols
34 HTML_NOFOLLOW_LINKS // only link with rel="nofollow"
35 HTML_HREF_TARGET_BLANK // add a blank target
36 HTML_TOC // generate a table of contents
37 HTML_OMIT_CONTENTS // skip the main contents (for a standalone table of contents)
38 HTML_COMPLETE_PAGE // generate a complete HTML page
39 HTML_GITHUB_BLOCKCODE // use github fenced code rendering rules
40 HTML_USE_XHTML // generate XHTML output instead of HTML
41 HTML_USE_SMARTYPANTS // enable smart punctuation substitutions
42 HTML_SMARTYPANTS_FRACTIONS // enable smart fractions (with HTML_USE_SMARTYPANTS)
43 HTML_SMARTYPANTS_LATEX_DASHES // enable LaTeX-style dashes (with HTML_USE_SMARTYPANTS)
44 HTML_ABSOLUTE_LINKS // convert all links to absolute links, using AbsolutePrefix
45 HTML_FOOTNOTE_RETURN_LINKS // generate a link at the end of a footnote to return to the source
46)
47
48var (
49 alignments = []string{
50 "left",
51 "right",
52 "center",
53 }
54
55 // TODO: improve this regexp to catch all possible entities:
56 htmlEntity = regexp.MustCompile(`&[a-z]{2,5};`)
57)
58
59type HtmlRendererParameters struct {
60 // Prepend this text to each URL, if the HTML_ABSOLUTE_LINKS option is enabled.
61 AbsolutePrefix string
62 // Add this text to ecah footnote anchor, to ensure uniqueness.
63 FootnoteAnchorPrefix string
64 // Show this text inside the <a> tag for a footnote return link, if the
65 // HTML_FOOTNOTE_RETURN_LINKS flag is enabled. If blank, the string
66 // <sup>[return]</sup> is used.
67 FootnoteReturnLinkContents string
68}
69
70// Html is a type that implements the Renderer interface for HTML output.
71//
72// Do not create this directly, instead use the HtmlRenderer function.
73type Html struct {
74 flags int // HTML_* options
75 closeTag string // how to end singleton tags: either " />\n" or ">\n"
76 title string // document title
77 css string // optional css file url (used with HTML_COMPLETE_PAGE)
78
79 parameters HtmlRendererParameters
80
81 // table of contents data
82 tocMarker int
83 headerCount int
84 currentLevel int
85 toc *bytes.Buffer
86
87 smartypants *smartypantsRenderer
88}
89
90const (
91 xhtmlClose = " />\n"
92 htmlClose = ">\n"
93)
94
95// HtmlRenderer creates and configures an Html object, which
96// satisfies the Renderer interface.
97//
98// flags is a set of HTML_* options ORed together.
99// title is the title of the document, and css is a URL for the document's
100// stylesheet.
101// title and css are only used when HTML_COMPLETE_PAGE is selected.
102func HtmlRenderer(flags int, title string, css string) Renderer {
103 return HtmlRendererWithParameters(flags, title, css, HtmlRendererParameters{})
104}
105
106func HtmlRendererWithParameters(flags int, title string,
107 css string, renderParameters HtmlRendererParameters) Renderer {
108 // configure the rendering engine
109 closeTag := htmlClose
110 if flags&HTML_USE_XHTML != 0 {
111 closeTag = xhtmlClose
112 }
113
114 if renderParameters.FootnoteReturnLinkContents == "" {
115 renderParameters.FootnoteReturnLinkContents = `<sup>[return]</sup>`
116 }
117
118 return &Html{
119 flags: flags,
120 closeTag: closeTag,
121 title: title,
122 css: css,
123 parameters: renderParameters,
124
125 headerCount: 0,
126 currentLevel: 0,
127 toc: new(bytes.Buffer),
128
129 smartypants: smartypants(flags),
130 }
131}
132
133// Using if statements is a bit faster than a switch statement. As the compiler
134// improves, this should be unnecessary this is only worthwhile because
135// attrEscape is the single largest CPU user in normal use.
136// Also tried using map, but that gave a ~3x slowdown.
137func escapeSingleChar(char byte) (string, bool) {
138 if char == '"' {
139 return """, true
140 }
141 if char == '&' {
142 return "&", true
143 }
144 if char == '<' {
145 return "<", true
146 }
147 if char == '>' {
148 return ">", true
149 }
150 return "", false
151}
152
153func attrEscape(out *bytes.Buffer, src []byte) {
154 org := 0
155 for i, ch := range src {
156 if entity, ok := escapeSingleChar(ch); ok {
157 if i > org {
158 // copy all the normal characters since the last escape
159 out.Write(src[org:i])
160 }
161 org = i + 1
162 out.WriteString(entity)
163 }
164 }
165 if org < len(src) {
166 out.Write(src[org:])
167 }
168}
169
170func entityEscapeWithSkip(out *bytes.Buffer, src []byte, skipRanges [][]int) {
171 end := 0
172 for _, rang := range skipRanges {
173 attrEscape(out, src[end:rang[0]])
174 out.Write(src[rang[0]:rang[1]])
175 end = rang[1]
176 }
177 attrEscape(out, src[end:])
178}
179
180func (options *Html) GetFlags() int {
181 return options.flags
182}
183
184func (options *Html) Header(out *bytes.Buffer, text func() bool, level int, id string) {
185 marker := out.Len()
186 doubleSpace(out)
187
188 if id != "" {
189 out.WriteString(fmt.Sprintf("<h%d id=\"%s\">", level, id))
190 } else if options.flags&HTML_TOC != 0 {
191 // headerCount is incremented in htmlTocHeader
192 out.WriteString(fmt.Sprintf("<h%d id=\"toc_%d\">", level, options.headerCount))
193 } else {
194 out.WriteString(fmt.Sprintf("<h%d>", level))
195 }
196
197 tocMarker := out.Len()
198 if !text() {
199 out.Truncate(marker)
200 return
201 }
202
203 // are we building a table of contents?
204 if options.flags&HTML_TOC != 0 {
205 options.TocHeader(out.Bytes()[tocMarker:], level)
206 }
207
208 out.WriteString(fmt.Sprintf("</h%d>\n", level))
209}
210
211func (options *Html) BlockHtml(out *bytes.Buffer, text []byte) {
212 if options.flags&HTML_SKIP_HTML != 0 {
213 return
214 }
215
216 doubleSpace(out)
217 out.Write(text)
218 out.WriteByte('\n')
219}
220
221func (options *Html) HRule(out *bytes.Buffer) {
222 doubleSpace(out)
223 out.WriteString("<hr")
224 out.WriteString(options.closeTag)
225}
226
227func (options *Html) BlockCode(out *bytes.Buffer, text []byte, lang string) {
228 if options.flags&HTML_GITHUB_BLOCKCODE != 0 {
229 options.BlockCodeGithub(out, text, lang)
230 } else {
231 options.BlockCodeNormal(out, text, lang)
232 }
233}
234
235func (options *Html) BlockCodeNormal(out *bytes.Buffer, text []byte, lang string) {
236 doubleSpace(out)
237
238 // parse out the language names/classes
239 count := 0
240 for _, elt := range strings.Fields(lang) {
241 if elt[0] == '.' {
242 elt = elt[1:]
243 }
244 if len(elt) == 0 {
245 continue
246 }
247 if count == 0 {
248 out.WriteString("<pre><code class=\"")
249 } else {
250 out.WriteByte(' ')
251 }
252 attrEscape(out, []byte(elt))
253 count++
254 }
255
256 if count == 0 {
257 out.WriteString("<pre><code>")
258 } else {
259 out.WriteString("\">")
260 }
261
262 attrEscape(out, text)
263 out.WriteString("</code></pre>\n")
264}
265
266// GitHub style code block:
267//
268// <pre lang="LANG"><code>
269// ...
270// </code></pre>
271//
272// Unlike other parsers, we store the language identifier in the <pre>,
273// and don't let the user generate custom classes.
274//
275// The language identifier in the <pre> block gets postprocessed and all
276// the code inside gets syntax highlighted with Pygments. This is much safer
277// than letting the user specify a CSS class for highlighting.
278//
279// Note that we only generate HTML for the first specifier.
280// E.g.
281// ~~~~ {.python .numbered} => <pre lang="python"><code>
282func (options *Html) BlockCodeGithub(out *bytes.Buffer, text []byte, lang string) {
283 doubleSpace(out)
284
285 // parse out the language name
286 count := 0
287 for _, elt := range strings.Fields(lang) {
288 if elt[0] == '.' {
289 elt = elt[1:]
290 }
291 if len(elt) == 0 {
292 continue
293 }
294 out.WriteString("<pre lang=\"")
295 attrEscape(out, []byte(elt))
296 out.WriteString("\"><code>")
297 count++
298 break
299 }
300
301 if count == 0 {
302 out.WriteString("<pre><code>")
303 }
304
305 attrEscape(out, text)
306 out.WriteString("</code></pre>\n")
307}
308
309func (options *Html) BlockQuote(out *bytes.Buffer, text []byte) {
310 doubleSpace(out)
311 out.WriteString("<blockquote>\n")
312 out.Write(text)
313 out.WriteString("</blockquote>\n")
314}
315
316func (options *Html) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) {
317 doubleSpace(out)
318 out.WriteString("<table>\n<thead>\n")
319 out.Write(header)
320 out.WriteString("</thead>\n\n<tbody>\n")
321 out.Write(body)
322 out.WriteString("</tbody>\n</table>\n")
323}
324
325func (options *Html) TableRow(out *bytes.Buffer, text []byte) {
326 doubleSpace(out)
327 out.WriteString("<tr>\n")
328 out.Write(text)
329 out.WriteString("\n</tr>\n")
330}
331
332func (options *Html) TableHeaderCell(out *bytes.Buffer, text []byte, align int) {
333 doubleSpace(out)
334 switch align {
335 case TABLE_ALIGNMENT_LEFT:
336 out.WriteString("<th align=\"left\">")
337 case TABLE_ALIGNMENT_RIGHT:
338 out.WriteString("<th align=\"right\">")
339 case TABLE_ALIGNMENT_CENTER:
340 out.WriteString("<th align=\"center\">")
341 default:
342 out.WriteString("<th>")
343 }
344
345 out.Write(text)
346 out.WriteString("</th>")
347}
348
349func (options *Html) TableCell(out *bytes.Buffer, text []byte, align int) {
350 doubleSpace(out)
351 switch align {
352 case TABLE_ALIGNMENT_LEFT:
353 out.WriteString("<td align=\"left\">")
354 case TABLE_ALIGNMENT_RIGHT:
355 out.WriteString("<td align=\"right\">")
356 case TABLE_ALIGNMENT_CENTER:
357 out.WriteString("<td align=\"center\">")
358 default:
359 out.WriteString("<td>")
360 }
361
362 out.Write(text)
363 out.WriteString("</td>")
364}
365
366func (options *Html) Footnotes(out *bytes.Buffer, text func() bool) {
367 out.WriteString("<div class=\"footnotes\">\n")
368 options.HRule(out)
369 options.List(out, text, LIST_TYPE_ORDERED)
370 out.WriteString("</div>\n")
371}
372
373func (options *Html) FootnoteItem(out *bytes.Buffer, name, text []byte, flags int) {
374 if flags&LIST_ITEM_CONTAINS_BLOCK != 0 || flags&LIST_ITEM_BEGINNING_OF_LIST != 0 {
375 doubleSpace(out)
376 }
377 slug := slugify(name)
378 out.WriteString(`<li id="`)
379 out.WriteString(`fn:`)
380 out.WriteString(options.parameters.FootnoteAnchorPrefix)
381 out.Write(slug)
382 out.WriteString(`">`)
383 out.Write(text)
384 if options.flags&HTML_FOOTNOTE_RETURN_LINKS != 0 {
385 out.WriteString(` <a class="footnote-return" href="#`)
386 out.WriteString(`fnref:`)
387 out.WriteString(options.parameters.FootnoteAnchorPrefix)
388 out.Write(slug)
389 out.WriteString(`">`)
390 out.WriteString(options.parameters.FootnoteReturnLinkContents)
391 out.WriteString(`</a>`)
392 }
393 out.WriteString("</li>\n")
394}
395
396func (options *Html) List(out *bytes.Buffer, text func() bool, flags int) {
397 marker := out.Len()
398 doubleSpace(out)
399
400 if flags&LIST_TYPE_ORDERED != 0 {
401 out.WriteString("<ol>")
402 } else {
403 out.WriteString("<ul>")
404 }
405 if !text() {
406 out.Truncate(marker)
407 return
408 }
409 if flags&LIST_TYPE_ORDERED != 0 {
410 out.WriteString("</ol>\n")
411 } else {
412 out.WriteString("</ul>\n")
413 }
414}
415
416func (options *Html) ListItem(out *bytes.Buffer, text []byte, flags int) {
417 if flags&LIST_ITEM_CONTAINS_BLOCK != 0 || flags&LIST_ITEM_BEGINNING_OF_LIST != 0 {
418 doubleSpace(out)
419 }
420 out.WriteString("<li>")
421 out.Write(text)
422 out.WriteString("</li>\n")
423}
424
425func (options *Html) Paragraph(out *bytes.Buffer, text func() bool) {
426 marker := out.Len()
427 doubleSpace(out)
428
429 out.WriteString("<p>")
430 if !text() {
431 out.Truncate(marker)
432 return
433 }
434 out.WriteString("</p>\n")
435}
436
437func (options *Html) AutoLink(out *bytes.Buffer, link []byte, kind int) {
438 skipRanges := htmlEntity.FindAllIndex(link, -1)
439 if options.flags&HTML_SAFELINK != 0 && !isSafeLink(link) && kind != LINK_TYPE_EMAIL {
440 // mark it but don't link it if it is not a safe link: no smartypants
441 out.WriteString("<tt>")
442 entityEscapeWithSkip(out, link, skipRanges)
443 out.WriteString("</tt>")
444 return
445 }
446
447 out.WriteString("<a href=\"")
448 if kind == LINK_TYPE_EMAIL {
449 out.WriteString("mailto:")
450 } else {
451 options.maybeWriteAbsolutePrefix(out, link)
452 }
453
454 entityEscapeWithSkip(out, link, skipRanges)
455
456 if options.flags&HTML_NOFOLLOW_LINKS != 0 && !isRelativeLink(link) {
457 out.WriteString("\" rel=\"nofollow")
458 }
459 // blank target only add to external link
460 if options.flags&HTML_HREF_TARGET_BLANK != 0 && !isRelativeLink(link) {
461 out.WriteString("\" target=\"_blank")
462 }
463
464 out.WriteString("\">")
465
466 // Pretty print: if we get an email address as
467 // an actual URI, e.g. `mailto:foo@bar.com`, we don't
468 // want to print the `mailto:` prefix
469 switch {
470 case bytes.HasPrefix(link, []byte("mailto://")):
471 attrEscape(out, link[len("mailto://"):])
472 case bytes.HasPrefix(link, []byte("mailto:")):
473 attrEscape(out, link[len("mailto:"):])
474 default:
475 entityEscapeWithSkip(out, link, skipRanges)
476 }
477
478 out.WriteString("</a>")
479}
480
481func (options *Html) CodeSpan(out *bytes.Buffer, text []byte) {
482 out.WriteString("<code>")
483 attrEscape(out, text)
484 out.WriteString("</code>")
485}
486
487func (options *Html) DoubleEmphasis(out *bytes.Buffer, text []byte) {
488 out.WriteString("<strong>")
489 out.Write(text)
490 out.WriteString("</strong>")
491}
492
493func (options *Html) Emphasis(out *bytes.Buffer, text []byte) {
494 if len(text) == 0 {
495 return
496 }
497 out.WriteString("<em>")
498 out.Write(text)
499 out.WriteString("</em>")
500}
501
502func (options *Html) maybeWriteAbsolutePrefix(out *bytes.Buffer, link []byte) {
503 if options.flags&HTML_ABSOLUTE_LINKS != 0 && isRelativeLink(link) {
504 out.WriteString(options.parameters.AbsolutePrefix)
505 if link[0] != '/' {
506 out.WriteByte('/')
507 }
508 }
509}
510
511func (options *Html) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
512 if options.flags&HTML_SKIP_IMAGES != 0 {
513 return
514 }
515
516 out.WriteString("<img src=\"")
517 options.maybeWriteAbsolutePrefix(out, link)
518 attrEscape(out, link)
519 out.WriteString("\" alt=\"")
520 if len(alt) > 0 {
521 attrEscape(out, alt)
522 }
523 if len(title) > 0 {
524 out.WriteString("\" title=\"")
525 attrEscape(out, title)
526 }
527
528 out.WriteByte('"')
529 out.WriteString(options.closeTag)
530 return
531}
532
533func (options *Html) LineBreak(out *bytes.Buffer) {
534 out.WriteString("<br")
535 out.WriteString(options.closeTag)
536}
537
538func (options *Html) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
539 if options.flags&HTML_SKIP_LINKS != 0 {
540 // write the link text out but don't link it, just mark it with typewriter font
541 out.WriteString("<tt>")
542 attrEscape(out, content)
543 out.WriteString("</tt>")
544 return
545 }
546
547 if options.flags&HTML_SAFELINK != 0 && !isSafeLink(link) {
548 // write the link text out but don't link it, just mark it with typewriter font
549 out.WriteString("<tt>")
550 attrEscape(out, content)
551 out.WriteString("</tt>")
552 return
553 }
554
555 out.WriteString("<a href=\"")
556 options.maybeWriteAbsolutePrefix(out, link)
557 attrEscape(out, link)
558 if len(title) > 0 {
559 out.WriteString("\" title=\"")
560 attrEscape(out, title)
561 }
562 if options.flags&HTML_NOFOLLOW_LINKS != 0 && !isRelativeLink(link) {
563 out.WriteString("\" rel=\"nofollow")
564 }
565 // blank target only add to external link
566 if options.flags&HTML_HREF_TARGET_BLANK != 0 && !isRelativeLink(link) {
567 out.WriteString("\" target=\"_blank")
568 }
569
570 out.WriteString("\">")
571 out.Write(content)
572 out.WriteString("</a>")
573 return
574}
575
576func (options *Html) RawHtmlTag(out *bytes.Buffer, text []byte) {
577 if options.flags&HTML_SKIP_HTML != 0 {
578 return
579 }
580 if options.flags&HTML_SKIP_STYLE != 0 && isHtmlTag(text, "style") {
581 return
582 }
583 if options.flags&HTML_SKIP_LINKS != 0 && isHtmlTag(text, "a") {
584 return
585 }
586 if options.flags&HTML_SKIP_IMAGES != 0 && isHtmlTag(text, "img") {
587 return
588 }
589 out.Write(text)
590}
591
592func (options *Html) TripleEmphasis(out *bytes.Buffer, text []byte) {
593 out.WriteString("<strong><em>")
594 out.Write(text)
595 out.WriteString("</em></strong>")
596}
597
598func (options *Html) StrikeThrough(out *bytes.Buffer, text []byte) {
599 out.WriteString("<del>")
600 out.Write(text)
601 out.WriteString("</del>")
602}
603
604func (options *Html) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {
605 slug := slugify(ref)
606 out.WriteString(`<sup class="footnote-ref" id="`)
607 out.WriteString(`fnref:`)
608 out.WriteString(options.parameters.FootnoteAnchorPrefix)
609 out.Write(slug)
610 out.WriteString(`"><a rel="footnote" href="#`)
611 out.WriteString(`fn:`)
612 out.WriteString(options.parameters.FootnoteAnchorPrefix)
613 out.Write(slug)
614 out.WriteString(`">`)
615 out.WriteString(strconv.Itoa(id))
616 out.WriteString(`</a></sup>`)
617}
618
619func (options *Html) Entity(out *bytes.Buffer, entity []byte) {
620 out.Write(entity)
621}
622
623func (options *Html) NormalText(out *bytes.Buffer, text []byte) {
624 if options.flags&HTML_USE_SMARTYPANTS != 0 {
625 options.Smartypants(out, text)
626 } else {
627 attrEscape(out, text)
628 }
629}
630
631func (options *Html) Smartypants(out *bytes.Buffer, text []byte) {
632 smrt := smartypantsData{false, false}
633
634 // first do normal entity escaping
635 var escaped bytes.Buffer
636 attrEscape(&escaped, text)
637 text = escaped.Bytes()
638
639 mark := 0
640 for i := 0; i < len(text); i++ {
641 if action := options.smartypants[text[i]]; action != nil {
642 if i > mark {
643 out.Write(text[mark:i])
644 }
645
646 previousChar := byte(0)
647 if i > 0 {
648 previousChar = text[i-1]
649 }
650 i += action(out, &smrt, previousChar, text[i:])
651 mark = i + 1
652 }
653 }
654
655 if mark < len(text) {
656 out.Write(text[mark:])
657 }
658}
659
660func (options *Html) DocumentHeader(out *bytes.Buffer) {
661 if options.flags&HTML_COMPLETE_PAGE == 0 {
662 return
663 }
664
665 ending := ""
666 if options.flags&HTML_USE_XHTML != 0 {
667 out.WriteString("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" ")
668 out.WriteString("\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n")
669 out.WriteString("<html xmlns=\"http://www.w3.org/1999/xhtml\">\n")
670 ending = " /"
671 } else {
672 out.WriteString("<!DOCTYPE html>\n")
673 out.WriteString("<html>\n")
674 }
675 out.WriteString("<head>\n")
676 out.WriteString(" <title>")
677 options.NormalText(out, []byte(options.title))
678 out.WriteString("</title>\n")
679 out.WriteString(" <meta name=\"GENERATOR\" content=\"Blackfriday Markdown Processor v")
680 out.WriteString(VERSION)
681 out.WriteString("\"")
682 out.WriteString(ending)
683 out.WriteString(">\n")
684 out.WriteString(" <meta charset=\"utf-8\"")
685 out.WriteString(ending)
686 out.WriteString(">\n")
687 if options.css != "" {
688 out.WriteString(" <link rel=\"stylesheet\" type=\"text/css\" href=\"")
689 attrEscape(out, []byte(options.css))
690 out.WriteString("\"")
691 out.WriteString(ending)
692 out.WriteString(">\n")
693 }
694 out.WriteString("</head>\n")
695 out.WriteString("<body>\n")
696
697 options.tocMarker = out.Len()
698}
699
700func (options *Html) DocumentFooter(out *bytes.Buffer) {
701 // finalize and insert the table of contents
702 if options.flags&HTML_TOC != 0 {
703 options.TocFinalize()
704
705 // now we have to insert the table of contents into the document
706 var temp bytes.Buffer
707
708 // start by making a copy of everything after the document header
709 temp.Write(out.Bytes()[options.tocMarker:])
710
711 // now clear the copied material from the main output buffer
712 out.Truncate(options.tocMarker)
713
714 // corner case spacing issue
715 if options.flags&HTML_COMPLETE_PAGE != 0 {
716 out.WriteByte('\n')
717 }
718
719 // insert the table of contents
720 out.WriteString("<nav>\n")
721 out.Write(options.toc.Bytes())
722 out.WriteString("</nav>\n")
723
724 // corner case spacing issue
725 if options.flags&HTML_COMPLETE_PAGE == 0 && options.flags&HTML_OMIT_CONTENTS == 0 {
726 out.WriteByte('\n')
727 }
728
729 // write out everything that came after it
730 if options.flags&HTML_OMIT_CONTENTS == 0 {
731 out.Write(temp.Bytes())
732 }
733 }
734
735 if options.flags&HTML_COMPLETE_PAGE != 0 {
736 out.WriteString("\n</body>\n")
737 out.WriteString("</html>\n")
738 }
739
740}
741
742func (options *Html) TocHeader(text []byte, level int) {
743 for level > options.currentLevel {
744 switch {
745 case bytes.HasSuffix(options.toc.Bytes(), []byte("</li>\n")):
746 // this sublist can nest underneath a header
747 size := options.toc.Len()
748 options.toc.Truncate(size - len("</li>\n"))
749
750 case options.currentLevel > 0:
751 options.toc.WriteString("<li>")
752 }
753 if options.toc.Len() > 0 {
754 options.toc.WriteByte('\n')
755 }
756 options.toc.WriteString("<ul>\n")
757 options.currentLevel++
758 }
759
760 for level < options.currentLevel {
761 options.toc.WriteString("</ul>")
762 if options.currentLevel > 1 {
763 options.toc.WriteString("</li>\n")
764 }
765 options.currentLevel--
766 }
767
768 options.toc.WriteString("<li><a href=\"#toc_")
769 options.toc.WriteString(strconv.Itoa(options.headerCount))
770 options.toc.WriteString("\">")
771 options.headerCount++
772
773 options.toc.Write(text)
774
775 options.toc.WriteString("</a></li>\n")
776}
777
778func (options *Html) TocFinalize() {
779 for options.currentLevel > 1 {
780 options.toc.WriteString("</ul></li>\n")
781 options.currentLevel--
782 }
783
784 if options.currentLevel > 0 {
785 options.toc.WriteString("</ul>\n")
786 }
787}
788
789func isHtmlTag(tag []byte, tagname string) bool {
790 found, _ := findHtmlTagPos(tag, tagname)
791 return found
792}
793
794// Look for a character, but ignore it when it's in any kind of quotes, it
795// might be JavaScript
796func skipUntilCharIgnoreQuotes(html []byte, start int, char byte) int {
797 inSingleQuote := false
798 inDoubleQuote := false
799 inGraveQuote := false
800 i := start
801 for i < len(html) {
802 switch {
803 case html[i] == char && !inSingleQuote && !inDoubleQuote && !inGraveQuote:
804 return i
805 case html[i] == '\'':
806 inSingleQuote = !inSingleQuote
807 case html[i] == '"':
808 inDoubleQuote = !inDoubleQuote
809 case html[i] == '`':
810 inGraveQuote = !inGraveQuote
811 }
812 i++
813 }
814 return start
815}
816
817func findHtmlTagPos(tag []byte, tagname string) (bool, int) {
818 i := 0
819 if i < len(tag) && tag[0] != '<' {
820 return false, -1
821 }
822 i++
823 i = skipSpace(tag, i)
824
825 if i < len(tag) && tag[i] == '/' {
826 i++
827 }
828
829 i = skipSpace(tag, i)
830 j := 0
831 for ; i < len(tag); i, j = i+1, j+1 {
832 if j >= len(tagname) {
833 break
834 }
835
836 if strings.ToLower(string(tag[i]))[0] != tagname[j] {
837 return false, -1
838 }
839 }
840
841 if i == len(tag) {
842 return false, -1
843 }
844
845 rightAngle := skipUntilCharIgnoreQuotes(tag, i, '>')
846 if rightAngle > i {
847 return true, rightAngle
848 }
849
850 return false, -1
851}
852
853func skipUntilChar(text []byte, start int, char byte) int {
854 i := start
855 for i < len(text) && text[i] != char {
856 i++
857 }
858 return i
859}
860
861func skipSpace(tag []byte, i int) int {
862 for i < len(tag) && isspace(tag[i]) {
863 i++
864 }
865 return i
866}
867
868func doubleSpace(out *bytes.Buffer) {
869 if out.Len() > 0 {
870 out.WriteByte('\n')
871 }
872}
873
874func isRelativeLink(link []byte) (yes bool) {
875 yes = false
876
877 // a tag begin with '#'
878 if link[0] == '#' {
879 yes = true
880 }
881
882 // link begin with '/' but not '//', the second maybe a protocol relative link
883 if len(link) >= 2 && link[0] == '/' && link[1] != '/' {
884 yes = true
885 }
886
887 // only the root '/'
888 if len(link) == 1 && link[0] == '/' {
889 yes = true
890 }
891 return
892}