| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
/*
|
|
|
6 |
* This file is part of the league/commonmark package.
|
|
|
7 |
*
|
|
|
8 |
* (c) Colin O'Dell <colinodell@gmail.com>
|
|
|
9 |
*
|
|
|
10 |
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
|
|
|
11 |
* - (c) John MacFarlane
|
|
|
12 |
*
|
|
|
13 |
* For the full copyright and license information, please view the LICENSE
|
|
|
14 |
* file that was distributed with this source code.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
namespace League\CommonMark\Parser;
|
|
|
18 |
|
|
|
19 |
use League\CommonMark\Environment\EnvironmentInterface;
|
|
|
20 |
use League\CommonMark\Node\Block\AbstractBlock;
|
|
|
21 |
use League\CommonMark\Node\Inline\AdjacentTextMerger;
|
|
|
22 |
use League\CommonMark\Node\Inline\Text;
|
|
|
23 |
use League\CommonMark\Parser\Inline\InlineParserInterface;
|
|
|
24 |
use League\CommonMark\Reference\ReferenceMapInterface;
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* @internal
|
|
|
28 |
*/
|
|
|
29 |
final class InlineParserEngine implements InlineParserEngineInterface
|
|
|
30 |
{
|
|
|
31 |
/** @psalm-readonly */
|
|
|
32 |
private EnvironmentInterface $environment;
|
|
|
33 |
|
|
|
34 |
/** @psalm-readonly */
|
|
|
35 |
private ReferenceMapInterface $referenceMap;
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* @var array<int, InlineParserInterface|string|bool>
|
|
|
39 |
* @psalm-var list<array{0: InlineParserInterface, 1: string, 2: bool}>
|
|
|
40 |
* @phpstan-var array<int, array{0: InlineParserInterface, 1: string, 2: bool}>
|
|
|
41 |
*/
|
|
|
42 |
private array $parsers = [];
|
|
|
43 |
|
|
|
44 |
public function __construct(EnvironmentInterface $environment, ReferenceMapInterface $referenceMap)
|
|
|
45 |
{
|
|
|
46 |
$this->environment = $environment;
|
|
|
47 |
$this->referenceMap = $referenceMap;
|
|
|
48 |
|
|
|
49 |
foreach ($environment->getInlineParsers() as $parser) {
|
|
|
50 |
\assert($parser instanceof InlineParserInterface);
|
|
|
51 |
$regex = $parser->getMatchDefinition()->getRegex();
|
|
|
52 |
|
|
|
53 |
$this->parsers[] = [$parser, $regex, \strlen($regex) !== \mb_strlen($regex, 'UTF-8')];
|
|
|
54 |
}
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
public function parse(string $contents, AbstractBlock $block): void
|
|
|
58 |
{
|
|
|
59 |
$contents = \trim($contents);
|
|
|
60 |
$cursor = new Cursor($contents);
|
|
|
61 |
|
|
|
62 |
$inlineParserContext = new InlineParserContext($cursor, $block, $this->referenceMap);
|
|
|
63 |
|
|
|
64 |
// Have all parsers look at the line to determine what they might want to parse and what positions they exist at
|
|
|
65 |
foreach ($this->matchParsers($contents) as $matchPosition => $parsers) {
|
|
|
66 |
$currentPosition = $cursor->getPosition();
|
|
|
67 |
// We've already gone past this point
|
|
|
68 |
if ($currentPosition > $matchPosition) {
|
|
|
69 |
continue;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
// We've skipped over some uninteresting text that should be added as a plain text node
|
|
|
73 |
if ($currentPosition < $matchPosition) {
|
|
|
74 |
$cursor->advanceBy($matchPosition - $currentPosition);
|
|
|
75 |
$this->addPlainText($cursor->getPreviousText(), $block);
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
// We're now at a potential start - see which of the current parsers can handle it
|
|
|
79 |
$parsed = false;
|
|
|
80 |
foreach ($parsers as [$parser, $matches]) {
|
|
|
81 |
\assert($parser instanceof InlineParserInterface);
|
|
|
82 |
if ($parser->parse($inlineParserContext->withMatches($matches))) {
|
|
|
83 |
// A parser has successfully handled the text at the given position; don't consider any others at this position
|
|
|
84 |
$parsed = true;
|
|
|
85 |
break;
|
|
|
86 |
}
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
if ($parsed) {
|
|
|
90 |
continue;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
// Despite potentially being interested, nothing actually parsed text here, so add the current character and continue onwards
|
|
|
94 |
$this->addPlainText((string) $cursor->getCurrentCharacter(), $block);
|
|
|
95 |
$cursor->advance();
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
// Add any remaining text that wasn't parsed
|
|
|
99 |
if (! $cursor->isAtEnd()) {
|
|
|
100 |
$this->addPlainText($cursor->getRemainder(), $block);
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
// Process any delimiters that were found
|
|
|
104 |
$delimiterStack = $inlineParserContext->getDelimiterStack();
|
|
|
105 |
$delimiterStack->processDelimiters(null, $this->environment->getDelimiterProcessors());
|
|
|
106 |
$delimiterStack->removeAll();
|
|
|
107 |
|
|
|
108 |
// Combine adjacent text notes into one
|
|
|
109 |
AdjacentTextMerger::mergeChildNodes($block);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
private function addPlainText(string $text, AbstractBlock $container): void
|
|
|
113 |
{
|
|
|
114 |
$lastInline = $container->lastChild();
|
|
|
115 |
if ($lastInline instanceof Text && ! $lastInline->data->has('delim')) {
|
|
|
116 |
$lastInline->append($text);
|
|
|
117 |
} else {
|
|
|
118 |
$container->appendChild(new Text($text));
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* Given the current line, ask all the parsers which parts of the text they would be interested in parsing.
|
|
|
124 |
*
|
|
|
125 |
* The resulting array provides a list of character positions, which parsers are interested in trying to parse
|
|
|
126 |
* the text at those points, and (for convenience/optimization) what the matching text happened to be.
|
|
|
127 |
*
|
|
|
128 |
* @return array<array<int, InlineParserInterface|string>>
|
|
|
129 |
*
|
|
|
130 |
* @psalm-return array<int, list<array{0: InlineParserInterface, 1: non-empty-array<string>}>>
|
|
|
131 |
*
|
|
|
132 |
* @phpstan-return array<int, array<int, array{0: InlineParserInterface, 1: non-empty-array<string>}>>
|
|
|
133 |
*/
|
|
|
134 |
private function matchParsers(string $contents): array
|
|
|
135 |
{
|
|
|
136 |
$contents = \trim($contents);
|
| 991 |
lars |
137 |
$isMultibyte = ! \mb_check_encoding($contents, 'ASCII');
|
| 148 |
lars |
138 |
|
|
|
139 |
$ret = [];
|
|
|
140 |
|
|
|
141 |
foreach ($this->parsers as [$parser, $regex, $isRegexMultibyte]) {
|
|
|
142 |
if ($isMultibyte || $isRegexMultibyte) {
|
|
|
143 |
$regex .= 'u';
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
// See if the parser's InlineParserMatch regex matched against any part of the string
|
|
|
147 |
if (! \preg_match_all($regex, $contents, $matches, \PREG_OFFSET_CAPTURE | \PREG_SET_ORDER)) {
|
|
|
148 |
continue;
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
// For each part that matched...
|
|
|
152 |
foreach ($matches as $match) {
|
|
|
153 |
if ($isMultibyte) {
|
|
|
154 |
// PREG_OFFSET_CAPTURE always returns the byte offset, not the char offset, which is annoying
|
|
|
155 |
$offset = \mb_strlen(\substr($contents, 0, $match[0][1]), 'UTF-8');
|
|
|
156 |
} else {
|
|
|
157 |
$offset = \intval($match[0][1]);
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
// Remove the offsets, keeping only the matched text
|
|
|
161 |
$m = \array_column($match, 0);
|
|
|
162 |
|
|
|
163 |
if ($m === []) {
|
|
|
164 |
continue;
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
// Add this match to the list of character positions to stop at
|
|
|
168 |
$ret[$offset][] = [$parser, $m];
|
|
|
169 |
}
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
// Sort matches by position so we visit them in order
|
|
|
173 |
\ksort($ret);
|
|
|
174 |
|
|
|
175 |
return $ret;
|
|
|
176 |
}
|
|
|
177 |
}
|