2011-05-29 03:17:53 +00:00
|
|
|
//
|
|
|
|
// Black Friday Markdown Processor
|
|
|
|
// Originally based on http://github.com/tanoku/upskirt
|
|
|
|
// by Russ Ross <russ@russross.com>
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// HTML rendering backend
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
package blackfriday
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
HTML_SKIP_HTML = 1 << iota
|
|
|
|
HTML_SKIP_STYLE
|
|
|
|
HTML_SKIP_IMAGES
|
|
|
|
HTML_SKIP_LINKS
|
|
|
|
HTML_EXPAND_TABS
|
|
|
|
HTML_SAFELINK
|
|
|
|
HTML_TOC
|
|
|
|
HTML_HARD_WRAP
|
|
|
|
HTML_GITHUB_BLOCKCODE
|
|
|
|
HTML_USE_XHTML
|
|
|
|
HTML_USE_SMARTYPANTS
|
|
|
|
HTML_SMARTYPANTS_FRACTIONS
|
|
|
|
HTML_SMARTYPANTS_LATEX_DASHES
|
|
|
|
)
|
|
|
|
|
|
|
|
type htmlOptions struct {
|
2011-05-31 17:49:49 +00:00
|
|
|
flags int
|
2011-05-31 03:44:52 +00:00
|
|
|
closeTag string // how to end singleton tags: usually " />\n", possibly ">\n"
|
|
|
|
tocData struct {
|
|
|
|
headerCount int
|
|
|
|
currentLevel int
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
smartypants *SmartypantsRenderer
|
|
|
|
}
|
|
|
|
|
2011-05-31 03:44:52 +00:00
|
|
|
var xhtmlClose = " />\n"
|
|
|
|
var htmlClose = ">\n"
|
2011-05-29 03:17:53 +00:00
|
|
|
|
|
|
|
func HtmlRenderer(flags int) *Renderer {
|
|
|
|
// configure the rendering engine
|
|
|
|
r := new(Renderer)
|
|
|
|
if flags&HTML_GITHUB_BLOCKCODE == 0 {
|
2011-05-31 03:44:52 +00:00
|
|
|
r.BlockCode = htmlBlockCode
|
2011-05-29 03:17:53 +00:00
|
|
|
} else {
|
2011-05-31 03:44:52 +00:00
|
|
|
r.BlockCode = htmlBlockCodeGithub
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2011-05-31 03:44:52 +00:00
|
|
|
r.BlockQuote = htmlBlockQuote
|
2011-05-29 03:17:53 +00:00
|
|
|
if flags&HTML_SKIP_HTML == 0 {
|
2011-05-31 03:44:52 +00:00
|
|
|
r.BlockHtml = htmlRawBlock
|
|
|
|
}
|
|
|
|
r.Header = htmlHeader
|
|
|
|
r.HRule = htmlHRule
|
|
|
|
r.List = htmlList
|
|
|
|
r.ListItem = htmlListItem
|
|
|
|
r.Paragraph = htmlParagraph
|
|
|
|
r.Table = htmlTable
|
|
|
|
r.TableRow = htmlTableRow
|
|
|
|
r.TableCell = htmlTableCell
|
|
|
|
|
|
|
|
r.AutoLink = htmlAutoLink
|
|
|
|
r.CodeSpan = htmlCodeSpan
|
|
|
|
r.DoubleEmphasis = htmlDoubleEmphasis
|
|
|
|
r.Emphasis = htmlEmphasis
|
2011-05-29 03:17:53 +00:00
|
|
|
if flags&HTML_SKIP_IMAGES == 0 {
|
2011-05-31 03:44:52 +00:00
|
|
|
r.Image = htmlImage
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2011-05-31 03:44:52 +00:00
|
|
|
r.LineBreak = htmlLineBreak
|
2011-05-29 03:17:53 +00:00
|
|
|
if flags&HTML_SKIP_LINKS == 0 {
|
2011-05-31 03:44:52 +00:00
|
|
|
r.Link = htmlLink
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2011-05-31 03:44:52 +00:00
|
|
|
r.RawHtmlTag = htmlRawTag
|
|
|
|
r.TripleEmphasis = htmlTripleEmphasis
|
|
|
|
r.StrikeThrough = htmlStrikeThrough
|
2011-05-29 03:17:53 +00:00
|
|
|
|
|
|
|
var cb *SmartypantsRenderer
|
|
|
|
if flags&HTML_USE_SMARTYPANTS == 0 {
|
2011-05-31 03:44:52 +00:00
|
|
|
r.NormalText = htmlNormalText
|
2011-05-29 03:17:53 +00:00
|
|
|
} else {
|
|
|
|
cb = Smartypants(flags)
|
2011-05-31 03:44:52 +00:00
|
|
|
r.NormalText = htmlSmartypants
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-31 03:44:52 +00:00
|
|
|
closeTag := htmlClose
|
2011-05-29 03:17:53 +00:00
|
|
|
if flags&HTML_USE_XHTML != 0 {
|
2011-05-31 03:44:52 +00:00
|
|
|
closeTag = xhtmlClose
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2011-05-31 03:44:52 +00:00
|
|
|
r.Opaque = &htmlOptions{flags: flags, closeTag: closeTag, smartypants: cb}
|
2011-05-29 03:17:53 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func HtmlTocRenderer(flags int) *Renderer {
|
|
|
|
// configure the rendering engine
|
|
|
|
r := new(Renderer)
|
2011-05-31 03:44:52 +00:00
|
|
|
r.Header = htmlTocHeader
|
2011-05-29 03:17:53 +00:00
|
|
|
|
2011-05-31 03:44:52 +00:00
|
|
|
r.CodeSpan = htmlCodeSpan
|
|
|
|
r.DoubleEmphasis = htmlDoubleEmphasis
|
|
|
|
r.Emphasis = htmlEmphasis
|
|
|
|
r.TripleEmphasis = htmlTripleEmphasis
|
|
|
|
r.StrikeThrough = htmlStrikeThrough
|
2011-05-29 03:17:53 +00:00
|
|
|
|
2011-05-31 03:44:52 +00:00
|
|
|
r.DocumentFooter = htmlTocFinalize
|
2011-05-29 03:17:53 +00:00
|
|
|
|
2011-05-31 03:44:52 +00:00
|
|
|
closeTag := ">\n"
|
2011-05-29 03:17:53 +00:00
|
|
|
if flags&HTML_USE_XHTML != 0 {
|
2011-05-31 03:44:52 +00:00
|
|
|
closeTag = " />\n"
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2011-05-31 03:44:52 +00:00
|
|
|
r.Opaque = &htmlOptions{flags: flags | HTML_TOC, closeTag: closeTag}
|
2011-05-29 03:17:53 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
func attrEscape(out *bytes.Buffer, src []byte) {
|
2011-06-25 01:11:06 +00:00
|
|
|
org := 0
|
|
|
|
for i, ch := range src {
|
2011-06-25 03:53:46 +00:00
|
|
|
// doing this check is a bit faster than falling into
|
|
|
|
// the switch statement. as the compiler improves, this
|
|
|
|
// should be unnecessary
|
|
|
|
if ch != '"' && ch != '&' && ch != '<' && ch != '>' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2011-06-25 01:11:06 +00:00
|
|
|
switch ch {
|
2011-05-29 03:17:53 +00:00
|
|
|
case '<':
|
2011-06-25 01:11:06 +00:00
|
|
|
if i > org {
|
|
|
|
// copy all the normal characters since the last escape
|
|
|
|
out.Write(src[org:i])
|
|
|
|
}
|
|
|
|
org = i + 1
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<")
|
2011-05-29 03:17:53 +00:00
|
|
|
case '>':
|
2011-06-25 01:11:06 +00:00
|
|
|
if i > org {
|
|
|
|
out.Write(src[org:i])
|
|
|
|
}
|
|
|
|
org = i + 1
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString(">")
|
2011-05-29 03:17:53 +00:00
|
|
|
case '&':
|
2011-06-25 01:11:06 +00:00
|
|
|
if i > org {
|
|
|
|
out.Write(src[org:i])
|
|
|
|
}
|
|
|
|
org = i + 1
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("&")
|
2011-05-29 03:17:53 +00:00
|
|
|
case '"':
|
2011-06-25 01:11:06 +00:00
|
|
|
if i > org {
|
|
|
|
out.Write(src[org:i])
|
|
|
|
}
|
|
|
|
org = i + 1
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString(""")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-25 01:11:06 +00:00
|
|
|
if org < len(src) {
|
|
|
|
out.Write(src[org:])
|
|
|
|
}
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-06-25 14:20:08 +00:00
|
|
|
func htmlHeader(out *bytes.Buffer, text func() bool, level int, opaque interface{}) {
|
2011-05-29 03:17:53 +00:00
|
|
|
options := opaque.(*htmlOptions)
|
2011-06-25 14:20:08 +00:00
|
|
|
marker := out.Len()
|
2011-05-29 03:17:53 +00:00
|
|
|
|
2011-06-25 14:20:08 +00:00
|
|
|
if marker > 0 {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteByte('\n')
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-29 23:00:31 +00:00
|
|
|
if options.flags&HTML_TOC != 0 {
|
2011-05-31 03:44:52 +00:00
|
|
|
out.WriteString(fmt.Sprintf("<h%d id=\"toc_%d\">", level, options.tocData.headerCount))
|
|
|
|
options.tocData.headerCount++
|
2011-05-29 03:17:53 +00:00
|
|
|
} else {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString(fmt.Sprintf("<h%d>", level))
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-06-25 14:20:08 +00:00
|
|
|
if !text() {
|
|
|
|
out.Truncate(marker)
|
|
|
|
return
|
|
|
|
}
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString(fmt.Sprintf("</h%d>\n", level))
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
func htmlRawBlock(out *bytes.Buffer, text []byte, opaque interface{}) {
|
2011-05-29 03:17:53 +00:00
|
|
|
sz := len(text)
|
|
|
|
for sz > 0 && text[sz-1] == '\n' {
|
|
|
|
sz--
|
|
|
|
}
|
|
|
|
org := 0
|
|
|
|
for org < sz && text[org] == '\n' {
|
|
|
|
org++
|
|
|
|
}
|
|
|
|
if org >= sz {
|
|
|
|
return
|
|
|
|
}
|
2011-05-30 17:06:20 +00:00
|
|
|
if out.Len() > 0 {
|
|
|
|
out.WriteByte('\n')
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2011-05-30 17:06:20 +00:00
|
|
|
out.Write(text[org:sz])
|
|
|
|
out.WriteByte('\n')
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-31 03:44:52 +00:00
|
|
|
func htmlHRule(out *bytes.Buffer, opaque interface{}) {
|
2011-05-29 03:17:53 +00:00
|
|
|
options := opaque.(*htmlOptions)
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
if out.Len() > 0 {
|
|
|
|
out.WriteByte('\n')
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<hr")
|
2011-05-31 03:44:52 +00:00
|
|
|
out.WriteString(options.closeTag)
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-31 03:44:52 +00:00
|
|
|
func htmlBlockCode(out *bytes.Buffer, text []byte, lang string, opaque interface{}) {
|
2011-05-30 17:06:20 +00:00
|
|
|
if out.Len() > 0 {
|
|
|
|
out.WriteByte('\n')
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if lang != "" {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<pre><code class=\"")
|
2011-05-29 03:17:53 +00:00
|
|
|
|
|
|
|
for i, cls := 0, 0; i < len(lang); i, cls = i+1, cls+1 {
|
|
|
|
for i < len(lang) && isspace(lang[i]) {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
if i < len(lang) {
|
|
|
|
org := i
|
|
|
|
for i < len(lang) && !isspace(lang[i]) {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
if lang[org] == '.' {
|
|
|
|
org++
|
|
|
|
}
|
|
|
|
|
|
|
|
if cls > 0 {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteByte(' ')
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2011-05-30 17:06:20 +00:00
|
|
|
attrEscape(out, []byte(lang[org:]))
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("\">")
|
2011-05-29 03:17:53 +00:00
|
|
|
} else {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<pre><code>")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(text) > 0 {
|
2011-05-30 17:06:20 +00:00
|
|
|
attrEscape(out, text)
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("</code></pre>\n")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GitHub style code block:
|
|
|
|
*
|
|
|
|
* <pre lang="LANG"><code>
|
|
|
|
* ...
|
|
|
|
* </pre></code>
|
|
|
|
*
|
|
|
|
* 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-05-31 03:44:52 +00:00
|
|
|
func htmlBlockCodeGithub(out *bytes.Buffer, text []byte, lang string, opaque interface{}) {
|
2011-05-30 17:06:20 +00:00
|
|
|
if out.Len() > 0 {
|
|
|
|
out.WriteByte('\n')
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(lang) > 0 {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<pre lang=\"")
|
2011-05-29 03:17:53 +00:00
|
|
|
|
|
|
|
i := 0
|
|
|
|
for i < len(lang) && !isspace(lang[i]) {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
if lang[0] == '.' {
|
2011-05-30 17:06:20 +00:00
|
|
|
attrEscape(out, []byte(lang[1:i]))
|
2011-05-29 03:17:53 +00:00
|
|
|
} else {
|
2011-05-30 17:06:20 +00:00
|
|
|
attrEscape(out, []byte(lang[:i]))
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("\"><code>")
|
2011-05-29 03:17:53 +00:00
|
|
|
} else {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<pre><code>")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(text) > 0 {
|
2011-05-30 17:06:20 +00:00
|
|
|
attrEscape(out, text)
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("</code></pre>\n")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-31 03:44:52 +00:00
|
|
|
func htmlBlockQuote(out *bytes.Buffer, text []byte, opaque interface{}) {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<blockquote>\n")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("</blockquote>")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
func htmlTable(out *bytes.Buffer, header []byte, body []byte, columnData []int, opaque interface{}) {
|
|
|
|
if out.Len() > 0 {
|
|
|
|
out.WriteByte('\n')
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<table><thead>\n")
|
|
|
|
out.Write(header)
|
|
|
|
out.WriteString("\n</thead><tbody>\n")
|
|
|
|
out.Write(body)
|
|
|
|
out.WriteString("\n</tbody></table>")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
func htmlTableRow(out *bytes.Buffer, text []byte, opaque interface{}) {
|
|
|
|
if out.Len() > 0 {
|
|
|
|
out.WriteByte('\n')
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<tr>\n")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("\n</tr>")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
func htmlTableCell(out *bytes.Buffer, text []byte, align int, opaque interface{}) {
|
|
|
|
if out.Len() > 0 {
|
|
|
|
out.WriteByte('\n')
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
switch align {
|
|
|
|
case TABLE_ALIGNMENT_LEFT:
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<td align=\"left\">")
|
2011-05-29 03:17:53 +00:00
|
|
|
case TABLE_ALIGNMENT_RIGHT:
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<td align=\"right\">")
|
2011-05-29 03:17:53 +00:00
|
|
|
case TABLE_ALIGNMENT_CENTER:
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<td align=\"center\">")
|
2011-05-29 03:17:53 +00:00
|
|
|
default:
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<td>")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("</td>")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
func htmlList(out *bytes.Buffer, text []byte, flags int, opaque interface{}) {
|
|
|
|
if out.Len() > 0 {
|
|
|
|
out.WriteByte('\n')
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
if flags&LIST_TYPE_ORDERED != 0 {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<ol>\n")
|
2011-05-29 03:17:53 +00:00
|
|
|
} else {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<ul>\n")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2011-05-30 17:06:20 +00:00
|
|
|
out.Write(text)
|
2011-05-29 03:17:53 +00:00
|
|
|
if flags&LIST_TYPE_ORDERED != 0 {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("</ol>\n")
|
2011-05-29 03:17:53 +00:00
|
|
|
} else {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("</ul>\n")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-31 03:44:52 +00:00
|
|
|
func htmlListItem(out *bytes.Buffer, text []byte, flags int, opaque interface{}) {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<li>")
|
2011-05-29 03:17:53 +00:00
|
|
|
size := len(text)
|
|
|
|
for size > 0 && text[size-1] == '\n' {
|
|
|
|
size--
|
|
|
|
}
|
2011-05-30 17:06:20 +00:00
|
|
|
out.Write(text[:size])
|
|
|
|
out.WriteString("</li>\n")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
func htmlParagraph(out *bytes.Buffer, text []byte, opaque interface{}) {
|
2011-05-29 03:17:53 +00:00
|
|
|
options := opaque.(*htmlOptions)
|
|
|
|
i := 0
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
if out.Len() > 0 {
|
|
|
|
out.WriteByte('\n')
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(text) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i < len(text) && isspace(text[i]) {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == len(text) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<p>")
|
2011-05-29 23:00:31 +00:00
|
|
|
if options.flags&HTML_HARD_WRAP != 0 {
|
2011-05-29 03:17:53 +00:00
|
|
|
for i < len(text) {
|
|
|
|
org := i
|
|
|
|
for i < len(text) && text[i] != '\n' {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
if i > org {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.Write(text[org:i])
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if i >= len(text) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<br>")
|
2011-05-31 03:44:52 +00:00
|
|
|
out.WriteString(options.closeTag)
|
2011-05-29 03:17:53 +00:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
} else {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.Write(text[i:])
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("</p>\n")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-31 03:44:52 +00:00
|
|
|
func htmlAutoLink(out *bytes.Buffer, link []byte, kind int, opaque interface{}) int {
|
2011-05-29 03:17:53 +00:00
|
|
|
options := opaque.(*htmlOptions)
|
|
|
|
|
|
|
|
if len(link) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2011-05-29 23:00:31 +00:00
|
|
|
if options.flags&HTML_SAFELINK != 0 && !isSafeLink(link) && kind != LINK_TYPE_EMAIL {
|
2011-05-29 03:17:53 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<a href=\"")
|
2011-05-29 03:17:53 +00:00
|
|
|
if kind == LINK_TYPE_EMAIL {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("mailto:")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2011-06-24 17:50:03 +00:00
|
|
|
attrEscape(out, link)
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("\">")
|
2011-05-29 03:17:53 +00: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 17:49:49 +00:00
|
|
|
switch {
|
2011-05-30 21:36:31 +00:00
|
|
|
case bytes.HasPrefix(link, []byte("mailto://")):
|
|
|
|
attrEscape(out, link[9:])
|
2011-05-31 17:49:49 +00:00
|
|
|
case bytes.HasPrefix(link, []byte("mailto:")):
|
2011-05-30 17:06:20 +00:00
|
|
|
attrEscape(out, link[7:])
|
2011-05-30 21:36:31 +00:00
|
|
|
default:
|
2011-05-30 17:06:20 +00:00
|
|
|
attrEscape(out, link)
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("</a>")
|
2011-05-29 03:17:53 +00:00
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2011-05-31 03:44:52 +00:00
|
|
|
func htmlCodeSpan(out *bytes.Buffer, text []byte, opaque interface{}) int {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<code>")
|
|
|
|
attrEscape(out, text)
|
|
|
|
out.WriteString("</code>")
|
2011-05-29 03:17:53 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
func htmlDoubleEmphasis(out *bytes.Buffer, text []byte, opaque interface{}) int {
|
2011-05-29 03:17:53 +00:00
|
|
|
if len(text) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<strong>")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("</strong>")
|
2011-05-29 03:17:53 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
func htmlEmphasis(out *bytes.Buffer, text []byte, opaque interface{}) int {
|
2011-05-29 03:17:53 +00:00
|
|
|
if len(text) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<em>")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("</em>")
|
2011-05-29 03:17:53 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
func htmlImage(out *bytes.Buffer, link []byte, title []byte, alt []byte, opaque interface{}) int {
|
2011-05-29 03:17:53 +00:00
|
|
|
options := opaque.(*htmlOptions)
|
|
|
|
if len(link) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<img src=\"")
|
|
|
|
attrEscape(out, link)
|
|
|
|
out.WriteString("\" alt=\"")
|
2011-05-29 03:17:53 +00:00
|
|
|
if len(alt) > 0 {
|
2011-05-30 17:06:20 +00:00
|
|
|
attrEscape(out, alt)
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
if len(title) > 0 {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("\" title=\"")
|
|
|
|
attrEscape(out, title)
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteByte('"')
|
2011-05-31 03:44:52 +00:00
|
|
|
out.WriteString(options.closeTag)
|
2011-05-29 03:17:53 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2011-05-31 03:44:52 +00:00
|
|
|
func htmlLineBreak(out *bytes.Buffer, opaque interface{}) int {
|
2011-05-29 03:17:53 +00:00
|
|
|
options := opaque.(*htmlOptions)
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<br")
|
2011-05-31 03:44:52 +00:00
|
|
|
out.WriteString(options.closeTag)
|
2011-05-29 03:17:53 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
func htmlLink(out *bytes.Buffer, link []byte, title []byte, content []byte, opaque interface{}) int {
|
2011-05-29 03:17:53 +00:00
|
|
|
options := opaque.(*htmlOptions)
|
|
|
|
|
2011-05-29 23:00:31 +00:00
|
|
|
if options.flags&HTML_SAFELINK != 0 && !isSafeLink(link) {
|
2011-05-29 03:17:53 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<a href=\"")
|
2011-06-24 17:50:03 +00:00
|
|
|
attrEscape(out, link)
|
2011-05-29 03:17:53 +00:00
|
|
|
if len(title) > 0 {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("\" title=\"")
|
|
|
|
attrEscape(out, title)
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("\">")
|
2011-06-24 17:50:03 +00:00
|
|
|
out.Write(content)
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("</a>")
|
2011-05-29 03:17:53 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
func htmlRawTag(out *bytes.Buffer, text []byte, opaque interface{}) int {
|
2011-05-29 03:17:53 +00:00
|
|
|
options := opaque.(*htmlOptions)
|
2011-05-29 23:00:31 +00:00
|
|
|
if options.flags&HTML_SKIP_HTML != 0 {
|
2011-05-29 03:17:53 +00:00
|
|
|
return 1
|
|
|
|
}
|
2011-05-29 23:00:31 +00:00
|
|
|
if options.flags&HTML_SKIP_STYLE != 0 && isHtmlTag(text, "style") {
|
2011-05-29 03:17:53 +00:00
|
|
|
return 1
|
|
|
|
}
|
2011-05-29 23:00:31 +00:00
|
|
|
if options.flags&HTML_SKIP_LINKS != 0 && isHtmlTag(text, "a") {
|
2011-05-29 03:17:53 +00:00
|
|
|
return 1
|
|
|
|
}
|
2011-05-29 23:00:31 +00:00
|
|
|
if options.flags&HTML_SKIP_IMAGES != 0 && isHtmlTag(text, "img") {
|
2011-05-29 03:17:53 +00:00
|
|
|
return 1
|
|
|
|
}
|
2011-05-30 17:06:20 +00:00
|
|
|
out.Write(text)
|
2011-05-29 03:17:53 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
func htmlTripleEmphasis(out *bytes.Buffer, text []byte, opaque interface{}) int {
|
2011-05-29 03:17:53 +00:00
|
|
|
if len(text) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<strong><em>")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("</em></strong>")
|
2011-05-29 03:17:53 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2011-05-31 03:44:52 +00:00
|
|
|
func htmlStrikeThrough(out *bytes.Buffer, text []byte, opaque interface{}) int {
|
2011-05-29 03:17:53 +00:00
|
|
|
if len(text) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<del>")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("</del>")
|
2011-05-29 03:17:53 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
func htmlNormalText(out *bytes.Buffer, text []byte, opaque interface{}) {
|
|
|
|
attrEscape(out, text)
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-06-25 14:20:08 +00:00
|
|
|
func htmlTocHeader(out *bytes.Buffer, text func() bool, level int, opaque interface{}) {
|
2011-05-29 03:17:53 +00:00
|
|
|
options := opaque.(*htmlOptions)
|
2011-06-25 14:20:08 +00:00
|
|
|
marker := out.Len()
|
|
|
|
|
2011-05-31 03:44:52 +00:00
|
|
|
for level > options.tocData.currentLevel {
|
|
|
|
if options.tocData.currentLevel > 0 {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<li>")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<ul>\n")
|
2011-05-31 03:44:52 +00:00
|
|
|
options.tocData.currentLevel++
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-31 03:44:52 +00:00
|
|
|
for level < options.tocData.currentLevel {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("</ul>")
|
2011-05-31 03:44:52 +00:00
|
|
|
if options.tocData.currentLevel > 1 {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("</li>\n")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2011-05-31 03:44:52 +00:00
|
|
|
options.tocData.currentLevel--
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<li><a href=\"#toc_")
|
2011-05-31 03:44:52 +00:00
|
|
|
out.WriteString(strconv.Itoa(options.tocData.headerCount))
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("\">")
|
2011-05-31 03:44:52 +00:00
|
|
|
options.tocData.headerCount++
|
2011-05-29 03:17:53 +00:00
|
|
|
|
2011-06-25 14:20:08 +00:00
|
|
|
if !text() {
|
|
|
|
out.Truncate(marker)
|
|
|
|
return
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("</a></li>\n")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
func htmlTocFinalize(out *bytes.Buffer, opaque interface{}) {
|
2011-05-29 03:17:53 +00:00
|
|
|
options := opaque.(*htmlOptions)
|
2011-05-31 03:44:52 +00:00
|
|
|
for options.tocData.currentLevel > 1 {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("</ul></li>\n")
|
2011-05-31 03:44:52 +00:00
|
|
|
options.tocData.currentLevel--
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-31 03:44:52 +00:00
|
|
|
if options.tocData.currentLevel > 0 {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("</ul>\n")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-29 23:00:31 +00:00
|
|
|
func isHtmlTag(tag []byte, tagname string) bool {
|
2011-05-29 03:17:53 +00:00
|
|
|
i := 0
|
|
|
|
if i < len(tag) && tag[0] != '<' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
for i < len(tag) && isspace(tag[i]) {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
if i < len(tag) && tag[i] == '/' {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
for i < len(tag) && isspace(tag[i]) {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
tag_i := i
|
|
|
|
for ; i < len(tag); i, tag_i = i+1, tag_i+1 {
|
|
|
|
if tag_i >= len(tagname) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if tag[i] != tagname[tag_i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == len(tag) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return isspace(tag[i]) || tag[i] == '>'
|
|
|
|
}
|