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
|
128 |
|
protected function identifyTable($line, $lines, $current) |
19
|
|
|
{ |
20
|
128 |
|
return strpos($line, '|') !== false && isset($lines[$current + 1]) |
21
|
128 |
|
&& preg_match('~^\\s*\\|?(\\s*:?-[\\-\\s]*:?\\s*\\|\\s*:?-[\\-\\s]*:?\\s*)+\\|?\\s*$~', $lines[$current + 1]); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Consume lines for a table |
26
|
|
|
*/ |
27
|
2 |
|
protected function consumeTable($lines, $current) |
28
|
|
|
{ |
29
|
|
|
// consume until newline |
30
|
|
|
|
31
|
|
|
$block = [ |
32
|
2 |
|
'table', |
33
|
2 |
|
'cols' => [], |
34
|
2 |
|
'rows' => [], |
35
|
2 |
|
]; |
36
|
2 |
|
$beginsWithPipe = $lines[$current][0] === '|'; |
37
|
2 |
|
for ($i = $current, $count = count($lines); $i < $count; $i++) { |
38
|
2 |
|
$line = rtrim($lines[$i]); |
39
|
|
|
|
40
|
|
|
// extract alignment from second line |
41
|
2 |
|
if ($i == $current+1) { |
42
|
2 |
|
$cols = explode('|', trim($line, ' |')); |
43
|
2 |
|
foreach($cols as $col) { |
44
|
2 |
|
$col = trim($col); |
45
|
2 |
|
if (empty($col)) { |
46
|
|
|
$block['cols'][] = ''; |
47
|
|
|
continue; |
48
|
|
|
} |
49
|
2 |
|
$l = ($col[0] === ':'); |
50
|
2 |
|
$r = (substr($col, -1, 1) === ':'); |
51
|
2 |
|
if ($l && $r) { |
52
|
2 |
|
$block['cols'][] = 'center'; |
53
|
2 |
|
} elseif ($l) { |
54
|
2 |
|
$block['cols'][] = 'left'; |
55
|
2 |
|
} elseif ($r) { |
56
|
2 |
|
$block['cols'][] = 'right'; |
57
|
2 |
|
} else { |
58
|
2 |
|
$block['cols'][] = ''; |
59
|
|
|
} |
60
|
2 |
|
} |
61
|
|
|
|
62
|
2 |
|
continue; |
63
|
|
|
} |
64
|
2 |
|
if ($line === '' || $beginsWithPipe && $line[0] !== '|') { |
65
|
2 |
|
break; |
66
|
|
|
} |
67
|
2 |
|
if ($line[0] === '|') { |
68
|
2 |
|
$line = substr($line, 1); |
69
|
2 |
|
} |
70
|
2 |
|
if (substr($line, -1, 1) === '|' && (substr($line, -2, 2) !== '\\|' || substr($line, -3, 3) === '\\\\|')) { |
71
|
2 |
|
$line = substr($line, 0, -1); |
72
|
2 |
|
} |
73
|
|
|
|
74
|
2 |
|
array_unshift($this->context, 'table'); |
|
|
|
|
75
|
2 |
|
$row = $this->parseInline($line); |
76
|
2 |
|
array_shift($this->context); |
77
|
|
|
|
78
|
2 |
|
$r = count($block['rows']); |
79
|
2 |
|
$c = 0; |
80
|
2 |
|
$block['rows'][] = []; |
81
|
2 |
|
foreach ($row as $absy) { |
82
|
2 |
|
if (!isset($block['rows'][$r][$c])) { |
83
|
2 |
|
$block['rows'][$r][] = []; |
84
|
2 |
|
} |
85
|
2 |
|
if ($absy[0] === 'boundary') { |
86
|
2 |
|
$c++; |
87
|
2 |
|
} else { |
88
|
2 |
|
$block['rows'][$r][$c][] = $absy; |
89
|
|
|
} |
90
|
2 |
|
} |
91
|
2 |
|
} |
92
|
|
|
|
93
|
2 |
|
return [$block, --$i]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* render a table block |
98
|
|
|
*/ |
99
|
2 |
|
protected function renderTable($block) |
100
|
|
|
{ |
101
|
2 |
|
$content = ''; |
102
|
2 |
|
$cols = $block['cols']; |
103
|
2 |
|
$first = true; |
104
|
2 |
|
foreach($block['rows'] as $row) { |
105
|
2 |
|
$cellTag = $first ? 'th' : 'td'; |
106
|
2 |
|
$content .= '<tr>'; |
107
|
2 |
|
foreach ($row as $c => $cell) { |
108
|
2 |
|
$align = empty($cols[$c]) ? '' : ' align="' . $cols[$c] . '"'; |
109
|
2 |
|
$content .= "<$cellTag$align>" . trim($this->renderAbsy($cell)) . "</$cellTag>"; |
110
|
2 |
|
} |
111
|
2 |
|
$content .= "</tr>\n"; |
112
|
2 |
|
if ($first) { |
113
|
2 |
|
$content .= "</thead>\n<tbody>\n"; |
114
|
2 |
|
} |
115
|
2 |
|
$first = false; |
116
|
2 |
|
} |
117
|
2 |
|
return "<table>\n<thead>\n$content</tbody>\n</table>\n"; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @marker | |
122
|
|
|
*/ |
123
|
3 |
|
protected function parseTd($markdown) |
124
|
|
|
{ |
125
|
3 |
|
if (isset($this->context[1]) && $this->context[1] === 'table') { |
126
|
2 |
|
return [['boundary'], isset($markdown[1]) && $markdown[1] === ' ' ? 2 : 1]; |
127
|
|
|
} |
128
|
3 |
|
return [['text', $markdown[0]], 1]; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
abstract protected function parseInline($text); |
132
|
|
|
abstract protected function renderAbsy($absy); |
133
|
|
|
} |
134
|
|
|
|
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: