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\AbstractBlock; |
16
|
|
|
use League\CommonMark\ContextInterface; |
17
|
|
|
use League\CommonMark\Cursor; |
18
|
|
|
|
19
|
|
|
class Table extends AbstractBlock |
20
|
|
|
{ |
21
|
|
|
private $parser; |
22
|
|
|
|
23
|
|
|
public function __construct(\Closure $parser) |
24
|
|
|
{ |
25
|
|
|
parent::__construct(); |
26
|
|
|
$this->appendChild(new TableRows(TableRows::TYPE_HEAD)); |
27
|
|
|
$this->appendChild(new TableRows(TableRows::TYPE_BODY)); |
28
|
|
|
$this->parser = $parser; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function canContain(AbstractBlock $block) |
32
|
|
|
{ |
33
|
|
|
return $block instanceof TableRows || $block instanceof TableCaption; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function acceptsLines() |
37
|
|
|
{ |
38
|
|
|
return true; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function isCode() |
42
|
|
|
{ |
43
|
|
|
return false; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function setCaption(TableCaption $caption = null) |
47
|
|
|
{ |
48
|
|
|
$node = $this->getCaption(); |
49
|
|
|
if ($node instanceof TableCaption) { |
50
|
|
|
$node->detach(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($caption instanceof TableCaption) { |
54
|
|
|
$this->prependChild($caption); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getCaption() |
59
|
|
|
{ |
60
|
|
|
foreach ($this->children() as $child) { |
61
|
|
|
if ($child instanceof TableCaption) { |
62
|
|
|
return $child; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getHead() |
68
|
|
|
{ |
69
|
|
|
foreach ($this->children() as $child) { |
70
|
|
|
if ($child instanceof TableRows && $child->isHead()) { |
71
|
|
|
return $child; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getBody() |
77
|
|
|
{ |
78
|
|
|
foreach ($this->children() as $child) { |
79
|
|
|
if ($child instanceof TableRows && $child->isBody()) { |
80
|
|
|
return $child; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function matchesNextLine(Cursor $cursor) |
86
|
|
|
{ |
87
|
|
|
return call_user_func($this->parser, $cursor); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function handleRemainingContents(ContextInterface $context, Cursor $cursor) |
91
|
|
|
{ |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|