|
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\Extension\CommonMark\Renderer\Block; |
|
16
|
|
|
|
|
17
|
|
|
use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode; |
|
18
|
|
|
use League\CommonMark\Node\Block\AbstractBlock; |
|
19
|
|
|
use League\CommonMark\Renderer\Block\BlockRendererInterface; |
|
20
|
|
|
use League\CommonMark\Renderer\ElementRendererInterface; |
|
21
|
|
|
use League\CommonMark\Util\HtmlElement; |
|
22
|
|
|
use League\CommonMark\Util\Xml; |
|
23
|
|
|
|
|
24
|
|
|
final class FencedCodeRenderer implements BlockRendererInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @param FencedCode $block |
|
28
|
|
|
* @param ElementRendererInterface $htmlRenderer |
|
29
|
|
|
* @param bool $inTightList |
|
30
|
|
|
* |
|
31
|
|
|
* @return HtmlElement |
|
32
|
|
|
*/ |
|
33
|
114 |
|
public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, bool $inTightList = false) |
|
34
|
|
|
{ |
|
35
|
114 |
|
if (!($block instanceof FencedCode)) { |
|
36
|
3 |
|
throw new \InvalidArgumentException('Incompatible block type: ' . \get_class($block)); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
111 |
|
$attrs = $block->getData('attributes', []); |
|
40
|
|
|
|
|
41
|
111 |
|
$infoWords = $block->getInfoWords(); |
|
42
|
111 |
|
if (\count($infoWords) !== 0 && \strlen($infoWords[0]) !== 0) { |
|
43
|
21 |
|
$attrs['class'] = isset($attrs['class']) ? $attrs['class'] . ' ' : ''; |
|
44
|
21 |
|
$attrs['class'] .= 'language-' . $infoWords[0]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
111 |
|
return new HtmlElement( |
|
48
|
111 |
|
'pre', |
|
49
|
111 |
|
[], |
|
50
|
111 |
|
new HtmlElement('code', $attrs, Xml::escape($block->getStringContent())) |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|