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\Inline\Parser; |
16
|
|
|
|
17
|
|
|
use League\CommonMark\Cursor; |
18
|
|
|
use League\CommonMark\Delimiter\DelimiterInterface; |
19
|
|
|
use League\CommonMark\EnvironmentAwareInterface; |
20
|
|
|
use League\CommonMark\EnvironmentInterface; |
21
|
|
|
use League\CommonMark\Inline\AdjacentTextMerger; |
22
|
|
|
use League\CommonMark\Inline\Element\AbstractWebResource; |
23
|
|
|
use League\CommonMark\Inline\Element\Image; |
24
|
|
|
use League\CommonMark\Inline\Element\Link; |
25
|
|
|
use League\CommonMark\InlineParserContext; |
26
|
|
|
use League\CommonMark\Reference\ReferenceInterface; |
27
|
|
|
use League\CommonMark\Reference\ReferenceMapInterface; |
28
|
|
|
use League\CommonMark\Util\LinkParserHelper; |
29
|
|
|
use League\CommonMark\Util\RegexHelper; |
30
|
|
|
|
31
|
|
|
final class CloseBracketParser implements InlineParserInterface, EnvironmentAwareInterface |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var EnvironmentInterface |
35
|
|
|
*/ |
36
|
|
|
private $environment; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return string[] |
40
|
|
|
*/ |
41
|
2463 |
|
public function getCharacters(): array |
42
|
|
|
{ |
43
|
2463 |
|
return [']']; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param InlineParserContext $inlineContext |
48
|
|
|
* |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
453 |
|
public function parse(InlineParserContext $inlineContext): bool |
52
|
|
|
{ |
53
|
|
|
// Look through stack of delimiters for a [ or ! |
54
|
453 |
|
$opener = $inlineContext->getDelimiterStack()->searchByCharacter(['[', '!']); |
55
|
453 |
|
if ($opener === null) { |
56
|
12 |
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
444 |
|
if (!$opener->isActive()) { |
60
|
|
|
// no matched opener; remove from emphasis stack |
61
|
18 |
|
$inlineContext->getDelimiterStack()->removeDelimiter($opener); |
62
|
|
|
|
63
|
18 |
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
444 |
|
$cursor = $inlineContext->getCursor(); |
67
|
|
|
|
68
|
444 |
|
$startPos = $cursor->getPosition(); |
69
|
444 |
|
$previousState = $cursor->saveState(); |
70
|
|
|
|
71
|
444 |
|
$cursor->advanceBy(1); |
72
|
|
|
|
73
|
|
|
// Check to see if we have a link/image |
74
|
444 |
|
if (!($link = $this->tryParseLink($cursor, $inlineContext->getReferenceMap(), $opener, $startPos))) { |
75
|
|
|
// No match |
76
|
117 |
|
$inlineContext->getDelimiterStack()->removeDelimiter($opener); // Remove this opener from stack |
77
|
117 |
|
$cursor->restoreState($previousState); |
78
|
|
|
|
79
|
117 |
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
357 |
|
$isImage = $opener->getChar() === '!'; |
83
|
|
|
|
84
|
357 |
|
$inline = $this->createInline($link['url'], $link['title'], $isImage); |
85
|
357 |
|
$opener->getInlineNode()->replaceWith($inline); |
86
|
357 |
|
while (($label = $inline->next()) !== null) { |
87
|
354 |
|
$inline->appendChild($label); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// Process delimiters such as emphasis inside link/image |
91
|
357 |
|
$delimiterStack = $inlineContext->getDelimiterStack(); |
92
|
357 |
|
$stackBottom = $opener->getPrevious(); |
93
|
357 |
|
$delimiterStack->processDelimiters($stackBottom, $this->environment->getDelimiterProcessors()); |
94
|
357 |
|
$delimiterStack->removeAll($stackBottom); |
95
|
|
|
|
96
|
|
|
// Merge any adjacent Text nodes together |
97
|
357 |
|
AdjacentTextMerger::mergeChildNodes($inline); |
98
|
|
|
|
99
|
|
|
// processEmphasis will remove this and later delimiters. |
100
|
|
|
// Now, for a link, we also remove earlier link openers (no links in links) |
101
|
357 |
|
if (!$isImage) { |
102
|
303 |
|
$inlineContext->getDelimiterStack()->removeEarlierMatches('['); |
103
|
|
|
} |
104
|
|
|
|
105
|
357 |
|
return true; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param EnvironmentInterface $environment |
110
|
|
|
*/ |
111
|
2463 |
|
public function setEnvironment(EnvironmentInterface $environment) |
112
|
|
|
{ |
113
|
2463 |
|
$this->environment = $environment; |
114
|
2463 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param Cursor $cursor |
118
|
|
|
* @param ReferenceMapInterface $referenceMap |
119
|
|
|
* @param DelimiterInterface $opener |
120
|
|
|
* @param int $startPos |
121
|
|
|
* |
122
|
|
|
* @return array|bool |
123
|
|
|
*/ |
124
|
444 |
|
private function tryParseLink(Cursor $cursor, ReferenceMapInterface $referenceMap, DelimiterInterface $opener, int $startPos) |
125
|
|
|
{ |
126
|
|
|
// Check to see if we have a link/image |
127
|
|
|
// Inline link? |
128
|
444 |
|
if ($result = $this->tryParseInlineLinkAndTitle($cursor)) { |
129
|
165 |
|
return $result; |
130
|
|
|
} |
131
|
|
|
|
132
|
294 |
|
if ($link = $this->tryParseReference($cursor, $referenceMap, $opener, $startPos)) { |
133
|
198 |
|
return ['url' => $link->getDestination(), 'title' => $link->getTitle()]; |
134
|
|
|
} |
135
|
|
|
|
136
|
117 |
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param Cursor $cursor |
141
|
|
|
* |
142
|
|
|
* @return array|bool |
143
|
|
|
*/ |
144
|
444 |
|
private function tryParseInlineLinkAndTitle(Cursor $cursor) |
145
|
|
|
{ |
146
|
444 |
|
if ($cursor->getCharacter() !== '(') { |
147
|
258 |
|
return false; |
148
|
|
|
} |
149
|
|
|
|
150
|
195 |
|
$previousState = $cursor->saveState(); |
151
|
|
|
|
152
|
195 |
|
$cursor->advanceBy(1); |
153
|
195 |
|
$cursor->advanceToNextNonSpaceOrNewline(); |
154
|
195 |
|
if (($dest = LinkParserHelper::parseLinkDestination($cursor)) === null) { |
155
|
9 |
|
$cursor->restoreState($previousState); |
156
|
|
|
|
157
|
9 |
|
return false; |
158
|
|
|
} |
159
|
|
|
|
160
|
189 |
|
$cursor->advanceToNextNonSpaceOrNewline(); |
161
|
|
|
|
162
|
189 |
|
$title = ''; |
163
|
|
|
// make sure there's a space before the title: |
164
|
189 |
|
if (\preg_match(RegexHelper::REGEX_WHITESPACE_CHAR, $cursor->peek(-1))) { |
165
|
54 |
|
$title = LinkParserHelper::parseLinkTitle($cursor) ?? ''; |
166
|
|
|
} |
167
|
|
|
|
168
|
189 |
|
$cursor->advanceToNextNonSpaceOrNewline(); |
169
|
|
|
|
170
|
189 |
|
if ($cursor->getCharacter() !== ')') { |
171
|
30 |
|
$cursor->restoreState($previousState); |
172
|
|
|
|
173
|
30 |
|
return false; |
174
|
|
|
} |
175
|
|
|
|
176
|
165 |
|
$cursor->advanceBy(1); |
177
|
|
|
|
178
|
165 |
|
return ['url' => $dest, 'title' => $title]; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param Cursor $cursor |
183
|
|
|
* @param ReferenceMapInterface $referenceMap |
184
|
|
|
* @param DelimiterInterface $opener |
185
|
|
|
* @param int $startPos |
186
|
|
|
* |
187
|
|
|
* @return ReferenceInterface|null |
188
|
|
|
*/ |
189
|
294 |
|
private function tryParseReference(Cursor $cursor, ReferenceMapInterface $referenceMap, DelimiterInterface $opener, int $startPos): ?ReferenceInterface |
190
|
|
|
{ |
191
|
294 |
|
if ($opener->getIndex() === null) { |
192
|
|
|
return null; |
193
|
|
|
} |
194
|
|
|
|
195
|
294 |
|
$savePos = $cursor->saveState(); |
196
|
294 |
|
$beforeLabel = $cursor->getPosition(); |
197
|
294 |
|
$n = LinkParserHelper::parseLinkLabel($cursor); |
198
|
294 |
|
if ($n === 0 || $n === 2) { |
199
|
243 |
|
$start = $opener->getIndex(); |
200
|
243 |
|
$length = $startPos - $opener->getIndex(); |
201
|
|
|
} else { |
202
|
66 |
|
$start = $beforeLabel + 1; |
203
|
66 |
|
$length = $n - 2; |
204
|
|
|
} |
205
|
|
|
|
206
|
294 |
|
$referenceLabel = $cursor->getSubstring($start, $length); |
207
|
|
|
|
208
|
294 |
|
if ($n === 0) { |
209
|
|
|
// If shortcut reference link, rewind before spaces we skipped |
210
|
219 |
|
$cursor->restoreState($savePos); |
211
|
|
|
} |
212
|
|
|
|
213
|
294 |
|
return $referenceMap->getReference($referenceLabel); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param string $url |
218
|
|
|
* @param string $title |
219
|
|
|
* @param bool $isImage |
220
|
|
|
* |
221
|
|
|
* @return AbstractWebResource |
222
|
|
|
*/ |
223
|
357 |
|
private function createInline(string $url, string $title, bool $isImage): AbstractWebResource |
224
|
|
|
{ |
225
|
357 |
|
if ($isImage) { |
226
|
72 |
|
return new Image($url, null, $title); |
227
|
|
|
} |
228
|
|
|
|
229
|
303 |
|
return new Link($url, null, $title); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|