all repos — grayfriday @ 7ec50399c3d71d9fddbafa5f0d7851602c37fa0f

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