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
|
|
|
} |