|
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\Inline\AdjoiningTextCollapser; |
|
18
|
|
|
use League\CommonMark\Inline\Element\Text; |
|
19
|
|
|
use League\CommonMark\Node\Node; |
|
20
|
|
|
use League\CommonMark\Reference\ReferenceMap; |
|
21
|
|
|
|
|
22
|
|
|
class InlineParserEngine |
|
23
|
|
|
{ |
|
24
|
|
|
protected $environment; |
|
25
|
1995 |
|
|
|
26
|
|
|
public function __construct(EnvironmentInterface $environment) |
|
27
|
1995 |
|
{ |
|
28
|
1995 |
|
$this->environment = $environment; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param Node $container |
|
33
|
|
|
* @param ReferenceMap $referenceMap |
|
34
|
1704 |
|
*/ |
|
35
|
|
|
public function parse(Node $container, ReferenceMap $referenceMap) |
|
36
|
1704 |
|
{ |
|
37
|
1704 |
|
$inlineParserContext = new InlineParserContext($container, $referenceMap); |
|
|
|
|
|
|
38
|
1701 |
|
while (($character = $inlineParserContext->getCursor()->getCharacter()) !== null) { |
|
39
|
1572 |
|
if (!$this->parseCharacter($character, $inlineParserContext)) { |
|
40
|
|
|
$this->addPlainText($character, $container, $inlineParserContext); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
1704 |
|
|
|
44
|
|
|
$this->processInlines($inlineParserContext); |
|
45
|
1704 |
|
|
|
46
|
1704 |
|
AdjoiningTextCollapser::collapseTextNodes($container); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $character |
|
51
|
|
|
* @param InlineParserContext $inlineParserContext |
|
52
|
|
|
* |
|
53
|
|
|
* @return bool Whether we successfully parsed a character at that position |
|
54
|
1701 |
|
*/ |
|
55
|
|
|
protected function parseCharacter(string $character, InlineParserContext $inlineParserContext): bool |
|
56
|
1701 |
|
{ |
|
57
|
1278 |
|
foreach ($this->environment->getInlineParsersForCharacter($character) as $parser) { |
|
58
|
1256 |
|
if ($parser->parse($inlineParserContext)) { |
|
59
|
|
|
return true; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
1572 |
|
|
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param InlineParserContext $inlineParserContext |
|
68
|
1704 |
|
*/ |
|
69
|
|
|
protected function processInlines(InlineParserContext $inlineParserContext) |
|
70
|
1704 |
|
{ |
|
71
|
|
|
$delimiterStack = $inlineParserContext->getDelimiterStack(); |
|
72
|
1704 |
|
|
|
73
|
1704 |
|
foreach ($this->environment->getInlineProcessors() as $inlineProcessor) { |
|
74
|
|
|
$inlineProcessor->processInlines($delimiterStack); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1704 |
|
// Remove all delimiters |
|
78
|
1704 |
|
$delimiterStack->removeAll(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $character |
|
83
|
|
|
* @param Node $container |
|
84
|
|
|
* @param InlineParserContext $inlineParserContext |
|
85
|
1572 |
|
*/ |
|
86
|
|
|
private function addPlainText(string $character, Node $container, InlineParserContext $inlineParserContext) |
|
87
|
|
|
{ |
|
88
|
|
|
// We reach here if none of the parsers can handle the input |
|
89
|
1572 |
|
// Attempt to match multiple non-special characters at once |
|
90
|
|
|
$text = $inlineParserContext->getCursor()->match($this->environment->getInlineParserCharacterRegex()); |
|
91
|
1572 |
|
// This might fail if we're currently at a special character which wasn't parsed; if so, just add that character |
|
92
|
183 |
|
if ($text === null) { |
|
93
|
183 |
|
$inlineParserContext->getCursor()->advance(); |
|
94
|
|
|
$text = $character; |
|
95
|
|
|
} |
|
96
|
1572 |
|
|
|
97
|
1572 |
|
$lastInline = $container->lastChild(); |
|
98
|
216 |
|
if ($lastInline instanceof Text && !isset($lastInline->data['delim'])) { |
|
99
|
|
|
$lastInline->append($text); |
|
100
|
1539 |
|
} else { |
|
101
|
|
|
$container->appendChild(new Text($text)); |
|
102
|
1572 |
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.