X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fcebe%2Fmarkdown%2Fblock%2FTableTrait.php;fp=vendor%2Fcebe%2Fmarkdown%2Fblock%2FTableTrait.php;h=bd4d9fea40ab2b13e35db7f0d985e52e297b2b52;hp=0000000000000000000000000000000000000000;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad diff --git a/vendor/cebe/markdown/block/TableTrait.php b/vendor/cebe/markdown/block/TableTrait.php new file mode 100644 index 000000000..bd4d9fea4 --- /dev/null +++ b/vendor/cebe/markdown/block/TableTrait.php @@ -0,0 +1,116 @@ + [], + 'rows' => [], + ]; + $beginsWithPipe = $lines[$current][0] === '|'; + for ($i = $current, $count = count($lines); $i < $count; $i++) { + $line = $lines[$i]; + + if ($i == $current+1) { // skip second line + $cols = explode('|', trim($line, ' |')); + foreach($cols as $col) { + $col = trim($col); + if (empty($col)) { + $block['cols'][] = ''; + continue; + } + $l = ($col[0] === ':'); + $r = (substr($col, -1, 1) === ':'); + if ($l && $r) { + $block['cols'][] = 'center'; + } elseif ($l) { + $block['cols'][] = 'left'; + } elseif ($r) { + $block['cols'][] = 'right'; + } else { + $block['cols'][] = ''; + } + } + + continue; + } + if (trim($line) === '' || $beginsWithPipe && $line[0] !== '|') { + break; + } + if (substr($line, -2, 2) !== '\\|' || substr($line, -3, 3) === '\\\\|') { + $block['rows'][] = trim($line, '| '); + } else { + $block['rows'][] = ltrim($line, '| '); + } + } + + return [$block, --$i]; + } + + /** + * render a table block + */ + protected function renderTable($block) + { + $content = ''; + $this->_tableCellAlign = $block['cols']; + $content .= "\n"; + $first = true; + foreach($block['rows'] as $row) { + $this->_tableCellTag = $first ? 'th' : 'td'; + $align = empty($this->_tableCellAlign[$this->_tableCellCount]) ? '' : ' align="' . $this->_tableCellAlign[$this->_tableCellCount++] . '"'; + $tds = "<$this->_tableCellTag$align>" . $this->renderAbsy($this->parseInline($row)) . "_tableCellTag>"; // TODO move this to the consume step + $content .= "$tds\n"; + if ($first) { + $content .= "\n\n"; + } + $first = false; + $this->_tableCellCount = 0; + } + return "\n$content\n
\n"; + } + + /** + * @marker | + */ + protected function parseTd($markdown) + { + if (isset($this->context[1]) && $this->context[1] === 'table') { + $align = empty($this->_tableCellAlign[$this->_tableCellCount]) ? '' : ' align="' . $this->_tableCellAlign[$this->_tableCellCount++] . '"'; + return [['text', "_tableCellTag><$this->_tableCellTag$align>"], isset($markdown[1]) && $markdown[1] === ' ' ? 2 : 1]; // TODO make a absy node + } + return [['text', $markdown[0]], 1]; + } + + abstract protected function parseInline($text); + abstract protected function renderAbsy($absy); +}