summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/russross/blackfriday/inline.go
diff options
context:
space:
mode:
authorDuco van Amstel <duco.vanamstel@gmail.com>2018-10-22 10:48:29 -0700
committerWim <wim@42.be>2018-10-22 19:48:29 +0200
commitf2cdda7278e01b548f8aff6567376ae2f15c1803 (patch)
treece6edbde50dc721b366e97e43c2726df808b59f8 /vendor/github.com/russross/blackfriday/inline.go
parent6911458d15f548070da870cc7f68c2a882f1c674 (diff)
downloadmatterbridge-msglm-f2cdda7278e01b548f8aff6567376ae2f15c1803.tar.gz
matterbridge-msglm-f2cdda7278e01b548f8aff6567376ae2f15c1803.tar.bz2
matterbridge-msglm-f2cdda7278e01b548f8aff6567376ae2f15c1803.zip
Update Blackfriday dependency (closes #522) (#532)
- Fixup Telegram bridge implementation to support updated dependency.
Diffstat (limited to 'vendor/github.com/russross/blackfriday/inline.go')
-rw-r--r--vendor/github.com/russross/blackfriday/inline.go552
1 files changed, 306 insertions, 246 deletions
diff --git a/vendor/github.com/russross/blackfriday/inline.go b/vendor/github.com/russross/blackfriday/inline.go
index 4483b8f1..3d633106 100644
--- a/vendor/github.com/russross/blackfriday/inline.go
+++ b/vendor/github.com/russross/blackfriday/inline.go
@@ -22,6 +22,9 @@ import (
var (
urlRe = `((https?|ftp):\/\/|\/)[-A-Za-z0-9+&@#\/%?=~_|!:,.;\(\)]+`
anchorRe = regexp.MustCompile(`^(<a\shref="` + urlRe + `"(\stitle="[^"<>]+")?\s?>` + urlRe + `<\/a>)`)
+
+ // TODO: improve this regexp to catch all possible entities:
+ htmlEntityRe = regexp.MustCompile(`&[a-z]{2,5};`)
)
// Functions to parse text within a block
@@ -29,87 +32,89 @@ var (
// data is the complete block being rendered
// offset is the number of valid chars before the current cursor
-func (p *parser) inline(out *bytes.Buffer, data []byte) {
- // this is called recursively: enforce a maximum depth
- if p.nesting >= p.maxNesting {
+func (p *Markdown) inline(currBlock *Node, data []byte) {
+ // handlers might call us recursively: enforce a maximum depth
+ if p.nesting >= p.maxNesting || len(data) == 0 {
return
}
p.nesting++
-
- i, end := 0, 0
- for i < len(data) {
- // copy inactive chars into the output
- for end < len(data) && p.inlineCallback[data[end]] == nil {
- end++
- }
-
- p.r.NormalText(out, data[i:end])
-
- if end >= len(data) {
- break
- }
- i = end
-
- // call the trigger
+ beg, end := 0, 0
+ for end < len(data) {
handler := p.inlineCallback[data[end]]
- if consumed := handler(p, out, data, i); consumed == 0 {
- // no action from the callback; buffer the byte for later
- end = i + 1
+ if handler != nil {
+ if consumed, node := handler(p, data, end); consumed == 0 {
+ // No action from the callback.
+ end++
+ } else {
+ // Copy inactive chars into the output.
+ currBlock.AppendChild(text(data[beg:end]))
+ if node != nil {
+ currBlock.AppendChild(node)
+ }
+ // Skip past whatever the callback used.
+ beg = end + consumed
+ end = beg
+ }
} else {
- // skip past whatever the callback used
- i += consumed
- end = i
+ end++
}
}
-
+ if beg < len(data) {
+ if data[end-1] == '\n' {
+ end--
+ }
+ currBlock.AppendChild(text(data[beg:end]))
+ }
p.nesting--
}
// single and double emphasis parsing
-func emphasis(p *parser, out *bytes.Buffer, data []byte, offset int) int {
+func emphasis(p *Markdown, data []byte, offset int) (int, *Node) {
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
+ return 0, nil
}
- if ret = helperEmphasis(p, out, data[1:], c); ret == 0 {
- return 0
+ ret, node := helperEmphasis(p, data[1:], c)
+ if ret == 0 {
+ return 0, nil
}
- return ret + 1
+ return ret + 1, node
}
if len(data) > 3 && data[1] == c && data[2] != c {
if isspace(data[2]) {
- return 0
+ return 0, nil
}
- if ret = helperDoubleEmphasis(p, out, data[2:], c); ret == 0 {
- return 0
+ ret, node := helperDoubleEmphasis(p, data[2:], c)
+ if ret == 0 {
+ return 0, nil
}
- return ret + 2
+ return ret + 2, node
}
if len(data) > 4 && data[1] == c && data[2] == c && data[3] != c {
if c == '~' || isspace(data[3]) {
- return 0
+ return 0, nil
}
- if ret = helperTripleEmphasis(p, out, data, 3, c); ret == 0 {
- return 0
+ ret, node := helperTripleEmphasis(p, data, 3, c)
+ if ret == 0 {
+ return 0, nil
}
- return ret + 3
+ return ret + 3, node
}
- return 0
+ return 0, nil
}
-func codeSpan(p *parser, out *bytes.Buffer, data []byte, offset int) int {
+func codeSpan(p *Markdown, data []byte, offset int) (int, *Node) {
data = data[offset:]
nb := 0
@@ -131,7 +136,7 @@ func codeSpan(p *parser, out *bytes.Buffer, data []byte, offset int) int {
// no matching delimiter?
if i < nb && end >= len(data) {
- return 0
+ return 0, nil
}
// trim outside whitespace
@@ -147,43 +152,36 @@ func codeSpan(p *parser, out *bytes.Buffer, data []byte, offset int) int {
// render the code span
if fBegin != fEnd {
- p.r.CodeSpan(out, data[fBegin:fEnd])
+ code := NewNode(Code)
+ code.Literal = data[fBegin:fEnd]
+ return end, code
}
- return end
-
+ return end, nil
}
// newline preceded by two spaces becomes <br>
-// newline without two spaces works when EXTENSION_HARD_LINE_BREAK is enabled
-func lineBreak(p *parser, out *bytes.Buffer, data []byte, offset int) int {
- // remove trailing spaces from out
- outBytes := out.Bytes()
- end := len(outBytes)
- eol := end
- for eol > 0 && outBytes[eol-1] == ' ' {
- eol--
- }
- out.Truncate(eol)
-
- precededByTwoSpaces := offset >= 2 && data[offset-2] == ' ' && data[offset-1] == ' '
- precededByBackslash := offset >= 1 && data[offset-1] == '\\' // see http://spec.commonmark.org/0.18/#example-527
- precededByBackslash = precededByBackslash && p.flags&EXTENSION_BACKSLASH_LINE_BREAK != 0
-
- if p.flags&EXTENSION_JOIN_LINES != 0 {
- return 1
+func maybeLineBreak(p *Markdown, data []byte, offset int) (int, *Node) {
+ origOffset := offset
+ for offset < len(data) && data[offset] == ' ' {
+ offset++
}
- // should there be a hard line break here?
- if p.flags&EXTENSION_HARD_LINE_BREAK == 0 && !precededByTwoSpaces && !precededByBackslash {
- return 0
+ if offset < len(data) && data[offset] == '\n' {
+ if offset-origOffset >= 2 {
+ return offset - origOffset + 1, NewNode(Hardbreak)
+ }
+ return offset - origOffset, nil
}
+ return 0, nil
+}
- if precededByBackslash && eol > 0 {
- out.Truncate(eol - 1)
+// newline without two spaces works when HardLineBreak is enabled
+func lineBreak(p *Markdown, data []byte, offset int) (int, *Node) {
+ if p.extensions&HardLineBreak != 0 {
+ return 1, NewNode(Hardbreak)
}
- p.r.LineBreak(out)
- return 1
+ return 0, nil
}
type linkType int
@@ -202,27 +200,43 @@ func isReferenceStyleLink(data []byte, pos int, t linkType) bool {
return pos < len(data)-1 && data[pos] == '[' && data[pos+1] != '^'
}
+func maybeImage(p *Markdown, data []byte, offset int) (int, *Node) {
+ if offset < len(data)-1 && data[offset+1] == '[' {
+ return link(p, data, offset)
+ }
+ return 0, nil
+}
+
+func maybeInlineFootnote(p *Markdown, data []byte, offset int) (int, *Node) {
+ if offset < len(data)-1 && data[offset+1] == '[' {
+ return link(p, data, offset)
+ }
+ return 0, nil
+}
+
// '[': parse a link or an image or a footnote
-func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
+func link(p *Markdown, data []byte, offset int) (int, *Node) {
// no links allowed inside regular links, footnote, and deferred footnotes
if p.insideLink && (offset > 0 && data[offset-1] == '[' || len(data)-1 > offset && data[offset+1] == '^') {
- return 0
+ return 0, nil
}
var t linkType
switch {
// special case: ![^text] == deferred footnote (that follows something with
// an exclamation point)
- case p.flags&EXTENSION_FOOTNOTES != 0 && len(data)-1 > offset && data[offset+1] == '^':
+ case p.extensions&Footnotes != 0 && len(data)-1 > offset && data[offset+1] == '^':
t = linkDeferredFootnote
// ![alt] == image
- case offset > 0 && data[offset-1] == '!':
+ case offset >= 0 && data[offset] == '!':
t = linkImg
+ offset++
// ^[text] == inline footnote
// [^refId] == deferred footnote
- case p.flags&EXTENSION_FOOTNOTES != 0:
- if offset > 0 && data[offset-1] == '^' {
+ case p.extensions&Footnotes != 0:
+ if offset >= 0 && data[offset] == '^' {
t = linkInlineFootnote
+ offset++
} else if len(data)-1 > offset && data[offset+1] == '^' {
t = linkDeferredFootnote
}
@@ -235,7 +249,7 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
var (
i = 1
- noteId int
+ noteID int
title, link, altContent []byte
textHasNl = false
)
@@ -244,8 +258,6 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
i++
}
- brace := 0
-
// look for the matching closing bracket
for level := 1; level > 0 && i < len(data); i++ {
switch {
@@ -267,11 +279,12 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
}
if i >= len(data) {
- return 0
+ return 0, nil
}
txtE := i
i++
+ var footnoteNode *Node
// skip any amount of whitespace or newline
// (this is much more lax than original markdown syntax)
@@ -279,8 +292,8 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
i++
}
- switch {
// inline style link
+ switch {
case i < len(data) && data[i] == '(':
// skip initial whitespace
i++
@@ -291,27 +304,14 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
linkB := i
- // look for link end: ' " ), check for new opening braces and take this
- // into account, this may lead for overshooting and probably will require
- // some fine-tuning.
+ // look for link end: ' " )
findlinkend:
for i < len(data) {
switch {
case data[i] == '\\':
i += 2
- case data[i] == '(':
- brace++
- i++
-
- case data[i] == ')':
- if brace <= 0 {
- break findlinkend
- }
- brace--
- i++
-
- case data[i] == '\'' || data[i] == '"':
+ case data[i] == ')' || data[i] == '\'' || data[i] == '"':
break findlinkend
default:
@@ -320,7 +320,7 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
}
if i >= len(data) {
- return 0
+ return 0, nil
}
linkE := i
@@ -345,7 +345,7 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
}
if i >= len(data) {
- return 0
+ return 0, nil
}
// skip whitespace after title
@@ -397,7 +397,7 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
i++
}
if i >= len(data) {
- return 0
+ return 0, nil
}
linkE := i
@@ -427,7 +427,7 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
// find the reference with matching id
lr, ok := p.getRef(string(id))
if !ok {
- return 0
+ return 0, nil
}
// keep link and title from reference
@@ -464,9 +464,10 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
}
}
+ footnoteNode = NewNode(Item)
if t == linkInlineFootnote {
// create a new reference
- noteId = len(p.notes) + 1
+ noteID = len(p.notes) + 1
var fragment []byte
if len(id) > 0 {
@@ -477,18 +478,18 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
}
copy(fragment, slugify(id))
} else {
- fragment = append([]byte("footnote-"), []byte(strconv.Itoa(noteId))...)
+ fragment = append([]byte("footnote-"), []byte(strconv.Itoa(noteID))...)
}
ref := &reference{
- noteId: noteId,
+ noteID: noteID,
hasBlock: false,
link: fragment,
title: id,
+ footnote: footnoteNode,
}
p.notes = append(p.notes, ref)
- p.notesRecord[string(ref.link)] = struct{}{}
link = ref.link
title = ref.title
@@ -496,40 +497,26 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
// find the reference with matching id
lr, ok := p.getRef(string(id))
if !ok {
- return 0
+ return 0, nil
}
- if t == linkDeferredFootnote && !p.isFootnote(lr) {
- lr.noteId = len(p.notes) + 1
+ if t == linkDeferredFootnote {
+ lr.noteID = len(p.notes) + 1
+ lr.footnote = footnoteNode
p.notes = append(p.notes, lr)
- p.notesRecord[string(lr.link)] = struct{}{}
}
// keep link and title from reference
link = lr.link
// if inline footnote, title == footnote contents
title = lr.title
- noteId = lr.noteId
+ noteID = lr.noteID
}
// rewind the whitespace
i = txtE + 1
}
- // build content: img alt is escaped, link content is parsed
- var content bytes.Buffer
- if txtE > 1 {
- if t == linkImg {
- content.Write(data[1:txtE])
- } else {
- // links cannot contain other links, so turn off link parsing temporarily
- insideLink := p.insideLink
- p.insideLink = true
- p.inline(&content, data[1:txtE])
- p.insideLink = insideLink
- }
- }
-
var uLink []byte
if t == linkNormal || t == linkImg {
if len(link) > 0 {
@@ -539,49 +526,54 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
}
// links need something to click on and somewhere to go
- if len(uLink) == 0 || (t == linkNormal && content.Len() == 0) {
- return 0
+ if len(uLink) == 0 || (t == linkNormal && txtE <= 1) {
+ return 0, nil
}
}
// call the relevant rendering function
+ var linkNode *Node
switch t {
case linkNormal:
+ linkNode = NewNode(Link)
+ linkNode.Destination = normalizeURI(uLink)
+ linkNode.Title = title
if len(altContent) > 0 {
- p.r.Link(out, uLink, title, altContent)
+ linkNode.AppendChild(text(altContent))
} else {
- p.r.Link(out, uLink, title, content.Bytes())
+ // links cannot contain other links, so turn off link parsing
+ // temporarily and recurse
+ insideLink := p.insideLink
+ p.insideLink = true
+ p.inline(linkNode, data[1:txtE])
+ p.insideLink = insideLink
}
case linkImg:
- outSize := out.Len()
- outBytes := out.Bytes()
- if outSize > 0 && outBytes[outSize-1] == '!' {
- out.Truncate(outSize - 1)
- }
-
- p.r.Image(out, uLink, title, content.Bytes())
+ linkNode = NewNode(Image)
+ linkNode.Destination = uLink
+ linkNode.Title = title
+ linkNode.AppendChild(text(data[1:txtE]))
+ i++
- case linkInlineFootnote:
- outSize := out.Len()
- outBytes := out.Bytes()
- if outSize > 0 && outBytes[outSize-1] == '^' {
- out.Truncate(outSize - 1)
+ case linkInlineFootnote, linkDeferredFootnote:
+ linkNode = NewNode(Link)
+ linkNode.Destination = link
+ linkNode.Title = title
+ linkNode.NoteID = noteID
+ linkNode.Footnote = footnoteNode
+ if t == linkInlineFootnote {
+ i++
}
- p.r.FootnoteRef(out, link, noteId)
-
- case linkDeferredFootnote:
- p.r.FootnoteRef(out, link, noteId)
-
default:
- return 0
+ return 0, nil
}
- return i
+ return i, linkNode
}
-func (p *parser) inlineHTMLComment(out *bytes.Buffer, data []byte) int {
+func (p *Markdown) inlineHTMLComment(data []byte) int {
if len(data) < 5 {
return 0
}
@@ -600,44 +592,75 @@ func (p *parser) inlineHTMLComment(out *bytes.Buffer, data []byte) int {
return i + 1
}
+func stripMailto(link []byte) []byte {
+ if bytes.HasPrefix(link, []byte("mailto://")) {
+ return link[9:]
+ } else if bytes.HasPrefix(link, []byte("mailto:")) {
+ return link[7:]
+ } else {
+ return link
+ }
+}
+
+// autolinkType specifies a kind of autolink that gets detected.
+type autolinkType int
+
+// These are the possible flag values for the autolink renderer.
+const (
+ notAutolink autolinkType = iota
+ normalAutolink
+ emailAutolink
+)
+
// '<' when tags or autolinks are allowed
-func leftAngle(p *parser, out *bytes.Buffer, data []byte, offset int) int {
+func leftAngle(p *Markdown, data []byte, offset int) (int, *Node) {
data = data[offset:]
- altype := LINK_TYPE_NOT_AUTOLINK
- end := tagLength(data, &altype)
- if size := p.inlineHTMLComment(out, data); size > 0 {
+ altype, end := tagLength(data)
+ if size := p.inlineHTMLComment(data); size > 0 {
end = size
}
if end > 2 {
- if altype != LINK_TYPE_NOT_AUTOLINK {
+ if altype != notAutolink {
var uLink bytes.Buffer
unescapeText(&uLink, data[1:end+1-2])
if uLink.Len() > 0 {
- p.r.AutoLink(out, uLink.Bytes(), altype)
+ link := uLink.Bytes()
+ node := NewNode(Link)
+ node.Destination = link
+ if altype == emailAutolink {
+ node.Destination = append([]byte("mailto:"), link...)
+ }
+ node.AppendChild(text(stripMailto(link)))
+ return end, node
}
} else {
- p.r.RawHtmlTag(out, data[:end])
+ htmlTag := NewNode(HTMLSpan)
+ htmlTag.Literal = data[:end]
+ return end, htmlTag
}
}
- return end
+ return end, nil
}
// '\\' backslash escape
var escapeChars = []byte("\\`*_{}[]()#+-.!:|&<>~")
-func escape(p *parser, out *bytes.Buffer, data []byte, offset int) int {
+func escape(p *Markdown, data []byte, offset int) (int, *Node) {
data = data[offset:]
if len(data) > 1 {
+ if p.extensions&BackslashLineBreak != 0 && data[1] == '\n' {
+ return 2, NewNode(Hardbreak)
+ }
if bytes.IndexByte(escapeChars, data[1]) < 0 {
- return 0
+ return 0, nil
}
- p.r.NormalText(out, data[1:2])
+ return 2, text(data[1:2])
}
- return 2
+ return 2, nil
}
func unescapeText(ob *bytes.Buffer, src []byte) {
@@ -663,7 +686,7 @@ func unescapeText(ob *bytes.Buffer, src []byte) {
// '&' escaped when it doesn't belong to an entity
// valid entities are assumed to be anything matching &#?[A-Za-z0-9]+;
-func entity(p *parser, out *bytes.Buffer, data []byte, offset int) int {
+func entity(p *Markdown, data []byte, offset int) (int, *Node) {
data = data[offset:]
end := 1
@@ -679,25 +702,70 @@ func entity(p *parser, out *bytes.Buffer, data []byte, offset int) int {
if end < len(data) && data[end] == ';' {
end++ // real entity
} else {
- return 0 // lone '&'
+ return 0, nil // lone '&'
}
- p.r.Entity(out, data[:end])
+ ent := data[:end]
+ // undo &amp; escaping or it will be converted to &amp;amp; by another
+ // escaper in the renderer
+ if bytes.Equal(ent, []byte("&amp;")) {
+ ent = []byte{'&'}
+ }
- return end
+ return end, text(ent)
}
func linkEndsWithEntity(data []byte, linkEnd int) bool {
- entityRanges := htmlEntity.FindAllIndex(data[:linkEnd], -1)
+ entityRanges := htmlEntityRe.FindAllIndex(data[:linkEnd], -1)
return entityRanges != nil && entityRanges[len(entityRanges)-1][1] == linkEnd
}
-func autoLink(p *parser, out *bytes.Buffer, data []byte, offset int) int {
- // quick check to rule out most false hits on ':'
- if p.insideLink || len(data) < offset+3 || data[offset+1] != '/' || data[offset+2] != '/' {
- return 0
+// hasPrefixCaseInsensitive is a custom implementation of
+// strings.HasPrefix(strings.ToLower(s), prefix)
+// we rolled our own because ToLower pulls in a huge machinery of lowercasing
+// anything from Unicode and that's very slow. Since this func will only be
+// used on ASCII protocol prefixes, we can take shortcuts.
+func hasPrefixCaseInsensitive(s, prefix []byte) bool {
+ if len(s) < len(prefix) {
+ return false
+ }
+ delta := byte('a' - 'A')
+ for i, b := range prefix {
+ if b != s[i] && b != s[i]+delta {
+ return false
+ }
}
+ return true
+}
+var protocolPrefixes = [][]byte{
+ []byte("http://"),
+ []byte("https://"),
+ []byte("ftp://"),
+ []byte("file://"),
+ []byte("mailto:"),
+}
+
+const shortestPrefix = 6 // len("ftp://"), the shortest of the above
+
+func maybeAutoLink(p *Markdown, data []byte, offset int) (int, *Node) {
+ // quick check to rule out most false hits
+ if p.insideLink || len(data) < offset+shortestPrefix {
+ return 0, nil
+ }
+ for _, prefix := range protocolPrefixes {
+ endOfHead := offset + 8 // 8 is the len() of the longest prefix
+ if endOfHead > len(data) {
+ endOfHead = len(data)
+ }
+ if hasPrefixCaseInsensitive(data[offset:endOfHead], prefix) {
+ return autoLink(p, data, offset)
+ }
+ }
+ return 0, nil
+}
+
+func autoLink(p *Markdown, data []byte, offset int) (int, *Node) {
// Now a more expensive check to see if we're not inside an anchor element
anchorStart := offset
offsetFromAnchor := 0
@@ -708,8 +776,9 @@ func autoLink(p *parser, out *bytes.Buffer, data []byte, offset int) int {
anchorStr := anchorRe.Find(data[anchorStart:])
if anchorStr != nil {
- out.Write(anchorStr[offsetFromAnchor:])
- return len(anchorStr) - offsetFromAnchor
+ anchorClose := NewNode(HTMLSpan)
+ anchorClose.Literal = anchorStr[offsetFromAnchor:]
+ return len(anchorStr) - offsetFromAnchor, anchorClose
}
// scan backward for a word boundary
@@ -718,14 +787,14 @@ func autoLink(p *parser, out *bytes.Buffer, data []byte, offset int) int {
rewind++
}
if rewind > 6 { // longest supported protocol is "mailto" which has 6 letters
- return 0
+ return 0, nil
}
origData := data
data = data[offset-rewind:]
if !isSafeLink(data) {
- return 0
+ return 0, nil
}
linkEnd := 0
@@ -802,19 +871,17 @@ func autoLink(p *parser, out *bytes.Buffer, data []byte, offset int) int {
}
}
- // we were triggered on the ':', so we need to rewind the output a bit
- if out.Len() >= rewind {
- out.Truncate(len(out.Bytes()) - rewind)
- }
-
var uLink bytes.Buffer
unescapeText(&uLink, data[:linkEnd])
if uLink.Len() > 0 {
- p.r.AutoLink(out, uLink.Bytes(), LINK_TYPE_NORMAL)
+ node := NewNode(Link)
+ node.Destination = uLink.Bytes()
+ node.AppendChild(text(uLink.Bytes()))
+ return linkEnd, node
}
- return linkEnd - rewind
+ return linkEnd, nil
}
func isEndOfLink(char byte) bool {
@@ -847,17 +914,17 @@ func isSafeLink(link []byte) bool {
}
// return the length of the given tag, or 0 is it's not valid
-func tagLength(data []byte, autolink *int) int {
+func tagLength(data []byte) (autolink autolinkType, end int) {
var i, j int
// a valid tag can't be shorter than 3 chars
if len(data) < 3 {
- return 0
+ return notAutolink, 0
}
// begins with a '<' optionally followed by '/', followed by letter or number
if data[0] != '<' {
- return 0
+ return notAutolink, 0
}
if data[1] == '/' {
i = 2
@@ -866,11 +933,11 @@ func tagLength(data []byte, autolink *int) int {
}
if !isalnum(data[i]) {
- return 0
+ return notAutolink, 0
}
// scheme test
- *autolink = LINK_TYPE_NOT_AUTOLINK
+ autolink = notAutolink
// try to find the beginning of an URI
for i < len(data) && (isalnum(data[i]) || data[i] == '.' || data[i] == '+' || data[i] == '-') {
@@ -879,21 +946,20 @@ func tagLength(data []byte, autolink *int) int {
if i > 1 && i < len(data) && data[i] == '@' {
if j = isMailtoAutoLink(data[i:]); j != 0 {
- *autolink = LINK_TYPE_EMAIL
- return i + j
+ return emailAutolink, i + j
}
}
if i > 2 && i < len(data) && data[i] == ':' {
- *autolink = LINK_TYPE_NORMAL
+ autolink = normalAutolink
i++
}
// complete autolink test: no whitespace or ' or "
switch {
case i >= len(data):
- *autolink = LINK_TYPE_NOT_AUTOLINK
- case *autolink != 0:
+ autolink = notAutolink
+ case autolink != notAutolink:
j = i
for i < len(data) {
@@ -908,24 +974,20 @@ func tagLength(data []byte, autolink *int) int {
}
if i >= len(data) {
- return 0
+ return autolink, 0
}
if i > j && data[i] == '>' {
- return i + 1
+ return autolink, i + 1
}
// one of the forbidden chars has been found
- *autolink = LINK_TYPE_NOT_AUTOLINK
+ autolink = notAutolink
}
-
- // look for something looking like a tag end
- for i < len(data) && data[i] != '>' {
- i++
- }
- if i >= len(data) {
- return 0
+ i += bytes.IndexByte(data[i:], '>')
+ if i < 0 {
+ return autolink, 0
}
- return i + 1
+ return autolink, i + 1
}
// look for the address part of a mail autolink and '>'
@@ -944,14 +1006,13 @@ func isMailtoAutoLink(data []byte) int {
nb++
case '-', '.', '_':
- // Do nothing.
+ break
case '>':
if nb == 1 {
return i + 1
- } else {
- return 0
}
+ return 0
default:
return 0
}
@@ -1014,9 +1075,8 @@ func helperFindEmphChar(data []byte, c byte) int {
if data[i] != '[' && data[i] != '(' { // not a link
if tmpI > 0 {
return tmpI
- } else {
- continue
}
+ continue
}
cc := data[i]
i++
@@ -1035,7 +1095,7 @@ func helperFindEmphChar(data []byte, c byte) int {
return 0
}
-func helperEmphasis(p *parser, out *bytes.Buffer, data []byte, c byte) int {
+func helperEmphasis(p *Markdown, data []byte, c byte) (int, *Node) {
i := 0
// skip one symbol if coming from emph3
@@ -1046,11 +1106,11 @@ func helperEmphasis(p *parser, out *bytes.Buffer, data []byte, c byte) int {
for i < len(data) {
length := helperFindEmphChar(data[i:], c)
if length == 0 {
- return 0
+ return 0, nil
}
i += length
if i >= len(data) {
- return 0
+ return 0, nil
}
if i+1 < len(data) && data[i+1] == c {
@@ -1060,52 +1120,46 @@ func helperEmphasis(p *parser, out *bytes.Buffer, data []byte, c byte) int {
if data[i] == c && !isspace(data[i-1]) {
- if p.flags&EXTENSION_NO_INTRA_EMPHASIS != 0 {
+ if p.extensions&NoIntraEmphasis != 0 {
if !(i+1 == len(data) || isspace(data[i+1]) || ispunct(data[i+1])) {
continue
}
}
- var work bytes.Buffer
- p.inline(&work, data[:i])
- p.r.Emphasis(out, work.Bytes())
- return i + 1
+ emph := NewNode(Emph)
+ p.inline(emph, data[:i])
+ return i + 1, emph
}
}
- return 0
+ return 0, nil
}
-func helperDoubleEmphasis(p *parser, out *bytes.Buffer, data []byte, c byte) int {
+func helperDoubleEmphasis(p *Markdown, data []byte, c byte) (int, *Node) {
i := 0
for i < len(data) {
length := helperFindEmphChar(data[i:], c)
if length == 0 {
- return 0
+ return 0, nil
}
i += length
if i+1 < len(data) && data[i] == c && data[i+1] == c && i > 0 && !isspace(data[i-1]) {
- var work bytes.Buffer
- p.inline(&work, data[:i])
-
- if work.Len() > 0 {
- // pick the right renderer
- if c == '~' {
- p.r.StrikeThrough(out, work.Bytes())
- } else {
- p.r.DoubleEmphasis(out, work.Bytes())
- }
+ nodeType := Strong
+ if c == '~' {
+ nodeType = Del
}
- return i + 2
+ node := NewNode(nodeType)
+ p.inline(node, data[:i])
+ return i + 2, node
}
i++
}
- return 0
+ return 0, nil
}
-func helperTripleEmphasis(p *parser, out *bytes.Buffer, data []byte, offset int, c byte) int {
+func helperTripleEmphasis(p *Markdown, data []byte, offset int, c byte) (int, *Node) {
i := 0
origData := data
data = data[offset:]
@@ -1113,7 +1167,7 @@ func helperTripleEmphasis(p *parser, out *bytes.Buffer, data []byte, offset int,
for i < len(data) {
length := helperFindEmphChar(data[i:], c)
if length == 0 {
- return 0
+ return 0, nil
}
i += length
@@ -1125,30 +1179,36 @@ func helperTripleEmphasis(p *parser, out *bytes.Buffer, data []byte, offset int,
switch {
case i+2 < len(data) && data[i+1] == c && data[i+2] == c:
// triple symbol found
- var work bytes.Buffer
-
- p.inline(&work, data[:i])
- if work.Len() > 0 {
- p.r.TripleEmphasis(out, work.Bytes())
- }
- return i + 3
+ strong := NewNode(Strong)
+ em := NewNode(Emph)
+ strong.AppendChild(em)
+ p.inline(em, data[:i])
+ return i + 3, strong
case (i+1 < len(data) && data[i+1] == c):
// double symbol found, hand over to emph1
- length = helperEmphasis(p, out, origData[offset-2:], c)
+ length, node := helperEmphasis(p, origData[offset-2:], c)
if length == 0 {
- return 0
- } else {
- return length - 2
+ return 0, nil
}
+ return length - 2, node
default:
// single symbol found, hand over to emph2
- length = helperDoubleEmphasis(p, out, origData[offset-1:], c)
+ length, node := helperDoubleEmphasis(p, origData[offset-1:], c)
if length == 0 {
- return 0
- } else {
- return length - 1
+ return 0, nil
}
+ return length - 1, node
}
}
- return 0
+ return 0, nil
+}
+
+func text(s []byte) *Node {
+ node := NewNode(Text)
+ node.Literal = s
+ return node
+}
+
+func normalizeURI(s []byte) []byte {
+ return s // TODO: implement
}