Removed modules/contrib/media module to allow update to the core media module
[yaffs-website] / vendor / cebe / markdown / block / TableTrait.php
1 <?php
2 /**
3  * @copyright Copyright (c) 2014 Carsten Brandt
4  * @license https://github.com/cebe/markdown/blob/master/LICENSE
5  * @link https://github.com/cebe/markdown#readme
6  */
7
8 namespace cebe\markdown\block;
9
10 /**
11  * Adds the table blocks
12  */
13 trait TableTrait
14 {
15         private $_tableCellTag = 'td';
16         private $_tableCellCount = 0;
17         private $_tableCellAlign = [];
18
19         /**
20          * identify a line as the beginning of a table block.
21          */
22         protected function identifyTable($line, $lines, $current)
23         {
24                 return strpos($line, '|') !== false && preg_match('~|.*|~', $line) && isset($lines[$current + 1]) && preg_match('~^[\s\|\:-]+$~', $lines[$current + 1]);
25         }
26
27         /**
28          * Consume lines for a table
29          */
30         protected function consumeTable($lines, $current)
31         {
32                 // consume until newline
33
34                 $block = [
35                         'table',
36                         'cols' => [],
37                         'rows' => [],
38                 ];
39                 $beginsWithPipe = $lines[$current][0] === '|';
40                 for ($i = $current, $count = count($lines); $i < $count; $i++) {
41                         $line = $lines[$i];
42
43                         if ($i == $current+1) { // skip second line
44                                 $cols = explode('|', trim($line, ' |'));
45                                 foreach($cols as $col) {
46                                         $col = trim($col);
47                                         if (empty($col)) {
48                                                 $block['cols'][] = '';
49                                                 continue;
50                                         }
51                                         $l = ($col[0] === ':');
52                                         $r = (substr($col, -1, 1) === ':');
53                                         if ($l && $r) {
54                                                 $block['cols'][] = 'center';
55                                         } elseif ($l) {
56                                                 $block['cols'][] = 'left';
57                                         } elseif ($r) {
58                                                 $block['cols'][] = 'right';
59                                         } else {
60                                                 $block['cols'][] = '';
61                                         }
62                                 }
63
64                                 continue;
65                         }
66                         if (trim($line) === '' || $beginsWithPipe && $line[0] !== '|') {
67                                 break;
68                         }
69                         if (substr($line, -2, 2) !== '\\|' || substr($line, -3, 3) === '\\\\|') {
70                                 $block['rows'][] = trim($line, '| ');
71                         } else {
72                                 $block['rows'][] = ltrim($line, '| ');
73                         }
74                 }
75
76                 return [$block, --$i];
77         }
78
79         /**
80          * render a table block
81          */
82         protected function renderTable($block)
83         {
84                 $content = '';
85                 $this->_tableCellAlign = $block['cols'];
86                 $content .= "<thead>\n";
87                 $first = true;
88                 foreach($block['rows'] as $row) {
89                         $this->_tableCellTag = $first ? 'th' : 'td';
90                         $align = empty($this->_tableCellAlign[$this->_tableCellCount]) ? '' : ' align="' . $this->_tableCellAlign[$this->_tableCellCount++] . '"';
91                         $tds = "<$this->_tableCellTag$align>" . $this->renderAbsy($this->parseInline($row)) . "</$this->_tableCellTag>"; // TODO move this to the consume step
92                         $content .= "<tr>$tds</tr>\n";
93                         if ($first) {
94                                 $content .= "</thead>\n<tbody>\n";
95                         }
96                         $first = false;
97                         $this->_tableCellCount = 0;
98                 }
99                 return "<table>\n$content</tbody>\n</table>\n";
100         }
101
102         /**
103          * @marker |
104          */
105         protected function parseTd($markdown)
106         {
107                 if (isset($this->context[1]) && $this->context[1] === 'table') {
108                         $align = empty($this->_tableCellAlign[$this->_tableCellCount]) ? '' : ' align="' . $this->_tableCellAlign[$this->_tableCellCount++] . '"';
109                         return [['text', "</$this->_tableCellTag><$this->_tableCellTag$align>"], isset($markdown[1]) && $markdown[1] === ' ' ? 2 : 1]; // TODO make a absy node
110                 }
111                 return [['text', $markdown[0]], 1];
112         }
113
114         abstract protected function parseInline($text);
115         abstract protected function renderAbsy($absy);
116 }