public function parse(ContextInterface $context, Cursor $cursor) { $document = $context->getDocument(); $tip = $context->getTip(); if (!$document->getLastChild() instanceof AttributesDocument) { $attributesDocument = new AttributesDocument(); foreach ($document->getChildren() as $child) { $document->removeChild($child); $attributesDocument->addChild($child); } $document->addChild($attributesDocument); if ($tip instanceof Document) { $context->setTip($attributesDocument); } } $state = $cursor->saveState(); $attributes = AttributesUtils::parse($cursor); if (empty($attributes)) { return false; } if (null !== $cursor->getFirstNonSpaceCharacter()) { $cursor->restoreState($state); return false; } $prepend = $tip instanceof Document || !$tip->getParent() instanceof Document && $context->getBlockCloser()->areAllClosed(); $context->addBlock(new Attributes($attributes, $prepend ? Attributes::PREPEND : Attributes::APPEND)); $context->setBlocksParsed(true); return true; }
private function incorporateLine(ContextInterface $context) { $cursor = new Cursor($context->getLine()); $context->getBlockCloser()->resetTip(); $context->setBlocksParsed(false); $context->setContainer($context->getDocument()); while ($context->getContainer()->hasChildren()) { $lastChild = $context->getContainer()->getLastChild(); if (!$lastChild->isOpen()) { break; } $context->setContainer($lastChild); if (!$context->getContainer()->matchesNextLine($cursor)) { $context->setContainer($context->getContainer()->getParent()); // back up to the last matching block break; } } $context->getBlockCloser()->setLastMatchedContainer($context->getContainer()); // Check to see if we've hit 2nd blank line; if so break out of list: if ($cursor->isBlank() && $context->getContainer()->endsWithBlankLine()) { $this->breakOutOfLists($context, $context->getContainer()); } while (!$context->getContainer()->isCode() && !$context->getBlocksParsed()) { $parsed = false; foreach ($this->environment->getBlockParsers() as $parser) { if ($parser->parse($context, $cursor)) { $parsed = true; break; } } if (!$parsed || $context->getContainer()->acceptsLines()) { $context->setBlocksParsed(true); } } // What remains at the offset is a text line. Add the text to the appropriate container. // First check for a lazy paragraph continuation: if (!$context->getBlockCloser()->areAllClosed() && !$cursor->isBlank() && $context->getTip() instanceof Paragraph && count($context->getTip()->getStrings()) > 0) { // lazy paragraph continuation $context->getTip()->addLine($cursor->getRemainder()); } else { // not a lazy continuation // finalize any blocks not matched $context->getBlockCloser()->closeUnmatchedBlocks(); // Determine whether the last line is blank, updating parents as needed $context->getContainer()->setLastLineBlank($cursor, $context->getLineNumber()); // Handle any remaining cursor contents if ($context->getContainer()->isOpen()) { $context->getContainer()->handleRemainingContents($context, $cursor); } elseif (!$cursor->isBlank()) { // Create paragraph container for line $context->addBlock(new Paragraph()); $cursor->advanceToFirstNonSpace(); $context->getTip()->addLine($cursor->getRemainder()); } } }
/** * @param ContextInterface $context * @param InlineParserContext $inlineContext * * @return bool */ public function parse(ContextInterface $context, 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() === '!'; // Instead of copying a slice, we null out the parts of inlines that don't correspond to linkText; later, we'll // collapse them. This is awkward, and could be simplified if we made inlines a linked list instead $inlines = $inlineContext->getInlines(); $labelInlines = new ArrayCollection($inlines->toArray()); $this->nullify($labelInlines, 0, $opener->getPos() + 1); $cursor->advance(); // Check to see if we have a link/image if (!($link = $this->tryParseLink($cursor, $context->getDocument()->getReferenceMap(), $opener, $startPos))) { // No match $inlineContext->getDelimiterStack()->removeDelimiter($opener); // Remove this opener from stack $cursor->restoreState($previousState); return false; } $delimiterStack = $inlineContext->getDelimiterStack(); $stackBottom = $opener->getPrevious(); foreach ($this->environment->getInlineProcessors() as $inlineProcessor) { $inlineProcessor->processInlines($labelInlines, $delimiterStack, $stackBottom); } if ($delimiterStack instanceof DelimiterStack) { $delimiterStack->removeAll($stackBottom); } // Remove the part of inlines that become link_text $this->nullify($inlines, $opener->getPos(), $inlines->count()); // 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('['); } $inlines->add($this->createInline($link['url'], $labelInlines, $link['title'], $isImage)); return true; }
/** * Sets the container to the last open child (or its parent) * * @param ContextInterface $context * @param Cursor $cursor */ private function resetContainer(ContextInterface $context, Cursor $cursor) { $context->setContainer($context->getDocument()); while ($context->getContainer()->hasChildren()) { $lastChild = $context->getContainer()->getLastChild(); if (!$lastChild->isOpen()) { break; } $context->setContainer($lastChild); if (!$context->getContainer()->matchesNextLine($cursor)) { $context->setContainer($context->getContainer()->getParent()); // back up to the last matching block break; } } }