Pathologic was missing because of a .git folder inside.
[yaffs-website] / web / modules / contrib / typogrify / src / UnicodeConversion.php
1 <?php
2
3 namespace Drupal\typogrify;
4
5 /**
6  * Return the unicode conversion maps.
7  */
8 class UnicodeConversion {
9
10   /**
11    * Provides Unicode-mapping.
12    *
13    * @param string $type
14    *   The map type we're looking for, one of 'ligature', 'punctuation',
15    *   'arrow' 'nested' or 'all'.
16    *
17    * @return array
18    *   Array of conversions, keyed by the original string.
19    */
20   public static function map($type = 'all') {
21     $map = array(
22       // See http://www.unicode.org/charts/PDF/UFB00.pdf .
23       'ligature' => array(
24         'ffi' => '&#xfb03;',
25         'ffl' => '&#xfb04;',
26         'ff'  => '&#xfb00;',
27         'fi'  => '&#xfb01;',
28         'fl'  => '&#xfb02;',
29         'ij'  => '&#x0133;',
30         'IJ'  => '&#x0132;',
31         'st'  => '&#xfb06;',
32         'ss'  => '&szlig;',
33       ),
34       // See http:#www.unicode.org/charts/PDF/U2000.pdf .
35       'punctuation' => array(
36         '...'   => '&#x2026;',
37         '..'    => '&#x2025;',
38         '. . .' => '&#x2026;',
39         '---'   => '&mdash;',
40         '--'    => '&ndash;',
41       ),
42       'fraction' => array(
43         '1/4'   => '&frac14;',
44         '1/2'   => '&frac12;',
45         '3/4'   => '&frac34;',
46         '1/3'   => '&#8531;',
47         '2/3'   => '&#8532;',
48         '1/5'   => '&#8533;',
49         '2/5'   => '&#8534;',
50         '3/5'   => '&#8535;',
51         '4/5'   => '&#8536;',
52         '1/6'   => '&#8537;',
53         '5/6'   => '&#8538;',
54         '1/8'   => '&#8539;',
55         '3/8'   => '&#8540;',
56         '5/8'   => '&#8541;',
57         '7/8'   => '&#8542;',
58       ),
59       'quotes' => array(
60         ',,' => '&bdquo;',
61         "''" => '&rdquo;',
62         '&lt;&lt;' => '&laquo;',
63         '&gt;&gt;' => '&raquo;',
64       ),
65       // See http:#www.unicode.org/charts/PDF/U2190.pdf .
66       'arrow' => array(
67         '-&gt;&gt;' => '&#x21a0;',
68         '&lt;&lt;-' => '&#x219e;',
69         '-&gt;|'    => '&#x21e5;',
70         '|&lt;-'    => '&#x21e4;',
71         '&lt;-&gt;' => '&#x2194;',
72         '-&gt;'     => '&#x2192;',
73         '&lt;-'     => '&#x2190;',
74         '&lt;=&gt;' => '&#x21d4;',
75         '=&gt;'     => '&#x21d2;',
76         '&lt;='     => '&#x21d0;',
77       ),
78     );
79
80     if ($type == 'all') {
81       return array_merge($map['ligature'], $map['arrow'], $map['punctuation'], $map['quotes'], $map['fraction']);
82     }
83     elseif ($type == 'nested') {
84       return $map;
85     }
86     else {
87       return $map[$type];
88     }
89   }
90
91   /**
92    * Perform character conversion.
93    *
94    * @param string $text
95    *   Text to be parsed.
96    * @param array $characters_to_convert
97    *   Array of ASCII characters to convert.
98    *
99    * @return string
100    *   The result of the conversion.
101    */
102   public static function convertCharacters($text, $characters_to_convert) {
103     if (($characters_to_convert == NULL) || (count($characters_to_convert) < 1)) {
104       // Do nothing.
105       return $text;
106     }
107
108     // Get ascii to unicode mappings.
109     $unicode_map = self::map();
110
111     foreach ($characters_to_convert as $ascii_string) {
112       $unicode_strings[] = $unicode_map[$ascii_string];
113     }
114
115     $tokens = SmartyPants::tokenizeHtml($text);
116     $result = '';
117     // Keep track of when we're inside <pre> or <code> tags.
118     $in_pre = 0;
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;
126         }
127       }
128       else {
129         $t = $cur_token[1];
130         if ($in_pre == 0) {
131           $t = SmartyPants::processEscapes($t);
132           $t = str_replace($characters_to_convert, $unicode_strings, $t);
133         }
134         $result .= $t;
135       }
136     }
137     return $result;
138   }
139
140 }