/** * @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; }
private function parseRow($line, array $columns, $type = TableCell::TYPE_BODY) { $cells = RegexHelper::matchAll(self::REGEXP_CELLS, $line); if (null === $cells || !is_array($cells[0])) { return; } $row = new TableRow(); foreach ($cells[0] as $i => $cell) { $row->addChild(new TableCell(trim($cell), $type, isset($columns[$i]) ? $columns[$i] : null)); } for ($j = count($columns) - 1; $j > $i; --$j) { $row->addChild(new TableCell('', $type, null)); } return $row; }
/** * @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; }
/** * @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; }
/** * @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; }
/** * @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()); }
private function parseCaption($line) { $caption = RegexHelper::matchAll(self::REGEXP_CAPTION, $line); if (null === $caption) { return; } return new TableCaption($caption[1], $caption[2]); }