Пример #1
0
 /**
  * @param ContextInterface $context
  * @param Cursor $cursor
  *
  * @return bool
  */
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     $tmpCursor = clone $cursor;
     $indent = $tmpCursor->advanceWhileMatches(' ', 3);
     $rest = $tmpCursor->getRemainder();
     $data = new ListData();
     if ($matches = RegexHelper::matchAll('/^[*+-]( +|$)/', $rest)) {
         $spacesAfterMarker = strlen($matches[1]);
         $data->type = ListBlock::TYPE_UNORDERED;
         $data->delimiter = null;
         $data->bulletChar = $matches[0][0];
     } elseif ($matches = RegexHelper::matchAll('/^(\\d+)([.)])( +|$)/', $rest)) {
         $spacesAfterMarker = strlen($matches[3]);
         $data->type = ListBlock::TYPE_ORDERED;
         $data->start = intval($matches[1]);
         $data->delimiter = $matches[2];
         $data->bulletChar = null;
     } else {
         return false;
     }
     $data->padding = $this->calculateListMarkerPadding($matches[0], $spacesAfterMarker, $rest);
     $cursor->advanceToFirstNonSpace();
     $cursor->advanceBy($data->padding);
     // list item
     $data->markerOffset = $indent;
     // add the list if needed
     $container = $context->getContainer();
     if (!$container || !$context->getContainer() instanceof ListBlock || !$data->equals($container->getListData())) {
         $context->addBlock(new ListBlock($data));
     }
     // add the list item
     $context->addBlock(new ListItem($data));
     return true;
 }
Пример #2
0
 /**
  * @param ContextInterface $context
  * @param Cursor $cursor
  *
  * @return bool
  */
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     if (!$cursor->isIndented()) {
         return false;
     }
     $context->setBlocksParsed(true);
     return true;
 }
 /**
  * @param ContextInterface $context
  * @param Cursor $cursor
  *
  * @return bool
  */
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     if ($cursor->getIndent() < IndentedCodeParser::CODE_INDENT_LEVEL) {
         return false;
     }
     $context->setBlocksParsed(true);
     return true;
 }
Пример #4
0
 /**
  * @param ContextInterface $context
  * @param Cursor           $cursor
  */
 public function handleRemainingContents(ContextInterface $context, Cursor $cursor)
 {
     if ($cursor->isBlank()) {
         return;
     }
     $context->addBlock(new Paragraph());
     $cursor->advanceToFirstNonSpace();
     $context->getTip()->addLine($cursor->getRemainder());
 }
Пример #5
0
 /**
  * @param ContextInterface $context
  * @param Cursor $cursor
  *
  * @return bool
  */
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     $match = RegexHelper::matchAt(RegexHelper::getInstance()->getHtmlBlockOpenRegex(), $cursor->getLine(), $cursor->getFirstNonSpacePosition());
     if ($match === null) {
         return false;
     }
     $context->addBlock(new HtmlBlock());
     $context->setBlocksParsed(true);
     return true;
 }
Пример #6
0
 /**
  * @param ContextInterface $context
  * @param Cursor $cursor
  *
  * @return bool
  */
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     if ($cursor->getFirstNonSpaceCharacter() !== '>') {
         return false;
     }
     $cursor->advanceToFirstNonSpace();
     $cursor->advance();
     if ($cursor->getCharacter() === ' ') {
         $cursor->advance();
     }
     $context->addBlock(new BlockQuote());
     return true;
 }
Пример #7
0
 /**
  * @param ContextInterface $context
  * @param Cursor $cursor
  *
  * @return bool
  */
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     $previousState = $cursor->saveState();
     $indent = $cursor->advanceToFirstNonSpace();
     $fence = $cursor->match('/^`{3,}(?!.*`)|^~{3,}(?!.*~)/');
     if (!$fence) {
         $cursor->restoreState($previousState);
         return false;
     }
     // fenced code block
     $fenceLength = strlen($fence);
     $context->addBlock(new FencedCode($fenceLength, $fence[0], $indent));
     return true;
 }
 /**
  * @param ContextInterface $context
  * @param Cursor           $cursor
  *
  * @return bool
  */
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     if (!$cursor->isIndented()) {
         return false;
     }
     if ($context->getTip() instanceof Paragraph) {
         return false;
     }
     if ($cursor->isBlank()) {
         return false;
     }
     $cursor->advanceBy(Cursor::INDENT_LEVEL, true);
     $context->addBlock(new IndentedCode());
     return true;
 }
Пример #9
0
 /**
  * @param \League\CommonMark\ContextInterface $context
  * @param \League\CommonMark\Cursor $cursor
  *
  * @return bool
  */
 public function parse(\League\CommonMark\ContextInterface $context, \League\CommonMark\Cursor $cursor)
 {
     $line = $cursor->getLine();
     //either a line starting with 'example:' (expected to have a set of links)
     //or a line starting with [example] (a single link)
     //remove potential markdown formatting (except what we need)
     $check = preg_replace('#[^a-z:\\[\\]]#', '', strtolower($line));
     if (substr($check, 0, 8) != 'example:' and substr($check, 0, 9) != '[example]') {
         return false;
     }
     $context->addBlock(new ExampleElement($cursor->getLine(), $this->path));
     $cursor->advanceBy(strlen($line));
     $context->setBlocksParsed(true);
     return true;
 }
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     $state = $cursor->saveState();
     $attributes = AttributesUtils::parse($cursor);
     if (empty($attributes)) {
         return false;
     }
     if (null !== $cursor->getFirstNonSpaceCharacter()) {
         $cursor->restoreState($state);
         return false;
     }
     $context->addBlock(new Attributes($attributes));
     $context->setBlocksParsed(true);
     return true;
 }
Пример #11
0
 /**
  * @param ContextInterface $context
  * @param Cursor $cursor
  *
  * @return bool
  */
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     if ($cursor->isIndented()) {
         return false;
     }
     $previousState = $cursor->saveState();
     $cursor->advanceToFirstNonSpace();
     $fence = $cursor->match('/^\\[TOC\\]/');
     if (is_null($fence)) {
         $cursor->restoreState($previousState);
         return false;
     }
     $context->addBlock(new TableOfContents());
     return true;
 }
 /**
  * @param ContextInterface $context
  * @param Cursor           $cursor
  *
  * @return bool
  */
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     if ($cursor->isIndented()) {
         return false;
     }
     $match = RegexHelper::matchAt(RegexHelper::getInstance()->getThematicBreakRegex(), $cursor->getLine(), $cursor->getFirstNonSpacePosition());
     if ($match === null) {
         return false;
     }
     // Advance to the end of the string, consuming the entire line (of the thematic break)
     $cursor->advanceBy(mb_strlen($cursor->getRemainder()));
     $context->addBlock(new ThematicBreak());
     $context->setBlocksParsed(true);
     return true;
 }
Пример #13
0
 /**
  * @param ContextInterface $context
  * @param Cursor $cursor
  *
  * @return bool
  */
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     if ($cursor->getFirstNonSpaceCharacter() !== 'A' || $cursor->getCharacter($cursor->getFirstNonSpacePosition() + 1) !== '>') {
         return false;
     }
     $cursor->advanceToFirstNonSpace();
     if ($cursor->peek() === '>') {
         $cursor->advanceBy(2);
         if ($cursor->getCharacter() === ' ') {
             $cursor->advance();
         }
     }
     $context->addBlock(new Aside());
     return true;
 }
Пример #14
0
 /**
  * @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;
 }
Пример #15
0
 /**
  * @param ContextInterface $context
  * @param Cursor $cursor
  *
  * @return bool
  */
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     $match = RegexHelper::matchAll('/^#{1,6}(?: +|$)/', $cursor->getLine(), $cursor->getFirstNonSpacePosition());
     if (!$match) {
         return false;
     }
     $cursor->advanceToFirstNonSpace();
     $cursor->advanceBy(strlen($match[0]));
     $level = strlen(trim($match[0]));
     $str = $cursor->getRemainder();
     $str = preg_replace('/^ *#+ *$/', '', $str);
     $str = preg_replace('/ +#+ *$/', '', $str);
     $context->addBlock(new Header($level, $str));
     $context->setBlocksParsed(true);
     return true;
 }
Пример #16
0
 /**
  * @param ContextInterface $context
  * @param Cursor $cursor
  *
  * @return bool
  */
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     if (!in_array($cursor->getFirstNonSpaceCharacter(), IconBlock::getIconBlockTypes()) || $cursor->getCharacter($cursor->getFirstNonSpacePosition() + 1) !== '>') {
         return false;
     }
     $type = $cursor->getFirstNonSpaceCharacter();
     $cursor->advanceToFirstNonSpace();
     if ($cursor->peek() === '>') {
         $cursor->advanceBy(2);
         if ($cursor->getCharacter() === ' ') {
             $cursor->advance();
         }
     }
     $context->addBlock(new IconBlock($type));
     return true;
 }
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     $container = $context->getContainer();
     if (!$container instanceof Paragraph) {
         return false;
     }
     $lines = $container->getStrings();
     if (count($lines) < 1) {
         return false;
     }
     $match = RegexHelper::matchAll(self::REGEXP_DEFINITION, $cursor->getLine(), $cursor->getFirstNonSpacePosition());
     if (null === $match) {
         return false;
     }
     $columns = $this->parseColumns($match);
     $head = $this->parseRow(trim(array_pop($lines)), $columns, TableCell::TYPE_HEAD);
     if (null === $head) {
         return false;
     }
     $table = new Table(function (Cursor $cursor) use(&$table, $columns) {
         $row = $this->parseRow($cursor->getLine(), $columns);
         if (null === $row) {
             if (null !== $table->getCaption()) {
                 return false;
             }
             if (null !== ($caption = $this->parseCaption($cursor->getLine()))) {
                 $table->setCaption($caption);
                 return true;
             }
             return false;
         }
         $table->getBody()->appendChild($row);
         return true;
     });
     $table->getHead()->appendChild($head);
     if (count($lines) >= 1) {
         $paragraph = new Paragraph();
         foreach ($lines as $line) {
             $paragraph->addLine($line);
         }
         $context->replaceContainerBlock($paragraph);
         $context->addBlock($table);
     } else {
         $context->replaceContainerBlock($table);
     }
     return true;
 }
Пример #18
0
 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 Cursor           $cursor
  *
  * @return bool
  */
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     if ($cursor->isIndented()) {
         return false;
     }
     if (!$context->getContainer() instanceof Paragraph) {
         return false;
     }
     $match = RegexHelper::matchAll('/^(?:=+|-+)[ \\t]*$/', $cursor->getLine(), $cursor->getFirstNonSpacePosition());
     if ($match === null) {
         return false;
     }
     $level = $match[0][0] === '=' ? 1 : 2;
     $strings = $context->getContainer()->getStrings();
     $context->replaceContainerBlock(new Heading($level, $strings));
     return true;
 }
 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;
 }
 /**
  * @param ContextInterface $context
  * @param Cursor           $cursor
  *
  * @return bool
  */
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     if ($cursor->isIndented()) {
         return false;
     }
     if ($cursor->getFirstNonSpaceCharacter() !== '<') {
         return false;
     }
     $savedState = $cursor->saveState();
     $cursor->advanceToFirstNonSpace();
     $line = $cursor->getRemainder();
     for ($blockType = 1; $blockType <= 7; $blockType++) {
         $match = RegexHelper::matchAt(RegexHelper::getHtmlBlockOpenRegex($blockType), $line);
         if ($match !== null && ($blockType < 7 || !$context->getContainer() instanceof Paragraph)) {
             $cursor->restoreState($savedState);
             $context->addBlock(new HtmlBlock($blockType));
             $context->setBlocksParsed(true);
             return true;
         }
     }
     $cursor->restoreState($savedState);
     return false;
 }
Пример #22
0
 /**
  * @param ContextInterface $context
  * @param Cursor           $cursor
  *
  * @return bool
  */
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     if ($cursor->isIndented() && !$context->getContainer() instanceof ListBlock) {
         return false;
     }
     $tmpCursor = clone $cursor;
     $tmpCursor->advanceToFirstNonSpace();
     $rest = $tmpCursor->getRemainder();
     $data = new ListData();
     $data->markerOffset = $cursor->getIndent();
     if ($matches = RegexHelper::matchAll('/^[*+-]/', $rest)) {
         $data->type = ListBlock::TYPE_UNORDERED;
         $data->delimiter = null;
         $data->bulletChar = $matches[0][0];
     } elseif (($matches = RegexHelper::matchAll('/^(\\d{1,9})([.)])/', $rest)) && (!$context->getContainer() instanceof Paragraph || $matches[1] === '1')) {
         $data->type = ListBlock::TYPE_ORDERED;
         $data->start = intval($matches[1]);
         $data->delimiter = $matches[2];
         $data->bulletChar = null;
     } else {
         return false;
     }
     $markerLength = strlen($matches[0]);
     // Make sure we have spaces after
     $nextChar = $tmpCursor->peek($markerLength);
     if (!($nextChar === null || $nextChar === "\t" || $nextChar === ' ')) {
         return false;
     }
     // If it interrupts paragraph, make sure first line isn't blank
     if ($context->getContainer() instanceof Paragraph && !RegexHelper::matchAt(RegexHelper::REGEX_NON_SPACE, $rest, $markerLength)) {
         return false;
     }
     // We've got a match! Advance offset and calculate padding
     $cursor->advanceToFirstNonSpace();
     // to start of marker
     $cursor->advanceBy($markerLength, true);
     // to end of marker
     $data->padding = $this->calculateListMarkerPadding($cursor, $markerLength);
     // add the list if needed
     $container = $context->getContainer();
     if (!$container || !$context->getContainer() instanceof ListBlock || !$data->equals($container->getListData())) {
         $context->addBlock(new ListBlock($data));
     }
     // add the list item
     $context->addBlock(new ListItem($data));
     return true;
 }
Пример #23
0
 /**
  * @param ContextInterface $context
  * @param Cursor           $cursor
  */
 public function handleRemainingContents(ContextInterface $context, Cursor $cursor)
 {
     // create paragraph container for line
     $context->addBlock(new Paragraph());
     $cursor->advanceToFirstNonSpace();
     $context->getTip()->addLine($cursor->getRemainder());
 }
Пример #24
0
 /**
  * @param ContextInterface $context
  * @param Cursor           $cursor
  */
 public function handleRemainingContents(ContextInterface $context, Cursor $cursor)
 {
     $context->getTip()->addLine($cursor->getRemainder());
     // Check for end condition
     if ($this->type >= self::TYPE_1_CODE_CONTAINER && $this->type <= self::TYPE_5_CDATA) {
         if ($cursor->match(RegexHelper::getHtmlBlockCloseRegex($this->type)) !== null) {
             $this->finalize($context, $context->getLineNumber());
         }
     }
 }
Пример #25
0
 /**
  * Finalize the block; mark it closed for modification
  *
  * @param ContextInterface $context
  */
 public function finalize(ContextInterface $context)
 {
     if (!$this->open) {
         return;
         // TODO: Throw AlreadyClosedException?
     }
     $this->open = false;
     if ($context->getLineNumber() > $this->getStartLine()) {
         $this->endLine = $context->getLineNumber() - 1;
     } else {
         $this->endLine = $context->getLineNumber();
     }
     $context->setTip($context->getTip()->getParent());
 }
Пример #26
0
 /**
  * Finalize the block; mark it closed for modification
  *
  * @param ContextInterface $context
  * @param int              $endLineNumber
  */
 public function finalize(ContextInterface $context, $endLineNumber)
 {
     if (!$this->open) {
         return;
         // TODO: Throw AlreadyClosedException?
     }
     $this->open = false;
     $this->endLine = $endLineNumber;
     $context->setTip($context->getTip()->parent());
 }
 /**
  * @param ContextInterface $context
  * @param Cursor           $cursor
  */
 public function handleRemainingContents(ContextInterface $context, Cursor $cursor)
 {
     $cursor->advanceToFirstNonSpace();
     $context->getTip()->addLine($cursor->getRemainder());
 }
Пример #28
0
 /**
  * @param ContextInterface $context
  * @param Cursor           $cursor
  */
 public function handleRemainingContents(ContextInterface $context, Cursor $cursor)
 {
     /** @var FencedCode $container */
     $container = $context->getContainer();
     // check for closing code fence
     if ($cursor->getIndent() <= 3 && $cursor->getFirstNonSpaceCharacter() == $container->getChar()) {
         $match = RegexHelper::matchAll('/^(?:`{3,}|~{3,})(?= *$)/', $cursor->getLine(), $cursor->getFirstNonSpacePosition());
         if (strlen($match[0]) >= $container->getLength()) {
             // don't add closing fence to container; instead, close it:
             $this->setLength(-1);
             // -1 means we've passed closer
             return;
         }
     }
     $context->getTip()->addLine($cursor->getRemainder());
 }
Пример #29
0
 /**
  * @param ContextInterface $context
  * @param Cursor           $cursor
  *
  * @return bool
  */
 private function isLazyParagraphContinuation(ContextInterface $context, Cursor $cursor)
 {
     return !$context->getBlockCloser()->areAllClosed() && !$cursor->isBlank() && $context->getTip() instanceof Paragraph && count($context->getTip()->getStrings()) > 0;
 }
 /**
  * @return bool
  */
 public function areAllClosed()
 {
     return $this->context->getTip() === $this->lastMatchedContainer;
 }