all repos — grayfriday @ 2aca6670787400c2c9c065606b5f9db2eb6ff525

blackfriday fork with a few changes

simplify inline callback interface
Russ Ross russ@dixie.edu
Wed, 29 Jun 2011 13:00:54 -0600
commit

2aca6670787400c2c9c065606b5f9db2eb6ff525

parent

8b9cd447d79fee1450f0ad18063edbc375d4225b

6 files changed, 70 insertions(+), 110 deletions(-)

jump to
M html.gohtml.go

@@ -374,12 +374,12 @@ }

out.WriteString("</p>\n") } -func (options *Html) AutoLink(out *bytes.Buffer, link []byte, kind int) bool { +func (options *Html) AutoLink(out *bytes.Buffer, link []byte, kind int) { if len(link) == 0 { - return false + return } if options.flags&HTML_SAFELINK != 0 && !isSafeLink(link) && kind != LINK_TYPE_EMAIL { - return false + return } out.WriteString("<a href=\"")

@@ -404,44 +404,39 @@ attrEscape(out, link)

} out.WriteString("</a>") - - return true } -func (options *Html) CodeSpan(out *bytes.Buffer, text []byte) bool { +func (options *Html) CodeSpan(out *bytes.Buffer, text []byte) { out.WriteString("<code>") attrEscape(out, text) out.WriteString("</code>") - return true } -func (options *Html) DoubleEmphasis(out *bytes.Buffer, text []byte) bool { +func (options *Html) DoubleEmphasis(out *bytes.Buffer, text []byte) { if len(text) == 0 { - return false + return } out.WriteString("<strong>") out.Write(text) out.WriteString("</strong>") - return true } -func (options *Html) Emphasis(out *bytes.Buffer, text []byte) bool { +func (options *Html) Emphasis(out *bytes.Buffer, text []byte) { if len(text) == 0 { - return false + return } out.WriteString("<em>") out.Write(text) out.WriteString("</em>") - return true } -func (options *Html) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) bool { +func (options *Html) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) { if options.flags&HTML_SKIP_IMAGES != 0 { - return false + return } if len(link) == 0 { - return false + return } out.WriteString("<img src=\"") attrEscape(out, link)

@@ -456,22 +451,21 @@ }

out.WriteByte('"') out.WriteString(options.closeTag) - return true + return } -func (options *Html) LineBreak(out *bytes.Buffer) bool { +func (options *Html) LineBreak(out *bytes.Buffer) { out.WriteString("<br") out.WriteString(options.closeTag) - return true } -func (options *Html) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) bool { +func (options *Html) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) { if options.flags&HTML_SKIP_LINKS != 0 { - return false + return } if options.flags&HTML_SAFELINK != 0 && !isSafeLink(link) { - return false + return } out.WriteString("<a href=\"")

@@ -483,44 +477,41 @@ }

out.WriteString("\">") out.Write(content) out.WriteString("</a>") - return true + return } -func (options *Html) RawHtmlTag(out *bytes.Buffer, text []byte) bool { +func (options *Html) RawHtmlTag(out *bytes.Buffer, text []byte) { if options.flags&HTML_SKIP_HTML != 0 { - return true + return } if options.flags&HTML_SKIP_STYLE != 0 && isHtmlTag(text, "style") { - return true + return } if options.flags&HTML_SKIP_LINKS != 0 && isHtmlTag(text, "a") { - return true + return } if options.flags&HTML_SKIP_IMAGES != 0 && isHtmlTag(text, "img") { - return true + return } out.Write(text) - return true } -func (options *Html) TripleEmphasis(out *bytes.Buffer, text []byte) bool { +func (options *Html) TripleEmphasis(out *bytes.Buffer, text []byte) { if len(text) == 0 { - return false + return } out.WriteString("<strong><em>") out.Write(text) out.WriteString("</em></strong>") - return true } -func (options *Html) StrikeThrough(out *bytes.Buffer, text []byte) bool { +func (options *Html) StrikeThrough(out *bytes.Buffer, text []byte) { if len(text) == 0 { - return false + return } out.WriteString("<del>") out.Write(text) out.WriteString("</del>") - return true } func (options *Html) Entity(out *bytes.Buffer, entity []byte) {
M inline.goinline.go

@@ -139,9 +139,7 @@ fEnd--

} // render the code span - if !parser.r.CodeSpan(out, data[fBegin:fEnd]) { - end = 0 - } + parser.r.CodeSpan(out, data[fBegin:fEnd]) return end

@@ -164,13 +162,8 @@ if parser.flags&EXTENSION_HARD_LINE_BREAK == 0 && end-eol < 2 {

return 0 } - if parser.r.LineBreak(out) { - return 1 - } else { - return 0 - } - - return 0 + parser.r.LineBreak(out) + return 1 } // '[': parse a link or an image

@@ -415,8 +408,12 @@ unescapeText(&uLinkBuf, link)

uLink = uLinkBuf.Bytes() } + // links need something to click on and somewhere to go + if len(uLink) == 0 || content.Len() == 0 { + return 0 + } + // call the relevant rendering function - ret := false if isImg { outSize := out.Len() outBytes := out.Bytes()

@@ -424,15 +421,12 @@ if outSize > 0 && outBytes[outSize-1] == '!' {

out.Truncate(outSize - 1) } - ret = parser.r.Image(out, uLink, title, content.Bytes()) + parser.r.Image(out, uLink, title, content.Bytes()) } else { - ret = parser.r.Link(out, uLink, title, content.Bytes()) + parser.r.Link(out, uLink, title, content.Bytes()) } - if ret { - return i - } - return 0 + return i } // '<' when tags or autolinks are allowed

@@ -440,21 +434,17 @@ func inlineLAngle(parser *Parser, out *bytes.Buffer, data []byte, offset int) int {

data = data[offset:] altype := LINK_TYPE_NOT_AUTOLINK end := tagLength(data, &altype) - ret := false if end > 2 { if altype != LINK_TYPE_NOT_AUTOLINK { var uLink bytes.Buffer unescapeText(&uLink, data[1:end+1-2]) - ret = parser.r.AutoLink(out, uLink.Bytes(), altype) + parser.r.AutoLink(out, uLink.Bytes(), altype) } else { - ret = parser.r.RawHtmlTag(out, data[:end]) + parser.r.RawHtmlTag(out, data[:end]) } } - if !ret { - return 0 - } return end }

@@ -867,11 +857,8 @@ }

var work bytes.Buffer parser.parseInline(&work, data[:i]) - if parser.r.Emphasis(out, work.Bytes()) { - return i + 1 - } else { - return 0 - } + parser.r.Emphasis(out, work.Bytes()) + return i + 1 } }

@@ -891,19 +878,14 @@

if i+1 < len(data) && data[i] == c && data[i+1] == c && i > 0 && !isspace(data[i-1]) { var work bytes.Buffer parser.parseInline(&work, data[:i]) - success := false // pick the right renderer if c == '~' { - success = parser.r.StrikeThrough(out, work.Bytes()) + parser.r.StrikeThrough(out, work.Bytes()) } else { - success = parser.r.DoubleEmphasis(out, work.Bytes()) + parser.r.DoubleEmphasis(out, work.Bytes()) } - if success { - return i + 2 - } else { - return 0 - } + return i + 2 } i++ }

@@ -933,11 +915,8 @@ // triple symbol found

var work bytes.Buffer parser.parseInline(&work, data[:i]) - if parser.r.TripleEmphasis(out, work.Bytes()) { - return i + 3 - } else { - return 0 - } + parser.r.TripleEmphasis(out, work.Bytes()) + return i + 3 case (i+1 < len(data) && data[i+1] == c): // double symbol found, hand over to emph1 length = inlineHelperEmph1(parser, out, origData[offset-2:], c)
M inline_test.goinline_test.go

@@ -276,7 +276,7 @@ "[foo with a title](/bar/ title with no quotes)\n",

"<p><a href=\"/bar/ title with no quotes\">foo with a title</a></p>\n", "[foo]()\n", - "<p><a href=\"\">foo</a></p>\n", + "<p>[foo]()</p>\n", "![foo](/bar/)\n", "<p><img src=\"/bar/\" alt=\"foo\" />\n</p>\n",

@@ -294,7 +294,7 @@ "![foo with a title](/bar/ title with no quotes)\n",

"<p><img src=\"/bar/ title with no quotes\" alt=\"foo with a title\" />\n</p>\n", "![foo]()\n", - "<p>[foo]()</p>\n", + "<p>![foo]()</p>\n", "[a link]\t(/with_a_tab/)\n", "<p><a href=\"/with_a_tab/\">a link</a></p>\n",
M latex.golatex.go

@@ -151,7 +151,7 @@ }

out.Write(text) } -func (options *Latex) AutoLink(out *bytes.Buffer, link []byte, kind int) bool { +func (options *Latex) AutoLink(out *bytes.Buffer, link []byte, kind int) { out.WriteString("\\href{") if kind == LINK_TYPE_EMAIL { out.WriteString("mailto:")

@@ -160,31 +160,27 @@ out.Write(link)

out.WriteString("}{") out.Write(link) out.WriteString("}") - return true } -func (options *Latex) CodeSpan(out *bytes.Buffer, text []byte) bool { +func (options *Latex) CodeSpan(out *bytes.Buffer, text []byte) { out.WriteString("\\texttt{") escapeSpecialChars(out, text) out.WriteString("}") - return true } -func (options *Latex) DoubleEmphasis(out *bytes.Buffer, text []byte) bool { +func (options *Latex) DoubleEmphasis(out *bytes.Buffer, text []byte) { out.WriteString("\\textbf{") out.Write(text) out.WriteString("}") - return true } -func (options *Latex) Emphasis(out *bytes.Buffer, text []byte) bool { +func (options *Latex) Emphasis(out *bytes.Buffer, text []byte) { out.WriteString("\\textit{") out.Write(text) out.WriteString("}") - return true } -func (options *Latex) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) bool { +func (options *Latex) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) { if bytes.HasPrefix(link, []byte("http://")) || bytes.HasPrefix(link, []byte("https://")) { // treat it like a link out.WriteString("\\href{")

@@ -197,39 +193,33 @@ out.WriteString("\\includegraphics{")

out.Write(link) out.WriteString("}") } - return true } -func (options *Latex) LineBreak(out *bytes.Buffer) bool { +func (options *Latex) LineBreak(out *bytes.Buffer) { out.WriteString(" \\\\\n") - return true } -func (options *Latex) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) bool { +func (options *Latex) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) { out.WriteString("\\href{") out.Write(link) out.WriteString("}{") out.Write(content) out.WriteString("}") - return true } -func (options *Latex) RawHtmlTag(out *bytes.Buffer, tag []byte) bool { - return true +func (options *Latex) RawHtmlTag(out *bytes.Buffer, tag []byte) { } -func (options *Latex) TripleEmphasis(out *bytes.Buffer, text []byte) bool { +func (options *Latex) TripleEmphasis(out *bytes.Buffer, text []byte) { out.WriteString("\\textbf{\\textit{") out.Write(text) out.WriteString("}}") - return true } -func (options *Latex) StrikeThrough(out *bytes.Buffer, text []byte) bool { +func (options *Latex) StrikeThrough(out *bytes.Buffer, text []byte) { out.WriteString("\\sout{") out.Write(text) out.WriteString("}") - return true } func needsBackslash(c byte) bool {
M markdown.gomarkdown.go

@@ -114,17 +114,17 @@ Table(out *bytes.Buffer, header []byte, body []byte, columnData []int)

TableRow(out *bytes.Buffer, text []byte) TableCell(out *bytes.Buffer, text []byte, flags int) - // Span-level callbacks---return false prints the span verbatim - AutoLink(out *bytes.Buffer, link []byte, kind int) bool - CodeSpan(out *bytes.Buffer, text []byte) bool - DoubleEmphasis(out *bytes.Buffer, text []byte) bool - Emphasis(out *bytes.Buffer, text []byte) bool - Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) bool - LineBreak(out *bytes.Buffer) bool - Link(out *bytes.Buffer, link []byte, title []byte, content []byte) bool - RawHtmlTag(out *bytes.Buffer, tag []byte) bool - TripleEmphasis(out *bytes.Buffer, text []byte) bool - StrikeThrough(out *bytes.Buffer, text []byte) bool + // Span-level callbacks + AutoLink(out *bytes.Buffer, link []byte, kind int) + CodeSpan(out *bytes.Buffer, text []byte) + DoubleEmphasis(out *bytes.Buffer, text []byte) + Emphasis(out *bytes.Buffer, text []byte) + Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) + LineBreak(out *bytes.Buffer) + Link(out *bytes.Buffer, link []byte, title []byte, content []byte) + RawHtmlTag(out *bytes.Buffer, tag []byte) + TripleEmphasis(out *bytes.Buffer, text []byte) + StrikeThrough(out *bytes.Buffer, text []byte) // Low-level callbacks Entity(out *bytes.Buffer, entity []byte)
M upskirtref/Links, inline style.htmlupskirtref/Links, inline style.html

@@ -8,4 +8,4 @@ <p><a href="/url/" title="title preceded by a tab">URL and title</a>.</p>

<p><a href="/url/" title="title has spaces afterward">URL and title</a>.</p> -<p><a href="">Empty</a>.</p> +<p>[Empty]().</p>