1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This is part of the webuni/commonmark-table-extension package. |
5
|
|
|
* |
6
|
|
|
* (c) Martin Hasoň <[email protected]> |
7
|
|
|
* (c) Webuni s.r.o. <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Webuni\CommonMark\TableExtension; |
14
|
|
|
|
15
|
|
|
use League\CommonMark\Block\Element\Paragraph; |
16
|
|
|
use League\CommonMark\Block\Parser\AbstractBlockParser; |
17
|
|
|
use League\CommonMark\ContextInterface; |
18
|
|
|
use League\CommonMark\Cursor; |
19
|
|
|
use League\CommonMark\Util\RegexHelper; |
20
|
|
|
|
21
|
|
|
class TableParser extends AbstractBlockParser |
22
|
|
|
{ |
23
|
|
|
const REGEXP_DEFINITION = '/(?: *(:?) *-+ *(:?) *)+(?=\||$)/'; |
24
|
|
|
const REGEXP_CELLS = '/(?:`[^`]*`|\\\\\\\\|\\\\\||[^|`\\\\]+)+(?=\||$)/'; |
25
|
|
|
const REGEXP_CAPTION = '/^\[(.+?)\](?:\[(.+)\])?\s*$/'; |
26
|
|
|
|
27
|
|
|
public function parse(ContextInterface $context, Cursor $cursor) |
28
|
|
|
{ |
29
|
|
|
$container = $context->getContainer(); |
30
|
|
|
|
31
|
|
|
if (!$container instanceof Paragraph) { |
32
|
|
|
return false; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$lines = $container->getStrings(); |
36
|
|
|
if (count($lines) < 1) { |
37
|
|
|
return false; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$match = RegexHelper::matchAll(self::REGEXP_DEFINITION, $cursor->getLine(), $cursor->getFirstNonSpacePosition()); |
41
|
|
|
if (null === $match) { |
42
|
|
|
return false; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$columns = $this->parseColumns($match); |
46
|
|
|
$head = $this->parseRow(trim(array_pop($lines)), $columns, TableCell::TYPE_HEAD); |
47
|
|
|
if (null === $head) { |
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$table = new Table(function (Cursor $cursor) use (&$table, $columns) { |
52
|
|
|
$row = $this->parseRow($cursor->getLine(), $columns); |
53
|
|
|
if (null === $row) { |
54
|
|
|
if (null !== $table->getCaption()) { |
55
|
|
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (null !== ($caption = $this->parseCaption($cursor->getLine()))) { |
59
|
|
|
$table->setCaption($caption); |
60
|
|
|
|
61
|
|
|
return true; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$table->getBody()->appendChild($row); |
68
|
|
|
|
69
|
|
|
return true; |
70
|
|
|
}); |
71
|
|
|
|
72
|
|
|
$table->getHead()->appendChild($head); |
73
|
|
|
|
74
|
|
|
if (count($lines) >= 1) { |
75
|
|
|
$paragraph = new Paragraph(); |
76
|
|
|
foreach ($lines as $line) { |
77
|
|
|
$paragraph->addLine($line); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$context->replaceContainerBlock($paragraph); |
81
|
|
|
$context->addBlock($table); |
82
|
|
|
} else { |
83
|
|
|
$context->replaceContainerBlock($table); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function parseColumns(array $match) |
90
|
|
|
{ |
91
|
|
|
$columns = []; |
92
|
|
|
foreach ((array) $match[0] as $i => $column) { |
93
|
|
|
if (isset($match[1][$i]) && $match[1][$i] && isset($match[2][$i]) && $match[2][$i]) { |
94
|
|
|
$columns[] = TableCell::ALIGN_CENTER; |
95
|
|
|
} elseif (isset($match[1][$i]) && $match[1][$i]) { |
96
|
|
|
$columns[] = TableCell::ALIGN_LEFT; |
97
|
|
|
} elseif (isset($match[2][$i]) && $match[2][$i]) { |
98
|
|
|
$columns[] = TableCell::ALIGN_RIGHT; |
99
|
|
|
} else { |
100
|
|
|
$columns[] = null; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $columns; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function parseRow($line, array $columns, $type = TableCell::TYPE_BODY) |
108
|
|
|
{ |
109
|
|
|
$cells = RegexHelper::matchAll(self::REGEXP_CELLS, $line); |
110
|
|
|
if (null === $cells || !is_array($cells[0])) { |
111
|
|
|
return; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$row = new TableRow(); |
115
|
|
|
foreach ($cells[0] as $i => $cell) { |
116
|
|
|
$row->appendChild(new TableCell(trim($cell), $type, isset($columns[$i]) ? $columns[$i] : null)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
for ($j = count($columns) - 1; $j > $i; --$j) { |
|
|
|
|
120
|
|
|
$row->appendChild(new TableCell('', $type, null)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $row; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
private function parseCaption($line) |
127
|
|
|
{ |
128
|
|
|
$caption = RegexHelper::matchAll(self::REGEXP_CAPTION, $line); |
129
|
|
|
|
130
|
|
|
if (null === $caption) { |
131
|
|
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return new TableCaption($caption[1], $caption[2]); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
It seems like you are relying on a variable being defined by an iteration: