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

ParsedownHighlight   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 34
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A blockFencedCodeComplete() 0 24 3
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