View source
<?php
namespace Drupal\typogrify;
class UnicodeConversion {
public static function map($type = 'all') {
$map = [
'ligature' => [
'ffi' => 'ffi',
'ffl' => 'ffl',
'ff' => 'ff',
'fi' => 'fi',
'fl' => 'fl',
'ij' => 'ij',
'IJ' => 'IJ',
'st' => 'st',
'ss' => 'ß',
],
'punctuation' => [
'...' => '…',
'..' => '‥',
'. . .' => '…',
'---' => '—',
'--' => '–',
],
'fraction' => [
'1/4' => '¼',
'1/2' => '½',
'3/4' => '¾',
'1/3' => '⅓',
'2/3' => '⅔',
'1/5' => '⅕',
'2/5' => '⅖',
'3/5' => '⅗',
'4/5' => '⅘',
'1/6' => '⅙',
'5/6' => '⅚',
'1/8' => '⅛',
'3/8' => '⅜',
'5/8' => '⅝',
'7/8' => '⅞',
],
'quotes' => [
',,' => '„',
"''" => '”',
'<<' => '«',
'>>' => '»',
],
'arrow' => [
'->>' => '↠',
'<<-' => '↞',
'->|' => '⇥',
'|<-' => '⇤',
'<->' => '↔',
'->' => '→',
'<-' => '←',
'<=>' => '⇔',
'=>' => '⇒',
'<=' => '⇐',
],
];
if ($type == 'all') {
return array_merge($map['ligature'], $map['arrow'], $map['punctuation'], $map['quotes'], $map['fraction']);
}
elseif ($type == 'nested') {
return $map;
}
else {
return $map[$type];
}
}
public static function convertCharacters($text, array $characters_to_convert) {
if ($characters_to_convert == NULL || count($characters_to_convert) < 1) {
return $text;
}
$unicode_map = self::map();
foreach ($characters_to_convert as $ascii_string) {
$unicode_strings[] = $unicode_map[$ascii_string];
}
$tokens = SmartyPants::tokenizeHtml($text);
$result = '';
$in_pre = 0;
foreach ($tokens as $cur_token) {
if ($cur_token[0] == "tag") {
$result .= $cur_token[1];
if (preg_match(SmartyPants::SMARTYPANTS_TAGS_TO_SKIP, $cur_token[1], $matches)) {
$in_pre = isset($matches[1]) && $matches[1] == '/' ? 0 : 1;
}
}
else {
$t = $cur_token[1];
if ($in_pre == 0) {
$t = SmartyPants::processEscapes($t);
$t = str_replace($characters_to_convert, $unicode_strings, $t);
}
$result .= $t;
}
}
return $result;
}
}