Completed
Push — master ( 7a6aee...e5c914 )
by Colin
52:42 queued 27:44
created

src/HtmlRenderer.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 1944
     */
34
    public function __construct(Environment $environment)
35 1944
    {
36 1944
        $this->environment = $environment;
37
    }
38
39
    /**
40
     * @param string $option
41
     * @param mixed  $default
42
     *
43
     * @return mixed
44 1935
     */
45
    public function getOption($option, $default = null)
46 1935
    {
47
        return $this->environment->getConfig('renderer/' . $option, $default);
48
    }
49
50
    /**
51
     * @param string $string
52
     * @param bool   $preserveEntities
53
     *
54
     * @return string
55 1782
     *
56
     * @deprecated
57 1782
     */
58 399
    public function escape($string, $preserveEntities = false)
59 266
    {
60 1779
        @trigger_error('HtmlRenderer::escape() will be removed in a future 0.x release.  Use Xml::escape() instead.', E_USER_DEPRECATED);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
61
62
        return Xml::escape($string, $preserveEntities);
63 1782
    }
64
65
    /**
66
     * @param AbstractInline $inline
67
     *
68
     * @throws \RuntimeException
69
     *
70
     * @return string
71
     */
72
    protected function renderInline(AbstractInline $inline)
73 1653
    {
74
        $renderer = $this->environment->getInlineRendererForClass(get_class($inline));
75 1653
        if (!$renderer) {
76 1653
            throw new \RuntimeException('Unable to find corresponding renderer for inline type ' . get_class($inline));
77
        }
78
79
        return $renderer->render($inline, $this);
80 1653
    }
81
82
    /**
83
     * @param AbstractInline[] $inlines
84
     *
85
     * @return string
86
     */
87
    public function renderInlines($inlines)
88 1656
    {
89
        $result = [];
90 1656
        foreach ($inlines as $inline) {
91 1656
            $result[] = $this->renderInline($inline);
92 1653
        }
93 1104
94
        return implode('', $result);
95 1656
    }
96
97
    /**
98
     * @param AbstractBlock $block
99
     * @param bool          $inTightList
100
     *
101
     * @throws \RuntimeException
102
     *
103
     * @return string
104
     */
105
    public function renderBlock(AbstractBlock $block, $inTightList = false)
106 1935
    {
107
        $renderer = $this->environment->getBlockRendererForClass(get_class($block));
108 1935
        if (!$renderer) {
109 1935
            throw new \RuntimeException('Unable to find corresponding renderer for block type ' . get_class($block));
110
        }
111
112
        return $renderer->render($block, $this, $inTightList);
113 1935
    }
114
115
    /**
116
     * @param AbstractBlock[] $blocks
117
     * @param bool            $inTightList
118
     *
119
     * @return string
120
     */
121
    public function renderBlocks($blocks, $inTightList = false)
122 1935
    {
123
        $result = [];
124 1935
        foreach ($blocks as $block) {
125 1935
            $result[] = $this->renderBlock($block, $inTightList);
126 1929
        }
127 1290
128
        $separator = $this->getOption('block_separator', "\n");
129 1935
130
        return implode($separator, $result);
131 1935
    }
132
}
133