diff --git a/inline_test.go b/inline_test.go index e95eb97..fb3bdec 100644 --- a/inline_test.go +++ b/inline_test.go @@ -839,3 +839,21 @@ func TestSmartAngledDoubleQuotes(t *testing.T) { doTestsInlineParam(t, tests, 0, HTML_USE_SMARTYPANTS|HTML_SMARTYPANTS_ANGLED_QUOTES, HtmlRendererParameters{}) } + +func TestSmartFractions(t *testing.T) { + var tests = []string{ + "1/2, 1/4 and 3/4; 1/4th and 3/4ths\n", + "

½, ¼ and ¾; ¼th and ¾ths

\n", + "1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.\n", + "

1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.

\n"} + + doTestsInlineParam(t, tests, 0, HTML_USE_SMARTYPANTS, HtmlRendererParameters{}) + + tests = []string{ + "1/2, 2/3, 81/100 and 1000000/1048576.\n", + "

12, 23, 81100 and 10000001048576.

\n", + "1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.\n", + "

1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.

\n"} + + doTestsInlineParam(t, tests, 0, HTML_USE_SMARTYPANTS|HTML_SMARTYPANTS_FRACTIONS, HtmlRendererParameters{}) +} diff --git a/smartypants.go b/smartypants.go index 38ffcf7..8027571 100644 --- a/smartypants.go +++ b/smartypants.go @@ -263,9 +263,10 @@ 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 { + 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) + // and avoid changing dates like 1/23/2005 into fractions. numEnd := 0 for len(text) > numEnd && isdigit(text[numEnd]) { numEnd++ @@ -289,7 +290,7 @@ func smartNumberGeneric(out *bytes.Buffer, smrt *smartypantsData, previousChar b out.WriteByte(text[0]) return 0 } - if len(text) == denEnd || wordBoundary(text[denEnd]) { + if len(text) == denEnd || wordBoundary(text[denEnd]) && text[denEnd] != '/' { out.WriteString("") out.Write(text[:numEnd]) out.WriteString("") @@ -304,23 +305,23 @@ func smartNumberGeneric(out *bytes.Buffer, smrt *smartypantsData, previousChar b } func smartNumber(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int { - if wordBoundary(previousChar) && len(text) >= 3 { + if wordBoundary(previousChar) && previousChar != '/' && len(text) >= 3 { if text[0] == '1' && text[1] == '/' && text[2] == '2' { - if len(text) < 4 || wordBoundary(text[3]) { + if len(text) < 4 || wordBoundary(text[3]) && text[3] != '/' { out.WriteString("½") return 2 } } if text[0] == '1' && text[1] == '/' && text[2] == '4' { - if len(text) < 4 || wordBoundary(text[3]) || (len(text) >= 5 && tolower(text[3]) == 't' && tolower(text[4]) == 'h') { + if len(text) < 4 || wordBoundary(text[3]) && text[3] != '/' || (len(text) >= 5 && tolower(text[3]) == 't' && tolower(text[4]) == 'h') { out.WriteString("¼") return 2 } } if text[0] == '3' && text[1] == '/' && text[2] == '4' { - if len(text) < 4 || wordBoundary(text[3]) || (len(text) >= 6 && tolower(text[3]) == 't' && tolower(text[4]) == 'h' && tolower(text[5]) == 's') { + if len(text) < 4 || wordBoundary(text[3]) && text[3] != '/' || (len(text) >= 6 && tolower(text[3]) == 't' && tolower(text[4]) == 'h' && tolower(text[5]) == 's') { out.WriteString("¾") return 2 }