all repos — grayfriday @ c7617b05ad8da2825e1b2d311622bf3472101624

blackfriday fork with a few changes

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