1 | <?php |
||
31 | class CloseBracketParser implements InlineParserInterface, EnvironmentAwareInterface |
||
32 | { |
||
33 | /** |
||
34 | * @var EnvironmentInterface |
||
35 | */ |
||
36 | protected $environment; |
||
37 | |||
38 | /** |
||
39 | * @return string[] |
||
40 | */ |
||
41 | 2028 | public function getCharacters(): array |
|
45 | |||
46 | /** |
||
47 | * @param InlineParserContext $inlineContext |
||
48 | * |
||
49 | * @return bool |
||
50 | */ |
||
51 | 438 | public function parse(InlineParserContext $inlineContext): bool |
|
52 | { |
||
53 | // Look through stack of delimiters for a [ or ! |
||
54 | 438 | $opener = $inlineContext->getDelimiterStack()->searchByCharacter(['[', '!']); |
|
55 | 438 | if ($opener === null) { |
|
56 | 12 | return false; |
|
57 | } |
||
58 | |||
59 | 429 | 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 | 429 | $cursor = $inlineContext->getCursor(); |
|
67 | |||
68 | 429 | $startPos = $cursor->getPosition(); |
|
69 | 429 | $previousState = $cursor->saveState(); |
|
70 | |||
71 | 429 | $cursor->advance(); |
|
72 | |||
73 | // Check to see if we have a link/image |
||
74 | 429 | if (!($link = $this->tryParseLink($cursor, $inlineContext->getReferenceMap(), $opener, $startPos))) { |
|
75 | // No match |
||
76 | 108 | $inlineContext->getDelimiterStack()->removeDelimiter($opener); // Remove this opener from stack |
|
77 | 108 | $cursor->restoreState($previousState); |
|
78 | |||
79 | 108 | return false; |
|
80 | } |
||
81 | |||
82 | 351 | $isImage = $opener->getChar() === '!'; |
|
83 | |||
84 | 351 | $inline = $this->createInline($link['url'], $link['title'], $isImage); |
|
85 | 351 | $opener->getInlineNode()->replaceWith($inline); |
|
86 | 351 | while (($label = $inline->next()) !== null) { |
|
87 | 348 | $inline->appendChild($label); |
|
88 | } |
||
89 | |||
90 | 351 | $delimiterStack = $inlineContext->getDelimiterStack(); |
|
91 | 351 | $stackBottom = $opener->getPrevious(); |
|
92 | 351 | foreach ($this->environment->getInlineProcessors() as $inlineProcessor) { |
|
93 | 351 | $inlineProcessor->processInlines($delimiterStack, $stackBottom); |
|
94 | } |
||
95 | 351 | if ($delimiterStack instanceof DelimiterStack) { |
|
96 | 351 | $delimiterStack->removeAll($stackBottom); |
|
97 | } |
||
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 | 351 | if (!$isImage) { |
|
102 | 297 | $inlineContext->getDelimiterStack()->removeEarlierMatches('['); |
|
103 | } |
||
104 | |||
105 | 351 | return true; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param EnvironmentInterface $environment |
||
110 | */ |
||
111 | 2028 | public function setEnvironment(EnvironmentInterface $environment) |
|
115 | |||
116 | /** |
||
117 | * @param Cursor $cursor |
||
118 | * @param ReferenceMap $referenceMap |
||
119 | * @param Delimiter $opener |
||
120 | * @param int $startPos |
||
121 | * |
||
122 | * @return array|bool |
||
123 | */ |
||
124 | 429 | protected function tryParseLink(Cursor $cursor, ReferenceMap $referenceMap, Delimiter $opener, int $startPos) |
|
138 | |||
139 | /** |
||
140 | * @param Cursor $cursor |
||
141 | * |
||
142 | * @return array|bool |
||
143 | */ |
||
144 | 429 | protected function tryParseInlineLinkAndTitle(Cursor $cursor) |
|
145 | { |
||
146 | 429 | if ($cursor->getCharacter() !== '(') { |
|
147 | 249 | return false; |
|
148 | } |
||
149 | |||
150 | 189 | $previousState = $cursor->saveState(); |
|
151 | |||
152 | 189 | $cursor->advance(); |
|
153 | 189 | $cursor->advanceToNextNonSpaceOrNewline(); |
|
154 | 189 | if (($dest = LinkParserHelper::parseLinkDestination($cursor)) === null) { |
|
155 | 9 | $cursor->restoreState($previousState); |
|
156 | |||
157 | 9 | return false; |
|
158 | } |
||
159 | |||
160 | 183 | $cursor->advanceToNextNonSpaceOrNewline(); |
|
161 | |||
162 | 183 | $title = ''; |
|
163 | // make sure there's a space before the title: |
||
164 | 183 | if (\preg_match(RegexHelper::REGEX_WHITESPACE_CHAR, $cursor->peek(-1))) { |
|
165 | 54 | $title = LinkParserHelper::parseLinkTitle($cursor) ?: ''; |
|
166 | } |
||
167 | |||
168 | 183 | $cursor->advanceToNextNonSpaceOrNewline(); |
|
169 | |||
170 | 183 | if ($cursor->match('/^\\)/') === null) { |
|
171 | 30 | $cursor->restoreState($previousState); |
|
172 | |||
173 | 30 | return false; |
|
174 | } |
||
175 | |||
176 | 159 | return ['url' => $dest, 'title' => $title]; |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | * @param Cursor $cursor |
||
181 | * @param ReferenceMap $referenceMap |
||
182 | * @param Delimiter $opener |
||
183 | * @param int $startPos |
||
184 | * |
||
185 | * @return Reference|null |
||
186 | */ |
||
187 | 285 | protected function tryParseReference(Cursor $cursor, ReferenceMap $referenceMap, Delimiter $opener, int $startPos): ?Reference |
|
188 | { |
||
189 | 285 | $savePos = $cursor->saveState(); |
|
190 | 285 | $beforeLabel = $cursor->getPosition(); |
|
191 | 285 | $n = LinkParserHelper::parseLinkLabel($cursor); |
|
192 | 285 | if ($n === 0 || $n === 2) { |
|
193 | // Empty or missing second label |
||
194 | 234 | $reflabel = \mb_substr($cursor->getLine(), $opener->getIndex(), $startPos - $opener->getIndex(), 'utf-8'); |
|
195 | } else { |
||
196 | 63 | $reflabel = \mb_substr($cursor->getLine(), $beforeLabel + 1, $n - 2, 'utf-8'); |
|
197 | } |
||
198 | |||
199 | 285 | if ($n === 0) { |
|
200 | // If shortcut reference link, rewind before spaces we skipped |
||
201 | 210 | $cursor->restoreState($savePos); |
|
202 | } |
||
203 | |||
204 | 285 | return $referenceMap->getReference($reflabel); |
|
205 | } |
||
206 | |||
207 | /** |
||
208 | * @param string $url |
||
209 | * @param string $title |
||
210 | * @param bool $isImage |
||
211 | * |
||
212 | * @return AbstractWebResource |
||
213 | */ |
||
214 | 351 | protected function createInline(string $url, string $title, bool $isImage) |
|
222 | } |
||
223 |