Completed
Push — master ( 9292e6...a84270 )
by Colin
03:12 queued 02:10
created

HtmlRenderer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
9
 *  - (c) John MacFarlane
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 League\CommonMark\Renderer;
16
17
use League\CommonMark\Environment\EnvironmentInterface;
18
use League\CommonMark\Node\Block\AbstractBlock;
19
use League\CommonMark\Node\Block\Document;
20
use League\CommonMark\Node\Node;
21
22
final class HtmlRenderer implements HtmlRendererInterface, ChildNodeRendererInterface
23
{
24
    /**
25
     * @var EnvironmentInterface
26
     */
27
    private $environment;
28
29
    /**
30
     * @param EnvironmentInterface $environment
31
     */
32 2514
    public function __construct(EnvironmentInterface $environment)
33
    {
34 2514
        $this->environment = $environment;
35 2514
    }
36
37 2487
    public function renderDocument(Document $node): string
38
    {
39 2487
        return $this->renderNode($node);
40
    }
41
42 2496
    public function renderNodes(iterable $nodes): string
43
    {
44 2496
        $out = '';
45 2496
        $lastItemWasBlock = false;
46
47 2496
        foreach ($nodes as $node) {
48 2490
            if ($lastItemWasBlock) {
49 666
                $lastItemWasBlock = false;
50 666
                $out .= $this->getBlockSeparator();
51
            }
52
53 2490
            $out .= $this->renderNode($node);
54
55 2487
            if ($node instanceof AbstractBlock) {
56 2481
                $lastItemWasBlock = true;
57
            }
58
        }
59
60 2493
        return $out;
61
    }
62
63
    /**
64
     * @param Node $node
65
     *
66
     * @throws \RuntimeException
67
     *
68
     * @return string
69
     */
70 2499
    private function renderNode(Node $node): string
71
    {
72 2499
        $renderers = $this->environment->getRenderersForClass(\get_class($node));
73
74
        /** @var NodeRendererInterface $renderer */
75 2499
        foreach ($renderers as $renderer) {
76 2496
            if (($result = $renderer->render($node, $this)) !== null) {
77 2496
                return $result;
78
            }
79
        }
80
81 3
        throw new \RuntimeException('Unable to find corresponding renderer for node type ' . \get_class($node));
82
    }
83
84 666
    public function getBlockSeparator(): string
85
    {
86 666
        return $this->environment->getConfig('renderer/block_separator', "\n");
87
    }
88
89 465
    public function getInnerSeparator(): string
90
    {
91 465
        return $this->environment->getConfig('renderer/inner_separator', "\n");
92
    }
93
}
94