diff options
Diffstat (limited to 'app/CommonMark')
| -rw-r--r-- | app/CommonMark/CensusTableExtension.php | 68 | ||||
| -rw-r--r-- | app/CommonMark/CensusTableParser.php | 119 | ||||
| -rw-r--r-- | app/CommonMark/XrefExtension.php | 54 | ||||
| -rw-r--r-- | app/CommonMark/XrefParser.php | 100 |
4 files changed, 178 insertions, 163 deletions
diff --git a/app/CommonMark/CensusTableExtension.php b/app/CommonMark/CensusTableExtension.php index 82c92dbdc4..915a3c33a2 100644 --- a/app/CommonMark/CensusTableExtension.php +++ b/app/CommonMark/CensusTableExtension.php @@ -33,38 +33,42 @@ use Webuni\CommonMark\TableExtension\TableRowsRenderer; * * Based on the table parser from webuni/commonmark-table-extension. */ -class CensusTableExtension extends Extension { - /** - * Returns a list of block parsers to add to the existing list - * - * @return BlockParserInterface[] - */ - public function getBlockParsers() { - return [ - new CensusTableParser, - ]; - } +class CensusTableExtension extends Extension +{ + /** + * Returns a list of block parsers to add to the existing list + * + * @return BlockParserInterface[] + */ + public function getBlockParsers() + { + return [ + new CensusTableParser, + ]; + } - /** - * Returns a list of block renderers to add to the existing list - * - * The list keys are the block class names which the corresponding value (renderer) will handle. - * - * @return BlockRendererInterface[] - */ - public function getBlockRenderers() { - return [ - Table::class => new TableRenderer, - TableRows::class => new TableRowsRenderer, - TableRow::class => new TableRowRenderer, - TableCell::class => new TableCellRenderer, - ]; - } + /** + * Returns a list of block renderers to add to the existing list + * + * The list keys are the block class names which the corresponding value (renderer) will handle. + * + * @return BlockRendererInterface[] + */ + public function getBlockRenderers() + { + return [ + Table::class => new TableRenderer, + TableRows::class => new TableRowsRenderer, + TableRow::class => new TableRowRenderer, + TableCell::class => new TableCellRenderer, + ]; + } - /** - * @return string - */ - public function getName() { - return 'censustabletable'; - } + /** + * @return string + */ + public function getName() + { + return 'censustabletable'; + } } diff --git a/app/CommonMark/CensusTableParser.php b/app/CommonMark/CensusTableParser.php index eaf118b4e4..6302a3f3e4 100644 --- a/app/CommonMark/CensusTableParser.php +++ b/app/CommonMark/CensusTableParser.php @@ -29,75 +29,78 @@ use Webuni\CommonMark\TableExtension\TableRow; * * Based on the table parser from webuni/commonmark-table-extension. */ -class CensusTableParser extends AbstractBlockParser { - const REGEX_CENSUS_TABLE_HEADER = '/^\.b\.[^.|]+(?:\|\.b\.[^.|]+)*$/'; +class CensusTableParser extends AbstractBlockParser +{ + const REGEX_CENSUS_TABLE_HEADER = '/^\.b\.[^.|]+(?:\|\.b\.[^.|]+)*$/'; - /** - * Parse a paragraph of text with the following stucture: - * - * .b.HEADING1|.b.HEADING2|.b.HEADING3 - * COL1|COL2|COL3 - * COL1|COL2|COL3 - * - * @param ContextInterface $context - * @param Cursor $cursor - * - * @return bool - */ - public function parse(ContextInterface $context, Cursor $cursor) { - $container = $context->getContainer(); + /** + * Parse a paragraph of text with the following stucture: + * + * .b.HEADING1|.b.HEADING2|.b.HEADING3 + * COL1|COL2|COL3 + * COL1|COL2|COL3 + * + * @param ContextInterface $context + * @param Cursor $cursor + * + * @return bool + */ + public function parse(ContextInterface $context, Cursor $cursor) + { + $container = $context->getContainer(); - // Replace paragraphs with tables - if (!$container instanceof Paragraph) { - return false; - } + // Replace paragraphs with tables + if (!$container instanceof Paragraph) { + return false; + } - $lines = $container->getStrings(); + $lines = $container->getStrings(); - $first_line = array_pop($lines); + $first_line = array_pop($lines); - if (!preg_match(self::REGEX_CENSUS_TABLE_HEADER, $first_line)) { - return false; - } + if (!preg_match(self::REGEX_CENSUS_TABLE_HEADER, $first_line)) { + return false; + } - $head = $this->parseRow($first_line, TableCell::TYPE_HEAD); + $head = $this->parseRow($first_line, TableCell::TYPE_HEAD); - $table = new Table(function (Cursor $cursor) use (&$table) { - $row = $this->parseRow($cursor->getLine(), TableCell::TYPE_BODY); - if ($row === null) { - return false; - } - $table->getBody()->appendChild($row); + $table = new Table(function (Cursor $cursor) use (&$table) { + $row = $this->parseRow($cursor->getLine(), TableCell::TYPE_BODY); + if ($row === null) { + return false; + } + $table->getBody()->appendChild($row); - return true; - }); + return true; + }); - $table->getHead()->appendChild($head); - $context->replaceContainerBlock($table); + $table->getHead()->appendChild($head); + $context->replaceContainerBlock($table); - return true; - } + return true; + } - /** - * @param string $line - * @param string $type - * - * @return TableRow|null - */ - private function parseRow($line, $type) { - if (strpos($line, '|') === false) { - return null; - } + /** + * @param string $line + * @param string $type + * + * @return TableRow|null + */ + private function parseRow($line, $type) + { + if (strpos($line, '|') === false) { + return null; + } - $row = new TableRow; - foreach (explode('|', $line) as $cell) { - // Strip leading ".b." from <th> cells - if ($type === TableCell::TYPE_HEAD && substr_compare($cell, '.b.', 0)) { - $cell = substr($cell, 3); - } - $row->appendChild(new TableCell($cell, $type, null)); - } + $row = new TableRow; + foreach (explode('|', $line) as $cell) { + // Strip leading ".b." from <th> cells + if ($type === TableCell::TYPE_HEAD && substr_compare($cell, '.b.', 0)) { + $cell = substr($cell, 3); + } + $row->appendChild(new TableCell($cell, $type, null)); + } - return $row; - } + return $row; + } } diff --git a/app/CommonMark/XrefExtension.php b/app/CommonMark/XrefExtension.php index fbd9713b10..fc0d52bbc1 100644 --- a/app/CommonMark/XrefExtension.php +++ b/app/CommonMark/XrefExtension.php @@ -21,32 +21,36 @@ use League\CommonMark\Extension\Extension; /** * Convert XREFs within markdown text to links */ -class XrefExtension extends Extension { - /** @var Tree - match XREFs in this tree */ - private $tree; +class XrefExtension extends Extension +{ + /** @var Tree - match XREFs in this tree */ + private $tree; - /** - * MarkdownXrefParser constructor. - * - * @param Tree $tree - */ - public function __construct(Tree $tree) { - $this->tree = $tree; - } + /** + * MarkdownXrefParser constructor. + * + * @param Tree $tree + */ + public function __construct(Tree $tree) + { + $this->tree = $tree; + } - /** - * @return array - */ - public function getInlineParsers() { - return [ - new XrefParser($this->tree), - ]; - } + /** + * @return array + */ + public function getInlineParsers() + { + return [ + new XrefParser($this->tree), + ]; + } - /** - * @return string - */ - public function getName() { - return 'xref'; - } + /** + * @return string + */ + public function getName() + { + return 'xref'; + } } diff --git a/app/CommonMark/XrefParser.php b/app/CommonMark/XrefParser.php index 4f2abfbb88..a5af0299d8 100644 --- a/app/CommonMark/XrefParser.php +++ b/app/CommonMark/XrefParser.php @@ -24,64 +24,68 @@ use League\CommonMark\InlineParserContext; /** * Convert XREFs within markdown text to links */ -class XrefParser extends AbstractInlineParser { - /** @var Tree - match XREFs in this tree */ - private $tree; +class XrefParser extends AbstractInlineParser +{ + /** @var Tree - match XREFs in this tree */ + private $tree; - /** - * MarkdownXrefParser constructor. - * - * @param Tree $tree - */ - public function __construct(Tree $tree) { - $this->tree = $tree; - } + /** + * MarkdownXrefParser constructor. + * + * @param Tree $tree + */ + public function __construct(Tree $tree) + { + $this->tree = $tree; + } - /** - * We are only interested in text that begins with '@'. - * - * @return array - */ - public function getCharacters() { - return ['@']; - } + /** + * We are only interested in text that begins with '@'. + * + * @return array + */ + public function getCharacters() + { + return ['@']; + } - /** - * @param InlineParserContext $context - * - * @return bool - */ - public function parse(InlineParserContext $context) { - // The cursor should be positioned on the opening '@'. - $cursor = $context->getcursor(); + /** + * @param InlineParserContext $context + * + * @return bool + */ + public function parse(InlineParserContext $context) + { + // The cursor should be positioned on the opening '@'. + $cursor = $context->getcursor(); - // If this isn't the start of an XREF, we'll need to rewind. - $previous_state = $cursor->saveState(); + // If this isn't the start of an XREF, we'll need to rewind. + $previous_state = $cursor->saveState(); - $handle = $cursor->match('/@' . WT_REGEX_XREF . '@/'); - if (empty($handle)) { - // Not an XREF? - $cursor->restoreState($previous_state); + $handle = $cursor->match('/@' . WT_REGEX_XREF . '@/'); + if (empty($handle)) { + // Not an XREF? + $cursor->restoreState($previous_state); - return false; - } + return false; + } - $xref = trim($handle, '@'); + $xref = trim($handle, '@'); - $record = GedcomRecord::getInstance($xref, $this->tree); + $record = GedcomRecord::getInstance($xref, $this->tree); - if ($record === null) { - // Linked record does not exist? - $cursor->restoreState($previous_state); + if ($record === null) { + // Linked record does not exist? + $cursor->restoreState($previous_state); - return false; - } + return false; + } - $url = $record->url(); - $label = $handle; - $title = strip_tags($record->getFullName()); - $context->getContainer()->appendChild(new Link($url, $label, $title)); + $url = $record->url(); + $label = $handle; + $title = strip_tags($record->getFullName()); + $context->getContainer()->appendChild(new Link($url, $label, $title)); - return true; - } + return true; + } } |
