3 namespace Drupal\typogrify;
6 * Return the unicode conversion maps.
8 class UnicodeConversion {
11 * Provides Unicode-mapping.
14 * The map type we're looking for, one of 'ligature', 'punctuation',
15 * 'arrow' 'nested' or 'all'.
18 * Array of conversions, keyed by the original string.
20 public static function map($type = 'all') {
22 // See http://www.unicode.org/charts/PDF/UFB00.pdf .
34 // See http:#www.unicode.org/charts/PDF/U2000.pdf .
35 'punctuation' => array(
38 '. . .' => '…',
62 '<<' => '«',
63 '>>' => '»',
65 // See http:#www.unicode.org/charts/PDF/U2190.pdf .
67 '->>' => '↠',
68 '<<-' => '↞',
69 '->|' => '⇥',
70 '|<-' => '⇤',
71 '<->' => '↔',
72 '->' => '→',
73 '<-' => '←',
74 '<=>' => '⇔',
75 '=>' => '⇒',
76 '<=' => '⇐',
81 return array_merge($map['ligature'], $map['arrow'], $map['punctuation'], $map['quotes'], $map['fraction']);
83 elseif ($type == 'nested') {
92 * Perform character conversion.
96 * @param array $characters_to_convert
97 * Array of ASCII characters to convert.
100 * The result of the conversion.
102 public static function convertCharacters($text, $characters_to_convert) {
103 if (($characters_to_convert == NULL) || (count($characters_to_convert) < 1)) {
108 // Get ascii to unicode mappings.
109 $unicode_map = self::map();
111 foreach ($characters_to_convert as $ascii_string) {
112 $unicode_strings[] = $unicode_map[$ascii_string];
115 $tokens = SmartyPants::tokenizeHtml($text);
117 // Keep track of when we're inside <pre> or <code> tags.
119 foreach ($tokens as $cur_token) {
120 if ($cur_token[0] == "tag") {
121 // Don't mess with text inside tags, <pre> blocks, or <code> blocks.
122 $result .= $cur_token[1];
123 // Get the tags to skip regex from SmartyPants.
124 if (preg_match(SmartyPants::SMARTYPANTS_TAGS_TO_SKIP, $cur_token[1], $matches)) {
125 $in_pre = isset($matches[1]) && $matches[1] == '/' ? 0 : 1;
131 $t = SmartyPants::processEscapes($t);
132 $t = str_replace($characters_to_convert, $unicode_strings, $t);