forked from suminb/readown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmartyPants.php
1148 lines (960 loc) · 33.4 KB
/
SmartyPants.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
#
# SmartyPants Typographer - Smart typography for web sites
#
# PHP SmartyPants & Typographer
# Copyright (c) 2004-2006 Michel Fortin
# <http://www.michelf.com/>
#
# Original SmartyPants
# Copyright (c) 2003-2004 John Gruber
# <http://daringfireball.net/>
#
define( 'SMARTYPANTS_VERSION', "1.5.1oo2" ); # Unreleased
define( 'SMARTYPANTSTYPOGRAPHER_VERSION', "1.0" ); # Wed 28 Jun 2006
#
# Default configuration:
#
# 1 -> "--" for em-dashes; no en-dash support
# 2 -> "---" for em-dashes; "--" for en-dashes
# 3 -> "--" for em-dashes; "---" for en-dashes
# See docs for more configuration options.
#
define( 'SMARTYPANTS_ATTR', 1 );
# Openning and closing smart double-quotes.
define( 'SMARTYPANTS_SMART_DOUBLEQUOTE_OPEN', "“" );
define( 'SMARTYPANTS_SMART_DOUBLEQUOTE_CLOSE', "”" );
# Space around em-dashes. "He_—_or she_—_should change that."
define( 'SMARTYPANTS_SPACE_EMDASH', " " );
# Space around en-dashes. "He_–_or she_–_should change that."
define( 'SMARTYPANTS_SPACE_ENDASH', " " );
# Space before a colon. "He said_: here it is."
define( 'SMARTYPANTS_SPACE_COLON', " " );
# Space before a semicolon. "That's what I said_; that's what he said."
define( 'SMARTYPANTS_SPACE_SEMICOLON', " " );
# Space before a question mark and an exclamation mark: "¡_Holà_! What_?"
define( 'SMARTYPANTS_SPACE_MARKS', " " );
# Space inside french quotes. "Voici la «_chose_» qui m'a attaqué."
define( 'SMARTYPANTS_SPACE_FRENCHQUOTE', " " );
# Space as thousand separator. "On compte 10_000 maisons sur cette liste."
define( 'SMARTYPANTS_SPACE_THOUSAND', " " );
# Space before a unit abreviation. "This 12_kg of matter costs 10_$."
define( 'SMARTYPANTS_SPACE_UNIT', " " );
# SmartyPants will not alter the content of these tags:
define( 'SMARTYPANTS_TAGS_TO_SKIP', 'pre|code|kbd|script|math');
### Standard Function Interface ###
define( 'SMARTYPANTS_PARSER_CLASS', 'SmartyPantsTypographer_Parser' );
function SmartyPants($text, $attr = SMARTYPANTS_ATTR) {
#
# Initialize the parser and return the result of its transform method.
#
# Setup static parser array.
static $parser = array();
if (!isset($parser[$attr])) {
$parser_class = SMARTYPANTS_PARSER_CLASS;
$parser[$attr] = new $parser_class($attr);
}
# Transform text using parser.
return $parser[$attr]->transform($text);
}
function SmartQuotes($text, $attr = 1) {
switch ($attr) {
case 0: return $text;
case 2: $attr = 'qb'; break;
default: $attr = 'q'; break;
}
return SmartyPants($text, $attr);
}
function SmartDashes($text, $attr = 1) {
switch ($attr) {
case 0: return $text;
case 2: $attr = 'D'; break;
case 3: $attr = 'i'; break;
default: $attr = 'd'; break;
}
return SmartyPants($text, $attr);
}
function SmartEllipsis($text, $attr = 1) {
switch ($attr) {
case 0: return $text;
default: $attr = 'e'; break;
}
return SmartyPants($text, $attr);
}
#
# SmartyPants Parser Class
#
class SmartyPants_Parser {
# Options to specify which transformations to make:
var $do_nothing = 0;
var $do_quotes = 0;
var $do_backticks = 0;
var $do_dashes = 0;
var $do_ellipses = 0;
var $do_stupefy = 0;
var $convert_quot = 0; # should we translate " entities into normal quotes?
function SmartyPants_Parser($attr = SMARTYPANTS_ATTR) {
#
# Initialize a SmartyPants_Parser with certain attributes.
#
# Parser attributes:
# 0 : do nothing
# 1 : set all
# 2 : set all, using old school en- and em- dash shortcuts
# 3 : set all, using inverted old school en and em- dash shortcuts
#
# q : quotes
# b : backtick quotes (``double'' only)
# B : backtick quotes (``double'' and `single')
# d : dashes
# D : old school dashes
# i : inverted old school dashes
# e : ellipses
# w : convert " entities to " for Dreamweaver users
#
if ($attr == "0") {
$this->do_nothing = 1;
}
else if ($attr == "1") {
# Do everything, turn all options on.
$this->do_quotes = 1;
$this->do_backticks = 1;
$this->do_dashes = 1;
$this->do_ellipses = 1;
}
else if ($attr == "2") {
# Do everything, turn all options on, use old school dash shorthand.
$this->do_quotes = 1;
$this->do_backticks = 1;
$this->do_dashes = 2;
$this->do_ellipses = 1;
}
else if ($attr == "3") {
# Do everything, turn all options on, use inverted old school dash shorthand.
$this->do_quotes = 1;
$this->do_backticks = 1;
$this->do_dashes = 3;
$this->do_ellipses = 1;
}
else if ($attr == "-1") {
# Special "stupefy" mode.
$this->do_stupefy = 1;
}
else {
$chars = preg_split('//', $attr);
foreach ($chars as $c){
if ($c == "q") { $this->do_quotes = 1; }
else if ($c == "b") { $this->do_backticks = 1; }
else if ($c == "B") { $this->do_backticks = 2; }
else if ($c == "d") { $this->do_dashes = 1; }
else if ($c == "D") { $this->do_dashes = 2; }
else if ($c == "i") { $this->do_dashes = 3; }
else if ($c == "e") { $this->do_ellipses = 1; }
else if ($c == "w") { $this->convert_quot = 1; }
else {
# Unknown attribute option, ignore.
}
}
}
}
function transform($text) {
if ($this->do_nothing) {
return $text;
}
$tokens = $this->tokenizeHTML($text);
$result = '';
$in_pre = 0; # Keep track of when we're inside <pre> or <code> tags.
$prev_token_last_char = ""; # This is a cheat, used to get some context
# for one-character tokens that consist of
# just a quote char. What we do is remember
# the last character of the previous text
# token, to use as context to curl single-
# character quote tokens correctly.
foreach ($tokens as $cur_token) {
if ($cur_token[0] == "tag") {
# Don't mess with quotes inside tags.
$result .= $cur_token[1];
if (preg_match('@<(/?)(?:'.SMARTYPANTS_TAGS_TO_SKIP.')[\s>]@', $cur_token[1], $matches)) {
$in_pre = isset($matches[1]) && $matches[1] == '/' ? 0 : 1;
}
} else {
$t = $cur_token[1];
$last_char = substr($t, -1); # Remember last char of this token before processing.
if (! $in_pre) {
$t = $this->educate($t, $prev_token_last_char);
}
$prev_token_last_char = $last_char;
$result .= $t;
}
}
return $result;
}
function educate($t, $prev_token_last_char) {
$t = $this->processEscapes($t);
if ($this->convert_quot) {
$t = preg_replace('/"/', '"', $t);
}
if ($this->do_dashes) {
if ($this->do_dashes == 1) $t = $this->educateDashes($t);
if ($this->do_dashes == 2) $t = $this->educateDashesOldSchool($t);
if ($this->do_dashes == 3) $t = $this->educateDashesOldSchoolInverted($t);
}
if ($this->do_ellipses) $t = $this->educateEllipses($t);
# Note: backticks need to be processed before quotes.
if ($this->do_backticks) {
$t = $this->educateBackticks($t);
if ($this->do_backticks == 2) $t = $this->educateSingleBackticks($t);
}
if ($this->do_quotes) {
if ($t == "'") {
# Special case: single-character ' token
if (preg_match('/\S/', $prev_token_last_char)) {
$t = "’";
}
else {
$t = "‘";
}
}
else if ($t == '"') {
# Special case: single-character " token
if (preg_match('/\S/', $prev_token_last_char)) {
$t = "”";
}
else {
$t = "“";
}
}
else {
# Normal case:
$t = $this->educateQuotes($t);
}
}
if ($this->do_stupefy) $t = $this->stupefyEntities($t);
return $t;
}
function educateQuotes($_) {
#
# Parameter: String.
#
# Returns: The string, with "educated" curly quote HTML entities.
#
# Example input: "Isn't this fun?"
# Example output: “Isn’t this fun?”
#
# Make our own "punctuation" character class, because the POSIX-style
# [:PUNCT:] is only available in Perl 5.6 or later:
$punct_class = "[!\"#\\$\\%'()*+,-.\\/:;<=>?\\@\\[\\\\\]\\^_`{|}~]";
# Special case if the very first character is a quote
# followed by punctuation at a non-word-break. Close the quotes by brute force:
$_ = preg_replace(
array("/^'(?=$punct_class\\B)/", "/^\"(?=$punct_class\\B)/"),
array('’', '”'), $_);
# Special case for double sets of quotes, e.g.:
# <p>He said, "'Quoted' words in a larger quote."</p>
$_ = preg_replace(
array("/\"'(?=\w)/", "/'\"(?=\w)/"),
array('“‘', '‘“'), $_);
# Special case for decade abbreviations (the '80s):
$_ = preg_replace("/'(?=\\d{2}s)/", '’', $_);
$close_class = '[^\ \t\r\n\[\{\(\-]';
$dec_dashes = '&\#8211;|&\#8212;';
# Get most opening single quotes:
$_ = preg_replace("{
(
\\s | # a whitespace char, or
| # a non-breaking space entity, or
-- | # dashes, or
&[mn]dash; | # named dash entities
$dec_dashes | # or decimal entities
&\\#x201[34]; # or hex
)
' # the quote
(?=\\w) # followed by a word character
}x", '\1‘', $_);
# Single closing quotes:
$_ = preg_replace("{
($close_class)?
'
(?(1)| # If $1 captured, then do nothing;
(?=\\s | s\\b) # otherwise, positive lookahead for a whitespace
) # char or an 's' at a word ending position. This
# is a special case to handle something like:
# \"<i>Custer</i>'s Last Stand.\"
}xi", '\1’', $_);
# Any remaining single quotes should be opening ones:
$_ = str_replace("'", '‘', $_);
# Get most opening double quotes:
$_ = preg_replace("{
(
\\s | # a whitespace char, or
| # a non-breaking space entity, or
-- | # dashes, or
&[mn]dash; | # named dash entities
$dec_dashes | # or decimal entities
&\\#x201[34]; # or hex
)
\" # the quote
(?=\\w) # followed by a word character
}x", '\1“', $_);
# Double closing quotes:
$_ = preg_replace("{
($close_class)?
\"
(?(1)|(?=\\s)) # If $1 captured, then do nothing;
# if not, then make sure the next char is whitespace.
}x", '\1”', $_);
# Any remaining quotes should be opening ones.
$_ = str_replace('"', '“', $_);
return $_;
}
function educateBackticks($_) {
#
# Parameter: String.
# Returns: The string, with ``backticks'' -style double quotes
# translated into HTML curly quote entities.
#
# Example input: ``Isn't this fun?''
# Example output: “Isn't this fun?”
#
$_ = str_replace(array("``", "''",),
array('“', '”'), $_);
return $_;
}
function educateSingleBackticks($_) {
#
# Parameter: String.
# Returns: The string, with `backticks' -style single quotes
# translated into HTML curly quote entities.
#
# Example input: `Isn't this fun?'
# Example output: ‘Isn’t this fun?’
#
$_ = str_replace(array("`", "'",),
array('‘', '’'), $_);
return $_;
}
function educateDashes($_) {
#
# Parameter: String.
#
# Returns: The string, with each instance of "--" translated to
# an em-dash HTML entity.
#
$_ = str_replace('--', '—', $_);
return $_;
}
function educateDashesOldSchool($_) {
#
# Parameter: String.
#
# Returns: The string, with each instance of "--" translated to
# an en-dash HTML entity, and each "---" translated to
# an em-dash HTML entity.
#
# em en
$_ = str_replace(array("---", "--",),
array('—', '–'), $_);
return $_;
}
function educateDashesOldSchoolInverted($_) {
#
# Parameter: String.
#
# Returns: The string, with each instance of "--" translated to
# an em-dash HTML entity, and each "---" translated to
# an en-dash HTML entity. Two reasons why: First, unlike the
# en- and em-dash syntax supported by
# EducateDashesOldSchool(), it's compatible with existing
# entries written before SmartyPants 1.1, back when "--" was
# only used for em-dashes. Second, em-dashes are more
# common than en-dashes, and so it sort of makes sense that
# the shortcut should be shorter to type. (Thanks to Aaron
# Swartz for the idea.)
#
# en em
$_ = str_replace(array("---", "--",),
array('–', '—'), $_);
return $_;
}
function educateEllipses($_) {
#
# Parameter: String.
# Returns: The string, with each instance of "..." translated to
# an ellipsis HTML entity. Also converts the case where
# there are spaces between the dots.
#
# Example input: Huh...?
# Example output: Huh…?
#
$_ = str_replace(array("...", ". . .",), '…', $_);
return $_;
}
function stupefyEntities($_) {
#
# Parameter: String.
# Returns: The string, with each SmartyPants HTML entity translated to
# its ASCII counterpart.
#
# Example input: “Hello — world.”
# Example output: "Hello -- world."
#
# en-dash em-dash
$_ = str_replace(array('–', '—'),
array('-', '--'), $_);
# single quote open close
$_ = str_replace(array('‘', '’'), "'", $_);
# double quote open close
$_ = str_replace(array('“', '”'), '"', $_);
$_ = str_replace('…', '...', $_); # ellipsis
return $_;
}
function processEscapes($_) {
#
# Parameter: String.
# Returns: The string, with after processing the following backslash
# escape sequences. This is useful if you want to force a "dumb"
# quote or other character to appear.
#
# Escape Value
# ------ -----
# \\ \
# \" "
# \' '
# \. .
# \- -
# \` `
#
$_ = str_replace(
array('\\\\', '\"', "\'", '\.', '\-', '\`'),
array('\', '"', ''', '.', '-', '`'), $_);
return $_;
}
function tokenizeHTML($str) {
#
# Parameter: String containing HTML markup.
# Returns: An array of the tokens comprising the input
# string. Each token is either a tag (possibly with nested,
# tags contained therein, such as <a href="<MTFoo>">, or a
# run of text between tags. Each element of the array is a
# two-element array; the first is either 'tag' or 'text';
# the second is the actual value.
#
#
# Regular expression derived from the _tokenize() subroutine in
# Brad Choate's MTRegex plugin.
# <http://www.bradchoate.com/past/mtregex.php>
#
$index = 0;
$tokens = array();
$match = '(?s:<!(?:--.*?--\s*)+>)|'. # comment
'(?s:<\?.*?\?>)|'. # processing instruction
# regular tags
'(?:<[/!$]?[-a-zA-Z0-9:]+\b(?>[^"\'>]+|"[^"]*"|\'[^\']*\')*>)';
$parts = preg_split("{($match)}", $str, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($parts as $part) {
if (++$index % 2 && $part != '')
$tokens[] = array('text', $part);
else
$tokens[] = array('tag', $part);
}
return $tokens;
}
}
#
# SmartyPants Typographer Parser Class
#
class SmartyPantsTypographer_Parser extends SmartyPants_Parser {
# Options to specify which transformations to make:
var $do_comma_quotes = 0;
var $do_guillemets = 0;
var $do_space_emdash = 0;
var $do_space_endash = 0;
var $do_space_colon = 0;
var $do_space_semicolon = 0;
var $do_space_marks = 0;
var $do_space_frenchquote = 0;
var $do_space_thousand = 0;
var $do_space_unit = 0;
# Smart quote characters:
var $smart_doublequote_open = SMARTYPANTS_SMART_DOUBLEQUOTE_OPEN;
var $smart_doublequote_close = SMARTYPANTS_SMART_DOUBLEQUOTE_CLOSE;
var $smart_singlequote_open = '‘';
var $smart_singlequote_close = '’'; # Also apostrophe.
# Space characters for different places:
var $space_emdash = SMARTYPANTS_SPACE_EMDASH;
var $space_endash = SMARTYPANTS_SPACE_ENDASH;
var $space_colon = SMARTYPANTS_SPACE_COLON;
var $space_semicolon = SMARTYPANTS_SPACE_SEMICOLON;
var $space_marks = SMARTYPANTS_SPACE_MARKS;
var $space_frenchquote = SMARTYPANTS_SPACE_FRENCHQUOTE;
var $space_thousand = SMARTYPANTS_SPACE_THOUSAND;
var $space_unit = SMARTYPANTS_SPACE_UNIT;
# Expression of a space (breakable or not):
var $space = '(?: | | |�*160;|�*[aA]0;)';
function SmartyPantsTypographer_Parser($attr = SMARTYPANTS_ATTR) {
#
# Initialize a SmartyPantsTypographer_Parser with certain attributes.
#
# Parser attributes:
# 0 : do nothing
# 1 : set all, except dash spacing
# 2 : set all, except dash spacing, using old school en- and em- dash shortcuts
# 3 : set all, except dash spacing, using inverted old school en and em- dash shortcuts
#
# Punctuation:
# q -> quotes
# b -> backtick quotes (``double'' only)
# B -> backtick quotes (``double'' and `single')
# c -> comma quotes (,,double`` only)
# g -> guillemets (<<double>> only)
# d -> dashes
# D -> old school dashes
# i -> inverted old school dashes
# e -> ellipses
# w -> convert " entities to " for Dreamweaver users
#
# Spacing:
# : -> colon spacing +-
# ; -> semicolon spacing +-
# m -> question and exclamation marks spacing +-
# h -> em-dash spacing +-
# H -> en-dash spacing +-
# f -> french quote spacing +-
# t -> thousand separator spacing -
# u -> unit spacing +-
# (you can add a plus sign after some of these options denoted by + to
# add the space when it is not already present, or you can add a minus
# sign to completly remove any space present)
#
# Initialize inherited SmartyPants parser.
parent::SmartyPants_Parser($attr);
if ($attr == "1" || $attr == "2" || $attr == "3") {
# Do everything, turn all options on.
$this->do_comma_quotes = 1;
$this->do_guillemets = 1;
$this->do_space_emdash = 1;
$this->do_space_endash = 1;
$this->do_space_colon = 1;
$this->do_space_semicolon = 1;
$this->do_space_marks = 1;
$this->do_space_frenchquote = 1;
$this->do_space_thousand = 1;
$this->do_space_unit = 1;
}
else if ($attr == "-1") {
# Special "stupefy" mode.
$this->do_stupefy = 1;
}
else {
$chars = preg_split('//', $attr);
foreach ($chars as $c){
if ($c == "c") { $current =& $this->do_comma_quotes; }
else if ($c == "g") { $current =& $this->do_guillemets; }
else if ($c == ":") { $current =& $this->do_space_colon; }
else if ($c == ";") { $current =& $this->do_space_semicolon; }
else if ($c == "m") { $current =& $this->do_space_marks; }
else if ($c == "h") { $current =& $this->do_space_emdash; }
else if ($c == "H") { $current =& $this->do_space_endash; }
else if ($c == "f") { $current =& $this->do_space_frenchquote; }
else if ($c == "t") { $current =& $this->do_space_thousand; }
else if ($c == "u") { $current =& $this->do_space_unit; }
else if ($c == "+") {
$current = 2;
unset($current);
}
else if ($c == "-") {
$current = -1;
unset($current);
}
else {
# Unknown attribute option, ignore.
}
$current = 1;
}
}
}
function educate($t, $prev_token_last_char) {
$t = parent::educate($t, $prev_token_last_char);
if ($this->do_comma_quotes) $t = $this->educateCommaQuotes($t);
if ($this->do_guillemets) $t = $this->educateGuillemets($t);
if ($this->do_space_emdash) $t = $this->spaceEmDash($t);
if ($this->do_space_endash) $t = $this->spaceEnDash($t);
if ($this->do_space_colon) $t = $this->spaceColon($t);
if ($this->do_space_semicolon) $t = $this->spaceSemicolon($t);
if ($this->do_space_marks) $t = $this->spaceMarks($t);
if ($this->do_space_frenchquote) $t = $this->spaceFrenchQuotes($t);
if ($this->do_space_thousand) $t = $this->spaceThousandSeparator($t);
if ($this->do_space_unit) $t = $this->spaceUnit($t);
return $t;
}
function educateQuotes($_) {
#
# Parameter: String.
#
# Returns: The string, with "educated" curly quote HTML entities.
#
# Example input: "Isn't this fun?"
# Example output: “Isn’t this fun?”
#
$dq_open = $this->smart_doublequote_open;
$dq_close = $this->smart_doublequote_close;
$sq_open = $this->smart_singlequote_open;
$sq_close = $this->smart_singlequote_close;
# Make our own "punctuation" character class, because the POSIX-style
# [:PUNCT:] is only available in Perl 5.6 or later:
$punct_class = "[!\"#\\$\\%'()*+,-.\\/:;<=>?\\@\\[\\\\\]\\^_`{|}~]";
# Special case if the very first character is a quote
# followed by punctuation at a non-word-break. Close the quotes by brute force:
$_ = preg_replace(
array("/^'(?=$punct_class\\B)/", "/^\"(?=$punct_class\\B)/"),
array($sq_close, $dq_close), $_);
# Special case for double sets of quotes, e.g.:
# <p>He said, "'Quoted' words in a larger quote."</p>
$_ = preg_replace(
array("/\"'(?=\w)/", "/'\"(?=\w)/"),
array($dq_open.$sq_open, $sq_open.$dq_open), $_);
# Special case for decade abbreviations (the '80s):
$_ = preg_replace("/'(?=\\d{2}s)/", $sq_close, $_);
$close_class = '[^\ \t\r\n\[\{\(\-]';
$dec_dashes = '&\#8211;|&\#8212;';
# Get most opening single quotes:
$_ = preg_replace("{
(
\\s | # a whitespace char, or
| # a non-breaking space entity, or
-- | # dashes, or
&[mn]dash; | # named dash entities
$dec_dashes | # or decimal entities
&\\#x201[34]; # or hex
)
' # the quote
(?=\\w) # followed by a word character
}x", '\1'.$sq_open, $_);
# Single closing quotes:
$_ = preg_replace("{
($close_class)?
'
(?(1)| # If $1 captured, then do nothing;
(?=\\s | s\\b) # otherwise, positive lookahead for a whitespace
) # char or an 's' at a word ending position. This
# is a special case to handle something like:
# \"<i>Custer</i>'s Last Stand.\"
}xi", '\1'.$sq_close, $_);
# Any remaining single quotes should be opening ones:
$_ = str_replace("'", $sq_open, $_);
# Get most opening double quotes:
$_ = preg_replace("{
(
\\s | # a whitespace char, or
| # a non-breaking space entity, or
-- | # dashes, or
&[mn]dash; | # named dash entities
$dec_dashes | # or decimal entities
&\\#x201[34]; # or hex
)
\" # the quote
(?=\\w) # followed by a word character
}x", '\1'.$dq_open, $_);
# Double closing quotes:
$_ = preg_replace("{
($close_class)?
\"
(?(1)|(?=\\s)) # If $1 captured, then do nothing;
# if not, then make sure the next char is whitespace.
}x", '\1'.$dq_close, $_);
# Any remaining quotes should be opening ones.
$_ = str_replace('"', $dq_open, $_);
return $_;
}
function educateCommaQuotes($_) {
#
# Parameter: String.
# Returns: The string, with ,,comma,, -style double quotes
# translated into HTML curly quote entities.
#
# Example input: ,,Isn't this fun?,,
# Example output: „Isn't this fun?„
#
# Note: this is meant to be used alongside with backtick quotes; there is
# no language that use only lower quotations alone mark like in the example.
#
$_ = str_replace(",,", '„', $_);
return $_;
}
function educateGuillemets($_) {
#
# Parameter: String.
# Returns: The string, with << guillemets >> -style quotes
# translated into HTML guillemets entities.
#
# Example input: << Isn't this fun? >>
# Example output: „ Isn't this fun? „
#
$_ = preg_replace("/(?:<|<){2}/", '«', $_);
$_ = preg_replace("/(?:>|>){2}/", '»', $_);
return $_;
}
function spaceFrenchQuotes($_) {
#
# Parameters: String, replacement character, and forcing flag.
# Returns: The string, with appropriates spaces replaced
# inside french-style quotes, only french quotes.
#
# Example input: Quotes in « French », »German« and »Finnish» style.
# Example output: Quotes in «_French_», »German« and »Finnish» style.
#
$opt = ( $this->do_space_frenchquote == 2 ? '?' : '' );
$chr = ( $this->do_space_frenchquote != -1 ? $this->space_frenchquote : '' );
# Characters allowed immediatly outside quotes.
$outside_char = $this->space . '|\s|[.,:;!?\[\](){}|@*~=+-]|¡|¿';
$_ = preg_replace(
"/(^|$outside_char)(«|«|›|‹)$this->space$opt/",
"\\1\\2$chr", $_);
$_ = preg_replace(
"/$this->space$opt(»|»|‹|›)($outside_char|$)/",
"$chr\\1\\2", $_);
return $_;
}
function spaceColon($_) {
#
# Parameters: String, replacement character, and forcing flag.
# Returns: The string, with appropriates spaces replaced
# before colons.
#
# Example input: Ingredients : fun.
# Example output: Ingredients_: fun.
#
$opt = ( $this->do_space_colon == 2 ? '?' : '' );
$chr = ( $this->do_space_colon != -1 ? $this->space_colon : '' );
$_ = preg_replace("/$this->space$opt(:)(\\s|$)/m",
"$chr\\1\\2", $_);
return $_;
}
function spaceSemicolon($_) {
#
# Parameters: String, replacement character, and forcing flag.
# Returns: The string, with appropriates spaces replaced
# before semicolons.
#
# Example input: There he goes ; there she goes.
# Example output: There he goes_; there she goes.
#
$opt = ( $this->do_space_semicolon == 2 ? '?' : '' );
$chr = ( $this->do_space_semicolon != -1 ? $this->space_semicolon : '' );
$_ = preg_replace("/$this->space(;)(?=\\s|$)/m",
" \\1", $_);
$_ = preg_replace("/((?:^|\\s)(?>[^&;\\s]+|&#?[a-zA-Z0-9]+;)*)".
" $opt(;)(?=\\s|$)/m",
"\\1$chr\\2", $_);
return $_;
}
function spaceMarks($_) {
#
# Parameters: String, replacement character, and forcing flag.
# Returns: The string, with appropriates spaces replaced
# around question and exclamation marks.
#
# Example input: ¡ Holà ! What ?
# Example output: ¡_Holà_! What_?
#
$opt = ( $this->do_space_marks == 2 ? '?' : '' );
$chr = ( $this->do_space_marks != -1 ? $this->space_marks : '' );
// Regular marks.
$_ = preg_replace("/$this->space$opt([?!]+)/", "$chr\\1", $_);
// Inverted marks.
$imarks = "(?:¡|¡|¡|&#x[Aa]1;|¿|¿|¿|&#x[Bb][Ff];)";
$_ = preg_replace("/($imarks+)$this->space$opt/", "\\1$chr", $_);
return $_;
}
function spaceEmDash($_) {
#
# Parameters: String, two replacement characters separated by a hyphen (`-`),
# and forcing flag.
#
# Returns: The string, with appropriates spaces replaced
# around dashes.
#
# Example input: Then — without any plan — the fun happend.
# Example output: Then_—_without any plan_—_the fun happend.
#
$opt = ( $this->do_space_emdash == 2 ? '?' : '' );
$chr = ( $this->do_space_emdash != -1 ? $this->space_emdash : '' );
$_ = preg_replace("/$this->space$opt(—|—)$this->space$opt/",
"$chr\\1$chr", $_);
return $_;
}
function spaceEnDash($_) {
#
# Parameters: String, two replacement characters separated by a hyphen (`-`),
# and forcing flag.
#
# Returns: The string, with appropriates spaces replaced
# around dashes.
#
# Example input: Then — without any plan — the fun happend.
# Example output: Then_—_without any plan_—_the fun happend.
#
$opt = ( $this->do_space_endash == 2 ? '?' : '' );
$chr = ( $this->do_space_endash != -1 ? $this->space_endash : '' );
$_ = preg_replace("/$this->space$opt(–|–)$this->space$opt/",
"$chr\\1$chr", $_);
return $_;
}
function spaceThousandSeparator($_) {
#
# Parameters: String, replacement character, and forcing flag.
# Returns: The string, with appropriates spaces replaced
# inside numbers (thousand separator in french).
#
# Example input: Il y a 10 000 insectes amusants dans ton jardin.
# Example output: Il y a 10_000 insectes amusants dans ton jardin.
#
$chr = ( $this->do_space_thousand != -1 ? $this->space_thousand : '' );
$_ = preg_replace('/([0-9]) ([0-9])/', "\\1$chr\\2", $_);
return $_;
}
var $units = '
### Metric units (with prefixes)
(?:
p |
µ | µ | &\#0*181; | &\#[xX]0*[Bb]5; |
[mcdhkMGT]
)?
(?:
[mgstAKNJWCVFSTHBL]|mol|cd|rad|Hz|Pa|Wb|lm|lx|Bq|Gy|Sv|kat|
Ω | Ohm | Ω | &\#0*937; | &\#[xX]0*3[Aa]9;
)|
### Computers units (KB, Kb, TB, Kbps)
[kKMGT]?(?:[oBb]|[oBb]ps|flops)|
### Money
¢ | ¢ | &\#0*162; | &\#[xX]0*[Aa]2; |
M?(?:
£ | £ | &\#0*163; | &\#[xX]0*[Aa]3; |
¥ | ¥ | &\#0*165; | &\#[xX]0*[Aa]5; |
€ | € | &\#0*8364; | &\#[xX]0*20[Aa][Cc]; |
$
)|
### Other units
(?: ° | ° | &\#0*176; | &\#[xX]0*[Bb]0; ) [CF]? |
%|pt|pi|M?px|em|en|gal|lb|[NSEOW]|[NS][EOW]|ha|mbar
'; //x
function spaceUnit($_) {
#
# Parameters: String, replacement character, and forcing flag.
# Returns: The string, with appropriates spaces replaced
# before unit symbols.
#
# Example input: Get 3 mol of fun for 3 $.
# Example output: Get 3_mol of fun for 3_$.
#
$opt = ( $this->do_space_unit == 2 ? '?' : '' );
$chr = ( $this->do_space_unit != -1 ? $this->space_unit : '' );