2011-05-29 05:17:53 +02:00
|
|
|
//
|
2011-06-28 04:11:32 +02:00
|
|
|
// Blackfriday Markdown Processor
|
|
|
|
// Available at http://github.com/russross/blackfriday
|
|
|
|
//
|
|
|
|
// Copyright © 2011 Russ Ross <russ@russross.com>.
|
2011-06-28 19:30:10 +02:00
|
|
|
// Distributed under the Simplified BSD License.
|
2011-06-28 04:11:32 +02:00
|
|
|
// See README.md for details.
|
2011-05-29 05:17:53 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// HTML rendering backend
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
package blackfriday
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2014-01-22 00:14:35 +01:00
|
|
|
"regexp"
|
2011-05-29 05:17:53 +02:00
|
|
|
"strconv"
|
2011-06-29 23:38:35 +02:00
|
|
|
"strings"
|
2011-05-29 05:17:53 +02:00
|
|
|
)
|
|
|
|
|
2011-07-07 20:05:29 +02:00
|
|
|
// Html renderer configuration options.
|
2011-05-29 05:17:53 +02:00
|
|
|
const (
|
2011-07-07 20:05:29 +02:00
|
|
|
HTML_SKIP_HTML = 1 << iota // skip preformatted HTML blocks
|
|
|
|
HTML_SKIP_STYLE // skip embedded <style> elements
|
|
|
|
HTML_SKIP_IMAGES // skip embedded images
|
|
|
|
HTML_SKIP_LINKS // skip all links
|
2014-01-22 00:23:43 +01:00
|
|
|
HTML_SANITIZE_OUTPUT // strip output of everything that's not known to be safe
|
2011-07-07 20:05:29 +02:00
|
|
|
HTML_SAFELINK // only link to trusted protocols
|
2014-02-25 15:21:57 +01:00
|
|
|
HTML_NOFOLLOW_LINKS // only link with rel="nofollow"
|
2014-03-21 03:52:46 +01:00
|
|
|
HTML_HREF_TARGET_BLANK // add a blank target
|
2011-07-07 20:05:29 +02:00
|
|
|
HTML_TOC // generate a table of contents
|
|
|
|
HTML_OMIT_CONTENTS // skip the main contents (for a standalone table of contents)
|
|
|
|
HTML_COMPLETE_PAGE // generate a complete HTML page
|
|
|
|
HTML_GITHUB_BLOCKCODE // use github fenced code rendering rules
|
|
|
|
HTML_USE_XHTML // generate XHTML output instead of HTML
|
|
|
|
HTML_USE_SMARTYPANTS // enable smart punctuation substitutions
|
|
|
|
HTML_SMARTYPANTS_FRACTIONS // enable smart fractions (with HTML_USE_SMARTYPANTS)
|
|
|
|
HTML_SMARTYPANTS_LATEX_DASHES // enable LaTeX-style dashes (with HTML_USE_SMARTYPANTS)
|
2014-05-24 20:29:39 +02:00
|
|
|
HTML_FOOTNOTE_RETURN_LINKS // generate a link at the end of a footnote to return to the source
|
2011-05-29 05:17:53 +02:00
|
|
|
)
|
|
|
|
|
2014-01-22 00:14:35 +01:00
|
|
|
var (
|
2014-04-19 23:59:04 +02:00
|
|
|
alignments = []string{
|
|
|
|
"left",
|
|
|
|
"right",
|
|
|
|
"center",
|
|
|
|
}
|
|
|
|
|
2014-01-26 20:39:38 +01:00
|
|
|
// TODO: improve this regexp to catch all possible entities:
|
|
|
|
htmlEntity = regexp.MustCompile(`&[a-z]{2,5};`)
|
2014-01-22 00:14:35 +01:00
|
|
|
)
|
|
|
|
|
2014-05-24 09:55:13 +02:00
|
|
|
type HtmlRendererParameters struct {
|
2014-05-29 16:17:20 +02:00
|
|
|
// Prepend this text to each relative URL.
|
2014-05-24 09:55:13 +02:00
|
|
|
AbsolutePrefix string
|
2014-05-29 06:52:45 +02:00
|
|
|
// Add this text to each footnote anchor, to ensure uniqueness.
|
2014-05-24 20:29:39 +02:00
|
|
|
FootnoteAnchorPrefix string
|
|
|
|
// Show this text inside the <a> tag for a footnote return link, if the
|
|
|
|
// HTML_FOOTNOTE_RETURN_LINKS flag is enabled. If blank, the string
|
|
|
|
// <sup>[return]</sup> is used.
|
|
|
|
FootnoteReturnLinkContents string
|
2014-05-24 09:55:13 +02:00
|
|
|
}
|
|
|
|
|
2011-07-07 19:56:45 +02:00
|
|
|
// Html is a type that implements the Renderer interface for HTML output.
|
|
|
|
//
|
|
|
|
// Do not create this directly, instead use the HtmlRenderer function.
|
2011-06-29 19:13:17 +02:00
|
|
|
type Html struct {
|
2014-05-24 09:55:13 +02:00
|
|
|
flags int // HTML_* options
|
|
|
|
closeTag string // how to end singleton tags: either " />\n" or ">\n"
|
|
|
|
title string // document title
|
|
|
|
css string // optional css file url (used with HTML_COMPLETE_PAGE)
|
|
|
|
|
|
|
|
parameters HtmlRendererParameters
|
2011-06-29 18:08:56 +02:00
|
|
|
|
|
|
|
// table of contents data
|
2011-06-29 18:36:56 +02:00
|
|
|
tocMarker int
|
2011-06-29 18:08:56 +02:00
|
|
|
headerCount int
|
|
|
|
currentLevel int
|
|
|
|
toc *bytes.Buffer
|
|
|
|
|
2011-07-07 19:56:45 +02:00
|
|
|
smartypants *smartypantsRenderer
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
const (
|
|
|
|
xhtmlClose = " />\n"
|
|
|
|
htmlClose = ">\n"
|
|
|
|
)
|
2011-05-29 05:17:53 +02:00
|
|
|
|
2011-07-07 19:56:45 +02:00
|
|
|
// HtmlRenderer creates and configures an Html object, which
|
|
|
|
// satisfies the Renderer interface.
|
|
|
|
//
|
|
|
|
// flags is a set of HTML_* options ORed together.
|
|
|
|
// title is the title of the document, and css is a URL for the document's
|
|
|
|
// stylesheet.
|
|
|
|
// title and css are only used when HTML_COMPLETE_PAGE is selected.
|
2014-05-24 09:55:13 +02:00
|
|
|
func HtmlRenderer(flags int, title string, css string) Renderer {
|
|
|
|
return HtmlRendererWithParameters(flags, title, css, HtmlRendererParameters{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func HtmlRendererWithParameters(flags int, title string,
|
|
|
|
css string, renderParameters HtmlRendererParameters) Renderer {
|
2011-05-29 05:17:53 +02:00
|
|
|
// configure the rendering engine
|
2011-05-31 05:44:52 +02:00
|
|
|
closeTag := htmlClose
|
2011-05-29 05:17:53 +02:00
|
|
|
if flags&HTML_USE_XHTML != 0 {
|
2011-05-31 05:44:52 +02:00
|
|
|
closeTag = xhtmlClose
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2014-05-24 20:29:39 +02:00
|
|
|
if renderParameters.FootnoteReturnLinkContents == "" {
|
|
|
|
renderParameters.FootnoteReturnLinkContents = `<sup>[return]</sup>`
|
|
|
|
}
|
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
return &Html{
|
2014-05-24 09:55:13 +02:00
|
|
|
flags: flags,
|
|
|
|
closeTag: closeTag,
|
|
|
|
title: title,
|
|
|
|
css: css,
|
|
|
|
parameters: renderParameters,
|
2011-05-29 05:17:53 +02:00
|
|
|
|
2011-06-29 18:08:56 +02:00
|
|
|
headerCount: 0,
|
|
|
|
currentLevel: 0,
|
2011-06-29 18:36:56 +02:00
|
|
|
toc: new(bytes.Buffer),
|
2011-05-29 05:17:53 +02:00
|
|
|
|
2011-07-07 19:56:45 +02:00
|
|
|
smartypants: smartypants(flags),
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-26 20:27:34 +01:00
|
|
|
// Using if statements is a bit faster than a switch statement. As the compiler
|
|
|
|
// improves, this should be unnecessary this is only worthwhile because
|
|
|
|
// attrEscape is the single largest CPU user in normal use.
|
|
|
|
// Also tried using map, but that gave a ~3x slowdown.
|
|
|
|
func escapeSingleChar(char byte) (string, bool) {
|
|
|
|
if char == '"' {
|
|
|
|
return """, true
|
|
|
|
}
|
|
|
|
if char == '&' {
|
|
|
|
return "&", true
|
|
|
|
}
|
|
|
|
if char == '<' {
|
|
|
|
return "<", true
|
|
|
|
}
|
|
|
|
if char == '>' {
|
|
|
|
return ">", true
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2011-05-30 19:06:20 +02:00
|
|
|
func attrEscape(out *bytes.Buffer, src []byte) {
|
2011-06-25 03:11:06 +02:00
|
|
|
org := 0
|
|
|
|
for i, ch := range src {
|
2014-01-26 20:27:34 +01:00
|
|
|
if entity, ok := escapeSingleChar(ch); ok {
|
2011-06-25 03:11:06 +02:00
|
|
|
if i > org {
|
|
|
|
// copy all the normal characters since the last escape
|
|
|
|
out.Write(src[org:i])
|
|
|
|
}
|
|
|
|
org = i + 1
|
2014-01-26 20:27:34 +01:00
|
|
|
out.WriteString(entity)
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
}
|
2011-06-25 03:11:06 +02:00
|
|
|
if org < len(src) {
|
|
|
|
out.Write(src[org:])
|
|
|
|
}
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2014-01-26 20:39:38 +01:00
|
|
|
func entityEscapeWithSkip(out *bytes.Buffer, src []byte, skipRanges [][]int) {
|
|
|
|
end := 0
|
|
|
|
for _, rang := range skipRanges {
|
|
|
|
attrEscape(out, src[end:rang[0]])
|
|
|
|
out.Write(src[rang[0]:rang[1]])
|
|
|
|
end = rang[1]
|
|
|
|
}
|
|
|
|
attrEscape(out, src[end:])
|
|
|
|
}
|
|
|
|
|
2014-01-22 00:14:35 +01:00
|
|
|
func (options *Html) GetFlags() int {
|
|
|
|
return options.flags
|
|
|
|
}
|
|
|
|
|
2014-08-02 04:54:21 +02:00
|
|
|
func (options *Html) TitleBlock(out *bytes.Buffer, text []byte) {
|
|
|
|
text = bytes.TrimPrefix(text, []byte("% "))
|
|
|
|
text = bytes.Replace(text, []byte("\n% "), []byte("\n"), -1)
|
|
|
|
out.WriteString("<h1 class=\"title\">")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("\n</h1>")
|
|
|
|
}
|
|
|
|
|
2014-04-05 21:42:58 +02:00
|
|
|
func (options *Html) Header(out *bytes.Buffer, text func() bool, level int, id string) {
|
2011-06-25 16:20:08 +02:00
|
|
|
marker := out.Len()
|
2011-06-29 23:38:35 +02:00
|
|
|
doubleSpace(out)
|
2011-05-29 05:17:53 +02:00
|
|
|
|
2014-04-05 21:42:58 +02:00
|
|
|
if id != "" {
|
|
|
|
out.WriteString(fmt.Sprintf("<h%d id=\"%s\">", level, id))
|
|
|
|
} else if options.flags&HTML_TOC != 0 {
|
2011-06-29 18:36:56 +02:00
|
|
|
// headerCount is incremented in htmlTocHeader
|
2011-06-29 18:08:56 +02:00
|
|
|
out.WriteString(fmt.Sprintf("<h%d id=\"toc_%d\">", level, options.headerCount))
|
2011-05-29 05:17:53 +02:00
|
|
|
} else {
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString(fmt.Sprintf("<h%d>", level))
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 18:36:56 +02:00
|
|
|
tocMarker := out.Len()
|
2011-06-25 16:20:08 +02:00
|
|
|
if !text() {
|
|
|
|
out.Truncate(marker)
|
|
|
|
return
|
|
|
|
}
|
2011-06-29 18:08:56 +02:00
|
|
|
|
|
|
|
// are we building a table of contents?
|
|
|
|
if options.flags&HTML_TOC != 0 {
|
2011-06-29 19:13:17 +02:00
|
|
|
options.TocHeader(out.Bytes()[tocMarker:], level)
|
2011-06-29 18:08:56 +02:00
|
|
|
}
|
|
|
|
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString(fmt.Sprintf("</h%d>\n", level))
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
func (options *Html) BlockHtml(out *bytes.Buffer, text []byte) {
|
2011-06-29 03:46:35 +02:00
|
|
|
if options.flags&HTML_SKIP_HTML != 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-06-29 23:38:35 +02:00
|
|
|
doubleSpace(out)
|
2014-01-22 00:14:35 +01:00
|
|
|
out.Write(text)
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteByte('\n')
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
func (options *Html) HRule(out *bytes.Buffer) {
|
2011-06-29 23:38:35 +02:00
|
|
|
doubleSpace(out)
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("<hr")
|
2011-05-31 05:44:52 +02:00
|
|
|
out.WriteString(options.closeTag)
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
func (options *Html) BlockCode(out *bytes.Buffer, text []byte, lang string) {
|
2011-06-29 03:46:35 +02:00
|
|
|
if options.flags&HTML_GITHUB_BLOCKCODE != 0 {
|
2011-06-29 19:13:17 +02:00
|
|
|
options.BlockCodeGithub(out, text, lang)
|
2011-06-29 03:46:35 +02:00
|
|
|
} else {
|
2011-06-29 19:13:17 +02:00
|
|
|
options.BlockCodeNormal(out, text, lang)
|
2011-06-29 03:46:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
func (options *Html) BlockCodeNormal(out *bytes.Buffer, text []byte, lang string) {
|
2011-06-29 23:38:35 +02:00
|
|
|
doubleSpace(out)
|
2011-05-29 05:17:53 +02:00
|
|
|
|
2011-06-29 23:38:35 +02:00
|
|
|
// parse out the language names/classes
|
|
|
|
count := 0
|
|
|
|
for _, elt := range strings.Fields(lang) {
|
|
|
|
if elt[0] == '.' {
|
|
|
|
elt = elt[1:]
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
2011-06-29 23:38:35 +02:00
|
|
|
if len(elt) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if count == 0 {
|
|
|
|
out.WriteString("<pre><code class=\"")
|
|
|
|
} else {
|
|
|
|
out.WriteByte(' ')
|
|
|
|
}
|
|
|
|
attrEscape(out, []byte(elt))
|
|
|
|
count++
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 23:38:35 +02:00
|
|
|
if count == 0 {
|
|
|
|
out.WriteString("<pre><code>")
|
|
|
|
} else {
|
|
|
|
out.WriteString("\">")
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 23:38:35 +02:00
|
|
|
attrEscape(out, text)
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("</code></pre>\n")
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2012-11-22 21:12:08 +01:00
|
|
|
// GitHub style code block:
|
|
|
|
//
|
|
|
|
// <pre lang="LANG"><code>
|
|
|
|
// ...
|
2012-11-22 21:52:52 +01:00
|
|
|
// </code></pre>
|
2012-11-22 21:12:08 +01:00
|
|
|
//
|
|
|
|
// Unlike other parsers, we store the language identifier in the <pre>,
|
|
|
|
// and don't let the user generate custom classes.
|
|
|
|
//
|
|
|
|
// The language identifier in the <pre> block gets postprocessed and all
|
|
|
|
// the code inside gets syntax highlighted with Pygments. This is much safer
|
|
|
|
// than letting the user specify a CSS class for highlighting.
|
|
|
|
//
|
|
|
|
// Note that we only generate HTML for the first specifier.
|
|
|
|
// E.g.
|
|
|
|
// ~~~~ {.python .numbered} => <pre lang="python"><code>
|
2011-06-29 19:13:17 +02:00
|
|
|
func (options *Html) BlockCodeGithub(out *bytes.Buffer, text []byte, lang string) {
|
2011-06-29 23:38:35 +02:00
|
|
|
doubleSpace(out)
|
2011-05-29 05:17:53 +02:00
|
|
|
|
2011-06-29 23:38:35 +02:00
|
|
|
// parse out the language name
|
|
|
|
count := 0
|
|
|
|
for _, elt := range strings.Fields(lang) {
|
|
|
|
if elt[0] == '.' {
|
|
|
|
elt = elt[1:]
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
2011-06-29 23:38:35 +02:00
|
|
|
if len(elt) == 0 {
|
|
|
|
continue
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
2011-06-29 23:38:35 +02:00
|
|
|
out.WriteString("<pre lang=\"")
|
|
|
|
attrEscape(out, []byte(elt))
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("\"><code>")
|
2011-06-29 23:38:35 +02:00
|
|
|
count++
|
|
|
|
break
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 23:38:35 +02:00
|
|
|
if count == 0 {
|
|
|
|
out.WriteString("<pre><code>")
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 23:38:35 +02:00
|
|
|
attrEscape(out, text)
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("</code></pre>\n")
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
func (options *Html) BlockQuote(out *bytes.Buffer, text []byte) {
|
2011-07-01 19:19:42 +02:00
|
|
|
doubleSpace(out)
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("<blockquote>\n")
|
|
|
|
out.Write(text)
|
2011-07-01 19:19:42 +02:00
|
|
|
out.WriteString("</blockquote>\n")
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
func (options *Html) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) {
|
2011-06-29 23:38:35 +02:00
|
|
|
doubleSpace(out)
|
|
|
|
out.WriteString("<table>\n<thead>\n")
|
2011-05-30 19:06:20 +02:00
|
|
|
out.Write(header)
|
2011-07-01 22:13:26 +02:00
|
|
|
out.WriteString("</thead>\n\n<tbody>\n")
|
2011-05-30 19:06:20 +02:00
|
|
|
out.Write(body)
|
2011-07-01 22:13:26 +02:00
|
|
|
out.WriteString("</tbody>\n</table>\n")
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
func (options *Html) TableRow(out *bytes.Buffer, text []byte) {
|
2011-06-29 23:38:35 +02:00
|
|
|
doubleSpace(out)
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("<tr>\n")
|
|
|
|
out.Write(text)
|
2011-07-01 22:13:26 +02:00
|
|
|
out.WriteString("\n</tr>\n")
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2013-10-16 12:36:33 +02:00
|
|
|
func (options *Html) TableHeaderCell(out *bytes.Buffer, text []byte, align int) {
|
|
|
|
doubleSpace(out)
|
|
|
|
switch align {
|
|
|
|
case TABLE_ALIGNMENT_LEFT:
|
|
|
|
out.WriteString("<th align=\"left\">")
|
|
|
|
case TABLE_ALIGNMENT_RIGHT:
|
|
|
|
out.WriteString("<th align=\"right\">")
|
|
|
|
case TABLE_ALIGNMENT_CENTER:
|
|
|
|
out.WriteString("<th align=\"center\">")
|
|
|
|
default:
|
|
|
|
out.WriteString("<th>")
|
|
|
|
}
|
|
|
|
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("</th>")
|
|
|
|
}
|
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
func (options *Html) TableCell(out *bytes.Buffer, text []byte, align int) {
|
2011-06-29 23:38:35 +02:00
|
|
|
doubleSpace(out)
|
2011-05-29 05:17:53 +02:00
|
|
|
switch align {
|
|
|
|
case TABLE_ALIGNMENT_LEFT:
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("<td align=\"left\">")
|
2011-05-29 05:17:53 +02:00
|
|
|
case TABLE_ALIGNMENT_RIGHT:
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("<td align=\"right\">")
|
2011-05-29 05:17:53 +02:00
|
|
|
case TABLE_ALIGNMENT_CENTER:
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("<td align=\"center\">")
|
2011-05-29 05:17:53 +02:00
|
|
|
default:
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("<td>")
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-05-30 19:06:20 +02:00
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("</td>")
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2013-06-26 17:57:51 +02:00
|
|
|
func (options *Html) Footnotes(out *bytes.Buffer, text func() bool) {
|
2013-06-25 03:18:47 +02:00
|
|
|
out.WriteString("<div class=\"footnotes\">\n")
|
|
|
|
options.HRule(out)
|
2013-06-26 17:57:51 +02:00
|
|
|
options.List(out, text, LIST_TYPE_ORDERED)
|
2013-06-25 03:18:47 +02:00
|
|
|
out.WriteString("</div>\n")
|
|
|
|
}
|
|
|
|
|
2013-06-26 17:57:51 +02:00
|
|
|
func (options *Html) FootnoteItem(out *bytes.Buffer, name, text []byte, flags int) {
|
|
|
|
if flags&LIST_ITEM_CONTAINS_BLOCK != 0 || flags&LIST_ITEM_BEGINNING_OF_LIST != 0 {
|
|
|
|
doubleSpace(out)
|
|
|
|
}
|
2014-05-24 20:29:39 +02:00
|
|
|
slug := slugify(name)
|
2014-05-24 09:55:13 +02:00
|
|
|
out.WriteString(`<li id="`)
|
|
|
|
out.WriteString(`fn:`)
|
2014-05-24 21:28:37 +02:00
|
|
|
out.WriteString(options.parameters.FootnoteAnchorPrefix)
|
2014-05-24 20:29:39 +02:00
|
|
|
out.Write(slug)
|
2013-06-26 17:57:51 +02:00
|
|
|
out.WriteString(`">`)
|
|
|
|
out.Write(text)
|
2014-05-24 20:29:39 +02:00
|
|
|
if options.flags&HTML_FOOTNOTE_RETURN_LINKS != 0 {
|
|
|
|
out.WriteString(` <a class="footnote-return" href="#`)
|
|
|
|
out.WriteString(`fnref:`)
|
2014-05-24 21:28:37 +02:00
|
|
|
out.WriteString(options.parameters.FootnoteAnchorPrefix)
|
2014-05-24 20:29:39 +02:00
|
|
|
out.Write(slug)
|
|
|
|
out.WriteString(`">`)
|
|
|
|
out.WriteString(options.parameters.FootnoteReturnLinkContents)
|
|
|
|
out.WriteString(`</a>`)
|
|
|
|
}
|
2013-06-26 17:57:51 +02:00
|
|
|
out.WriteString("</li>\n")
|
|
|
|
}
|
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
func (options *Html) List(out *bytes.Buffer, text func() bool, flags int) {
|
2011-06-25 23:02:46 +02:00
|
|
|
marker := out.Len()
|
2011-06-29 23:38:35 +02:00
|
|
|
doubleSpace(out)
|
2011-06-25 23:02:46 +02:00
|
|
|
|
2011-05-29 05:17:53 +02:00
|
|
|
if flags&LIST_TYPE_ORDERED != 0 {
|
2011-07-01 19:19:42 +02:00
|
|
|
out.WriteString("<ol>")
|
2011-05-29 05:17:53 +02:00
|
|
|
} else {
|
2011-07-01 19:19:42 +02:00
|
|
|
out.WriteString("<ul>")
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
2011-06-25 23:02:46 +02:00
|
|
|
if !text() {
|
|
|
|
out.Truncate(marker)
|
|
|
|
return
|
|
|
|
}
|
2011-05-29 05:17:53 +02:00
|
|
|
if flags&LIST_TYPE_ORDERED != 0 {
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("</ol>\n")
|
2011-05-29 05:17:53 +02:00
|
|
|
} else {
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("</ul>\n")
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
func (options *Html) ListItem(out *bytes.Buffer, text []byte, flags int) {
|
2011-07-01 19:19:42 +02:00
|
|
|
if flags&LIST_ITEM_CONTAINS_BLOCK != 0 || flags&LIST_ITEM_BEGINNING_OF_LIST != 0 {
|
|
|
|
doubleSpace(out)
|
|
|
|
}
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("<li>")
|
2011-06-29 23:38:35 +02:00
|
|
|
out.Write(text)
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("</li>\n")
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
func (options *Html) Paragraph(out *bytes.Buffer, text func() bool) {
|
2011-06-27 01:21:11 +02:00
|
|
|
marker := out.Len()
|
2011-06-29 23:38:35 +02:00
|
|
|
doubleSpace(out)
|
2011-05-29 05:17:53 +02:00
|
|
|
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("<p>")
|
2011-06-27 01:21:11 +02:00
|
|
|
if !text() {
|
|
|
|
out.Truncate(marker)
|
|
|
|
return
|
|
|
|
}
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("</p>\n")
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 21:00:54 +02:00
|
|
|
func (options *Html) AutoLink(out *bytes.Buffer, link []byte, kind int) {
|
2014-01-26 20:39:38 +01:00
|
|
|
skipRanges := htmlEntity.FindAllIndex(link, -1)
|
2011-05-30 01:00:31 +02:00
|
|
|
if options.flags&HTML_SAFELINK != 0 && !isSafeLink(link) && kind != LINK_TYPE_EMAIL {
|
2011-06-29 23:38:35 +02:00
|
|
|
// mark it but don't link it if it is not a safe link: no smartypants
|
|
|
|
out.WriteString("<tt>")
|
2014-01-26 20:39:38 +01:00
|
|
|
entityEscapeWithSkip(out, link, skipRanges)
|
2011-06-29 23:38:35 +02:00
|
|
|
out.WriteString("</tt>")
|
2011-06-29 21:00:54 +02:00
|
|
|
return
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("<a href=\"")
|
2011-05-29 05:17:53 +02:00
|
|
|
if kind == LINK_TYPE_EMAIL {
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("mailto:")
|
2014-05-18 08:28:15 +02:00
|
|
|
} else {
|
|
|
|
options.maybeWriteAbsolutePrefix(out, link)
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
2014-05-18 08:28:15 +02:00
|
|
|
|
2014-01-26 20:39:38 +01:00
|
|
|
entityEscapeWithSkip(out, link, skipRanges)
|
2014-03-21 03:52:46 +01:00
|
|
|
|
2014-03-21 04:14:58 +01:00
|
|
|
if options.flags&HTML_NOFOLLOW_LINKS != 0 && !isRelativeLink(link) {
|
2014-03-21 03:52:46 +01:00
|
|
|
out.WriteString("\" rel=\"nofollow")
|
|
|
|
}
|
|
|
|
// blank target only add to external link
|
|
|
|
if options.flags&HTML_HREF_TARGET_BLANK != 0 && !isRelativeLink(link) {
|
|
|
|
out.WriteString("\" target=\"_blank")
|
|
|
|
}
|
|
|
|
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("\">")
|
2011-05-29 05:17:53 +02:00
|
|
|
|
2011-06-29 23:38:35 +02:00
|
|
|
// Pretty print: if we get an email address as
|
|
|
|
// an actual URI, e.g. `mailto:foo@bar.com`, we don't
|
|
|
|
// want to print the `mailto:` prefix
|
2011-05-31 19:49:49 +02:00
|
|
|
switch {
|
2011-05-30 23:36:31 +02:00
|
|
|
case bytes.HasPrefix(link, []byte("mailto://")):
|
2011-06-29 23:38:35 +02:00
|
|
|
attrEscape(out, link[len("mailto://"):])
|
2011-05-31 19:49:49 +02:00
|
|
|
case bytes.HasPrefix(link, []byte("mailto:")):
|
2011-06-29 23:38:35 +02:00
|
|
|
attrEscape(out, link[len("mailto:"):])
|
2011-05-30 23:36:31 +02:00
|
|
|
default:
|
2014-01-26 20:39:38 +01:00
|
|
|
entityEscapeWithSkip(out, link, skipRanges)
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("</a>")
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 21:00:54 +02:00
|
|
|
func (options *Html) CodeSpan(out *bytes.Buffer, text []byte) {
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("<code>")
|
|
|
|
attrEscape(out, text)
|
|
|
|
out.WriteString("</code>")
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 21:00:54 +02:00
|
|
|
func (options *Html) DoubleEmphasis(out *bytes.Buffer, text []byte) {
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("<strong>")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("</strong>")
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 21:00:54 +02:00
|
|
|
func (options *Html) Emphasis(out *bytes.Buffer, text []byte) {
|
2011-05-29 05:17:53 +02:00
|
|
|
if len(text) == 0 {
|
2011-06-29 21:00:54 +02:00
|
|
|
return
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("<em>")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("</em>")
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2014-05-18 08:28:15 +02:00
|
|
|
func (options *Html) maybeWriteAbsolutePrefix(out *bytes.Buffer, link []byte) {
|
2014-05-29 16:17:20 +02:00
|
|
|
if options.parameters.AbsolutePrefix != "" && isRelativeLink(link) {
|
2014-05-24 09:55:13 +02:00
|
|
|
out.WriteString(options.parameters.AbsolutePrefix)
|
2014-05-18 08:28:15 +02:00
|
|
|
if link[0] != '/' {
|
|
|
|
out.WriteByte('/')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-29 21:00:54 +02:00
|
|
|
func (options *Html) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
|
2011-06-29 03:46:35 +02:00
|
|
|
if options.flags&HTML_SKIP_IMAGES != 0 {
|
2011-06-29 21:00:54 +02:00
|
|
|
return
|
2011-06-29 03:46:35 +02:00
|
|
|
}
|
|
|
|
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("<img src=\"")
|
2014-05-18 08:28:15 +02:00
|
|
|
options.maybeWriteAbsolutePrefix(out, link)
|
2011-05-30 19:06:20 +02:00
|
|
|
attrEscape(out, link)
|
|
|
|
out.WriteString("\" alt=\"")
|
2011-05-29 05:17:53 +02:00
|
|
|
if len(alt) > 0 {
|
2011-05-30 19:06:20 +02:00
|
|
|
attrEscape(out, alt)
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
if len(title) > 0 {
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("\" title=\"")
|
|
|
|
attrEscape(out, title)
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteByte('"')
|
2011-05-31 05:44:52 +02:00
|
|
|
out.WriteString(options.closeTag)
|
2011-06-29 21:00:54 +02:00
|
|
|
return
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 21:00:54 +02:00
|
|
|
func (options *Html) LineBreak(out *bytes.Buffer) {
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("<br")
|
2011-05-31 05:44:52 +02:00
|
|
|
out.WriteString(options.closeTag)
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 21:00:54 +02:00
|
|
|
func (options *Html) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
|
2011-06-29 03:46:35 +02:00
|
|
|
if options.flags&HTML_SKIP_LINKS != 0 {
|
2011-06-29 23:38:35 +02:00
|
|
|
// write the link text out but don't link it, just mark it with typewriter font
|
|
|
|
out.WriteString("<tt>")
|
|
|
|
attrEscape(out, content)
|
|
|
|
out.WriteString("</tt>")
|
2011-06-29 21:00:54 +02:00
|
|
|
return
|
2011-06-29 03:46:35 +02:00
|
|
|
}
|
2011-05-29 05:17:53 +02:00
|
|
|
|
2011-05-30 01:00:31 +02:00
|
|
|
if options.flags&HTML_SAFELINK != 0 && !isSafeLink(link) {
|
2011-06-29 23:38:35 +02:00
|
|
|
// write the link text out but don't link it, just mark it with typewriter font
|
|
|
|
out.WriteString("<tt>")
|
|
|
|
attrEscape(out, content)
|
|
|
|
out.WriteString("</tt>")
|
2011-06-29 21:00:54 +02:00
|
|
|
return
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("<a href=\"")
|
2014-05-18 08:28:15 +02:00
|
|
|
options.maybeWriteAbsolutePrefix(out, link)
|
2011-06-24 19:50:03 +02:00
|
|
|
attrEscape(out, link)
|
2011-05-29 05:17:53 +02:00
|
|
|
if len(title) > 0 {
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("\" title=\"")
|
|
|
|
attrEscape(out, title)
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
2014-03-21 04:14:58 +01:00
|
|
|
if options.flags&HTML_NOFOLLOW_LINKS != 0 && !isRelativeLink(link) {
|
2014-02-25 15:21:57 +01:00
|
|
|
out.WriteString("\" rel=\"nofollow")
|
|
|
|
}
|
2014-03-21 03:52:46 +01:00
|
|
|
// blank target only add to external link
|
|
|
|
if options.flags&HTML_HREF_TARGET_BLANK != 0 && !isRelativeLink(link) {
|
|
|
|
out.WriteString("\" target=\"_blank")
|
|
|
|
}
|
|
|
|
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("\">")
|
2011-06-24 19:50:03 +02:00
|
|
|
out.Write(content)
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("</a>")
|
2011-06-29 21:00:54 +02:00
|
|
|
return
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 21:00:54 +02:00
|
|
|
func (options *Html) RawHtmlTag(out *bytes.Buffer, text []byte) {
|
2011-05-30 01:00:31 +02:00
|
|
|
if options.flags&HTML_SKIP_HTML != 0 {
|
2011-06-29 21:00:54 +02:00
|
|
|
return
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
2011-05-30 01:00:31 +02:00
|
|
|
if options.flags&HTML_SKIP_STYLE != 0 && isHtmlTag(text, "style") {
|
2011-06-29 21:00:54 +02:00
|
|
|
return
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
2011-05-30 01:00:31 +02:00
|
|
|
if options.flags&HTML_SKIP_LINKS != 0 && isHtmlTag(text, "a") {
|
2011-06-29 21:00:54 +02:00
|
|
|
return
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
2011-05-30 01:00:31 +02:00
|
|
|
if options.flags&HTML_SKIP_IMAGES != 0 && isHtmlTag(text, "img") {
|
2011-06-29 21:00:54 +02:00
|
|
|
return
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
2011-05-30 19:06:20 +02:00
|
|
|
out.Write(text)
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 21:00:54 +02:00
|
|
|
func (options *Html) TripleEmphasis(out *bytes.Buffer, text []byte) {
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("<strong><em>")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("</em></strong>")
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 21:00:54 +02:00
|
|
|
func (options *Html) StrikeThrough(out *bytes.Buffer, text []byte) {
|
2011-05-30 19:06:20 +02:00
|
|
|
out.WriteString("<del>")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("</del>")
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2013-06-25 03:18:47 +02:00
|
|
|
func (options *Html) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {
|
|
|
|
slug := slugify(ref)
|
2014-05-24 09:55:13 +02:00
|
|
|
out.WriteString(`<sup class="footnote-ref" id="`)
|
|
|
|
out.WriteString(`fnref:`)
|
2014-05-24 21:28:37 +02:00
|
|
|
out.WriteString(options.parameters.FootnoteAnchorPrefix)
|
2013-06-25 03:18:47 +02:00
|
|
|
out.Write(slug)
|
2014-05-24 09:55:13 +02:00
|
|
|
out.WriteString(`"><a rel="footnote" href="#`)
|
|
|
|
out.WriteString(`fn:`)
|
2014-05-24 21:28:37 +02:00
|
|
|
out.WriteString(options.parameters.FootnoteAnchorPrefix)
|
2013-06-25 03:18:47 +02:00
|
|
|
out.Write(slug)
|
|
|
|
out.WriteString(`">`)
|
|
|
|
out.WriteString(strconv.Itoa(id))
|
|
|
|
out.WriteString(`</a></sup>`)
|
|
|
|
}
|
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
func (options *Html) Entity(out *bytes.Buffer, entity []byte) {
|
2011-06-29 18:08:56 +02:00
|
|
|
out.Write(entity)
|
|
|
|
}
|
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
func (options *Html) NormalText(out *bytes.Buffer, text []byte) {
|
2011-06-29 03:46:35 +02:00
|
|
|
if options.flags&HTML_USE_SMARTYPANTS != 0 {
|
2011-06-29 19:13:17 +02:00
|
|
|
options.Smartypants(out, text)
|
2011-06-29 03:46:35 +02:00
|
|
|
} else {
|
|
|
|
attrEscape(out, text)
|
|
|
|
}
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
func (options *Html) Smartypants(out *bytes.Buffer, text []byte) {
|
|
|
|
smrt := smartypantsData{false, false}
|
|
|
|
|
|
|
|
// first do normal entity escaping
|
|
|
|
var escaped bytes.Buffer
|
|
|
|
attrEscape(&escaped, text)
|
|
|
|
text = escaped.Bytes()
|
|
|
|
|
|
|
|
mark := 0
|
|
|
|
for i := 0; i < len(text); i++ {
|
|
|
|
if action := options.smartypants[text[i]]; action != nil {
|
|
|
|
if i > mark {
|
|
|
|
out.Write(text[mark:i])
|
|
|
|
}
|
|
|
|
|
|
|
|
previousChar := byte(0)
|
|
|
|
if i > 0 {
|
|
|
|
previousChar = text[i-1]
|
|
|
|
}
|
|
|
|
i += action(out, &smrt, previousChar, text[i:])
|
|
|
|
mark = i + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if mark < len(text) {
|
|
|
|
out.Write(text[mark:])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (options *Html) DocumentHeader(out *bytes.Buffer) {
|
2011-06-29 18:08:56 +02:00
|
|
|
if options.flags&HTML_COMPLETE_PAGE == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ending := ""
|
|
|
|
if options.flags&HTML_USE_XHTML != 0 {
|
|
|
|
out.WriteString("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" ")
|
|
|
|
out.WriteString("\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n")
|
|
|
|
out.WriteString("<html xmlns=\"http://www.w3.org/1999/xhtml\">\n")
|
|
|
|
ending = " /"
|
|
|
|
} else {
|
2012-10-22 06:23:44 +02:00
|
|
|
out.WriteString("<!DOCTYPE html>\n")
|
2011-06-29 18:08:56 +02:00
|
|
|
out.WriteString("<html>\n")
|
|
|
|
}
|
|
|
|
out.WriteString("<head>\n")
|
|
|
|
out.WriteString(" <title>")
|
2011-06-29 19:13:17 +02:00
|
|
|
options.NormalText(out, []byte(options.title))
|
2011-06-29 18:08:56 +02:00
|
|
|
out.WriteString("</title>\n")
|
|
|
|
out.WriteString(" <meta name=\"GENERATOR\" content=\"Blackfriday Markdown Processor v")
|
|
|
|
out.WriteString(VERSION)
|
|
|
|
out.WriteString("\"")
|
|
|
|
out.WriteString(ending)
|
|
|
|
out.WriteString(">\n")
|
2012-10-22 06:23:44 +02:00
|
|
|
out.WriteString(" <meta charset=\"utf-8\"")
|
2011-06-29 18:08:56 +02:00
|
|
|
out.WriteString(ending)
|
|
|
|
out.WriteString(">\n")
|
|
|
|
if options.css != "" {
|
|
|
|
out.WriteString(" <link rel=\"stylesheet\" type=\"text/css\" href=\"")
|
|
|
|
attrEscape(out, []byte(options.css))
|
|
|
|
out.WriteString("\"")
|
|
|
|
out.WriteString(ending)
|
|
|
|
out.WriteString(">\n")
|
|
|
|
}
|
|
|
|
out.WriteString("</head>\n")
|
|
|
|
out.WriteString("<body>\n")
|
2011-06-29 18:36:56 +02:00
|
|
|
|
|
|
|
options.tocMarker = out.Len()
|
2011-06-29 18:08:56 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
func (options *Html) DocumentFooter(out *bytes.Buffer) {
|
2011-06-29 18:36:56 +02:00
|
|
|
// finalize and insert the table of contents
|
|
|
|
if options.flags&HTML_TOC != 0 {
|
2011-06-29 19:13:17 +02:00
|
|
|
options.TocFinalize()
|
2011-06-29 18:36:56 +02:00
|
|
|
|
|
|
|
// now we have to insert the table of contents into the document
|
|
|
|
var temp bytes.Buffer
|
|
|
|
|
|
|
|
// start by making a copy of everything after the document header
|
|
|
|
temp.Write(out.Bytes()[options.tocMarker:])
|
|
|
|
|
|
|
|
// now clear the copied material from the main output buffer
|
|
|
|
out.Truncate(options.tocMarker)
|
|
|
|
|
2011-06-29 21:24:15 +02:00
|
|
|
// corner case spacing issue
|
|
|
|
if options.flags&HTML_COMPLETE_PAGE != 0 {
|
|
|
|
out.WriteByte('\n')
|
|
|
|
}
|
|
|
|
|
2011-06-29 18:36:56 +02:00
|
|
|
// insert the table of contents
|
2012-10-22 06:23:44 +02:00
|
|
|
out.WriteString("<nav>\n")
|
2011-06-29 18:36:56 +02:00
|
|
|
out.Write(options.toc.Bytes())
|
2012-10-22 06:23:44 +02:00
|
|
|
out.WriteString("</nav>\n")
|
2011-06-29 18:36:56 +02:00
|
|
|
|
2011-06-29 21:24:15 +02:00
|
|
|
// corner case spacing issue
|
|
|
|
if options.flags&HTML_COMPLETE_PAGE == 0 && options.flags&HTML_OMIT_CONTENTS == 0 {
|
|
|
|
out.WriteByte('\n')
|
|
|
|
}
|
|
|
|
|
2011-06-29 18:36:56 +02:00
|
|
|
// write out everything that came after it
|
|
|
|
if options.flags&HTML_OMIT_CONTENTS == 0 {
|
|
|
|
out.Write(temp.Bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.flags&HTML_COMPLETE_PAGE != 0 {
|
|
|
|
out.WriteString("\n</body>\n")
|
|
|
|
out.WriteString("</html>\n")
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
2011-06-29 18:08:56 +02:00
|
|
|
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
func (options *Html) TocHeader(text []byte, level int) {
|
2011-06-29 18:36:56 +02:00
|
|
|
for level > options.currentLevel {
|
|
|
|
switch {
|
|
|
|
case bytes.HasSuffix(options.toc.Bytes(), []byte("</li>\n")):
|
2011-06-29 21:24:15 +02:00
|
|
|
// this sublist can nest underneath a header
|
2011-06-29 18:36:56 +02:00
|
|
|
size := options.toc.Len()
|
|
|
|
options.toc.Truncate(size - len("</li>\n"))
|
|
|
|
|
|
|
|
case options.currentLevel > 0:
|
|
|
|
options.toc.WriteString("<li>")
|
|
|
|
}
|
2011-06-29 21:24:15 +02:00
|
|
|
if options.toc.Len() > 0 {
|
|
|
|
options.toc.WriteByte('\n')
|
|
|
|
}
|
|
|
|
options.toc.WriteString("<ul>\n")
|
2011-06-29 18:36:56 +02:00
|
|
|
options.currentLevel++
|
|
|
|
}
|
|
|
|
|
|
|
|
for level < options.currentLevel {
|
|
|
|
options.toc.WriteString("</ul>")
|
|
|
|
if options.currentLevel > 1 {
|
|
|
|
options.toc.WriteString("</li>\n")
|
|
|
|
}
|
|
|
|
options.currentLevel--
|
|
|
|
}
|
|
|
|
|
|
|
|
options.toc.WriteString("<li><a href=\"#toc_")
|
|
|
|
options.toc.WriteString(strconv.Itoa(options.headerCount))
|
|
|
|
options.toc.WriteString("\">")
|
|
|
|
options.headerCount++
|
|
|
|
|
|
|
|
options.toc.Write(text)
|
|
|
|
|
|
|
|
options.toc.WriteString("</a></li>\n")
|
|
|
|
}
|
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
func (options *Html) TocFinalize() {
|
2011-06-29 18:08:56 +02:00
|
|
|
for options.currentLevel > 1 {
|
2011-06-29 18:36:56 +02:00
|
|
|
options.toc.WriteString("</ul></li>\n")
|
2011-06-29 18:08:56 +02:00
|
|
|
options.currentLevel--
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 18:08:56 +02:00
|
|
|
if options.currentLevel > 0 {
|
2011-06-29 18:36:56 +02:00
|
|
|
options.toc.WriteString("</ul>\n")
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-30 01:00:31 +02:00
|
|
|
func isHtmlTag(tag []byte, tagname string) bool {
|
2013-04-18 02:15:47 +02:00
|
|
|
found, _ := findHtmlTagPos(tag, tagname)
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
2014-01-21 23:45:43 +01:00
|
|
|
// Look for a character, but ignore it when it's in any kind of quotes, it
|
|
|
|
// might be JavaScript
|
|
|
|
func skipUntilCharIgnoreQuotes(html []byte, start int, char byte) int {
|
|
|
|
inSingleQuote := false
|
|
|
|
inDoubleQuote := false
|
|
|
|
inGraveQuote := false
|
|
|
|
i := start
|
|
|
|
for i < len(html) {
|
|
|
|
switch {
|
|
|
|
case html[i] == char && !inSingleQuote && !inDoubleQuote && !inGraveQuote:
|
|
|
|
return i
|
|
|
|
case html[i] == '\'':
|
|
|
|
inSingleQuote = !inSingleQuote
|
|
|
|
case html[i] == '"':
|
|
|
|
inDoubleQuote = !inDoubleQuote
|
|
|
|
case html[i] == '`':
|
|
|
|
inGraveQuote = !inGraveQuote
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return start
|
|
|
|
}
|
|
|
|
|
2013-04-18 02:15:47 +02:00
|
|
|
func findHtmlTagPos(tag []byte, tagname string) (bool, int) {
|
2011-05-29 05:17:53 +02:00
|
|
|
i := 0
|
|
|
|
if i < len(tag) && tag[0] != '<' {
|
2013-04-18 02:15:47 +02:00
|
|
|
return false, -1
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
i++
|
2013-04-13 21:26:29 +02:00
|
|
|
i = skipSpace(tag, i)
|
2011-05-29 05:17:53 +02:00
|
|
|
|
|
|
|
if i < len(tag) && tag[i] == '/' {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
2013-04-13 21:26:29 +02:00
|
|
|
i = skipSpace(tag, i)
|
2013-04-13 21:21:47 +02:00
|
|
|
j := 0
|
2011-06-29 00:02:12 +02:00
|
|
|
for ; i < len(tag); i, j = i+1, j+1 {
|
|
|
|
if j >= len(tagname) {
|
2011-05-29 05:17:53 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2013-04-13 21:34:37 +02:00
|
|
|
if strings.ToLower(string(tag[i]))[0] != tagname[j] {
|
2013-04-18 02:15:47 +02:00
|
|
|
return false, -1
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == len(tag) {
|
2013-04-18 02:15:47 +02:00
|
|
|
return false, -1
|
|
|
|
}
|
|
|
|
|
2014-01-21 23:45:43 +01:00
|
|
|
rightAngle := skipUntilCharIgnoreQuotes(tag, i, '>')
|
|
|
|
if rightAngle > i {
|
|
|
|
return true, rightAngle
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
|
|
|
|
2013-04-18 02:15:47 +02:00
|
|
|
return false, -1
|
2011-05-29 05:17:53 +02:00
|
|
|
}
|
2011-06-29 23:38:35 +02:00
|
|
|
|
2014-01-22 00:14:35 +01:00
|
|
|
func skipUntilChar(text []byte, start int, char byte) int {
|
|
|
|
i := start
|
|
|
|
for i < len(text) && text[i] != char {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2013-04-13 21:26:29 +02:00
|
|
|
func skipSpace(tag []byte, i int) int {
|
|
|
|
for i < len(tag) && isspace(tag[i]) {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2011-06-29 23:38:35 +02:00
|
|
|
func doubleSpace(out *bytes.Buffer) {
|
|
|
|
if out.Len() > 0 {
|
|
|
|
out.WriteByte('\n')
|
|
|
|
}
|
|
|
|
}
|
2014-03-21 03:52:46 +01:00
|
|
|
|
|
|
|
func isRelativeLink(link []byte) (yes bool) {
|
|
|
|
yes = false
|
|
|
|
|
|
|
|
// a tag begin with '#'
|
|
|
|
if link[0] == '#' {
|
|
|
|
yes = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// link begin with '/' but not '//', the second maybe a protocol relative link
|
|
|
|
if len(link) >= 2 && link[0] == '/' && link[1] != '/' {
|
|
|
|
yes = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// only the root '/'
|
|
|
|
if len(link) == 1 && link[0] == '/' {
|
|
|
|
yes = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|