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"
|
2014-01-21 23:14:35 +00:00
|
|
|
"regexp"
|
2011-05-29 03:17:53 +00:00
|
|
|
"strconv"
|
2011-06-29 21:38:35 +00:00
|
|
|
"strings"
|
2011-05-29 03:17:53 +00:00
|
|
|
)
|
|
|
|
|
2015-10-26 15:29:52 +00:00
|
|
|
type HtmlFlags int
|
|
|
|
|
2011-07-07 18:05:29 +00:00
|
|
|
// Html renderer configuration options.
|
2011-05-29 03:17:53 +00:00
|
|
|
const (
|
2015-10-26 15:29:52 +00:00
|
|
|
HtmlFlagsNone HtmlFlags = 0
|
|
|
|
SkipHTML HtmlFlags = 1 << iota // Skip preformatted HTML blocks
|
|
|
|
SkipStyle // Skip embedded <style> elements
|
|
|
|
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
|
|
|
|
Toc // Generate a table of contents
|
|
|
|
OmitContents // Skip the main contents (for a standalone table of contents)
|
|
|
|
CompletePage // Generate a complete HTML page
|
|
|
|
UseXHTML // Generate XHTML output instead of HTML
|
|
|
|
UseSmartypants // Enable smart punctuation substitutions
|
|
|
|
SmartypantsFractions // Enable smart fractions (with UseSmartypants)
|
|
|
|
SmartypantsDashes // Enable smart dashes (with UseSmartypants)
|
|
|
|
SmartypantsLatexDashes // Enable LaTeX-style dashes (with UseSmartypants)
|
|
|
|
SmartypantsAngledQuotes // Enable angled double quotes (with UseSmartypants) for double quotes rendering
|
|
|
|
FootnoteReturnLinks // Generate a link at the end of a footnote to return to the source
|
2011-05-29 03:17:53 +00:00
|
|
|
)
|
|
|
|
|
2014-01-21 23:14:35 +00:00
|
|
|
var (
|
2014-04-19 21:59:04 +00:00
|
|
|
alignments = []string{
|
|
|
|
"left",
|
|
|
|
"right",
|
|
|
|
"center",
|
|
|
|
}
|
|
|
|
|
2014-01-26 19:39:38 +00:00
|
|
|
// TODO: improve this regexp to catch all possible entities:
|
|
|
|
htmlEntity = regexp.MustCompile(`&[a-z]{2,5};`)
|
2014-01-21 23:14:35 +00:00
|
|
|
)
|
|
|
|
|
2014-05-24 07:55:13 +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
|
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
|
|
|
// If set, add this text to the front of each Header ID, to ensure
|
|
|
|
// uniqueness.
|
|
|
|
HeaderIDPrefix string
|
|
|
|
// If set, add this text to the back of each Header ID, to ensure uniqueness.
|
|
|
|
HeaderIDSuffix string
|
2014-05-24 07:55:13 +00:00
|
|
|
}
|
|
|
|
|
2011-07-07 17:56:45 +00:00
|
|
|
// Html is a type that implements the Renderer interface for HTML output.
|
|
|
|
//
|
|
|
|
// Do not create this directly, instead use the HtmlRenderer function.
|
2011-06-29 17:13:17 +00:00
|
|
|
type Html struct {
|
2015-10-26 16:16:57 +00:00
|
|
|
flags HtmlFlags
|
2015-05-25 19:41:06 +00:00
|
|
|
closeTag string // how to end singleton tags: either " />" or ">"
|
2014-05-24 07:55:13 +00:00
|
|
|
title string // document title
|
|
|
|
css string // optional css file url (used with HTML_COMPLETE_PAGE)
|
|
|
|
|
|
|
|
parameters HtmlRendererParameters
|
2011-06-29 16:08:56 +00:00
|
|
|
|
|
|
|
// table of contents data
|
2011-06-29 16:36:56 +00:00
|
|
|
tocMarker int
|
2011-06-29 16:08:56 +00:00
|
|
|
headerCount int
|
|
|
|
currentLevel int
|
|
|
|
toc *bytes.Buffer
|
|
|
|
|
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
|
|
|
// Track header IDs to prevent ID collision in a single generation.
|
|
|
|
headerIDs map[string]int
|
|
|
|
|
2011-07-07 17:56:45 +00:00
|
|
|
smartypants *smartypantsRenderer
|
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
|
|
|
|
2011-07-07 17:56:45 +00:00
|
|
|
// HtmlRenderer creates and configures an Html object, which
|
|
|
|
// satisfies the Renderer interface.
|
|
|
|
//
|
2015-10-26 16:16:57 +00:00
|
|
|
// flags is a set of HtmlFlags ORed together.
|
2011-07-07 17:56:45 +00:00
|
|
|
// title is the title of the document, and css is a URL for the document's
|
|
|
|
// stylesheet.
|
|
|
|
// title and css are only used when HTML_COMPLETE_PAGE is selected.
|
2015-10-26 16:16:57 +00:00
|
|
|
func HtmlRenderer(flags HtmlFlags, title string, css string) Renderer {
|
2014-05-24 07:55:13 +00:00
|
|
|
return HtmlRendererWithParameters(flags, title, css, HtmlRendererParameters{})
|
|
|
|
}
|
|
|
|
|
2015-10-26 16:16:57 +00:00
|
|
|
func HtmlRendererWithParameters(flags HtmlFlags, title string,
|
2014-05-24 07:55:13 +00:00
|
|
|
css string, renderParameters HtmlRendererParameters) Renderer {
|
2011-05-29 03:17:53 +00:00
|
|
|
// configure the rendering engine
|
2011-05-31 03:44:52 +00:00
|
|
|
closeTag := htmlClose
|
2015-10-26 16:16:57 +00:00
|
|
|
if flags&UseXHTML != 0 {
|
2011-05-31 03:44:52 +00:00
|
|
|
closeTag = xhtmlClose
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2014-05-24 18:29:39 +00:00
|
|
|
if renderParameters.FootnoteReturnLinkContents == "" {
|
|
|
|
renderParameters.FootnoteReturnLinkContents = `<sup>[return]</sup>`
|
|
|
|
}
|
|
|
|
|
2011-06-29 17:13:17 +00:00
|
|
|
return &Html{
|
2014-05-24 07:55:13 +00:00
|
|
|
flags: flags,
|
|
|
|
closeTag: closeTag,
|
|
|
|
title: title,
|
|
|
|
css: css,
|
|
|
|
parameters: renderParameters,
|
2011-05-29 03:17:53 +00:00
|
|
|
|
2011-06-29 16:08:56 +00:00
|
|
|
headerCount: 0,
|
|
|
|
currentLevel: 0,
|
2011-06-29 16:36:56 +00:00
|
|
|
toc: new(bytes.Buffer),
|
2011-05-29 03:17:53 +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
|
|
|
headerIDs: make(map[string]int),
|
|
|
|
|
2011-07-07 17:56:45 +00:00
|
|
|
smartypants: smartypants(flags),
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-26 19:27:34 +00:00
|
|
|
// Using if statements is a bit faster than a switch statement. As the compiler
|
|
|
|
// improves, this should be unnecessary this is only worthwhile because
|
|
|
|
// attrEscape is the single largest CPU user in normal use.
|
|
|
|
// Also tried using map, but that gave a ~3x slowdown.
|
|
|
|
func escapeSingleChar(char byte) (string, bool) {
|
|
|
|
if char == '"' {
|
|
|
|
return """, true
|
|
|
|
}
|
|
|
|
if char == '&' {
|
|
|
|
return "&", true
|
|
|
|
}
|
|
|
|
if char == '<' {
|
|
|
|
return "<", true
|
|
|
|
}
|
|
|
|
if char == '>' {
|
|
|
|
return ">", true
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func attrEscape(src []byte) {
|
2011-06-25 01:11:06 +00:00
|
|
|
org := 0
|
|
|
|
for i, ch := range src {
|
2014-01-26 19:27:34 +00:00
|
|
|
if entity, ok := escapeSingleChar(ch); ok {
|
2011-06-25 01:11:06 +00:00
|
|
|
if i > org {
|
|
|
|
// copy all the normal characters since the last escape
|
|
|
|
out.Write(src[org:i])
|
|
|
|
}
|
|
|
|
org = i + 1
|
2014-01-26 19:27:34 +00:00
|
|
|
out.WriteString(entity)
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-25 01:11:06 +00:00
|
|
|
if org < len(src) {
|
|
|
|
out.Write(src[org:])
|
|
|
|
}
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func entityEscapeWithSkip(src []byte, skipRanges [][]int) {
|
2014-01-26 19:39:38 +00:00
|
|
|
end := 0
|
|
|
|
for _, rang := range skipRanges {
|
|
|
|
attrEscape(out, src[end:rang[0]])
|
|
|
|
out.Write(src[rang[0]:rang[1]])
|
|
|
|
end = rang[1]
|
|
|
|
}
|
|
|
|
attrEscape(out, src[end:])
|
|
|
|
}
|
|
|
|
|
2015-10-26 18:42:41 +00:00
|
|
|
func (r *Html) GetFlags() HtmlFlags {
|
|
|
|
return r.flags
|
2014-01-21 23:14:35 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) TitleBlock(text []byte) {
|
2014-08-02 02:54:21 +00:00
|
|
|
text = bytes.TrimPrefix(text, []byte("% "))
|
|
|
|
text = bytes.Replace(text, []byte("\n% "), []byte("\n"), -1)
|
|
|
|
out.WriteString("<h1 class=\"title\">")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("\n</h1>")
|
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) BeginHeader(level int, id string) int {
|
2011-06-29 21:38:35 +00:00
|
|
|
doubleSpace(out)
|
2011-05-29 03:17:53 +00:00
|
|
|
|
2015-10-26 18:42:41 +00:00
|
|
|
if id == "" && r.flags&Toc != 0 {
|
|
|
|
id = fmt.Sprintf("toc_%d", r.headerCount)
|
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
|
|
|
}
|
|
|
|
|
2014-04-05 19:42:58 +00:00
|
|
|
if id != "" {
|
2015-10-26 18:42:41 +00:00
|
|
|
id = r.ensureUniqueHeaderID(id)
|
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
|
|
|
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.parameters.HeaderIDPrefix != "" {
|
|
|
|
id = r.parameters.HeaderIDPrefix + id
|
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
|
|
|
}
|
|
|
|
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.parameters.HeaderIDSuffix != "" {
|
|
|
|
id = id + r.parameters.HeaderIDSuffix
|
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
|
|
|
}
|
|
|
|
|
|
|
|
out.WriteString(fmt.Sprintf("<h%d id=\"%s\">", level, id))
|
2011-05-29 03:17:53 +00:00
|
|
|
} else {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString(fmt.Sprintf("<h%d>", level))
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 18:25:23 +00:00
|
|
|
return out.Len()
|
|
|
|
}
|
2011-06-29 16:08:56 +00:00
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) EndHeader(level int, id string, tocMarker int) {
|
2011-06-29 16:08:56 +00:00
|
|
|
// are we building a table of contents?
|
2015-10-26 18:25:23 +00:00
|
|
|
if r.flags&Toc != 0 {
|
|
|
|
r.TocHeaderWithAnchor(out.Bytes()[tocMarker:], level, id)
|
2011-06-29 16:08:56 +00:00
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString(fmt.Sprintf("</h%d>\n", level))
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) BlockHtml(text []byte) {
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.flags&SkipHTML != 0 {
|
2011-06-29 01:46:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-06-29 21:38:35 +00:00
|
|
|
doubleSpace(out)
|
2014-01-21 23:14:35 +00:00
|
|
|
out.Write(text)
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteByte('\n')
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) HRule() {
|
2011-06-29 21:38:35 +00:00
|
|
|
doubleSpace(out)
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<hr")
|
2015-10-26 18:42:41 +00:00
|
|
|
out.WriteString(r.closeTag)
|
2015-05-25 19:41:06 +00:00
|
|
|
out.WriteByte('\n')
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) BlockCode(text []byte, lang string) {
|
2011-06-29 21:38:35 +00:00
|
|
|
doubleSpace(out)
|
2011-05-29 03:17:53 +00:00
|
|
|
|
2011-06-29 21:38:35 +00:00
|
|
|
// parse out the language names/classes
|
|
|
|
count := 0
|
|
|
|
for _, elt := range strings.Fields(lang) {
|
|
|
|
if elt[0] == '.' {
|
|
|
|
elt = elt[1:]
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2011-06-29 21:38:35 +00:00
|
|
|
if len(elt) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if count == 0 {
|
2014-08-28 03:26:20 +00:00
|
|
|
out.WriteString("<pre><code class=\"language-")
|
2011-06-29 21:38:35 +00:00
|
|
|
} else {
|
|
|
|
out.WriteByte(' ')
|
|
|
|
}
|
|
|
|
attrEscape(out, []byte(elt))
|
|
|
|
count++
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-06-29 21:38:35 +00:00
|
|
|
if count == 0 {
|
|
|
|
out.WriteString("<pre><code>")
|
|
|
|
} else {
|
|
|
|
out.WriteString("\">")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-06-29 21:38:35 +00:00
|
|
|
attrEscape(out, text)
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("</code></pre>\n")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) BlockQuote(text []byte) {
|
2011-07-01 17:19:42 +00:00
|
|
|
doubleSpace(out)
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<blockquote>\n")
|
|
|
|
out.Write(text)
|
2011-07-01 17:19:42 +00:00
|
|
|
out.WriteString("</blockquote>\n")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) Table(header []byte, body []byte, columnData []int) {
|
2011-06-29 21:38:35 +00:00
|
|
|
doubleSpace(out)
|
|
|
|
out.WriteString("<table>\n<thead>\n")
|
2011-05-30 17:06:20 +00:00
|
|
|
out.Write(header)
|
2011-07-01 20:13:26 +00:00
|
|
|
out.WriteString("</thead>\n\n<tbody>\n")
|
2011-05-30 17:06:20 +00:00
|
|
|
out.Write(body)
|
2011-07-01 20:13:26 +00:00
|
|
|
out.WriteString("</tbody>\n</table>\n")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) TableRow(text []byte) {
|
2011-06-29 21:38:35 +00:00
|
|
|
doubleSpace(out)
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<tr>\n")
|
|
|
|
out.Write(text)
|
2011-07-01 20:13:26 +00:00
|
|
|
out.WriteString("\n</tr>\n")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 18:42:41 +00:00
|
|
|
func (r *Html) TableHeaderCell(out *bytes.Buffer, text []byte, align int) {
|
2013-10-16 10:36:33 +00:00
|
|
|
doubleSpace(out)
|
|
|
|
switch align {
|
2015-10-26 16:16:57 +00:00
|
|
|
case TableAlignmentLeft:
|
2013-10-16 10:36:33 +00:00
|
|
|
out.WriteString("<th align=\"left\">")
|
2015-10-26 16:16:57 +00:00
|
|
|
case TableAlignmentRight:
|
2013-10-16 10:36:33 +00:00
|
|
|
out.WriteString("<th align=\"right\">")
|
2015-10-26 16:16:57 +00:00
|
|
|
case TableAlignmentCenter:
|
2013-10-16 10:36:33 +00:00
|
|
|
out.WriteString("<th align=\"center\">")
|
|
|
|
default:
|
|
|
|
out.WriteString("<th>")
|
|
|
|
}
|
|
|
|
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("</th>")
|
|
|
|
}
|
|
|
|
|
2015-10-26 18:42:41 +00:00
|
|
|
func (r *Html) TableCell(out *bytes.Buffer, text []byte, align int) {
|
2011-06-29 21:38:35 +00:00
|
|
|
doubleSpace(out)
|
2011-05-29 03:17:53 +00:00
|
|
|
switch align {
|
2015-10-26 16:16:57 +00:00
|
|
|
case TableAlignmentLeft:
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<td align=\"left\">")
|
2015-10-26 16:16:57 +00:00
|
|
|
case TableAlignmentRight:
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<td align=\"right\">")
|
2015-10-26 16:16:57 +00:00
|
|
|
case TableAlignmentCenter:
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<td align=\"center\">")
|
2011-05-29 03:17:53 +00:00
|
|
|
default:
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<td>")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("</td>")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) BeginFootnotes() {
|
2013-06-25 01:18:47 +00:00
|
|
|
out.WriteString("<div class=\"footnotes\">\n")
|
2015-11-01 19:57:30 +00:00
|
|
|
r.HRule()
|
|
|
|
r.BeginList(ListTypeOrdered)
|
2015-10-26 18:39:08 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) EndFootnotes() {
|
2015-11-01 19:57:30 +00:00
|
|
|
r.EndList(ListTypeOrdered)
|
2013-06-25 01:18:47 +00:00
|
|
|
out.WriteString("</div>\n")
|
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) FootnoteItem(name, text []byte, flags ListType) {
|
2015-10-26 16:16:57 +00:00
|
|
|
if flags&ListItemContainsBlock != 0 || flags&ListItemBeginningOfList != 0 {
|
2013-06-26 15:57:51 +00:00
|
|
|
doubleSpace(out)
|
|
|
|
}
|
2014-05-24 18:29:39 +00:00
|
|
|
slug := slugify(name)
|
2014-05-24 07:55:13 +00:00
|
|
|
out.WriteString(`<li id="`)
|
|
|
|
out.WriteString(`fn:`)
|
2015-10-26 18:42:41 +00:00
|
|
|
out.WriteString(r.parameters.FootnoteAnchorPrefix)
|
2014-05-24 18:29:39 +00:00
|
|
|
out.Write(slug)
|
2013-06-26 15:57:51 +00:00
|
|
|
out.WriteString(`">`)
|
|
|
|
out.Write(text)
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.flags&FootnoteReturnLinks != 0 {
|
2014-05-24 18:29:39 +00:00
|
|
|
out.WriteString(` <a class="footnote-return" href="#`)
|
|
|
|
out.WriteString(`fnref:`)
|
2015-10-26 18:42:41 +00:00
|
|
|
out.WriteString(r.parameters.FootnoteAnchorPrefix)
|
2014-05-24 18:29:39 +00:00
|
|
|
out.Write(slug)
|
|
|
|
out.WriteString(`">`)
|
2015-10-26 18:42:41 +00:00
|
|
|
out.WriteString(r.parameters.FootnoteReturnLinkContents)
|
2014-05-24 18:29:39 +00:00
|
|
|
out.WriteString(`</a>`)
|
|
|
|
}
|
2013-06-26 15:57:51 +00:00
|
|
|
out.WriteString("</li>\n")
|
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) BeginList(flags ListType) {
|
2011-06-29 21:38:35 +00:00
|
|
|
doubleSpace(out)
|
2011-06-25 21:02:46 +00:00
|
|
|
|
2015-10-26 16:16:57 +00:00
|
|
|
if flags&ListTypeDefinition != 0 {
|
2015-05-29 11:30:49 +00:00
|
|
|
out.WriteString("<dl>")
|
2015-10-26 16:16:57 +00:00
|
|
|
} else if flags&ListTypeOrdered != 0 {
|
2011-07-01 17:19:42 +00:00
|
|
|
out.WriteString("<ol>")
|
2011-05-29 03:17:53 +00:00
|
|
|
} else {
|
2011-07-01 17:19:42 +00:00
|
|
|
out.WriteString("<ul>")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2015-10-26 18:32:33 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) EndList(flags ListType) {
|
2015-10-26 16:16:57 +00:00
|
|
|
if flags&ListTypeDefinition != 0 {
|
2015-05-29 11:30:49 +00:00
|
|
|
out.WriteString("</dl>\n")
|
2015-10-26 16:16:57 +00:00
|
|
|
} else if flags&ListTypeOrdered != 0 {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("</ol>\n")
|
2011-05-29 03:17:53 +00:00
|
|
|
} else {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("</ul>\n")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) ListItem(text []byte, flags ListType) {
|
2015-10-26 16:16:57 +00:00
|
|
|
if (flags&ListItemContainsBlock != 0 && flags&ListTypeDefinition == 0) ||
|
|
|
|
flags&ListItemBeginningOfList != 0 {
|
2011-07-01 17:19:42 +00:00
|
|
|
doubleSpace(out)
|
|
|
|
}
|
2015-10-26 16:16:57 +00:00
|
|
|
if flags&ListTypeTerm != 0 {
|
2015-05-29 11:30:49 +00:00
|
|
|
out.WriteString("<dt>")
|
2015-10-26 16:16:57 +00:00
|
|
|
} else if flags&ListTypeDefinition != 0 {
|
2015-05-29 11:30:49 +00:00
|
|
|
out.WriteString("<dd>")
|
|
|
|
} else {
|
|
|
|
out.WriteString("<li>")
|
|
|
|
}
|
2011-06-29 21:38:35 +00:00
|
|
|
out.Write(text)
|
2015-10-26 16:16:57 +00:00
|
|
|
if flags&ListTypeTerm != 0 {
|
2015-05-29 11:30:49 +00:00
|
|
|
out.WriteString("</dt>\n")
|
2015-10-26 16:16:57 +00:00
|
|
|
} else if flags&ListTypeDefinition != 0 {
|
2015-05-29 11:30:49 +00:00
|
|
|
out.WriteString("</dd>\n")
|
|
|
|
} else {
|
|
|
|
out.WriteString("</li>\n")
|
|
|
|
}
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) BeginParagraph() {
|
2011-06-29 21:38:35 +00:00
|
|
|
doubleSpace(out)
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<p>")
|
2015-10-26 18:35:42 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) EndParagraph() {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("</p>\n")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) AutoLink(link []byte, kind LinkType) {
|
2014-01-26 19:39:38 +00:00
|
|
|
skipRanges := htmlEntity.FindAllIndex(link, -1)
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.flags&Safelink != 0 && !isSafeLink(link) && kind != LinkTypeEmail {
|
2011-06-29 21:38:35 +00:00
|
|
|
// mark it but don't link it if it is not a safe link: no smartypants
|
|
|
|
out.WriteString("<tt>")
|
2014-01-26 19:39:38 +00:00
|
|
|
entityEscapeWithSkip(out, link, skipRanges)
|
2011-06-29 21:38:35 +00:00
|
|
|
out.WriteString("</tt>")
|
2011-06-29 19:00:54 +00:00
|
|
|
return
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<a href=\"")
|
2015-10-26 16:16:57 +00:00
|
|
|
if kind == LinkTypeEmail {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("mailto:")
|
2014-05-18 06:28:15 +00:00
|
|
|
} else {
|
2015-10-26 18:42:41 +00:00
|
|
|
r.maybeWriteAbsolutePrefix(out, link)
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2014-05-18 06:28:15 +00:00
|
|
|
|
2014-01-26 19:39:38 +00:00
|
|
|
entityEscapeWithSkip(out, link, skipRanges)
|
2014-03-21 02:52:46 +00:00
|
|
|
|
2015-03-14 23:46:32 +00:00
|
|
|
var relAttrs []string
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.flags&NofollowLinks != 0 && !isRelativeLink(link) {
|
2015-03-14 23:46:32 +00:00
|
|
|
relAttrs = append(relAttrs, "nofollow")
|
2014-03-21 02:52:46 +00:00
|
|
|
}
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.flags&NoreferrerLinks != 0 && !isRelativeLink(link) {
|
2015-03-14 23:46:32 +00:00
|
|
|
relAttrs = append(relAttrs, "noreferrer")
|
|
|
|
}
|
|
|
|
if len(relAttrs) > 0 {
|
|
|
|
out.WriteString(fmt.Sprintf("\" rel=\"%s", strings.Join(relAttrs, " ")))
|
|
|
|
}
|
|
|
|
|
2014-03-21 02:52:46 +00:00
|
|
|
// blank target only add to external link
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.flags&HrefTargetBlank != 0 && !isRelativeLink(link) {
|
2014-03-21 02:52:46 +00:00
|
|
|
out.WriteString("\" target=\"_blank")
|
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("\">")
|
2011-05-29 03:17:53 +00:00
|
|
|
|
2011-06-29 21:38:35 +00:00
|
|
|
// Pretty print: if we get an email address as
|
|
|
|
// an actual URI, e.g. `mailto:foo@bar.com`, we don't
|
|
|
|
// want to print the `mailto:` prefix
|
2011-05-31 17:49:49 +00:00
|
|
|
switch {
|
2011-05-30 21:36:31 +00:00
|
|
|
case bytes.HasPrefix(link, []byte("mailto://")):
|
2011-06-29 21:38:35 +00:00
|
|
|
attrEscape(out, link[len("mailto://"):])
|
2011-05-31 17:49:49 +00:00
|
|
|
case bytes.HasPrefix(link, []byte("mailto:")):
|
2011-06-29 21:38:35 +00:00
|
|
|
attrEscape(out, link[len("mailto:"):])
|
2011-05-30 21:36:31 +00:00
|
|
|
default:
|
2014-01-26 19:39:38 +00:00
|
|
|
entityEscapeWithSkip(out, link, skipRanges)
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("</a>")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) CodeSpan(text []byte) {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<code>")
|
|
|
|
attrEscape(out, text)
|
|
|
|
out.WriteString("</code>")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) DoubleEmphasis(text []byte) {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<strong>")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("</strong>")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) Emphasis(text []byte) {
|
2011-05-29 03:17:53 +00:00
|
|
|
if len(text) == 0 {
|
2011-06-29 19:00:54 +00:00
|
|
|
return
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<em>")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("</em>")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) maybeWriteAbsolutePrefix(link []byte) {
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.parameters.AbsolutePrefix != "" && isRelativeLink(link) && link[0] != '.' {
|
|
|
|
out.WriteString(r.parameters.AbsolutePrefix)
|
2014-05-18 06:28:15 +00:00
|
|
|
if link[0] != '/' {
|
|
|
|
out.WriteByte('/')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) Image(link []byte, title []byte, alt []byte) {
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.flags&SkipImages != 0 {
|
2011-06-29 19:00:54 +00:00
|
|
|
return
|
2011-06-29 01:46:35 +00:00
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<img src=\"")
|
2015-10-26 18:42:41 +00:00
|
|
|
r.maybeWriteAbsolutePrefix(out, link)
|
2011-05-30 17:06:20 +00:00
|
|
|
attrEscape(out, link)
|
|
|
|
out.WriteString("\" alt=\"")
|
2011-05-29 03:17:53 +00:00
|
|
|
if len(alt) > 0 {
|
2011-05-30 17:06:20 +00:00
|
|
|
attrEscape(out, alt)
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
if len(title) > 0 {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("\" title=\"")
|
|
|
|
attrEscape(out, title)
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteByte('"')
|
2015-10-26 18:42:41 +00:00
|
|
|
out.WriteString(r.closeTag)
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) LineBreak() {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<br")
|
2015-10-26 18:42:41 +00:00
|
|
|
out.WriteString(r.closeTag)
|
2015-05-25 19:41:06 +00:00
|
|
|
out.WriteByte('\n')
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) Link(link []byte, title []byte, content []byte) {
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.flags&SkipLinks != 0 {
|
2011-06-29 21:38:35 +00:00
|
|
|
// write the link text out but don't link it, just mark it with typewriter font
|
|
|
|
out.WriteString("<tt>")
|
|
|
|
attrEscape(out, content)
|
|
|
|
out.WriteString("</tt>")
|
2011-06-29 19:00:54 +00:00
|
|
|
return
|
2011-06-29 01:46:35 +00:00
|
|
|
}
|
2011-05-29 03:17:53 +00:00
|
|
|
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.flags&Safelink != 0 && !isSafeLink(link) {
|
2011-06-29 21:38:35 +00:00
|
|
|
// write the link text out but don't link it, just mark it with typewriter font
|
|
|
|
out.WriteString("<tt>")
|
|
|
|
attrEscape(out, content)
|
|
|
|
out.WriteString("</tt>")
|
2011-06-29 19:00:54 +00:00
|
|
|
return
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<a href=\"")
|
2015-10-26 18:42:41 +00:00
|
|
|
r.maybeWriteAbsolutePrefix(out, link)
|
2011-06-24 17:50:03 +00:00
|
|
|
attrEscape(out, link)
|
2011-05-29 03:17:53 +00:00
|
|
|
if len(title) > 0 {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("\" title=\"")
|
|
|
|
attrEscape(out, title)
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2015-03-14 23:46:32 +00:00
|
|
|
var relAttrs []string
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.flags&NofollowLinks != 0 && !isRelativeLink(link) {
|
2015-03-14 23:46:32 +00:00
|
|
|
relAttrs = append(relAttrs, "nofollow")
|
2014-02-25 14:21:57 +00:00
|
|
|
}
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.flags&NoreferrerLinks != 0 && !isRelativeLink(link) {
|
2015-03-14 23:46:32 +00:00
|
|
|
relAttrs = append(relAttrs, "noreferrer")
|
|
|
|
}
|
|
|
|
if len(relAttrs) > 0 {
|
|
|
|
out.WriteString(fmt.Sprintf("\" rel=\"%s", strings.Join(relAttrs, " ")))
|
|
|
|
}
|
|
|
|
|
2014-03-21 02:52:46 +00:00
|
|
|
// blank target only add to external link
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.flags&HrefTargetBlank != 0 && !isRelativeLink(link) {
|
2014-03-21 02:52:46 +00:00
|
|
|
out.WriteString("\" target=\"_blank")
|
|
|
|
}
|
|
|
|
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("\">")
|
2011-06-24 17:50:03 +00:00
|
|
|
out.Write(content)
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("</a>")
|
2011-06-29 19:00:54 +00:00
|
|
|
return
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) RawHtmlTag(text []byte) {
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.flags&SkipHTML != 0 {
|
2011-06-29 19:00:54 +00:00
|
|
|
return
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.flags&SkipStyle != 0 && isHtmlTag(text, "style") {
|
2011-06-29 19:00:54 +00:00
|
|
|
return
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.flags&SkipLinks != 0 && isHtmlTag(text, "a") {
|
2011-06-29 19:00:54 +00:00
|
|
|
return
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.flags&SkipImages != 0 && isHtmlTag(text, "img") {
|
2011-06-29 19:00:54 +00:00
|
|
|
return
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2011-05-30 17:06:20 +00:00
|
|
|
out.Write(text)
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) TripleEmphasis(text []byte) {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<strong><em>")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("</em></strong>")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) StrikeThrough(text []byte) {
|
2011-05-30 17:06:20 +00:00
|
|
|
out.WriteString("<del>")
|
|
|
|
out.Write(text)
|
|
|
|
out.WriteString("</del>")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) FootnoteRef(ref []byte, id int) {
|
2013-06-25 01:18:47 +00:00
|
|
|
slug := slugify(ref)
|
2014-05-24 07:55:13 +00:00
|
|
|
out.WriteString(`<sup class="footnote-ref" id="`)
|
|
|
|
out.WriteString(`fnref:`)
|
2015-10-26 18:42:41 +00:00
|
|
|
out.WriteString(r.parameters.FootnoteAnchorPrefix)
|
2013-06-25 01:18:47 +00:00
|
|
|
out.Write(slug)
|
2014-05-24 07:55:13 +00:00
|
|
|
out.WriteString(`"><a rel="footnote" href="#`)
|
|
|
|
out.WriteString(`fn:`)
|
2015-10-26 18:42:41 +00:00
|
|
|
out.WriteString(r.parameters.FootnoteAnchorPrefix)
|
2013-06-25 01:18:47 +00:00
|
|
|
out.Write(slug)
|
|
|
|
out.WriteString(`">`)
|
|
|
|
out.WriteString(strconv.Itoa(id))
|
|
|
|
out.WriteString(`</a></sup>`)
|
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) Entity(entity []byte) {
|
2011-06-29 16:08:56 +00:00
|
|
|
out.Write(entity)
|
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) NormalText(text []byte) {
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.flags&UseSmartypants != 0 {
|
|
|
|
r.Smartypants(out, text)
|
2011-06-29 01:46:35 +00:00
|
|
|
} else {
|
|
|
|
attrEscape(out, text)
|
|
|
|
}
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) Smartypants(text []byte) {
|
2011-06-29 17:13:17 +00:00
|
|
|
smrt := smartypantsData{false, false}
|
|
|
|
|
|
|
|
// first do normal entity escaping
|
|
|
|
var escaped bytes.Buffer
|
|
|
|
attrEscape(&escaped, text)
|
|
|
|
text = escaped.Bytes()
|
|
|
|
|
|
|
|
mark := 0
|
|
|
|
for i := 0; i < len(text); i++ {
|
2015-10-26 18:42:41 +00:00
|
|
|
if action := r.smartypants[text[i]]; action != nil {
|
2011-06-29 17:13:17 +00:00
|
|
|
if i > mark {
|
|
|
|
out.Write(text[mark:i])
|
|
|
|
}
|
|
|
|
|
|
|
|
previousChar := byte(0)
|
|
|
|
if i > 0 {
|
|
|
|
previousChar = text[i-1]
|
|
|
|
}
|
|
|
|
i += action(out, &smrt, previousChar, text[i:])
|
|
|
|
mark = i + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if mark < len(text) {
|
|
|
|
out.Write(text[mark:])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) DocumentHeader() {
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.flags&CompletePage == 0 {
|
2011-06-29 16:08:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ending := ""
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.flags&UseXHTML != 0 {
|
2011-06-29 16:08:56 +00:00
|
|
|
out.WriteString("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" ")
|
|
|
|
out.WriteString("\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n")
|
|
|
|
out.WriteString("<html xmlns=\"http://www.w3.org/1999/xhtml\">\n")
|
|
|
|
ending = " /"
|
|
|
|
} else {
|
2012-10-22 04:23:44 +00:00
|
|
|
out.WriteString("<!DOCTYPE html>\n")
|
2011-06-29 16:08:56 +00:00
|
|
|
out.WriteString("<html>\n")
|
|
|
|
}
|
|
|
|
out.WriteString("<head>\n")
|
|
|
|
out.WriteString(" <title>")
|
2015-11-01 19:57:30 +00:00
|
|
|
r.NormalText([]byte(r.title))
|
2011-06-29 16:08:56 +00:00
|
|
|
out.WriteString("</title>\n")
|
|
|
|
out.WriteString(" <meta name=\"GENERATOR\" content=\"Blackfriday Markdown Processor v")
|
|
|
|
out.WriteString(VERSION)
|
|
|
|
out.WriteString("\"")
|
|
|
|
out.WriteString(ending)
|
|
|
|
out.WriteString(">\n")
|
2012-10-22 04:23:44 +00:00
|
|
|
out.WriteString(" <meta charset=\"utf-8\"")
|
2011-06-29 16:08:56 +00:00
|
|
|
out.WriteString(ending)
|
|
|
|
out.WriteString(">\n")
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.css != "" {
|
2011-06-29 16:08:56 +00:00
|
|
|
out.WriteString(" <link rel=\"stylesheet\" type=\"text/css\" href=\"")
|
2015-10-26 18:42:41 +00:00
|
|
|
attrEscape(out, []byte(r.css))
|
2011-06-29 16:08:56 +00:00
|
|
|
out.WriteString("\"")
|
|
|
|
out.WriteString(ending)
|
|
|
|
out.WriteString(">\n")
|
|
|
|
}
|
|
|
|
out.WriteString("</head>\n")
|
|
|
|
out.WriteString("<body>\n")
|
2011-06-29 16:36:56 +00:00
|
|
|
|
2015-10-26 18:42:41 +00:00
|
|
|
r.tocMarker = out.Len()
|
2011-06-29 16:08:56 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:27:55 +00:00
|
|
|
func (r *Html) DocumentFooter() {
|
2011-06-29 16:36:56 +00:00
|
|
|
// finalize and insert the table of contents
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.flags&Toc != 0 {
|
|
|
|
r.TocFinalize()
|
2011-06-29 16:36:56 +00:00
|
|
|
|
|
|
|
// now we have to insert the table of contents into the document
|
|
|
|
var temp bytes.Buffer
|
|
|
|
|
|
|
|
// start by making a copy of everything after the document header
|
2015-10-26 18:42:41 +00:00
|
|
|
temp.Write(out.Bytes()[r.tocMarker:])
|
2011-06-29 16:36:56 +00:00
|
|
|
|
|
|
|
// now clear the copied material from the main output buffer
|
2015-10-26 18:42:41 +00:00
|
|
|
out.Truncate(r.tocMarker)
|
2011-06-29 16:36:56 +00:00
|
|
|
|
2011-06-29 19:24:15 +00:00
|
|
|
// corner case spacing issue
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.flags&CompletePage != 0 {
|
2011-06-29 19:24:15 +00:00
|
|
|
out.WriteByte('\n')
|
|
|
|
}
|
|
|
|
|
2011-06-29 16:36:56 +00:00
|
|
|
// insert the table of contents
|
2012-10-22 04:23:44 +00:00
|
|
|
out.WriteString("<nav>\n")
|
2015-10-26 18:42:41 +00:00
|
|
|
out.Write(r.toc.Bytes())
|
2012-10-22 04:23:44 +00:00
|
|
|
out.WriteString("</nav>\n")
|
2011-06-29 16:36:56 +00:00
|
|
|
|
2011-06-29 19:24:15 +00:00
|
|
|
// corner case spacing issue
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.flags&CompletePage == 0 && r.flags&OmitContents == 0 {
|
2011-06-29 19:24:15 +00:00
|
|
|
out.WriteByte('\n')
|
|
|
|
}
|
|
|
|
|
2011-06-29 16:36:56 +00:00
|
|
|
// write out everything that came after it
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.flags&OmitContents == 0 {
|
2011-06-29 16:36:56 +00:00
|
|
|
out.Write(temp.Bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.flags&CompletePage != 0 {
|
2011-06-29 16:36:56 +00:00
|
|
|
out.WriteString("\n</body>\n")
|
|
|
|
out.WriteString("</html>\n")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
2011-06-29 16:08:56 +00:00
|
|
|
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 18:42:41 +00:00
|
|
|
func (r *Html) TocHeaderWithAnchor(text []byte, level int, anchor string) {
|
|
|
|
for level > r.currentLevel {
|
2011-06-29 16:36:56 +00:00
|
|
|
switch {
|
2015-10-26 18:42:41 +00:00
|
|
|
case bytes.HasSuffix(r.toc.Bytes(), []byte("</li>\n")):
|
2011-06-29 19:24:15 +00:00
|
|
|
// this sublist can nest underneath a header
|
2015-10-26 18:42:41 +00:00
|
|
|
size := r.toc.Len()
|
|
|
|
r.toc.Truncate(size - len("</li>\n"))
|
2011-06-29 16:36:56 +00:00
|
|
|
|
2015-10-26 18:42:41 +00:00
|
|
|
case r.currentLevel > 0:
|
|
|
|
r.toc.WriteString("<li>")
|
2011-06-29 16:36:56 +00:00
|
|
|
}
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.toc.Len() > 0 {
|
|
|
|
r.toc.WriteByte('\n')
|
2011-06-29 19:24:15 +00:00
|
|
|
}
|
2015-10-26 18:42:41 +00:00
|
|
|
r.toc.WriteString("<ul>\n")
|
|
|
|
r.currentLevel++
|
2011-06-29 16:36:56 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 18:42:41 +00:00
|
|
|
for level < r.currentLevel {
|
|
|
|
r.toc.WriteString("</ul>")
|
|
|
|
if r.currentLevel > 1 {
|
|
|
|
r.toc.WriteString("</li>\n")
|
2011-06-29 16:36:56 +00:00
|
|
|
}
|
2015-10-26 18:42:41 +00:00
|
|
|
r.currentLevel--
|
2011-06-29 16:36:56 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 18:42:41 +00:00
|
|
|
r.toc.WriteString("<li><a href=\"#")
|
2014-10-27 20:49:28 +00:00
|
|
|
if anchor != "" {
|
2015-10-26 18:42:41 +00:00
|
|
|
r.toc.WriteString(anchor)
|
2014-10-27 20:49:28 +00:00
|
|
|
} else {
|
2015-10-26 18:42:41 +00:00
|
|
|
r.toc.WriteString("toc_")
|
|
|
|
r.toc.WriteString(strconv.Itoa(r.headerCount))
|
2014-10-27 20:49:28 +00:00
|
|
|
}
|
2015-10-26 18:42:41 +00:00
|
|
|
r.toc.WriteString("\">")
|
|
|
|
r.headerCount++
|
2011-06-29 16:36:56 +00:00
|
|
|
|
2015-10-26 18:42:41 +00:00
|
|
|
r.toc.Write(text)
|
2011-06-29 16:36:56 +00:00
|
|
|
|
2015-10-26 18:42:41 +00:00
|
|
|
r.toc.WriteString("</a></li>\n")
|
2011-06-29 16:36:56 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 18:42:41 +00:00
|
|
|
func (r *Html) TocHeader(text []byte, level int) {
|
|
|
|
r.TocHeaderWithAnchor(text, level, "")
|
2014-10-27 20:49:28 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 18:42:41 +00:00
|
|
|
func (r *Html) TocFinalize() {
|
|
|
|
for r.currentLevel > 1 {
|
|
|
|
r.toc.WriteString("</ul></li>\n")
|
|
|
|
r.currentLevel--
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 18:42:41 +00:00
|
|
|
if r.currentLevel > 0 {
|
|
|
|
r.toc.WriteString("</ul>\n")
|
2011-05-29 03:17:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-29 23:00:31 +00:00
|
|
|
func isHtmlTag(tag []byte, tagname string) bool {
|
2013-04-18 00:15:47 +00:00
|
|
|
found, _ := findHtmlTagPos(tag, tagname)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-04-18 00:15:47 +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, '>')
|
|
|
|
if rightAngle > i {
|
|
|
|
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
|
|
|
|
2014-01-21 23:14:35 +00:00
|
|
|
func skipUntilChar(text []byte, start int, char byte) int {
|
|
|
|
i := start
|
|
|
|
for i < len(text) && text[i] != char {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2013-04-13 19:26:29 +00:00
|
|
|
func skipSpace(tag []byte, i int) int {
|
|
|
|
for i < len(tag) && isspace(tag[i]) {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2015-04-07 18:59:42 +00:00
|
|
|
func skipChar(data []byte, start int, char byte) int {
|
|
|
|
i := start
|
|
|
|
for i < len(data) && data[i] == char {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2011-06-29 21:38:35 +00:00
|
|
|
func doubleSpace(out *bytes.Buffer) {
|
|
|
|
if out.Len() > 0 {
|
|
|
|
out.WriteByte('\n')
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2015-10-26 18:42:41 +00:00
|
|
|
func (r *Html) ensureUniqueHeaderID(id string) string {
|
|
|
|
for count, found := r.headerIDs[id]; found; count, found = r.headerIDs[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)
|
|
|
|
|
2015-10-26 18:42:41 +00:00
|
|
|
if _, tmpFound := r.headerIDs[tmp]; !tmpFound {
|
|
|
|
r.headerIDs[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"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 18:42:41 +00:00
|
|
|
if _, found := r.headerIDs[id]; !found {
|
|
|
|
r.headerIDs[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
|
|
|
|
}
|