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