Completed
Push — develop ( 6bc50f )
by Colin
04:35
created

HtmlRenderer::renderInline()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2.032
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;
16
17
use League\CommonMark\Block\Element\AbstractBlock;
18
use League\CommonMark\Inline\Element\AbstractInline;
19
use League\CommonMark\Util\Xml;
20
21
/**
22
 * Renders a parsed AST to HTML
23
 */
24
class HtmlRenderer implements ElementRendererInterface
25
{
26
    /**
27
     * @var Environment
28
     */
29
    protected $environment;
30
31
    /**
32
     * @param Environment $environment
33
     */
34 1944
    public function __construct(Environment $environment)
35
    {
36 1944
        $this->environment = $environment;
37 1944
    }
38
39
    /**
40
     * @param string $option
41
     * @param mixed  $default
42
     *
43
     * @return mixed
44
     */
45 1935
    public function getOption($option, $default = null)
46
    {
47 1935
        return $this->environment->getConfig('renderer/' . $option, $default);
48
    }
49
50
    /**
51
     * @param AbstractInline $inline
52
     *
53
     * @throws \RuntimeException
54
     *
55
     * @return string
56
     */
57 1653
    protected function renderInline(AbstractInline $inline)
58
    {
59 1653
        $renderer = $this->environment->getInlineRendererForClass(get_class($inline));
60 1653
        if (!$renderer) {
61
            throw new \RuntimeException('Unable to find corresponding renderer for inline type ' . get_class($inline));
62
        }
63
64 1653
        return $renderer->render($inline, $this);
65
    }
66
67
    /**
68
     * @param AbstractInline[] $inlines
69
     *
70
     * @return string
71
     */
72 1656
    public function renderInlines($inlines)
73
    {
74 1656
        $result = [];
75 1656
        foreach ($inlines as $inline) {
76 1653
            $result[] = $this->renderInline($inline);
77 552
        }
78
79 1656
        return implode('', $result);
80
    }
81
82
    /**
83
     * @param AbstractBlock $block
84
     * @param bool          $inTightList
85
     *
86
     * @throws \RuntimeException
87
     *
88
     * @return string
89
     */
90 1935
    public function renderBlock(AbstractBlock $block, $inTightList = false)
91
    {
92 1935
        $renderer = $this->environment->getBlockRendererForClass(get_class($block));
93 1935
        if (!$renderer) {
94
            throw new \RuntimeException('Unable to find corresponding renderer for block type ' . get_class($block));
95
        }
96
97 1935
        return $renderer->render($block, $this, $inTightList);
98
    }
99
100
    /**
101
     * @param AbstractBlock[] $blocks
102
     * @param bool            $inTightList
103
     *
104
     * @return string
105
     */
106 1935
    public function renderBlocks($blocks, $inTightList = false)
107
    {
108 1935
        $result = [];
109 1935
        foreach ($blocks as $block) {
110 1929
            $result[] = $this->renderBlock($block, $inTightList);
111 645
        }
112
113 1935
        $separator = $this->getOption('block_separator', "\n");
114
115 1935
        return implode($separator, $result);
116
    }
117
}
118