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"
|
2016-03-31 02:13:02 +08:00
|
|
|
"io"
|
2014-01-22 07:14:35 +08:00
|
|
|
"regexp"
|
2011-06-30 05:38:35 +08:00
|
|
|
"strings"
|
2011-05-29 11:17:53 +08:00
|
|
|
)
|
|
|
|
|
2016-07-27 15:11:13 +08:00
|
|
|
// HTMLFlags control optional behavior of HTML renderer.
|
2016-04-01 15:49:23 +08:00
|
|
|
type HTMLFlags int
|
2015-10-26 23:29:52 +08:00
|
|
|
|
2016-03-31 18:54:09 +08:00
|
|
|
// HTML renderer configuration options.
|
2011-05-29 11:17:53 +08:00
|
|
|
const (
|
2016-08-10 12:26:37 +08:00
|
|
|
HTMLFlagsNone HTMLFlags = 0
|
|
|
|
SkipHTML HTMLFlags = 1 << iota // Skip preformatted HTML blocks
|
|
|
|
SkipImages // Skip embedded images
|
|
|
|
SkipLinks // Skip all links
|
|
|
|
Safelink // Only link to trusted protocols
|
|
|
|
NofollowLinks // Only link with rel="nofollow"
|
|
|
|
NoreferrerLinks // Only link with rel="noreferrer"
|
|
|
|
HrefTargetBlank // Add a blank target
|
|
|
|
CompletePage // Generate a complete HTML page
|
|
|
|
UseXHTML // Generate XHTML output instead of HTML
|
|
|
|
FootnoteReturnLinks // Generate a link at the end of a footnote to return to the source
|
|
|
|
Smartypants // Enable smart punctuation substitutions
|
|
|
|
SmartypantsFractions // Enable smart fractions (with Smartypants)
|
|
|
|
SmartypantsDashes // Enable smart dashes (with Smartypants)
|
|
|
|
SmartypantsLatexDashes // Enable LaTeX-style dashes (with Smartypants)
|
|
|
|
SmartypantsAngledQuotes // Enable angled double quotes (with Smartypants) for double quotes rendering
|
2017-07-31 23:40:36 +08:00
|
|
|
SmartypantsQuotesNBSP // Enable « French guillemets » (with Smartypants)
|
2016-11-25 03:48:48 +08:00
|
|
|
TOC // Generate a table of contents
|
2016-03-30 16:57:02 +08:00
|
|
|
|
|
|
|
TagName = "[A-Za-z][A-Za-z0-9-]*"
|
|
|
|
AttributeName = "[a-zA-Z_:][a-zA-Z0-9:._-]*"
|
|
|
|
UnquotedValue = "[^\"'=<>`\\x00-\\x20]+"
|
|
|
|
SingleQuotedValue = "'[^']*'"
|
|
|
|
DoubleQuotedValue = "\"[^\"]*\""
|
|
|
|
AttributeValue = "(?:" + UnquotedValue + "|" + SingleQuotedValue + "|" + DoubleQuotedValue + ")"
|
|
|
|
AttributeValueSpec = "(?:" + "\\s*=" + "\\s*" + AttributeValue + ")"
|
|
|
|
Attribute = "(?:" + "\\s+" + AttributeName + AttributeValueSpec + "?)"
|
|
|
|
OpenTag = "<" + TagName + Attribute + "*" + "\\s*/?>"
|
|
|
|
CloseTag = "</" + TagName + "\\s*[>]"
|
|
|
|
HTMLComment = "<!---->|<!--(?:-?[^>-])(?:-?[^-])*-->"
|
|
|
|
ProcessingInstruction = "[<][?].*?[?][>]"
|
|
|
|
Declaration = "<![A-Z]+" + "\\s+[^>]*>"
|
|
|
|
CDATA = "<!\\[CDATA\\[[\\s\\S]*?\\]\\]>"
|
|
|
|
HTMLTag = "(?:" + OpenTag + "|" + CloseTag + "|" + HTMLComment + "|" +
|
|
|
|
ProcessingInstruction + "|" + Declaration + "|" + CDATA + ")"
|
2011-05-29 11:17:53 +08:00
|
|
|
)
|
|
|
|
|
2014-01-22 07:14:35 +08:00
|
|
|
var (
|
2016-04-11 16:28:05 +08:00
|
|
|
htmlTagRe = regexp.MustCompile("(?i)^" + HTMLTag)
|
2014-01-22 07:14:35 +08:00
|
|
|
)
|
|
|
|
|
2016-07-27 15:11:13 +08:00
|
|
|
// HTMLRendererParameters is a collection of supplementary parameters tweaking
|
|
|
|
// the behavior of various parts of HTML renderer.
|
2016-04-01 20:37:21 +08:00
|
|
|
type HTMLRendererParameters struct {
|
2014-05-29 22:17:20 +08:00
|
|
|
// Prepend this text to each relative URL.
|
2014-05-24 15:55:13 +08:00
|
|
|
AbsolutePrefix string
|
2014-05-29 12:52:45 +08:00
|
|
|
// Add this text to each footnote anchor, to ensure uniqueness.
|
2014-05-25 02:29:39 +08: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
|
2017-02-13 01:00:18 +08:00
|
|
|
// If set, add this text to the front of each Heading ID, to ensure
|
Allow configurable header ID prefix/suffixes.
This is specifically driven by the Hugo usecase where multiple documents
are often rendered into the same ultimate HTML page.
When a header ID is written to the output HTML format (either through
`HTML_TOC`, `EXTENSION_HEADER_IDS`, or `EXTENSION_AUTO_HEADER_IDS`), it
is possible that multiple documents will hvae identical header IDs. To
permit validation to pass, it is useful to have a per-document prefix or
suffix (in our case, an MD5 of the content filename, and we will be
using it as a suffix).
That is, two documents (`A` and `B`) that have the same header ID (`#
Reason {#reason}`), will end up having an actual header ID of the form
`#reason-DOCID` (e.g., `#reason-A`, `#reason-B`) with these HTML
parameters.
This is built on top of #126 (more intelligent collision detection for
`EXTENSION_AUTO_HEADER_IDS`).
2014-11-24 09:37:27 +08:00
|
|
|
// uniqueness.
|
2017-02-13 01:00:18 +08:00
|
|
|
HeadingIDPrefix string
|
|
|
|
// If set, add this text to the back of each Heading ID, to ensure uniqueness.
|
|
|
|
HeadingIDSuffix string
|
2016-04-05 19:18:32 +08:00
|
|
|
|
|
|
|
Title string // Document title (used if CompletePage is set)
|
|
|
|
CSS string // Optional CSS file URL (used if CompletePage is set)
|
2016-08-09 20:24:34 +08:00
|
|
|
Icon string // Optional icon file URL (used if CompletePage is set)
|
2016-04-05 19:18:32 +08:00
|
|
|
|
2016-11-25 03:48:48 +08:00
|
|
|
Flags HTMLFlags // Flags allow customizing this renderer's behavior
|
2014-05-24 15:55:13 +08:00
|
|
|
}
|
|
|
|
|
2016-04-11 16:45:40 +08:00
|
|
|
// HTMLRenderer is a type that implements the Renderer interface for HTML output.
|
2011-07-08 01:56:45 +08:00
|
|
|
//
|
2016-04-05 19:18:32 +08:00
|
|
|
// Do not create this directly, instead use the NewHTMLRenderer function.
|
2016-04-11 16:45:40 +08:00
|
|
|
type HTMLRenderer struct {
|
2016-04-05 19:44:14 +08:00
|
|
|
HTMLRendererParameters
|
2014-05-24 15:55:13 +08:00
|
|
|
|
2016-04-05 19:44:14 +08:00
|
|
|
closeTag string // how to end singleton tags: either " />" or ">"
|
2011-06-30 00:08:56 +08:00
|
|
|
|
2017-02-13 01:00:18 +08:00
|
|
|
// Track heading IDs to prevent ID collision in a single generation.
|
|
|
|
headingIDs map[string]int
|
Prevent generated header collisions, less naively.
> This is a rework of an earlier version of this code.
The automatic header ID generation code submitted in #125 has a subtle
bug where it will use the same ID for multiple headers with identical
text. In the case below, all the headers are rendered a `<h1
id="header">Header</h1>`.
```markdown
# Header
# Header
# Header
# Header
```
This change is a simple but robust approach that uses an incrementing
counter and pre-checking to prevent header collision. (The above would
be rendered as `header`, `header-1`, `header-2`, and `header-3`.) In
more complex cases, it will append a new counter suffix (`-1`), like so:
```markdown
# Header
# Header 1
# Header
# Header
```
This will generate `header`, `header-1`, `header-1-1`, and `header-1-2`.
This code has two additional changes over the prior version:
1. Rather than reimplementing @shurcooL’s anchor sanitization code, I
have imported it as from
`github.com/shurcooL/go/github_flavored_markdown/sanitized_anchor_name`.
2. The markdown block parser is now only interested in *generating* a
sanitized anchor name, not with ensuring its uniqueness. That code
has been moved to the HTML renderer. This means that if the HTML
renderer is modified to identify all unique headers prior to
rendering, the hackish nature of the collision detection can be
eliminated.
2014-11-02 06:35:35 +08:00
|
|
|
|
2016-03-30 20:37:03 +08:00
|
|
|
lastOutputLen int
|
|
|
|
disableTags int
|
2016-08-10 12:26:37 +08:00
|
|
|
|
|
|
|
sr *SPRenderer
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 01:13:17 +08:00
|
|
|
const (
|
2015-05-26 03:41:06 +08:00
|
|
|
xhtmlClose = " />"
|
|
|
|
htmlClose = ">"
|
2011-06-30 01:13:17 +08:00
|
|
|
)
|
2011-05-29 11:17:53 +08:00
|
|
|
|
2016-04-11 16:45:40 +08:00
|
|
|
// NewHTMLRenderer creates and configures an HTMLRenderer object, which
|
2016-04-05 19:18:32 +08:00
|
|
|
// satisfies the Renderer interface.
|
2016-07-01 19:25:26 +08:00
|
|
|
func NewHTMLRenderer(params HTMLRendererParameters) *HTMLRenderer {
|
2011-05-29 11:17:53 +08:00
|
|
|
// configure the rendering engine
|
2011-05-31 11:44:52 +08:00
|
|
|
closeTag := htmlClose
|
2016-04-05 19:18:32 +08:00
|
|
|
if params.Flags&UseXHTML != 0 {
|
2011-05-31 11:44:52 +08:00
|
|
|
closeTag = xhtmlClose
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2016-04-05 19:18:32 +08:00
|
|
|
if params.FootnoteReturnLinkContents == "" {
|
|
|
|
params.FootnoteReturnLinkContents = `<sup>[return]</sup>`
|
2014-05-25 02:29:39 +08:00
|
|
|
}
|
|
|
|
|
2016-04-11 16:45:40 +08:00
|
|
|
return &HTMLRenderer{
|
2016-04-05 19:44:14 +08:00
|
|
|
HTMLRendererParameters: params,
|
2011-05-29 11:17:53 +08:00
|
|
|
|
2017-02-13 01:00:18 +08:00
|
|
|
closeTag: closeTag,
|
|
|
|
headingIDs: make(map[string]int),
|
2016-08-10 12:26:37 +08:00
|
|
|
|
|
|
|
sr: NewSmartypantsRenderer(params.Flags),
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 15:11:13 +08:00
|
|
|
func isHTMLTag(tag []byte, tagname string) bool {
|
|
|
|
found, _ := findHTMLTagPos(tag, tagname)
|
2013-04-18 08:15:47 +08:00
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
2014-01-22 06:45:43 +08: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
|
|
|
|
}
|
|
|
|
|
2016-07-27 15:11:13 +08:00
|
|
|
func findHTMLTagPos(tag []byte, tagname string) (bool, int) {
|
2011-05-29 11:17:53 +08:00
|
|
|
i := 0
|
|
|
|
if i < len(tag) && tag[0] != '<' {
|
2013-04-18 08:15:47 +08:00
|
|
|
return false, -1
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
i++
|
2013-04-14 03:26:29 +08:00
|
|
|
i = skipSpace(tag, i)
|
2011-05-29 11:17:53 +08:00
|
|
|
|
|
|
|
if i < len(tag) && tag[i] == '/' {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
2013-04-14 03:26:29 +08:00
|
|
|
i = skipSpace(tag, i)
|
2013-04-14 03:21:47 +08:00
|
|
|
j := 0
|
2011-06-29 06:02:12 +08:00
|
|
|
for ; i < len(tag); i, j = i+1, j+1 {
|
|
|
|
if j >= len(tagname) {
|
2011-05-29 11:17:53 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2013-04-14 03:34:37 +08:00
|
|
|
if strings.ToLower(string(tag[i]))[0] != tagname[j] {
|
2013-04-18 08:15:47 +08:00
|
|
|
return false, -1
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == len(tag) {
|
2013-04-18 08:15:47 +08:00
|
|
|
return false, -1
|
|
|
|
}
|
|
|
|
|
2014-01-22 06:45:43 +08:00
|
|
|
rightAngle := skipUntilCharIgnoreQuotes(tag, i, '>')
|
2016-04-05 17:13:37 +08:00
|
|
|
if rightAngle >= i {
|
2014-01-22 06:45:43 +08:00
|
|
|
return true, rightAngle
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2013-04-18 08:15:47 +08:00
|
|
|
return false, -1
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
2011-06-30 05:38:35 +08:00
|
|
|
|
2013-04-14 03:26:29 +08:00
|
|
|
func skipSpace(tag []byte, i int) int {
|
|
|
|
for i < len(tag) && isspace(tag[i]) {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2014-03-21 10:52:46 +08:00
|
|
|
func isRelativeLink(link []byte) (yes bool) {
|
|
|
|
// a tag begin with '#'
|
|
|
|
if link[0] == '#' {
|
2015-04-11 23:06:30 +08:00
|
|
|
return true
|
2014-03-21 10:52:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// link begin with '/' but not '//', the second maybe a protocol relative link
|
|
|
|
if len(link) >= 2 && link[0] == '/' && link[1] != '/' {
|
2015-04-11 23:06:30 +08:00
|
|
|
return true
|
2014-03-21 10:52:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// only the root '/'
|
|
|
|
if len(link) == 1 && link[0] == '/' {
|
2015-04-11 23:06:30 +08:00
|
|
|
return true
|
2014-03-21 10:52:46 +08:00
|
|
|
}
|
2015-02-20 17:06:55 +08:00
|
|
|
|
|
|
|
// current directory : begin with "./"
|
2015-04-11 23:06:30 +08:00
|
|
|
if bytes.HasPrefix(link, []byte("./")) {
|
|
|
|
return true
|
2015-02-20 17:06:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// parent directory : begin with "../"
|
2015-04-11 23:06:30 +08:00
|
|
|
if bytes.HasPrefix(link, []byte("../")) {
|
|
|
|
return true
|
2015-02-20 17:06:55 +08:00
|
|
|
}
|
|
|
|
|
2015-04-11 23:06:30 +08:00
|
|
|
return false
|
2014-03-21 10:52:46 +08:00
|
|
|
}
|
Prevent generated header collisions, less naively.
> This is a rework of an earlier version of this code.
The automatic header ID generation code submitted in #125 has a subtle
bug where it will use the same ID for multiple headers with identical
text. In the case below, all the headers are rendered a `<h1
id="header">Header</h1>`.
```markdown
# Header
# Header
# Header
# Header
```
This change is a simple but robust approach that uses an incrementing
counter and pre-checking to prevent header collision. (The above would
be rendered as `header`, `header-1`, `header-2`, and `header-3`.) In
more complex cases, it will append a new counter suffix (`-1`), like so:
```markdown
# Header
# Header 1
# Header
# Header
```
This will generate `header`, `header-1`, `header-1-1`, and `header-1-2`.
This code has two additional changes over the prior version:
1. Rather than reimplementing @shurcooL’s anchor sanitization code, I
have imported it as from
`github.com/shurcooL/go/github_flavored_markdown/sanitized_anchor_name`.
2. The markdown block parser is now only interested in *generating* a
sanitized anchor name, not with ensuring its uniqueness. That code
has been moved to the HTML renderer. This means that if the HTML
renderer is modified to identify all unique headers prior to
rendering, the hackish nature of the collision detection can be
eliminated.
2014-11-02 06:35:35 +08:00
|
|
|
|
2017-02-13 01:00:18 +08:00
|
|
|
func (r *HTMLRenderer) ensureUniqueHeadingID(id string) string {
|
|
|
|
for count, found := r.headingIDs[id]; found; count, found = r.headingIDs[id] {
|
Prevent generated header collisions, less naively.
> This is a rework of an earlier version of this code.
The automatic header ID generation code submitted in #125 has a subtle
bug where it will use the same ID for multiple headers with identical
text. In the case below, all the headers are rendered a `<h1
id="header">Header</h1>`.
```markdown
# Header
# Header
# Header
# Header
```
This change is a simple but robust approach that uses an incrementing
counter and pre-checking to prevent header collision. (The above would
be rendered as `header`, `header-1`, `header-2`, and `header-3`.) In
more complex cases, it will append a new counter suffix (`-1`), like so:
```markdown
# Header
# Header 1
# Header
# Header
```
This will generate `header`, `header-1`, `header-1-1`, and `header-1-2`.
This code has two additional changes over the prior version:
1. Rather than reimplementing @shurcooL’s anchor sanitization code, I
have imported it as from
`github.com/shurcooL/go/github_flavored_markdown/sanitized_anchor_name`.
2. The markdown block parser is now only interested in *generating* a
sanitized anchor name, not with ensuring its uniqueness. That code
has been moved to the HTML renderer. This means that if the HTML
renderer is modified to identify all unique headers prior to
rendering, the hackish nature of the collision detection can be
eliminated.
2014-11-02 06:35:35 +08:00
|
|
|
tmp := fmt.Sprintf("%s-%d", id, count+1)
|
|
|
|
|
2017-02-13 01:00:18 +08:00
|
|
|
if _, tmpFound := r.headingIDs[tmp]; !tmpFound {
|
|
|
|
r.headingIDs[id] = count + 1
|
Prevent generated header collisions, less naively.
> This is a rework of an earlier version of this code.
The automatic header ID generation code submitted in #125 has a subtle
bug where it will use the same ID for multiple headers with identical
text. In the case below, all the headers are rendered a `<h1
id="header">Header</h1>`.
```markdown
# Header
# Header
# Header
# Header
```
This change is a simple but robust approach that uses an incrementing
counter and pre-checking to prevent header collision. (The above would
be rendered as `header`, `header-1`, `header-2`, and `header-3`.) In
more complex cases, it will append a new counter suffix (`-1`), like so:
```markdown
# Header
# Header 1
# Header
# Header
```
This will generate `header`, `header-1`, `header-1-1`, and `header-1-2`.
This code has two additional changes over the prior version:
1. Rather than reimplementing @shurcooL’s anchor sanitization code, I
have imported it as from
`github.com/shurcooL/go/github_flavored_markdown/sanitized_anchor_name`.
2. The markdown block parser is now only interested in *generating* a
sanitized anchor name, not with ensuring its uniqueness. That code
has been moved to the HTML renderer. This means that if the HTML
renderer is modified to identify all unique headers prior to
rendering, the hackish nature of the collision detection can be
eliminated.
2014-11-02 06:35:35 +08:00
|
|
|
id = tmp
|
|
|
|
} else {
|
|
|
|
id = id + "-1"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 01:00:18 +08:00
|
|
|
if _, found := r.headingIDs[id]; !found {
|
|
|
|
r.headingIDs[id] = 0
|
Prevent generated header collisions, less naively.
> This is a rework of an earlier version of this code.
The automatic header ID generation code submitted in #125 has a subtle
bug where it will use the same ID for multiple headers with identical
text. In the case below, all the headers are rendered a `<h1
id="header">Header</h1>`.
```markdown
# Header
# Header
# Header
# Header
```
This change is a simple but robust approach that uses an incrementing
counter and pre-checking to prevent header collision. (The above would
be rendered as `header`, `header-1`, `header-2`, and `header-3`.) In
more complex cases, it will append a new counter suffix (`-1`), like so:
```markdown
# Header
# Header 1
# Header
# Header
```
This will generate `header`, `header-1`, `header-1-1`, and `header-1-2`.
This code has two additional changes over the prior version:
1. Rather than reimplementing @shurcooL’s anchor sanitization code, I
have imported it as from
`github.com/shurcooL/go/github_flavored_markdown/sanitized_anchor_name`.
2. The markdown block parser is now only interested in *generating* a
sanitized anchor name, not with ensuring its uniqueness. That code
has been moved to the HTML renderer. This means that if the HTML
renderer is modified to identify all unique headers prior to
rendering, the hackish nature of the collision detection can be
eliminated.
2014-11-02 06:35:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
2016-03-30 16:57:02 +08:00
|
|
|
|
2016-04-11 16:45:40 +08:00
|
|
|
func (r *HTMLRenderer) addAbsPrefix(link []byte) []byte {
|
2016-04-05 19:44:14 +08:00
|
|
|
if r.AbsolutePrefix != "" && isRelativeLink(link) && link[0] != '.' {
|
|
|
|
newDest := r.AbsolutePrefix
|
2016-03-30 16:57:02 +08:00
|
|
|
if link[0] != '/' {
|
|
|
|
newDest += "/"
|
|
|
|
}
|
|
|
|
newDest += string(link)
|
|
|
|
return []byte(newDest)
|
|
|
|
}
|
|
|
|
return link
|
|
|
|
}
|
|
|
|
|
2016-04-01 15:49:23 +08:00
|
|
|
func appendLinkAttrs(attrs []string, flags HTMLFlags, link []byte) []string {
|
2016-03-30 16:57:02 +08:00
|
|
|
if isRelativeLink(link) {
|
|
|
|
return attrs
|
|
|
|
}
|
|
|
|
val := []string{}
|
|
|
|
if flags&NofollowLinks != 0 {
|
|
|
|
val = append(val, "nofollow")
|
|
|
|
}
|
|
|
|
if flags&NoreferrerLinks != 0 {
|
|
|
|
val = append(val, "noreferrer")
|
|
|
|
}
|
|
|
|
if flags&HrefTargetBlank != 0 {
|
|
|
|
attrs = append(attrs, "target=\"_blank\"")
|
|
|
|
}
|
|
|
|
if len(val) == 0 {
|
|
|
|
return attrs
|
|
|
|
}
|
|
|
|
attr := fmt.Sprintf("rel=%q", strings.Join(val, " "))
|
|
|
|
return append(attrs, attr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isMailto(link []byte) bool {
|
|
|
|
return bytes.HasPrefix(link, []byte("mailto:"))
|
|
|
|
}
|
|
|
|
|
2016-04-04 19:08:35 +08:00
|
|
|
func needSkipLink(flags HTMLFlags, dest []byte) bool {
|
|
|
|
if flags&SkipLinks != 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return flags&Safelink != 0 && !isSafeLink(dest) && !isMailto(dest)
|
|
|
|
}
|
|
|
|
|
2016-03-30 16:57:02 +08:00
|
|
|
func isSmartypantable(node *Node) bool {
|
|
|
|
pt := node.Parent.Type
|
|
|
|
return pt != Link && pt != CodeBlock && pt != Code
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendLanguageAttr(attrs []string, info []byte) []string {
|
2016-10-29 17:09:34 +08:00
|
|
|
if len(info) == 0 {
|
|
|
|
return attrs
|
|
|
|
}
|
|
|
|
endOfLang := bytes.IndexAny(info, "\t ")
|
|
|
|
if endOfLang < 0 {
|
|
|
|
endOfLang = len(info)
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
2016-10-29 17:09:34 +08:00
|
|
|
return append(attrs, fmt.Sprintf("class=\"language-%s\"", info[:endOfLang]))
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
|
|
|
|
2016-09-10 17:39:22 +08:00
|
|
|
func (r *HTMLRenderer) tag(w io.Writer, name []byte, attrs []string) {
|
|
|
|
w.Write(name)
|
|
|
|
if len(attrs) > 0 {
|
|
|
|
w.Write(spaceBytes)
|
|
|
|
w.Write([]byte(strings.Join(attrs, " ")))
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
2016-09-10 17:39:22 +08:00
|
|
|
w.Write(gtBytes)
|
|
|
|
r.lastOutputLen = 1
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func footnoteRef(prefix string, node *Node) []byte {
|
|
|
|
urlFrag := prefix + string(slugify(node.Destination))
|
|
|
|
anchor := fmt.Sprintf(`<a rel="footnote" href="#fn:%s">%d</a>`, urlFrag, node.NoteID)
|
|
|
|
return []byte(fmt.Sprintf(`<sup class="footnote-ref" id="fnref:%s">%s</sup>`, urlFrag, anchor))
|
|
|
|
}
|
|
|
|
|
|
|
|
func footnoteItem(prefix string, slug []byte) []byte {
|
|
|
|
return []byte(fmt.Sprintf(`<li id="fn:%s%s">`, prefix, slug))
|
|
|
|
}
|
|
|
|
|
|
|
|
func footnoteReturnLink(prefix, returnLink string, slug []byte) []byte {
|
|
|
|
const format = ` <a class="footnote-return" href="#fnref:%s%s">%s</a>`
|
|
|
|
return []byte(fmt.Sprintf(format, prefix, slug, returnLink))
|
|
|
|
}
|
|
|
|
|
|
|
|
func itemOpenCR(node *Node) bool {
|
|
|
|
if node.Prev == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
ld := node.Parent.ListData
|
2016-04-01 16:21:25 +08:00
|
|
|
return !ld.Tight && ld.ListFlags&ListTypeDefinition == 0
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func skipParagraphTags(node *Node) bool {
|
|
|
|
grandparent := node.Parent.Parent
|
2016-04-01 16:21:25 +08:00
|
|
|
if grandparent == nil || grandparent.Type != List {
|
2016-03-30 16:57:02 +08:00
|
|
|
return false
|
|
|
|
}
|
2016-04-01 16:21:25 +08:00
|
|
|
tightOrTerm := grandparent.Tight || node.Parent.ListFlags&ListTypeTerm != 0
|
2016-03-30 16:57:02 +08:00
|
|
|
return grandparent.Type == List && tightOrTerm
|
|
|
|
}
|
|
|
|
|
2016-04-01 16:44:59 +08:00
|
|
|
func cellAlignment(align CellAlignFlags) string {
|
2016-03-30 16:57:02 +08:00
|
|
|
switch align {
|
|
|
|
case TableAlignmentLeft:
|
|
|
|
return "left"
|
|
|
|
case TableAlignmentRight:
|
|
|
|
return "right"
|
|
|
|
case TableAlignmentCenter:
|
|
|
|
return "center"
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-11 16:45:40 +08:00
|
|
|
func (r *HTMLRenderer) out(w io.Writer, text []byte) {
|
2016-03-30 20:37:03 +08:00
|
|
|
if r.disableTags > 0 {
|
2016-04-11 16:28:05 +08:00
|
|
|
w.Write(htmlTagRe.ReplaceAll(text, []byte{}))
|
2016-03-30 20:37:03 +08:00
|
|
|
} else {
|
2016-03-31 02:13:02 +08:00
|
|
|
w.Write(text)
|
2016-03-30 20:37:03 +08:00
|
|
|
}
|
|
|
|
r.lastOutputLen = len(text)
|
|
|
|
}
|
|
|
|
|
2016-04-11 16:45:40 +08:00
|
|
|
func (r *HTMLRenderer) cr(w io.Writer) {
|
2016-03-30 20:37:03 +08:00
|
|
|
if r.lastOutputLen > 0 {
|
2016-10-08 23:17:00 +08:00
|
|
|
r.out(w, nlBytes)
|
2016-03-30 20:37:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 23:17:00 +08:00
|
|
|
var (
|
|
|
|
nlBytes = []byte{'\n'}
|
|
|
|
gtBytes = []byte{'>'}
|
|
|
|
spaceBytes = []byte{' '}
|
|
|
|
)
|
|
|
|
|
2016-09-10 17:39:22 +08:00
|
|
|
var (
|
|
|
|
brTag = []byte("<br>")
|
|
|
|
brXHTMLTag = []byte("<br />")
|
|
|
|
emTag = []byte("<em>")
|
|
|
|
emCloseTag = []byte("</em>")
|
|
|
|
strongTag = []byte("<strong>")
|
|
|
|
strongCloseTag = []byte("</strong>")
|
|
|
|
delTag = []byte("<del>")
|
|
|
|
delCloseTag = []byte("</del>")
|
|
|
|
ttTag = []byte("<tt>")
|
|
|
|
ttCloseTag = []byte("</tt>")
|
|
|
|
aTag = []byte("<a")
|
|
|
|
aCloseTag = []byte("</a>")
|
|
|
|
preTag = []byte("<pre>")
|
|
|
|
preCloseTag = []byte("</pre>")
|
|
|
|
codeTag = []byte("<code>")
|
|
|
|
codeCloseTag = []byte("</code>")
|
|
|
|
pTag = []byte("<p>")
|
|
|
|
pCloseTag = []byte("</p>")
|
|
|
|
blockquoteTag = []byte("<blockquote>")
|
|
|
|
blockquoteCloseTag = []byte("</blockquote>")
|
|
|
|
hrTag = []byte("<hr>")
|
|
|
|
hrXHTMLTag = []byte("<hr />")
|
|
|
|
ulTag = []byte("<ul>")
|
|
|
|
ulCloseTag = []byte("</ul>")
|
|
|
|
olTag = []byte("<ol>")
|
|
|
|
olCloseTag = []byte("</ol>")
|
|
|
|
dlTag = []byte("<dl>")
|
|
|
|
dlCloseTag = []byte("</dl>")
|
|
|
|
liTag = []byte("<li>")
|
|
|
|
liCloseTag = []byte("</li>")
|
|
|
|
ddTag = []byte("<dd>")
|
|
|
|
ddCloseTag = []byte("</dd>")
|
|
|
|
dtTag = []byte("<dt>")
|
|
|
|
dtCloseTag = []byte("</dt>")
|
|
|
|
tableTag = []byte("<table>")
|
|
|
|
tableCloseTag = []byte("</table>")
|
|
|
|
tdTag = []byte("<td")
|
|
|
|
tdCloseTag = []byte("</td>")
|
|
|
|
thTag = []byte("<th")
|
|
|
|
thCloseTag = []byte("</th>")
|
|
|
|
theadTag = []byte("<thead>")
|
|
|
|
theadCloseTag = []byte("</thead>")
|
|
|
|
tbodyTag = []byte("<tbody>")
|
|
|
|
tbodyCloseTag = []byte("</tbody>")
|
|
|
|
trTag = []byte("<tr>")
|
|
|
|
trCloseTag = []byte("</tr>")
|
|
|
|
h1Tag = []byte("<h1")
|
|
|
|
h1CloseTag = []byte("</h1>")
|
|
|
|
h2Tag = []byte("<h2")
|
|
|
|
h2CloseTag = []byte("</h2>")
|
|
|
|
h3Tag = []byte("<h3")
|
|
|
|
h3CloseTag = []byte("</h3>")
|
|
|
|
h4Tag = []byte("<h4")
|
|
|
|
h4CloseTag = []byte("</h4>")
|
|
|
|
h5Tag = []byte("<h5")
|
|
|
|
h5CloseTag = []byte("</h5>")
|
|
|
|
h6Tag = []byte("<h6")
|
|
|
|
h6CloseTag = []byte("</h6>")
|
|
|
|
|
|
|
|
footnotesDivBytes = []byte("\n<div class=\"footnotes\">\n\n")
|
|
|
|
footnotesCloseDivBytes = []byte("\n</div>\n")
|
|
|
|
)
|
|
|
|
|
2017-02-13 01:00:18 +08:00
|
|
|
func headingTagsFromLevel(level int) ([]byte, []byte) {
|
2016-09-10 17:39:22 +08:00
|
|
|
switch level {
|
|
|
|
case 1:
|
|
|
|
return h1Tag, h1CloseTag
|
|
|
|
case 2:
|
|
|
|
return h2Tag, h2CloseTag
|
|
|
|
case 3:
|
|
|
|
return h3Tag, h3CloseTag
|
|
|
|
case 4:
|
|
|
|
return h4Tag, h4CloseTag
|
|
|
|
case 5:
|
|
|
|
return h5Tag, h5CloseTag
|
|
|
|
default:
|
|
|
|
return h6Tag, h6CloseTag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *HTMLRenderer) outHRTag(w io.Writer) {
|
|
|
|
if r.Flags&UseXHTML == 0 {
|
|
|
|
r.out(w, hrTag)
|
|
|
|
} else {
|
|
|
|
r.out(w, hrXHTMLTag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 15:11:13 +08:00
|
|
|
// RenderNode is a default renderer of a single node of a syntax tree. For
|
|
|
|
// block nodes it will be called twice: first time with entering=true, second
|
|
|
|
// time with entering=false, so that it could know when it's working on an open
|
|
|
|
// tag and when on close. It writes the result to w.
|
|
|
|
//
|
|
|
|
// The return value is a way to tell the calling walker to adjust its walk
|
|
|
|
// pattern: e.g. it can terminate the traversal by returning Terminate. Or it
|
|
|
|
// can ask the walker to skip a subtree of this node by returning SkipChildren.
|
|
|
|
// The typical behavior is to return GoToNext, which asks for the usual
|
|
|
|
// traversal to the next node.
|
2016-04-11 16:45:40 +08:00
|
|
|
func (r *HTMLRenderer) RenderNode(w io.Writer, node *Node, entering bool) WalkStatus {
|
2016-03-30 20:48:43 +08:00
|
|
|
attrs := []string{}
|
|
|
|
switch node.Type {
|
|
|
|
case Text:
|
2016-08-10 12:26:37 +08:00
|
|
|
if r.Flags&Smartypants != 0 {
|
2016-09-10 19:33:37 +08:00
|
|
|
var tmp bytes.Buffer
|
|
|
|
escapeHTML(&tmp, node.Literal)
|
|
|
|
r.sr.Process(w, tmp.Bytes())
|
|
|
|
} else {
|
|
|
|
if node.Parent.Type == Link {
|
|
|
|
escLink(w, node.Literal)
|
|
|
|
} else {
|
|
|
|
escapeHTML(w, node.Literal)
|
|
|
|
}
|
2016-08-10 12:26:37 +08:00
|
|
|
}
|
2016-03-30 20:48:43 +08:00
|
|
|
case Softbreak:
|
2016-10-08 23:17:00 +08:00
|
|
|
r.cr(w)
|
2016-03-30 20:48:43 +08:00
|
|
|
// TODO: make it configurable via out(renderer.softbreak)
|
|
|
|
case Hardbreak:
|
2016-09-10 17:39:22 +08:00
|
|
|
if r.Flags&UseXHTML == 0 {
|
|
|
|
r.out(w, brTag)
|
|
|
|
} else {
|
|
|
|
r.out(w, brXHTMLTag)
|
|
|
|
}
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-03-30 20:48:43 +08:00
|
|
|
case Emph:
|
|
|
|
if entering {
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, emTag)
|
2016-03-30 20:48:43 +08:00
|
|
|
} else {
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, emCloseTag)
|
2016-03-30 20:48:43 +08:00
|
|
|
}
|
|
|
|
case Strong:
|
|
|
|
if entering {
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, strongTag)
|
2016-03-30 20:48:43 +08:00
|
|
|
} else {
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, strongCloseTag)
|
2016-03-30 20:48:43 +08:00
|
|
|
}
|
|
|
|
case Del:
|
|
|
|
if entering {
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, delTag)
|
2016-03-30 20:48:43 +08:00
|
|
|
} else {
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, delCloseTag)
|
2016-03-30 20:48:43 +08:00
|
|
|
}
|
2016-04-01 18:15:47 +08:00
|
|
|
case HTMLSpan:
|
2016-04-05 19:44:14 +08:00
|
|
|
if r.Flags&SkipHTML != 0 {
|
2016-04-05 17:37:02 +08:00
|
|
|
break
|
|
|
|
}
|
2016-03-31 02:13:02 +08:00
|
|
|
r.out(w, node.Literal)
|
2016-03-30 20:48:43 +08:00
|
|
|
case Link:
|
|
|
|
// mark it but don't link it if it is not a safe link: no smartypants
|
|
|
|
dest := node.LinkData.Destination
|
2016-04-05 19:44:14 +08:00
|
|
|
if needSkipLink(r.Flags, dest) {
|
2016-03-30 16:57:02 +08:00
|
|
|
if entering {
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, ttTag)
|
2016-03-30 16:57:02 +08:00
|
|
|
} else {
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, ttCloseTag)
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
2016-03-30 20:48:43 +08:00
|
|
|
} else {
|
2016-03-30 16:57:02 +08:00
|
|
|
if entering {
|
|
|
|
dest = r.addAbsPrefix(dest)
|
2017-02-02 17:52:49 +08:00
|
|
|
var hrefBuf bytes.Buffer
|
|
|
|
hrefBuf.WriteString("href=\"")
|
|
|
|
escLink(&hrefBuf, dest)
|
|
|
|
hrefBuf.WriteByte('"')
|
|
|
|
attrs = append(attrs, hrefBuf.String())
|
2016-03-30 20:48:43 +08:00
|
|
|
if node.NoteID != 0 {
|
2016-04-05 19:44:14 +08:00
|
|
|
r.out(w, footnoteRef(r.FootnoteAnchorPrefix, node))
|
2016-03-30 20:48:43 +08:00
|
|
|
break
|
|
|
|
}
|
2016-04-05 19:44:14 +08:00
|
|
|
attrs = appendLinkAttrs(attrs, r.Flags, dest)
|
2016-03-30 20:48:43 +08:00
|
|
|
if len(node.LinkData.Title) > 0 {
|
2016-09-10 19:33:37 +08:00
|
|
|
var titleBuff bytes.Buffer
|
|
|
|
titleBuff.WriteString("title=\"")
|
|
|
|
escapeHTML(&titleBuff, node.LinkData.Title)
|
|
|
|
titleBuff.WriteByte('"')
|
|
|
|
attrs = append(attrs, titleBuff.String())
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
2016-09-10 17:39:22 +08:00
|
|
|
r.tag(w, aTag, attrs)
|
2016-03-30 16:57:02 +08:00
|
|
|
} else {
|
2016-03-30 20:48:43 +08:00
|
|
|
if node.NoteID != 0 {
|
|
|
|
break
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, aCloseTag)
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
2016-03-30 20:48:43 +08:00
|
|
|
}
|
|
|
|
case Image:
|
2016-04-05 19:44:14 +08:00
|
|
|
if r.Flags&SkipImages != 0 {
|
2016-04-05 16:12:30 +08:00
|
|
|
return SkipChildren
|
|
|
|
}
|
2016-03-30 20:48:43 +08:00
|
|
|
if entering {
|
|
|
|
dest := node.LinkData.Destination
|
|
|
|
dest = r.addAbsPrefix(dest)
|
|
|
|
if r.disableTags == 0 {
|
|
|
|
//if options.safe && potentiallyUnsafe(dest) {
|
2016-03-31 02:13:02 +08:00
|
|
|
//out(w, `<img src="" alt="`)
|
2016-03-30 20:48:43 +08:00
|
|
|
//} else {
|
2016-09-10 19:33:37 +08:00
|
|
|
r.out(w, []byte(`<img src="`))
|
|
|
|
escLink(w, dest)
|
|
|
|
r.out(w, []byte(`" alt="`))
|
2016-03-30 20:48:43 +08:00
|
|
|
//}
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
2016-03-30 20:48:43 +08:00
|
|
|
r.disableTags++
|
|
|
|
} else {
|
|
|
|
r.disableTags--
|
|
|
|
if r.disableTags == 0 {
|
|
|
|
if node.LinkData.Title != nil {
|
2016-03-31 02:13:02 +08:00
|
|
|
r.out(w, []byte(`" title="`))
|
2016-09-10 19:33:37 +08:00
|
|
|
escapeHTML(w, node.LinkData.Title)
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
2016-03-31 02:13:02 +08:00
|
|
|
r.out(w, []byte(`" />`))
|
2016-03-30 20:48:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case Code:
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, codeTag)
|
2016-09-10 19:33:37 +08:00
|
|
|
escapeHTML(w, node.Literal)
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, codeCloseTag)
|
2016-03-30 20:48:43 +08:00
|
|
|
case Document:
|
|
|
|
break
|
|
|
|
case Paragraph:
|
|
|
|
if skipParagraphTags(node) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if entering {
|
|
|
|
// TODO: untangle this clusterfuck about when the newlines need
|
|
|
|
// to be added and when not.
|
|
|
|
if node.Prev != nil {
|
2016-08-10 18:06:50 +08:00
|
|
|
switch node.Prev.Type {
|
2017-02-13 01:00:18 +08:00
|
|
|
case HTMLBlock, List, Paragraph, Heading, CodeBlock, BlockQuote, HorizontalRule:
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
|
|
|
}
|
2016-03-30 20:48:43 +08:00
|
|
|
if node.Parent.Type == BlockQuote && node.Prev == nil {
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, pTag)
|
2016-03-30 20:48:43 +08:00
|
|
|
} else {
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, pCloseTag)
|
2016-03-30 20:48:43 +08:00
|
|
|
if !(node.Parent.Type == Item && node.Next == nil) {
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
2016-03-30 20:48:43 +08:00
|
|
|
}
|
|
|
|
case BlockQuote:
|
|
|
|
if entering {
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, blockquoteTag)
|
2016-03-30 20:48:43 +08:00
|
|
|
} else {
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, blockquoteCloseTag)
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-03-30 20:48:43 +08:00
|
|
|
}
|
2016-04-01 18:15:47 +08:00
|
|
|
case HTMLBlock:
|
2016-04-05 19:44:14 +08:00
|
|
|
if r.Flags&SkipHTML != 0 {
|
2016-04-05 17:37:02 +08:00
|
|
|
break
|
|
|
|
}
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
|
|
|
r.out(w, node.Literal)
|
|
|
|
r.cr(w)
|
2017-02-13 01:00:18 +08:00
|
|
|
case Heading:
|
|
|
|
openTag, closeTag := headingTagsFromLevel(node.Level)
|
2016-03-30 20:48:43 +08:00
|
|
|
if entering {
|
|
|
|
if node.IsTitleblock {
|
|
|
|
attrs = append(attrs, `class="title"`)
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
2017-02-13 01:00:18 +08:00
|
|
|
if node.HeadingID != "" {
|
|
|
|
id := r.ensureUniqueHeadingID(node.HeadingID)
|
|
|
|
if r.HeadingIDPrefix != "" {
|
|
|
|
id = r.HeadingIDPrefix + id
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
2017-02-13 01:00:18 +08:00
|
|
|
if r.HeadingIDSuffix != "" {
|
|
|
|
id = id + r.HeadingIDSuffix
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
2016-03-30 20:48:43 +08:00
|
|
|
attrs = append(attrs, fmt.Sprintf(`id="%s"`, id))
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-09-10 17:39:22 +08:00
|
|
|
r.tag(w, openTag, attrs)
|
2016-03-30 20:48:43 +08:00
|
|
|
} else {
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, closeTag)
|
2016-03-30 20:48:43 +08:00
|
|
|
if !(node.Parent.Type == Item && node.Next == nil) {
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
2016-03-30 20:48:43 +08:00
|
|
|
}
|
|
|
|
case HorizontalRule:
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-09-10 17:39:22 +08:00
|
|
|
r.outHRTag(w)
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-03-30 20:48:43 +08:00
|
|
|
case List:
|
2016-09-10 17:39:22 +08:00
|
|
|
openTag := ulTag
|
|
|
|
closeTag := ulCloseTag
|
2016-04-01 16:21:25 +08:00
|
|
|
if node.ListFlags&ListTypeOrdered != 0 {
|
2016-09-10 17:39:22 +08:00
|
|
|
openTag = olTag
|
|
|
|
closeTag = olCloseTag
|
2016-03-30 20:48:43 +08:00
|
|
|
}
|
2016-04-01 16:21:25 +08:00
|
|
|
if node.ListFlags&ListTypeDefinition != 0 {
|
2016-09-10 17:39:22 +08:00
|
|
|
openTag = dlTag
|
|
|
|
closeTag = dlCloseTag
|
2016-03-30 20:48:43 +08:00
|
|
|
}
|
|
|
|
if entering {
|
2016-09-03 17:39:16 +08:00
|
|
|
if node.IsFootnotesList {
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, footnotesDivBytes)
|
|
|
|
r.outHRTag(w)
|
2016-09-03 17:39:16 +08:00
|
|
|
r.cr(w)
|
|
|
|
}
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-04-01 16:21:25 +08:00
|
|
|
if node.Parent.Type == Item && node.Parent.Parent.Tight {
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
2016-09-10 17:39:22 +08:00
|
|
|
r.tag(w, openTag[:len(openTag)-1], attrs)
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-03-30 20:48:43 +08:00
|
|
|
} else {
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, closeTag)
|
2016-03-31 02:13:02 +08:00
|
|
|
//cr(w)
|
2016-03-30 20:48:43 +08:00
|
|
|
//if node.parent.Type != Item {
|
2016-03-31 02:13:02 +08:00
|
|
|
// cr(w)
|
2016-03-30 20:48:43 +08:00
|
|
|
//}
|
|
|
|
if node.Parent.Type == Item && node.Next != nil {
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
2016-03-30 20:48:43 +08:00
|
|
|
if node.Parent.Type == Document || node.Parent.Type == BlockQuote {
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-03-30 20:48:43 +08:00
|
|
|
}
|
2016-09-03 17:39:16 +08:00
|
|
|
if node.IsFootnotesList {
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, footnotesCloseDivBytes)
|
2016-09-03 17:39:16 +08:00
|
|
|
}
|
2016-03-30 20:48:43 +08:00
|
|
|
}
|
|
|
|
case Item:
|
2016-09-10 17:39:22 +08:00
|
|
|
openTag := liTag
|
|
|
|
closeTag := liCloseTag
|
2016-04-01 16:21:25 +08:00
|
|
|
if node.ListFlags&ListTypeDefinition != 0 {
|
2016-09-10 17:39:22 +08:00
|
|
|
openTag = ddTag
|
|
|
|
closeTag = ddCloseTag
|
2016-03-30 20:48:43 +08:00
|
|
|
}
|
2016-04-01 16:21:25 +08:00
|
|
|
if node.ListFlags&ListTypeTerm != 0 {
|
2016-09-10 17:39:22 +08:00
|
|
|
openTag = dtTag
|
|
|
|
closeTag = dtCloseTag
|
2016-03-30 20:48:43 +08:00
|
|
|
}
|
|
|
|
if entering {
|
|
|
|
if itemOpenCR(node) {
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
2016-03-30 20:48:43 +08:00
|
|
|
if node.ListData.RefLink != nil {
|
|
|
|
slug := slugify(node.ListData.RefLink)
|
2016-04-05 19:44:14 +08:00
|
|
|
r.out(w, footnoteItem(r.FootnoteAnchorPrefix, slug))
|
2016-03-30 20:48:43 +08:00
|
|
|
break
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, openTag)
|
2016-03-30 20:48:43 +08:00
|
|
|
} else {
|
|
|
|
if node.ListData.RefLink != nil {
|
|
|
|
slug := slugify(node.ListData.RefLink)
|
2016-04-05 19:44:14 +08:00
|
|
|
if r.Flags&FootnoteReturnLinks != 0 {
|
|
|
|
r.out(w, footnoteReturnLink(r.FootnoteAnchorPrefix, r.FootnoteReturnLinkContents, slug))
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
|
|
|
}
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, closeTag)
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-03-30 20:48:43 +08:00
|
|
|
}
|
|
|
|
case CodeBlock:
|
|
|
|
attrs = appendLanguageAttr(attrs, node.Info)
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, preTag)
|
|
|
|
r.tag(w, codeTag[:len(codeTag)-1], attrs)
|
2016-09-10 19:33:37 +08:00
|
|
|
escapeHTML(w, node.Literal)
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, codeCloseTag)
|
|
|
|
r.out(w, preCloseTag)
|
2016-03-30 20:48:43 +08:00
|
|
|
if node.Parent.Type != Item {
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-03-30 20:48:43 +08:00
|
|
|
}
|
|
|
|
case Table:
|
|
|
|
if entering {
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, tableTag)
|
2016-03-30 20:48:43 +08:00
|
|
|
} else {
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, tableCloseTag)
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-03-30 20:48:43 +08:00
|
|
|
}
|
|
|
|
case TableCell:
|
2016-09-10 17:39:22 +08:00
|
|
|
openTag := tdTag
|
|
|
|
closeTag := tdCloseTag
|
2016-03-30 20:48:43 +08:00
|
|
|
if node.IsHeader {
|
2016-09-10 17:39:22 +08:00
|
|
|
openTag = thTag
|
|
|
|
closeTag = thCloseTag
|
2016-03-30 20:48:43 +08:00
|
|
|
}
|
|
|
|
if entering {
|
|
|
|
align := cellAlignment(node.Align)
|
|
|
|
if align != "" {
|
|
|
|
attrs = append(attrs, fmt.Sprintf(`align="%s"`, align))
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
2016-03-30 20:48:43 +08:00
|
|
|
if node.Prev == nil {
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
2016-09-10 17:39:22 +08:00
|
|
|
r.tag(w, openTag, attrs)
|
2016-03-30 20:48:43 +08:00
|
|
|
} else {
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, closeTag)
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-03-30 20:48:43 +08:00
|
|
|
}
|
|
|
|
case TableHead:
|
|
|
|
if entering {
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, theadTag)
|
2016-03-30 20:48:43 +08:00
|
|
|
} else {
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, theadCloseTag)
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-03-30 20:48:43 +08:00
|
|
|
}
|
|
|
|
case TableBody:
|
|
|
|
if entering {
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, tbodyTag)
|
2016-03-30 20:48:43 +08:00
|
|
|
// XXX: this is to adhere to a rather silly test. Should fix test.
|
|
|
|
if node.FirstChild == nil {
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
2016-03-30 20:48:43 +08:00
|
|
|
} else {
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, tbodyCloseTag)
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-03-30 16:57:02 +08:00
|
|
|
}
|
2016-03-30 20:48:43 +08:00
|
|
|
case TableRow:
|
|
|
|
if entering {
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, trTag)
|
2016-03-30 20:48:43 +08:00
|
|
|
} else {
|
2016-09-10 17:39:22 +08:00
|
|
|
r.out(w, trCloseTag)
|
2016-03-31 02:13:02 +08:00
|
|
|
r.cr(w)
|
2016-03-30 20:48:43 +08:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("Unknown node type " + node.Type.String())
|
|
|
|
}
|
2016-04-05 16:10:46 +08:00
|
|
|
return GoToNext
|
2016-03-30 20:48:43 +08:00
|
|
|
}
|
|
|
|
|
2017-07-09 20:20:34 +08:00
|
|
|
// RenderHeader writes HTML document preamble and TOC if requested.
|
|
|
|
func (r *HTMLRenderer) RenderHeader(w io.Writer, ast *Node) {
|
|
|
|
r.writeDocumentHeader(w)
|
|
|
|
if r.Flags&TOC != 0 {
|
|
|
|
r.writeTOC(w, ast)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenderFooter writes HTML document footer.
|
|
|
|
func (r *HTMLRenderer) RenderFooter(w io.Writer, ast *Node) {
|
|
|
|
if r.Flags&CompletePage == 0 {
|
|
|
|
return
|
|
|
|
}
|
2017-07-11 00:11:30 +08:00
|
|
|
io.WriteString(w, "\n</body>\n</html>\n")
|
2017-07-09 20:20:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *HTMLRenderer) writeDocumentHeader(w io.Writer) {
|
2016-04-05 19:44:14 +08:00
|
|
|
if r.Flags&CompletePage == 0 {
|
2016-04-04 15:14:49 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ending := ""
|
2016-04-05 19:44:14 +08:00
|
|
|
if r.Flags&UseXHTML != 0 {
|
2017-07-11 00:11:30 +08:00
|
|
|
io.WriteString(w, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" ")
|
|
|
|
io.WriteString(w, "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n")
|
|
|
|
io.WriteString(w, "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n")
|
2016-04-04 15:14:49 +08:00
|
|
|
ending = " /"
|
|
|
|
} else {
|
2017-07-11 00:11:30 +08:00
|
|
|
io.WriteString(w, "<!DOCTYPE html>\n")
|
|
|
|
io.WriteString(w, "<html>\n")
|
2016-04-04 15:14:49 +08:00
|
|
|
}
|
2017-07-11 00:11:30 +08:00
|
|
|
io.WriteString(w, "<head>\n")
|
|
|
|
io.WriteString(w, " <title>")
|
2016-08-10 12:26:37 +08:00
|
|
|
if r.Flags&Smartypants != 0 {
|
2016-09-10 19:33:37 +08:00
|
|
|
r.sr.Process(w, []byte(r.Title))
|
2016-04-04 15:14:49 +08:00
|
|
|
} else {
|
2016-09-10 19:33:37 +08:00
|
|
|
escapeHTML(w, []byte(r.Title))
|
2016-04-04 15:14:49 +08:00
|
|
|
}
|
2017-07-11 00:11:30 +08:00
|
|
|
io.WriteString(w, "</title>\n")
|
|
|
|
io.WriteString(w, " <meta name=\"GENERATOR\" content=\"Blackfriday Markdown Processor v")
|
|
|
|
io.WriteString(w, Version)
|
|
|
|
io.WriteString(w, "\"")
|
|
|
|
io.WriteString(w, ending)
|
|
|
|
io.WriteString(w, ">\n")
|
|
|
|
io.WriteString(w, " <meta charset=\"utf-8\"")
|
|
|
|
io.WriteString(w, ending)
|
|
|
|
io.WriteString(w, ">\n")
|
2016-04-05 19:44:14 +08:00
|
|
|
if r.CSS != "" {
|
2017-07-11 00:11:30 +08:00
|
|
|
io.WriteString(w, " <link rel=\"stylesheet\" type=\"text/css\" href=\"")
|
2016-09-10 19:33:37 +08:00
|
|
|
escapeHTML(w, []byte(r.CSS))
|
2017-07-11 00:11:30 +08:00
|
|
|
io.WriteString(w, "\"")
|
|
|
|
io.WriteString(w, ending)
|
|
|
|
io.WriteString(w, ">\n")
|
2016-04-04 15:14:49 +08:00
|
|
|
}
|
2016-08-09 20:24:34 +08:00
|
|
|
if r.Icon != "" {
|
2017-07-11 00:11:30 +08:00
|
|
|
io.WriteString(w, " <link rel=\"icon\" type=\"image/x-icon\" href=\"")
|
2016-09-10 19:33:37 +08:00
|
|
|
escapeHTML(w, []byte(r.Icon))
|
2017-07-11 00:11:30 +08:00
|
|
|
io.WriteString(w, "\"")
|
|
|
|
io.WriteString(w, ending)
|
|
|
|
io.WriteString(w, ">\n")
|
2016-08-09 20:24:34 +08:00
|
|
|
}
|
2017-07-11 00:11:30 +08:00
|
|
|
io.WriteString(w, "</head>\n")
|
|
|
|
io.WriteString(w, "<body>\n\n")
|
2016-04-04 15:14:49 +08:00
|
|
|
}
|
|
|
|
|
2017-07-09 20:20:34 +08:00
|
|
|
func (r *HTMLRenderer) writeTOC(w io.Writer, ast *Node) {
|
2016-08-09 17:29:22 +08:00
|
|
|
buf := bytes.Buffer{}
|
|
|
|
|
2017-02-13 01:00:18 +08:00
|
|
|
inHeading := false
|
2016-08-09 15:07:44 +08:00
|
|
|
tocLevel := 0
|
2017-02-13 01:00:18 +08:00
|
|
|
headingCount := 0
|
2016-08-09 15:07:44 +08:00
|
|
|
|
|
|
|
ast.Walk(func(node *Node, entering bool) WalkStatus {
|
2017-02-13 01:00:18 +08:00
|
|
|
if node.Type == Heading && !node.HeadingData.IsTitleblock {
|
|
|
|
inHeading = entering
|
2016-08-09 15:07:44 +08:00
|
|
|
if entering {
|
2017-02-13 01:00:18 +08:00
|
|
|
node.HeadingID = fmt.Sprintf("toc_%d", headingCount)
|
2016-08-09 15:07:44 +08:00
|
|
|
if node.Level == tocLevel {
|
2017-07-11 00:11:30 +08:00
|
|
|
buf.WriteString("</li>\n\n<li>")
|
2016-08-09 15:07:44 +08:00
|
|
|
} else if node.Level < tocLevel {
|
|
|
|
for node.Level < tocLevel {
|
|
|
|
tocLevel--
|
2017-07-11 00:11:30 +08:00
|
|
|
buf.WriteString("</li>\n</ul>")
|
2016-08-09 15:07:44 +08:00
|
|
|
}
|
2017-07-11 00:11:30 +08:00
|
|
|
buf.WriteString("</li>\n\n<li>")
|
2016-08-09 15:07:44 +08:00
|
|
|
} else {
|
|
|
|
for node.Level > tocLevel {
|
|
|
|
tocLevel++
|
2017-07-11 00:11:30 +08:00
|
|
|
buf.WriteString("\n<ul>\n<li>")
|
2016-08-09 15:07:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 01:00:18 +08:00
|
|
|
fmt.Fprintf(&buf, `<a href="#toc_%d">`, headingCount)
|
|
|
|
headingCount++
|
2016-08-09 15:07:44 +08:00
|
|
|
} else {
|
2017-07-11 00:11:30 +08:00
|
|
|
buf.WriteString("</a>")
|
2016-08-09 15:07:44 +08:00
|
|
|
}
|
|
|
|
return GoToNext
|
|
|
|
}
|
|
|
|
|
2017-02-13 01:00:18 +08:00
|
|
|
if inHeading {
|
2016-08-09 17:29:22 +08:00
|
|
|
return r.RenderNode(&buf, node, entering)
|
2016-08-09 15:07:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return GoToNext
|
|
|
|
})
|
|
|
|
|
|
|
|
for ; tocLevel > 0; tocLevel-- {
|
2017-07-11 00:11:30 +08:00
|
|
|
buf.WriteString("</li>\n</ul>")
|
2016-08-09 15:07:44 +08:00
|
|
|
}
|
|
|
|
|
2016-08-09 17:29:22 +08:00
|
|
|
if buf.Len() > 0 {
|
2017-07-11 00:11:30 +08:00
|
|
|
io.WriteString(w, "<nav>\n")
|
2016-08-09 17:29:22 +08:00
|
|
|
w.Write(buf.Bytes())
|
2017-07-11 00:11:30 +08:00
|
|
|
io.WriteString(w, "\n\n</nav>\n")
|
2016-08-09 17:29:22 +08:00
|
|
|
}
|
2016-09-10 19:33:37 +08:00
|
|
|
r.lastOutputLen = buf.Len()
|
2016-08-09 15:07:44 +08:00
|
|
|
}
|