all repos — grayfriday @ 7869a127bd88fb57ef044b29b04643c50c03ff9c

blackfriday fork with a few changes

Combine two Smartypants structs into one

Combine smartypantsRenderer and smartypantsData into one struct. Make
action funcs methods on that struct.
Vytautas Šaltenis vytas@rtfb.lt
Thu, 31 Mar 2016 21:40:37 +0300
commit

7869a127bd88fb57ef044b29b04643c50c03ff9c

parent

4a7ff562a7e3c58311b4678169a0ae9b0ecc2e59

2 files changed, 44 insertions(+), 71 deletions(-)

jump to
M html.gohtml.go

@@ -109,7 +109,7 @@

// Track header IDs to prevent ID collision in a single generation. headerIDs map[string]int - smartypants *smartypantsRenderer + smartypants *SPRenderer w HtmlWriter lastOutputLen int disableTags int

@@ -183,7 +183,7 @@ toc: new(bytes.Buffer),

headerIDs: make(map[string]int), - smartypants: smartypants(flags), + smartypants: NewSmartypantsRenderer(flags), w: writer, } }

@@ -696,13 +696,12 @@ }

} func (r *HTML) Smartypants2(text []byte) []byte { - smrt := smartypantsData{false, false} var buff bytes.Buffer // first do normal entity escaping text = attrEscape2(text) mark := 0 for i := 0; i < len(text); i++ { - if action := r.smartypants[text[i]]; action != nil { + if action := r.smartypants.callbacks[text[i]]; action != nil { if i > mark { buff.Write(text[mark:i]) }

@@ -711,7 +710,7 @@ if i > 0 {

previousChar = text[i-1] } var tmp bytes.Buffer - i += action(&tmp, &smrt, previousChar, text[i:]) + i += action(&tmp, previousChar, text[i:]) buff.Write(tmp.Bytes()) mark = i + 1 }

@@ -723,32 +722,7 @@ return buff.Bytes()

} func (r *HTML) Smartypants(text []byte) { - smrt := smartypantsData{false, false} - - // first do normal entity escaping - r.attrEscape(text) - - mark := 0 - for i := 0; i < len(text); i++ { - if action := r.smartypants[text[i]]; action != nil { - if i > mark { - r.w.Write(text[mark:i]) - } - - previousChar := byte(0) - if i > 0 { - previousChar = text[i-1] - } - var tmp bytes.Buffer - i += action(&tmp, &smrt, previousChar, text[i:]) - r.w.Write(tmp.Bytes()) - mark = i + 1 - } - } - - if mark < len(text) { - r.w.Write(text[mark:]) - } + r.w.Write(r.Smartypants2(text)) } func (r *HTML) DocumentHeader() {
M smartypants.gosmartypants.go

@@ -19,9 +19,10 @@ import (

"bytes" ) -type smartypantsData struct { +type SPRenderer struct { inSingleQuote bool inDoubleQuote bool + callbacks [256]smartCallback } func wordBoundary(c byte) bool {

@@ -107,7 +108,7 @@ out.WriteString("quo;")

return true } -func smartSingleQuote(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int { +func (smrt *SPRenderer) smartSingleQuote(out *bytes.Buffer, previousChar byte, text []byte) int { if len(text) >= 2 { t1 := tolower(text[1])

@@ -149,7 +150,7 @@ out.WriteByte(text[0])

return 0 } -func smartParens(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int { +func (smrt *SPRenderer) smartParens(out *bytes.Buffer, previousChar byte, text []byte) int { if len(text) >= 3 { t1 := tolower(text[1]) t2 := tolower(text[2])

@@ -174,7 +175,7 @@ out.WriteByte(text[0])

return 0 } -func smartDash(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int { +func (smrt *SPRenderer) smartDash(out *bytes.Buffer, previousChar byte, text []byte) int { if len(text) >= 2 { if text[1] == '-' { out.WriteString("&mdash;")

@@ -191,7 +192,7 @@ out.WriteByte(text[0])

return 0 } -func smartDashLatex(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int { +func (smrt *SPRenderer) smartDashLatex(out *bytes.Buffer, previousChar byte, text []byte) int { if len(text) >= 3 && text[1] == '-' && text[2] == '-' { out.WriteString("&mdash;") return 2

@@ -205,7 +206,7 @@ out.WriteByte(text[0])

return 0 } -func smartAmpVariant(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte, quote byte) int { +func (smrt *SPRenderer) smartAmpVariant(out *bytes.Buffer, previousChar byte, text []byte, quote byte) int { if bytes.HasPrefix(text, []byte("&quot;")) { nextChar := byte(0) if len(text) >= 7 {

@@ -224,15 +225,15 @@ out.WriteByte('&')

return 0 } -func smartAmp(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int { - return smartAmpVariant(out, smrt, previousChar, text, 'd') +func (smrt *SPRenderer) smartAmp(out *bytes.Buffer, previousChar byte, text []byte) int { + return smrt.smartAmpVariant(out, previousChar, text, 'd') } -func smartAmpAngledQuote(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int { - return smartAmpVariant(out, smrt, previousChar, text, 'a') +func (smrt *SPRenderer) smartAmpAngledQuote(out *bytes.Buffer, previousChar byte, text []byte) int { + return smrt.smartAmpVariant(out, previousChar, text, 'a') } -func smartPeriod(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int { +func (smrt *SPRenderer) smartPeriod(out *bytes.Buffer, previousChar byte, text []byte) int { if len(text) >= 3 && text[1] == '.' && text[2] == '.' { out.WriteString("&hellip;") return 2

@@ -247,7 +248,7 @@ out.WriteByte(text[0])

return 0 } -func smartBacktick(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int { +func (smrt *SPRenderer) smartBacktick(out *bytes.Buffer, previousChar byte, text []byte) int { if len(text) >= 2 && text[1] == '`' { nextChar := byte(0) if len(text) >= 3 {

@@ -262,7 +263,7 @@ out.WriteByte(text[0])

return 0 } -func smartNumberGeneric(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int { +func (smrt *SPRenderer) smartNumberGeneric(out *bytes.Buffer, previousChar byte, text []byte) int { if wordBoundary(previousChar) && previousChar != '/' && len(text) >= 3 { // is it of the form digits/digits(word boundary)?, i.e., \d+/\d+\b // note: check for regular slash (/) or fraction slash (⁄, 0x2044, or 0xe2 81 84 in utf-8)

@@ -304,7 +305,7 @@ out.WriteByte(text[0])

return 0 } -func smartNumber(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int { +func (smrt *SPRenderer) smartNumber(out *bytes.Buffer, previousChar byte, text []byte) int { if wordBoundary(previousChar) && previousChar != '/' && len(text) >= 3 { if text[0] == '1' && text[1] == '/' && text[2] == '2' { if len(text) < 4 || wordBoundary(text[3]) && text[3] != '/' {

@@ -332,7 +333,7 @@ out.WriteByte(text[0])

return 0 } -func smartDoubleQuoteVariant(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte, quote byte) int { +func (smrt *SPRenderer) smartDoubleQuoteVariant(out *bytes.Buffer, previousChar byte, text []byte, quote byte) int { nextChar := byte(0) if len(text) > 1 { nextChar = text[1]

@@ -344,15 +345,15 @@

return 0 } -func smartDoubleQuote(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int { - return smartDoubleQuoteVariant(out, smrt, previousChar, text, 'd') +func (smrt *SPRenderer) smartDoubleQuote(out *bytes.Buffer, previousChar byte, text []byte) int { + return smrt.smartDoubleQuoteVariant(out, previousChar, text, 'd') } -func smartAngledDoubleQuote(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int { - return smartDoubleQuoteVariant(out, smrt, previousChar, text, 'a') +func (smrt *SPRenderer) smartAngledDoubleQuote(out *bytes.Buffer, previousChar byte, text []byte) int { + return smrt.smartDoubleQuoteVariant(out, previousChar, text, 'a') } -func smartLeftAngle(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int { +func (smrt *SPRenderer) smartLeftAngle(out *bytes.Buffer, previousChar byte, text []byte) int { i := 0 for i < len(text) && text[i] != '>' {

@@ -363,38 +364,36 @@ out.Write(text[:i+1])

return i } -type smartCallback func(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int +type smartCallback func(out *bytes.Buffer, previousChar byte, text []byte) int -type smartypantsRenderer [256]smartCallback - -func smartypants(flags HtmlFlags) *smartypantsRenderer { - r := new(smartypantsRenderer) +func NewSmartypantsRenderer(flags HtmlFlags) *SPRenderer { + var r SPRenderer if flags&SmartypantsAngledQuotes == 0 { - r['"'] = smartDoubleQuote - r['&'] = smartAmp + r.callbacks['"'] = r.smartDoubleQuote + r.callbacks['&'] = r.smartAmp } else { - r['"'] = smartAngledDoubleQuote - r['&'] = smartAmpAngledQuote + r.callbacks['"'] = r.smartAngledDoubleQuote + r.callbacks['&'] = r.smartAmpAngledQuote } - r['\''] = smartSingleQuote - r['('] = smartParens + r.callbacks['\''] = r.smartSingleQuote + r.callbacks['('] = r.smartParens if flags&SmartypantsDashes != 0 { if flags&SmartypantsLatexDashes == 0 { - r['-'] = smartDash + r.callbacks['-'] = r.smartDash } else { - r['-'] = smartDashLatex + r.callbacks['-'] = r.smartDashLatex } } - r['.'] = smartPeriod + r.callbacks['.'] = r.smartPeriod if flags&SmartypantsFractions == 0 { - r['1'] = smartNumber - r['3'] = smartNumber + r.callbacks['1'] = r.smartNumber + r.callbacks['3'] = r.smartNumber } else { for ch := '1'; ch <= '9'; ch++ { - r[ch] = smartNumberGeneric + r.callbacks[ch] = r.smartNumberGeneric } } - r['<'] = smartLeftAngle - r['`'] = smartBacktick - return r + r.callbacks['<'] = r.smartLeftAngle + r.callbacks['`'] = r.smartBacktick + return &r }