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