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\Settings; |
32
|
|
|
use PHP_Typography\U; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Applies spacing around dashes (if enabled). |
36
|
|
|
* |
37
|
|
|
* @author Peter Putzer <[email protected]> |
38
|
|
|
* |
39
|
|
|
* @since 5.0.0 |
40
|
|
|
*/ |
41
|
|
|
class Dash_Spacing_Fix extends Abstract_Node_Fix { |
42
|
|
|
|
43
|
|
|
// Mandatory UTF-8 modifier. |
44
|
|
|
const EM_DASH_SPACING = '/ |
45
|
|
|
(?: |
46
|
|
|
\s |
47
|
|
|
(' . U::EM_DASH . ') |
48
|
|
|
\s |
49
|
|
|
) |
50
|
|
|
| |
51
|
|
|
(?: |
52
|
|
|
(?<=\S) # lookbehind assertion |
53
|
|
|
(' . U::EM_DASH . ') |
54
|
|
|
(?=\S) # lookahead assertion |
55
|
|
|
) |
56
|
|
|
/xu'; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Regular expression matching cached parenthetical dash. |
60
|
|
|
* |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $parenthetical_dash_spacing; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Regular expression matching cached interval dash. |
67
|
|
|
* |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
protected $interval_dash_spacing; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Replacement pattern for em-dash spacing. |
74
|
|
|
* |
75
|
|
|
* @var string |
76
|
|
|
*/ |
77
|
|
|
protected $em_dash_replacement; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Replacement pattern for parenthetical dash spacing. |
81
|
|
|
* |
82
|
|
|
* @var string |
83
|
|
|
*/ |
84
|
|
|
protected $parenthetical_dash_replacement; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Replacement pattern for interval dash spacing. |
88
|
|
|
* |
89
|
|
|
* @var string |
90
|
|
|
*/ |
91
|
|
|
protected $interval_dash_replacement; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Cached dashes style. |
95
|
|
|
* |
96
|
|
|
* @var \PHP_Typography\Settings\Dashes|null |
97
|
|
|
*/ |
98
|
|
|
protected $cached_dash_style; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Apply the fix to a given textnode. |
102
|
|
|
* |
103
|
|
|
* @param \DOMText $textnode Required. |
104
|
|
|
* @param Settings $settings Required. |
105
|
|
|
* @param bool $is_title Optional. Default false. |
106
|
|
|
*/ |
107
|
22 |
|
public function apply( \DOMText $textnode, Settings $settings, $is_title = false ) { |
108
|
22 |
|
if ( empty( $settings[ Settings::DASH_SPACING ] ) ) { |
109
|
9 |
|
return; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Various special characters and regular expressions. |
113
|
13 |
|
$s = $settings->dash_style(); |
114
|
|
|
|
115
|
13 |
|
if ( $s != $this->cached_dash_style ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison -- object value comparison. |
116
|
13 |
|
$this->update_dash_spacing_regex( $s->parenthetical_dash(), $s->parenthetical_space(), $s->interval_dash(), $s->interval_space() ); |
117
|
13 |
|
$this->cached_dash_style = $s; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// Cache $textnode->data for this fix. |
121
|
13 |
|
$node_data = $textnode->data; |
122
|
|
|
|
123
|
13 |
|
$node_data = \preg_replace( self::EM_DASH_SPACING, $this->em_dash_replacement, $node_data ); |
124
|
13 |
|
$node_data = \preg_replace( $this->parenthetical_dash_spacing, $this->parenthetical_dash_replacement, $node_data ); |
125
|
13 |
|
$node_data = \preg_replace( $this->interval_dash_spacing, $this->interval_dash_replacement, $node_data ); |
126
|
|
|
|
127
|
|
|
// Restore textnode content. |
128
|
13 |
|
$textnode->data = $node_data; |
129
|
13 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Update the dash spacing regular expression. |
133
|
|
|
* |
134
|
|
|
* @param string $parenthetical The dash character used for parenthetical dashes. |
135
|
|
|
* @param string $parenthetical_space The space character used around parenthetical dashes. |
136
|
|
|
* @param string $interval The dash character used for interval dashes. |
137
|
|
|
* @param string $interval_space The space character used around interval dashes. |
138
|
|
|
*/ |
139
|
9 |
|
private function update_dash_spacing_regex( $parenthetical, $parenthetical_space, $interval, $interval_space ) { |
140
|
|
|
// Mandatory UTF-8 modifier. |
141
|
9 |
|
$this->parenthetical_dash_spacing = "/ |
142
|
|
|
(?: |
143
|
|
|
\s |
144
|
9 |
|
({$parenthetical}) |
145
|
|
|
\s |
146
|
|
|
) |
147
|
|
|
/xu"; |
148
|
|
|
|
149
|
|
|
// Mandatory UTF-8 modifier. |
150
|
9 |
|
$this->interval_dash_spacing = "/ |
151
|
|
|
(?: |
152
|
|
|
(?<=\S) # lookbehind assertion |
153
|
9 |
|
({$interval}) |
154
|
|
|
(?=\S) # lookahead assertion |
155
|
|
|
) |
156
|
|
|
/xu"; |
157
|
|
|
|
158
|
9 |
|
$this->em_dash_replacement = "{$interval_space}\$1\$2{$interval_space}"; // is this correct? |
159
|
9 |
|
$this->parenthetical_dash_replacement = "{$parenthetical_space}\$1\$2{$parenthetical_space}"; |
160
|
9 |
|
$this->interval_dash_replacement = "{$interval_space}\$1\$2{$interval_space}"; |
161
|
9 |
|
} |
162
|
|
|
} |
163
|
|
|
|