Completed
Push — master ( f5b50a...e05632 )
by TJ
02:13
created

ParsedownHighlight::blockFencedCodeComplete()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3.0032

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 13
cts 14
cp 0.9286
rs 9.536
c 0
b 0
f 0
cc 3
nc 5
nop 1
crap 3.0032
1
<?php
2
3
namespace sixlive;
4
5
use Parsedown;
6
use DomainException;
7
use Highlight\Highlighter;
8
9
class ParsedownHighlight extends Parsedown
10
{
11
    protected $highlighter;
12
13 2
    public function __construct()
14
    {
15 2
        $this->highlighter = new Highlighter;
16 2
    }
17
18 2
    protected function blockFencedCodeComplete($block)
19
    {
20 2
        if (! isset($block['element']['element']['attributes'])) {
21
            return $block;
22
        }
23
24 2
        $code = $block['element']['element']['text'];
25 2
        $languageClass = $block['element']['element']['attributes']['class'];
26 2
        $language = explode('-', $languageClass);
27
28
        try {
29 2
            $highlighted = $this->highlighter->highlight($language[1], $code);
30 2
            $block['element']['element']['attributes']['class'] = vsprintf('%s hljs %s', [
31 2
                $languageClass,
32 2
                $highlighted->language,
33
            ]);
34 2
            $block['element']['element']['rawHtml'] = $highlighted->value;
35 2
            unset($block['element']['element']['text']);
36 2
        } catch (DomainException $e) {
37
            //
38
        }
39
40 2
        return $block;
41
    }
42
}
43