Skip to content

Add an option 'G' to use German quotes #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: lib
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 38 additions & 18 deletions Michelf/SmartyPants.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SmartyPants {

### Version ###

const SMARTYPANTSLIB_VERSION = "1.6.0-beta1";
const SMARTYPANTSLIB_VERSION = "1.6.0-beta2";


### Standard Function Interface ###
Expand Down Expand Up @@ -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 ###

Expand All @@ -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;
Expand Down Expand Up @@ -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.
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.:
# <p>He said, "'Quoted' words in a larger quote."</p>
$_ = preg_replace(
array("/\"'(?=\w)/", "/'\"(?=\w)/"),
array('&#8220;&#8216;', '&#8216;&#8220;'), $_);
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)/", '&#8217;', $_);
$_ = preg_replace("/'(?=\\d{2}s)/", $this->single_quote_close, $_);

$close_class = '[^\ \t\r\n\[\{\(\-]';
$dec_dashes = '&\#8211;|&\#8212;';
Expand All @@ -279,7 +292,7 @@ protected function educateQuotes($_) {
)
' # the quote
(?=\\w) # followed by a word character
}x", '\1&#8216;', $_);
}x", '\1'.$this->single_quote_open, $_);
# Single closing quotes:
$_ = preg_replace("{
($close_class)?
Expand All @@ -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:
# \"<i>Custer</i>'s Last Stand.\"
}xi", '\1&#8217;', $_);
}xi", '\1'.$this->single_quote_close, $_);

# Any remaining single quotes should be opening ones:
$_ = str_replace("'", '&#8216;', $_);
$_ = str_replace("'", $this->single_quote_open, $_);


# Get most opening double quotes:
Expand All @@ -307,18 +320,18 @@ protected function educateQuotes($_) {
)
\" # the quote
(?=\\w) # followed by a word character
}x", '\1&#8220;', $_);
}x", '\1'.$this->double_quote_open, $_);

# Double closing quotes:
$_ = preg_replace("{
($close_class)?
\"
(?(1)|(?=\\s)) # If $1 captured, then do nothing;
# if not, then make sure the next char is whitespace.
}x", '\1&#8221;', $_);
}x", '\1'.$this->double_quote_close, $_);

# Any remaining quotes should be opening ones.
$_ = str_replace('"', '&#8220;', $_);
$_ = str_replace('"', $this->double_quote_open, $_);

return $_;
}
Expand All @@ -335,7 +348,7 @@ protected function educateBackticks($_) {
#

$_ = str_replace(array("``", "''",),
array('&#8220;', '&#8221;'), $_);
array($this->double_quote_open, $this->double_quote_close), $_);
return $_;
}

Expand All @@ -351,7 +364,7 @@ protected function educateSingleBackticks($_) {
#

$_ = str_replace(array("`", "'",),
array('&#8216;', '&#8217;'), $_);
array($this->single_quote_open, $this->single_quote_close), $_);
return $_;
}

Expand Down Expand Up @@ -439,10 +452,10 @@ protected function stupefyEntities($_) {
array('-', '--'), $_);

# single quote open close
$_ = str_replace(array('&#8216;', '&#8217;'), "'", $_);
$_ = str_replace(array($this->single_quote_open, $this->single_quote_close), "'", $_);

# double quote open close
$_ = str_replace(array('&#8220;', '&#8221;'), '"', $_);
$_ = str_replace(array($this->double_quote_open, $this->double_quote_close), '"', $_);

$_ = str_replace('&#8230;', '...', $_); # ellipsis

Expand Down Expand Up @@ -582,6 +595,7 @@ public function __construct($attr = SMARTYPANTS_ATTR_DEFAULT) {
# i -> inverted old school dashes
# e -> ellipses
# w -> convert &quot; entities to " for Dreamweaver users
# G -> use German quotes („like this“)
#
# Spacing:
# : -> colon spacing +-
Expand Down Expand Up @@ -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 = '&#8222;';
$this->smart_doublequote_close = '&#8221;';
$this->smart_singlequote_open = '&#8218;';
$this->smart_singlequote_close = '&#8217;';
}
else if ($c == "+") {
$current = 2;
unset($current);
Expand Down
3 changes: 3 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ###
Expand Down