/** * @param InlineParserContext $inlineContext * * @return bool */ public function parse(InlineParserContext $inlineContext) { $cursor = $inlineContext->getCursor(); $startPos = $cursor->getPosition(); $previousState = $cursor->saveState(); // 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; } $isImage = $opener->getChar() === '!'; $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; } $inline = $this->createInline($link['url'], $link['title'], $isImage); $opener->getInlineNode()->replaceWith($inline); while (($label = $inline->next()) !== null) { $inline->appendChild($label); } $delimiterStack = $inlineContext->getDelimiterStack(); $stackBottom = $opener->getPrevious(); foreach ($this->environment->getInlineProcessors() as $inlineProcessor) { $inlineProcessor->processInlines($delimiterStack, $stackBottom); } if ($delimiterStack instanceof DelimiterStack) { $delimiterStack->removeAll($stackBottom); } // 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; }