diff -ur smartypants.orig/smartypants.php smartypants.new/smartypants.php --- smartypants.orig/smartypants.php 2007-10-18 21:53:40.000000000 -0400 +++ smartypants.new/smartypants.php 2007-12-04 22:44:08.000000000 -0500 @@ -159,6 +158,7 @@ var $do_backticks = 0; var $do_dashes = 0; var $do_ellipses = 0; + var $do_fractions = 0; var $do_stupefy = 0; var $convert_quot = 0; # should we translate " entities into normal quotes? @@ -179,6 +179,7 @@ # D : old school dashes # i : inverted old school dashes # e : ellipses + # f : fractions # w : convert " entities to " for Dreamweaver users # if ($attr == "0") { @@ -190,6 +191,7 @@ $this->do_backticks = 1; $this->do_dashes = 1; $this->do_ellipses = 1; + $this->do_fractions = 1; } else if ($attr == "2") { # Do everything, turn all options on, use old school dash shorthand. @@ -197,6 +199,7 @@ $this->do_backticks = 1; $this->do_dashes = 2; $this->do_ellipses = 1; + $this->do_fractions = 1; } else if ($attr == "3") { # Do everything, turn all options on, use inverted old school dash shorthand. @@ -204,6 +207,7 @@ $this->do_backticks = 1; $this->do_dashes = 3; $this->do_ellipses = 1; + $this->do_fractions = 1; } else if ($attr == "-1") { # Special "stupefy" mode. @@ -219,6 +223,7 @@ else if ($c == "D") { $this->do_dashes = 2; } else if ($c == "i") { $this->do_dashes = 3; } else if ($c == "e") { $this->do_ellipses = 1; } + else if ($c == "f") { $this->do_fractions = 1; } else if ($c == "w") { $this->convert_quot = 1; } else { # Unknown attribute option, ignore. @@ -312,6 +317,8 @@ } } + if ($this->do_fractions) $t = $this->educateFractions($t); + if ($this->do_stupefy) $t = $this->stupefyEntities($t); return $t; @@ -507,6 +514,26 @@ } + function educateFractions($_) { + # + # Parameter: String. + # Returns: The string, with each instance of "i/j" translated to + # a fraction HTML entity (for values of i,j for which an) + # entity exists.) + # + # Example input: 1/2 cup extra-virgin olive oil + # Example output: ½ cup extra-virgin olive oil + + $fraction_entities = array('1/4' => '¼', '1/2' => '½', '3/4' => '¾'); + $_ = preg_replace("{ + \\b # a word boundary + (1/4|1/2|3/4) # a fraction that'll be receptive to some book learnin' + \\b + }xe", '$fraction_entities["\\1"]', $_); + return $_; + } + + function stupefyEntities($_) { # # Parameter: String. @@ -529,6 +556,9 @@ $_ = str_replace('…', '...', $_); # ellipsis + # fractions + $_ = str_replace(array('¼', '½', '¾'), array('1/4', '1/2', '3/4')); + return $_; }