| 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\Extension\CommonMark\Parser\Inline;
|
|
|
18 |
|
|
|
19 |
use League\CommonMark\Environment\EnvironmentAwareInterface;
|
|
|
20 |
use League\CommonMark\Environment\EnvironmentInterface;
|
|
|
21 |
use League\CommonMark\Extension\CommonMark\Node\Inline\AbstractWebResource;
|
|
|
22 |
use League\CommonMark\Extension\CommonMark\Node\Inline\Image;
|
|
|
23 |
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
|
|
|
24 |
use League\CommonMark\Extension\Mention\Mention;
|
|
|
25 |
use League\CommonMark\Node\Inline\AdjacentTextMerger;
|
|
|
26 |
use League\CommonMark\Node\Inline\Text;
|
|
|
27 |
use League\CommonMark\Parser\Cursor;
|
|
|
28 |
use League\CommonMark\Parser\Inline\InlineParserInterface;
|
|
|
29 |
use League\CommonMark\Parser\Inline\InlineParserMatch;
|
|
|
30 |
use League\CommonMark\Parser\InlineParserContext;
|
|
|
31 |
use League\CommonMark\Reference\ReferenceInterface;
|
|
|
32 |
use League\CommonMark\Reference\ReferenceMapInterface;
|
|
|
33 |
use League\CommonMark\Util\LinkParserHelper;
|
|
|
34 |
use League\CommonMark\Util\RegexHelper;
|
|
|
35 |
|
|
|
36 |
final class CloseBracketParser implements InlineParserInterface, EnvironmentAwareInterface
|
|
|
37 |
{
|
|
|
38 |
/** @psalm-readonly-allow-private-mutation */
|
|
|
39 |
private EnvironmentInterface $environment;
|
|
|
40 |
|
|
|
41 |
public function getMatchDefinition(): InlineParserMatch
|
|
|
42 |
{
|
|
|
43 |
return InlineParserMatch::string(']');
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
public function parse(InlineParserContext $inlineContext): bool
|
|
|
47 |
{
|
|
|
48 |
// Look through stack of delimiters for a [ or !
|
|
|
49 |
$opener = $inlineContext->getDelimiterStack()->searchByCharacter(['[', '!']);
|
|
|
50 |
if ($opener === null) {
|
|
|
51 |
return false;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
if (! $opener->isActive()) {
|
|
|
55 |
// no matched opener; remove from emphasis stack
|
|
|
56 |
$inlineContext->getDelimiterStack()->removeDelimiter($opener);
|
|
|
57 |
|
|
|
58 |
return false;
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
$cursor = $inlineContext->getCursor();
|
|
|
62 |
|
|
|
63 |
$startPos = $cursor->getPosition();
|
|
|
64 |
$previousState = $cursor->saveState();
|
|
|
65 |
|
|
|
66 |
$cursor->advanceBy(1);
|
|
|
67 |
|
|
|
68 |
// Check to see if we have a link/image
|
|
|
69 |
|
|
|
70 |
// Inline link?
|
|
|
71 |
if ($result = $this->tryParseInlineLinkAndTitle($cursor)) {
|
|
|
72 |
$link = $result;
|
|
|
73 |
} elseif ($link = $this->tryParseReference($cursor, $inlineContext->getReferenceMap(), $opener->getIndex(), $startPos)) {
|
|
|
74 |
$reference = $link;
|
|
|
75 |
$link = ['url' => $link->getDestination(), 'title' => $link->getTitle()];
|
|
|
76 |
} else {
|
|
|
77 |
// No match
|
|
|
78 |
$inlineContext->getDelimiterStack()->removeDelimiter($opener); // Remove this opener from stack
|
|
|
79 |
$cursor->restoreState($previousState);
|
|
|
80 |
|
|
|
81 |
return false;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
$isImage = $opener->getChar() === '!';
|
|
|
85 |
|
|
|
86 |
$inline = $this->createInline($link['url'], $link['title'], $isImage, $reference ?? null);
|
|
|
87 |
$opener->getInlineNode()->replaceWith($inline);
|
|
|
88 |
while (($label = $inline->next()) !== null) {
|
|
|
89 |
// Is there a Mention or Link contained within this link?
|
|
|
90 |
// CommonMark does not allow nested links, so we'll restore the original text.
|
|
|
91 |
if ($label instanceof Mention) {
|
|
|
92 |
$label->replaceWith($replacement = new Text($label->getPrefix() . $label->getIdentifier()));
|
|
|
93 |
$inline->appendChild($replacement);
|
|
|
94 |
} elseif ($label instanceof Link) {
|
|
|
95 |
foreach ($label->children() as $child) {
|
|
|
96 |
$label->insertBefore($child);
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
$label->detach();
|
|
|
100 |
} else {
|
|
|
101 |
$inline->appendChild($label);
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
// Process delimiters such as emphasis inside link/image
|
|
|
106 |
$delimiterStack = $inlineContext->getDelimiterStack();
|
|
|
107 |
$stackBottom = $opener->getPrevious();
|
|
|
108 |
$delimiterStack->processDelimiters($stackBottom, $this->environment->getDelimiterProcessors());
|
|
|
109 |
$delimiterStack->removeAll($stackBottom);
|
|
|
110 |
|
|
|
111 |
// Merge any adjacent Text nodes together
|
|
|
112 |
AdjacentTextMerger::mergeChildNodes($inline);
|
|
|
113 |
|
|
|
114 |
// processEmphasis will remove this and later delimiters.
|
|
|
115 |
// Now, for a link, we also remove earlier link openers (no links in links)
|
|
|
116 |
if (! $isImage) {
|
|
|
117 |
$inlineContext->getDelimiterStack()->removeEarlierMatches('[');
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
return true;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
public function setEnvironment(EnvironmentInterface $environment): void
|
|
|
124 |
{
|
|
|
125 |
$this->environment = $environment;
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
/**
|
|
|
129 |
* @return array<string, string>|null
|
|
|
130 |
*/
|
|
|
131 |
private function tryParseInlineLinkAndTitle(Cursor $cursor): ?array
|
|
|
132 |
{
|
|
|
133 |
if ($cursor->getCurrentCharacter() !== '(') {
|
|
|
134 |
return null;
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
$previousState = $cursor->saveState();
|
|
|
138 |
|
|
|
139 |
$cursor->advanceBy(1);
|
|
|
140 |
$cursor->advanceToNextNonSpaceOrNewline();
|
|
|
141 |
if (($dest = LinkParserHelper::parseLinkDestination($cursor)) === null) {
|
|
|
142 |
$cursor->restoreState($previousState);
|
|
|
143 |
|
|
|
144 |
return null;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
$cursor->advanceToNextNonSpaceOrNewline();
|
|
|
148 |
$previousCharacter = $cursor->peek(-1);
|
|
|
149 |
// We know from previous lines that we've advanced at least one space so far, so this next call should never be null
|
|
|
150 |
\assert(\is_string($previousCharacter));
|
|
|
151 |
|
|
|
152 |
$title = '';
|
|
|
153 |
// make sure there's a space before the title:
|
|
|
154 |
if (\preg_match(RegexHelper::REGEX_WHITESPACE_CHAR, $previousCharacter)) {
|
|
|
155 |
$title = LinkParserHelper::parseLinkTitle($cursor) ?? '';
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
$cursor->advanceToNextNonSpaceOrNewline();
|
|
|
159 |
|
|
|
160 |
if ($cursor->getCurrentCharacter() !== ')') {
|
|
|
161 |
$cursor->restoreState($previousState);
|
|
|
162 |
|
|
|
163 |
return null;
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
$cursor->advanceBy(1);
|
|
|
167 |
|
|
|
168 |
return ['url' => $dest, 'title' => $title];
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
private function tryParseReference(Cursor $cursor, ReferenceMapInterface $referenceMap, ?int $openerIndex, int $startPos): ?ReferenceInterface
|
|
|
172 |
{
|
|
|
173 |
if ($openerIndex === null) {
|
|
|
174 |
return null;
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
$savePos = $cursor->saveState();
|
|
|
178 |
$beforeLabel = $cursor->getPosition();
|
|
|
179 |
$n = LinkParserHelper::parseLinkLabel($cursor);
|
|
|
180 |
if ($n === 0 || $n === 2) {
|
|
|
181 |
$start = $openerIndex;
|
|
|
182 |
$length = $startPos - $openerIndex;
|
|
|
183 |
} else {
|
|
|
184 |
$start = $beforeLabel + 1;
|
|
|
185 |
$length = $n - 2;
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
$referenceLabel = $cursor->getSubstring($start, $length);
|
|
|
189 |
|
|
|
190 |
if ($n === 0) {
|
|
|
191 |
// If shortcut reference link, rewind before spaces we skipped
|
|
|
192 |
$cursor->restoreState($savePos);
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
return $referenceMap->get($referenceLabel);
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
private function createInline(string $url, string $title, bool $isImage, ?ReferenceInterface $reference = null): AbstractWebResource
|
|
|
199 |
{
|
|
|
200 |
if ($isImage) {
|
|
|
201 |
$inline = new Image($url, null, $title);
|
|
|
202 |
} else {
|
|
|
203 |
$inline = new Link($url, null, $title);
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
if ($reference) {
|
|
|
207 |
$inline->data->set('reference', $reference);
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
return $inline;
|
|
|
211 |
}
|
|
|
212 |
}
|