2011-05-29 11:17:53 +08:00
|
|
|
//
|
|
|
|
// Black Friday Markdown Processor
|
|
|
|
// Originally based on http://github.com/tanoku/upskirt
|
|
|
|
// by Russ Ross <russ@russross.com>
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// SmartyPants rendering
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
package blackfriday
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
)
|
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
type smartypantsData struct {
|
|
|
|
inSingleQuote bool
|
|
|
|
inDoubleQuote bool
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
func wordBoundary(c byte) bool {
|
2011-05-29 11:17:53 +08:00
|
|
|
return c == 0 || isspace(c) || ispunct(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func tolower(c byte) byte {
|
|
|
|
if c >= 'A' && c <= 'Z' {
|
|
|
|
return c - 'A' + 'a'
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func isdigit(c byte) bool {
|
|
|
|
return c >= '0' && c <= '9'
|
|
|
|
}
|
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
func smartQuotesHelper(ob *bytes.Buffer, previousChar byte, nextChar byte, quote byte, isOpen *bool) bool {
|
2011-05-29 11:17:53 +08:00
|
|
|
switch {
|
|
|
|
// edge of the buffer is likely to be a tag that we don't get to see,
|
|
|
|
// so we assume there is text there
|
2011-05-30 07:00:31 +08:00
|
|
|
case wordBoundary(previousChar) && previousChar != 0 && nextChar == 0:
|
|
|
|
*isOpen = true
|
|
|
|
case previousChar == 0 && wordBoundary(nextChar) && nextChar != 0:
|
|
|
|
*isOpen = false
|
|
|
|
case wordBoundary(previousChar) && !wordBoundary(nextChar):
|
|
|
|
*isOpen = true
|
|
|
|
case !wordBoundary(previousChar) && wordBoundary(nextChar):
|
|
|
|
*isOpen = false
|
|
|
|
case !wordBoundary(previousChar) && !wordBoundary(nextChar):
|
|
|
|
*isOpen = true
|
2011-05-29 11:17:53 +08:00
|
|
|
default:
|
2011-05-30 07:00:31 +08:00
|
|
|
*isOpen = !*isOpen
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ob.WriteByte('&')
|
2011-05-30 07:00:31 +08:00
|
|
|
if *isOpen {
|
2011-05-29 11:17:53 +08:00
|
|
|
ob.WriteByte('l')
|
|
|
|
} else {
|
|
|
|
ob.WriteByte('r')
|
|
|
|
}
|
|
|
|
ob.WriteByte(quote)
|
|
|
|
ob.WriteString("quo;")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
func smartSquote(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
|
2011-05-29 11:17:53 +08:00
|
|
|
if len(text) >= 2 {
|
|
|
|
t1 := tolower(text[1])
|
|
|
|
|
|
|
|
if t1 == '\'' {
|
2011-05-30 07:00:31 +08:00
|
|
|
nextChar := byte(0)
|
2011-05-29 11:17:53 +08:00
|
|
|
if len(text) >= 3 {
|
2011-05-30 07:00:31 +08:00
|
|
|
nextChar = text[2]
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
2011-05-30 07:00:31 +08:00
|
|
|
if smartQuotesHelper(ob, previousChar, nextChar, 'd', &smrt.inDoubleQuote) {
|
2011-05-29 11:17:53 +08:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
if (t1 == 's' || t1 == 't' || t1 == 'm' || t1 == 'd') && (len(text) < 3 || wordBoundary(text[2])) {
|
2011-05-29 11:17:53 +08:00
|
|
|
ob.WriteString("’")
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(text) >= 3 {
|
|
|
|
t2 := tolower(text[2])
|
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
if ((t1 == 'r' && t2 == 'e') || (t1 == 'l' && t2 == 'l') || (t1 == 'v' && t2 == 'e')) && (len(text) < 4 || wordBoundary(text[3])) {
|
2011-05-29 11:17:53 +08:00
|
|
|
ob.WriteString("’")
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
nextChar := byte(0)
|
2011-05-29 11:17:53 +08:00
|
|
|
if len(text) > 1 {
|
2011-05-30 07:00:31 +08:00
|
|
|
nextChar = text[1]
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
2011-05-30 07:00:31 +08:00
|
|
|
if smartQuotesHelper(ob, previousChar, nextChar, 's', &smrt.inSingleQuote) {
|
2011-05-29 11:17:53 +08:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
ob.WriteByte(text[0])
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
func smartParens(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
|
2011-05-29 11:17:53 +08:00
|
|
|
if len(text) >= 3 {
|
|
|
|
t1 := tolower(text[1])
|
|
|
|
t2 := tolower(text[2])
|
|
|
|
|
|
|
|
if t1 == 'c' && t2 == ')' {
|
|
|
|
ob.WriteString("©")
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
|
|
|
if t1 == 'r' && t2 == ')' {
|
|
|
|
ob.WriteString("®")
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(text) >= 4 && t1 == 't' && t2 == 'm' && text[3] == ')' {
|
|
|
|
ob.WriteString("™")
|
|
|
|
return 3
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ob.WriteByte(text[0])
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
func smartDash(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
|
2011-05-29 11:17:53 +08:00
|
|
|
if len(text) >= 2 {
|
|
|
|
if text[1] == '-' {
|
|
|
|
ob.WriteString("—")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
if wordBoundary(previousChar) && wordBoundary(text[1]) {
|
2011-05-29 11:17:53 +08:00
|
|
|
ob.WriteString("–")
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ob.WriteByte(text[0])
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
func smartDashLatex(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
|
2011-05-29 11:17:53 +08:00
|
|
|
if len(text) >= 3 && text[1] == '-' && text[2] == '-' {
|
|
|
|
ob.WriteString("—")
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
if len(text) >= 2 && text[1] == '-' {
|
|
|
|
ob.WriteString("–")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
ob.WriteByte(text[0])
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
func smartAmp(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
|
2011-05-29 11:17:53 +08:00
|
|
|
if bytes.HasPrefix(text, []byte(""")) {
|
2011-05-30 07:00:31 +08:00
|
|
|
nextChar := byte(0)
|
2011-05-29 11:17:53 +08:00
|
|
|
if len(text) >= 7 {
|
2011-05-30 07:00:31 +08:00
|
|
|
nextChar = text[6]
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
2011-05-30 07:00:31 +08:00
|
|
|
if smartQuotesHelper(ob, previousChar, nextChar, 'd', &smrt.inDoubleQuote) {
|
2011-05-29 11:17:53 +08:00
|
|
|
return 5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if bytes.HasPrefix(text, []byte("�")) {
|
|
|
|
return 3
|
|
|
|
}
|
|
|
|
|
|
|
|
ob.WriteByte('&')
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
func smartPeriod(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
|
2011-05-29 11:17:53 +08:00
|
|
|
if len(text) >= 3 && text[1] == '.' && text[2] == '.' {
|
|
|
|
ob.WriteString("…")
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(text) >= 5 && text[1] == ' ' && text[2] == '.' && text[3] == ' ' && text[4] == '.' {
|
|
|
|
ob.WriteString("…")
|
|
|
|
return 4
|
|
|
|
}
|
|
|
|
|
|
|
|
ob.WriteByte(text[0])
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
func smartBacktick(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
|
2011-05-29 11:17:53 +08:00
|
|
|
if len(text) >= 2 && text[1] == '`' {
|
2011-05-30 07:00:31 +08:00
|
|
|
nextChar := byte(0)
|
2011-05-29 11:17:53 +08:00
|
|
|
if len(text) >= 3 {
|
2011-05-30 07:00:31 +08:00
|
|
|
nextChar = text[2]
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
2011-05-30 07:00:31 +08:00
|
|
|
if smartQuotesHelper(ob, previousChar, nextChar, 'd', &smrt.inDoubleQuote) {
|
2011-05-29 11:17:53 +08:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
func smartNumberGeneric(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
|
|
|
|
if wordBoundary(previousChar) && len(text) >= 3 {
|
2011-05-29 11:17:53 +08:00
|
|
|
// is it of the form digits/digits(word boundary)?, i.e., \d+/\d+\b
|
|
|
|
num_end := 0
|
|
|
|
for len(text) > num_end && isdigit(text[num_end]) {
|
|
|
|
num_end++
|
|
|
|
}
|
|
|
|
if num_end == 0 {
|
2011-05-29 12:50:33 +08:00
|
|
|
ob.WriteByte(text[0])
|
2011-05-29 11:17:53 +08:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if len(text) < num_end+2 || text[num_end] != '/' {
|
2011-05-29 12:50:33 +08:00
|
|
|
ob.WriteByte(text[0])
|
2011-05-29 11:17:53 +08:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
den_end := num_end + 1
|
|
|
|
for len(text) > den_end && isdigit(text[den_end]) {
|
|
|
|
den_end++
|
|
|
|
}
|
|
|
|
if den_end == num_end+1 {
|
2011-05-29 12:50:33 +08:00
|
|
|
ob.WriteByte(text[0])
|
2011-05-29 11:17:53 +08:00
|
|
|
return 0
|
|
|
|
}
|
2011-05-30 07:00:31 +08:00
|
|
|
if len(text) == den_end || wordBoundary(text[den_end]) {
|
2011-05-29 11:33:16 +08:00
|
|
|
ob.WriteString("<sup>")
|
2011-05-29 11:17:53 +08:00
|
|
|
ob.Write(text[:num_end])
|
2011-05-29 11:33:16 +08:00
|
|
|
ob.WriteString("</sup>⁄<sub>")
|
2011-05-29 11:17:53 +08:00
|
|
|
ob.Write(text[num_end+1 : den_end])
|
2011-05-29 11:33:16 +08:00
|
|
|
ob.WriteString("</sub>")
|
2011-05-29 11:17:53 +08:00
|
|
|
return den_end - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ob.WriteByte(text[0])
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
func smartNumber(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
|
|
|
|
if wordBoundary(previousChar) && len(text) >= 3 {
|
2011-05-29 11:17:53 +08:00
|
|
|
if text[0] == '1' && text[1] == '/' && text[2] == '2' {
|
2011-05-30 07:00:31 +08:00
|
|
|
if len(text) < 4 || wordBoundary(text[3]) {
|
2011-05-29 11:17:53 +08:00
|
|
|
ob.WriteString("½")
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if text[0] == '1' && text[1] == '/' && text[2] == '4' {
|
2011-05-30 07:00:31 +08:00
|
|
|
if len(text) < 4 || wordBoundary(text[3]) || (len(text) >= 5 && tolower(text[3]) == 't' && tolower(text[4]) == 'h') {
|
2011-05-29 11:17:53 +08:00
|
|
|
ob.WriteString("¼")
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if text[0] == '3' && text[1] == '/' && text[2] == '4' {
|
2011-05-30 07:00:31 +08:00
|
|
|
if len(text) < 4 || wordBoundary(text[3]) || (len(text) >= 6 && tolower(text[3]) == 't' && tolower(text[4]) == 'h' && tolower(text[5]) == 's') {
|
2011-05-29 11:17:53 +08:00
|
|
|
ob.WriteString("¾")
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ob.WriteByte(text[0])
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
func smartDquote(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
|
|
|
|
nextChar := byte(0)
|
2011-05-29 11:17:53 +08:00
|
|
|
if len(text) > 1 {
|
2011-05-30 07:00:31 +08:00
|
|
|
nextChar = text[1]
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
2011-05-30 07:00:31 +08:00
|
|
|
if !smartQuotesHelper(ob, previousChar, nextChar, 'd', &smrt.inDoubleQuote) {
|
2011-05-29 11:17:53 +08:00
|
|
|
ob.WriteString(""")
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
func smartLtag(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int {
|
2011-05-29 11:17:53 +08:00
|
|
|
i := 0
|
|
|
|
|
|
|
|
for i < len(text) && text[i] != '>' {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
ob.Write(text[:i+1])
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
type smartCallback func(ob *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int
|
2011-05-29 11:17:53 +08:00
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
type SmartypantsRenderer [256]smartCallback
|
2011-05-29 11:17:53 +08:00
|
|
|
|
|
|
|
func Smartypants(flags int) *SmartypantsRenderer {
|
|
|
|
r := new(SmartypantsRenderer)
|
2011-05-30 07:00:31 +08:00
|
|
|
r['"'] = smartDquote
|
|
|
|
r['&'] = smartAmp
|
|
|
|
r['\''] = smartSquote
|
|
|
|
r['('] = smartParens
|
2011-05-29 11:17:53 +08:00
|
|
|
if flags&HTML_SMARTYPANTS_LATEX_DASHES == 0 {
|
2011-05-30 07:00:31 +08:00
|
|
|
r['-'] = smartDash
|
2011-05-29 11:17:53 +08:00
|
|
|
} else {
|
2011-05-30 07:00:31 +08:00
|
|
|
r['-'] = smartDashLatex
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
2011-05-30 07:00:31 +08:00
|
|
|
r['.'] = smartPeriod
|
2011-05-29 11:17:53 +08:00
|
|
|
if flags&HTML_SMARTYPANTS_FRACTIONS == 0 {
|
2011-05-30 07:00:31 +08:00
|
|
|
r['1'] = smartNumber
|
|
|
|
r['3'] = smartNumber
|
2011-05-29 11:17:53 +08:00
|
|
|
} else {
|
|
|
|
for ch := '1'; ch <= '9'; ch++ {
|
2011-05-30 07:00:31 +08:00
|
|
|
r[ch] = smartNumberGeneric
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
}
|
2011-05-30 07:00:31 +08:00
|
|
|
r['<'] = smartLtag
|
|
|
|
r['`'] = smartBacktick
|
2011-05-29 11:17:53 +08:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
func htmlSmartypants(ob *bytes.Buffer, text []byte, opaque interface{}) {
|
2011-05-29 11:17:53 +08:00
|
|
|
options := opaque.(*htmlOptions)
|
2011-05-30 07:00:31 +08:00
|
|
|
smrt := smartypantsData{false, false}
|
2011-05-29 11:17:53 +08:00
|
|
|
|
2011-05-29 12:50:33 +08:00
|
|
|
// first do normal entity escaping
|
2011-06-01 01:11:04 +08:00
|
|
|
var escaped bytes.Buffer
|
|
|
|
attrEscape(&escaped, text)
|
2011-05-29 12:50:33 +08:00
|
|
|
text = escaped.Bytes()
|
|
|
|
|
2011-05-29 11:17:53 +08:00
|
|
|
mark := 0
|
|
|
|
for i := 0; i < len(text); i++ {
|
|
|
|
if action := options.smartypants[text[i]]; action != nil {
|
|
|
|
if i > mark {
|
2011-05-29 12:50:33 +08:00
|
|
|
ob.Write(text[mark:i])
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
|
2011-05-30 07:00:31 +08:00
|
|
|
previousChar := byte(0)
|
2011-05-29 11:17:53 +08:00
|
|
|
if i > 0 {
|
2011-05-30 07:00:31 +08:00
|
|
|
previousChar = text[i-1]
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
2011-05-30 07:00:31 +08:00
|
|
|
i += action(ob, &smrt, previousChar, text[i:])
|
2011-05-29 11:17:53 +08:00
|
|
|
mark = i + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if mark < len(text) {
|
2011-05-29 12:50:33 +08:00
|
|
|
ob.Write(text[mark:])
|
2011-05-29 11:17:53 +08:00
|
|
|
}
|
|
|
|
}
|