1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the league/commonmark package. |
7
|
|
|
* |
8
|
|
|
* (c) Colin O'Dell <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace League\CommonMark\Extension\TableOfContents; |
15
|
|
|
|
16
|
|
|
use League\CommonMark\Exception\InvalidArgumentException; |
17
|
|
|
use League\CommonMark\Extension\TableOfContents\Node\TableOfContentsWrapper; |
18
|
|
|
use League\CommonMark\Node\Node; |
19
|
|
|
use League\CommonMark\Renderer\ChildNodeRendererInterface; |
20
|
|
|
use League\CommonMark\Renderer\NodeRendererInterface; |
21
|
|
|
use League\CommonMark\Util\HtmlElement; |
22
|
|
|
use League\CommonMark\Xml\XmlNodeRendererInterface; |
23
|
|
|
|
24
|
|
|
final class TableOfContentsWrapperRenderer implements NodeRendererInterface, XmlNodeRendererInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritDoc} |
28
|
|
|
*/ |
29
|
2 |
|
public function render(Node $node, ChildNodeRendererInterface $childRenderer) |
30
|
|
|
{ |
31
|
2 |
|
TableOfContentsWrapper::assertInstanceOf($node); |
32
|
2 |
|
$children = $node->children(); |
33
|
2 |
|
if (! \is_array($children)) { |
|
|
|
|
34
|
|
|
/** @psalm-suppress NoValue */ |
35
|
|
|
$children = \iterator_to_array($children); |
36
|
|
|
} |
37
|
|
|
|
38
|
2 |
|
if (\count($children) !== 2) { |
39
|
|
|
throw new InvalidArgumentException( |
40
|
|
|
'TableOfContentsWrapper nodes should have 2 children, found ' . \count($children) |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
2 |
|
$attrs = $node->data->get('attributes'); |
45
|
|
|
|
46
|
2 |
|
return new HtmlElement( |
47
|
2 |
|
'div', |
48
|
2 |
|
$attrs, |
49
|
2 |
|
$childRenderer->renderNodes($children) |
50
|
2 |
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
public function getXmlTagName(Node $node): string |
54
|
|
|
{ |
55
|
2 |
|
return 'table_of_contents_wrapper'; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return array<string, scalar> |
60
|
|
|
*/ |
61
|
2 |
|
public function getXmlAttributes(Node $node): array |
62
|
|
|
{ |
63
|
2 |
|
return []; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|