Completed
Push — master ( 0b0b24...54ff37 )
by Colin
02:37
created

HtmlRenderer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 102
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getOption() 0 4 1
A renderInline() 0 13 3
A renderInlines() 0 9 2
A renderBlock() 0 13 3
A renderBlocks() 0 11 2
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\Inline\AbstractInline;
20
use League\CommonMark\Renderer\Block\BlockRendererInterface;
21
use League\CommonMark\Renderer\Inline\InlineRendererInterface;
22
23
/**
24
 * Renders a parsed AST to HTML
25
 */
26
final class HtmlRenderer implements ElementRendererInterface
27
{
28
    /**
29
     * @var EnvironmentInterface
30
     */
31
    protected $environment;
32
33
    /**
34
     * @param EnvironmentInterface $environment
35
     */
36 2508
    public function __construct(EnvironmentInterface $environment)
37
    {
38 2508
        $this->environment = $environment;
39 2508
    }
40
41
    /**
42
     * @param string $option
43
     * @param mixed  $default
44
     *
45
     * @return mixed|null
46
     */
47 2481
    public function getOption(string $option, $default = null)
48
    {
49 2481
        return $this->environment->getConfig('renderer/' . $option, $default);
50
    }
51
52
    /**
53
     * @param AbstractInline $inline
54
     *
55
     * @throws \RuntimeException
56
     *
57
     * @return string
58
     */
59 2196
    public function renderInline(AbstractInline $inline): string
60
    {
61 2196
        $renderers = $this->environment->getInlineRenderersForClass(\get_class($inline));
62
63
        /** @var InlineRendererInterface $renderer */
64 2196
        foreach ($renderers as $renderer) {
65 2193
            if (($result = $renderer->render($inline, $this)) !== null) {
66 2193
                return $result;
67
            }
68
        }
69
70 3
        throw new \RuntimeException('Unable to find corresponding renderer for inline type ' . \get_class($inline));
71
    }
72
73
    /**
74
     * @param AbstractInline[] $inlines
75
     *
76
     * @return string
77
     */
78 2193
    public function renderInlines(iterable $inlines): string
79
    {
80 2193
        $result = [];
81 2193
        foreach ($inlines as $inline) {
82 2190
            $result[] = $this->renderInline($inline);
83
        }
84
85 2193
        return \implode('', $result);
86
    }
87
88
    /**
89
     * @param AbstractBlock $block
90
     * @param bool          $inTightList
91
     *
92
     * @throws \RuntimeException
93
     *
94
     * @return string
95
     */
96 2487
    public function renderBlock(AbstractBlock $block, bool $inTightList = false): string
97
    {
98 2487
        $renderers = $this->environment->getBlockRenderersForClass(\get_class($block));
99
100
        /** @var BlockRendererInterface $renderer */
101 2487
        foreach ($renderers as $renderer) {
102 2484
            if (($result = $renderer->render($block, $this, $inTightList)) !== null) {
103 2484
                return $result;
104
            }
105
        }
106
107 3
        throw new \RuntimeException('Unable to find corresponding renderer for block type ' . \get_class($block));
108
    }
109
110
    /**
111
     * @param AbstractBlock[] $blocks
112
     * @param bool            $inTightList
113
     *
114
     * @return string
115
     */
116 2481
    public function renderBlocks(iterable $blocks, bool $inTightList = false): string
117
    {
118 2481
        $result = [];
119 2481
        foreach ($blocks as $block) {
120 2475
            $result[] = $this->renderBlock($block, $inTightList);
121
        }
122
123 2481
        $separator = $this->getOption('block_separator', "\n");
124
125 2481
        return \implode($separator, $result);
126
    }
127
}
128