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

MarkdownEngine   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 52.38%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 46
ccs 11
cts 21
cp 0.5238
rs 10
wmc 5
lcom 1
cbo 2

3 Methods

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