|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This is part of the webuni/commonmark-table-extension package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Martin Hasoň <[email protected]> |
|
9
|
|
|
* (c) Webuni s.r.o. <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
12
|
|
|
* file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Webuni\CommonMark\TableExtension; |
|
16
|
|
|
|
|
17
|
|
|
use League\CommonMark\Block\Element\AbstractBlock; |
|
18
|
|
|
use League\CommonMark\ContextInterface; |
|
19
|
|
|
use League\CommonMark\Cursor; |
|
20
|
|
|
use League\CommonMark\Node\Node; |
|
21
|
|
|
|
|
22
|
|
|
class Table extends AbstractBlock |
|
23
|
|
|
{ |
|
24
|
|
|
private $caption; |
|
25
|
|
|
private $head; |
|
26
|
|
|
private $body; |
|
27
|
|
|
private $parser; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(\Closure $parser) |
|
30
|
|
|
{ |
|
31
|
|
|
parent::__construct(); |
|
32
|
|
|
$this->appendChild($this->head = new TableRows(TableRows::TYPE_HEAD)); |
|
33
|
|
|
$this->appendChild($this->body = new TableRows(TableRows::TYPE_BODY)); |
|
34
|
|
|
$this->parser = $parser; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function canContain(AbstractBlock $block): bool |
|
38
|
|
|
{ |
|
39
|
|
|
return $block instanceof TableRows || $block instanceof TableCaption; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function acceptsLines(): bool |
|
43
|
|
|
{ |
|
44
|
|
|
return true; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function isCode(): bool |
|
48
|
|
|
{ |
|
49
|
|
|
return false; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function setCaption(TableCaption $caption = null): void |
|
53
|
|
|
{ |
|
54
|
|
|
$node = $this->getCaption(); |
|
55
|
|
|
if ($node instanceof TableCaption) { |
|
56
|
|
|
$node->detach(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$this->caption = $caption; |
|
60
|
|
|
if (null !== $caption) { |
|
61
|
|
|
$this->prependChild($caption); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function getCaption(): ?TableCaption |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->caption; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function getHead(): TableRows |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->head; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function getBody(): TableRows |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->body; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function matchesNextLine(Cursor $cursor): bool |
|
81
|
|
|
{ |
|
82
|
|
|
return call_user_func($this->parser, $cursor, $this); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function handleRemainingContents(ContextInterface $context, Cursor $cursor): void |
|
86
|
|
|
{ |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return AbstractBlock[] |
|
91
|
|
|
*/ |
|
92
|
|
|
public function children(): array |
|
93
|
|
|
{ |
|
94
|
|
|
return array_filter(parent::children(), function (Node $child): bool { return $child instanceof AbstractBlock; }); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|