Completed
Push — master ( 0c3733...9b1319 )
by Vladimir
02:41
created

MarkdownEngine::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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