refactoring: inline renderers return bools, preparing rendering struct to become an interface

pull/5/merge
Russ Ross 2011-06-28 19:46:35 -06:00
parent f0cd9a420e
commit b1a0318250
5 changed files with 167 additions and 160 deletions

123
html.go
View File

@ -51,15 +51,9 @@ var htmlClose = ">\n"
func HtmlRenderer(flags int) *Renderer {
// configure the rendering engine
r := new(Renderer)
if flags&HTML_GITHUB_BLOCKCODE == 0 {
r.BlockCode = htmlBlockCode
} else {
r.BlockCode = htmlBlockCodeGithub
}
r.BlockCode = htmlBlockCode
r.BlockQuote = htmlBlockQuote
if flags&HTML_SKIP_HTML == 0 {
r.BlockHtml = htmlRawBlock
}
r.BlockHtml = htmlBlockHtml
r.Header = htmlHeader
r.HRule = htmlHRule
r.List = htmlList
@ -73,30 +67,19 @@ func HtmlRenderer(flags int) *Renderer {
r.CodeSpan = htmlCodeSpan
r.DoubleEmphasis = htmlDoubleEmphasis
r.Emphasis = htmlEmphasis
if flags&HTML_SKIP_IMAGES == 0 {
r.Image = htmlImage
}
r.Image = htmlImage
r.LineBreak = htmlLineBreak
if flags&HTML_SKIP_LINKS == 0 {
r.Link = htmlLink
}
r.Link = htmlLink
r.RawHtmlTag = htmlRawTag
r.TripleEmphasis = htmlTripleEmphasis
r.StrikeThrough = htmlStrikeThrough
var cb *SmartypantsRenderer
if flags&HTML_USE_SMARTYPANTS == 0 {
r.NormalText = htmlNormalText
} else {
cb = Smartypants(flags)
r.NormalText = htmlSmartypants
}
r.NormalText = htmlNormalText
closeTag := htmlClose
if flags&HTML_USE_XHTML != 0 {
closeTag = xhtmlClose
}
r.Opaque = &htmlOptions{flags: flags, closeTag: closeTag, smartypants: cb}
r.Opaque = &htmlOptions{flags: flags, closeTag: closeTag, smartypants: Smartypants(flags)}
return r
}
@ -189,7 +172,12 @@ func htmlHeader(out *bytes.Buffer, text func() bool, level int, opaque interface
out.WriteString(fmt.Sprintf("</h%d>\n", level))
}
func htmlRawBlock(out *bytes.Buffer, text []byte, opaque interface{}) {
func htmlBlockHtml(out *bytes.Buffer, text []byte, opaque interface{}) {
options := opaque.(*htmlOptions)
if options.flags&HTML_SKIP_HTML != 0 {
return
}
sz := len(text)
for sz > 0 && text[sz-1] == '\n' {
sz--
@ -219,6 +207,15 @@ func htmlHRule(out *bytes.Buffer, opaque interface{}) {
}
func htmlBlockCode(out *bytes.Buffer, text []byte, lang string, opaque interface{}) {
options := opaque.(*htmlOptions)
if options.flags&HTML_GITHUB_BLOCKCODE != 0 {
htmlBlockCodeGithub(out, text, lang, opaque)
} else {
htmlBlockCodeNormal(out, text, lang, opaque)
}
}
func htmlBlockCodeNormal(out *bytes.Buffer, text []byte, lang string, opaque interface{}) {
if out.Len() > 0 {
out.WriteByte('\n')
}
@ -401,14 +398,14 @@ func htmlParagraph(out *bytes.Buffer, text func() bool, opaque interface{}) {
out.WriteString("</p>\n")
}
func htmlAutoLink(out *bytes.Buffer, link []byte, kind int, opaque interface{}) int {
func htmlAutoLink(out *bytes.Buffer, link []byte, kind int, opaque interface{}) bool {
options := opaque.(*htmlOptions)
if len(link) == 0 {
return 0
return false
}
if options.flags&HTML_SAFELINK != 0 && !isSafeLink(link) && kind != LINK_TYPE_EMAIL {
return 0
return false
}
out.WriteString("<a href=\"")
@ -434,40 +431,44 @@ func htmlAutoLink(out *bytes.Buffer, link []byte, kind int, opaque interface{})
out.WriteString("</a>")
return 1
return true
}
func htmlCodeSpan(out *bytes.Buffer, text []byte, opaque interface{}) int {
func htmlCodeSpan(out *bytes.Buffer, text []byte, opaque interface{}) bool {
out.WriteString("<code>")
attrEscape(out, text)
out.WriteString("</code>")
return 1
return true
}
func htmlDoubleEmphasis(out *bytes.Buffer, text []byte, opaque interface{}) int {
func htmlDoubleEmphasis(out *bytes.Buffer, text []byte, opaque interface{}) bool {
if len(text) == 0 {
return 0
return false
}
out.WriteString("<strong>")
out.Write(text)
out.WriteString("</strong>")
return 1
return true
}
func htmlEmphasis(out *bytes.Buffer, text []byte, opaque interface{}) int {
func htmlEmphasis(out *bytes.Buffer, text []byte, opaque interface{}) bool {
if len(text) == 0 {
return 0
return false
}
out.WriteString("<em>")
out.Write(text)
out.WriteString("</em>")
return 1
return true
}
func htmlImage(out *bytes.Buffer, link []byte, title []byte, alt []byte, opaque interface{}) int {
func htmlImage(out *bytes.Buffer, link []byte, title []byte, alt []byte, opaque interface{}) bool {
options := opaque.(*htmlOptions)
if options.flags&HTML_SKIP_IMAGES != 0 {
return false
}
if len(link) == 0 {
return 0
return false
}
out.WriteString("<img src=\"")
attrEscape(out, link)
@ -482,21 +483,24 @@ func htmlImage(out *bytes.Buffer, link []byte, title []byte, alt []byte, opaque
out.WriteByte('"')
out.WriteString(options.closeTag)
return 1
return true
}
func htmlLineBreak(out *bytes.Buffer, opaque interface{}) int {
func htmlLineBreak(out *bytes.Buffer, opaque interface{}) bool {
options := opaque.(*htmlOptions)
out.WriteString("<br")
out.WriteString(options.closeTag)
return 1
return true
}
func htmlLink(out *bytes.Buffer, link []byte, title []byte, content []byte, opaque interface{}) int {
func htmlLink(out *bytes.Buffer, link []byte, title []byte, content []byte, opaque interface{}) bool {
options := opaque.(*htmlOptions)
if options.flags&HTML_SKIP_LINKS != 0 {
return false
}
if options.flags&HTML_SAFELINK != 0 && !isSafeLink(link) {
return 0
return false
}
out.WriteString("<a href=\"")
@ -508,49 +512,54 @@ func htmlLink(out *bytes.Buffer, link []byte, title []byte, content []byte, opaq
out.WriteString("\">")
out.Write(content)
out.WriteString("</a>")
return 1
return true
}
func htmlRawTag(out *bytes.Buffer, text []byte, opaque interface{}) int {
func htmlRawTag(out *bytes.Buffer, text []byte, opaque interface{}) bool {
options := opaque.(*htmlOptions)
if options.flags&HTML_SKIP_HTML != 0 {
return 1
return true
}
if options.flags&HTML_SKIP_STYLE != 0 && isHtmlTag(text, "style") {
return 1
return true
}
if options.flags&HTML_SKIP_LINKS != 0 && isHtmlTag(text, "a") {
return 1
return true
}
if options.flags&HTML_SKIP_IMAGES != 0 && isHtmlTag(text, "img") {
return 1
return true
}
out.Write(text)
return 1
return true
}
func htmlTripleEmphasis(out *bytes.Buffer, text []byte, opaque interface{}) int {
func htmlTripleEmphasis(out *bytes.Buffer, text []byte, opaque interface{}) bool {
if len(text) == 0 {
return 0
return false
}
out.WriteString("<strong><em>")
out.Write(text)
out.WriteString("</em></strong>")
return 1
return true
}
func htmlStrikeThrough(out *bytes.Buffer, text []byte, opaque interface{}) int {
func htmlStrikeThrough(out *bytes.Buffer, text []byte, opaque interface{}) bool {
if len(text) == 0 {
return 0
return false
}
out.WriteString("<del>")
out.Write(text)
out.WriteString("</del>")
return 1
return true
}
func htmlNormalText(out *bytes.Buffer, text []byte, opaque interface{}) {
attrEscape(out, text)
options := opaque.(*htmlOptions)
if options.flags&HTML_USE_SMARTYPANTS != 0 {
htmlSmartypants(out, text, opaque)
} else {
attrEscape(out, text)
}
}
func htmlTocHeader(out *bytes.Buffer, text func() bool, level int, opaque interface{}) {

View File

@ -146,7 +146,7 @@ func inlineCodeSpan(out *bytes.Buffer, parser *Parser, data []byte, offset int)
if parser.r.CodeSpan == nil {
return 0
}
if parser.r.CodeSpan(out, data[fBegin:fEnd], parser.r.Opaque) == 0 {
if !parser.r.CodeSpan(out, data[fBegin:fEnd], parser.r.Opaque) {
end = 0
}
@ -174,7 +174,7 @@ func inlineLineBreak(out *bytes.Buffer, parser *Parser, data []byte, offset int)
if parser.r.LineBreak == nil {
return 0
}
if parser.r.LineBreak(out, parser.r.Opaque) > 0 {
if parser.r.LineBreak(out, parser.r.Opaque) {
return 1
} else {
return 0
@ -431,7 +431,7 @@ func inlineLink(out *bytes.Buffer, parser *Parser, data []byte, offset int) int
}
// call the relevant rendering function
ret := 0
ret := false
if isImg {
outSize := out.Len()
outBytes := out.Bytes()
@ -444,7 +444,7 @@ func inlineLink(out *bytes.Buffer, parser *Parser, data []byte, offset int) int
ret = parser.r.Link(out, uLink, title, content.Bytes(), parser.r.Opaque)
}
if ret > 0 {
if ret {
return i
}
return 0
@ -455,7 +455,7 @@ func inlineLAngle(out *bytes.Buffer, parser *Parser, data []byte, offset int) in
data = data[offset:]
altype := LINK_TYPE_NOT_AUTOLINK
end := tagLength(data, &altype)
ret := 0
ret := false
if end > 2 {
switch {
@ -468,7 +468,7 @@ func inlineLAngle(out *bytes.Buffer, parser *Parser, data []byte, offset int) in
}
}
if ret == 0 {
if !ret {
return 0
}
return end
@ -897,8 +897,7 @@ func inlineHelperEmph1(out *bytes.Buffer, parser *Parser, data []byte, c byte) i
var work bytes.Buffer
parser.parseInline(&work, data[:i])
r := parser.r.Emphasis(out, work.Bytes(), parser.r.Opaque)
if r > 0 {
if parser.r.Emphasis(out, work.Bytes(), parser.r.Opaque) {
return i + 1
} else {
return 0
@ -931,8 +930,7 @@ func inlineHelperEmph2(out *bytes.Buffer, parser *Parser, data []byte, c byte) i
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])
r := renderMethod(out, work.Bytes(), parser.r.Opaque)
if r > 0 {
if renderMethod(out, work.Bytes(), parser.r.Opaque) {
return i + 2
} else {
return 0
@ -966,8 +964,7 @@ func inlineHelperEmph3(out *bytes.Buffer, parser *Parser, data []byte, offset in
var work bytes.Buffer
parser.parseInline(&work, data[:i])
r := parser.r.TripleEmphasis(out, work.Bytes(), parser.r.Opaque)
if r > 0 {
if parser.r.TripleEmphasis(out, work.Bytes(), parser.r.Opaque) {
return i + 3
} else {
return 0

View File

@ -173,7 +173,7 @@ func latexTableCell(out *bytes.Buffer, text []byte, align int, opaque interface{
out.Write(text)
}
func latexAutoLink(out *bytes.Buffer, link []byte, kind int, opaque interface{}) int {
func latexAutoLink(out *bytes.Buffer, link []byte, kind int, opaque interface{}) bool {
out.WriteString("\\href{")
if kind == LINK_TYPE_EMAIL {
out.WriteString("mailto:")
@ -182,31 +182,31 @@ func latexAutoLink(out *bytes.Buffer, link []byte, kind int, opaque interface{})
out.WriteString("}{")
out.Write(link)
out.WriteString("}")
return 1
return true
}
func latexCodeSpan(out *bytes.Buffer, text []byte, opaque interface{}) int {
func latexCodeSpan(out *bytes.Buffer, text []byte, opaque interface{}) bool {
out.WriteString("\\texttt{")
escapeSpecialChars(out, text)
out.WriteString("}")
return 1
return true
}
func latexDoubleEmphasis(out *bytes.Buffer, text []byte, opaque interface{}) int {
func latexDoubleEmphasis(out *bytes.Buffer, text []byte, opaque interface{}) bool {
out.WriteString("\\textbf{")
out.Write(text)
out.WriteString("}")
return 1
return true
}
func latexEmphasis(out *bytes.Buffer, text []byte, opaque interface{}) int {
func latexEmphasis(out *bytes.Buffer, text []byte, opaque interface{}) bool {
out.WriteString("\\textit{")
out.Write(text)
out.WriteString("}")
return 1
return true
}
func latexImage(out *bytes.Buffer, link []byte, title []byte, alt []byte, opaque interface{}) int {
func latexImage(out *bytes.Buffer, link []byte, title []byte, alt []byte, opaque interface{}) bool {
if bytes.HasPrefix(link, []byte("http://")) || bytes.HasPrefix(link, []byte("https://")) {
// treat it like a link
out.WriteString("\\href{")
@ -219,39 +219,39 @@ func latexImage(out *bytes.Buffer, link []byte, title []byte, alt []byte, opaque
out.Write(link)
out.WriteString("}")
}
return 1
return true
}
func latexLineBreak(out *bytes.Buffer, opaque interface{}) int {
func latexLineBreak(out *bytes.Buffer, opaque interface{}) bool {
out.WriteString(" \\\\\n")
return 1
return true
}
func latexLink(out *bytes.Buffer, link []byte, title []byte, content []byte, opaque interface{}) int {
func latexLink(out *bytes.Buffer, link []byte, title []byte, content []byte, opaque interface{}) bool {
out.WriteString("\\href{")
out.Write(link)
out.WriteString("}{")
out.Write(content)
out.WriteString("}")
return 1
return true
}
func latexRawHtmlTag(out *bytes.Buffer, tag []byte, opaque interface{}) int {
return 0
func latexRawHtmlTag(out *bytes.Buffer, tag []byte, opaque interface{}) bool {
return true
}
func latexTripleEmphasis(out *bytes.Buffer, text []byte, opaque interface{}) int {
func latexTripleEmphasis(out *bytes.Buffer, text []byte, opaque interface{}) bool {
out.WriteString("\\textbf{\\textit{")
out.Write(text)
out.WriteString("}}")
return 1
return true
}
func latexStrikeThrough(out *bytes.Buffer, text []byte, opaque interface{}) int {
func latexStrikeThrough(out *bytes.Buffer, text []byte, opaque interface{}) bool {
out.WriteString("\\sout{")
out.Write(text)
out.WriteString("}")
return 1
return true
}
func needsBackslash(c byte) bool {

View File

@ -119,17 +119,17 @@ type Renderer struct {
TableRow func(out *bytes.Buffer, text []byte, opaque interface{})
TableCell func(out *bytes.Buffer, text []byte, flags int, opaque interface{})
// Span-level callbacks---nil or return 0 prints the span verbatim
AutoLink func(out *bytes.Buffer, link []byte, kind int, opaque interface{}) int
CodeSpan func(out *bytes.Buffer, text []byte, opaque interface{}) int
DoubleEmphasis func(out *bytes.Buffer, text []byte, opaque interface{}) int
Emphasis func(out *bytes.Buffer, text []byte, opaque interface{}) int
Image func(out *bytes.Buffer, link []byte, title []byte, alt []byte, opaque interface{}) int
LineBreak func(out *bytes.Buffer, opaque interface{}) int
Link func(out *bytes.Buffer, link []byte, title []byte, content []byte, opaque interface{}) int
RawHtmlTag func(out *bytes.Buffer, tag []byte, opaque interface{}) int
TripleEmphasis func(out *bytes.Buffer, text []byte, opaque interface{}) int
StrikeThrough func(out *bytes.Buffer, text []byte, opaque interface{}) int
// Span-level callbacks---nil or return false prints the span verbatim
AutoLink func(out *bytes.Buffer, link []byte, kind int, opaque interface{}) bool
CodeSpan func(out *bytes.Buffer, text []byte, opaque interface{}) bool
DoubleEmphasis func(out *bytes.Buffer, text []byte, opaque interface{}) bool
Emphasis func(out *bytes.Buffer, text []byte, opaque interface{}) bool
Image func(out *bytes.Buffer, link []byte, title []byte, alt []byte, opaque interface{}) bool
LineBreak func(out *bytes.Buffer, opaque interface{}) bool
Link func(out *bytes.Buffer, link []byte, title []byte, content []byte, opaque interface{}) bool
RawHtmlTag func(out *bytes.Buffer, tag []byte, opaque interface{}) bool
TripleEmphasis func(out *bytes.Buffer, text []byte, opaque interface{}) bool
StrikeThrough func(out *bytes.Buffer, text []byte, opaque interface{}) bool
// Low-level callbacks---nil copies input directly into the output
Entity func(out *bytes.Buffer, entity []byte, opaque interface{})

View File

@ -39,7 +39,7 @@ func isdigit(c byte) bool {
return c >= '0' && c <= '9'
}
func smartQuotesHelper(ob *bytes.Buffer, previousChar byte, nextChar byte, quote byte, isOpen *bool) bool {
func smartQuotesHelper(out *bytes.Buffer, previousChar byte, nextChar byte, quote byte, isOpen *bool) bool {
// edge of the buffer is likely to be a tag that we don't get to see,
// so we treat it like text sometimes
@ -96,18 +96,18 @@ func smartQuotesHelper(ob *bytes.Buffer, previousChar byte, nextChar byte, quote
*isOpen = false
}
ob.WriteByte('&')
out.WriteByte('&')
if *isOpen {
ob.WriteByte('l')
out.WriteByte('l')
} else {
ob.WriteByte('r')
out.WriteByte('r')
}
ob.WriteByte(quote)
ob.WriteString("quo;")
out.WriteByte(quote)
out.WriteString("quo;")
return true
}
func smartSquote(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
func smartSquote(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
if len(text) >= 2 {
t1 := tolower(text[1])
@ -116,21 +116,22 @@ func smartSquote(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, tex
if len(text) >= 3 {
nextChar = text[2]
}
if smartQuotesHelper(ob, previousChar, nextChar, 'd', &smrt.inDoubleQuote) {
if smartQuotesHelper(out, previousChar, nextChar, 'd', &smrt.inDoubleQuote) {
return 1
}
}
if (t1 == 's' || t1 == 't' || t1 == 'm' || t1 == 'd') && (len(text) < 3 || wordBoundary(text[2])) {
ob.WriteString("&rsquo;")
out.WriteString("&rsquo;")
return 0
}
if len(text) >= 3 {
t2 := tolower(text[2])
if ((t1 == 'r' && t2 == 'e') || (t1 == 'l' && t2 == 'l') || (t1 == 'v' && t2 == 'e')) && (len(text) < 4 || wordBoundary(text[3])) {
ob.WriteString("&rsquo;")
if ((t1 == 'r' && t2 == 'e') || (t1 == 'l' && t2 == 'l') || (t1 == 'v' && t2 == 'e')) &&
(len(text) < 4 || wordBoundary(text[3])) {
out.WriteString("&rsquo;")
return 0
}
}
@ -140,77 +141,77 @@ func smartSquote(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, tex
if len(text) > 1 {
nextChar = text[1]
}
if smartQuotesHelper(ob, previousChar, nextChar, 's', &smrt.inSingleQuote) {
if smartQuotesHelper(out, previousChar, nextChar, 's', &smrt.inSingleQuote) {
return 0
}
ob.WriteByte(text[0])
out.WriteByte(text[0])
return 0
}
func smartParens(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
func smartParens(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
if len(text) >= 3 {
t1 := tolower(text[1])
t2 := tolower(text[2])
if t1 == 'c' && t2 == ')' {
ob.WriteString("&copy;")
out.WriteString("&copy;")
return 2
}
if t1 == 'r' && t2 == ')' {
ob.WriteString("&reg;")
out.WriteString("&reg;")
return 2
}
if len(text) >= 4 && t1 == 't' && t2 == 'm' && text[3] == ')' {
ob.WriteString("&trade;")
out.WriteString("&trade;")
return 3
}
}
ob.WriteByte(text[0])
out.WriteByte(text[0])
return 0
}
func smartDash(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
func smartDash(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
if len(text) >= 2 {
if text[1] == '-' {
ob.WriteString("&mdash;")
out.WriteString("&mdash;")
return 1
}
if wordBoundary(previousChar) && wordBoundary(text[1]) {
ob.WriteString("&ndash;")
out.WriteString("&ndash;")
return 0
}
}
ob.WriteByte(text[0])
out.WriteByte(text[0])
return 0
}
func smartDashLatex(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
func smartDashLatex(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
if len(text) >= 3 && text[1] == '-' && text[2] == '-' {
ob.WriteString("&mdash;")
out.WriteString("&mdash;")
return 2
}
if len(text) >= 2 && text[1] == '-' {
ob.WriteString("&ndash;")
out.WriteString("&ndash;")
return 1
}
ob.WriteByte(text[0])
out.WriteByte(text[0])
return 0
}
func smartAmp(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
func smartAmp(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
if bytes.HasPrefix(text, []byte("&quot;")) {
nextChar := byte(0)
if len(text) >= 7 {
nextChar = text[6]
}
if smartQuotesHelper(ob, previousChar, nextChar, 'd', &smrt.inDoubleQuote) {
if smartQuotesHelper(out, previousChar, nextChar, 'd', &smrt.inDoubleQuote) {
return 5
}
}
@ -219,32 +220,32 @@ func smartAmp(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text [
return 3
}
ob.WriteByte('&')
out.WriteByte('&')
return 0
}
func smartPeriod(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
func smartPeriod(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
if len(text) >= 3 && text[1] == '.' && text[2] == '.' {
ob.WriteString("&hellip;")
out.WriteString("&hellip;")
return 2
}
if len(text) >= 5 && text[1] == ' ' && text[2] == '.' && text[3] == ' ' && text[4] == '.' {
ob.WriteString("&hellip;")
out.WriteString("&hellip;")
return 4
}
ob.WriteByte(text[0])
out.WriteByte(text[0])
return 0
}
func smartBacktick(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
func smartBacktick(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
if len(text) >= 2 && text[1] == '`' {
nextChar := byte(0)
if len(text) >= 3 {
nextChar = text[2]
}
if smartQuotesHelper(ob, previousChar, nextChar, 'd', &smrt.inDoubleQuote) {
if smartQuotesHelper(out, previousChar, nextChar, 'd', &smrt.inDoubleQuote) {
return 1
}
}
@ -252,7 +253,7 @@ func smartBacktick(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, t
return 0
}
func smartNumberGeneric(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
func smartNumberGeneric(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
if wordBoundary(previousChar) && len(text) >= 3 {
// is it of the form digits/digits(word boundary)?, i.e., \d+/\d+\b
numEnd := 0
@ -260,11 +261,11 @@ func smartNumberGeneric(ob *bytes.Buffer, smrt *smartypantsData, previousChar by
numEnd++
}
if numEnd == 0 {
ob.WriteByte(text[0])
out.WriteByte(text[0])
return 0
}
if len(text) < numEnd+2 || text[numEnd] != '/' {
ob.WriteByte(text[0])
out.WriteByte(text[0])
return 0
}
denEnd := numEnd + 1
@ -272,75 +273,75 @@ func smartNumberGeneric(ob *bytes.Buffer, smrt *smartypantsData, previousChar by
denEnd++
}
if denEnd == numEnd+1 {
ob.WriteByte(text[0])
out.WriteByte(text[0])
return 0
}
if len(text) == denEnd || wordBoundary(text[denEnd]) {
ob.WriteString("<sup>")
ob.Write(text[:numEnd])
ob.WriteString("</sup>&frasl;<sub>")
ob.Write(text[numEnd+1 : denEnd])
ob.WriteString("</sub>")
out.WriteString("<sup>")
out.Write(text[:numEnd])
out.WriteString("</sup>&frasl;<sub>")
out.Write(text[numEnd+1 : denEnd])
out.WriteString("</sub>")
return denEnd - 1
}
}
ob.WriteByte(text[0])
out.WriteByte(text[0])
return 0
}
func smartNumber(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
func smartNumber(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
if wordBoundary(previousChar) && len(text) >= 3 {
if text[0] == '1' && text[1] == '/' && text[2] == '2' {
if len(text) < 4 || wordBoundary(text[3]) {
ob.WriteString("&frac12;")
out.WriteString("&frac12;")
return 2
}
}
if text[0] == '1' && text[1] == '/' && text[2] == '4' {
if len(text) < 4 || wordBoundary(text[3]) || (len(text) >= 5 && tolower(text[3]) == 't' && tolower(text[4]) == 'h') {
ob.WriteString("&frac14;")
out.WriteString("&frac14;")
return 2
}
}
if text[0] == '3' && text[1] == '/' && text[2] == '4' {
if len(text) < 4 || wordBoundary(text[3]) || (len(text) >= 6 && tolower(text[3]) == 't' && tolower(text[4]) == 'h' && tolower(text[5]) == 's') {
ob.WriteString("&frac34;")
out.WriteString("&frac34;")
return 2
}
}
}
ob.WriteByte(text[0])
out.WriteByte(text[0])
return 0
}
func smartDquote(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
func smartDquote(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
nextChar := byte(0)
if len(text) > 1 {
nextChar = text[1]
}
if !smartQuotesHelper(ob, previousChar, nextChar, 'd', &smrt.inDoubleQuote) {
ob.WriteString("&quot;")
if !smartQuotesHelper(out, previousChar, nextChar, 'd', &smrt.inDoubleQuote) {
out.WriteString("&quot;")
}
return 0
}
func smartLtag(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
func smartLtag(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
i := 0
for i < len(text) && text[i] != '>' {
i++
}
ob.Write(text[:i+1])
out.Write(text[:i+1])
return i
}
type smartCallback func(ob *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
@ -369,7 +370,7 @@ func Smartypants(flags int) *SmartypantsRenderer {
return r
}
func htmlSmartypants(ob *bytes.Buffer, text []byte, opaque interface{}) {
func htmlSmartypants(out *bytes.Buffer, text []byte, opaque interface{}) {
options := opaque.(*htmlOptions)
smrt := smartypantsData{false, false}
@ -382,19 +383,19 @@ func htmlSmartypants(ob *bytes.Buffer, text []byte, opaque interface{}) {
for i := 0; i < len(text); i++ {
if action := options.smartypants[text[i]]; action != nil {
if i > mark {
ob.Write(text[mark:i])
out.Write(text[mark:i])
}
previousChar := byte(0)
if i > 0 {
previousChar = text[i-1]
}
i += action(ob, &smrt, previousChar, text[i:])
i += action(out, &smrt, previousChar, text[i:])
mark = i + 1
}
}
if mark < len(text) {
ob.Write(text[mark:])
out.Write(text[mark:])
}
}