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