1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of PHP-Typography. |
4
|
|
|
* |
5
|
|
|
* Copyright 2017-2019 Peter Putzer. |
6
|
|
|
* |
7
|
|
|
* This program is free software; you can redistribute it and/or modify modify |
8
|
|
|
* it under the terms of the GNU General Public License as published by |
9
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License along |
18
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
19
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
20
|
|
|
* |
21
|
|
|
* *** |
22
|
|
|
* |
23
|
|
|
* @package mundschenk-at/php-typography |
24
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.html |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace PHP_Typography\Fixes; |
28
|
|
|
|
29
|
|
|
use PHP_Typography\Settings; |
30
|
|
|
|
31
|
|
|
use PHP_Typography\Fixes\Node_Fix; |
32
|
|
|
use PHP_Typography\Fixes\Token_Fix; |
33
|
|
|
|
34
|
|
|
use PHP_Typography\Hyphenator\Cache; |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* A registry implementation containing the default fixes for PHP_Typography. |
39
|
|
|
* |
40
|
|
|
* @author Peter Putzer <[email protected]> |
41
|
|
|
* |
42
|
|
|
* @since 6.0.0 |
43
|
|
|
*/ |
44
|
|
|
class Default_Registry extends Registry { |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* An array of CSS classes that are added for ampersands, numbers etc. |
48
|
|
|
*/ |
49
|
|
|
const DEFAULT_CSS_CLASSES = [ |
50
|
|
|
'caps' => 'caps', |
51
|
|
|
'numbers' => 'numbers', |
52
|
|
|
'amp' => 'amp', |
53
|
|
|
'quo' => 'quo', |
54
|
|
|
'dquo' => 'dquo', |
55
|
|
|
'pull-single' => 'pull-single', |
56
|
|
|
'pull-double' => 'pull-double', |
57
|
|
|
'push-single' => 'push-single', |
58
|
|
|
'push-double' => 'push-double', |
59
|
|
|
'numerator' => 'numerator', |
60
|
|
|
'denominator' => 'denominator', |
61
|
|
|
'ordinal' => 'ordinal', |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Creates new default registry instance. |
66
|
|
|
* |
67
|
|
|
* @param Cache|null $cache Optional. A hyphenatation cache instance to use. Default null. |
68
|
|
|
* @param string[] $css_classes Optional. An array of CSS classes to use. Defaults to null (i.e. use the predefined classes). |
69
|
|
|
*/ |
70
|
1 |
|
public function __construct( Cache $cache = null, array $css_classes = [] ) { |
71
|
1 |
|
parent::__construct(); |
72
|
|
|
|
73
|
1 |
|
if ( empty( $css_classes ) ) { |
74
|
1 |
|
$css_classes = self::DEFAULT_CSS_CLASSES; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Initialize node fixes. |
78
|
1 |
|
foreach ( self::get_default_node_fixes() as $group => $node_fixes ) { |
79
|
1 |
|
foreach ( $node_fixes as $fix => $params ) { |
80
|
1 |
|
$arguments = []; |
81
|
|
|
|
82
|
1 |
|
if ( ! empty( $params['classes'] ) ) { |
83
|
1 |
|
$arguments += \array_map( |
84
|
|
|
function( $index ) use ( $css_classes ) { |
85
|
1 |
|
return $css_classes[ $index ]; |
86
|
1 |
|
}, |
87
|
1 |
|
$params['classes'] |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
if ( isset( $params['feed'] ) ) { |
92
|
1 |
|
$arguments += [ $params['feed'] ]; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
$this->register_node_fix( new $fix( ...$arguments ), $group ); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Register token fixes. |
100
|
1 |
|
foreach ( self::get_default_token_fixes() as $fix => $params ) { |
101
|
1 |
|
$arguments = []; |
102
|
|
|
|
103
|
1 |
|
if ( ! empty( $params['cache'] ) ) { |
104
|
1 |
|
$arguments += [ $cache ]; |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
$this->register_token_fix( new $fix( ...$arguments ) ); |
108
|
|
|
} |
109
|
1 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns a configuration array for the default node fixes. |
113
|
|
|
* |
114
|
|
|
* @return array { |
115
|
|
|
* @type array $group { |
116
|
|
|
* A group of fixes. |
117
|
|
|
* |
118
|
|
|
* @type array $fix_class Additional parameters for the fix constructor. |
119
|
|
|
* } |
120
|
|
|
* } |
121
|
|
|
*/ |
122
|
1 |
|
protected static function get_default_node_fixes() { |
123
|
|
|
return [ |
124
|
1 |
|
self::CHARACTERS => [ |
125
|
|
|
// Nodify anything that requires adjacent text awareness here. |
126
|
1 |
|
Node_Fixes\Smart_Maths_Fix::class => [], |
127
|
|
|
Node_Fixes\Smart_Diacritics_Fix::class => [], |
128
|
|
|
Node_Fixes\Smart_Quotes_Fix::class => [ 'feed' => true ], |
129
|
|
|
Node_Fixes\Smart_Dashes_Fix::class => [ 'feed' => true ], |
130
|
|
|
Node_Fixes\Smart_Ellipses_Fix::class => [ 'feed' => true ], |
131
|
|
|
Node_Fixes\Smart_Marks_Fix::class => [ 'feed' => true ], |
132
|
|
|
Node_Fixes\Smart_Area_Units_Fix::class => [ 'feed' => true ], |
133
|
|
|
], |
134
|
|
|
|
135
|
1 |
|
self::SPACING_PRE_WORDS => [ |
136
|
|
|
// Keep spacing after smart character replacement. |
137
|
|
|
Node_Fixes\Single_Character_Word_Spacing_Fix::class => [], |
138
|
|
|
Node_Fixes\Dash_Spacing_Fix::class => [], |
139
|
|
|
Node_Fixes\Unit_Spacing_Fix::class => [], |
140
|
|
|
Node_Fixes\Numbered_Abbreviation_Spacing_Fix::class => [], |
141
|
|
|
Node_Fixes\French_Punctuation_Spacing_Fix::class => [], |
142
|
|
|
], |
143
|
|
|
|
144
|
1 |
|
self::SPACING_POST_WORDS => [ |
145
|
|
|
// Some final space manipulation. |
146
|
|
|
Node_Fixes\Dewidow_Fix::class => [], |
147
|
|
|
Node_Fixes\Space_Collapse_Fix::class => [], |
148
|
|
|
], |
149
|
|
|
|
150
|
1 |
|
self::HTML_INSERTION => [ |
151
|
|
|
// Everything that requires HTML injection occurs here (functions above assume tag-free content) |
152
|
|
|
// pay careful attention to functions below for tolerance of injected tags. |
153
|
|
|
Node_Fixes\Smart_Ordinal_Suffix_Fix::class => [ |
154
|
|
|
// call before "Style_Numbers_Fix" and "Smart_Fractions_Fix". |
155
|
|
|
'classes' => [ 'ordinal' ], |
156
|
|
|
], |
157
|
|
|
Node_Fixes\Smart_Exponents_Fix::class => [ |
158
|
|
|
// call before "Style_Numbers_Fix". |
159
|
|
|
], |
160
|
|
|
Node_Fixes\Smart_Fractions_Fix::class => [ |
161
|
|
|
// call before "Style_Numbers_Fix" and after "Smart_Ordinal_Suffix_Fix". |
162
|
|
|
'classes' => [ 'numerator', 'denominator' ], |
163
|
|
|
], |
164
|
|
|
Node_Fixes\Style_Caps_Fix::class => [ |
165
|
|
|
// Call before "Style_Numbers_Fix". |
166
|
|
|
'classes' => [ 'caps' ], |
167
|
|
|
], |
168
|
|
|
Node_Fixes\Style_Numbers_Fix::class => [ |
169
|
|
|
// Call after "Smart_Ordinal_Suffix_Fix", "Smart_Exponents_Fix", "Smart_Fractions_Fix", and "Style_Caps_Fix". |
170
|
|
|
'classes' => [ 'numbers' ], |
171
|
|
|
], |
172
|
|
|
Node_Fixes\Style_Ampersands_Fix::class => [ |
173
|
|
|
'classes' => [ 'amp' ], |
174
|
|
|
], |
175
|
|
|
Node_Fixes\Style_Initial_Quotes_Fix::class => [ |
176
|
|
|
'classes' => [ 'quo', 'dquo' ], |
177
|
|
|
], |
178
|
|
|
Node_Fixes\Style_Hanging_Punctuation_Fix::class => [ |
179
|
|
|
'classes' => [ 'push-single', 'push-double', 'pull-single', 'pull-double' ], |
180
|
|
|
], |
181
|
|
|
], |
182
|
|
|
]; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Returns a configuration array for the default token fixes. |
187
|
|
|
* |
188
|
|
|
* @return array |
189
|
|
|
*/ |
190
|
1 |
|
protected static function get_default_token_fixes() { |
191
|
|
|
return [ |
192
|
1 |
|
Token_Fixes\Wrap_Hard_Hyphens_Fix::class => [], |
193
|
|
|
Token_Fixes\Smart_Dashes_Hyphen_Fix::class => [], |
194
|
|
|
Token_Fixes\Hyphenate_Compounds_Fix::class => [ 'cache' => true ], |
195
|
|
|
Token_Fixes\Hyphenate_Fix::class => [ 'cache' => true ], |
196
|
|
|
Token_Fixes\Wrap_URLs_Fix::class => [ 'cache' => true ], |
197
|
|
|
Token_Fixes\Wrap_Emails_Fix::class => [], |
198
|
|
|
]; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|