recognize fraction slash as well as regular slash to make fractions

This commit is contained in:
Russ Ross 2012-03-11 16:10:42 -06:00
parent 82ba58501c
commit b3c6494605

View File

@ -256,6 +256,7 @@ func smartBacktick(out *bytes.Buffer, smrt *smartypantsData, previousChar byte,
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
// note: check for regular slash (/) or fraction slash (, 0x2044, or 0xe2 81 84 in utf-8)
numEnd := 0
for len(text) > numEnd && isdigit(text[numEnd]) {
numEnd++
@ -264,15 +265,18 @@ func smartNumberGeneric(out *bytes.Buffer, smrt *smartypantsData, previousChar b
out.WriteByte(text[0])
return 0
}
if len(text) < numEnd+2 || text[numEnd] != '/' {
denStart := numEnd + 1
if len(text) > numEnd+3 && text[numEnd] == 0xe2 && text[numEnd+1] == 0x81 && text[numEnd+2] == 0x84 {
denStart = numEnd + 3
} else if len(text) < numEnd+2 || text[numEnd] != '/' {
out.WriteByte(text[0])
return 0
}
denEnd := numEnd + 1
denEnd := denStart
for len(text) > denEnd && isdigit(text[denEnd]) {
denEnd++
}
if denEnd == numEnd+1 {
if denEnd == denStart {
out.WriteByte(text[0])
return 0
}
@ -280,7 +284,7 @@ func smartNumberGeneric(out *bytes.Buffer, smrt *smartypantsData, previousChar b
out.WriteString("<sup>")
out.Write(text[:numEnd])
out.WriteString("</sup>&frasl;<sub>")
out.Write(text[numEnd+1 : denEnd])
out.Write(text[denStart:denEnd])
out.WriteString("</sub>")
return denEnd - 1
}