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
|
|
|
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js) |
11
|
|
|
* - (c) John MacFarlane |
12
|
|
|
* |
13
|
|
|
* For the full copyright and license information, please view the LICENSE |
14
|
|
|
* file that was distributed with this source code. |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace League\CommonMark\Renderer; |
18
|
|
|
|
19
|
|
|
use League\CommonMark\Environment\EnvironmentInterface; |
20
|
|
|
use League\CommonMark\Event\DocumentRenderedEvent; |
21
|
|
|
use League\CommonMark\Node\Block\AbstractBlock; |
22
|
|
|
use League\CommonMark\Node\Block\Document; |
23
|
|
|
use League\CommonMark\Node\Node; |
24
|
|
|
use League\CommonMark\Output\RenderedContent; |
25
|
|
|
use League\CommonMark\Output\RenderedContentInterface; |
26
|
|
|
use League\CommonMark\Util\HtmlElement; |
27
|
|
|
|
28
|
|
|
final class HtmlRenderer implements HtmlRendererInterface, ChildNodeRendererInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var EnvironmentInterface |
32
|
|
|
* |
33
|
|
|
* @psalm-readonly |
34
|
|
|
*/ |
35
|
|
|
private $environment; |
36
|
|
|
|
37
|
3030 |
|
public function __construct(EnvironmentInterface $environment) |
38
|
|
|
{ |
39
|
3030 |
|
$this->environment = $environment; |
40
|
3030 |
|
} |
41
|
|
|
|
42
|
2982 |
|
public function renderDocument(Document $node): RenderedContentInterface |
43
|
|
|
{ |
44
|
2982 |
|
$output = new RenderedContent($node, (string) $this->renderNode($node)); |
45
|
|
|
|
46
|
2982 |
|
$event = new DocumentRenderedEvent($output); |
47
|
2982 |
|
$this->environment->dispatch($event); |
48
|
|
|
|
49
|
2982 |
|
return $event->getOutput(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
2991 |
|
public function renderNodes(iterable $nodes): string |
56
|
|
|
{ |
57
|
2991 |
|
$output = ''; |
58
|
|
|
|
59
|
2991 |
|
$isFirstItem = true; |
60
|
|
|
|
61
|
2991 |
|
foreach ($nodes as $node) { |
62
|
2982 |
|
if (! $isFirstItem && $node instanceof AbstractBlock) { |
63
|
846 |
|
$output .= $this->getBlockSeparator(); |
64
|
|
|
} |
65
|
|
|
|
66
|
2982 |
|
$output .= $this->renderNode($node); |
67
|
|
|
|
68
|
2979 |
|
$isFirstItem = false; |
69
|
|
|
} |
70
|
|
|
|
71
|
2988 |
|
return $output; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return HtmlElement|string |
76
|
|
|
* |
77
|
|
|
* @throws \RuntimeException |
78
|
|
|
*/ |
79
|
2994 |
|
private function renderNode(Node $node) |
80
|
|
|
{ |
81
|
2994 |
|
$renderers = $this->environment->getRenderersForClass(\get_class($node)); |
82
|
|
|
|
83
|
2994 |
|
foreach ($renderers as $renderer) { |
84
|
|
|
\assert($renderer instanceof NodeRendererInterface); |
85
|
2991 |
|
if (($result = $renderer->render($node, $this)) !== null) { |
86
|
2991 |
|
return $result; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
3 |
|
throw new \RuntimeException('Unable to find corresponding renderer for node type ' . \get_class($node)); |
91
|
|
|
} |
92
|
|
|
|
93
|
846 |
|
public function getBlockSeparator(): string |
94
|
|
|
{ |
95
|
846 |
|
return $this->environment->getConfiguration()->get('renderer/block_separator'); |
96
|
|
|
} |
97
|
|
|
|
98
|
495 |
|
public function getInnerSeparator(): string |
99
|
|
|
{ |
100
|
495 |
|
return $this->environment->getConfiguration()->get('renderer/inner_separator'); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|