2011-05-30 01:00:31 +02:00
|
|
|
//
|
2011-06-28 04:11:32 +02:00
|
|
|
// Blackfriday Markdown Processor
|
|
|
|
// Available at http://github.com/russross/blackfriday
|
|
|
|
//
|
|
|
|
// Copyright © 2011 Russ Ross <russ@russross.com>.
|
2011-06-28 19:30:10 +02:00
|
|
|
// Distributed under the Simplified BSD License.
|
2011-06-28 04:11:32 +02:00
|
|
|
// See README.md for details.
|
2011-05-30 01:00:31 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// Functions to parse inline elements.
|
|
|
|
//
|
|
|
|
|
|
|
|
package blackfriday
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Functions to parse text within a block
|
|
|
|
// Each function returns the number of chars taken care of
|
|
|
|
// data is the complete block being rendered
|
|
|
|
// offset is the number of valid chars before the current cursor
|
|
|
|
|
2011-06-29 02:58:53 +02:00
|
|
|
func (parser *Parser) parseInline(out *bytes.Buffer, data []byte) {
|
2011-06-01 00:28:07 +02:00
|
|
|
// this is called recursively: enforce a maximum depth
|
2011-06-29 02:58:53 +02:00
|
|
|
if parser.nesting >= parser.maxNesting {
|
2011-05-30 01:00:31 +02:00
|
|
|
return
|
|
|
|
}
|
2011-06-29 02:58:53 +02:00
|
|
|
parser.nesting++
|
2011-05-30 01:00:31 +02:00
|
|
|
|
|
|
|
i, end := 0, 0
|
|
|
|
for i < len(data) {
|
|
|
|
// copy inactive chars into the output
|
2011-06-29 02:58:53 +02:00
|
|
|
for end < len(data) && parser.inline[data[end]] == nil {
|
2011-05-30 01:00:31 +02:00
|
|
|
end++
|
|
|
|
}
|
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
parser.r.NormalText(out, data[i:end])
|
2011-05-30 01:00:31 +02:00
|
|
|
|
|
|
|
if end >= len(data) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
i = end
|
|
|
|
|
|
|
|
// call the trigger
|
2011-06-29 02:58:53 +02:00
|
|
|
handler := parser.inline[data[end]]
|
2011-06-29 19:21:46 +02:00
|
|
|
if consumed := handler(parser, out, data, i); consumed == 0 {
|
2011-06-01 00:28:07 +02:00
|
|
|
// no action from the callback; buffer the byte for later
|
2011-05-30 01:00:31 +02:00
|
|
|
end = i + 1
|
|
|
|
} else {
|
2011-06-01 00:28:07 +02:00
|
|
|
// skip past whatever the callback used
|
|
|
|
i += consumed
|
2011-05-30 01:00:31 +02:00
|
|
|
end = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-29 02:58:53 +02:00
|
|
|
parser.nesting--
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// single and double emphasis parsing
|
2011-06-29 19:21:46 +02:00
|
|
|
func inlineEmphasis(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
2011-05-30 01:00:31 +02:00
|
|
|
data = data[offset:]
|
|
|
|
c := data[0]
|
|
|
|
ret := 0
|
|
|
|
|
|
|
|
if len(data) > 2 && data[1] != c {
|
|
|
|
// whitespace cannot follow an opening emphasis;
|
|
|
|
// strikethrough only takes two characters '~~'
|
|
|
|
if c == '~' || isspace(data[1]) {
|
|
|
|
return 0
|
|
|
|
}
|
2011-06-29 19:21:46 +02:00
|
|
|
if ret = inlineHelperEmph1(parser, out, data[1:], c); ret == 0 {
|
2011-05-30 01:00:31 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data) > 3 && data[1] == c && data[2] != c {
|
|
|
|
if isspace(data[2]) {
|
|
|
|
return 0
|
|
|
|
}
|
2011-06-29 19:21:46 +02:00
|
|
|
if ret = inlineHelperEmph2(parser, out, data[2:], c); ret == 0 {
|
2011-05-30 01:00:31 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret + 2
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data) > 4 && data[1] == c && data[2] == c && data[3] != c {
|
|
|
|
if c == '~' || isspace(data[3]) {
|
|
|
|
return 0
|
|
|
|
}
|
2011-06-29 19:21:46 +02:00
|
|
|
if ret = inlineHelperEmph3(parser, out, data, 3, c); ret == 0 {
|
2011-05-30 01:00:31 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret + 3
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2011-06-29 19:21:46 +02:00
|
|
|
func inlineCodeSpan(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
2011-05-30 01:00:31 +02:00
|
|
|
data = data[offset:]
|
|
|
|
|
|
|
|
nb := 0
|
|
|
|
|
|
|
|
// count the number of backticks in the delimiter
|
|
|
|
for nb < len(data) && data[nb] == '`' {
|
|
|
|
nb++
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the next delimiter
|
|
|
|
i, end := 0, 0
|
|
|
|
for end = nb; end < len(data) && i < nb; end++ {
|
|
|
|
if data[end] == '`' {
|
|
|
|
i++
|
|
|
|
} else {
|
|
|
|
i = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-27 22:35:11 +02:00
|
|
|
// no matching delimiter?
|
2011-05-30 01:00:31 +02:00
|
|
|
if i < nb && end >= len(data) {
|
2011-06-27 22:35:11 +02:00
|
|
|
return 0
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// trim outside whitespace
|
2011-06-29 00:02:12 +02:00
|
|
|
fBegin := nb
|
|
|
|
for fBegin < end && (data[fBegin] == ' ' || data[fBegin] == '\t') {
|
|
|
|
fBegin++
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 00:02:12 +02:00
|
|
|
fEnd := end - nb
|
|
|
|
for fEnd > fBegin && (data[fEnd-1] == ' ' || data[fEnd-1] == '\t') {
|
|
|
|
fEnd--
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
|
2011-06-27 22:35:11 +02:00
|
|
|
// render the code span
|
2011-06-29 19:13:17 +02:00
|
|
|
if !parser.r.CodeSpan(out, data[fBegin:fEnd]) {
|
2011-06-27 22:35:11 +02:00
|
|
|
end = 0
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return end
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-06-25 23:45:51 +02:00
|
|
|
// newline preceded by two spaces becomes <br>
|
|
|
|
// newline without two spaces works when EXTENSION_HARD_LINE_BREAK is enabled
|
2011-06-29 19:21:46 +02:00
|
|
|
func inlineLineBreak(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
2011-06-28 00:06:16 +02:00
|
|
|
// remove trailing spaces from out
|
2011-05-30 01:00:31 +02:00
|
|
|
outBytes := out.Bytes()
|
|
|
|
end := len(outBytes)
|
2011-06-28 00:06:16 +02:00
|
|
|
eol := end
|
|
|
|
for eol > 0 && (outBytes[eol-1] == ' ' || outBytes[eol-1] == '\t') {
|
|
|
|
eol--
|
|
|
|
}
|
|
|
|
out.Truncate(eol)
|
|
|
|
|
|
|
|
// should there be a hard line break here?
|
2011-06-29 02:58:53 +02:00
|
|
|
if parser.flags&EXTENSION_HARD_LINE_BREAK == 0 && end-eol < 2 {
|
2011-06-28 00:06:16 +02:00
|
|
|
return 0
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
if parser.r.LineBreak(out) {
|
2011-05-30 01:00:31 +02:00
|
|
|
return 1
|
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// '[': parse a link or an image
|
2011-06-29 19:21:46 +02:00
|
|
|
func inlineLink(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
2011-06-24 19:50:03 +02:00
|
|
|
// no links allowed inside other links
|
2011-06-29 02:58:53 +02:00
|
|
|
if parser.insideLink {
|
2011-06-24 19:50:03 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2011-05-30 01:00:31 +02:00
|
|
|
isImg := offset > 0 && data[offset-1] == '!'
|
|
|
|
|
|
|
|
data = data[offset:]
|
|
|
|
|
|
|
|
i := 1
|
|
|
|
var title, link []byte
|
2011-06-29 00:02:12 +02:00
|
|
|
textHasNl := false
|
2011-05-30 01:00:31 +02:00
|
|
|
|
|
|
|
// look for the matching closing bracket
|
|
|
|
for level := 1; level > 0 && i < len(data); i++ {
|
|
|
|
switch {
|
|
|
|
case data[i] == '\n':
|
2011-06-29 00:02:12 +02:00
|
|
|
textHasNl = true
|
2011-05-30 01:00:31 +02:00
|
|
|
|
|
|
|
case data[i-1] == '\\':
|
|
|
|
continue
|
|
|
|
|
|
|
|
case data[i] == '[':
|
|
|
|
level++
|
|
|
|
|
|
|
|
case data[i] == ']':
|
|
|
|
level--
|
|
|
|
if level <= 0 {
|
|
|
|
i-- // compensate for extra i++ in for loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i >= len(data) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2011-06-29 00:02:12 +02:00
|
|
|
txtE := i
|
2011-05-30 01:00:31 +02:00
|
|
|
i++
|
|
|
|
|
|
|
|
// skip any amount of whitespace or newline
|
|
|
|
// (this is much more lax than original markdown syntax)
|
|
|
|
for i < len(data) && isspace(data[i]) {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
// inline style link
|
|
|
|
switch {
|
|
|
|
case i < len(data) && data[i] == '(':
|
|
|
|
// skip initial whitespace
|
|
|
|
i++
|
|
|
|
|
|
|
|
for i < len(data) && isspace(data[i]) {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
2011-06-29 00:02:12 +02:00
|
|
|
linkB := i
|
2011-05-30 01:00:31 +02:00
|
|
|
|
|
|
|
// look for link end: ' " )
|
|
|
|
for i < len(data) {
|
|
|
|
if data[i] == '\\' {
|
|
|
|
i += 2
|
|
|
|
} else {
|
|
|
|
if data[i] == ')' || data[i] == '\'' || data[i] == '"' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i >= len(data) {
|
|
|
|
return 0
|
|
|
|
}
|
2011-06-29 00:02:12 +02:00
|
|
|
linkE := i
|
2011-05-30 01:00:31 +02:00
|
|
|
|
|
|
|
// look for title end if present
|
2011-06-29 00:02:12 +02:00
|
|
|
titleB, titleE := 0, 0
|
2011-05-30 01:00:31 +02:00
|
|
|
if data[i] == '\'' || data[i] == '"' {
|
|
|
|
i++
|
2011-06-29 00:02:12 +02:00
|
|
|
titleB = i
|
2011-05-30 01:00:31 +02:00
|
|
|
|
|
|
|
for i < len(data) {
|
|
|
|
if data[i] == '\\' {
|
|
|
|
i += 2
|
|
|
|
} else {
|
|
|
|
if data[i] == ')' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i >= len(data) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip whitespace after title
|
2011-06-29 00:02:12 +02:00
|
|
|
titleE = i - 1
|
|
|
|
for titleE > titleB && isspace(data[titleE]) {
|
|
|
|
titleE--
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// check for closing quote presence
|
2011-06-29 00:02:12 +02:00
|
|
|
if data[titleE] != '\'' && data[titleE] != '"' {
|
|
|
|
titleB, titleE = 0, 0
|
|
|
|
linkE = i
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove whitespace at the end of the link
|
2011-06-29 00:02:12 +02:00
|
|
|
for linkE > linkB && isspace(data[linkE-1]) {
|
|
|
|
linkE--
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove optional angle brackets around the link
|
2011-06-29 00:02:12 +02:00
|
|
|
if data[linkB] == '<' {
|
|
|
|
linkB++
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
2011-06-29 00:02:12 +02:00
|
|
|
if data[linkE-1] == '>' {
|
|
|
|
linkE--
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// build escaped link and title
|
2011-06-29 00:02:12 +02:00
|
|
|
if linkE > linkB {
|
|
|
|
link = data[linkB:linkE]
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 00:02:12 +02:00
|
|
|
if titleE > titleB {
|
|
|
|
title = data[titleB:titleE]
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
i++
|
|
|
|
|
|
|
|
// reference style link
|
|
|
|
case i < len(data) && data[i] == '[':
|
|
|
|
var id []byte
|
|
|
|
|
|
|
|
// look for the id
|
|
|
|
i++
|
2011-06-29 00:02:12 +02:00
|
|
|
linkB := i
|
2011-05-30 01:00:31 +02:00
|
|
|
for i < len(data) && data[i] != ']' {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
if i >= len(data) {
|
|
|
|
return 0
|
|
|
|
}
|
2011-06-29 00:02:12 +02:00
|
|
|
linkE := i
|
2011-05-30 01:00:31 +02:00
|
|
|
|
|
|
|
// find the reference
|
2011-06-29 00:02:12 +02:00
|
|
|
if linkB == linkE {
|
|
|
|
if textHasNl {
|
2011-05-31 19:11:04 +02:00
|
|
|
var b bytes.Buffer
|
2011-05-30 01:00:31 +02:00
|
|
|
|
2011-06-29 00:02:12 +02:00
|
|
|
for j := 1; j < txtE; j++ {
|
2011-05-30 01:00:31 +02:00
|
|
|
switch {
|
|
|
|
case data[j] != '\n':
|
|
|
|
b.WriteByte(data[j])
|
|
|
|
case data[j-1] != ' ':
|
|
|
|
b.WriteByte(' ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
id = b.Bytes()
|
|
|
|
} else {
|
2011-06-29 00:02:12 +02:00
|
|
|
id = data[1:txtE]
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
} else {
|
2011-06-29 00:02:12 +02:00
|
|
|
id = data[linkB:linkE]
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// find the reference with matching id (ids are case-insensitive)
|
|
|
|
key := string(bytes.ToLower(id))
|
2011-06-29 02:58:53 +02:00
|
|
|
lr, ok := parser.refs[key]
|
2011-05-30 01:00:31 +02:00
|
|
|
if !ok {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// keep link and title from reference
|
|
|
|
link = lr.link
|
|
|
|
title = lr.title
|
|
|
|
i++
|
|
|
|
|
|
|
|
// shortcut reference style link
|
|
|
|
default:
|
|
|
|
var id []byte
|
|
|
|
|
|
|
|
// craft the id
|
2011-06-29 00:02:12 +02:00
|
|
|
if textHasNl {
|
2011-05-31 19:11:04 +02:00
|
|
|
var b bytes.Buffer
|
2011-05-30 01:00:31 +02:00
|
|
|
|
2011-06-29 00:02:12 +02:00
|
|
|
for j := 1; j < txtE; j++ {
|
2011-05-30 01:00:31 +02:00
|
|
|
switch {
|
|
|
|
case data[j] != '\n':
|
|
|
|
b.WriteByte(data[j])
|
|
|
|
case data[j-1] != ' ':
|
|
|
|
b.WriteByte(' ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
id = b.Bytes()
|
|
|
|
} else {
|
2011-06-29 00:02:12 +02:00
|
|
|
id = data[1:txtE]
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// find the reference with matching id
|
|
|
|
key := string(bytes.ToLower(id))
|
2011-06-29 02:58:53 +02:00
|
|
|
lr, ok := parser.refs[key]
|
2011-05-30 01:00:31 +02:00
|
|
|
if !ok {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// keep link and title from reference
|
|
|
|
link = lr.link
|
|
|
|
title = lr.title
|
|
|
|
|
|
|
|
// rewind the whitespace
|
2011-06-29 00:02:12 +02:00
|
|
|
i = txtE + 1
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// build content: img alt is escaped, link content is parsed
|
2011-05-31 19:11:04 +02:00
|
|
|
var content bytes.Buffer
|
2011-06-29 00:02:12 +02:00
|
|
|
if txtE > 1 {
|
2011-05-30 01:00:31 +02:00
|
|
|
if isImg {
|
2011-06-29 00:02:12 +02:00
|
|
|
content.Write(data[1:txtE])
|
2011-05-30 01:00:31 +02:00
|
|
|
} else {
|
2011-06-24 19:50:03 +02:00
|
|
|
// links cannot contain other links, so turn off link parsing temporarily
|
2011-06-29 02:58:53 +02:00
|
|
|
insideLink := parser.insideLink
|
|
|
|
parser.insideLink = true
|
|
|
|
parser.parseInline(&content, data[1:txtE])
|
|
|
|
parser.insideLink = insideLink
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-29 00:02:12 +02:00
|
|
|
var uLink []byte
|
2011-05-30 01:00:31 +02:00
|
|
|
if len(link) > 0 {
|
2011-06-29 00:02:12 +02:00
|
|
|
var uLinkBuf bytes.Buffer
|
|
|
|
unescapeText(&uLinkBuf, link)
|
|
|
|
uLink = uLinkBuf.Bytes()
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// call the relevant rendering function
|
2011-06-29 03:46:35 +02:00
|
|
|
ret := false
|
2011-05-30 01:00:31 +02:00
|
|
|
if isImg {
|
|
|
|
outSize := out.Len()
|
|
|
|
outBytes := out.Bytes()
|
|
|
|
if outSize > 0 && outBytes[outSize-1] == '!' {
|
|
|
|
out.Truncate(outSize - 1)
|
|
|
|
}
|
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
ret = parser.r.Image(out, uLink, title, content.Bytes())
|
2011-05-30 01:00:31 +02:00
|
|
|
} else {
|
2011-06-29 19:13:17 +02:00
|
|
|
ret = parser.r.Link(out, uLink, title, content.Bytes())
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 03:46:35 +02:00
|
|
|
if ret {
|
2011-05-30 01:00:31 +02:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// '<' when tags or autolinks are allowed
|
2011-06-29 19:21:46 +02:00
|
|
|
func inlineLAngle(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
2011-05-30 01:00:31 +02:00
|
|
|
data = data[offset:]
|
|
|
|
altype := LINK_TYPE_NOT_AUTOLINK
|
|
|
|
end := tagLength(data, &altype)
|
2011-06-29 03:46:35 +02:00
|
|
|
ret := false
|
2011-05-30 01:00:31 +02:00
|
|
|
|
|
|
|
if end > 2 {
|
2011-06-29 19:13:17 +02:00
|
|
|
if altype != LINK_TYPE_NOT_AUTOLINK {
|
2011-06-29 00:02:12 +02:00
|
|
|
var uLink bytes.Buffer
|
|
|
|
unescapeText(&uLink, data[1:end+1-2])
|
2011-06-29 19:13:17 +02:00
|
|
|
ret = parser.r.AutoLink(out, uLink.Bytes(), altype)
|
|
|
|
} else {
|
|
|
|
ret = parser.r.RawHtmlTag(out, data[:end])
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-29 03:46:35 +02:00
|
|
|
if !ret {
|
2011-05-30 01:00:31 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return end
|
|
|
|
}
|
|
|
|
|
|
|
|
// '\\' backslash escape
|
|
|
|
var escapeChars = []byte("\\`*_{}[]()#+-.!:|&<>")
|
|
|
|
|
2011-06-29 19:21:46 +02:00
|
|
|
func inlineEscape(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
2011-05-30 01:00:31 +02:00
|
|
|
data = data[offset:]
|
|
|
|
|
|
|
|
if len(data) > 1 {
|
|
|
|
if bytes.IndexByte(escapeChars, data[1]) < 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
parser.r.NormalText(out, data[1:2])
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
|
|
|
func unescapeText(ob *bytes.Buffer, src []byte) {
|
|
|
|
i := 0
|
|
|
|
for i < len(src) {
|
|
|
|
org := i
|
|
|
|
for i < len(src) && src[i] != '\\' {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
if i > org {
|
|
|
|
ob.Write(src[org:i])
|
|
|
|
}
|
|
|
|
|
|
|
|
if i+1 >= len(src) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
ob.WriteByte(src[i+1])
|
|
|
|
i += 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// '&' escaped when it doesn't belong to an entity
|
|
|
|
// valid entities are assumed to be anything matching &#?[A-Za-z0-9]+;
|
2011-06-29 19:21:46 +02:00
|
|
|
func inlineEntity(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
2011-05-30 01:00:31 +02:00
|
|
|
data = data[offset:]
|
|
|
|
|
|
|
|
end := 1
|
|
|
|
|
|
|
|
if end < len(data) && data[end] == '#' {
|
|
|
|
end++
|
|
|
|
}
|
|
|
|
|
|
|
|
for end < len(data) && isalnum(data[end]) {
|
|
|
|
end++
|
|
|
|
}
|
|
|
|
|
|
|
|
if end < len(data) && data[end] == ';' {
|
|
|
|
end++ // real entity
|
|
|
|
} else {
|
|
|
|
return 0 // lone '&'
|
|
|
|
}
|
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
parser.r.Entity(out, data[:end])
|
2011-05-30 01:00:31 +02:00
|
|
|
|
|
|
|
return end
|
|
|
|
}
|
|
|
|
|
2011-06-29 19:21:46 +02:00
|
|
|
func inlineAutoLink(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {
|
2011-05-31 19:49:49 +02:00
|
|
|
// quick check to rule out most false hits on ':'
|
2011-06-29 02:58:53 +02:00
|
|
|
if parser.insideLink || len(data) < offset+3 || data[offset+1] != '/' || data[offset+2] != '/' {
|
2011-05-31 19:49:49 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// scan backward for a word boundary
|
|
|
|
rewind := 0
|
|
|
|
for offset-rewind > 0 && rewind <= 7 && !isspace(data[offset-rewind-1]) && !isspace(data[offset-rewind-1]) {
|
|
|
|
rewind++
|
|
|
|
}
|
|
|
|
if rewind > 6 { // longest supported protocol is "mailto" which has 6 letters
|
|
|
|
return 0
|
|
|
|
}
|
2011-05-30 01:00:31 +02:00
|
|
|
|
2011-06-29 00:02:12 +02:00
|
|
|
origData := data
|
2011-05-30 23:36:31 +02:00
|
|
|
data = data[offset-rewind:]
|
2011-05-30 01:00:31 +02:00
|
|
|
|
|
|
|
if !isSafeLink(data) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2011-06-29 00:02:12 +02:00
|
|
|
linkEnd := 0
|
|
|
|
for linkEnd < len(data) && !isspace(data[linkEnd]) {
|
|
|
|
linkEnd++
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Skip punctuation at the end of the link
|
2011-06-29 00:02:12 +02:00
|
|
|
if (data[linkEnd-1] == '.' || data[linkEnd-1] == ',' || data[linkEnd-1] == ';') && data[linkEnd-2] != '\\' {
|
|
|
|
linkEnd--
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// See if the link finishes with a punctuation sign that can be closed.
|
|
|
|
var copen byte
|
2011-06-29 00:02:12 +02:00
|
|
|
switch data[linkEnd-1] {
|
2011-05-30 01:00:31 +02:00
|
|
|
case '"':
|
|
|
|
copen = '"'
|
|
|
|
case '\'':
|
|
|
|
copen = '\''
|
|
|
|
case ')':
|
|
|
|
copen = '('
|
|
|
|
case ']':
|
|
|
|
copen = '['
|
|
|
|
case '}':
|
|
|
|
copen = '{'
|
|
|
|
default:
|
|
|
|
copen = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if copen != 0 {
|
2011-06-29 00:02:12 +02:00
|
|
|
bufEnd := offset - rewind + linkEnd - 2
|
2011-05-30 01:00:31 +02:00
|
|
|
|
2011-06-29 00:02:12 +02:00
|
|
|
openDelim := 1
|
2011-05-30 01:00:31 +02:00
|
|
|
|
|
|
|
/* Try to close the final punctuation sign in this same line;
|
|
|
|
* if we managed to close it outside of the URL, that means that it's
|
|
|
|
* not part of the URL. If it closes inside the URL, that means it
|
|
|
|
* is part of the URL.
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
*
|
|
|
|
* foo http://www.pokemon.com/Pikachu_(Electric) bar
|
|
|
|
* => http://www.pokemon.com/Pikachu_(Electric)
|
|
|
|
*
|
|
|
|
* foo (http://www.pokemon.com/Pikachu_(Electric)) bar
|
|
|
|
* => http://www.pokemon.com/Pikachu_(Electric)
|
|
|
|
*
|
|
|
|
* foo http://www.pokemon.com/Pikachu_(Electric)) bar
|
|
|
|
* => http://www.pokemon.com/Pikachu_(Electric))
|
|
|
|
*
|
|
|
|
* (foo http://www.pokemon.com/Pikachu_(Electric)) bar
|
|
|
|
* => foo http://www.pokemon.com/Pikachu_(Electric)
|
|
|
|
*/
|
|
|
|
|
2011-06-29 00:02:12 +02:00
|
|
|
for bufEnd >= 0 && origData[bufEnd] != '\n' && openDelim != 0 {
|
|
|
|
if origData[bufEnd] == data[linkEnd-1] {
|
|
|
|
openDelim++
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 00:02:12 +02:00
|
|
|
if origData[bufEnd] == copen {
|
|
|
|
openDelim--
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 00:02:12 +02:00
|
|
|
bufEnd--
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 00:02:12 +02:00
|
|
|
if openDelim == 0 {
|
|
|
|
linkEnd--
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-31 19:49:49 +02:00
|
|
|
// we were triggered on the ':', so we need to rewind the output a bit
|
|
|
|
if out.Len() >= rewind {
|
|
|
|
out.Truncate(len(out.Bytes()) - rewind)
|
|
|
|
}
|
2011-05-30 23:36:31 +02:00
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
var uLink bytes.Buffer
|
|
|
|
unescapeText(&uLink, data[:linkEnd])
|
2011-05-30 01:00:31 +02:00
|
|
|
|
2011-06-29 19:13:17 +02:00
|
|
|
parser.r.AutoLink(out, uLink.Bytes(), LINK_TYPE_NORMAL)
|
2011-05-30 01:00:31 +02:00
|
|
|
|
2011-06-29 00:02:12 +02:00
|
|
|
return linkEnd - rewind
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var validUris = [][]byte{[]byte("http://"), []byte("https://"), []byte("ftp://"), []byte("mailto://")}
|
|
|
|
|
|
|
|
func isSafeLink(link []byte) bool {
|
|
|
|
for _, prefix := range validUris {
|
|
|
|
// TODO: handle unicode here
|
|
|
|
// case-insensitive prefix test
|
2011-05-30 22:42:38 +02:00
|
|
|
if len(link) > len(prefix) && bytes.Equal(bytes.ToLower(link[:len(prefix)]), prefix) && isalnum(link[len(prefix)]) {
|
2011-05-30 01:00:31 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the length of the given tag, or 0 is it's not valid
|
|
|
|
func tagLength(data []byte, autolink *int) int {
|
|
|
|
var i, j int
|
|
|
|
|
|
|
|
// a valid tag can't be shorter than 3 chars
|
|
|
|
if len(data) < 3 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// begins with a '<' optionally followed by '/', followed by letter or number
|
|
|
|
if data[0] != '<' {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if data[1] == '/' {
|
|
|
|
i = 2
|
|
|
|
} else {
|
|
|
|
i = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isalnum(data[i]) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// scheme test
|
|
|
|
*autolink = LINK_TYPE_NOT_AUTOLINK
|
|
|
|
|
2011-06-25 00:39:50 +02:00
|
|
|
// try to find the beginning of an URI
|
2011-05-30 01:00:31 +02:00
|
|
|
for i < len(data) && (isalnum(data[i]) || data[i] == '.' || data[i] == '+' || data[i] == '-') {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
if i > 1 && data[i] == '@' {
|
2011-05-31 05:44:52 +02:00
|
|
|
if j = isMailtoAutoLink(data[i:]); j != 0 {
|
2011-05-30 01:00:31 +02:00
|
|
|
*autolink = LINK_TYPE_EMAIL
|
|
|
|
return i + j
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i > 2 && data[i] == ':' {
|
|
|
|
*autolink = LINK_TYPE_NORMAL
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
// complete autolink test: no whitespace or ' or "
|
|
|
|
switch {
|
|
|
|
case i >= len(data):
|
|
|
|
*autolink = LINK_TYPE_NOT_AUTOLINK
|
|
|
|
case *autolink != 0:
|
|
|
|
j = i
|
|
|
|
|
|
|
|
for i < len(data) {
|
|
|
|
if data[i] == '\\' {
|
|
|
|
i += 2
|
|
|
|
} else {
|
|
|
|
if data[i] == '>' || data[i] == '\'' || data[i] == '"' || isspace(data[i]) {
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if i >= len(data) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if i > j && data[i] == '>' {
|
|
|
|
return i + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// one of the forbidden chars has been found
|
|
|
|
*autolink = LINK_TYPE_NOT_AUTOLINK
|
|
|
|
}
|
|
|
|
|
|
|
|
// look for something looking like a tag end
|
|
|
|
for i < len(data) && data[i] != '>' {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
if i >= len(data) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return i + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// look for the address part of a mail autolink and '>'
|
|
|
|
// this is less strict than the original markdown e-mail address matching
|
2011-05-31 05:44:52 +02:00
|
|
|
func isMailtoAutoLink(data []byte) int {
|
2011-05-30 01:00:31 +02:00
|
|
|
nb := 0
|
|
|
|
|
|
|
|
// address is assumed to be: [-@._a-zA-Z0-9]+ with exactly one '@'
|
|
|
|
for i := 0; i < len(data); i++ {
|
|
|
|
if isalnum(data[i]) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch data[i] {
|
|
|
|
case '@':
|
|
|
|
nb++
|
|
|
|
|
|
|
|
case '-', '.', '_':
|
|
|
|
break
|
|
|
|
|
|
|
|
case '>':
|
|
|
|
if nb == 1 {
|
|
|
|
return i + 1
|
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// look for the next emph char, skipping other constructs
|
|
|
|
func inlineHelperFindEmphChar(data []byte, c byte) int {
|
|
|
|
i := 1
|
|
|
|
|
|
|
|
for i < len(data) {
|
|
|
|
for i < len(data) && data[i] != c && data[i] != '`' && data[i] != '[' {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
if i >= len(data) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if data[i] == c {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
// do not count escaped chars
|
|
|
|
if i != 0 && data[i-1] == '\\' {
|
|
|
|
i++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if data[i] == '`' {
|
|
|
|
// skip a code span
|
2011-06-29 00:02:12 +02:00
|
|
|
tmpI := 0
|
2011-05-30 01:00:31 +02:00
|
|
|
i++
|
|
|
|
for i < len(data) && data[i] != '`' {
|
2011-06-29 00:02:12 +02:00
|
|
|
if tmpI == 0 && data[i] == c {
|
|
|
|
tmpI = i
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
if i >= len(data) {
|
2011-06-29 00:02:12 +02:00
|
|
|
return tmpI
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
i++
|
|
|
|
} else {
|
|
|
|
if data[i] == '[' {
|
|
|
|
// skip a link
|
2011-06-29 00:02:12 +02:00
|
|
|
tmpI := 0
|
2011-05-30 01:00:31 +02:00
|
|
|
i++
|
|
|
|
for i < len(data) && data[i] != ']' {
|
2011-06-29 00:02:12 +02:00
|
|
|
if tmpI == 0 && data[i] == c {
|
|
|
|
tmpI = i
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
for i < len(data) && (data[i] == ' ' || data[i] == '\t' || data[i] == '\n') {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
if i >= len(data) {
|
2011-06-29 00:02:12 +02:00
|
|
|
return tmpI
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
if data[i] != '[' && data[i] != '(' { // not a link
|
2011-06-29 00:02:12 +02:00
|
|
|
if tmpI > 0 {
|
|
|
|
return tmpI
|
2011-05-30 01:00:31 +02:00
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cc := data[i]
|
|
|
|
i++
|
|
|
|
for i < len(data) && data[i] != cc {
|
2011-06-29 00:02:12 +02:00
|
|
|
if tmpI == 0 && data[i] == c {
|
|
|
|
tmpI = i
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
if i >= len(data) {
|
2011-06-29 00:02:12 +02:00
|
|
|
return tmpI
|
2011-05-30 01:00:31 +02:00
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2011-06-29 19:21:46 +02:00
|
|
|
func inlineHelperEmph1(parser *Parser, out *bytes.Buffer, data []byte, c byte) int {
|
2011-05-30 01:00:31 +02:00
|
|
|
i := 0
|
|
|
|
|
|
|
|
// skip one symbol if coming from emph3
|
|
|
|
if len(data) > 1 && data[0] == c && data[1] == c {
|
|
|
|
i = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
for i < len(data) {
|
|
|
|
length := inlineHelperFindEmphChar(data[i:], c)
|
|
|
|
if length == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
i += length
|
|
|
|
if i >= len(data) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if i+1 < len(data) && data[i+1] == c {
|
|
|
|
i++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if data[i] == c && !isspace(data[i-1]) {
|
|
|
|
|
2011-06-29 02:58:53 +02:00
|
|
|
if parser.flags&EXTENSION_NO_INTRA_EMPHASIS != 0 {
|
2011-05-30 01:00:31 +02:00
|
|
|
if !(i+1 == len(data) || isspace(data[i+1]) || ispunct(data[i+1])) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-31 19:11:04 +02:00
|
|
|
var work bytes.Buffer
|
2011-06-29 02:58:53 +02:00
|
|
|
parser.parseInline(&work, data[:i])
|
2011-06-29 19:13:17 +02:00
|
|
|
if parser.r.Emphasis(out, work.Bytes()) {
|
2011-05-30 01:00:31 +02:00
|
|
|
return i + 1
|
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2011-06-29 19:21:46 +02:00
|
|
|
func inlineHelperEmph2(parser *Parser, out *bytes.Buffer, data []byte, c byte) int {
|
2011-05-30 01:00:31 +02:00
|
|
|
i := 0
|
|
|
|
|
|
|
|
for i < len(data) {
|
|
|
|
length := inlineHelperFindEmphChar(data[i:], c)
|
|
|
|
if length == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
i += length
|
|
|
|
|
|
|
|
if i+1 < len(data) && data[i] == c && data[i+1] == c && i > 0 && !isspace(data[i-1]) {
|
2011-05-31 19:11:04 +02:00
|
|
|
var work bytes.Buffer
|
2011-06-29 02:58:53 +02:00
|
|
|
parser.parseInline(&work, data[:i])
|
2011-06-29 19:13:17 +02:00
|
|
|
success := false
|
|
|
|
|
|
|
|
// pick the right renderer
|
|
|
|
if c == '~' {
|
|
|
|
success = parser.r.StrikeThrough(out, work.Bytes())
|
|
|
|
} else {
|
|
|
|
success = parser.r.DoubleEmphasis(out, work.Bytes())
|
|
|
|
}
|
|
|
|
if success {
|
2011-05-30 01:00:31 +02:00
|
|
|
return i + 2
|
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2011-06-29 19:21:46 +02:00
|
|
|
func inlineHelperEmph3(parser *Parser, out *bytes.Buffer, data []byte, offset int, c byte) int {
|
2011-05-30 01:00:31 +02:00
|
|
|
i := 0
|
2011-06-29 00:02:12 +02:00
|
|
|
origData := data
|
2011-05-30 01:00:31 +02:00
|
|
|
data = data[offset:]
|
|
|
|
|
|
|
|
for i < len(data) {
|
|
|
|
length := inlineHelperFindEmphChar(data[i:], c)
|
|
|
|
if length == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
i += length
|
|
|
|
|
|
|
|
// skip whitespace preceded symbols
|
|
|
|
if data[i] != c || isspace(data[i-1]) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
2011-06-29 19:13:17 +02:00
|
|
|
case i+2 < len(data) && data[i+1] == c && data[i+2] == c:
|
2011-05-30 01:00:31 +02:00
|
|
|
// triple symbol found
|
2011-05-31 19:11:04 +02:00
|
|
|
var work bytes.Buffer
|
2011-05-30 01:00:31 +02:00
|
|
|
|
2011-06-29 02:58:53 +02:00
|
|
|
parser.parseInline(&work, data[:i])
|
2011-06-29 19:13:17 +02:00
|
|
|
if parser.r.TripleEmphasis(out, work.Bytes()) {
|
2011-05-30 01:00:31 +02:00
|
|
|
return i + 3
|
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
case (i+1 < len(data) && data[i+1] == c):
|
|
|
|
// double symbol found, hand over to emph1
|
2011-06-29 19:21:46 +02:00
|
|
|
length = inlineHelperEmph1(parser, out, origData[offset-2:], c)
|
2011-05-30 01:00:31 +02:00
|
|
|
if length == 0 {
|
|
|
|
return 0
|
|
|
|
} else {
|
|
|
|
return length - 2
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// single symbol found, hand over to emph2
|
2011-06-29 19:21:46 +02:00
|
|
|
length = inlineHelperEmph2(parser, out, origData[offset-1:], c)
|
2011-05-30 01:00:31 +02:00
|
|
|
if length == 0 {
|
|
|
|
return 0
|
|
|
|
} else {
|
|
|
|
return length - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|