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 | */ |
||
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 string $string |
||
52 | * @param bool $preserveEntities |
||
53 | * |
||
54 | * @return string |
||
55 | * |
||
56 | * @deprecated |
||
57 | */ |
||
58 | public function escape($string, $preserveEntities = false) |
||
59 | { |
||
60 | @trigger_error('HtmlRenderer::escape() will be removed in a future 0.x release. Use Xml::escape() instead.', E_USER_DEPRECATED); |
||
1 ignored issue
–
show
|
|||
61 | |||
62 | return Xml::escape($string, $preserveEntities); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param AbstractInline $inline |
||
67 | * |
||
68 | * @throws \RuntimeException |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | 1653 | protected function renderInline(AbstractInline $inline) |
|
73 | { |
||
74 | 1653 | $renderer = $this->environment->getInlineRendererForClass(get_class($inline)); |
|
75 | 1653 | if (!$renderer) { |
|
76 | throw new \RuntimeException('Unable to find corresponding renderer for inline type ' . get_class($inline)); |
||
77 | } |
||
78 | |||
79 | 1653 | return $renderer->render($inline, $this); |
|
80 | } |
||
81 | |||
82 | /** |
||
83 | * @param AbstractInline[] $inlines |
||
84 | * |
||
85 | * @return string |
||
86 | */ |
||
87 | 1656 | public function renderInlines($inlines) |
|
88 | { |
||
89 | 1656 | $result = []; |
|
90 | 1656 | foreach ($inlines as $inline) { |
|
91 | 1653 | $result[] = $this->renderInline($inline); |
|
92 | 1104 | } |
|
93 | |||
94 | 1656 | return implode('', $result); |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param AbstractBlock $block |
||
99 | * @param bool $inTightList |
||
100 | * |
||
101 | * @throws \RuntimeException |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | 1935 | public function renderBlock(AbstractBlock $block, $inTightList = false) |
|
106 | { |
||
107 | 1935 | $renderer = $this->environment->getBlockRendererForClass(get_class($block)); |
|
108 | 1935 | if (!$renderer) { |
|
109 | throw new \RuntimeException('Unable to find corresponding renderer for block type ' . get_class($block)); |
||
110 | } |
||
111 | |||
112 | 1935 | return $renderer->render($block, $this, $inTightList); |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param AbstractBlock[] $blocks |
||
117 | * @param bool $inTightList |
||
118 | * |
||
119 | * @return string |
||
120 | */ |
||
121 | 1935 | public function renderBlocks($blocks, $inTightList = false) |
|
122 | { |
||
123 | 1935 | $result = []; |
|
124 | 1935 | foreach ($blocks as $block) { |
|
125 | 1929 | $result[] = $this->renderBlock($block, $inTightList); |
|
126 | 1290 | } |
|
127 | |||
128 | 1935 | $separator = $this->getOption('block_separator', "\n"); |
|
129 | |||
130 | 1935 | return implode($separator, $result); |
|
131 | } |
||
132 | } |
||
133 |
If you suppress an error, we recommend checking for the error condition explicitly: