2011-05-29 11:17:53 +08:00
|
|
|
//
|
2011-06-28 10:11:32 +08:00
|
|
|
// Blackfriday Markdown Processor
|
|
|
|
// Available at http://github.com/russross/blackfriday
|
|
|
|
//
|
|
|
|
// Copyright © 2011 Russ Ross <russ@russross.com>.
|
2011-06-29 01:30:10 +08:00
|
|
|
// Distributed under the Simplified BSD License.
|
2011-06-28 10:11:32 +08:00
|
|
|
// See README.md for details.
|
2011-05-29 11:17:53 +08:00
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// HTML rendering backend
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
package blackfriday
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2011-06-30 05:38:35 +08:00
|
|
|
"strings"
|
2011-05-29 11:17:53 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
HTML_SKIP_HTML = 1 << iota
|
|
|
|
HTML_SKIP_STYLE
|
|
|
|
HTML_SKIP_IMAGES
|
|
|
|
HTML_SKIP_LINKS
|
|
|
|
HTML_SAFELINK
|
|
|
|
HTML_TOC
|
2011-06-30 00:36:56 +08:00
|
|
|
HTML_OMIT_CONTENTS
|
2011-06-30 00:08:56 +08:00
|
|
|
HTML_COMPLETE_PAGE
|
2011-05-29 11:17:53 +08:00
|
|
|
HTML_GITHUB_BLOCKCODE
|
|
|
|
HTML_USE_XHTML
|
|
|
|
HTML_USE_SMARTYPANTS
|
|
|
|
HTML_SMARTYPANTS_FRACTIONS
|
|
|
|
HTML_SMARTYPANTS_LATEX_DASHES
|
|
|
|
)
|
|
|
|
|
2011-06-30 01:13:17 +08:00
|
|
|
type Html struct {
|
2011-06-30 00:08:56 +08:00
|
|
|
flags int // HTML_* options
|
2011-06-26 23:51:36 +08:00
|
|
|
closeTag string // how to end singleton tags: either " />\n" or ">\n"
|
2011-06-30 00:08:56 +08:00
|
|
|
title string // document title
|
|
|
|
css string // optional css file url (used with HTML_COMPLETE_PAGE)
|
|
|
|
|
|
|
|
// table of contents data
|
2011-06-30 00:36:56 +08:00
|
|
|
tocMarker int
|
2011-06-30 00:08:56 +08:00
|
|
|
headerCount int
|
|
|
|
currentLevel int
|
|
|
|
toc *bytes.Buffer
|
|
|
|
|
2011-05-29 11:17:53 +08:00
|
|
|
smartypants *SmartypantsRenderer
|
|
|
|
}
|
|
|
|
|
2011-06-30 01:13:17 +08:00
|
|
|
const (
|
|
|
|
xhtmlClose = " />\n"
|
|
|
|
htmlClose = ">\n"
|
|
|
|
)
|
2011-05-29 11:17:53 +08:00
|
|
|
|
2011-06-30 01:13:17 +08:00
|
|
|
func HtmlRenderer(flags int, title string, css string) Renderer {
|
2011-05-29 11:17:53 +08:00
|
|
|
// configure the rendering engine
|
2011-05-31 11:44:52 +08:00
|
|
|
closeTag := htmlClose
|
2011-05-29 11:17:53 +08:00
|
|
|
if flags&HTML_USE_XHTML != 0 {
|
2011-05-31 11:44:52 +08:00
|
|
|
closeTag = xhtmlClose
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 01:13:17 +08:00
|
|
|
return &Html{
|
2011-06-30 00:08:56 +08:00
|
|
|
flags: flags,
|
|
|
|
closeTag: closeTag,
|
|
|
|
title: title,
|
|
|
|
css: css,
|
2011-05-29 11:17:53 +08:00
|
|
|
|
2011-06-30 00:08:56 +08:00
|
|
|
headerCount: 0,
|
|
|
|
currentLevel: 0,
|
2011-06-30 00:36:56 +08:00
|
|
|
toc: new(bytes.Buffer),
|
2011-05-29 11:17:53 +08:00
|
|
|
|
2011-06-30 00:08:56 +08:00
|
|
|
smartypants: Smartypants(flags),
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-31 01:06:20 +08:00
|
|
|
func attrEscape(out *bytes.Buffer, src []byte) {
|
2011-06-25 09:11:06 +08:00
|
|
|
org := 0
|
|
|
|
for i, ch := range src {
|
2011-06-26 05:02:46 +08: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
|
|
|
|
if ch == '"' {
|
2011-06-25 09:11:06 +08:00
|
|
|
if i > org {
|
|
|
|
// copy all the normal characters since the last escape
|
|
|
|
out.Write(src[org:i])
|
|
|
|
}
|
|
|
|
org = i + 1
|
2011-06-26 05:02:46 +08:00
|
|
|
out.WriteString(""")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ch == '&' {
|
2011-06-25 09:11:06 +08:00
|
|
|
if i > org {
|
|
|
|
out.Write(src[org:i])
|
|
|
|
}
|
|
|
|
org = i + 1
|
2011-06-26 05:02:46 +08:00
|
|
|
out.WriteString("&")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ch == '<' {
|
2011-06-25 09:11:06 +08:00
|
|
|
if i > org {
|
|
|
|
out.Write(src[org:i])
|
|
|
|
}
|
|
|
|
org = i + 1
|
2011-06-26 05:02:46 +08:00
|
|
|
out.WriteString("<")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ch == '>' {
|
2011-06-25 09:11:06 +08:00
|
|
|
if i > org {
|
|
|
|
out.Write(src[org:i])
|
|
|
|
}
|
|
|
|
org = i + 1
|
2011-06-26 05:02:46 +08:00
|
|
|
out.WriteString(">")
|
|
|
|
continue
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
}
|
2011-06-25 09:11:06 +08:00
|
|
|
if org < len(src) {
|
|
|
|
out.Write(src[org:])
|
|
|
|
}
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 01:13:17 +08:00
|
|
|
func (options *Html) Header(out *bytes.Buffer, text func() bool, level int) {
|
2011-06-25 22:20:08 +08:00
|
|
|
marker := out.Len()
|
2011-06-30 05:38:35 +08:00
|
|
|
doubleSpace(out)
|
2011-05-29 11:17:53 +08:00
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
if options.flags&HTML_TOC != 0 {
|
2011-06-30 00:36:56 +08:00
|
|
|
// headerCount is incremented in htmlTocHeader
|
2011-06-30 00:08:56 +08:00
|
|
|
out.WriteString(fmt.Sprintf("<h%d id=\"toc_%d\">", level, options.headerCount))
|
2011-05-29 11:17:53 +08:00
|
|
|
} else {
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString(fmt.Sprintf("<h%d>", level))
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 00:36:56 +08:00
|
|
|
tocMarker := out.Len()
|
2011-06-25 22:20:08 +08:00
|
|
|
if !text() {
|
|
|
|
out.Truncate(marker)
|
|
|
|
return
|
|
|
|
}
|
2011-06-30 00:08:56 +08:00
|
|
|
|
|
|
|
// are we building a table of contents?
|
|
|
|
if options.flags&HTML_TOC != 0 {
|
2011-06-30 01:13:17 +08:00
|
|
|
options.TocHeader(out.Bytes()[tocMarker:], level)
|
2011-06-30 00:08:56 +08:00
|
|
|
}
|
|
|
|
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString(fmt.Sprintf("</h%d>\n", level))
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 01:13:17 +08:00
|
|
|
func (options *Html) BlockHtml(out *bytes.Buffer, text []byte) {
|
2011-06-29 09:46:35 +08:00
|
|
|
if options.flags&HTML_SKIP_HTML != 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-06-30 05:38:35 +08:00
|
|
|
doubleSpace(out)
|
|
|
|
out.Write(text)
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteByte('\n')
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 01:13:17 +08:00
|
|
|
func (options *Html) HRule(out *bytes.Buffer) {
|
2011-06-30 05:38:35 +08:00
|
|
|
doubleSpace(out)
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("<hr")
|
2011-05-31 11:44:52 +08:00
|
|
|
out.WriteString(options.closeTag)
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 01:13:17 +08:00
|
|
|
func (options *Html) BlockCode(out *bytes.Buffer, text []byte, lang string) {
|
2011-06-29 09:46:35 +08:00
|
|
|
if options.flags&HTML_GITHUB_BLOCKCODE != 0 {
|
2011-06-30 01:13:17 +08:00
|
|
|
options.BlockCodeGithub(out, text, lang)
|
2011-06-29 09:46:35 +08:00
|
|
|
} else {
|
2011-06-30 01:13:17 +08:00
|
|
|
options.BlockCodeNormal(out, text, lang)
|
2011-06-29 09:46:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-30 01:13:17 +08:00
|
|
|
func (options *Html) BlockCodeNormal(out *bytes.Buffer, text []byte, lang string) {
|
2011-06-30 05:38:35 +08:00
|
|
|
doubleSpace(out)
|
2011-05-29 11:17:53 +08:00
|
|
|
|
2011-06-30 05:38:35 +08:00
|
|
|
// parse out the language names/classes
|
|
|
|
count := 0
|
|
|
|
for _, elt := range strings.Fields(lang) {
|
|
|
|
if elt[0] == '.' {
|
|
|
|
elt = elt[1:]
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
2011-06-30 05:38:35 +08: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 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 05:38:35 +08:00
|
|
|
if count == 0 {
|
|
|
|
out.WriteString("<pre><code>")
|
|
|
|
} else {
|
|
|
|
out.WriteString("\">")
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 05:38:35 +08:00
|
|
|
attrEscape(out, text)
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("</code></pre>\n")
|
2011-05-29 11:17:53 +08: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-06-30 01:13:17 +08:00
|
|
|
func (options *Html) BlockCodeGithub(out *bytes.Buffer, text []byte, lang string) {
|
2011-06-30 05:38:35 +08:00
|
|
|
doubleSpace(out)
|
2011-05-29 11:17:53 +08:00
|
|
|
|
2011-06-30 05:38:35 +08:00
|
|
|
// parse out the language name
|
|
|
|
count := 0
|
|
|
|
for _, elt := range strings.Fields(lang) {
|
|
|
|
if elt[0] == '.' {
|
|
|
|
elt = elt[1:]
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
2011-06-30 05:38:35 +08:00
|
|
|
if len(elt) == 0 {
|
|
|
|
continue
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
2011-06-30 05:38:35 +08:00
|
|
|
out.WriteString("<pre lang=\"")
|
|
|
|
attrEscape(out, []byte(elt))
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("\"><code>")
|
2011-06-30 05:38:35 +08:00
|
|
|
count++
|
|
|
|
break
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 05:38:35 +08:00
|
|
|
if count == 0 {
|
|
|
|
out.WriteString("<pre><code>")
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 05:38:35 +08:00
|
|
|
attrEscape(out, text)
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("</code></pre>\n")
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-30 01:13:17 +08:00
|
|
|
func (options *Html) BlockQuote(out *bytes.Buffer, text []byte) {
|
2011-07-02 01:19:42 +08:00
|
|
|
doubleSpace(out)
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("<blockquote>\n")
|
|
|
|
out.Write(text)
|
2011-07-02 01:19:42 +08:00
|
|
|
out.WriteString("</blockquote>\n")
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 01:13:17 +08:00
|
|
|
func (options *Html) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) {
|
2011-06-30 05:38:35 +08:00
|
|
|
doubleSpace(out)
|
|
|
|
out.WriteString("<table>\n<thead>\n")
|
2011-05-31 01:06:20 +08:00
|
|
|
out.Write(header)
|
2011-06-30 05:38:35 +08:00
|
|
|
out.WriteString("\n</thead>\n<tbody>\n")
|
2011-05-31 01:06:20 +08:00
|
|
|
out.Write(body)
|
2011-07-02 01:19:42 +08:00
|
|
|
out.WriteString("\n</tbody>\n</table>\n")
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 01:13:17 +08:00
|
|
|
func (options *Html) TableRow(out *bytes.Buffer, text []byte) {
|
2011-06-30 05:38:35 +08:00
|
|
|
doubleSpace(out)
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("<tr>\n")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("\n</tr>")
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 01:13:17 +08:00
|
|
|
func (options *Html) TableCell(out *bytes.Buffer, text []byte, align int) {
|
2011-06-30 05:38:35 +08:00
|
|
|
doubleSpace(out)
|
2011-05-29 11:17:53 +08:00
|
|
|
switch align {
|
|
|
|
case TABLE_ALIGNMENT_LEFT:
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("<td align=\"left\">")
|
2011-05-29 11:17:53 +08:00
|
|
|
case TABLE_ALIGNMENT_RIGHT:
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("<td align=\"right\">")
|
2011-05-29 11:17:53 +08:00
|
|
|
case TABLE_ALIGNMENT_CENTER:
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("<td align=\"center\">")
|
2011-05-29 11:17:53 +08:00
|
|
|
default:
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("<td>")
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-05-31 01:06:20 +08:00
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("</td>")
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 01:13:17 +08:00
|
|
|
func (options *Html) List(out *bytes.Buffer, text func() bool, flags int) {
|
2011-06-26 05:02:46 +08:00
|
|
|
marker := out.Len()
|
2011-06-30 05:38:35 +08:00
|
|
|
doubleSpace(out)
|
2011-06-26 05:02:46 +08:00
|
|
|
|
2011-05-29 11:17:53 +08:00
|
|
|
if flags&LIST_TYPE_ORDERED != 0 {
|
2011-07-02 01:19:42 +08:00
|
|
|
out.WriteString("<ol>")
|
2011-05-29 11:17:53 +08:00
|
|
|
} else {
|
2011-07-02 01:19:42 +08:00
|
|
|
out.WriteString("<ul>")
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
2011-06-26 05:02:46 +08:00
|
|
|
if !text() {
|
|
|
|
out.Truncate(marker)
|
|
|
|
return
|
|
|
|
}
|
2011-05-29 11:17:53 +08:00
|
|
|
if flags&LIST_TYPE_ORDERED != 0 {
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("</ol>\n")
|
2011-05-29 11:17:53 +08:00
|
|
|
} else {
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("</ul>\n")
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-30 01:13:17 +08:00
|
|
|
func (options *Html) ListItem(out *bytes.Buffer, text []byte, flags int) {
|
2011-07-02 01:19:42 +08:00
|
|
|
if flags&LIST_ITEM_CONTAINS_BLOCK != 0 || flags&LIST_ITEM_BEGINNING_OF_LIST != 0 {
|
|
|
|
doubleSpace(out)
|
|
|
|
}
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("<li>")
|
2011-06-30 05:38:35 +08:00
|
|
|
out.Write(text)
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("</li>\n")
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 01:13:17 +08:00
|
|
|
func (options *Html) Paragraph(out *bytes.Buffer, text func() bool) {
|
2011-06-27 07:21:11 +08:00
|
|
|
marker := out.Len()
|
2011-06-30 05:38:35 +08:00
|
|
|
doubleSpace(out)
|
2011-05-29 11:17:53 +08:00
|
|
|
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("<p>")
|
2011-06-27 07:21:11 +08:00
|
|
|
if !text() {
|
|
|
|
out.Truncate(marker)
|
|
|
|
return
|
|
|
|
}
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("</p>\n")
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 03:00:54 +08:00
|
|
|
func (options *Html) AutoLink(out *bytes.Buffer, link []byte, kind int) {
|
2011-05-30 07:00:31 +08:00
|
|
|
if options.flags&HTML_SAFELINK != 0 && !isSafeLink(link) && kind != LINK_TYPE_EMAIL {
|
2011-06-30 05:38:35 +08:00
|
|
|
// mark it but don't link it if it is not a safe link: no smartypants
|
|
|
|
out.WriteString("<tt>")
|
|
|
|
attrEscape(out, link)
|
|
|
|
out.WriteString("</tt>")
|
2011-06-30 03:00:54 +08:00
|
|
|
return
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("<a href=\"")
|
2011-05-29 11:17:53 +08:00
|
|
|
if kind == LINK_TYPE_EMAIL {
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("mailto:")
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
2011-06-25 01:50:03 +08:00
|
|
|
attrEscape(out, link)
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("\">")
|
2011-05-29 11:17:53 +08:00
|
|
|
|
2011-06-30 05:38:35 +08: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-06-01 01:49:49 +08:00
|
|
|
switch {
|
2011-05-31 05:36:31 +08:00
|
|
|
case bytes.HasPrefix(link, []byte("mailto://")):
|
2011-06-30 05:38:35 +08:00
|
|
|
attrEscape(out, link[len("mailto://"):])
|
2011-06-01 01:49:49 +08:00
|
|
|
case bytes.HasPrefix(link, []byte("mailto:")):
|
2011-06-30 05:38:35 +08:00
|
|
|
attrEscape(out, link[len("mailto:"):])
|
2011-05-31 05:36:31 +08:00
|
|
|
default:
|
2011-05-31 01:06:20 +08:00
|
|
|
attrEscape(out, link)
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("</a>")
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 03:00:54 +08:00
|
|
|
func (options *Html) CodeSpan(out *bytes.Buffer, text []byte) {
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("<code>")
|
|
|
|
attrEscape(out, text)
|
|
|
|
out.WriteString("</code>")
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 03:00:54 +08:00
|
|
|
func (options *Html) DoubleEmphasis(out *bytes.Buffer, text []byte) {
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("<strong>")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("</strong>")
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 03:00:54 +08:00
|
|
|
func (options *Html) Emphasis(out *bytes.Buffer, text []byte) {
|
2011-05-29 11:17:53 +08:00
|
|
|
if len(text) == 0 {
|
2011-06-30 03:00:54 +08:00
|
|
|
return
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("<em>")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("</em>")
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 03:00:54 +08:00
|
|
|
func (options *Html) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
|
2011-06-29 09:46:35 +08:00
|
|
|
if options.flags&HTML_SKIP_IMAGES != 0 {
|
2011-06-30 03:00:54 +08:00
|
|
|
return
|
2011-06-29 09:46:35 +08:00
|
|
|
}
|
|
|
|
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("<img src=\"")
|
|
|
|
attrEscape(out, link)
|
|
|
|
out.WriteString("\" alt=\"")
|
2011-05-29 11:17:53 +08:00
|
|
|
if len(alt) > 0 {
|
2011-05-31 01:06:20 +08:00
|
|
|
attrEscape(out, alt)
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
if len(title) > 0 {
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("\" title=\"")
|
|
|
|
attrEscape(out, title)
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteByte('"')
|
2011-05-31 11:44:52 +08:00
|
|
|
out.WriteString(options.closeTag)
|
2011-06-30 03:00:54 +08:00
|
|
|
return
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 03:00:54 +08:00
|
|
|
func (options *Html) LineBreak(out *bytes.Buffer) {
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("<br")
|
2011-05-31 11:44:52 +08:00
|
|
|
out.WriteString(options.closeTag)
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 03:00:54 +08:00
|
|
|
func (options *Html) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
|
2011-06-29 09:46:35 +08:00
|
|
|
if options.flags&HTML_SKIP_LINKS != 0 {
|
2011-06-30 05:38:35 +08: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-30 03:00:54 +08:00
|
|
|
return
|
2011-06-29 09:46:35 +08:00
|
|
|
}
|
2011-05-29 11:17:53 +08:00
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
if options.flags&HTML_SAFELINK != 0 && !isSafeLink(link) {
|
2011-06-30 05:38:35 +08: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-30 03:00:54 +08:00
|
|
|
return
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("<a href=\"")
|
2011-06-25 01:50:03 +08:00
|
|
|
attrEscape(out, link)
|
2011-05-29 11:17:53 +08:00
|
|
|
if len(title) > 0 {
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("\" title=\"")
|
|
|
|
attrEscape(out, title)
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("\">")
|
2011-06-25 01:50:03 +08:00
|
|
|
out.Write(content)
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("</a>")
|
2011-06-30 03:00:54 +08:00
|
|
|
return
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 03:00:54 +08:00
|
|
|
func (options *Html) RawHtmlTag(out *bytes.Buffer, text []byte) {
|
2011-05-30 07:00:31 +08:00
|
|
|
if options.flags&HTML_SKIP_HTML != 0 {
|
2011-06-30 03:00:54 +08:00
|
|
|
return
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
2011-05-30 07:00:31 +08:00
|
|
|
if options.flags&HTML_SKIP_STYLE != 0 && isHtmlTag(text, "style") {
|
2011-06-30 03:00:54 +08:00
|
|
|
return
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
2011-05-30 07:00:31 +08:00
|
|
|
if options.flags&HTML_SKIP_LINKS != 0 && isHtmlTag(text, "a") {
|
2011-06-30 03:00:54 +08:00
|
|
|
return
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
2011-05-30 07:00:31 +08:00
|
|
|
if options.flags&HTML_SKIP_IMAGES != 0 && isHtmlTag(text, "img") {
|
2011-06-30 03:00:54 +08:00
|
|
|
return
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
2011-05-31 01:06:20 +08:00
|
|
|
out.Write(text)
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 03:00:54 +08:00
|
|
|
func (options *Html) TripleEmphasis(out *bytes.Buffer, text []byte) {
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("<strong><em>")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("</em></strong>")
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 03:00:54 +08:00
|
|
|
func (options *Html) StrikeThrough(out *bytes.Buffer, text []byte) {
|
2011-05-31 01:06:20 +08:00
|
|
|
out.WriteString("<del>")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("</del>")
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 01:13:17 +08:00
|
|
|
func (options *Html) Entity(out *bytes.Buffer, entity []byte) {
|
2011-06-30 00:08:56 +08:00
|
|
|
out.Write(entity)
|
|
|
|
}
|
|
|
|
|
2011-06-30 01:13:17 +08:00
|
|
|
func (options *Html) NormalText(out *bytes.Buffer, text []byte) {
|
2011-06-29 09:46:35 +08:00
|
|
|
if options.flags&HTML_USE_SMARTYPANTS != 0 {
|
2011-06-30 01:13:17 +08:00
|
|
|
options.Smartypants(out, text)
|
2011-06-29 09:46:35 +08:00
|
|
|
} else {
|
|
|
|
attrEscape(out, text)
|
|
|
|
}
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 01:13:17 +08: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-30 00:08:56 +08: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 {
|
|
|
|
out.WriteString("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" ")
|
|
|
|
out.WriteString("\"http://www.w3.org/TR/html4/strict.dtd\">\n")
|
|
|
|
out.WriteString("<html>\n")
|
|
|
|
}
|
|
|
|
out.WriteString("<head>\n")
|
|
|
|
out.WriteString(" <title>")
|
2011-06-30 01:13:17 +08:00
|
|
|
options.NormalText(out, []byte(options.title))
|
2011-06-30 00:08:56 +08: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")
|
|
|
|
out.WriteString(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"")
|
|
|
|
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-30 00:36:56 +08:00
|
|
|
|
|
|
|
options.tocMarker = out.Len()
|
2011-06-30 00:08:56 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 01:13:17 +08:00
|
|
|
func (options *Html) DocumentFooter(out *bytes.Buffer) {
|
2011-06-30 00:36:56 +08:00
|
|
|
// finalize and insert the table of contents
|
|
|
|
if options.flags&HTML_TOC != 0 {
|
2011-06-30 01:13:17 +08:00
|
|
|
options.TocFinalize()
|
2011-06-30 00:36:56 +08: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-30 03:24:15 +08:00
|
|
|
// corner case spacing issue
|
|
|
|
if options.flags&HTML_COMPLETE_PAGE != 0 {
|
|
|
|
out.WriteByte('\n')
|
|
|
|
}
|
|
|
|
|
2011-06-30 00:36:56 +08:00
|
|
|
// insert the table of contents
|
|
|
|
out.Write(options.toc.Bytes())
|
|
|
|
|
2011-06-30 03:24:15 +08:00
|
|
|
// corner case spacing issue
|
|
|
|
if options.flags&HTML_COMPLETE_PAGE == 0 && options.flags&HTML_OMIT_CONTENTS == 0 {
|
|
|
|
out.WriteByte('\n')
|
|
|
|
}
|
|
|
|
|
2011-06-30 00:36:56 +08: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 11:17:53 +08:00
|
|
|
}
|
2011-06-30 00:08:56 +08:00
|
|
|
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 01:13:17 +08:00
|
|
|
func (options *Html) TocHeader(text []byte, level int) {
|
2011-06-30 00:36:56 +08:00
|
|
|
for level > options.currentLevel {
|
|
|
|
switch {
|
|
|
|
case bytes.HasSuffix(options.toc.Bytes(), []byte("</li>\n")):
|
2011-06-30 03:24:15 +08:00
|
|
|
// this sublist can nest underneath a header
|
2011-06-30 00:36:56 +08:00
|
|
|
size := options.toc.Len()
|
|
|
|
options.toc.Truncate(size - len("</li>\n"))
|
|
|
|
|
|
|
|
case options.currentLevel > 0:
|
|
|
|
options.toc.WriteString("<li>")
|
|
|
|
}
|
2011-06-30 03:24:15 +08:00
|
|
|
if options.toc.Len() > 0 {
|
|
|
|
options.toc.WriteByte('\n')
|
|
|
|
}
|
|
|
|
options.toc.WriteString("<ul>\n")
|
2011-06-30 00:36:56 +08: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-30 01:13:17 +08:00
|
|
|
func (options *Html) TocFinalize() {
|
2011-06-30 00:08:56 +08:00
|
|
|
for options.currentLevel > 1 {
|
2011-06-30 00:36:56 +08:00
|
|
|
options.toc.WriteString("</ul></li>\n")
|
2011-06-30 00:08:56 +08:00
|
|
|
options.currentLevel--
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 00:08:56 +08:00
|
|
|
if options.currentLevel > 0 {
|
2011-06-30 00:36:56 +08:00
|
|
|
options.toc.WriteString("</ul>\n")
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
func isHtmlTag(tag []byte, tagname string) bool {
|
2011-05-29 11:17:53 +08: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++
|
|
|
|
}
|
|
|
|
|
2011-06-29 06:02:12 +08:00
|
|
|
j := i
|
|
|
|
for ; i < len(tag); i, j = i+1, j+1 {
|
|
|
|
if j >= len(tagname) {
|
2011-05-29 11:17:53 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2011-06-29 06:02:12 +08:00
|
|
|
if tag[i] != tagname[j] {
|
2011-05-29 11:17:53 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == len(tag) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return isspace(tag[i]) || tag[i] == '>'
|
|
|
|
}
|
2011-06-30 05:38:35 +08:00
|
|
|
|
|
|
|
func doubleSpace(out *bytes.Buffer) {
|
|
|
|
if out.Len() > 0 {
|
|
|
|
out.WriteByte('\n')
|
|
|
|
}
|
|
|
|
}
|