| 148 |
lars |
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 <colinodell@gmail.com>
|
|
|
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\DocumentPreRenderEvent;
|
|
|
21 |
use League\CommonMark\Event\DocumentRenderedEvent;
|
|
|
22 |
use League\CommonMark\Node\Block\AbstractBlock;
|
|
|
23 |
use League\CommonMark\Node\Block\Document;
|
|
|
24 |
use League\CommonMark\Node\Node;
|
|
|
25 |
use League\CommonMark\Output\RenderedContent;
|
|
|
26 |
use League\CommonMark\Output\RenderedContentInterface;
|
|
|
27 |
|
|
|
28 |
final class HtmlRenderer implements DocumentRendererInterface, ChildNodeRendererInterface
|
|
|
29 |
{
|
|
|
30 |
/** @psalm-readonly */
|
|
|
31 |
private EnvironmentInterface $environment;
|
|
|
32 |
|
|
|
33 |
public function __construct(EnvironmentInterface $environment)
|
|
|
34 |
{
|
|
|
35 |
$this->environment = $environment;
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
public function renderDocument(Document $document): RenderedContentInterface
|
|
|
39 |
{
|
|
|
40 |
$this->environment->dispatch(new DocumentPreRenderEvent($document, 'html'));
|
|
|
41 |
|
|
|
42 |
$output = new RenderedContent($document, (string) $this->renderNode($document));
|
|
|
43 |
|
|
|
44 |
$event = new DocumentRenderedEvent($output);
|
|
|
45 |
$this->environment->dispatch($event);
|
|
|
46 |
|
|
|
47 |
return $event->getOutput();
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* {@inheritDoc}
|
|
|
52 |
*/
|
|
|
53 |
public function renderNodes(iterable $nodes): string
|
|
|
54 |
{
|
|
|
55 |
$output = '';
|
|
|
56 |
|
|
|
57 |
$isFirstItem = true;
|
|
|
58 |
|
|
|
59 |
foreach ($nodes as $node) {
|
|
|
60 |
if (! $isFirstItem && $node instanceof AbstractBlock) {
|
|
|
61 |
$output .= $this->getBlockSeparator();
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
$output .= $this->renderNode($node);
|
|
|
65 |
|
|
|
66 |
$isFirstItem = false;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
return $output;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* @return \Stringable|string
|
|
|
74 |
*
|
| 991 |
lars |
75 |
* @throws NoMatchingRendererException
|
| 148 |
lars |
76 |
*/
|
|
|
77 |
private function renderNode(Node $node)
|
|
|
78 |
{
|
|
|
79 |
$renderers = $this->environment->getRenderersForClass(\get_class($node));
|
|
|
80 |
|
|
|
81 |
foreach ($renderers as $renderer) {
|
|
|
82 |
\assert($renderer instanceof NodeRendererInterface);
|
|
|
83 |
if (($result = $renderer->render($node, $this)) !== null) {
|
|
|
84 |
return $result;
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
|
| 991 |
lars |
88 |
throw new NoMatchingRendererException('Unable to find corresponding renderer for node type ' . \get_class($node));
|
| 148 |
lars |
89 |
}
|
|
|
90 |
|
|
|
91 |
public function getBlockSeparator(): string
|
|
|
92 |
{
|
|
|
93 |
return $this->environment->getConfiguration()->get('renderer/block_separator');
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
public function getInnerSeparator(): string
|
|
|
97 |
{
|
|
|
98 |
return $this->environment->getConfiguration()->get('renderer/inner_separator');
|
|
|
99 |
}
|
|
|
100 |
}
|