Permalink
Browse files

recognize fraction slash as well as regular slash to make fractions

  • Loading branch information...
1 parent 82ba585 commit b3c64946051e90924054f0ec721092dbfbcda1f2 @russross committed Mar 11, 2012
Showing with 8 additions and 4 deletions.
  1. +8 −4 smartypants.go
View
@@ -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,23 +265,26 @@ 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
}
if len(text) == denEnd || wordBoundary(text[denEnd]) {
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
}

0 comments on commit b3c6494

Please sign in to comment.