2011-05-29 03:17:53 +00:00
|
|
|
//
|
2011-06-28 02:11:32 +00:00
|
|
|
// Blackfriday Markdown Processor
|
|
|
|
// Available at http://github.com/russross/blackfriday
|
|
|
|
//
|
|
|
|
// Copyright © 2011 Russ Ross <russ@russross.com>.
|
2011-06-28 17:30:10 +00:00
|
|
|
// Distributed under the Simplified BSD License.
|
2011-06-28 02:11:32 +00:00
|
|
|
// See README.md for details.
|
2011-05-29 03:17:53 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// HTML rendering backend
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
package blackfriday
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2016-03-30 18:13:02 +00:00
|
|
|
"io"
|
2014-01-21 23:14:35 +00:00
|
|
|
"regexp"
|
2011-06-29 21:38:35 +00:00
|
|
|
"strings"
|
2011-05-29 03:17:53 +00:00
|
|
|
)
|
|
|
|
|
2016-07-27 07:11:13 +00:00
|
|
|
// HTMLFlags control optional behavior of HTML renderer.
|
2016-04-01 07:49:23 +00:00
|
|
|
type HTMLFlags int
|
2015-10-26 15:29:52 +00:00
|
|
|
|
2016-03-31 10:54:09 +00:00
|
|
|
// HTML renderer configuration options.
|
2011-05-29 03:17:53 +00:00
|
|
|
const (
|
2016-08-10 04:26:37 +00: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
|
2016-11-24 19:48:48 +00:00
|
|
|
TOC // Generate a table of contents
|
|
|
|
OmitContents // Skip the main contents (for a standalone table of contents)
|
2016-03-30 08:57:02 +00: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 03:17:53 +00:00
|
|
|
)
|
|
|
|
|
2014-01-21 23:14:35 +00:00
|
|
|
var (
|
2016-04-11 08:28:05 +00:00
|
|
|
htmlTagRe = regexp.MustCompile("(?i)^" + HTMLTag)
|
2014-01-21 23:14:35 +00:00
|
|
|
)
|
|
|
|
|
2016-07-27 07:11:13 +00:00
|
|
|
// HTMLRendererParameters is a collection of supplementary parameters tweaking
|
|
|
|
// the behavior of various parts of HTML renderer.
|
2016-04-01 12:37:21 +00:00
|
|
|
type HTMLRendererParameters struct {
|
2014-05-29 14:17:20 +00:00
|
|
|
// Prepend this text to each relative URL.
|
2014-05-24 07:55:13 +00:00
|
|
|
AbsolutePrefix string
|
2014-05-29 04:52:45 +00:00
|
|
|
// Add this text to each footnote anchor, to ensure uniqueness.
|
2014-05-24 18:29:39 +00: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-12 17:00:18 +00: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 01:37:27 +00:00
|
|
|
// uniqueness.
|
2017-02-12 17:00:18 +00:00
|
|
|
HeadingIDPrefix string
|
|
|
|
// If set, add this text to the back of each Heading ID, to ensure uniqueness.
|
|
|
|
HeadingIDSuffix string
|
2016-04-05 11:18:32 +00:00
|
|
|
|
|
|
|
Title string // Document title (used if CompletePage is set)
|
|
|
|
CSS string // Optional CSS file URL (used if CompletePage is set)
|
2016-08-09 12:24:34 +00:00
|
|
|
Icon string // Optional icon file URL (used if CompletePage is set)
|
2016-04-05 11:18:32 +00:00
|
|
|
|
2016-11-24 19:48:48 +00:00
|
|
|
Flags HTMLFlags // Flags allow customizing this renderer's behavior
|
2014-05-24 07:55:13 +00:00
|
|
|
}
|
|
|
|
|
2016-04-11 08:45:40 +00:00
|
|
|
// HTMLRenderer is a type that implements the Renderer interface for HTML output.
|
2011-07-07 17:56:45 +00:00
|
|
|
//
|
2016-04-05 11:18:32 +00:00
|
|
|
// Do not create this directly, instead use the NewHTMLRenderer function.
|
2016-04-11 08:45:40 +00:00
|
|
|
type HTMLRenderer struct {
|
2016-04-05 11:44:14 +00:00
|
|
|
HTMLRendererParameters
|
2014-05-24 07:55:13 +00:00
|
|
|
|
2016-04-05 11:44:14 +00:00
|
|
|
closeTag string // how to end singleton tags: either " />" or ">"
|
2011-06-29 16:08:56 +00:00
|
|
|
|
2017-02-12 17:00:18 +00: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-01 22:35:35 +00:00
|
|
|
|
2016-03-30 12:37:03 +00:00
|
|
|
lastOutputLen int
|
|
|
|
disableTags int
|
2016-08-10 04:26:37 +00:00
|
|
|
|
|
|
|
sr *SPRenderer
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-06-29 17:13:17 +00:00
|
|
|
const (
|
2015-05-25 19:41:06 +00:00
|
|
|
xhtmlClose = " />"
|
|
|
|
htmlClose = ">"
|
2011-06-29 17:13:17 +00:00
|
|
|
)
|
2011-05-29 03:17:53 +00:00
|
|
|
|
2016-04-11 08:45:40 +00:00
|
|
|
// NewHTMLRenderer creates and configures an HTMLRenderer object, which
|
2016-04-05 11:18:32 +00:00
|
|
|
// satisfies the Renderer interface.
|
2016-07-01 11:25:26 +00:00
|
|
|
func NewHTMLRenderer(params HTMLRendererParameters) *HTMLRenderer {
|
2011-05-29 03:17:53 +00:00
|
|
|
// configure the rendering engine
|
2011-05-31 03:44:52 +00:00
|
|
|
closeTag := htmlClose
|
2016-04-05 11:18:32 +00:00
|
|
|
if params.Flags&UseXHTML != 0 {
|
2011-05-31 03:44:52 +00:00
|
|
|
closeTag = xhtmlClose
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2016-04-05 11:18:32 +00:00
|
|
|
if params.FootnoteReturnLinkContents == "" {
|
|
|
|
params.FootnoteReturnLinkContents = `<sup>[return]</sup>`
|
2014-05-24 18:29:39 +00:00
|
|
|
}
|
|
|
|
|
2016-04-11 08:45:40 +00:00
|
|
|
return &HTMLRenderer{
|
2016-04-05 11:44:14 +00:00
|
|
|
HTMLRendererParameters: params,
|
2011-05-29 03:17:53 +00:00
|
|
|
|
2017-02-12 17:00:18 +00:00
|
|
|
closeTag: closeTag,
|
|
|
|
headingIDs: make(map[string]int),
|
2016-08-10 04:26:37 +00:00
|
|
|
|
|
|
|
sr: NewSmartypantsRenderer(params.Flags),
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 07:11:13 +00:00
|
|
|
func isHTMLTag(tag []byte, tagname string) bool {
|
|
|
|
found, _ := findHTMLTagPos(tag, tagname)
|
2013-04-18 00:15:47 +00:00
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
2014-01-21 22:45:43 +00: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 07:11:13 +00:00
|
|
|
func findHTMLTagPos(tag []byte, tagname string) (bool, int) {
|
2011-05-29 03:17:53 +00:00
|
|
|
i := 0
|
|
|
|
if i < len(tag) && tag[0] != '<' {
|
2013-04-18 00:15:47 +00:00
|
|
|
return false, -1
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
i++
|
2013-04-13 19:26:29 +00:00
|
|
|
i = skipSpace(tag, i)
|
2011-05-29 03:17:53 +00:00
|
|
|
|
|
|
|
if i < len(tag) && tag[i] == '/' {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
2013-04-13 19:26:29 +00:00
|
|
|
i = skipSpace(tag, i)
|
2013-04-13 19:21:47 +00:00
|
|
|
j := 0
|
2011-06-28 22:02:12 +00:00
|
|
|
for ; i < len(tag); i, j = i+1, j+1 {
|
|
|
|
if j >= len(tagname) {
|
2011-05-29 03:17:53 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2013-04-13 19:34:37 +00:00
|
|
|
if strings.ToLower(string(tag[i]))[0] != tagname[j] {
|
2013-04-18 00:15:47 +00:00
|
|
|
return false, -1
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == len(tag) {
|
2013-04-18 00:15:47 +00:00
|
|
|
return false, -1
|
|
|
|
}
|
|
|
|
|
2014-01-21 22:45:43 +00:00
|
|
|
rightAngle := skipUntilCharIgnoreQuotes(tag, i, '>')
|
2016-04-05 09:13:37 +00:00
|
|
|
if rightAngle >= i {
|
2014-01-21 22:45:43 +00:00
|
|
|
return true, rightAngle
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 00:15:47 +00:00
|
|
|
return false, -1
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2011-06-29 21:38:35 +00:00
|
|
|
|
2013-04-13 19:26:29 +00:00
|
|
|
func skipSpace(tag []byte, i int) int {
|
|
|
|
for i < len(tag) && isspace(tag[i]) {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2014-03-21 02:52:46 +00:00
|
|
|
func isRelativeLink(link []byte) (yes bool) {
|
|
|
|
// a tag begin with '#'
|
|
|
|
if link[0] == '#' {
|
2015-04-11 15:06:30 +00:00
|
|
|
return true
|
2014-03-21 02:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// link begin with '/' but not '//', the second maybe a protocol relative link
|
|
|
|
if len(link) >= 2 && link[0] == '/' && link[1] != '/' {
|
2015-04-11 15:06:30 +00:00
|
|
|
return true
|
2014-03-21 02:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// only the root '/'
|
|
|
|
if len(link) == 1 && link[0] == '/' {
|
2015-04-11 15:06:30 +00:00
|
|
|
return true
|
2014-03-21 02:52:46 +00:00
|
|
|
}
|
2015-02-20 09:06:55 +00:00
|
|
|
|
|
|
|
// current directory : begin with "./"
|
2015-04-11 15:06:30 +00:00
|
|
|
if bytes.HasPrefix(link, []byte("./")) {
|
|
|
|
return true
|
2015-02-20 09:06:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// parent directory : begin with "../"
|
2015-04-11 15:06:30 +00:00
|
|
|
if bytes.HasPrefix(link, []byte("../")) {
|
|
|
|
return true
|
2015-02-20 09:06:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-11 15:06:30 +00:00
|
|
|
return false
|
2014-03-21 02:52:46 +00: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-01 22:35:35 +00:00
|
|
|
|
2017-02-12 17:00:18 +00: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-01 22:35:35 +00:00
|
|
|
tmp := fmt.Sprintf("%s-%d", id, count+1)
|
|
|
|
|
2017-02-12 17:00:18 +00: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-01 22:35:35 +00:00
|
|
|
id = tmp
|
|
|
|
} else {
|
|
|
|
id = id + "-1"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-12 17:00:18 +00: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-01 22:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
2016-03-30 08:57:02 +00:00
|
|
|
|
2016-04-11 08:45:40 +00:00
|
|
|
func (r *HTMLRenderer) addAbsPrefix(link []byte) []byte {
|
2016-04-05 11:44:14 +00:00
|
|
|
if r.AbsolutePrefix != "" && isRelativeLink(link) && link[0] != '.' {
|
|
|
|
newDest := r.AbsolutePrefix
|
2016-03-30 08:57:02 +00:00
|
|
|
if link[0] != '/' {
|
|
|
|
newDest += "/"
|
|
|
|
}
|
|
|
|
newDest += string(link)
|
|
|
|
return []byte(newDest)
|
|
|
|
}
|
|
|
|
return link
|
|
|
|
}
|
|
|
|
|
2016-04-01 07:49:23 +00:00
|
|
|
func appendLinkAttrs(attrs []string, flags HTMLFlags, link []byte) []string {
|
2016-03-30 08:57:02 +00: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 11:08:35 +00: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 08:57:02 +00: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 09:09:34 +00:00
|
|
|
if len(info) == 0 {
|
|
|
|
return attrs
|
|
|
|
}
|
|
|
|
endOfLang := bytes.IndexAny(info, "\t ")
|
|
|
|
if endOfLang < 0 {
|
|
|
|
endOfLang = len(info)
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
2016-10-29 09:09:34 +00:00
|
|
|
return append(attrs, fmt.Sprintf("class=\"language-%s\"", info[:endOfLang]))
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
|
|
|
|
2016-09-10 09:39:22 +00: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 08:57:02 +00:00
|
|
|
}
|
2016-09-10 09:39:22 +00:00
|
|
|
w.Write(gtBytes)
|
|
|
|
r.lastOutputLen = 1
|
2016-03-30 08:57:02 +00: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 08:21:25 +00:00
|
|
|
return !ld.Tight && ld.ListFlags&ListTypeDefinition == 0
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func skipParagraphTags(node *Node) bool {
|
|
|
|
grandparent := node.Parent.Parent
|
2016-04-01 08:21:25 +00:00
|
|
|
if grandparent == nil || grandparent.Type != List {
|
2016-03-30 08:57:02 +00:00
|
|
|
return false
|
|
|
|
}
|
2016-04-01 08:21:25 +00:00
|
|
|
tightOrTerm := grandparent.Tight || node.Parent.ListFlags&ListTypeTerm != 0
|
2016-03-30 08:57:02 +00:00
|
|
|
return grandparent.Type == List && tightOrTerm
|
|
|
|
}
|
|
|
|
|
2016-04-01 08:44:59 +00:00
|
|
|
func cellAlignment(align CellAlignFlags) string {
|
2016-03-30 08:57:02 +00:00
|
|
|
switch align {
|
|
|
|
case TableAlignmentLeft:
|
|
|
|
return "left"
|
|
|
|
case TableAlignmentRight:
|
|
|
|
return "right"
|
|
|
|
case TableAlignmentCenter:
|
|
|
|
return "center"
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-11 08:45:40 +00:00
|
|
|
func (r *HTMLRenderer) out(w io.Writer, text []byte) {
|
2016-03-30 12:37:03 +00:00
|
|
|
if r.disableTags > 0 {
|
2016-04-11 08:28:05 +00:00
|
|
|
w.Write(htmlTagRe.ReplaceAll(text, []byte{}))
|
2016-03-30 12:37:03 +00:00
|
|
|
} else {
|
2016-03-30 18:13:02 +00:00
|
|
|
w.Write(text)
|
2016-03-30 12:37:03 +00:00
|
|
|
}
|
|
|
|
r.lastOutputLen = len(text)
|
|
|
|
}
|
|
|
|
|
2016-04-11 08:45:40 +00:00
|
|
|
func (r *HTMLRenderer) cr(w io.Writer) {
|
2016-03-30 12:37:03 +00:00
|
|
|
if r.lastOutputLen > 0 {
|
2016-10-08 15:17:00 +00:00
|
|
|
r.out(w, nlBytes)
|
2016-03-30 12:37:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 15:17:00 +00:00
|
|
|
var (
|
|
|
|
nlBytes = []byte{'\n'}
|
|
|
|
gtBytes = []byte{'>'}
|
|
|
|
spaceBytes = []byte{' '}
|
|
|
|
)
|
|
|
|
|
2016-09-10 09:39:22 +00: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-12 17:00:18 +00:00
|
|
|
func headingTagsFromLevel(level int) ([]byte, []byte) {
|
2016-09-10 09:39:22 +00: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 07:11:13 +00: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 08:45:40 +00:00
|
|
|
func (r *HTMLRenderer) RenderNode(w io.Writer, node *Node, entering bool) WalkStatus {
|
2016-03-30 12:48:43 +00:00
|
|
|
attrs := []string{}
|
|
|
|
switch node.Type {
|
|
|
|
case Text:
|
2016-08-10 04:26:37 +00:00
|
|
|
if r.Flags&Smartypants != 0 {
|
2016-09-10 11:33:37 +00: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 04:26:37 +00:00
|
|
|
}
|
2016-03-30 12:48:43 +00:00
|
|
|
case Softbreak:
|
2016-10-08 15:17:00 +00:00
|
|
|
r.cr(w)
|
2016-03-30 12:48:43 +00:00
|
|
|
// TODO: make it configurable via out(renderer.softbreak)
|
|
|
|
case Hardbreak:
|
2016-09-10 09:39:22 +00:00
|
|
|
if r.Flags&UseXHTML == 0 {
|
|
|
|
r.out(w, brTag)
|
|
|
|
} else {
|
|
|
|
r.out(w, brXHTMLTag)
|
|
|
|
}
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-03-30 12:48:43 +00:00
|
|
|
case Emph:
|
|
|
|
if entering {
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, emTag)
|
2016-03-30 12:48:43 +00:00
|
|
|
} else {
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, emCloseTag)
|
2016-03-30 12:48:43 +00:00
|
|
|
}
|
|
|
|
case Strong:
|
|
|
|
if entering {
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, strongTag)
|
2016-03-30 12:48:43 +00:00
|
|
|
} else {
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, strongCloseTag)
|
2016-03-30 12:48:43 +00:00
|
|
|
}
|
|
|
|
case Del:
|
|
|
|
if entering {
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, delTag)
|
2016-03-30 12:48:43 +00:00
|
|
|
} else {
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, delCloseTag)
|
2016-03-30 12:48:43 +00:00
|
|
|
}
|
2016-04-01 10:15:47 +00:00
|
|
|
case HTMLSpan:
|
2016-04-05 11:44:14 +00:00
|
|
|
if r.Flags&SkipHTML != 0 {
|
2016-04-05 09:37:02 +00:00
|
|
|
break
|
|
|
|
}
|
2016-03-30 18:13:02 +00:00
|
|
|
r.out(w, node.Literal)
|
2016-03-30 12:48:43 +00: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 11:44:14 +00:00
|
|
|
if needSkipLink(r.Flags, dest) {
|
2016-03-30 08:57:02 +00:00
|
|
|
if entering {
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, ttTag)
|
2016-03-30 08:57:02 +00:00
|
|
|
} else {
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, ttCloseTag)
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
2016-03-30 12:48:43 +00:00
|
|
|
} else {
|
2016-03-30 08:57:02 +00:00
|
|
|
if entering {
|
|
|
|
dest = r.addAbsPrefix(dest)
|
2017-02-02 09:52:49 +00:00
|
|
|
var hrefBuf bytes.Buffer
|
|
|
|
hrefBuf.WriteString("href=\"")
|
|
|
|
escLink(&hrefBuf, dest)
|
|
|
|
hrefBuf.WriteByte('"')
|
|
|
|
attrs = append(attrs, hrefBuf.String())
|
2016-03-30 12:48:43 +00:00
|
|
|
if node.NoteID != 0 {
|
2016-04-05 11:44:14 +00:00
|
|
|
r.out(w, footnoteRef(r.FootnoteAnchorPrefix, node))
|
2016-03-30 12:48:43 +00:00
|
|
|
break
|
|
|
|
}
|
2016-04-05 11:44:14 +00:00
|
|
|
attrs = appendLinkAttrs(attrs, r.Flags, dest)
|
2016-03-30 12:48:43 +00:00
|
|
|
if len(node.LinkData.Title) > 0 {
|
2016-09-10 11:33:37 +00:00
|
|
|
var titleBuff bytes.Buffer
|
|
|
|
titleBuff.WriteString("title=\"")
|
|
|
|
escapeHTML(&titleBuff, node.LinkData.Title)
|
|
|
|
titleBuff.WriteByte('"')
|
|
|
|
attrs = append(attrs, titleBuff.String())
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
2016-09-10 09:39:22 +00:00
|
|
|
r.tag(w, aTag, attrs)
|
2016-03-30 08:57:02 +00:00
|
|
|
} else {
|
2016-03-30 12:48:43 +00:00
|
|
|
if node.NoteID != 0 {
|
|
|
|
break
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, aCloseTag)
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
2016-03-30 12:48:43 +00:00
|
|
|
}
|
|
|
|
case Image:
|
2016-04-05 11:44:14 +00:00
|
|
|
if r.Flags&SkipImages != 0 {
|
2016-04-05 08:12:30 +00:00
|
|
|
return SkipChildren
|
|
|
|
}
|
2016-03-30 12:48:43 +00:00
|
|
|
if entering {
|
|
|
|
dest := node.LinkData.Destination
|
|
|
|
dest = r.addAbsPrefix(dest)
|
|
|
|
if r.disableTags == 0 {
|
|
|
|
//if options.safe && potentiallyUnsafe(dest) {
|
2016-03-30 18:13:02 +00:00
|
|
|
//out(w, `<img src="" alt="`)
|
2016-03-30 12:48:43 +00:00
|
|
|
//} else {
|
2016-09-10 11:33:37 +00:00
|
|
|
r.out(w, []byte(`<img src="`))
|
|
|
|
escLink(w, dest)
|
|
|
|
r.out(w, []byte(`" alt="`))
|
2016-03-30 12:48:43 +00:00
|
|
|
//}
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
2016-03-30 12:48:43 +00:00
|
|
|
r.disableTags++
|
|
|
|
} else {
|
|
|
|
r.disableTags--
|
|
|
|
if r.disableTags == 0 {
|
|
|
|
if node.LinkData.Title != nil {
|
2016-03-30 18:13:02 +00:00
|
|
|
r.out(w, []byte(`" title="`))
|
2016-09-10 11:33:37 +00:00
|
|
|
escapeHTML(w, node.LinkData.Title)
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
2016-03-30 18:13:02 +00:00
|
|
|
r.out(w, []byte(`" />`))
|
2016-03-30 12:48:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case Code:
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, codeTag)
|
2016-09-10 11:33:37 +00:00
|
|
|
escapeHTML(w, node.Literal)
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, codeCloseTag)
|
2016-03-30 12:48:43 +00: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 10:06:50 +00:00
|
|
|
switch node.Prev.Type {
|
2017-02-12 17:00:18 +00:00
|
|
|
case HTMLBlock, List, Paragraph, Heading, CodeBlock, BlockQuote, HorizontalRule:
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-30 12:48:43 +00:00
|
|
|
if node.Parent.Type == BlockQuote && node.Prev == nil {
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, pTag)
|
2016-03-30 12:48:43 +00:00
|
|
|
} else {
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, pCloseTag)
|
2016-03-30 12:48:43 +00:00
|
|
|
if !(node.Parent.Type == Item && node.Next == nil) {
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
2016-03-30 12:48:43 +00:00
|
|
|
}
|
|
|
|
case BlockQuote:
|
|
|
|
if entering {
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, blockquoteTag)
|
2016-03-30 12:48:43 +00:00
|
|
|
} else {
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, blockquoteCloseTag)
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-03-30 12:48:43 +00:00
|
|
|
}
|
2016-04-01 10:15:47 +00:00
|
|
|
case HTMLBlock:
|
2016-04-05 11:44:14 +00:00
|
|
|
if r.Flags&SkipHTML != 0 {
|
2016-04-05 09:37:02 +00:00
|
|
|
break
|
|
|
|
}
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
|
|
|
r.out(w, node.Literal)
|
|
|
|
r.cr(w)
|
2017-02-12 17:00:18 +00:00
|
|
|
case Heading:
|
|
|
|
openTag, closeTag := headingTagsFromLevel(node.Level)
|
2016-03-30 12:48:43 +00:00
|
|
|
if entering {
|
|
|
|
if node.IsTitleblock {
|
|
|
|
attrs = append(attrs, `class="title"`)
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
2017-02-12 17:00:18 +00:00
|
|
|
if node.HeadingID != "" {
|
|
|
|
id := r.ensureUniqueHeadingID(node.HeadingID)
|
|
|
|
if r.HeadingIDPrefix != "" {
|
|
|
|
id = r.HeadingIDPrefix + id
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
2017-02-12 17:00:18 +00:00
|
|
|
if r.HeadingIDSuffix != "" {
|
|
|
|
id = id + r.HeadingIDSuffix
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
2016-03-30 12:48:43 +00:00
|
|
|
attrs = append(attrs, fmt.Sprintf(`id="%s"`, id))
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-09-10 09:39:22 +00:00
|
|
|
r.tag(w, openTag, attrs)
|
2016-03-30 12:48:43 +00:00
|
|
|
} else {
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, closeTag)
|
2016-03-30 12:48:43 +00:00
|
|
|
if !(node.Parent.Type == Item && node.Next == nil) {
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
2016-03-30 12:48:43 +00:00
|
|
|
}
|
|
|
|
case HorizontalRule:
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-09-10 09:39:22 +00:00
|
|
|
r.outHRTag(w)
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-03-30 12:48:43 +00:00
|
|
|
case List:
|
2016-09-10 09:39:22 +00:00
|
|
|
openTag := ulTag
|
|
|
|
closeTag := ulCloseTag
|
2016-04-01 08:21:25 +00:00
|
|
|
if node.ListFlags&ListTypeOrdered != 0 {
|
2016-09-10 09:39:22 +00:00
|
|
|
openTag = olTag
|
|
|
|
closeTag = olCloseTag
|
2016-03-30 12:48:43 +00:00
|
|
|
}
|
2016-04-01 08:21:25 +00:00
|
|
|
if node.ListFlags&ListTypeDefinition != 0 {
|
2016-09-10 09:39:22 +00:00
|
|
|
openTag = dlTag
|
|
|
|
closeTag = dlCloseTag
|
2016-03-30 12:48:43 +00:00
|
|
|
}
|
|
|
|
if entering {
|
2016-09-03 09:39:16 +00:00
|
|
|
if node.IsFootnotesList {
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, footnotesDivBytes)
|
|
|
|
r.outHRTag(w)
|
2016-09-03 09:39:16 +00:00
|
|
|
r.cr(w)
|
|
|
|
}
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-04-01 08:21:25 +00:00
|
|
|
if node.Parent.Type == Item && node.Parent.Parent.Tight {
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
2016-09-10 09:39:22 +00:00
|
|
|
r.tag(w, openTag[:len(openTag)-1], attrs)
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-03-30 12:48:43 +00:00
|
|
|
} else {
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, closeTag)
|
2016-03-30 18:13:02 +00:00
|
|
|
//cr(w)
|
2016-03-30 12:48:43 +00:00
|
|
|
//if node.parent.Type != Item {
|
2016-03-30 18:13:02 +00:00
|
|
|
// cr(w)
|
2016-03-30 12:48:43 +00:00
|
|
|
//}
|
|
|
|
if node.Parent.Type == Item && node.Next != nil {
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
2016-03-30 12:48:43 +00:00
|
|
|
if node.Parent.Type == Document || node.Parent.Type == BlockQuote {
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-03-30 12:48:43 +00:00
|
|
|
}
|
2016-09-03 09:39:16 +00:00
|
|
|
if node.IsFootnotesList {
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, footnotesCloseDivBytes)
|
2016-09-03 09:39:16 +00:00
|
|
|
}
|
2016-03-30 12:48:43 +00:00
|
|
|
}
|
|
|
|
case Item:
|
2016-09-10 09:39:22 +00:00
|
|
|
openTag := liTag
|
|
|
|
closeTag := liCloseTag
|
2016-04-01 08:21:25 +00:00
|
|
|
if node.ListFlags&ListTypeDefinition != 0 {
|
2016-09-10 09:39:22 +00:00
|
|
|
openTag = ddTag
|
|
|
|
closeTag = ddCloseTag
|
2016-03-30 12:48:43 +00:00
|
|
|
}
|
2016-04-01 08:21:25 +00:00
|
|
|
if node.ListFlags&ListTypeTerm != 0 {
|
2016-09-10 09:39:22 +00:00
|
|
|
openTag = dtTag
|
|
|
|
closeTag = dtCloseTag
|
2016-03-30 12:48:43 +00:00
|
|
|
}
|
|
|
|
if entering {
|
|
|
|
if itemOpenCR(node) {
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
2016-03-30 12:48:43 +00:00
|
|
|
if node.ListData.RefLink != nil {
|
|
|
|
slug := slugify(node.ListData.RefLink)
|
2016-04-05 11:44:14 +00:00
|
|
|
r.out(w, footnoteItem(r.FootnoteAnchorPrefix, slug))
|
2016-03-30 12:48:43 +00:00
|
|
|
break
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, openTag)
|
2016-03-30 12:48:43 +00:00
|
|
|
} else {
|
|
|
|
if node.ListData.RefLink != nil {
|
|
|
|
slug := slugify(node.ListData.RefLink)
|
2016-04-05 11:44:14 +00:00
|
|
|
if r.Flags&FootnoteReturnLinks != 0 {
|
|
|
|
r.out(w, footnoteReturnLink(r.FootnoteAnchorPrefix, r.FootnoteReturnLinkContents, slug))
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, closeTag)
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-03-30 12:48:43 +00:00
|
|
|
}
|
|
|
|
case CodeBlock:
|
|
|
|
attrs = appendLanguageAttr(attrs, node.Info)
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, preTag)
|
|
|
|
r.tag(w, codeTag[:len(codeTag)-1], attrs)
|
2016-09-10 11:33:37 +00:00
|
|
|
escapeHTML(w, node.Literal)
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, codeCloseTag)
|
|
|
|
r.out(w, preCloseTag)
|
2016-03-30 12:48:43 +00:00
|
|
|
if node.Parent.Type != Item {
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-03-30 12:48:43 +00:00
|
|
|
}
|
|
|
|
case Table:
|
|
|
|
if entering {
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, tableTag)
|
2016-03-30 12:48:43 +00:00
|
|
|
} else {
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, tableCloseTag)
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-03-30 12:48:43 +00:00
|
|
|
}
|
|
|
|
case TableCell:
|
2016-09-10 09:39:22 +00:00
|
|
|
openTag := tdTag
|
|
|
|
closeTag := tdCloseTag
|
2016-03-30 12:48:43 +00:00
|
|
|
if node.IsHeader {
|
2016-09-10 09:39:22 +00:00
|
|
|
openTag = thTag
|
|
|
|
closeTag = thCloseTag
|
2016-03-30 12:48:43 +00:00
|
|
|
}
|
|
|
|
if entering {
|
|
|
|
align := cellAlignment(node.Align)
|
|
|
|
if align != "" {
|
|
|
|
attrs = append(attrs, fmt.Sprintf(`align="%s"`, align))
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
2016-03-30 12:48:43 +00:00
|
|
|
if node.Prev == nil {
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
2016-09-10 09:39:22 +00:00
|
|
|
r.tag(w, openTag, attrs)
|
2016-03-30 12:48:43 +00:00
|
|
|
} else {
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, closeTag)
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-03-30 12:48:43 +00:00
|
|
|
}
|
|
|
|
case TableHead:
|
|
|
|
if entering {
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, theadTag)
|
2016-03-30 12:48:43 +00:00
|
|
|
} else {
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, theadCloseTag)
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-03-30 12:48:43 +00:00
|
|
|
}
|
|
|
|
case TableBody:
|
|
|
|
if entering {
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, tbodyTag)
|
2016-03-30 12:48:43 +00:00
|
|
|
// XXX: this is to adhere to a rather silly test. Should fix test.
|
|
|
|
if node.FirstChild == nil {
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
2016-03-30 12:48:43 +00:00
|
|
|
} else {
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, tbodyCloseTag)
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|
2016-03-30 12:48:43 +00:00
|
|
|
case TableRow:
|
|
|
|
if entering {
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, trTag)
|
2016-03-30 12:48:43 +00:00
|
|
|
} else {
|
2016-09-10 09:39:22 +00:00
|
|
|
r.out(w, trCloseTag)
|
2016-03-30 18:13:02 +00:00
|
|
|
r.cr(w)
|
2016-03-30 12:48:43 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("Unknown node type " + node.Type.String())
|
|
|
|
}
|
2016-04-05 08:10:46 +00:00
|
|
|
return GoToNext
|
2016-03-30 12:48:43 +00:00
|
|
|
}
|
|
|
|
|
2016-08-10 04:26:37 +00:00
|
|
|
func (r *HTMLRenderer) writeDocumentHeader(w *bytes.Buffer) {
|
2016-04-05 11:44:14 +00:00
|
|
|
if r.Flags&CompletePage == 0 {
|
2016-04-04 07:14:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ending := ""
|
2016-04-05 11:44:14 +00:00
|
|
|
if r.Flags&UseXHTML != 0 {
|
2016-04-04 07:14:49 +00:00
|
|
|
w.WriteString("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" ")
|
|
|
|
w.WriteString("\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n")
|
|
|
|
w.WriteString("<html xmlns=\"http://www.w3.org/1999/xhtml\">\n")
|
|
|
|
ending = " /"
|
|
|
|
} else {
|
|
|
|
w.WriteString("<!DOCTYPE html>\n")
|
|
|
|
w.WriteString("<html>\n")
|
|
|
|
}
|
|
|
|
w.WriteString("<head>\n")
|
|
|
|
w.WriteString(" <title>")
|
2016-08-10 04:26:37 +00:00
|
|
|
if r.Flags&Smartypants != 0 {
|
2016-09-10 11:33:37 +00:00
|
|
|
r.sr.Process(w, []byte(r.Title))
|
2016-04-04 07:14:49 +00:00
|
|
|
} else {
|
2016-09-10 11:33:37 +00:00
|
|
|
escapeHTML(w, []byte(r.Title))
|
2016-04-04 07:14:49 +00:00
|
|
|
}
|
|
|
|
w.WriteString("</title>\n")
|
|
|
|
w.WriteString(" <meta name=\"GENERATOR\" content=\"Blackfriday Markdown Processor v")
|
2016-07-27 18:28:41 +00:00
|
|
|
w.WriteString(Version)
|
2016-04-04 07:14:49 +00:00
|
|
|
w.WriteString("\"")
|
|
|
|
w.WriteString(ending)
|
|
|
|
w.WriteString(">\n")
|
|
|
|
w.WriteString(" <meta charset=\"utf-8\"")
|
|
|
|
w.WriteString(ending)
|
|
|
|
w.WriteString(">\n")
|
2016-04-05 11:44:14 +00:00
|
|
|
if r.CSS != "" {
|
2016-04-04 07:14:49 +00:00
|
|
|
w.WriteString(" <link rel=\"stylesheet\" type=\"text/css\" href=\"")
|
2016-09-10 11:33:37 +00:00
|
|
|
escapeHTML(w, []byte(r.CSS))
|
2016-04-04 07:14:49 +00:00
|
|
|
w.WriteString("\"")
|
|
|
|
w.WriteString(ending)
|
|
|
|
w.WriteString(">\n")
|
|
|
|
}
|
2016-08-09 12:24:34 +00:00
|
|
|
if r.Icon != "" {
|
|
|
|
w.WriteString(" <link rel=\"icon\" type=\"image/x-icon\" href=\"")
|
2016-09-10 11:33:37 +00:00
|
|
|
escapeHTML(w, []byte(r.Icon))
|
2016-08-09 12:24:34 +00:00
|
|
|
w.WriteString("\"")
|
|
|
|
w.WriteString(ending)
|
|
|
|
w.WriteString(">\n")
|
|
|
|
}
|
2016-04-04 07:14:49 +00:00
|
|
|
w.WriteString("</head>\n")
|
|
|
|
w.WriteString("<body>\n\n")
|
|
|
|
}
|
|
|
|
|
2016-08-09 07:07:44 +00:00
|
|
|
func (r *HTMLRenderer) writeTOC(w *bytes.Buffer, ast *Node) {
|
2016-08-09 09:29:22 +00:00
|
|
|
buf := bytes.Buffer{}
|
|
|
|
|
2017-02-12 17:00:18 +00:00
|
|
|
inHeading := false
|
2016-08-09 07:07:44 +00:00
|
|
|
tocLevel := 0
|
2017-02-12 17:00:18 +00:00
|
|
|
headingCount := 0
|
2016-08-09 07:07:44 +00:00
|
|
|
|
|
|
|
ast.Walk(func(node *Node, entering bool) WalkStatus {
|
2017-02-12 17:00:18 +00:00
|
|
|
if node.Type == Heading && !node.HeadingData.IsTitleblock {
|
|
|
|
inHeading = entering
|
2016-08-09 07:07:44 +00:00
|
|
|
if entering {
|
2017-02-12 17:00:18 +00:00
|
|
|
node.HeadingID = fmt.Sprintf("toc_%d", headingCount)
|
2016-08-09 07:07:44 +00:00
|
|
|
if node.Level == tocLevel {
|
2016-08-09 09:29:22 +00:00
|
|
|
buf.WriteString("</li>\n\n<li>")
|
2016-08-09 07:07:44 +00:00
|
|
|
} else if node.Level < tocLevel {
|
|
|
|
for node.Level < tocLevel {
|
|
|
|
tocLevel--
|
2016-08-09 09:29:22 +00:00
|
|
|
buf.WriteString("</li>\n</ul>")
|
2016-08-09 07:07:44 +00:00
|
|
|
}
|
2016-08-09 09:29:22 +00:00
|
|
|
buf.WriteString("</li>\n\n<li>")
|
2016-08-09 07:07:44 +00:00
|
|
|
} else {
|
|
|
|
for node.Level > tocLevel {
|
|
|
|
tocLevel++
|
2016-08-09 09:29:22 +00:00
|
|
|
buf.WriteString("\n<ul>\n<li>")
|
2016-08-09 07:07:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-12 17:00:18 +00:00
|
|
|
fmt.Fprintf(&buf, `<a href="#toc_%d">`, headingCount)
|
|
|
|
headingCount++
|
2016-08-09 07:07:44 +00:00
|
|
|
} else {
|
2016-08-09 09:29:22 +00:00
|
|
|
buf.WriteString("</a>")
|
2016-08-09 07:07:44 +00:00
|
|
|
}
|
|
|
|
return GoToNext
|
|
|
|
}
|
|
|
|
|
2017-02-12 17:00:18 +00:00
|
|
|
if inHeading {
|
2016-08-09 09:29:22 +00:00
|
|
|
return r.RenderNode(&buf, node, entering)
|
2016-08-09 07:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return GoToNext
|
|
|
|
})
|
|
|
|
|
|
|
|
for ; tocLevel > 0; tocLevel-- {
|
2016-08-09 09:29:22 +00:00
|
|
|
buf.WriteString("</li>\n</ul>")
|
2016-08-09 07:07:44 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 09:29:22 +00:00
|
|
|
if buf.Len() > 0 {
|
|
|
|
w.WriteString("<nav>\n")
|
|
|
|
w.Write(buf.Bytes())
|
|
|
|
w.WriteString("\n\n</nav>\n")
|
|
|
|
}
|
2016-09-10 11:33:37 +00:00
|
|
|
r.lastOutputLen = buf.Len()
|
2016-08-09 07:07:44 +00:00
|
|
|
}
|
|
|
|
|
2016-04-11 08:45:40 +00:00
|
|
|
func (r *HTMLRenderer) writeDocumentFooter(w *bytes.Buffer) {
|
2016-04-05 11:44:14 +00:00
|
|
|
if r.Flags&CompletePage == 0 {
|
2016-04-04 07:14:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteString("\n</body>\n</html>\n")
|
|
|
|
}
|
|
|
|
|
2016-07-27 07:11:13 +00:00
|
|
|
// Render walks the specified syntax (sub)tree and returns a HTML document.
|
2016-04-11 08:45:40 +00:00
|
|
|
func (r *HTMLRenderer) Render(ast *Node) []byte {
|
2016-03-30 12:48:43 +00:00
|
|
|
//println("render_Blackfriday")
|
|
|
|
//dump(ast)
|
2017-02-02 09:52:49 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
r.writeDocumentHeader(&buf)
|
2016-11-24 19:48:48 +00:00
|
|
|
if r.Flags&TOC != 0 || r.Flags&OmitContents != 0 {
|
2017-02-02 09:52:49 +00:00
|
|
|
r.writeTOC(&buf, ast)
|
2016-11-24 19:48:48 +00:00
|
|
|
if r.Flags&OmitContents != 0 {
|
2017-02-02 09:52:49 +00:00
|
|
|
return buf.Bytes()
|
2016-08-09 09:29:22 +00:00
|
|
|
}
|
2016-08-09 07:07:44 +00:00
|
|
|
}
|
2016-04-05 08:10:46 +00:00
|
|
|
ast.Walk(func(node *Node, entering bool) WalkStatus {
|
2017-02-02 09:52:49 +00:00
|
|
|
return r.RenderNode(&buf, node, entering)
|
2016-03-30 18:13:02 +00:00
|
|
|
})
|
2017-02-02 09:52:49 +00:00
|
|
|
r.writeDocumentFooter(&buf)
|
|
|
|
return buf.Bytes()
|
2016-03-30 08:57:02 +00:00
|
|
|
}
|