Completed
Push — master ( afd04b...3b4c22 )
by Colin
10s
created

HtmlRenderer::escape()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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