From 5eaacf1042f833b7ca0a71c988b47f467b10bcee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20E=C3=9Fer?= Date: Sun, 31 Jan 2016 21:05:31 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Add=20an=20option=20'G'=20to=20use=20German?= =?UTF-8?q?=20quotes=20(=E2=80=9Elike=20this=E2=80=9C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Michelf/SmartyPants.php | 56 ++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/Michelf/SmartyPants.php b/Michelf/SmartyPants.php index ae8933d..a7999e9 100644 --- a/Michelf/SmartyPants.php +++ b/Michelf/SmartyPants.php @@ -36,7 +36,7 @@ class SmartyPants { ### Version ### - const SMARTYPANTSLIB_VERSION = "1.6.0-beta1"; + const SMARTYPANTSLIB_VERSION = "1.6.0-beta2"; ### Standard Function Interface ### @@ -76,6 +76,11 @@ public static function defaultTransform($text, $attr = SMARTYPANTS_ATTR_DEFAULT) protected $do_stupefy = 0; protected $convert_quot = 0; # should we translate " entities into normal quotes? + protected $double_quote_open = '“'; + protected $double_quote_close = '”'; + protected $single_quote_open = '‘'; + protected $single_quote_close = '’'; + ### Parser Implementation ### @@ -97,6 +102,7 @@ public function __construct($attr = SMARTYPANTS_ATTR_DEFAULT) { # i : inverted old school dashes # e : ellipses # w : convert " entities to " for Dreamweaver users + # G : use German quotes („like this“) # if ($attr == "0") { $this->do_nothing = 1; @@ -137,6 +143,13 @@ public function __construct($attr = SMARTYPANTS_ATTR_DEFAULT) { else if ($c == "i") { $this->do_dashes = 3; } else if ($c == "e") { $this->do_ellipses = 1; } else if ($c == "w") { $this->convert_quot = 1; } + else if ($c == "G") { + // use German quotes + $this->double_quote_open = '„'; + $this->double_quote_close = '”'; + $this->single_quote_open = '‚'; + $this->single_quote_close = '’'; + } else { # Unknown attribute option, ignore. } @@ -208,19 +221,19 @@ protected function educate($t, $prev_token_last_char) { if ($t == "'") { # Special case: single-character ' token if (preg_match('/\S/', $prev_token_last_char)) { - $t = "’"; + $t = $this->single_quote_close; } else { - $t = "‘"; + $t = $this->single_quote_open; } } else if ($t == '"') { # Special case: single-character " token if (preg_match('/\S/', $prev_token_last_char)) { - $t = "”"; + $t = $this->double_quote_close; } else { - $t = "“"; + $t = $this->double_quote_open; } } else { @@ -252,17 +265,17 @@ protected function educateQuotes($_) { # followed by punctuation at a non-word-break. Close the quotes by brute force: $_ = preg_replace( array("/^'(?=$punct_class\\B)/", "/^\"(?=$punct_class\\B)/"), - array('’', '”'), $_); + array($this->single_quote_close, $this->double_quote_close), $_); # Special case for double sets of quotes, e.g.: #

He said, "'Quoted' words in a larger quote."

$_ = preg_replace( array("/\"'(?=\w)/", "/'\"(?=\w)/"), - array('“‘', '‘“'), $_); + array($this->double_quote_open.$this->single_quote_open, $this->single_quote_open.$this->double_quote_open), $_); # Special case for decade abbreviations (the '80s): - $_ = preg_replace("/'(?=\\d{2}s)/", '’', $_); + $_ = preg_replace("/'(?=\\d{2}s)/", $this->single_quote_close, $_); $close_class = '[^\ \t\r\n\[\{\(\-]'; $dec_dashes = '&\#8211;|&\#8212;'; @@ -279,7 +292,7 @@ protected function educateQuotes($_) { ) ' # the quote (?=\\w) # followed by a word character - }x", '\1‘', $_); + }x", '\1'.$this->single_quote_open, $_); # Single closing quotes: $_ = preg_replace("{ ($close_class)? @@ -289,10 +302,10 @@ protected function educateQuotes($_) { ) # char or an 's' at a word ending position. This # is a special case to handle something like: # \"Custer's Last Stand.\" - }xi", '\1’', $_); + }xi", '\1'.$this->single_quote_close, $_); # Any remaining single quotes should be opening ones: - $_ = str_replace("'", '‘', $_); + $_ = str_replace("'", $this->single_quote_open, $_); # Get most opening double quotes: @@ -307,7 +320,7 @@ protected function educateQuotes($_) { ) \" # the quote (?=\\w) # followed by a word character - }x", '\1“', $_); + }x", '\1'.$this->double_quote_open, $_); # Double closing quotes: $_ = preg_replace("{ @@ -315,10 +328,10 @@ protected function educateQuotes($_) { \" (?(1)|(?=\\s)) # If $1 captured, then do nothing; # if not, then make sure the next char is whitespace. - }x", '\1”', $_); + }x", '\1'.$this->double_quote_close, $_); # Any remaining quotes should be opening ones. - $_ = str_replace('"', '“', $_); + $_ = str_replace('"', $this->double_quote_open, $_); return $_; } @@ -335,7 +348,7 @@ protected function educateBackticks($_) { # $_ = str_replace(array("``", "''",), - array('“', '”'), $_); + array($this->double_quote_open, $this->double_quote_close), $_); return $_; } @@ -351,7 +364,7 @@ protected function educateSingleBackticks($_) { # $_ = str_replace(array("`", "'",), - array('‘', '’'), $_); + array($this->single_quote_open, $this->single_quote_close), $_); return $_; } @@ -439,10 +452,10 @@ protected function stupefyEntities($_) { array('-', '--'), $_); # single quote open close - $_ = str_replace(array('‘', '’'), "'", $_); + $_ = str_replace(array($this->single_quote_open, $this->single_quote_close), "'", $_); # double quote open close - $_ = str_replace(array('“', '”'), '"', $_); + $_ = str_replace(array($this->double_quote_open, $this->double_quote_close), '"', $_); $_ = str_replace('…', '...', $_); # ellipsis @@ -582,6 +595,7 @@ public function __construct($attr = SMARTYPANTS_ATTR_DEFAULT) { # i -> inverted old school dashes # e -> ellipses # w -> convert " entities to " for Dreamweaver users + # G -> use German quotes („like this“) # # Spacing: # : -> colon spacing +- @@ -629,6 +643,12 @@ public function __construct($attr = SMARTYPANTS_ATTR_DEFAULT) { else if ($c == "f") { $current =& $this->do_space_frenchquote; } else if ($c == "t") { $current =& $this->do_space_thousand; } else if ($c == "u") { $current =& $this->do_space_unit; } + else if ($c == "G") { + $this->smart_doublequote_open = '„'; + $this->smart_doublequote_close = '”'; + $this->smart_singlequote_open = '‚'; + $this->smart_singlequote_close = '’'; + } else if ($c == "+") { $current = 2; unset($current); From 15db5c5afca725f2e217b3d96ea9e0a0c3e3ce33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20E=C3=9Fer?= Date: Sun, 31 Jan 2016 21:41:38 +0100 Subject: [PATCH 2/2] Update Readme.md add 'G' option to Readme --- Readme.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Readme.md b/Readme.md index 3023b9a..9f4e3be 100644 --- a/Readme.md +++ b/Readme.md @@ -218,6 +218,9 @@ ellipses or backticks-style quotes: function: $my_html = SmartyPants::defaultTransform($my_html, "qDew"); + +"G" +: Use „German“ quotes instead of "English" quotes. ### Algorithmic Shortcomings ###