-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixer.php
243 lines (213 loc) · 6.58 KB
/
fixer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/*
* This code is part of JoliTypo - a project by JoliCode.
*
* Copyright (c) 2013 Damien Alexandre (http://jolicode.com)
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*
* Copyright (c) 2022, Customizations for phpBB by Thorsten Ahlers
*
* https://en.wikipedia.org/wiki/Quotation_mark
* https://www.virtualsystem.de/browsersprachen/
*/
namespace imcger\curlyquotes\core;
class fixer
{
/**
* DOMDocument does not like all the HTML entities; sometimes they are double encoded.
* So the entities here are plain utf8 and DOCDocument::saveHTML transform them to entity.
*/
public const ALL_SPACES = "\xE2\x80\xAF|\xC2\xAD|\xC2\xA0|\\s"; // All supported spaces, used in regexps. Better than \s
public const NO_BREAK_THIN_SPACE = "\xE2\x80\xAF"; //  
public const NO_BREAK_SPACE = "\xC2\xA0"; //  
public const SHY = "\xC2\xAD"; // ­
public const ELLIPSIS = '…'; // …
public const LDQUO = '“'; // “ or “
public const RDQUO = '”'; // ” or ”
public const BDQUO = '„'; // „ or „
public const LSQUO = '‘'; // ‘
public const RSQUO = '’'; // ’
public const SBQUO = '‚'; // ‚
public const LAQUO = '«'; // «
public const RAQUO = '»'; // »
public const LSAQUO = '‹'; // ‹
public const RSAQUO = '›'; // ›
public const PRIME = '″'; // ″
public const SPRIME = '′'; // ′
public const TIMES = '×'; // ×
public const NDASH = '–'; // – or –
public const MDASH = '—'; // — or —
public const TRADE = '™'; // ™
public const REG = '®'; // ®
public const COPY = '©'; // ©
protected $dopening;
protected $dclosing;
protected $dopeningSuffix = '';
protected $dclosingPrefix = '';
protected $sopening;
protected $sclosing;
protected $sopeningSuffix = '';
protected $sclosingPrefix = '';
public function fix($content, $set_prime)
{
// Break when locale not set
if (!$this->dopening || !$this->dclosing || !$this->sopening || !$this->sclosing)
{
return $content;
}
$pattern = [
'@(\d+\°)[' . self::ALL_SPACES . ']{0,1}(\d+)\'[' . self::ALL_SPACES . ']{0,1}((\d{1,2})|(\d{1,2}[.,]\d+))"@im', // GPS Koordinaten ddd mm ss.ss
'@(\d+\°)[' . self::ALL_SPACES . ']{0,1}((\d{1,2})|(\d{1,2}[.,]\d+))\'@im', // GPS Koordinaten ddd mm.mmm
'@(\d+)\'[' . self::ALL_SPACES . ']{0,1}((\d{1,2})|(\d{1,2}[.,]\d+))"@im', // Dimension in inches
'@([a-z0-9])\'([a-z])@im', // Apostrophe
'@(^|\s|\>|\()"([^"^\<]+)"@im', // Double Quotes
'@(^|\s|\>|\()\'([^\'^\<]+)\'@im', // Single Quotes
'@([^\d\s]+)[' . self::ALL_SPACES . ']*(,)[' . self::ALL_SPACES . ']*@mu', // No space before comma (,)
];
$replacement = [
'$1' . self::NO_BREAK_THIN_SPACE . '$2' . self::SPRIME . self::NO_BREAK_THIN_SPACE . '$3' . self::PRIME,
'$1' . self::NO_BREAK_THIN_SPACE . '$2' . self::SPRIME,
'$1' . self::SPRIME . self::NO_BREAK_THIN_SPACE . '$2' . self::PRIME,
'$1' . self::RSQUO . '$2',
'$1' . $this->dopening . $this->dopeningSuffix . '$2' . $this->dclosingPrefix . $this->dclosing,
'$1' . $this->sopening . $this->sopeningSuffix . '$2' . $this->sclosingPrefix . $this->sclosing,
'$1$2 ',
];
// Fix simple cases
$content = preg_replace($pattern, $replacement, $content);
if ($set_prime)
{
// Replace single quotes with prime
$content = str_replace("'", self::SPRIME, $content);
// Replace double quotes with Prime
$prime_pos = strpos($content, '"', 0);
while ($prime_pos)
{
// Don't replace quotes between <…>
$stag = strpos($content, '<', $prime_pos);
$btag = strpos($content, '>', $prime_pos);
if ($stag === false || ($stag < $btag))
{
$content = substr_replace($content, self::PRIME, $prime_pos, 1);
}
$prime_pos = strpos($content, '"', $prime_pos+1);
}
}
return $content;
}
/**
* Default configuration for supported lang.
*
* @param string $locale
*/
public function setLocale($locale)
{
switch ($locale[1])
{
// “…”
case 1:
$this->dopening = self::LDQUO;
$this->dclosing = self::RDQUO;
$this->dopeningSuffix = '';
$this->dclosingPrefix = '';
break;
// ”…”
case 2:
$this->dopening = self::RDQUO;
$this->dclosing = self::RDQUO;
$this->dopeningSuffix = '';
$this->dclosingPrefix = '';
break;
// „…“
case 3:
$this->dopening = self::BDQUO;
$this->dclosing = self::LDQUO;
$this->dopeningSuffix = '';
$this->dclosingPrefix = '';
break;
// «…»
case 4:
$this->dopening = self::LAQUO;
$this->dclosing = self::RAQUO;
$this->dopeningSuffix = '';
$this->dclosingPrefix = '';
break;
// « … »
case 5:
$this->dopening = self::LAQUO;
$this->dclosing = self::RAQUO;
$this->dopeningSuffix = self::NO_BREAK_THIN_SPACE;
$this->dclosingPrefix = self::NO_BREAK_THIN_SPACE;
break;
// »…«
case 6:
$this->dopening = self::RAQUO;
$this->dclosing = self::LAQUO;
$this->dopeningSuffix = '';
$this->dclosingPrefix = '';
break;
}
switch ($locale[2])
{
// ‘…’
case 1:
$this->sopening = self::LSQUO;
$this->sclosing = self::RSQUO;
$this->sopeningSuffix = '';
$this->sclosingPrefix = '';
break;
// ’…’
case 2:
$this->sopening = self::RSQUO;
$this->sclosing = self::RSQUO;
$this->sopeningSuffix = '';
$this->sclosingPrefix = '';
break;
// ‚…‘
case 3:
$this->sopening = self::SBQUO;
$this->sclosing = self::LSQUO;
$this->sopeningSuffix = '';
$this->sclosingPrefix = '';
break;
// ‹…›
case 4:
$this->sopening = self::LSAQUO;
$this->sclosing = self::RSAQUO;
$this->sopeningSuffix = '';
$this->sclosingPrefix = '';
break;
// ‹ … ›
case 5:
$this->sopening = self::LSAQUO;
$this->sclosing = self::RSAQUO;
$this->sopeningSuffix = self::NO_BREAK_THIN_SPACE;
$this->sclosingPrefix = self::NO_BREAK_THIN_SPACE;
break;
// ›…‹
case 6:
$this->sopening = self::RSAQUO;
$this->sclosing = self::LSAQUO;
$this->sopeningSuffix = '';
$this->sclosingPrefix = '';
break;
}
}
/**
* Get language part of a Locale string (fr_FR => fr).
*
* @param $locale
*
* @return string
*/
public static function getLanguageFromLocale($locale)
{
if (strpos($locale, '-'))
{
$parts = explode('-', $locale);
return strtolower($parts[0]);
}
return $locale;
}
}