summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/russross/blackfriday/smartypants.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/russross/blackfriday/smartypants.go')
-rw-r--r--vendor/github.com/russross/blackfriday/smartypants.go139
1 files changed, 83 insertions, 56 deletions
diff --git a/vendor/github.com/russross/blackfriday/smartypants.go b/vendor/github.com/russross/blackfriday/smartypants.go
index f25bd07d..3a220e94 100644
--- a/vendor/github.com/russross/blackfriday/smartypants.go
+++ b/vendor/github.com/russross/blackfriday/smartypants.go
@@ -17,11 +17,14 @@ package blackfriday
import (
"bytes"
+ "io"
)
-type smartypantsData struct {
+// SPRenderer is a struct containing state of a Smartypants renderer.
+type SPRenderer struct {
inSingleQuote bool
inDoubleQuote bool
+ callbacks [256]smartCallback
}
func wordBoundary(c byte) bool {
@@ -118,7 +121,7 @@ func smartQuoteHelper(out *bytes.Buffer, previousChar byte, nextChar byte, quote
return true
}
-func smartSingleQuote(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
+func (r *SPRenderer) smartSingleQuote(out *bytes.Buffer, previousChar byte, text []byte) int {
if len(text) >= 2 {
t1 := tolower(text[1])
@@ -127,7 +130,7 @@ func smartSingleQuote(out *bytes.Buffer, smrt *smartypantsData, previousChar byt
if len(text) >= 3 {
nextChar = text[2]
}
- if smartQuoteHelper(out, previousChar, nextChar, 'd', &smrt.inDoubleQuote, false) {
+ if smartQuoteHelper(out, previousChar, nextChar, 'd', &r.inDoubleQuote, false) {
return 1
}
}
@@ -152,7 +155,7 @@ func smartSingleQuote(out *bytes.Buffer, smrt *smartypantsData, previousChar byt
if len(text) > 1 {
nextChar = text[1]
}
- if smartQuoteHelper(out, previousChar, nextChar, 's', &smrt.inSingleQuote, false) {
+ if smartQuoteHelper(out, previousChar, nextChar, 's', &r.inSingleQuote, false) {
return 0
}
@@ -160,7 +163,7 @@ func smartSingleQuote(out *bytes.Buffer, smrt *smartypantsData, previousChar byt
return 0
}
-func smartParens(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
+func (r *SPRenderer) smartParens(out *bytes.Buffer, previousChar byte, text []byte) int {
if len(text) >= 3 {
t1 := tolower(text[1])
t2 := tolower(text[2])
@@ -185,7 +188,7 @@ func smartParens(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, te
return 0
}
-func smartDash(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
+func (r *SPRenderer) smartDash(out *bytes.Buffer, previousChar byte, text []byte) int {
if len(text) >= 2 {
if text[1] == '-' {
out.WriteString("—")
@@ -202,7 +205,7 @@ func smartDash(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text
return 0
}
-func smartDashLatex(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
+func (r *SPRenderer) smartDashLatex(out *bytes.Buffer, previousChar byte, text []byte) int {
if len(text) >= 3 && text[1] == '-' && text[2] == '-' {
out.WriteString("—")
return 2
@@ -216,13 +219,13 @@ func smartDashLatex(out *bytes.Buffer, smrt *smartypantsData, previousChar byte,
return 0
}
-func smartAmpVariant(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte, quote byte, addNBSP bool) int {
+func (r *SPRenderer) smartAmpVariant(out *bytes.Buffer, previousChar byte, text []byte, quote byte, addNBSP bool) int {
if bytes.HasPrefix(text, []byte(""")) {
nextChar := byte(0)
if len(text) >= 7 {
nextChar = text[6]
}
- if smartQuoteHelper(out, previousChar, nextChar, quote, &smrt.inDoubleQuote, addNBSP) {
+ if smartQuoteHelper(out, previousChar, nextChar, quote, &r.inDoubleQuote, addNBSP) {
return 5
}
}
@@ -235,18 +238,18 @@ func smartAmpVariant(out *bytes.Buffer, smrt *smartypantsData, previousChar byte
return 0
}
-func smartAmp(angledQuotes, addNBSP bool) func(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
+func (r *SPRenderer) smartAmp(angledQuotes, addNBSP bool) func(*bytes.Buffer, byte, []byte) int {
var quote byte = 'd'
if angledQuotes {
quote = 'a'
}
- return func(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
- return smartAmpVariant(out, smrt, previousChar, text, quote, addNBSP)
+ return func(out *bytes.Buffer, previousChar byte, text []byte) int {
+ return r.smartAmpVariant(out, previousChar, text, quote, addNBSP)
}
}
-func smartPeriod(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
+func (r *SPRenderer) smartPeriod(out *bytes.Buffer, previousChar byte, text []byte) int {
if len(text) >= 3 && text[1] == '.' && text[2] == '.' {
out.WriteString("…")
return 2
@@ -261,13 +264,13 @@ func smartPeriod(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, te
return 0
}
-func smartBacktick(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
+func (r *SPRenderer) smartBacktick(out *bytes.Buffer, previousChar byte, text []byte) int {
if len(text) >= 2 && text[1] == '`' {
nextChar := byte(0)
if len(text) >= 3 {
nextChar = text[2]
}
- if smartQuoteHelper(out, previousChar, nextChar, 'd', &smrt.inDoubleQuote, false) {
+ if smartQuoteHelper(out, previousChar, nextChar, 'd', &r.inDoubleQuote, false) {
return 1
}
}
@@ -276,7 +279,7 @@ func smartBacktick(out *bytes.Buffer, smrt *smartypantsData, previousChar byte,
return 0
}
-func smartNumberGeneric(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
+func (r *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)
@@ -318,7 +321,7 @@ func smartNumberGeneric(out *bytes.Buffer, smrt *smartypantsData, previousChar b
return 0
}
-func smartNumber(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
+func (r *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] != '/' {
@@ -346,27 +349,27 @@ func smartNumber(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, te
return 0
}
-func smartDoubleQuoteVariant(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte, quote byte) int {
+func (r *SPRenderer) smartDoubleQuoteVariant(out *bytes.Buffer, previousChar byte, text []byte, quote byte) int {
nextChar := byte(0)
if len(text) > 1 {
nextChar = text[1]
}
- if !smartQuoteHelper(out, previousChar, nextChar, quote, &smrt.inDoubleQuote, false) {
+ if !smartQuoteHelper(out, previousChar, nextChar, quote, &r.inDoubleQuote, false) {
out.WriteString("&quot;")
}
return 0
}
-func smartDoubleQuote(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
- return smartDoubleQuoteVariant(out, smrt, previousChar, text, 'd')
+func (r *SPRenderer) smartDoubleQuote(out *bytes.Buffer, previousChar byte, text []byte) int {
+ return r.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 (r *SPRenderer) smartAngledDoubleQuote(out *bytes.Buffer, previousChar byte, text []byte) int {
+ return r.smartDoubleQuoteVariant(out, previousChar, text, 'a')
}
-func smartLeftAngle(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
+func (r *SPRenderer) smartLeftAngle(out *bytes.Buffer, previousChar byte, text []byte) int {
i := 0
for i < len(text) && text[i] != '>' {
@@ -377,54 +380,78 @@ func smartLeftAngle(out *bytes.Buffer, smrt *smartypantsData, previousChar byte,
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
+// NewSmartypantsRenderer constructs a Smartypants renderer object.
+func NewSmartypantsRenderer(flags HTMLFlags) *SPRenderer {
+ var (
+ r SPRenderer
-var (
- smartAmpAngled = smartAmp(true, false)
- smartAmpAngledNBSP = smartAmp(true, true)
- smartAmpRegular = smartAmp(false, false)
- smartAmpRegularNBSP = smartAmp(false, true)
-)
+ smartAmpAngled = r.smartAmp(true, false)
+ smartAmpAngledNBSP = r.smartAmp(true, true)
+ smartAmpRegular = r.smartAmp(false, false)
+ smartAmpRegularNBSP = r.smartAmp(false, true)
+
+ addNBSP = flags&SmartypantsQuotesNBSP != 0
+ )
-func smartypants(flags int) *smartypantsRenderer {
- r := new(smartypantsRenderer)
- addNBSP := flags&HTML_SMARTYPANTS_QUOTES_NBSP != 0
- if flags&HTML_SMARTYPANTS_ANGLED_QUOTES == 0 {
- r['"'] = smartDoubleQuote
+ if flags&SmartypantsAngledQuotes == 0 {
+ r.callbacks['"'] = r.smartDoubleQuote
if !addNBSP {
- r['&'] = smartAmpRegular
+ r.callbacks['&'] = smartAmpRegular
} else {
- r['&'] = smartAmpRegularNBSP
+ r.callbacks['&'] = smartAmpRegularNBSP
}
} else {
- r['"'] = smartAngledDoubleQuote
+ r.callbacks['"'] = r.smartAngledDoubleQuote
if !addNBSP {
- r['&'] = smartAmpAngled
+ r.callbacks['&'] = smartAmpAngled
} else {
- r['&'] = smartAmpAngledNBSP
+ r.callbacks['&'] = smartAmpAngledNBSP
}
}
- r['\''] = smartSingleQuote
- r['('] = smartParens
- if flags&HTML_SMARTYPANTS_DASHES != 0 {
- if flags&HTML_SMARTYPANTS_LATEX_DASHES == 0 {
- r['-'] = smartDash
+ r.callbacks['\''] = r.smartSingleQuote
+ r.callbacks['('] = r.smartParens
+ if flags&SmartypantsDashes != 0 {
+ if flags&SmartypantsLatexDashes == 0 {
+ r.callbacks['-'] = r.smartDash
} else {
- r['-'] = smartDashLatex
+ r.callbacks['-'] = r.smartDashLatex
}
}
- r['.'] = smartPeriod
- if flags&HTML_SMARTYPANTS_FRACTIONS == 0 {
- r['1'] = smartNumber
- r['3'] = smartNumber
+ r.callbacks['.'] = r.smartPeriod
+ if flags&SmartypantsFractions == 0 {
+ 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
+}
+
+// Process is the entry point of the Smartypants renderer.
+func (r *SPRenderer) Process(w io.Writer, text []byte) {
+ mark := 0
+ for i := 0; i < len(text); i++ {
+ if action := r.callbacks[text[i]]; action != nil {
+ if i > mark {
+ w.Write(text[mark:i])
+ }
+ previousChar := byte(0)
+ if i > 0 {
+ previousChar = text[i-1]
+ }
+ var tmp bytes.Buffer
+ i += action(&tmp, previousChar, text[i:])
+ w.Write(tmp.Bytes())
+ mark = i + 1
+ }
+ }
+ if mark < len(text) {
+ w.Write(text[mark:])
+ }
}