1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the league/commonmark package. |
5
|
|
|
* |
6
|
|
|
* (c) Colin O'Dell <[email protected]> |
7
|
|
|
* |
8
|
|
|
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js) |
9
|
|
|
* - (c) John MacFarlane |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace League\CommonMark; |
16
|
|
|
|
17
|
|
|
use League\CommonMark\Block\Parser\BlockParserInterface; |
18
|
|
|
use League\CommonMark\Block\Renderer\BlockRendererInterface; |
19
|
|
|
use League\CommonMark\Delimiter\Processor\DelimiterProcessorCollection; |
20
|
|
|
use League\CommonMark\Delimiter\Processor\DelimiterProcessorInterface; |
21
|
|
|
use League\CommonMark\Extension\CommonMarkCoreExtension; |
22
|
|
|
use League\CommonMark\Extension\ExtensionInterface; |
23
|
|
|
use League\CommonMark\Inline\Parser\InlineParserInterface; |
24
|
|
|
use League\CommonMark\Inline\Renderer\InlineRendererInterface; |
25
|
|
|
use League\CommonMark\Util\Configuration; |
26
|
|
|
use League\CommonMark\Util\ConfigurationAwareInterface; |
27
|
|
|
use League\CommonMark\Util\PrioritizedList; |
28
|
|
|
|
29
|
|
|
final class Environment implements EnvironmentInterface, ConfigurableEnvironmentInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var ExtensionInterface[] |
33
|
|
|
*/ |
34
|
|
|
private $extensions = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ExtensionInterface[] |
38
|
|
|
*/ |
39
|
|
|
private $uninitializedExtensions = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
private $extensionsInitialized = false; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var PrioritizedList<BlockParserInterface> |
48
|
|
|
*/ |
49
|
|
|
private $blockParsers; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var PrioritizedList<InlineParserInterface> |
53
|
|
|
*/ |
54
|
|
|
private $inlineParsers; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var array<string, PrioritizedList<InlineParserInterface>> |
58
|
|
|
*/ |
59
|
|
|
private $inlineParsersByCharacter = []; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var PrioritizedList<DocumentProcessorInterface> |
63
|
|
|
*/ |
64
|
|
|
private $documentProcessors; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var DelimiterProcessorCollection |
68
|
|
|
*/ |
69
|
|
|
private $delimiterProcessors; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var array<string, PrioritizedList<BlockRendererInterface>> |
73
|
|
|
*/ |
74
|
|
|
private $blockRenderersByClass = []; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var array<string, PrioritizedList<InlineRendererInterface>> |
78
|
|
|
*/ |
79
|
|
|
private $inlineRenderersByClass = []; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var Configuration |
83
|
|
|
*/ |
84
|
|
|
private $config; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var string |
88
|
|
|
*/ |
89
|
|
|
private $inlineParserCharacterRegex; |
90
|
|
|
|
91
|
2139 |
|
public function __construct(array $config = []) |
92
|
|
|
{ |
93
|
2139 |
|
$this->config = new Configuration($config); |
94
|
|
|
|
95
|
2139 |
|
$this->blockParsers = new PrioritizedList(); |
96
|
2139 |
|
$this->inlineParsers = new PrioritizedList(); |
97
|
2139 |
|
$this->documentProcessors = new PrioritizedList(); |
98
|
2139 |
|
$this->delimiterProcessors = new DelimiterProcessorCollection(); |
99
|
2139 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
2040 |
|
public function mergeConfig(array $config = []) |
105
|
|
|
{ |
106
|
2040 |
|
$this->assertUninitialized('Failed to modify configuration.'); |
107
|
|
|
|
108
|
2037 |
|
$this->config->mergeConfig($config); |
109
|
2037 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
6 |
|
public function setConfig(array $config = []) |
115
|
|
|
{ |
116
|
6 |
|
$this->assertUninitialized('Failed to modify configuration.'); |
117
|
|
|
|
118
|
3 |
|
$this->config->setConfig($config); |
119
|
3 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritdoc} |
123
|
|
|
*/ |
124
|
2046 |
|
public function getConfig($key = null, $default = null) |
125
|
|
|
{ |
126
|
2046 |
|
return $this->config->getConfig($key, $default); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
2040 |
|
public function addBlockParser(BlockParserInterface $parser, int $priority = 0): ConfigurableEnvironmentInterface |
133
|
|
|
{ |
134
|
2040 |
|
$this->assertUninitialized('Failed to add block parser.'); |
135
|
|
|
|
136
|
2037 |
|
$this->blockParsers->add($parser, $priority); |
137
|
2037 |
|
$this->injectEnvironmentAndConfigurationIfNeeded($parser); |
138
|
|
|
|
139
|
2037 |
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
2040 |
|
public function addInlineParser(InlineParserInterface $parser, int $priority = 0): ConfigurableEnvironmentInterface |
146
|
|
|
{ |
147
|
2040 |
|
$this->assertUninitialized('Failed to add inline parser.'); |
148
|
|
|
|
149
|
2037 |
|
$this->inlineParsers->add($parser, $priority); |
150
|
2037 |
|
$this->injectEnvironmentAndConfigurationIfNeeded($parser); |
151
|
|
|
|
152
|
2037 |
|
foreach ($parser->getCharacters() as $character) { |
153
|
2037 |
|
if (!isset($this->inlineParsersByCharacter[$character])) { |
154
|
2037 |
|
$this->inlineParsersByCharacter[$character] = new PrioritizedList(); |
155
|
|
|
} |
156
|
|
|
|
157
|
2037 |
|
$this->inlineParsersByCharacter[$character]->add($parser, $priority); |
158
|
|
|
} |
159
|
|
|
|
160
|
2037 |
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritdoc} |
165
|
|
|
*/ |
166
|
2034 |
|
public function addDelimiterProcessor(DelimiterProcessorInterface $processor): ConfigurableEnvironmentInterface |
167
|
|
|
{ |
168
|
2034 |
|
$this->assertUninitialized('Failed to add delimiter processor.'); |
169
|
2031 |
|
$this->delimiterProcessors->add($processor); |
170
|
2031 |
|
$this->injectEnvironmentAndConfigurationIfNeeded($processor); |
171
|
|
|
|
172
|
2031 |
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* {@inheritdoc} |
177
|
|
|
*/ |
178
|
9 |
|
public function addDocumentProcessor(DocumentProcessorInterface $processor, int $priority = 0): ConfigurableEnvironmentInterface |
179
|
|
|
{ |
180
|
9 |
|
$this->assertUninitialized('Failed to add document processor.'); |
181
|
|
|
|
182
|
6 |
|
$this->documentProcessors->add($processor, $priority); |
183
|
6 |
|
$this->injectEnvironmentAndConfigurationIfNeeded($processor); |
184
|
|
|
|
185
|
6 |
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* {@inheritdoc} |
190
|
|
|
*/ |
191
|
2037 |
|
public function addBlockRenderer($blockClass, BlockRendererInterface $blockRenderer, int $priority = 0): ConfigurableEnvironmentInterface |
192
|
|
|
{ |
193
|
2037 |
|
$this->assertUninitialized('Failed to add block renderer.'); |
194
|
|
|
|
195
|
2034 |
|
if (!isset($this->blockRenderersByClass[$blockClass])) { |
196
|
2034 |
|
$this->blockRenderersByClass[$blockClass] = new PrioritizedList(); |
197
|
|
|
} |
198
|
|
|
|
199
|
2034 |
|
$this->blockRenderersByClass[$blockClass]->add($blockRenderer, $priority); |
200
|
2034 |
|
$this->injectEnvironmentAndConfigurationIfNeeded($blockRenderer); |
201
|
|
|
|
202
|
2034 |
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* {@inheritdoc} |
207
|
|
|
*/ |
208
|
2037 |
|
public function addInlineRenderer(string $inlineClass, InlineRendererInterface $renderer, int $priority = 0): ConfigurableEnvironmentInterface |
209
|
|
|
{ |
210
|
2037 |
|
$this->assertUninitialized('Failed to add inline renderer.'); |
211
|
|
|
|
212
|
2034 |
|
if (!isset($this->inlineRenderersByClass[$inlineClass])) { |
213
|
2034 |
|
$this->inlineRenderersByClass[$inlineClass] = new PrioritizedList(); |
214
|
|
|
} |
215
|
|
|
|
216
|
2034 |
|
$this->inlineRenderersByClass[$inlineClass]->add($renderer, $priority); |
217
|
2034 |
|
$this->injectEnvironmentAndConfigurationIfNeeded($renderer); |
218
|
|
|
|
219
|
2034 |
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* {@inheritdoc} |
224
|
|
|
*/ |
225
|
2049 |
|
public function getBlockParsers(): iterable |
226
|
|
|
{ |
227
|
2049 |
|
$this->initializeExtensions(); |
228
|
|
|
|
229
|
2049 |
|
return $this->blockParsers->getIterator(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* {@inheritdoc} |
234
|
|
|
*/ |
235
|
1758 |
|
public function getInlineParsersForCharacter(string $character): iterable |
236
|
|
|
{ |
237
|
1758 |
|
$this->initializeExtensions(); |
238
|
|
|
|
239
|
1758 |
|
if (!isset($this->inlineParsersByCharacter[$character])) { |
240
|
1617 |
|
return []; |
241
|
|
|
} |
242
|
|
|
|
243
|
1326 |
|
return $this->inlineParsersByCharacter[$character]->getIterator(); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* {@inheritdoc} |
248
|
|
|
*/ |
249
|
1749 |
|
public function getDelimiterProcessors(): DelimiterProcessorCollection |
250
|
|
|
{ |
251
|
1749 |
|
$this->initializeExtensions(); |
252
|
|
|
|
253
|
1749 |
|
return $this->delimiterProcessors; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* {@inheritdoc} |
258
|
|
|
*/ |
259
|
2037 |
|
public function getDocumentProcessors(): iterable |
260
|
|
|
{ |
261
|
2037 |
|
$this->initializeExtensions(); |
262
|
|
|
|
263
|
2037 |
|
return $this->documentProcessors->getIterator(); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* {@inheritdoc} |
268
|
|
|
*/ |
269
|
2040 |
|
public function getBlockRenderersForClass(string $blockClass): iterable |
270
|
|
|
{ |
271
|
2040 |
|
$this->initializeExtensions(); |
272
|
|
|
|
273
|
2040 |
|
if (!isset($this->blockRenderersByClass[$blockClass])) { |
274
|
6 |
|
return []; |
275
|
|
|
} |
276
|
|
|
|
277
|
2034 |
|
return $this->blockRenderersByClass[$blockClass]->getIterator(); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* {@inheritdoc} |
282
|
|
|
*/ |
283
|
1755 |
|
public function getInlineRenderersForClass(string $inlineClass): iterable |
284
|
|
|
{ |
285
|
1755 |
|
$this->initializeExtensions(); |
286
|
|
|
|
287
|
1755 |
|
if (!isset($this->inlineRenderersByClass[$inlineClass])) { |
288
|
9 |
|
return []; |
289
|
|
|
} |
290
|
|
|
|
291
|
1746 |
|
return $this->inlineRenderersByClass[$inlineClass]->getIterator(); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Get all registered extensions |
296
|
|
|
* |
297
|
|
|
* @return ExtensionInterface[] |
298
|
|
|
*/ |
299
|
12 |
|
public function getExtensions(): iterable |
300
|
|
|
{ |
301
|
12 |
|
return $this->extensions; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Add a single extension |
306
|
|
|
* |
307
|
|
|
* @param ExtensionInterface $extension |
308
|
|
|
* |
309
|
|
|
* @return $this |
310
|
|
|
*/ |
311
|
2043 |
|
public function addExtension(ExtensionInterface $extension): ConfigurableEnvironmentInterface |
312
|
|
|
{ |
313
|
2043 |
|
$this->assertUninitialized('Failed to add extension.'); |
314
|
|
|
|
315
|
2040 |
|
$this->extensions[] = $extension; |
316
|
2040 |
|
$this->uninitializedExtensions[] = $extension; |
317
|
|
|
|
318
|
2040 |
|
return $this; |
319
|
|
|
} |
320
|
|
|
|
321
|
2109 |
|
private function initializeExtensions() |
322
|
|
|
{ |
323
|
|
|
// Only initialize them once |
324
|
2109 |
|
if ($this->extensionsInitialized) { |
325
|
2028 |
|
return; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
// Ask all extensions to register their components |
329
|
2109 |
|
while (!empty($this->uninitializedExtensions)) { |
330
|
2031 |
|
foreach ($this->uninitializedExtensions as $i => $extension) { |
331
|
2031 |
|
$extension->register($this); |
332
|
2031 |
|
unset($this->uninitializedExtensions[$i]); |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|
336
|
2109 |
|
$this->extensionsInitialized = true; |
337
|
|
|
|
338
|
|
|
// Lastly, let's build a regex which matches non-inline characters |
339
|
|
|
// This will enable a huge performance boost with inline parsing |
340
|
2109 |
|
$this->buildInlineParserCharacterRegex(); |
341
|
2109 |
|
} |
342
|
|
|
|
343
|
2067 |
|
private function injectEnvironmentAndConfigurationIfNeeded($object) |
344
|
|
|
{ |
345
|
2067 |
|
if ($object instanceof EnvironmentAwareInterface) { |
346
|
2031 |
|
$object->setEnvironment($this); |
347
|
|
|
} |
348
|
|
|
|
349
|
2067 |
|
if ($object instanceof ConfigurationAwareInterface) { |
350
|
2031 |
|
$object->setConfiguration($this->config); |
351
|
|
|
} |
352
|
2067 |
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* @return Environment |
356
|
|
|
*/ |
357
|
2034 |
|
public static function createCommonMarkEnvironment(): Environment |
358
|
|
|
{ |
359
|
2034 |
|
$environment = new static(); |
360
|
2034 |
|
$environment->addExtension(new CommonMarkCoreExtension()); |
361
|
2034 |
|
$environment->mergeConfig([ |
362
|
678 |
|
'renderer' => [ |
363
|
1356 |
|
'block_separator' => "\n", |
364
|
|
|
'inner_separator' => "\n", |
365
|
|
|
'soft_break' => "\n", |
366
|
|
|
], |
367
|
|
|
'safe' => false, // deprecated option |
368
|
2034 |
|
'html_input' => self::HTML_INPUT_ALLOW, |
369
|
|
|
'allow_unsafe_links' => true, |
370
|
2034 |
|
'max_nesting_level' => INF, |
371
|
|
|
]); |
372
|
|
|
|
373
|
2034 |
|
return $environment; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* {@inheritdoc} |
378
|
|
|
*/ |
379
|
1614 |
|
public function getInlineParserCharacterRegex(): string |
380
|
|
|
{ |
381
|
1614 |
|
return $this->inlineParserCharacterRegex; |
382
|
|
|
} |
383
|
|
|
|
384
|
2109 |
|
private function buildInlineParserCharacterRegex() |
385
|
|
|
{ |
386
|
2109 |
|
$chars = \array_keys($this->inlineParsersByCharacter); |
387
|
|
|
|
388
|
2109 |
|
if (empty($chars)) { |
389
|
|
|
// If no special inline characters exist then parse the whole line |
390
|
72 |
|
$this->inlineParserCharacterRegex = '/^.+$/u'; |
391
|
|
|
} else { |
392
|
|
|
// Match any character which inline parsers are not interested in |
393
|
2037 |
|
$this->inlineParserCharacterRegex = '/^[^' . \preg_quote(\implode('', $chars), '/') . ']+/u'; |
394
|
|
|
} |
395
|
2109 |
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* @param string $message |
399
|
|
|
* |
400
|
|
|
* @throws \RuntimeException |
401
|
|
|
*/ |
402
|
2112 |
|
private function assertUninitialized(string $message) |
403
|
|
|
{ |
404
|
2112 |
|
if ($this->extensionsInitialized) { |
405
|
27 |
|
throw new \RuntimeException($message . ' Extensions have already been initialized.'); |
406
|
|
|
} |
407
|
2085 |
|
} |
408
|
|
|
} |
409
|
|
|
|