|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package s9e\TextFormatter |
|
5
|
|
|
* @copyright Copyright (c) 2010-2017 The s9e Authors |
|
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace s9e\TextFormatter; |
|
9
|
|
|
|
|
10
|
|
|
use InvalidArgumentException; |
|
11
|
|
|
use RuntimeException; |
|
12
|
|
|
use s9e\TextFormatter\Configurator\BundleGenerator; |
|
13
|
|
|
use s9e\TextFormatter\Configurator\Collections\AttributeFilterCollection; |
|
14
|
|
|
use s9e\TextFormatter\Configurator\Collections\PluginCollection; |
|
15
|
|
|
use s9e\TextFormatter\Configurator\Collections\Ruleset; |
|
16
|
|
|
use s9e\TextFormatter\Configurator\Collections\TagCollection; |
|
17
|
|
|
use s9e\TextFormatter\Configurator\ConfigProvider; |
|
18
|
|
|
use s9e\TextFormatter\Configurator\Helpers\ConfigHelper; |
|
19
|
|
|
use s9e\TextFormatter\Configurator\Helpers\RulesHelper; |
|
20
|
|
|
use s9e\TextFormatter\Configurator\JavaScript; |
|
21
|
|
|
use s9e\TextFormatter\Configurator\JavaScript\Dictionary; |
|
22
|
|
|
use s9e\TextFormatter\Configurator\Rendering; |
|
23
|
|
|
use s9e\TextFormatter\Configurator\RulesGenerator; |
|
24
|
|
|
use s9e\TextFormatter\Configurator\TemplateChecker; |
|
25
|
|
|
use s9e\TextFormatter\Configurator\TemplateNormalizer; |
|
26
|
|
|
use s9e\TextFormatter\Configurator\UrlConfig; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @property Plugins\Autoemail\Configurator $Autoemail Autoemail plugin's configurator |
|
30
|
|
|
* @property Plugins\Autoimage\Configurator $Autolink Autoimage plugin's configurator |
|
31
|
|
|
* @property Plugins\Autolink\Configurator $Autolink Autolink plugin's configurator |
|
32
|
|
|
* @property Plugins\Autovideo\Configurator $Autovideo Autovideo plugin's configurator |
|
33
|
|
|
* @property Plugins\BBCodes\Configurator $BBCodes BBCodes plugin's configurator |
|
34
|
|
|
* @property Plugins\Censor\Configurator $Censor Censor plugin's configurator |
|
35
|
|
|
* @property Plugins\Emoji\Configurator $Emoji Emoji plugin's configurator |
|
36
|
|
|
* @property Plugins\Emoticons\Configurator $Emoticons Emoticons plugin's configurator |
|
37
|
|
|
* @property Plugins\Escaper\Configurator $Escaper Escaper plugin's configurator |
|
38
|
|
|
* @property Plugins\FancyPants\Configurator $FancyPants FancyPants plugin's configurator |
|
39
|
|
|
* @property Plugins\HTMLComments\Configurator $HTMLComments HTMLComments plugin's configurator |
|
40
|
|
|
* @property Plugins\HTMLElements\Configurator $HTMLElements HTMLElements plugin's configurator |
|
41
|
|
|
* @property Plugins\HTMLEntities\Configurator $HTMLEntities HTMLEntities plugin's configurator |
|
42
|
|
|
* @property Plugins\Keywords\Configurator $Keywords Keywords plugin's configurator |
|
43
|
|
|
* @property Plugins\Litedown\Configurator $Litedown Litedown plugin's configurator |
|
44
|
|
|
* @property Plugins\MediaEmbed\Configurator $MediaEmbed MediaEmbed plugin's configurator |
|
45
|
|
|
* @property Plugins\PipeTables\Configurator $PipeTables PipeTables plugin's configurator |
|
46
|
|
|
* @property Plugins\Preg\Configurator $Preg Preg plugin's configurator |
|
47
|
|
|
* @property UrlConfig $urlConfig Default URL config |
|
48
|
|
|
*/ |
|
49
|
|
|
class Configurator implements ConfigProvider |
|
50
|
|
|
{ |
|
51
|
|
|
/** |
|
52
|
|
|
* @var AttributeFilterCollection Dynamically-populated collection of AttributeFilter instances |
|
53
|
|
|
*/ |
|
54
|
|
|
public $attributeFilters; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var BundleGenerator Default bundle generator |
|
58
|
|
|
*/ |
|
59
|
|
|
public $bundleGenerator; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var JavaScript JavaScript manipulation object |
|
63
|
|
|
*/ |
|
64
|
|
|
public $javascript; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var PluginCollection Loaded plugins |
|
68
|
|
|
*/ |
|
69
|
|
|
public $plugins; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @var array Array of variables that are available to the filters during parsing |
|
73
|
|
|
*/ |
|
74
|
|
|
public $registeredVars; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @var Rendering Rendering configuration |
|
78
|
|
|
*/ |
|
79
|
|
|
public $rendering; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @var Ruleset Rules that apply at the root of the text |
|
83
|
|
|
*/ |
|
84
|
|
|
public $rootRules; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @var RulesGenerator Generator used by $this->getRenderer() |
|
88
|
|
|
*/ |
|
89
|
|
|
public $rulesGenerator; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @var TagCollection Tags repository |
|
93
|
|
|
*/ |
|
94
|
|
|
public $tags; |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @var TemplateChecker Default template checker |
|
98
|
|
|
*/ |
|
99
|
|
|
public $templateChecker; |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @var TemplateNormalizer Default template normalizer |
|
103
|
|
|
*/ |
|
104
|
|
|
public $templateNormalizer; |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Constructor |
|
108
|
|
|
* |
|
109
|
|
|
* Prepares the collections that hold tags and filters, the UrlConfig object as well as the |
|
110
|
|
|
* various helpers required to generate a full config. |
|
111
|
|
|
*/ |
|
112
|
45 |
|
public function __construct() |
|
113
|
|
|
{ |
|
114
|
45 |
|
$this->attributeFilters = new AttributeFilterCollection; |
|
115
|
45 |
|
$this->bundleGenerator = new BundleGenerator($this); |
|
116
|
45 |
|
$this->plugins = new PluginCollection($this); |
|
117
|
45 |
|
$this->registeredVars = ['urlConfig' => new UrlConfig]; |
|
118
|
45 |
|
$this->rendering = new Rendering($this); |
|
119
|
45 |
|
$this->rootRules = new Ruleset; |
|
120
|
45 |
|
$this->rulesGenerator = new RulesGenerator; |
|
121
|
45 |
|
$this->tags = new TagCollection; |
|
122
|
45 |
|
$this->templateChecker = new TemplateChecker; |
|
123
|
45 |
|
$this->templateNormalizer = new TemplateNormalizer; |
|
124
|
45 |
|
} |
|
125
|
|
|
|
|
126
|
|
|
//========================================================================== |
|
127
|
|
|
// Magic methods |
|
128
|
|
|
//========================================================================== |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Magic __get automatically loads plugins, returns registered vars |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $k Property name |
|
134
|
|
|
* @return mixed |
|
135
|
|
|
*/ |
|
136
|
6 |
|
public function __get($k) |
|
137
|
|
|
{ |
|
138
|
6 |
|
if (preg_match('#^[A-Z][A-Za-z_0-9]+$#D', $k)) |
|
139
|
6 |
|
{ |
|
140
|
4 |
|
return (isset($this->plugins[$k])) |
|
141
|
4 |
|
? $this->plugins[$k] |
|
142
|
4 |
|
: $this->plugins->load($k); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
2 |
|
if (isset($this->registeredVars[$k])) |
|
146
|
2 |
|
{ |
|
147
|
1 |
|
return $this->registeredVars[$k]; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
1 |
|
throw new RuntimeException("Undefined property '" . __CLASS__ . '::$' . $k . "'"); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Magic __isset checks existence in the plugins collection and registered vars |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $k Property name |
|
157
|
|
|
* @return bool |
|
158
|
|
|
*/ |
|
159
|
5 |
|
public function __isset($k) |
|
160
|
|
|
{ |
|
161
|
5 |
|
if (preg_match('#^[A-Z][A-Za-z_0-9]+$#D', $k)) |
|
162
|
5 |
|
{ |
|
163
|
2 |
|
return isset($this->plugins[$k]); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
3 |
|
return isset($this->registeredVars[$k]); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Magic __set adds to the plugins collection, registers vars |
|
171
|
|
|
* |
|
172
|
|
|
* @param string $k Property name |
|
173
|
|
|
* @param mixed $v Property value |
|
174
|
|
|
* @return mixed |
|
175
|
|
|
*/ |
|
176
|
2 |
|
public function __set($k, $v) |
|
177
|
|
|
{ |
|
178
|
2 |
|
if (preg_match('#^[A-Z][A-Za-z_0-9]+$#D', $k)) |
|
179
|
2 |
|
{ |
|
180
|
1 |
|
$this->plugins[$k] = $v; |
|
181
|
1 |
|
} |
|
182
|
|
|
else |
|
183
|
|
|
{ |
|
184
|
1 |
|
$this->registeredVars[$k] = $v; |
|
185
|
|
|
} |
|
186
|
2 |
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Magic __set removes plugins from the plugins collection, unregisters vars |
|
190
|
|
|
* |
|
191
|
|
|
* @param string $k Property name |
|
192
|
|
|
* @return mixed |
|
193
|
|
|
*/ |
|
194
|
2 |
|
public function __unset($k) |
|
195
|
|
|
{ |
|
196
|
2 |
|
if (preg_match('#^[A-Z][A-Za-z_0-9]+$#D', $k)) |
|
197
|
2 |
|
{ |
|
198
|
1 |
|
unset($this->plugins[$k]); |
|
199
|
1 |
|
} |
|
200
|
|
|
else |
|
201
|
|
|
{ |
|
202
|
1 |
|
unset($this->registeredVars[$k]); |
|
203
|
|
|
} |
|
204
|
2 |
|
} |
|
205
|
|
|
|
|
206
|
|
|
//========================================================================== |
|
207
|
|
|
// API |
|
208
|
|
|
//========================================================================== |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Enable the creation of a JavaScript parser |
|
212
|
|
|
* |
|
213
|
|
|
* @return void |
|
214
|
|
|
*/ |
|
215
|
3 |
|
public function enableJavaScript() |
|
216
|
|
|
{ |
|
217
|
3 |
|
if (!isset($this->javascript)) |
|
218
|
3 |
|
{ |
|
219
|
3 |
|
$this->javascript = new JavaScript($this); |
|
220
|
3 |
|
} |
|
221
|
3 |
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Finalize this configuration and return all the relevant objects |
|
225
|
|
|
* |
|
226
|
|
|
* @return array One "parser" element and one "renderer" element unless specified otherwise |
|
227
|
|
|
*/ |
|
228
|
11 |
|
public function finalize() |
|
229
|
|
|
{ |
|
230
|
11 |
|
$return = []; |
|
231
|
|
|
|
|
232
|
|
|
// Finalize the plugins' config |
|
233
|
11 |
|
$this->plugins->finalize(); |
|
234
|
|
|
|
|
235
|
|
|
// Normalize the tags' templates |
|
236
|
11 |
|
foreach ($this->tags as $tag) |
|
237
|
|
|
{ |
|
238
|
5 |
|
$this->templateNormalizer->normalizeTag($tag); |
|
239
|
11 |
|
} |
|
240
|
|
|
|
|
241
|
|
|
// Create a renderer |
|
242
|
11 |
|
$return['renderer'] = $this->rendering->getRenderer(); |
|
243
|
|
|
|
|
244
|
|
|
// Add the generated tag rules |
|
245
|
11 |
|
$this->addTagRules(); |
|
246
|
|
|
|
|
247
|
|
|
// Prepare the parser config |
|
248
|
11 |
|
$config = $this->asConfig(); |
|
249
|
11 |
|
if (isset($this->javascript)) |
|
250
|
11 |
|
{ |
|
251
|
1 |
|
$return['js'] = $this->javascript->getParser(ConfigHelper::filterConfig($config, 'JS')); |
|
252
|
1 |
|
} |
|
253
|
|
|
|
|
254
|
|
|
// Remove JS-specific data from the config |
|
255
|
11 |
|
$config = ConfigHelper::filterConfig($config, 'PHP'); |
|
256
|
11 |
|
ConfigHelper::optimizeArray($config); |
|
257
|
|
|
|
|
258
|
|
|
// Create a parser |
|
259
|
11 |
|
$return['parser'] = new Parser($config); |
|
260
|
|
|
|
|
261
|
11 |
|
return $return; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Load a bundle into this configuration |
|
266
|
|
|
* |
|
267
|
|
|
* @param string $bundleName Name of the bundle |
|
268
|
|
|
* @return void |
|
269
|
|
|
*/ |
|
270
|
2 |
|
public function loadBundle($bundleName) |
|
271
|
|
|
{ |
|
272
|
2 |
|
if (!preg_match('#^[A-Z][A-Za-z0-9]+$#D', $bundleName)) |
|
273
|
2 |
|
{ |
|
274
|
1 |
|
throw new InvalidArgumentException("Invalid bundle name '" . $bundleName . "'"); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
1 |
|
$className = __CLASS__ . '\\Bundles\\' . $bundleName; |
|
278
|
|
|
|
|
279
|
1 |
|
$bundle = new $className; |
|
280
|
1 |
|
$bundle->configure($this); |
|
281
|
1 |
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Create and save a bundle based on this configuration |
|
285
|
|
|
* |
|
286
|
|
|
* @param string $className Name of the bundle class |
|
287
|
|
|
* @param string $filepath Path where to save the bundle file |
|
288
|
|
|
* @param array $options Options passed to the bundle generator |
|
289
|
|
|
* @return bool Whether the write succeeded |
|
290
|
|
|
*/ |
|
291
|
3 |
|
public function saveBundle($className, $filepath, array $options = []) |
|
292
|
|
|
{ |
|
293
|
3 |
|
$file = "<?php\n\n" . $this->bundleGenerator->generate($className, $options); |
|
294
|
|
|
|
|
295
|
3 |
|
return (file_put_contents($filepath, $file) !== false); |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* Generate and return the complete config array |
|
300
|
|
|
* |
|
301
|
|
|
* @return array |
|
302
|
|
|
*/ |
|
303
|
20 |
|
public function asConfig() |
|
304
|
|
|
{ |
|
305
|
|
|
// Finalize the plugins' config |
|
306
|
20 |
|
$this->plugins->finalize(); |
|
307
|
|
|
|
|
308
|
|
|
// Remove properties that shouldn't be turned into config arrays |
|
309
|
20 |
|
$properties = get_object_vars($this); |
|
310
|
20 |
|
unset($properties['attributeFilters']); |
|
311
|
20 |
|
unset($properties['bundleGenerator']); |
|
312
|
20 |
|
unset($properties['javascript']); |
|
313
|
20 |
|
unset($properties['rendering']); |
|
314
|
20 |
|
unset($properties['rulesGenerator']); |
|
315
|
20 |
|
unset($properties['registeredVars']); |
|
316
|
20 |
|
unset($properties['templateChecker']); |
|
317
|
20 |
|
unset($properties['templateNormalizer']); |
|
318
|
20 |
|
unset($properties['stylesheet']); |
|
319
|
|
|
|
|
320
|
|
|
// Create the config array |
|
321
|
20 |
|
$config = ConfigHelper::toArray($properties); |
|
322
|
20 |
|
$bitfields = RulesHelper::getBitfields($this->tags, $this->rootRules); |
|
323
|
|
|
|
|
324
|
|
|
// Save the root context |
|
325
|
20 |
|
$config['rootContext'] = $bitfields['root']; |
|
326
|
20 |
|
$config['rootContext']['flags'] = $config['rootRules']['flags']; |
|
327
|
|
|
|
|
328
|
|
|
// Save the registered vars (including the empty ones) |
|
329
|
20 |
|
$config['registeredVars'] = ConfigHelper::toArray($this->registeredVars, true); |
|
330
|
|
|
|
|
331
|
|
|
// Make sure those keys exist even if they're empty |
|
332
|
|
|
$config += [ |
|
333
|
20 |
|
'plugins' => [], |
|
334
|
20 |
|
'tags' => [] |
|
335
|
20 |
|
]; |
|
336
|
|
|
|
|
337
|
|
|
// Remove unused tags |
|
338
|
20 |
|
$config['tags'] = array_intersect_key($config['tags'], $bitfields['tags']); |
|
339
|
|
|
|
|
340
|
|
|
// Add the bitfield information to each tag |
|
341
|
20 |
|
foreach ($bitfields['tags'] as $tagName => $tagBitfields) |
|
342
|
|
|
{ |
|
343
|
6 |
|
$config['tags'][$tagName] += $tagBitfields; |
|
344
|
20 |
|
} |
|
345
|
|
|
|
|
346
|
|
|
// Remove unused entries |
|
347
|
20 |
|
unset($config['rootRules']); |
|
348
|
|
|
|
|
349
|
20 |
|
return $config; |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
/** |
|
353
|
|
|
* Add the rules generated by $this->rulesGenerator |
|
354
|
|
|
* |
|
355
|
|
|
* @return void |
|
356
|
|
|
*/ |
|
357
|
11 |
|
protected function addTagRules() |
|
358
|
|
|
{ |
|
359
|
|
|
// Get the rules |
|
360
|
11 |
|
$rules = $this->rulesGenerator->getRules($this->tags); |
|
361
|
|
|
|
|
362
|
|
|
// Add the rules pertaining to the root |
|
363
|
11 |
|
$this->rootRules->merge($rules['root'], false); |
|
364
|
|
|
|
|
365
|
|
|
// Add the rules pertaining to each tag |
|
366
|
11 |
|
foreach ($rules['tags'] as $tagName => $tagRules) |
|
367
|
|
|
{ |
|
368
|
5 |
|
$this->tags[$tagName]->rules->merge($tagRules, false); |
|
369
|
11 |
|
} |
|
370
|
|
|
} |
|
371
|
|
|
} |