|
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
|
|
|
/** |
|
16
|
|
|
* identify a line as the beginning of a table block. |
|
17
|
|
|
*/ |
|
18
|
134 |
|
protected function identifyTable($line, $lines, $current) |
|
19
|
|
|
{ |
|
20
|
134 |
|
return strpos($line, '|') !== false && isset($lines[$current + 1]) |
|
21
|
134 |
|
&& preg_match('~^\\s*\\|?(\\s*:?-[\\-\\s]*:?\\s*\\|?)*\\s*$~', $lines[$current + 1]) |
|
22
|
134 |
|
&& strpos($lines[$current + 1], '|') !== false |
|
23
|
134 |
|
&& isset($lines[$current + 2]) && trim($lines[$current + 1]) !== ''; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Consume lines for a table |
|
28
|
|
|
*/ |
|
29
|
3 |
|
protected function consumeTable($lines, $current) |
|
30
|
|
|
{ |
|
31
|
|
|
// consume until newline |
|
32
|
|
|
|
|
33
|
|
|
$block = [ |
|
34
|
3 |
|
'table', |
|
35
|
|
|
'cols' => [], |
|
36
|
|
|
'rows' => [], |
|
37
|
|
|
]; |
|
38
|
3 |
|
for ($i = $current, $count = count($lines); $i < $count; $i++) { |
|
39
|
3 |
|
$line = trim($lines[$i]); |
|
40
|
|
|
|
|
41
|
|
|
// extract alignment from second line |
|
42
|
3 |
|
if ($i == $current+1) { |
|
43
|
3 |
|
$cols = explode('|', trim($line, ' |')); |
|
44
|
3 |
|
foreach($cols as $col) { |
|
45
|
3 |
|
$col = trim($col); |
|
46
|
3 |
|
if (empty($col)) { |
|
47
|
|
|
$block['cols'][] = ''; |
|
48
|
|
|
continue; |
|
49
|
|
|
} |
|
50
|
3 |
|
$l = ($col[0] === ':'); |
|
51
|
3 |
|
$r = (substr($col, -1, 1) === ':'); |
|
52
|
3 |
|
if ($l && $r) { |
|
53
|
2 |
|
$block['cols'][] = 'center'; |
|
54
|
3 |
|
} elseif ($l) { |
|
55
|
2 |
|
$block['cols'][] = 'left'; |
|
56
|
3 |
|
} elseif ($r) { |
|
57
|
2 |
|
$block['cols'][] = 'right'; |
|
58
|
|
|
} else { |
|
59
|
3 |
|
$block['cols'][] = ''; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
3 |
|
continue; |
|
64
|
|
|
} |
|
65
|
3 |
|
if ($line === '' || substr($lines[$i], 0, 4) === ' ') { |
|
66
|
3 |
|
break; |
|
67
|
|
|
} |
|
68
|
3 |
|
if ($line[0] === '|') { |
|
69
|
3 |
|
$line = substr($line, 1); |
|
70
|
|
|
} |
|
71
|
3 |
|
if (substr($line, -1, 1) === '|' && (substr($line, -2, 2) !== '\\|' || substr($line, -3, 3) === '\\\\|')) { |
|
72
|
3 |
|
$line = substr($line, 0, -1); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
3 |
|
array_unshift($this->context, 'table'); |
|
|
|
|
|
|
76
|
3 |
|
$row = $this->parseInline($line); |
|
77
|
3 |
|
array_shift($this->context); |
|
78
|
|
|
|
|
79
|
3 |
|
$r = count($block['rows']); |
|
80
|
3 |
|
$c = 0; |
|
81
|
3 |
|
$block['rows'][] = []; |
|
82
|
3 |
|
foreach ($row as $absy) { |
|
83
|
3 |
|
if (!isset($block['rows'][$r][$c])) { |
|
84
|
3 |
|
$block['rows'][$r][] = []; |
|
85
|
|
|
} |
|
86
|
3 |
|
if ($absy[0] === 'tableBoundary') { |
|
87
|
2 |
|
$c++; |
|
88
|
|
|
} else { |
|
89
|
3 |
|
$block['rows'][$r][$c][] = $absy; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
3 |
|
return [$block, --$i]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* render a table block |
|
99
|
|
|
*/ |
|
100
|
3 |
|
protected function renderTable($block) |
|
101
|
|
|
{ |
|
102
|
3 |
|
$head = ''; |
|
103
|
3 |
|
$body = ''; |
|
104
|
3 |
|
$cols = $block['cols']; |
|
105
|
3 |
|
$first = true; |
|
106
|
3 |
|
foreach($block['rows'] as $row) { |
|
107
|
3 |
|
$cellTag = $first ? 'th' : 'td'; |
|
108
|
3 |
|
$tds = ''; |
|
109
|
3 |
|
foreach ($row as $c => $cell) { |
|
110
|
3 |
|
$align = empty($cols[$c]) ? '' : ' align="' . $cols[$c] . '"'; |
|
111
|
3 |
|
$tds .= "<$cellTag$align>" . trim($this->renderAbsy($cell)) . "</$cellTag>"; |
|
112
|
|
|
} |
|
113
|
3 |
|
if ($first) { |
|
114
|
3 |
|
$head .= "<tr>$tds</tr>\n"; |
|
115
|
|
|
} else { |
|
116
|
3 |
|
$body .= "<tr>$tds</tr>\n"; |
|
117
|
|
|
} |
|
118
|
3 |
|
$first = false; |
|
119
|
|
|
} |
|
120
|
3 |
|
return $this->composeTable($head, $body); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* This method composes a table from parsed body and head HTML. |
|
125
|
|
|
* |
|
126
|
|
|
* You may override this method to customize the table rendering, for example by |
|
127
|
|
|
* adding a `class` to the table tag: |
|
128
|
|
|
* |
|
129
|
|
|
* ```php |
|
130
|
|
|
* return "<table class="table table-striped">\n<thead>\n$head</thead>\n<tbody>\n$body</tbody>\n</table>\n" |
|
131
|
|
|
* ``` |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $head table head HTML. |
|
134
|
|
|
* @param string $body table body HTML. |
|
135
|
|
|
* @return string the complete table HTML. |
|
136
|
|
|
* @since 1.2.0 |
|
137
|
|
|
*/ |
|
138
|
3 |
|
protected function composeTable($head, $body) |
|
139
|
|
|
{ |
|
140
|
3 |
|
return "<table>\n<thead>\n$head</thead>\n<tbody>\n$body</tbody>\n</table>\n"; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @marker | |
|
145
|
|
|
*/ |
|
146
|
6 |
|
protected function parseTd($markdown) |
|
147
|
|
|
{ |
|
148
|
6 |
|
if (isset($this->context[1]) && $this->context[1] === 'table') { |
|
149
|
2 |
|
return [['tableBoundary'], isset($markdown[1]) && $markdown[1] === ' ' ? 2 : 1]; |
|
150
|
|
|
} |
|
151
|
6 |
|
return [['text', $markdown[0]], 1]; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
abstract protected function parseInline($text); |
|
155
|
|
|
abstract protected function renderAbsy($absy); |
|
156
|
|
|
} |
|
157
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: