1 | <?php |
||
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 | 128 | protected function identifyTable($line, $lines, $current) |
|
27 | |||
28 | /** |
||
29 | * Consume lines for a table |
||
30 | */ |
||
31 | 2 | protected function consumeTable($lines, $current) |
|
32 | { |
||
33 | // consume until newline |
||
34 | |||
35 | $block = [ |
||
36 | 2 | 'table', |
|
37 | 'cols' => [], |
||
38 | 'rows' => [], |
||
39 | ]; |
||
40 | 2 | $beginsWithPipe = $lines[$current][0] === '|'; |
|
41 | 2 | for ($i = $current, $count = count($lines); $i < $count; $i++) { |
|
42 | 2 | $line = rtrim($lines[$i]); |
|
43 | |||
44 | // extract alignment from second line |
||
45 | 2 | if ($i == $current+1) { |
|
46 | 2 | $cols = explode('|', trim($line, ' |')); |
|
47 | 2 | foreach($cols as $col) { |
|
48 | 2 | $col = trim($col); |
|
49 | 2 | if (empty($col)) { |
|
50 | $block['cols'][] = ''; |
||
51 | continue; |
||
52 | } |
||
53 | 2 | $l = ($col[0] === ':'); |
|
54 | 2 | $r = (substr($col, -1, 1) === ':'); |
|
55 | 2 | if ($l && $r) { |
|
56 | 2 | $block['cols'][] = 'center'; |
|
57 | 2 | } elseif ($l) { |
|
58 | 2 | $block['cols'][] = 'left'; |
|
59 | 2 | } elseif ($r) { |
|
60 | 2 | $block['cols'][] = 'right'; |
|
61 | } else { |
||
62 | 2 | $block['cols'][] = ''; |
|
63 | } |
||
64 | } |
||
65 | |||
66 | 2 | continue; |
|
67 | } |
||
68 | 2 | if ($line === '' || $beginsWithPipe && $line[0] !== '|') { |
|
69 | 2 | break; |
|
70 | } |
||
71 | 2 | if ($line[0] === '|') { |
|
72 | 2 | $line = substr($line, 1); |
|
73 | } |
||
74 | 2 | if (substr($line, -1, 1) === '|' && (substr($line, -2, 2) !== '\\|' || substr($line, -3, 3) === '\\\\|')) { |
|
75 | 2 | $line = substr($line, 0, -1); |
|
76 | } |
||
77 | 2 | $block['rows'][] = $line; |
|
78 | } |
||
79 | |||
80 | 2 | return [$block, --$i]; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * render a table block |
||
85 | */ |
||
86 | 2 | protected function renderTable($block) |
|
87 | { |
||
88 | 2 | $content = ''; |
|
89 | 2 | $this->_tableCellAlign = $block['cols']; |
|
90 | 2 | $content .= "<thead>\n"; |
|
91 | 2 | $first = true; |
|
92 | 2 | foreach($block['rows'] as $row) { |
|
93 | 2 | $this->_tableCellTag = $first ? 'th' : 'td'; |
|
94 | 2 | $align = empty($this->_tableCellAlign[$this->_tableCellCount]) ? '' : ' align="' . $this->_tableCellAlign[$this->_tableCellCount] . '"'; |
|
95 | 2 | $this->_tableCellCount++; |
|
96 | 2 | $tds = "<$this->_tableCellTag$align>" . trim($this->renderAbsy($this->parseInline($row))) . "</$this->_tableCellTag>"; // TODO move this to the consume step |
|
97 | 2 | $content .= "<tr>$tds</tr>\n"; |
|
98 | 2 | if ($first) { |
|
99 | 2 | $content .= "</thead>\n<tbody>\n"; |
|
100 | } |
||
101 | 2 | $first = false; |
|
102 | 2 | $this->_tableCellCount = 0; |
|
103 | } |
||
104 | 2 | return "<table>\n$content</tbody>\n</table>\n"; |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * @marker | |
||
109 | */ |
||
110 | 3 | protected function parseTd($markdown) |
|
119 | |||
120 | abstract protected function parseInline($text); |
||
122 | } |
||
123 |