1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of PHP-Typography. |
4
|
|
|
* |
5
|
|
|
* Copyright 2014-2019 Peter Putzer. |
6
|
|
|
* Copyright 2009-2011 KINGdesk, LLC. |
7
|
|
|
* |
8
|
|
|
* This program is free software; you can redistribute it and/or modify modify |
9
|
|
|
* it under the terms of the GNU General Public License as published by |
10
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License along |
19
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
20
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
21
|
|
|
* |
22
|
|
|
* *** |
23
|
|
|
* |
24
|
|
|
* @package mundschenk-at/php-typography |
25
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.html |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
namespace PHP_Typography\Fixes\Node_Fixes; |
29
|
|
|
|
30
|
|
|
use PHP_Typography\DOM; |
31
|
|
|
use PHP_Typography\RE; |
32
|
|
|
use PHP_Typography\Settings; |
33
|
|
|
use PHP_Typography\U; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Applies smart dashes (if enabled). |
37
|
|
|
* |
38
|
|
|
* @author Peter Putzer <[email protected]> |
39
|
|
|
* |
40
|
|
|
* @since 5.0.0 |
41
|
|
|
*/ |
42
|
|
|
class Smart_Dashes_Fix extends Abstract_Node_Fix { |
43
|
|
|
|
44
|
|
|
// Standard dashes. |
45
|
|
|
const PARENTHETICAL_DOUBLE_DASH = '/(\s|' . RE::HTML_SPACES . ')--(\s|' . RE::HTML_SPACES . ')/Sxui'; // ' -- '. |
46
|
|
|
const PARENTHETICAL_SINGLE_DASH = '/(\s|' . RE::HTML_SPACES . ')-(\s|' . RE::HTML_SPACES . ')/Sxui'; // ' - '. |
47
|
|
|
const EN_DASH_WORDS = '/([\w])\-(' . U::THIN_SPACE . '|' . U::HAIR_SPACE . '|' . U::NO_BREAK_NARROW_SPACE . '|' . U::NO_BREAK_SPACE . ')/Su'; |
48
|
|
|
const EN_DASH_NUMBERS = "/(\b\d+(\.?))\-(\d+\\2)/S"; |
49
|
|
|
const EN_DASH_PHONE_NUMBERS = "/(\b\d{3})" . U::EN_DASH . "(\d{4}\b)/S"; |
50
|
|
|
const NO_BREAK_HYPHEN = "/ |
51
|
|
|
(?| |
52
|
|
|
# Elision at the beginning of a word |
53
|
|
|
(\s)\-(\w) | |
54
|
|
|
|
55
|
|
|
# Single letter before the hyphen. |
56
|
|
|
(?<!\-)\b(\w)\-(\w) | |
57
|
|
|
|
58
|
|
|
# Single letter after the hyphen, or a comma. |
59
|
|
|
(\w)\-(\w\b|,)(?!\-) |
60
|
|
|
) |
61
|
|
|
/Sux"; |
62
|
|
|
|
63
|
|
|
// Date handling. |
64
|
|
|
const DATE_YYYY_MM_DD = '/ |
65
|
|
|
( |
66
|
|
|
(?<=\s|\A|' . U::NO_BREAK_SPACE . ') |
67
|
|
|
[12][0-9]{3} |
68
|
|
|
) |
69
|
|
|
[\-' . U::EN_DASH . '] |
70
|
|
|
( |
71
|
|
|
(?:[0][1-9]|[1][0-2]) |
72
|
|
|
) |
73
|
|
|
[\-' . U::EN_DASH . "] |
74
|
|
|
( |
75
|
|
|
(?:[0][1-9]|[12][0-9]|[3][0-1]) |
76
|
|
|
(?=\s|\Z|\)|\]|\.|\,|\?|\;|\:|\'|\"|\!|" . U::NO_BREAK_SPACE . ') |
77
|
|
|
) |
78
|
|
|
/xu'; |
79
|
|
|
|
80
|
|
|
const DATE_MM_DD_YYYY = '/ |
81
|
|
|
(?: |
82
|
|
|
(?: |
83
|
|
|
( |
84
|
|
|
(?<=\s|\A|' . U::NO_BREAK_SPACE . ') |
85
|
|
|
(?:[0]?[1-9]|[1][0-2]) |
86
|
|
|
) |
87
|
|
|
[\-' . U::EN_DASH . '] |
88
|
|
|
( |
89
|
|
|
(?:[0]?[1-9]|[12][0-9]|[3][0-1]) |
90
|
|
|
) |
91
|
|
|
) |
92
|
|
|
| |
93
|
|
|
(?: |
94
|
|
|
( |
95
|
|
|
(?<=\s|\A|' . U::NO_BREAK_SPACE . ') |
96
|
|
|
(?:[0]?[1-9]|[12][0-9]|[3][0-1]) |
97
|
|
|
) |
98
|
|
|
[\-' . U::EN_DASH . '] |
99
|
|
|
( |
100
|
|
|
(?:[0]?[1-9]|[1][0-2]) |
101
|
|
|
) |
102
|
|
|
) |
103
|
|
|
) |
104
|
|
|
[\-' . U::EN_DASH . "] |
105
|
|
|
( |
106
|
|
|
[12][0-9]{3} |
107
|
|
|
(?=\s|\Z|\)|\]|\.|\,|\?|\;|\:|\'|\"|\!|" . U::NO_BREAK_SPACE . ') |
108
|
|
|
) |
109
|
|
|
/xu'; |
110
|
|
|
|
111
|
|
|
const DATE_YYYY_MM = '/ |
112
|
|
|
( |
113
|
|
|
(?<=\s|\A|' . U::NO_BREAK_SPACE . ') |
114
|
|
|
[12][0-9]{3} |
115
|
|
|
) |
116
|
|
|
[\-' . U::EN_DASH . "] |
117
|
|
|
( |
118
|
|
|
(?: |
119
|
|
|
(?:[0][1-9]|[1][0-2]) |
120
|
|
|
| |
121
|
|
|
(?:[0][0-9][1-9]|[1-2][0-9]{2}|[3][0-5][0-9]|[3][6][0-6]) |
122
|
|
|
) |
123
|
|
|
(?=\s|\Z|\)|\]|\.|\,|\?|\;|\:|\'|\"|\!|" . U::NO_BREAK_SPACE . ') |
124
|
|
|
) |
125
|
|
|
/xu'; |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Apply the fix to a given textnode. |
129
|
|
|
* |
130
|
|
|
* @param \DOMText $textnode Required. |
131
|
|
|
* @param Settings $settings Required. |
132
|
|
|
* @param bool $is_title Optional. Default false. |
133
|
|
|
*/ |
134
|
24 |
|
public function apply( \DOMText $textnode, Settings $settings, $is_title = false ) { |
135
|
24 |
|
if ( empty( $settings[ Settings::SMART_DASHES ] ) ) { |
136
|
12 |
|
return; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// Various special characters and regular expressions. |
140
|
12 |
|
$s = $settings->dash_style(); |
141
|
|
|
|
142
|
|
|
// Cache textnode content. |
143
|
12 |
|
$node_data = $textnode->data; |
144
|
|
|
|
145
|
12 |
|
$node_data = \str_replace( '---', U::EM_DASH, $node_data ); |
146
|
12 |
|
$node_data = \preg_replace( self::PARENTHETICAL_DOUBLE_DASH, "\$1{$s->parenthetical_dash()}\$2", $node_data ); |
147
|
12 |
|
$node_data = \str_replace( '--', U::EN_DASH, $node_data ); |
148
|
|
|
|
149
|
12 |
|
$node_data = \preg_replace( |
150
|
|
|
[ |
151
|
12 |
|
self::PARENTHETICAL_SINGLE_DASH, |
152
|
12 |
|
self::EN_DASH_WORDS, |
153
|
12 |
|
self::EN_DASH_NUMBERS, |
154
|
12 |
|
self::EN_DASH_PHONE_NUMBERS, |
155
|
12 |
|
self::NO_BREAK_HYPHEN, |
156
|
|
|
], |
157
|
|
|
[ |
158
|
12 |
|
"\$1{$s->parenthetical_dash()}\$2", |
159
|
|
|
'$1' . U::EN_DASH . '$2', |
160
|
12 |
|
"\$1{$s->interval_dash()}\$3", |
161
|
|
|
'$1' . U::NO_BREAK_HYPHEN . '$2', |
162
|
|
|
'$1' . U::NO_BREAK_HYPHEN . '$2', |
163
|
|
|
], |
164
|
12 |
|
$node_data |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
// Revert messed-up punycode. |
168
|
12 |
|
$node_data = \str_replace( 'xn' . U::EN_DASH, 'xn--', $node_data ); |
169
|
|
|
|
170
|
|
|
// Revert dates back to original formats. |
171
|
12 |
|
$node_data = \preg_replace( |
172
|
|
|
[ |
173
|
12 |
|
self::DATE_YYYY_MM_DD, // YYYY-MM-DD. |
174
|
12 |
|
self::DATE_MM_DD_YYYY, // MM-DD-YYYY or DD-MM-YYYY. |
175
|
12 |
|
self::DATE_YYYY_MM, // YYYY-MM or YYYY-DDDD. |
176
|
|
|
], |
177
|
|
|
[ |
178
|
12 |
|
'$1-$2-$3', |
179
|
|
|
'$1$3-$2$4-$5', |
180
|
|
|
'$1-$2', |
181
|
|
|
], |
182
|
12 |
|
$node_data |
183
|
|
|
); |
184
|
|
|
|
185
|
|
|
// Restore textnode content. |
186
|
12 |
|
$textnode->data = $node_data; |
187
|
12 |
|
} |
188
|
|
|
} |
189
|
|
|
|