mirror of
https://github.com/russross/blackfriday.git
synced 2024-03-22 13:40:34 +08:00
doc improvements, commenting
This commit is contained in:
parent
256ff94689
commit
bb8ee591d1
|
@ -164,8 +164,6 @@ Todo
|
||||||
----
|
----
|
||||||
|
|
||||||
* More unit testing
|
* More unit testing
|
||||||
* Code cleanup
|
|
||||||
* Better code documentation
|
|
||||||
* Markdown pretty-printer output engine
|
* Markdown pretty-printer output engine
|
||||||
* Improve unicode support. It does not understand all unicode
|
* Improve unicode support. It does not understand all unicode
|
||||||
rules (about what constitutes a letter, a punctuation symbol,
|
rules (about what constitutes a letter, a punctuation symbol,
|
||||||
|
|
244
block.go
244
block.go
|
@ -20,16 +20,16 @@ import (
|
||||||
// Parse block-level data.
|
// Parse block-level data.
|
||||||
// Note: this function and many that it calls assume that
|
// Note: this function and many that it calls assume that
|
||||||
// the input buffer ends with a newline.
|
// the input buffer ends with a newline.
|
||||||
func (parser *Parser) block(out *bytes.Buffer, data []byte) {
|
func (p *parser) block(out *bytes.Buffer, data []byte) {
|
||||||
if len(data) == 0 || data[len(data)-1] != '\n' {
|
if len(data) == 0 || data[len(data)-1] != '\n' {
|
||||||
panic("block input is missing terminating newline")
|
panic("block input is missing terminating newline")
|
||||||
}
|
}
|
||||||
|
|
||||||
// this is called recursively: enforce a maximum depth
|
// this is called recursively: enforce a maximum depth
|
||||||
if parser.nesting >= parser.maxNesting {
|
if p.nesting >= p.maxNesting {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
parser.nesting++
|
p.nesting++
|
||||||
|
|
||||||
// parse out one block-level construct at a time
|
// parse out one block-level construct at a time
|
||||||
for len(data) > 0 {
|
for len(data) > 0 {
|
||||||
|
@ -39,8 +39,8 @@ func (parser *Parser) block(out *bytes.Buffer, data []byte) {
|
||||||
// ## Header 2
|
// ## Header 2
|
||||||
// ...
|
// ...
|
||||||
// ###### Header 6
|
// ###### Header 6
|
||||||
if parser.isPrefixHeader(data) {
|
if p.isPrefixHeader(data) {
|
||||||
data = data[parser.prefixHeader(out, data):]
|
data = data[p.prefixHeader(out, data):]
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,14 +50,14 @@ func (parser *Parser) block(out *bytes.Buffer, data []byte) {
|
||||||
// ...
|
// ...
|
||||||
// </div>
|
// </div>
|
||||||
if data[0] == '<' {
|
if data[0] == '<' {
|
||||||
if i := parser.html(out, data, true); i > 0 {
|
if i := p.html(out, data, true); i > 0 {
|
||||||
data = data[i:]
|
data = data[i:]
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// blank lines. note: returns the # of bytes to skip
|
// blank lines. note: returns the # of bytes to skip
|
||||||
if i := parser.isEmpty(data); i > 0 {
|
if i := p.isEmpty(data); i > 0 {
|
||||||
data = data[i:]
|
data = data[i:]
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -70,8 +70,8 @@ func (parser *Parser) block(out *bytes.Buffer, data []byte) {
|
||||||
// }
|
// }
|
||||||
// return b
|
// return b
|
||||||
// }
|
// }
|
||||||
if parser.codePrefix(data) > 0 {
|
if p.codePrefix(data) > 0 {
|
||||||
data = data[parser.code(out, data):]
|
data = data[p.code(out, data):]
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,8 +85,8 @@ func (parser *Parser) block(out *bytes.Buffer, data []byte) {
|
||||||
// return n * fact(n-1)
|
// return n * fact(n-1)
|
||||||
// }
|
// }
|
||||||
// ```
|
// ```
|
||||||
if parser.flags&EXTENSION_FENCED_CODE != 0 {
|
if p.flags&EXTENSION_FENCED_CODE != 0 {
|
||||||
if i := parser.fencedCode(out, data); i > 0 {
|
if i := p.fencedCode(out, data); i > 0 {
|
||||||
data = data[i:]
|
data = data[i:]
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -99,8 +99,8 @@ func (parser *Parser) block(out *bytes.Buffer, data []byte) {
|
||||||
// ******
|
// ******
|
||||||
// or
|
// or
|
||||||
// ______
|
// ______
|
||||||
if parser.isHRule(data) {
|
if p.isHRule(data) {
|
||||||
parser.r.HRule(out)
|
p.r.HRule(out)
|
||||||
var i int
|
var i int
|
||||||
for i = 0; data[i] != '\n'; i++ {
|
for i = 0; data[i] != '\n'; i++ {
|
||||||
}
|
}
|
||||||
|
@ -112,8 +112,8 @@ func (parser *Parser) block(out *bytes.Buffer, data []byte) {
|
||||||
//
|
//
|
||||||
// > A big quote I found somewhere
|
// > A big quote I found somewhere
|
||||||
// > on the web
|
// > on the web
|
||||||
if parser.quotePrefix(data) > 0 {
|
if p.quotePrefix(data) > 0 {
|
||||||
data = data[parser.quote(out, data):]
|
data = data[p.quote(out, data):]
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,8 +123,8 @@ func (parser *Parser) block(out *bytes.Buffer, data []byte) {
|
||||||
// ------|-----|---------
|
// ------|-----|---------
|
||||||
// Bob | 31 | 555-1234
|
// Bob | 31 | 555-1234
|
||||||
// Alice | 27 | 555-4321
|
// Alice | 27 | 555-4321
|
||||||
if parser.flags&EXTENSION_TABLES != 0 {
|
if p.flags&EXTENSION_TABLES != 0 {
|
||||||
if i := parser.table(out, data); i > 0 {
|
if i := p.table(out, data); i > 0 {
|
||||||
data = data[i:]
|
data = data[i:]
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -136,8 +136,8 @@ func (parser *Parser) block(out *bytes.Buffer, data []byte) {
|
||||||
// * Item 2
|
// * Item 2
|
||||||
//
|
//
|
||||||
// also works with + or -
|
// also works with + or -
|
||||||
if parser.uliPrefix(data) > 0 {
|
if p.uliPrefix(data) > 0 {
|
||||||
data = data[parser.list(out, data, 0):]
|
data = data[p.list(out, data, 0):]
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,25 +145,25 @@ func (parser *Parser) block(out *bytes.Buffer, data []byte) {
|
||||||
//
|
//
|
||||||
// 1. Item 1
|
// 1. Item 1
|
||||||
// 2. Item 2
|
// 2. Item 2
|
||||||
if parser.oliPrefix(data) > 0 {
|
if p.oliPrefix(data) > 0 {
|
||||||
data = data[parser.list(out, data, LIST_TYPE_ORDERED):]
|
data = data[p.list(out, data, LIST_TYPE_ORDERED):]
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// anything else must look like a normal paragraph
|
// anything else must look like a normal paragraph
|
||||||
// note: this finds underlined headers, too
|
// note: this finds underlined headers, too
|
||||||
data = data[parser.paragraph(out, data):]
|
data = data[p.paragraph(out, data):]
|
||||||
}
|
}
|
||||||
|
|
||||||
parser.nesting--
|
p.nesting--
|
||||||
}
|
}
|
||||||
|
|
||||||
func (parser *Parser) isPrefixHeader(data []byte) bool {
|
func (p *parser) isPrefixHeader(data []byte) bool {
|
||||||
if data[0] != '#' {
|
if data[0] != '#' {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if parser.flags&EXTENSION_SPACE_HEADERS != 0 {
|
if p.flags&EXTENSION_SPACE_HEADERS != 0 {
|
||||||
level := 0
|
level := 0
|
||||||
for level < 6 && data[level] == '#' {
|
for level < 6 && data[level] == '#' {
|
||||||
level++
|
level++
|
||||||
|
@ -175,7 +175,7 @@ func (parser *Parser) isPrefixHeader(data []byte) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (parser *Parser) prefixHeader(out *bytes.Buffer, data []byte) int {
|
func (p *parser) prefixHeader(out *bytes.Buffer, data []byte) int {
|
||||||
level := 0
|
level := 0
|
||||||
for level < 6 && data[level] == '#' {
|
for level < 6 && data[level] == '#' {
|
||||||
level++
|
level++
|
||||||
|
@ -194,15 +194,15 @@ func (parser *Parser) prefixHeader(out *bytes.Buffer, data []byte) int {
|
||||||
}
|
}
|
||||||
if end > i {
|
if end > i {
|
||||||
work := func() bool {
|
work := func() bool {
|
||||||
parser.inline(out, data[i:end])
|
p.inline(out, data[i:end])
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
parser.r.Header(out, work, level)
|
p.r.Header(out, work, level)
|
||||||
}
|
}
|
||||||
return skip
|
return skip
|
||||||
}
|
}
|
||||||
|
|
||||||
func (parser *Parser) isUnderlinedHeader(data []byte) int {
|
func (p *parser) isUnderlinedHeader(data []byte) int {
|
||||||
// test of level 1 header
|
// test of level 1 header
|
||||||
if data[0] == '=' {
|
if data[0] == '=' {
|
||||||
i := 1
|
i := 1
|
||||||
|
@ -238,24 +238,24 @@ func (parser *Parser) isUnderlinedHeader(data []byte) int {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (parser *Parser) html(out *bytes.Buffer, data []byte, doRender bool) int {
|
func (p *parser) html(out *bytes.Buffer, data []byte, doRender bool) int {
|
||||||
var i, j int
|
var i, j int
|
||||||
|
|
||||||
// identify the opening tag
|
// identify the opening tag
|
||||||
if data[0] != '<' {
|
if data[0] != '<' {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
curtag, tagfound := parser.htmlFindTag(data[1:])
|
curtag, tagfound := p.htmlFindTag(data[1:])
|
||||||
|
|
||||||
// handle special cases
|
// handle special cases
|
||||||
if !tagfound {
|
if !tagfound {
|
||||||
// check for an HTML comment
|
// check for an HTML comment
|
||||||
if size := parser.htmlComment(out, data, doRender); size > 0 {
|
if size := p.htmlComment(out, data, doRender); size > 0 {
|
||||||
return size
|
return size
|
||||||
}
|
}
|
||||||
|
|
||||||
// check for an <hr> tag
|
// check for an <hr> tag
|
||||||
if size := parser.htmlHr(out, data, doRender); size > 0 {
|
if size := p.htmlHr(out, data, doRender); size > 0 {
|
||||||
return size
|
return size
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -278,14 +278,14 @@ func (parser *Parser) html(out *bytes.Buffer, data []byte, doRender bool) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// see if it is the only thing on the line
|
// see if it is the only thing on the line
|
||||||
if skip := parser.isEmpty(data[j:]); skip > 0 {
|
if skip := p.isEmpty(data[j:]); skip > 0 {
|
||||||
// see if it is followed by a blank line/eof
|
// see if it is followed by a blank line/eof
|
||||||
j += skip
|
j += skip
|
||||||
if j >= len(data) {
|
if j >= len(data) {
|
||||||
found = true
|
found = true
|
||||||
i = j
|
i = j
|
||||||
} else {
|
} else {
|
||||||
if skip := parser.isEmpty(data[j:]); skip > 0 {
|
if skip := p.isEmpty(data[j:]); skip > 0 {
|
||||||
j += skip
|
j += skip
|
||||||
found = true
|
found = true
|
||||||
i = j
|
i = j
|
||||||
|
@ -309,7 +309,7 @@ func (parser *Parser) html(out *bytes.Buffer, data []byte, doRender bool) int {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
j = parser.htmlFindEnd(curtag, data[i-1:])
|
j = p.htmlFindEnd(curtag, data[i-1:])
|
||||||
|
|
||||||
if j > 0 {
|
if j > 0 {
|
||||||
i += j - 1
|
i += j - 1
|
||||||
|
@ -330,14 +330,14 @@ func (parser *Parser) html(out *bytes.Buffer, data []byte, doRender bool) int {
|
||||||
for end > 0 && data[end-1] == '\n' {
|
for end > 0 && data[end-1] == '\n' {
|
||||||
end--
|
end--
|
||||||
}
|
}
|
||||||
parser.r.BlockHtml(out, data[:end])
|
p.r.BlockHtml(out, data[:end])
|
||||||
}
|
}
|
||||||
|
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTML comment, lax form
|
// HTML comment, lax form
|
||||||
func (parser *Parser) htmlComment(out *bytes.Buffer, data []byte, doRender bool) int {
|
func (p *parser) htmlComment(out *bytes.Buffer, data []byte, doRender bool) int {
|
||||||
if data[0] != '<' || data[1] != '!' || data[2] != '-' || data[3] != '-' {
|
if data[0] != '<' || data[1] != '!' || data[2] != '-' || data[3] != '-' {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
@ -356,7 +356,7 @@ func (parser *Parser) htmlComment(out *bytes.Buffer, data []byte, doRender bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
// needs to end with a blank line
|
// needs to end with a blank line
|
||||||
if j := parser.isEmpty(data[i:]); j > 0 {
|
if j := p.isEmpty(data[i:]); j > 0 {
|
||||||
size := i + j
|
size := i + j
|
||||||
if doRender {
|
if doRender {
|
||||||
// trim trailing newlines
|
// trim trailing newlines
|
||||||
|
@ -364,7 +364,7 @@ func (parser *Parser) htmlComment(out *bytes.Buffer, data []byte, doRender bool)
|
||||||
for end > 0 && data[end-1] == '\n' {
|
for end > 0 && data[end-1] == '\n' {
|
||||||
end--
|
end--
|
||||||
}
|
}
|
||||||
parser.r.BlockHtml(out, data[:end])
|
p.r.BlockHtml(out, data[:end])
|
||||||
}
|
}
|
||||||
return size
|
return size
|
||||||
}
|
}
|
||||||
|
@ -373,7 +373,7 @@ func (parser *Parser) htmlComment(out *bytes.Buffer, data []byte, doRender bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
// HR, which is the only self-closing block tag considered
|
// HR, which is the only self-closing block tag considered
|
||||||
func (parser *Parser) htmlHr(out *bytes.Buffer, data []byte, doRender bool) int {
|
func (p *parser) htmlHr(out *bytes.Buffer, data []byte, doRender bool) int {
|
||||||
if data[0] != '<' || (data[1] != 'h' && data[1] != 'H') || (data[2] != 'r' && data[2] != 'R') {
|
if data[0] != '<' || (data[1] != 'h' && data[1] != 'H') || (data[2] != 'r' && data[2] != 'R') {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
@ -389,7 +389,7 @@ func (parser *Parser) htmlHr(out *bytes.Buffer, data []byte, doRender bool) int
|
||||||
|
|
||||||
if data[i] == '>' {
|
if data[i] == '>' {
|
||||||
i++
|
i++
|
||||||
if j := parser.isEmpty(data[i:]); j > 0 {
|
if j := p.isEmpty(data[i:]); j > 0 {
|
||||||
size := i + j
|
size := i + j
|
||||||
if doRender {
|
if doRender {
|
||||||
// trim newlines
|
// trim newlines
|
||||||
|
@ -397,7 +397,7 @@ func (parser *Parser) htmlHr(out *bytes.Buffer, data []byte, doRender bool) int
|
||||||
for end > 0 && data[end-1] == '\n' {
|
for end > 0 && data[end-1] == '\n' {
|
||||||
end--
|
end--
|
||||||
}
|
}
|
||||||
parser.r.BlockHtml(out, data[:end])
|
p.r.BlockHtml(out, data[:end])
|
||||||
}
|
}
|
||||||
return size
|
return size
|
||||||
}
|
}
|
||||||
|
@ -406,7 +406,7 @@ func (parser *Parser) htmlHr(out *bytes.Buffer, data []byte, doRender bool) int
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (parser *Parser) htmlFindTag(data []byte) (string, bool) {
|
func (p *parser) htmlFindTag(data []byte) (string, bool) {
|
||||||
i := 0
|
i := 0
|
||||||
for isalnum(data[i]) {
|
for isalnum(data[i]) {
|
||||||
i++
|
i++
|
||||||
|
@ -418,7 +418,7 @@ func (parser *Parser) htmlFindTag(data []byte) (string, bool) {
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (parser *Parser) htmlFindEnd(tag string, data []byte) int {
|
func (p *parser) htmlFindEnd(tag string, data []byte) int {
|
||||||
// assume data[0] == '<' && data[1] == '/' already tested
|
// assume data[0] == '<' && data[1] == '/' already tested
|
||||||
|
|
||||||
// check if tag is a match
|
// check if tag is a match
|
||||||
|
@ -430,7 +430,7 @@ func (parser *Parser) htmlFindEnd(tag string, data []byte) int {
|
||||||
|
|
||||||
// check that the rest of the line is blank
|
// check that the rest of the line is blank
|
||||||
skip := 0
|
skip := 0
|
||||||
if skip = parser.isEmpty(data[i:]); skip == 0 {
|
if skip = p.isEmpty(data[i:]); skip == 0 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
i += skip
|
i += skip
|
||||||
|
@ -440,10 +440,10 @@ func (parser *Parser) htmlFindEnd(tag string, data []byte) int {
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
if parser.flags&EXTENSION_LAX_HTML_BLOCKS != 0 {
|
if p.flags&EXTENSION_LAX_HTML_BLOCKS != 0 {
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
if skip = parser.isEmpty(data[i:]); skip == 0 {
|
if skip = p.isEmpty(data[i:]); skip == 0 {
|
||||||
// following line must be blank
|
// following line must be blank
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
@ -451,7 +451,7 @@ func (parser *Parser) htmlFindEnd(tag string, data []byte) int {
|
||||||
return i + skip
|
return i + skip
|
||||||
}
|
}
|
||||||
|
|
||||||
func (parser *Parser) isEmpty(data []byte) int {
|
func (p *parser) isEmpty(data []byte) int {
|
||||||
// it is okay to call isEmpty on an empty buffer
|
// it is okay to call isEmpty on an empty buffer
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
return 0
|
return 0
|
||||||
|
@ -466,7 +466,7 @@ func (parser *Parser) isEmpty(data []byte) int {
|
||||||
return i + 1
|
return i + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (parser *Parser) isHRule(data []byte) bool {
|
func (p *parser) isHRule(data []byte) bool {
|
||||||
i := 0
|
i := 0
|
||||||
|
|
||||||
// skip up to three spaces
|
// skip up to three spaces
|
||||||
|
@ -495,7 +495,7 @@ func (parser *Parser) isHRule(data []byte) bool {
|
||||||
return n >= 3
|
return n >= 3
|
||||||
}
|
}
|
||||||
|
|
||||||
func (parser *Parser) isFencedCode(data []byte, syntax **string, oldmarker string) (skip int, marker string) {
|
func (p *parser) isFencedCode(data []byte, syntax **string, oldmarker string) (skip int, marker string) {
|
||||||
i, size := 0, 0
|
i, size := 0, 0
|
||||||
skip = 0
|
skip = 0
|
||||||
|
|
||||||
|
@ -584,9 +584,9 @@ func (parser *Parser) isFencedCode(data []byte, syntax **string, oldmarker strin
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (parser *Parser) fencedCode(out *bytes.Buffer, data []byte) int {
|
func (p *parser) fencedCode(out *bytes.Buffer, data []byte) int {
|
||||||
var lang *string
|
var lang *string
|
||||||
beg, marker := parser.isFencedCode(data, &lang, "")
|
beg, marker := p.isFencedCode(data, &lang, "")
|
||||||
if beg == 0 || beg >= len(data) {
|
if beg == 0 || beg >= len(data) {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
@ -597,7 +597,7 @@ func (parser *Parser) fencedCode(out *bytes.Buffer, data []byte) int {
|
||||||
// safe to assume beg < len(data)
|
// safe to assume beg < len(data)
|
||||||
|
|
||||||
// check for the end of the code block
|
// check for the end of the code block
|
||||||
fenceEnd, _ := parser.isFencedCode(data[beg:], nil, marker)
|
fenceEnd, _ := p.isFencedCode(data[beg:], nil, marker)
|
||||||
if fenceEnd != 0 {
|
if fenceEnd != 0 {
|
||||||
beg += fenceEnd
|
beg += fenceEnd
|
||||||
break
|
break
|
||||||
|
@ -625,14 +625,14 @@ func (parser *Parser) fencedCode(out *bytes.Buffer, data []byte) int {
|
||||||
syntax = *lang
|
syntax = *lang
|
||||||
}
|
}
|
||||||
|
|
||||||
parser.r.BlockCode(out, work.Bytes(), syntax)
|
p.r.BlockCode(out, work.Bytes(), syntax)
|
||||||
|
|
||||||
return beg
|
return beg
|
||||||
}
|
}
|
||||||
|
|
||||||
func (parser *Parser) table(out *bytes.Buffer, data []byte) int {
|
func (p *parser) table(out *bytes.Buffer, data []byte) int {
|
||||||
var header bytes.Buffer
|
var header bytes.Buffer
|
||||||
i, columns := parser.tableHeader(&header, data)
|
i, columns := p.tableHeader(&header, data)
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
@ -654,15 +654,15 @@ func (parser *Parser) table(out *bytes.Buffer, data []byte) int {
|
||||||
|
|
||||||
// include the newline in data sent to tableRow
|
// include the newline in data sent to tableRow
|
||||||
i++
|
i++
|
||||||
parser.tableRow(&body, data[rowStart:i], columns)
|
p.tableRow(&body, data[rowStart:i], columns)
|
||||||
}
|
}
|
||||||
|
|
||||||
parser.r.Table(out, header.Bytes(), body.Bytes(), columns)
|
p.r.Table(out, header.Bytes(), body.Bytes(), columns)
|
||||||
|
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
func (parser *Parser) tableHeader(out *bytes.Buffer, data []byte) (size int, columns []int) {
|
func (p *parser) tableHeader(out *bytes.Buffer, data []byte) (size int, columns []int) {
|
||||||
i := 0
|
i := 0
|
||||||
colCount := 1
|
colCount := 1
|
||||||
for i = 0; data[i] != '\n'; i++ {
|
for i = 0; data[i] != '\n'; i++ {
|
||||||
|
@ -757,12 +757,12 @@ func (parser *Parser) tableHeader(out *bytes.Buffer, data []byte) (size int, col
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
parser.tableRow(out, header, columns)
|
p.tableRow(out, header, columns)
|
||||||
size = i + 1
|
size = i + 1
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (parser *Parser) tableRow(out *bytes.Buffer, data []byte, columns []int) {
|
func (p *parser) tableRow(out *bytes.Buffer, data []byte, columns []int) {
|
||||||
i, col := 0, 0
|
i, col := 0, 0
|
||||||
var rowWork bytes.Buffer
|
var rowWork bytes.Buffer
|
||||||
|
|
||||||
|
@ -789,22 +789,22 @@ func (parser *Parser) tableRow(out *bytes.Buffer, data []byte, columns []int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var cellWork bytes.Buffer
|
var cellWork bytes.Buffer
|
||||||
parser.inline(&cellWork, data[cellStart:cellEnd])
|
p.inline(&cellWork, data[cellStart:cellEnd])
|
||||||
parser.r.TableCell(&rowWork, cellWork.Bytes(), columns[col])
|
p.r.TableCell(&rowWork, cellWork.Bytes(), columns[col])
|
||||||
}
|
}
|
||||||
|
|
||||||
// pad it out with empty columns to get the right number
|
// pad it out with empty columns to get the right number
|
||||||
for ; col < len(columns); col++ {
|
for ; col < len(columns); col++ {
|
||||||
parser.r.TableCell(&rowWork, nil, columns[col])
|
p.r.TableCell(&rowWork, nil, columns[col])
|
||||||
}
|
}
|
||||||
|
|
||||||
// silently ignore rows with too many cells
|
// silently ignore rows with too many cells
|
||||||
|
|
||||||
parser.r.TableRow(out, rowWork.Bytes())
|
p.r.TableRow(out, rowWork.Bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns blockquote prefix length
|
// returns blockquote prefix length
|
||||||
func (parser *Parser) quotePrefix(data []byte) int {
|
func (p *parser) quotePrefix(data []byte) int {
|
||||||
i := 0
|
i := 0
|
||||||
for i < 3 && data[i] == ' ' {
|
for i < 3 && data[i] == ' ' {
|
||||||
i++
|
i++
|
||||||
|
@ -819,7 +819,7 @@ func (parser *Parser) quotePrefix(data []byte) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse a blockquote fragment
|
// parse a blockquote fragment
|
||||||
func (parser *Parser) quote(out *bytes.Buffer, data []byte) int {
|
func (p *parser) quote(out *bytes.Buffer, data []byte) int {
|
||||||
var raw bytes.Buffer
|
var raw bytes.Buffer
|
||||||
beg, end := 0, 0
|
beg, end := 0, 0
|
||||||
for beg < len(data) {
|
for beg < len(data) {
|
||||||
|
@ -829,12 +829,12 @@ func (parser *Parser) quote(out *bytes.Buffer, data []byte) int {
|
||||||
}
|
}
|
||||||
end++
|
end++
|
||||||
|
|
||||||
if pre := parser.quotePrefix(data[beg:]); pre > 0 {
|
if pre := p.quotePrefix(data[beg:]); pre > 0 {
|
||||||
// string the prefix
|
// skip the prefix
|
||||||
beg += pre
|
beg += pre
|
||||||
} else if parser.isEmpty(data[beg:]) > 0 &&
|
} else if p.isEmpty(data[beg:]) > 0 &&
|
||||||
(end >= len(data) ||
|
(end >= len(data) ||
|
||||||
(parser.quotePrefix(data[end:]) == 0 && parser.isEmpty(data[end:]) == 0)) {
|
(p.quotePrefix(data[end:]) == 0 && p.isEmpty(data[end:]) == 0)) {
|
||||||
// blockquote ends with at least one blank line
|
// blockquote ends with at least one blank line
|
||||||
// followed by something without a blockquote prefix
|
// followed by something without a blockquote prefix
|
||||||
break
|
break
|
||||||
|
@ -846,20 +846,20 @@ func (parser *Parser) quote(out *bytes.Buffer, data []byte) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
var cooked bytes.Buffer
|
var cooked bytes.Buffer
|
||||||
parser.block(&cooked, raw.Bytes())
|
p.block(&cooked, raw.Bytes())
|
||||||
parser.r.BlockQuote(out, cooked.Bytes())
|
p.r.BlockQuote(out, cooked.Bytes())
|
||||||
return end
|
return end
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns prefix length for block code
|
// returns prefix length for block code
|
||||||
func (parser *Parser) codePrefix(data []byte) int {
|
func (p *parser) codePrefix(data []byte) int {
|
||||||
if data[0] == ' ' && data[1] == ' ' && data[2] == ' ' && data[3] == ' ' {
|
if data[0] == ' ' && data[1] == ' ' && data[2] == ' ' && data[3] == ' ' {
|
||||||
return 4
|
return 4
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (parser *Parser) code(out *bytes.Buffer, data []byte) int {
|
func (p *parser) code(out *bytes.Buffer, data []byte) int {
|
||||||
var work bytes.Buffer
|
var work bytes.Buffer
|
||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
|
@ -870,8 +870,8 @@ func (parser *Parser) code(out *bytes.Buffer, data []byte) int {
|
||||||
}
|
}
|
||||||
i++
|
i++
|
||||||
|
|
||||||
blankline := parser.isEmpty(data[beg:i]) > 0
|
blankline := p.isEmpty(data[beg:i]) > 0
|
||||||
if pre := parser.codePrefix(data[beg:i]); pre > 0 {
|
if pre := p.codePrefix(data[beg:i]); pre > 0 {
|
||||||
beg += pre
|
beg += pre
|
||||||
} else if !blankline {
|
} else if !blankline {
|
||||||
// non-empty, non-prefixed line breaks the pre
|
// non-empty, non-prefixed line breaks the pre
|
||||||
|
@ -899,13 +899,13 @@ func (parser *Parser) code(out *bytes.Buffer, data []byte) int {
|
||||||
|
|
||||||
work.WriteByte('\n')
|
work.WriteByte('\n')
|
||||||
|
|
||||||
parser.r.BlockCode(out, work.Bytes(), "")
|
p.r.BlockCode(out, work.Bytes(), "")
|
||||||
|
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns unordered list item prefix
|
// returns unordered list item prefix
|
||||||
func (parser *Parser) uliPrefix(data []byte) int {
|
func (p *parser) uliPrefix(data []byte) int {
|
||||||
i := 0
|
i := 0
|
||||||
|
|
||||||
// start with up to 3 spaces
|
// start with up to 3 spaces
|
||||||
|
@ -913,7 +913,7 @@ func (parser *Parser) uliPrefix(data []byte) int {
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
// need a *, +, or - followed by a space/tab
|
// need a *, +, or - followed by a space
|
||||||
if (data[i] != '*' && data[i] != '+' && data[i] != '-') ||
|
if (data[i] != '*' && data[i] != '+' && data[i] != '-') ||
|
||||||
data[i+1] != ' ' {
|
data[i+1] != ' ' {
|
||||||
return 0
|
return 0
|
||||||
|
@ -922,7 +922,7 @@ func (parser *Parser) uliPrefix(data []byte) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns ordered list item prefix
|
// returns ordered list item prefix
|
||||||
func (parser *Parser) oliPrefix(data []byte) int {
|
func (p *parser) oliPrefix(data []byte) int {
|
||||||
i := 0
|
i := 0
|
||||||
|
|
||||||
// start with up to 3 spaces
|
// start with up to 3 spaces
|
||||||
|
@ -936,7 +936,7 @@ func (parser *Parser) oliPrefix(data []byte) int {
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
// we need >= 1 digits followed by a dot and a space/tab
|
// we need >= 1 digits followed by a dot and a space
|
||||||
if start == i || data[i] != '.' || data[i+1] != ' ' {
|
if start == i || data[i] != '.' || data[i+1] != ' ' {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
@ -944,12 +944,12 @@ func (parser *Parser) oliPrefix(data []byte) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse ordered or unordered list block
|
// parse ordered or unordered list block
|
||||||
func (parser *Parser) list(out *bytes.Buffer, data []byte, flags int) int {
|
func (p *parser) list(out *bytes.Buffer, data []byte, flags int) int {
|
||||||
i := 0
|
i := 0
|
||||||
flags |= LIST_ITEM_BEGINNING_OF_LIST
|
flags |= LIST_ITEM_BEGINNING_OF_LIST
|
||||||
work := func() bool {
|
work := func() bool {
|
||||||
for i < len(data) {
|
for i < len(data) {
|
||||||
skip := parser.listItem(out, data[i:], &flags)
|
skip := p.listItem(out, data[i:], &flags)
|
||||||
i += skip
|
i += skip
|
||||||
|
|
||||||
if skip == 0 || flags&LIST_ITEM_END_OF_LIST != 0 {
|
if skip == 0 || flags&LIST_ITEM_END_OF_LIST != 0 {
|
||||||
|
@ -960,22 +960,22 @@ func (parser *Parser) list(out *bytes.Buffer, data []byte, flags int) int {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
parser.r.List(out, work, flags)
|
p.r.List(out, work, flags)
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse a single list item.
|
// Parse a single list item.
|
||||||
// Assumes initial prefix is already removed if this is a sublist.
|
// Assumes initial prefix is already removed if this is a sublist.
|
||||||
func (parser *Parser) listItem(out *bytes.Buffer, data []byte, flags *int) int {
|
func (p *parser) listItem(out *bytes.Buffer, data []byte, flags *int) int {
|
||||||
// keep track of the indentation of the first line
|
// keep track of the indentation of the first line
|
||||||
itemIndent := 0
|
itemIndent := 0
|
||||||
for itemIndent < 3 && data[itemIndent] == ' ' {
|
for itemIndent < 3 && data[itemIndent] == ' ' {
|
||||||
itemIndent++
|
itemIndent++
|
||||||
}
|
}
|
||||||
|
|
||||||
i := parser.uliPrefix(data)
|
i := p.uliPrefix(data)
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
i = parser.oliPrefix(data)
|
i = p.oliPrefix(data)
|
||||||
}
|
}
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
return 0
|
return 0
|
||||||
|
@ -1003,7 +1003,7 @@ func (parser *Parser) listItem(out *bytes.Buffer, data []byte, flags *int) int {
|
||||||
containsBlankLine := false
|
containsBlankLine := false
|
||||||
sublist := 0
|
sublist := 0
|
||||||
|
|
||||||
loop:
|
gatherlines:
|
||||||
for line < len(data) {
|
for line < len(data) {
|
||||||
i++
|
i++
|
||||||
|
|
||||||
|
@ -1014,7 +1014,7 @@ loop:
|
||||||
|
|
||||||
// if it is an empty line, guess that it is part of this item
|
// if it is an empty line, guess that it is part of this item
|
||||||
// and move on to the next line
|
// and move on to the next line
|
||||||
if parser.isEmpty(data[line:i]) > 0 {
|
if p.isEmpty(data[line:i]) > 0 {
|
||||||
containsBlankLine = true
|
containsBlankLine = true
|
||||||
line = i
|
line = i
|
||||||
continue
|
continue
|
||||||
|
@ -1031,8 +1031,8 @@ loop:
|
||||||
// evaluate how this line fits in
|
// evaluate how this line fits in
|
||||||
switch {
|
switch {
|
||||||
// is this a nested list item?
|
// is this a nested list item?
|
||||||
case (parser.uliPrefix(chunk) > 0 && !parser.isHRule(chunk)) ||
|
case (p.uliPrefix(chunk) > 0 && !p.isHRule(chunk)) ||
|
||||||
parser.oliPrefix(chunk) > 0:
|
p.oliPrefix(chunk) > 0:
|
||||||
|
|
||||||
if containsBlankLine {
|
if containsBlankLine {
|
||||||
*flags |= LIST_ITEM_CONTAINS_BLOCK
|
*flags |= LIST_ITEM_CONTAINS_BLOCK
|
||||||
|
@ -1041,7 +1041,7 @@ loop:
|
||||||
// to be a nested list, it must be indented more
|
// to be a nested list, it must be indented more
|
||||||
// if not, it is the next item in the same list
|
// if not, it is the next item in the same list
|
||||||
if indent <= itemIndent {
|
if indent <= itemIndent {
|
||||||
break loop
|
break gatherlines
|
||||||
}
|
}
|
||||||
|
|
||||||
// is this the first item in the the nested list?
|
// is this the first item in the the nested list?
|
||||||
|
@ -1050,12 +1050,12 @@ loop:
|
||||||
}
|
}
|
||||||
|
|
||||||
// is this a nested prefix header?
|
// is this a nested prefix header?
|
||||||
case parser.isPrefixHeader(chunk):
|
case p.isPrefixHeader(chunk):
|
||||||
// if the header is not indented, it is not nested in the list
|
// if the header is not indented, it is not nested in the list
|
||||||
// and thus ends the list
|
// and thus ends the list
|
||||||
if containsBlankLine && indent < 4 {
|
if containsBlankLine && indent < 4 {
|
||||||
*flags |= LIST_ITEM_END_OF_LIST
|
*flags |= LIST_ITEM_END_OF_LIST
|
||||||
break loop
|
break gatherlines
|
||||||
}
|
}
|
||||||
*flags |= LIST_ITEM_CONTAINS_BLOCK
|
*flags |= LIST_ITEM_CONTAINS_BLOCK
|
||||||
|
|
||||||
|
@ -1064,7 +1064,7 @@ loop:
|
||||||
// (regardless of the indentation of the beginning of the item)
|
// (regardless of the indentation of the beginning of the item)
|
||||||
case containsBlankLine && indent < 4:
|
case containsBlankLine && indent < 4:
|
||||||
*flags |= LIST_ITEM_END_OF_LIST
|
*flags |= LIST_ITEM_END_OF_LIST
|
||||||
break loop
|
break gatherlines
|
||||||
|
|
||||||
// a blank line means this should be parsed as a block
|
// a blank line means this should be parsed as a block
|
||||||
case containsBlankLine:
|
case containsBlankLine:
|
||||||
|
@ -1085,18 +1085,18 @@ loop:
|
||||||
if *flags&LIST_ITEM_CONTAINS_BLOCK != 0 {
|
if *flags&LIST_ITEM_CONTAINS_BLOCK != 0 {
|
||||||
// intermediate render of block li
|
// intermediate render of block li
|
||||||
if sublist > 0 {
|
if sublist > 0 {
|
||||||
parser.block(&cooked, rawBytes[:sublist])
|
p.block(&cooked, rawBytes[:sublist])
|
||||||
parser.block(&cooked, rawBytes[sublist:])
|
p.block(&cooked, rawBytes[sublist:])
|
||||||
} else {
|
} else {
|
||||||
parser.block(&cooked, rawBytes)
|
p.block(&cooked, rawBytes)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// intermediate render of inline li
|
// intermediate render of inline li
|
||||||
if sublist > 0 {
|
if sublist > 0 {
|
||||||
parser.inline(&cooked, rawBytes[:sublist])
|
p.inline(&cooked, rawBytes[:sublist])
|
||||||
parser.block(&cooked, rawBytes[sublist:])
|
p.block(&cooked, rawBytes[sublist:])
|
||||||
} else {
|
} else {
|
||||||
parser.inline(&cooked, rawBytes)
|
p.inline(&cooked, rawBytes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1108,13 +1108,13 @@ loop:
|
||||||
for parsedEnd > 0 && cookedBytes[parsedEnd-1] == '\n' {
|
for parsedEnd > 0 && cookedBytes[parsedEnd-1] == '\n' {
|
||||||
parsedEnd--
|
parsedEnd--
|
||||||
}
|
}
|
||||||
parser.r.ListItem(out, cookedBytes[:parsedEnd], *flags)
|
p.r.ListItem(out, cookedBytes[:parsedEnd], *flags)
|
||||||
|
|
||||||
return line
|
return line
|
||||||
}
|
}
|
||||||
|
|
||||||
// render a single paragraph that has already been parsed out
|
// render a single paragraph that has already been parsed out
|
||||||
func (parser *Parser) renderParagraph(out *bytes.Buffer, data []byte) {
|
func (p *parser) renderParagraph(out *bytes.Buffer, data []byte) {
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -1134,13 +1134,13 @@ func (parser *Parser) renderParagraph(out *bytes.Buffer, data []byte) {
|
||||||
}
|
}
|
||||||
|
|
||||||
work := func() bool {
|
work := func() bool {
|
||||||
parser.inline(out, data[beg:end])
|
p.inline(out, data[beg:end])
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
parser.r.Paragraph(out, work)
|
p.r.Paragraph(out, work)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (parser *Parser) paragraph(out *bytes.Buffer, data []byte) int {
|
func (p *parser) paragraph(out *bytes.Buffer, data []byte) int {
|
||||||
// prev: index of 1st char of previous line
|
// prev: index of 1st char of previous line
|
||||||
// line: index of 1st char of current line
|
// line: index of 1st char of current line
|
||||||
// i: index of cursor/end of current line
|
// i: index of cursor/end of current line
|
||||||
|
@ -1154,16 +1154,16 @@ func (parser *Parser) paragraph(out *bytes.Buffer, data []byte) int {
|
||||||
line = i
|
line = i
|
||||||
|
|
||||||
// did we find a blank line marking the end of the paragraph?
|
// did we find a blank line marking the end of the paragraph?
|
||||||
if n := parser.isEmpty(current); n > 0 {
|
if n := p.isEmpty(current); n > 0 {
|
||||||
parser.renderParagraph(out, data[:i])
|
p.renderParagraph(out, data[:i])
|
||||||
return i + n
|
return i + n
|
||||||
}
|
}
|
||||||
|
|
||||||
// an underline under some text marks a header, so our paragraph ended on prev line
|
// an underline under some text marks a header, so our paragraph ended on prev line
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
if level := parser.isUnderlinedHeader(current); level > 0 {
|
if level := p.isUnderlinedHeader(current); level > 0 {
|
||||||
// render the paragraph
|
// render the paragraph
|
||||||
parser.renderParagraph(out, data[:prev])
|
p.renderParagraph(out, data[:prev])
|
||||||
|
|
||||||
// ignore leading and trailing whitespace
|
// ignore leading and trailing whitespace
|
||||||
eol := i - 1
|
eol := i - 1
|
||||||
|
@ -1176,13 +1176,13 @@ func (parser *Parser) paragraph(out *bytes.Buffer, data []byte) int {
|
||||||
|
|
||||||
// render the header
|
// render the header
|
||||||
// this ugly double closure avoids forcing variables onto the heap
|
// this ugly double closure avoids forcing variables onto the heap
|
||||||
work := func(o *bytes.Buffer, p *Parser, d []byte) func() bool {
|
work := func(o *bytes.Buffer, pp *parser, d []byte) func() bool {
|
||||||
return func() bool {
|
return func() bool {
|
||||||
p.inline(o, d)
|
pp.inline(o, d)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}(out, parser, data[prev:eol])
|
}(out, p, data[prev:eol])
|
||||||
parser.r.Header(out, work, level)
|
p.r.Header(out, work, level)
|
||||||
|
|
||||||
// find the end of the underline
|
// find the end of the underline
|
||||||
for data[i] != '\n' {
|
for data[i] != '\n' {
|
||||||
|
@ -1193,17 +1193,17 @@ func (parser *Parser) paragraph(out *bytes.Buffer, data []byte) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the next line starts a block of HTML, then the paragraph ends here
|
// if the next line starts a block of HTML, then the paragraph ends here
|
||||||
if parser.flags&EXTENSION_LAX_HTML_BLOCKS != 0 {
|
if p.flags&EXTENSION_LAX_HTML_BLOCKS != 0 {
|
||||||
if data[i] == '<' && parser.html(out, current, false) > 0 {
|
if data[i] == '<' && p.html(out, current, false) > 0 {
|
||||||
// rewind to before the HTML block
|
// rewind to before the HTML block
|
||||||
parser.renderParagraph(out, data[:i])
|
p.renderParagraph(out, data[:i])
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if there's a prefixed header or a horizontal rule after this, paragraph is over
|
// if there's a prefixed header or a horizontal rule after this, paragraph is over
|
||||||
if parser.isPrefixHeader(current) || parser.isHRule(current) {
|
if p.isPrefixHeader(current) || p.isHRule(current) {
|
||||||
parser.renderParagraph(out, data[:i])
|
p.renderParagraph(out, data[:i])
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1214,6 +1214,6 @@ func (parser *Parser) paragraph(out *bytes.Buffer, data []byte) int {
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
parser.renderParagraph(out, data[:i])
|
p.renderParagraph(out, data[:i])
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
14
html.go
14
html.go
|
@ -38,6 +38,9 @@ const (
|
||||||
HTML_SMARTYPANTS_LATEX_DASHES
|
HTML_SMARTYPANTS_LATEX_DASHES
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Html is a type that implements the Renderer interface for HTML output.
|
||||||
|
//
|
||||||
|
// Do not create this directly, instead use the HtmlRenderer function.
|
||||||
type Html struct {
|
type Html struct {
|
||||||
flags int // HTML_* options
|
flags int // HTML_* options
|
||||||
closeTag string // how to end singleton tags: either " />\n" or ">\n"
|
closeTag string // how to end singleton tags: either " />\n" or ">\n"
|
||||||
|
@ -50,7 +53,7 @@ type Html struct {
|
||||||
currentLevel int
|
currentLevel int
|
||||||
toc *bytes.Buffer
|
toc *bytes.Buffer
|
||||||
|
|
||||||
smartypants *SmartypantsRenderer
|
smartypants *smartypantsRenderer
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -58,6 +61,13 @@ const (
|
||||||
htmlClose = ">\n"
|
htmlClose = ">\n"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// HtmlRenderer creates and configures an Html object, which
|
||||||
|
// satisfies the Renderer interface.
|
||||||
|
//
|
||||||
|
// flags is a set of HTML_* options ORed together.
|
||||||
|
// 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.
|
||||||
func HtmlRenderer(flags int, title string, css string) Renderer {
|
func HtmlRenderer(flags int, title string, css string) Renderer {
|
||||||
// configure the rendering engine
|
// configure the rendering engine
|
||||||
closeTag := htmlClose
|
closeTag := htmlClose
|
||||||
|
@ -75,7 +85,7 @@ func HtmlRenderer(flags int, title string, css string) Renderer {
|
||||||
currentLevel: 0,
|
currentLevel: 0,
|
||||||
toc: new(bytes.Buffer),
|
toc: new(bytes.Buffer),
|
||||||
|
|
||||||
smartypants: Smartypants(flags),
|
smartypants: smartypants(flags),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
100
inline.go
100
inline.go
|
@ -22,21 +22,21 @@ import (
|
||||||
// data is the complete block being rendered
|
// data is the complete block being rendered
|
||||||
// offset is the number of valid chars before the current cursor
|
// offset is the number of valid chars before the current cursor
|
||||||
|
|
||||||
func (parser *Parser) inline(out *bytes.Buffer, data []byte) {
|
func (p *parser) inline(out *bytes.Buffer, data []byte) {
|
||||||
// this is called recursively: enforce a maximum depth
|
// this is called recursively: enforce a maximum depth
|
||||||
if parser.nesting >= parser.maxNesting {
|
if p.nesting >= p.maxNesting {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
parser.nesting++
|
p.nesting++
|
||||||
|
|
||||||
i, end := 0, 0
|
i, end := 0, 0
|
||||||
for i < len(data) {
|
for i < len(data) {
|
||||||
// copy inactive chars into the output
|
// copy inactive chars into the output
|
||||||
for end < len(data) && parser.inlineCallback[data[end]] == nil {
|
for end < len(data) && p.inlineCallback[data[end]] == nil {
|
||||||
end++
|
end++
|
||||||
}
|
}
|
||||||
|
|
||||||
parser.r.NormalText(out, data[i:end])
|
p.r.NormalText(out, data[i:end])
|
||||||
|
|
||||||
if end >= len(data) {
|
if end >= len(data) {
|
||||||
break
|
break
|
||||||
|
@ -44,8 +44,8 @@ func (parser *Parser) inline(out *bytes.Buffer, data []byte) {
|
||||||
i = end
|
i = end
|
||||||
|
|
||||||
// call the trigger
|
// call the trigger
|
||||||
handler := parser.inlineCallback[data[end]]
|
handler := p.inlineCallback[data[end]]
|
||||||
if consumed := handler(parser, out, data, i); consumed == 0 {
|
if consumed := handler(p, out, data, i); consumed == 0 {
|
||||||
// no action from the callback; buffer the byte for later
|
// no action from the callback; buffer the byte for later
|
||||||
end = i + 1
|
end = i + 1
|
||||||
} else {
|
} else {
|
||||||
|
@ -55,11 +55,11 @@ func (parser *Parser) inline(out *bytes.Buffer, data []byte) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
parser.nesting--
|
p.nesting--
|
||||||
}
|
}
|
||||||
|
|
||||||
// single and double emphasis parsing
|
// single and double emphasis parsing
|
||||||
func emphasis(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
func emphasis(p *parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
data = data[offset:]
|
data = data[offset:]
|
||||||
c := data[0]
|
c := data[0]
|
||||||
ret := 0
|
ret := 0
|
||||||
|
@ -70,7 +70,7 @@ func emphasis(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
if c == '~' || isspace(data[1]) {
|
if c == '~' || isspace(data[1]) {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
if ret = helperEmphasis(parser, out, data[1:], c); ret == 0 {
|
if ret = helperEmphasis(p, out, data[1:], c); ret == 0 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,7 +81,7 @@ func emphasis(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
if isspace(data[2]) {
|
if isspace(data[2]) {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
if ret = helperDoubleEmphasis(parser, out, data[2:], c); ret == 0 {
|
if ret = helperDoubleEmphasis(p, out, data[2:], c); ret == 0 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,7 +92,7 @@ func emphasis(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
if c == '~' || isspace(data[3]) {
|
if c == '~' || isspace(data[3]) {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
if ret = helperTripleEmphasis(parser, out, data, 3, c); ret == 0 {
|
if ret = helperTripleEmphasis(p, out, data, 3, c); ret == 0 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,7 +102,7 @@ func emphasis(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func codeSpan(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
func codeSpan(p *parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
data = data[offset:]
|
data = data[offset:]
|
||||||
|
|
||||||
nb := 0
|
nb := 0
|
||||||
|
@ -140,7 +140,7 @@ func codeSpan(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
|
|
||||||
// render the code span
|
// render the code span
|
||||||
if fBegin != fEnd {
|
if fBegin != fEnd {
|
||||||
parser.r.CodeSpan(out, data[fBegin:fEnd])
|
p.r.CodeSpan(out, data[fBegin:fEnd])
|
||||||
}
|
}
|
||||||
|
|
||||||
return end
|
return end
|
||||||
|
@ -149,7 +149,7 @@ func codeSpan(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
|
|
||||||
// newline preceded by two spaces becomes <br>
|
// newline preceded by two spaces becomes <br>
|
||||||
// newline without two spaces works when EXTENSION_HARD_LINE_BREAK is enabled
|
// newline without two spaces works when EXTENSION_HARD_LINE_BREAK is enabled
|
||||||
func lineBreak(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
func lineBreak(p *parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
// remove trailing spaces from out
|
// remove trailing spaces from out
|
||||||
outBytes := out.Bytes()
|
outBytes := out.Bytes()
|
||||||
end := len(outBytes)
|
end := len(outBytes)
|
||||||
|
@ -160,18 +160,18 @@ func lineBreak(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
out.Truncate(eol)
|
out.Truncate(eol)
|
||||||
|
|
||||||
// should there be a hard line break here?
|
// should there be a hard line break here?
|
||||||
if parser.flags&EXTENSION_HARD_LINE_BREAK == 0 && end-eol < 2 {
|
if p.flags&EXTENSION_HARD_LINE_BREAK == 0 && end-eol < 2 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
parser.r.LineBreak(out)
|
p.r.LineBreak(out)
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// '[': parse a link or an image
|
// '[': parse a link or an image
|
||||||
func link(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
// no links allowed inside other links
|
// no links allowed inside other links
|
||||||
if parser.insideLink {
|
if p.insideLink {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -348,7 +348,7 @@ func link(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
|
|
||||||
// find the reference with matching id (ids are case-insensitive)
|
// find the reference with matching id (ids are case-insensitive)
|
||||||
key := string(bytes.ToLower(id))
|
key := string(bytes.ToLower(id))
|
||||||
lr, ok := parser.refs[key]
|
lr, ok := p.refs[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
@ -382,7 +382,7 @@ func link(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
|
|
||||||
// find the reference with matching id
|
// find the reference with matching id
|
||||||
key := string(bytes.ToLower(id))
|
key := string(bytes.ToLower(id))
|
||||||
lr, ok := parser.refs[key]
|
lr, ok := p.refs[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
@ -402,10 +402,10 @@ func link(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
content.Write(data[1:txtE])
|
content.Write(data[1:txtE])
|
||||||
} else {
|
} else {
|
||||||
// links cannot contain other links, so turn off link parsing temporarily
|
// links cannot contain other links, so turn off link parsing temporarily
|
||||||
insideLink := parser.insideLink
|
insideLink := p.insideLink
|
||||||
parser.insideLink = true
|
p.insideLink = true
|
||||||
parser.inline(&content, data[1:txtE])
|
p.inline(&content, data[1:txtE])
|
||||||
parser.insideLink = insideLink
|
p.insideLink = insideLink
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -429,16 +429,16 @@ func link(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
out.Truncate(outSize - 1)
|
out.Truncate(outSize - 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
parser.r.Image(out, uLink, title, content.Bytes())
|
p.r.Image(out, uLink, title, content.Bytes())
|
||||||
} else {
|
} else {
|
||||||
parser.r.Link(out, uLink, title, content.Bytes())
|
p.r.Link(out, uLink, title, content.Bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
// '<' when tags or autolinks are allowed
|
// '<' when tags or autolinks are allowed
|
||||||
func leftAngle(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
func leftAngle(p *parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
data = data[offset:]
|
data = data[offset:]
|
||||||
altype := LINK_TYPE_NOT_AUTOLINK
|
altype := LINK_TYPE_NOT_AUTOLINK
|
||||||
end := tagLength(data, &altype)
|
end := tagLength(data, &altype)
|
||||||
|
@ -448,10 +448,10 @@ func leftAngle(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
var uLink bytes.Buffer
|
var uLink bytes.Buffer
|
||||||
unescapeText(&uLink, data[1:end+1-2])
|
unescapeText(&uLink, data[1:end+1-2])
|
||||||
if uLink.Len() > 0 {
|
if uLink.Len() > 0 {
|
||||||
parser.r.AutoLink(out, uLink.Bytes(), altype)
|
p.r.AutoLink(out, uLink.Bytes(), altype)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
parser.r.RawHtmlTag(out, data[:end])
|
p.r.RawHtmlTag(out, data[:end])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -461,7 +461,7 @@ func leftAngle(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
// '\\' backslash escape
|
// '\\' backslash escape
|
||||||
var escapeChars = []byte("\\`*_{}[]()#+-.!:|&<>")
|
var escapeChars = []byte("\\`*_{}[]()#+-.!:|&<>")
|
||||||
|
|
||||||
func escape(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
func escape(p *parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
data = data[offset:]
|
data = data[offset:]
|
||||||
|
|
||||||
if len(data) > 1 {
|
if len(data) > 1 {
|
||||||
|
@ -469,7 +469,7 @@ func escape(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
parser.r.NormalText(out, data[1:2])
|
p.r.NormalText(out, data[1:2])
|
||||||
}
|
}
|
||||||
|
|
||||||
return 2
|
return 2
|
||||||
|
@ -498,7 +498,7 @@ func unescapeText(ob *bytes.Buffer, src []byte) {
|
||||||
|
|
||||||
// '&' escaped when it doesn't belong to an entity
|
// '&' escaped when it doesn't belong to an entity
|
||||||
// valid entities are assumed to be anything matching &#?[A-Za-z0-9]+;
|
// valid entities are assumed to be anything matching &#?[A-Za-z0-9]+;
|
||||||
func entity(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
func entity(p *parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
data = data[offset:]
|
data = data[offset:]
|
||||||
|
|
||||||
end := 1
|
end := 1
|
||||||
|
@ -517,14 +517,14 @@ func entity(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
return 0 // lone '&'
|
return 0 // lone '&'
|
||||||
}
|
}
|
||||||
|
|
||||||
parser.r.Entity(out, data[:end])
|
p.r.Entity(out, data[:end])
|
||||||
|
|
||||||
return end
|
return end
|
||||||
}
|
}
|
||||||
|
|
||||||
func autoLink(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
func autoLink(p *parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
// quick check to rule out most false hits on ':'
|
// quick check to rule out most false hits on ':'
|
||||||
if parser.insideLink || len(data) < offset+3 || data[offset+1] != '/' || data[offset+2] != '/' {
|
if p.insideLink || len(data) < offset+3 || data[offset+1] != '/' || data[offset+2] != '/' {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -622,7 +622,7 @@ func autoLink(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||||
unescapeText(&uLink, data[:linkEnd])
|
unescapeText(&uLink, data[:linkEnd])
|
||||||
|
|
||||||
if uLink.Len() > 0 {
|
if uLink.Len() > 0 {
|
||||||
parser.r.AutoLink(out, uLink.Bytes(), LINK_TYPE_NORMAL)
|
p.r.AutoLink(out, uLink.Bytes(), LINK_TYPE_NORMAL)
|
||||||
}
|
}
|
||||||
|
|
||||||
return linkEnd - rewind
|
return linkEnd - rewind
|
||||||
|
@ -832,7 +832,7 @@ func helperFindEmphChar(data []byte, c byte) int {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func helperEmphasis(parser *Parser, out *bytes.Buffer, data []byte, c byte) int {
|
func helperEmphasis(p *parser, out *bytes.Buffer, data []byte, c byte) int {
|
||||||
i := 0
|
i := 0
|
||||||
|
|
||||||
// skip one symbol if coming from emph3
|
// skip one symbol if coming from emph3
|
||||||
|
@ -857,15 +857,15 @@ func helperEmphasis(parser *Parser, out *bytes.Buffer, data []byte, c byte) int
|
||||||
|
|
||||||
if data[i] == c && !isspace(data[i-1]) {
|
if data[i] == c && !isspace(data[i-1]) {
|
||||||
|
|
||||||
if parser.flags&EXTENSION_NO_INTRA_EMPHASIS != 0 {
|
if p.flags&EXTENSION_NO_INTRA_EMPHASIS != 0 {
|
||||||
if !(i+1 == len(data) || isspace(data[i+1]) || ispunct(data[i+1])) {
|
if !(i+1 == len(data) || isspace(data[i+1]) || ispunct(data[i+1])) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var work bytes.Buffer
|
var work bytes.Buffer
|
||||||
parser.inline(&work, data[:i])
|
p.inline(&work, data[:i])
|
||||||
parser.r.Emphasis(out, work.Bytes())
|
p.r.Emphasis(out, work.Bytes())
|
||||||
return i + 1
|
return i + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -873,7 +873,7 @@ func helperEmphasis(parser *Parser, out *bytes.Buffer, data []byte, c byte) int
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func helperDoubleEmphasis(parser *Parser, out *bytes.Buffer, data []byte, c byte) int {
|
func helperDoubleEmphasis(p *parser, out *bytes.Buffer, data []byte, c byte) int {
|
||||||
i := 0
|
i := 0
|
||||||
|
|
||||||
for i < len(data) {
|
for i < len(data) {
|
||||||
|
@ -885,14 +885,14 @@ func helperDoubleEmphasis(parser *Parser, out *bytes.Buffer, data []byte, c byte
|
||||||
|
|
||||||
if i+1 < len(data) && data[i] == c && data[i+1] == c && i > 0 && !isspace(data[i-1]) {
|
if i+1 < len(data) && data[i] == c && data[i+1] == c && i > 0 && !isspace(data[i-1]) {
|
||||||
var work bytes.Buffer
|
var work bytes.Buffer
|
||||||
parser.inline(&work, data[:i])
|
p.inline(&work, data[:i])
|
||||||
|
|
||||||
if work.Len() > 0 {
|
if work.Len() > 0 {
|
||||||
// pick the right renderer
|
// pick the right renderer
|
||||||
if c == '~' {
|
if c == '~' {
|
||||||
parser.r.StrikeThrough(out, work.Bytes())
|
p.r.StrikeThrough(out, work.Bytes())
|
||||||
} else {
|
} else {
|
||||||
parser.r.DoubleEmphasis(out, work.Bytes())
|
p.r.DoubleEmphasis(out, work.Bytes())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return i + 2
|
return i + 2
|
||||||
|
@ -902,7 +902,7 @@ func helperDoubleEmphasis(parser *Parser, out *bytes.Buffer, data []byte, c byte
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func helperTripleEmphasis(parser *Parser, out *bytes.Buffer, data []byte, offset int, c byte) int {
|
func helperTripleEmphasis(p *parser, out *bytes.Buffer, data []byte, offset int, c byte) int {
|
||||||
i := 0
|
i := 0
|
||||||
origData := data
|
origData := data
|
||||||
data = data[offset:]
|
data = data[offset:]
|
||||||
|
@ -924,14 +924,14 @@ func helperTripleEmphasis(parser *Parser, out *bytes.Buffer, data []byte, offset
|
||||||
// triple symbol found
|
// triple symbol found
|
||||||
var work bytes.Buffer
|
var work bytes.Buffer
|
||||||
|
|
||||||
parser.inline(&work, data[:i])
|
p.inline(&work, data[:i])
|
||||||
if work.Len() > 0 {
|
if work.Len() > 0 {
|
||||||
parser.r.TripleEmphasis(out, work.Bytes())
|
p.r.TripleEmphasis(out, work.Bytes())
|
||||||
}
|
}
|
||||||
return i + 3
|
return i + 3
|
||||||
case (i+1 < len(data) && data[i+1] == c):
|
case (i+1 < len(data) && data[i+1] == c):
|
||||||
// double symbol found, hand over to emph1
|
// double symbol found, hand over to emph1
|
||||||
length = helperEmphasis(parser, out, origData[offset-2:], c)
|
length = helperEmphasis(p, out, origData[offset-2:], c)
|
||||||
if length == 0 {
|
if length == 0 {
|
||||||
return 0
|
return 0
|
||||||
} else {
|
} else {
|
||||||
|
@ -939,7 +939,7 @@ func helperTripleEmphasis(parser *Parser, out *bytes.Buffer, data []byte, offset
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
// single symbol found, hand over to emph2
|
// single symbol found, hand over to emph2
|
||||||
length = helperDoubleEmphasis(parser, out, origData[offset-1:], c)
|
length = helperDoubleEmphasis(p, out, origData[offset-1:], c)
|
||||||
if length == 0 {
|
if length == 0 {
|
||||||
return 0
|
return 0
|
||||||
} else {
|
} else {
|
||||||
|
|
8
latex.go
8
latex.go
|
@ -19,10 +19,18 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Latex is a type that implements the Renderer interface for LaTeX output.
|
||||||
|
//
|
||||||
|
// Do not create this directly, instead use the LatexRenderer function.
|
||||||
type Latex struct {
|
type Latex struct {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LatexRenderer creates and configures a Latex object, which
|
||||||
|
// satisfies the Renderer interface.
|
||||||
|
//
|
||||||
|
// flags is a set of LATEX_* options ORed together (currently no such options
|
||||||
|
// are defined).
|
||||||
func LatexRenderer(flags int) Renderer {
|
func LatexRenderer(flags int) Renderer {
|
||||||
return &Latex{}
|
return &Latex{}
|
||||||
}
|
}
|
||||||
|
|
110
markdown.go
110
markdown.go
|
@ -45,7 +45,7 @@ const (
|
||||||
LINK_TYPE_EMAIL
|
LINK_TYPE_EMAIL
|
||||||
)
|
)
|
||||||
|
|
||||||
// These are the possible flag values for the listitem renderer.
|
// These are the possible flag values for the ListItem renderer.
|
||||||
// Multiple flag values may be ORed together.
|
// Multiple flag values may be ORed together.
|
||||||
// These are mostly of interest if you are writing a new output format.
|
// These are mostly of interest if you are writing a new output format.
|
||||||
const (
|
const (
|
||||||
|
@ -97,8 +97,17 @@ var blockTags = map[string]bool{
|
||||||
"blockquote": true,
|
"blockquote": true,
|
||||||
}
|
}
|
||||||
|
|
||||||
// This interface defines the rendering interface.
|
// Renderer is the rendering interface.
|
||||||
// This is mostly of interest if you are implementing a new rendering format.
|
// This is mostly of interest if you are implementing a new rendering format.
|
||||||
|
//
|
||||||
|
// When a byte slice is provided, it contains the (rendered) contents of the
|
||||||
|
// element.
|
||||||
|
//
|
||||||
|
// When a callback is provided instead, it will write the contents of the
|
||||||
|
// respective element directly to the output buffer and return true on success.
|
||||||
|
// If the callback returns false, the rendering function should reset the
|
||||||
|
// output buffer as though it had never been called.
|
||||||
|
//
|
||||||
// Currently Html and Latex implementations are provided
|
// Currently Html and Latex implementations are provided
|
||||||
type Renderer interface {
|
type Renderer interface {
|
||||||
// block-level callbacks
|
// block-level callbacks
|
||||||
|
@ -137,12 +146,11 @@ type Renderer interface {
|
||||||
|
|
||||||
// Callback functions for inline parsing. One such function is defined
|
// Callback functions for inline parsing. One such function is defined
|
||||||
// for each character that triggers a response when parsing inline data.
|
// for each character that triggers a response when parsing inline data.
|
||||||
type inlineParser func(parser *Parser, out *bytes.Buffer, data []byte, offset int) int
|
type inlineParser func(p *parser, out *bytes.Buffer, data []byte, offset int) int
|
||||||
|
|
||||||
// The main parser object.
|
// Parser holds runtime state used by the parser.
|
||||||
// This is constructed by the Markdown function and
|
// This is constructed by the Markdown function.
|
||||||
// contains state used during the parsing process.
|
type parser struct {
|
||||||
type Parser struct {
|
|
||||||
r Renderer
|
r Renderer
|
||||||
refs map[string]*reference
|
refs map[string]*reference
|
||||||
inlineCallback [256]inlineParser
|
inlineCallback [256]inlineParser
|
||||||
|
@ -159,7 +167,8 @@ type Parser struct {
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
|
|
||||||
// Call Markdown with no extensions
|
// MarkdownBasic is a convenience function for simple rendering.
|
||||||
|
// It processes markdown input with no extensions enabled.
|
||||||
func MarkdownBasic(input []byte) []byte {
|
func MarkdownBasic(input []byte) []byte {
|
||||||
// set up the HTML renderer
|
// set up the HTML renderer
|
||||||
htmlFlags := HTML_USE_XHTML
|
htmlFlags := HTML_USE_XHTML
|
||||||
|
@ -172,6 +181,22 @@ func MarkdownBasic(input []byte) []byte {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call Markdown with most useful extensions enabled
|
// Call Markdown with most useful extensions enabled
|
||||||
|
// MarkdownCommon is a convenience function for simple rendering.
|
||||||
|
// It processes markdown input with common extensions enabled, including:
|
||||||
|
//
|
||||||
|
// * Smartypants processing with smart fractions and LaTeX dashes
|
||||||
|
//
|
||||||
|
// * Intra-word emphasis supression
|
||||||
|
//
|
||||||
|
// * Tables
|
||||||
|
//
|
||||||
|
// * Fenced code blocks
|
||||||
|
//
|
||||||
|
// * Autolinking
|
||||||
|
//
|
||||||
|
// * Strikethrough support
|
||||||
|
//
|
||||||
|
// * Strict header parsing
|
||||||
func MarkdownCommon(input []byte) []byte {
|
func MarkdownCommon(input []byte) []byte {
|
||||||
// set up the HTML renderer
|
// set up the HTML renderer
|
||||||
htmlFlags := 0
|
htmlFlags := 0
|
||||||
|
@ -193,9 +218,13 @@ func MarkdownCommon(input []byte) []byte {
|
||||||
return Markdown(input, renderer, extensions)
|
return Markdown(input, renderer, extensions)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse and render a block of markdown-encoded text.
|
// Markdown is the main rendering function.
|
||||||
// The renderer is used to format the output, and extensions dictates which
|
// It parses and renders a block of markdown-encoded text.
|
||||||
// non-standard extensions are enabled.
|
// The supplied Renderer is used to format the output, and extensions dictates
|
||||||
|
// which non-standard extensions are enabled.
|
||||||
|
//
|
||||||
|
// To use the supplied Html or LaTeX renderers, see HtmlRenderer and
|
||||||
|
// LatexRenderer, respectively.
|
||||||
func Markdown(input []byte, renderer Renderer, extensions int) []byte {
|
func Markdown(input []byte, renderer Renderer, extensions int) []byte {
|
||||||
// no point in parsing if we can't render
|
// no point in parsing if we can't render
|
||||||
if renderer == nil {
|
if renderer == nil {
|
||||||
|
@ -203,32 +232,32 @@ func Markdown(input []byte, renderer Renderer, extensions int) []byte {
|
||||||
}
|
}
|
||||||
|
|
||||||
// fill in the render structure
|
// fill in the render structure
|
||||||
parser := new(Parser)
|
p := new(parser)
|
||||||
parser.r = renderer
|
p.r = renderer
|
||||||
parser.flags = extensions
|
p.flags = extensions
|
||||||
parser.refs = make(map[string]*reference)
|
p.refs = make(map[string]*reference)
|
||||||
parser.maxNesting = 16
|
p.maxNesting = 16
|
||||||
parser.insideLink = false
|
p.insideLink = false
|
||||||
|
|
||||||
// register inline parsers
|
// register inline parsers
|
||||||
parser.inlineCallback['*'] = emphasis
|
p.inlineCallback['*'] = emphasis
|
||||||
parser.inlineCallback['_'] = emphasis
|
p.inlineCallback['_'] = emphasis
|
||||||
if extensions&EXTENSION_STRIKETHROUGH != 0 {
|
if extensions&EXTENSION_STRIKETHROUGH != 0 {
|
||||||
parser.inlineCallback['~'] = emphasis
|
p.inlineCallback['~'] = emphasis
|
||||||
}
|
}
|
||||||
parser.inlineCallback['`'] = codeSpan
|
p.inlineCallback['`'] = codeSpan
|
||||||
parser.inlineCallback['\n'] = lineBreak
|
p.inlineCallback['\n'] = lineBreak
|
||||||
parser.inlineCallback['['] = link
|
p.inlineCallback['['] = link
|
||||||
parser.inlineCallback['<'] = leftAngle
|
p.inlineCallback['<'] = leftAngle
|
||||||
parser.inlineCallback['\\'] = escape
|
p.inlineCallback['\\'] = escape
|
||||||
parser.inlineCallback['&'] = entity
|
p.inlineCallback['&'] = entity
|
||||||
|
|
||||||
if extensions&EXTENSION_AUTOLINK != 0 {
|
if extensions&EXTENSION_AUTOLINK != 0 {
|
||||||
parser.inlineCallback[':'] = autoLink
|
p.inlineCallback[':'] = autoLink
|
||||||
}
|
}
|
||||||
|
|
||||||
first := firstPass(parser, input)
|
first := firstPass(p, input)
|
||||||
second := secondPass(parser, first)
|
second := secondPass(p, first)
|
||||||
|
|
||||||
return second
|
return second
|
||||||
}
|
}
|
||||||
|
@ -238,15 +267,15 @@ func Markdown(input []byte, renderer Renderer, extensions int) []byte {
|
||||||
// - expand tabs
|
// - expand tabs
|
||||||
// - normalize newlines
|
// - normalize newlines
|
||||||
// - copy everything else
|
// - copy everything else
|
||||||
func firstPass(parser *Parser, input []byte) []byte {
|
func firstPass(p *parser, input []byte) []byte {
|
||||||
var out bytes.Buffer
|
var out bytes.Buffer
|
||||||
tabSize := TAB_SIZE_DEFAULT
|
tabSize := TAB_SIZE_DEFAULT
|
||||||
if parser.flags&EXTENSION_TAB_SIZE_EIGHT != 0 {
|
if p.flags&EXTENSION_TAB_SIZE_EIGHT != 0 {
|
||||||
tabSize = TAB_SIZE_EIGHT
|
tabSize = TAB_SIZE_EIGHT
|
||||||
}
|
}
|
||||||
beg, end := 0, 0
|
beg, end := 0, 0
|
||||||
for beg < len(input) { // iterate over lines
|
for beg < len(input) { // iterate over lines
|
||||||
if end = isReference(parser, input[beg:]); end > 0 {
|
if end = isReference(p, input[beg:]); end > 0 {
|
||||||
beg += end
|
beg += end
|
||||||
} else { // skip to the next line
|
} else { // skip to the next line
|
||||||
end = beg
|
end = beg
|
||||||
|
@ -280,14 +309,14 @@ func firstPass(parser *Parser, input []byte) []byte {
|
||||||
}
|
}
|
||||||
|
|
||||||
// second pass: actual rendering
|
// second pass: actual rendering
|
||||||
func secondPass(parser *Parser, input []byte) []byte {
|
func secondPass(p *parser, input []byte) []byte {
|
||||||
var output bytes.Buffer
|
var output bytes.Buffer
|
||||||
|
|
||||||
parser.r.DocumentHeader(&output)
|
p.r.DocumentHeader(&output)
|
||||||
parser.block(&output, input)
|
p.block(&output, input)
|
||||||
parser.r.DocumentFooter(&output)
|
p.r.DocumentFooter(&output)
|
||||||
|
|
||||||
if parser.nesting != 0 {
|
if p.nesting != 0 {
|
||||||
panic("Nesting level did not end at zero")
|
panic("Nesting level did not end at zero")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -321,7 +350,7 @@ type reference struct {
|
||||||
// (in the render struct).
|
// (in the render struct).
|
||||||
// Returns the number of bytes to skip to move past it,
|
// Returns the number of bytes to skip to move past it,
|
||||||
// or zero if the first line is not a reference.
|
// or zero if the first line is not a reference.
|
||||||
func isReference(parser *Parser, data []byte) int {
|
func isReference(p *parser, data []byte) int {
|
||||||
// up to 3 optional leading spaces
|
// up to 3 optional leading spaces
|
||||||
if len(data) < 4 {
|
if len(data) < 4 {
|
||||||
return 0
|
return 0
|
||||||
|
@ -437,13 +466,10 @@ func isReference(parser *Parser, data []byte) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// a valid ref has been found
|
// a valid ref has been found
|
||||||
if parser == nil {
|
|
||||||
return lineEnd
|
|
||||||
}
|
|
||||||
|
|
||||||
// id matches are case-insensitive
|
// id matches are case-insensitive
|
||||||
id := string(bytes.ToLower(data[idOffset:idEnd]))
|
id := string(bytes.ToLower(data[idOffset:idEnd]))
|
||||||
parser.refs[id] = &reference{
|
p.refs[id] = &reference{
|
||||||
link: data[linkOffset:linkEnd],
|
link: data[linkOffset:linkEnd],
|
||||||
title: data[titleOffset:titleEnd],
|
title: data[titleOffset:titleEnd],
|
||||||
}
|
}
|
||||||
|
|
|
@ -343,10 +343,10 @@ func smartLeftAngle(out *bytes.Buffer, smrt *smartypantsData, previousChar byte,
|
||||||
|
|
||||||
type smartCallback func(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int
|
type smartCallback func(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int
|
||||||
|
|
||||||
type SmartypantsRenderer [256]smartCallback
|
type smartypantsRenderer [256]smartCallback
|
||||||
|
|
||||||
func Smartypants(flags int) *SmartypantsRenderer {
|
func smartypants(flags int) *smartypantsRenderer {
|
||||||
r := new(SmartypantsRenderer)
|
r := new(smartypantsRenderer)
|
||||||
r['"'] = smartDoubleQuote
|
r['"'] = smartDoubleQuote
|
||||||
r['&'] = smartAmp
|
r['&'] = smartAmp
|
||||||
r['\''] = smartSingleQuote
|
r['\''] = smartSingleQuote
|
||||||
|
|
Loading…
Reference in New Issue
Block a user