all repos — grayfriday @ c9977f0c0bc6d08e7537bffcf0d2dbd622fba98d

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