-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpat_typo.php
398 lines (340 loc) · 9.41 KB
/
pat_typo.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php
/**
* Typographic enhancements in Textpattern CMS
*
* @author: Patrick LEFEVRE.
* @link: https://github.com/cara-tm/pat_typo
* @type: Admin + Public
* @prefs: no
* @order: 5
* @version: 0.4.0
* @license: GPLv2
*/
/**
* This plugin tag registry.
*/
if (class_exists('\Textpattern\Tag\Registry')) {
Txp::get('\Textpattern\Tag\Registry')
->register('pat_typo');
}
/**
* This plugin lifecycle.
*/
if (txpinterface == 'admin')
{
register_callback('pat_typo_prefs', 'prefs', '', 1);
register_callback('pat_typo_cleanup', 'plugin_lifecycle.pat_typo', 'deleted');
}
/**
* Main plugin function
*
* @param $text string The text content
* @param $lang string Country code (ISO2)
* @param $choice boolean Change the replacement sign
* @return $atts string The text content
*/
function pat_typo($atts, $thing = null)
{
global $_proceed;
extract(lAtts(array(
'text' => 'title',
'no_widow' => false,
'lang' => get_pref('language', TEXTPATTERN_DEFAULT_LANG, true),
'force' => false,
'inclusive' => false,
'preview_only' => true,
), $atts));
$_proceed = true;
// Text content choice
//$text = ($text == 'title' ? title(array('no_widow' => 0)) : body(array()));
if ($text == 'title') {
$text = parse('<txp:title escape="tidy, textile" no_widow="0" />');
} elseif ($text == 'body') {
$_proceed = false;
$text = body(array());
} else {
trigger_error(gTxt('invalid_attribute_value', array('{text}' => 'Incorrect text content.')), E_USER_WARNING);
}
// List of functions to load:
if (true == $preview_only or true == get_pref('pat_typo_preview_only')) {
if (gps('txpreview')) {
$text = _errors($text);
$text = _dialogs($text, $lang);
$text = _numerals($text);
$text = _nums($text, $lang);
$text = _smallwords($text);
$text = _punctuation($text, $lang);
$text = _widont($text, $no_widow);
$text = _first_signs($text , $lang, $force);
$text = _last_signs($text , $lang, $force);
$text = _dash($text, $lang, $force);
$text = _alpha($text, $lang);
$text = _inclusive($text, $inclusive);
}
} elseif (false == $preview_only or false == get_pref('pat_typo_proview_only')) {
$text = _errors($text);
$text = _dialogs($text, $lang);
$text = _numerals($text);
$text = _muns($text, $lang);
$text = _smallwords($text);
$text = _punctuation($text, $lang);
$text = _widont($text, $no_widow);
$text = _first_signs($text , $lang, $force);
$text = _last_signs($text , $lang, $force);
$text = _dash($text, $lang, $force);
$text = _alpha($text, $lang);
$text = _inclusive($text, $inclusive);
}
return $text;
}
/**
* _errors
*
* Remove some writing errors
*
* @param $text string The text entry
* @return $text string The new text entry
*/
function _errors($text)
{
$first = '@(\()\s+?@im';
$temp = preg_replace($first, '$1', $text);
$matches = '@\s+?(\,|\.|\)|\/)@im';
return preg_replace($matches, '$1', $temp);
}
/**
* _dialogs
*
* Converts starting hyphens into new lines by the relevant signs
*
* @param $text string The text entry
* @return $text string The new text entry
*/
function _dialogs($text, $lang)
{
$matches = '/^((<.*>)?[A-Za-z]*|(\R)|\s)\-(\s+)?(.*)/mxu';
return preg_replace($matches, '$1— $5', $text);
}
/**
* _numerals
*
* Converts some numbers
*
* @param $text string The text entry
* @return $text string The new text entry
*/
function _numerals($text)
{
$pos = '@\s?(\/1|\/2|\/3)@';
$temp = preg_replace($pos, '$1', $text);
// Note: Textile use this '[1/2]' convention instead but do not render '[1/3]'
$matches = array('1/2', '1/3', '2/3', '1/4', '3/4', '1/10', '0/00');
$numbers = array('½', '⅓', '⅔', '¼', '¾', '⅒', '‰');
return str_replace($matches, $numbers, $temp);
}
/**
* _mums
*
* Format numbers for French writers
*
* @param $text string The text entry
* @param $lang string The country code
* @return $text string The new text entry
*/
function _muns($text, $lang)
{
if ($lang === 'fr' or $lang === 'fr-FR') {
$matches = '@([^&#\w?\d+]\s?\d)(?=(\d{3})+(?!\d))@m';
return preg_replace($matches, '$1 ', $text);
} else {
return $text;
}
}
/**
* _smallwords
*
* Surrounds few characters with non breaking spaces
*
* @param $text string The text entry
* @return $text string The new text entry
*/
function _smallwords($text)
{
global $_proceed;
if ($_proceed) {
$matches = '@(\s)([\p{L}]{0,2}?[^'|\»|\!|\–|\:|\«]\s)@';
return preg_replace($matches, ' $2', $text);
} else {
return $text;
}
}
/**
* _punctuation
*
* Adds a space before some signs for French language
*
* @param $text string The text entry
* @param $lang string The country code
* @return $text string The new text entry
*/
function _punctuation($text , $lang = null)
{
if ($lang === 'fr' or $lang === 'fr-FR') {
// Semi colons
$pos = '/([^&\w\;]\p{L}+|\»|\"|\))(\s+)?([\;])/';
$temp = preg_replace($pos, '$1 ;', $text);
// All others punctuation signs
$pattern = '/(\s+)?(\:[^\/\/]|\?|!|%|€|km|cm|kg|mg|kW)/m';
return preg_replace($pattern, ' $2', $temp);
} else {
// All other languages than French
return $text;
}
}
/**
* widont
*
* Replaces the space between the last two words in a string with &160;
*
* @param $text string The text entry
* @param $no_widow boolean TXP attribute
* @return $text string The new text entry
*/
function _widont($text, $no_widow)
{
return (true == $no_widow ? preg_replace( '/(\s)([^\s\»]+)\s*(\s)?$/', ' $2', $text) : $text);
}
/**
* _signs
*
* Adds spaces around the french quotes 'guillemets' or change by quotation marks
*
* @param $text string The article title
* @param $lang string The country code
* @param $choice boolean Choice for hair spaces or HTML tags
* @return $string
*/
function _first_signs($text, $lang, $force)
{
global $_proceed;
if ($_proceed) {
if ($lang == 'fr' or $lang == 'fr-FR') {
$thin = $force == false ? '$1 ' : '<span class="thinsp">$1 </span>';
// Starting quotes
$matches = '@(\«)(?<space> )(\s+)?@'; // '/(«|"\/|"\/)(\s?)/';
return preg_replace($matches, $thin, $text);
} else {
$sign = $force == false ? '“$2' : '<span class="thinsp">“</span>$2';
$matches = '@("\/|"\/)(\s?)@';
return preg_replace($matches, $sign, $text);
}
} else {
return $text;
}
}
function _last_signs($text, $lang, $force)
{
global $_proceed;
if ($_proceed) {
if ($lang == 'fr' or $lang == 'fr-FR') {
$thin = $force == false ? ' $3' : '<span class="thinsp"> $3</span>';
// Final quotes
//$matches = '/([&#]\w+?;|(\bspa\b|\b160\b))?(\s+)?(\»)/'; // '/(\s?)(»|\/"|\/")/';
$matches = '@(\s+)?(?<char>�)?(\s+)?(\»)@';
return preg_replace($matches, $thin, $text);
} else {
$sign = $force == false ? '”$2' : '<span class="thinsp">”</span>$2';
$matches = '@(\/"|\/")(\s?)@';
return preg_replace($matches, $sign, $text);
}
} else {
return $text;
}
}
/**
* _dash
*
* Puts a   before and after an &ndash or —
* Change sinple dash by endashes instead.
*
* @param $text string The text entry
* @param $lang string The country code
* @param $force boolean Choice for hair spaces or HTML tags
* @return $text string The new text entry
*/
function _dash($text, $lang, $force)
{
if ($lang == 'fr' or $lang == 'fr-FR') {
$thin = $force == false ? $sign.' $3 '.$sign.'$6' : '<span class="thinsp">'.$sign.' $3 '.$sign.'</span>$6';
$pos = '/(–|-)(\s+)?(.*)(–|-)\s/'; // (–|—|–|-)(\s+)?(.*)(–|—|–|-)\s
$temp = preg_replace($pos, '– $3 – ', $text);
// But remove a final dash before a dot
$matches = '/(\s)( –|\–|\-)(\.)/m';
return preg_replace($matches, '$3', $temp);
} else {
return $text;
}
}
/**
* _alpha
*
* Change individual first 'A' letter to its propper 'À' sign
*
* @param $text string The text entry
* @param $lang string The country code
* @return $text string The new text content
*/
function _alpha($text, $lang)
{
if ($lang == 'fr' or $lang == 'fr-FR') {
$matches = '/^(<.*>)?(—\s|—\s)?[A][^\p{L}]|(\.\s)[A](\s)/im'; // '/^(<.*>)?[A][^\p{L}]|(\.\s)[A](\s)/im';
return preg_replace($matches, '$1$2$3À ', $text);
} else {
return $text;
}
}
/**
* _inclusive
*
* Adopts the "inclusive" notation for French users only
*
* @param $text string The text content
* @param $inclusive boolean Choose to adopt or not the "inclusive notation"
* @return $text string The new text content
*
*/
function _inclusive($text, $inclusive)
{
global $_proceed;
if (true == $inclusive and true == $_proceed) {
$matches = '/\.(\p{Ll}+)/m';
return preg_replace($matches, '<span class="bull">⁠•⁠</span>$1', $text);
} else {
return $text;
}
}
/**
* This plugin preferences
*
* @param
* @return SQL Plugin preference field
*/
function pat_typo_prefs()
{
global $textarray;
$textarray['pat_typo_preview_only'] = 'Enable pat_typo on article preview only?';
if (!safe_field('name', 'txp_prefs', "name='pat_typo_preview_only'"))
{
safe_insert('txp_prefs', "name='pat_typo_preview_only', val='1', type=1, event='admin', html='yesnoradio', position=25");
}
}
/**
* This plugin cleanup on deletion
*
* @param
* @return SQL Safe delete field
*/
function pat_typo_cleanup()
{
safe_delete('txp_prefs', "name='pat_typo_preview_only'");
}