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; }
/** * @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; }