Completed
Push — master ( 8c5fb1...837b1f )
by Vladimir
02:22
created

MarkdownEngine::blockFencedCodeComplete()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 24
ccs 9
cts 9
cp 1
crap 3
rs 8.9713
1
<?php
2
3
namespace allejo\stakx\Engines;
4
5
use Highlight\Highlighter;
6
7
class MarkdownEngine extends \ParsedownExtra
8
{
9
    protected $highlighter;
10
11 3
    public function __construct ()
12
    {
13 3
        parent::__construct();
14
15 3
        $this->highlighter = new Highlighter();
16 3
    }
17
18 2
    protected function blockHeader($line)
19
    {
20 2
        $Block = parent::blockHeader($line);
21
22
        // Create our unique ids by sanitizing the header content
23 2
        $id = strtolower($Block['element']['text']);
24 2
        $id = str_replace(' ', '-', $id);
25 2
        $id = preg_replace('/[^0-9a-zA-Z-_]/', '', $id);
26 2
        $id = preg_replace('/-+/', '-', $id);
27
28 2
        $Block['element']['attributes']['id'] = $id;
29
30 2
        return $Block;
31
    }
32
33 3
    public function blockFencedCodeComplete ($block)
34
    {
35
        // The class has a `language-` prefix, remove this to get the language
36 3
        if (isset($block['element']['text']['attributes']))
37
        {
38 2
            $language = substr($block['element']['text']['attributes']['class'], 9);
39
40
            try
41
            {
42 2
                $highlighted = $this->highlighter->highlight($language, $block['element']['text']['text']);
43 1
                $block['element']['text']['text'] = $highlighted->value;
44
45
                // Only return the block if Highlighter knew the language and how to handle it.
46 1
                return $block;
47
            }
48
            // Exception thrown when language not supported
49 1
            catch (\DomainException $exception)
50
            {
51 1
                trigger_error("An unsupported language ($language) was detected in a code block", E_USER_WARNING);
52
            }
53
        }
54
55 1
        return parent::blockFencedCodeComplete($block);
56
    }
57
}