|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright 2017 Vladimir Jimenez |
|
5
|
|
|
* @license https://github.com/allejo/stakx/blob/master/LICENSE.md MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace allejo\stakx\Engines\Markdown; |
|
9
|
|
|
|
|
10
|
|
|
use allejo\stakx\Configuration; |
|
11
|
|
|
use allejo\stakx\Engines\ParsingEngine; |
|
12
|
|
|
use allejo\stakx\Service; |
|
13
|
|
|
use Highlight\Highlighter; |
|
14
|
|
|
|
|
15
|
|
|
class MarkdownEngine extends \ParsedownExtra implements ParsingEngine |
|
16
|
|
|
{ |
|
17
|
|
|
protected $highlighter; |
|
18
|
|
|
|
|
19
|
10 |
|
public function __construct() |
|
20
|
|
|
{ |
|
21
|
10 |
|
parent::__construct(); |
|
22
|
|
|
|
|
23
|
10 |
|
$this->highlighter = new Highlighter(); |
|
24
|
10 |
|
} |
|
25
|
|
|
|
|
26
|
9 |
|
protected function blockHeader($Line) |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
9 |
|
$Block = parent::blockHeader($Line); |
|
29
|
|
|
|
|
30
|
9 |
|
if (isset($Block['element']['text'])) |
|
31
|
9 |
|
{ |
|
32
|
9 |
|
$Block['element']['attributes']['id'] = $this->slugifyHeader($Block); |
|
33
|
9 |
|
} |
|
34
|
|
|
|
|
35
|
9 |
|
return $Block; |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
protected function blockSetextHeader($Line, array $Block = null) |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
1 |
|
$Block = parent::blockSetextHeader($Line, $Block); |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
1 |
|
if (isset($Block['element']['name'])) |
|
43
|
1 |
|
{ |
|
44
|
|
|
$element = $Block['element']['name']; |
|
45
|
|
|
|
|
46
|
|
|
if (in_array($element, ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'])) |
|
47
|
|
|
{ |
|
48
|
|
|
$Block['element']['attributes']['id'] = $this->slugifyHeader($Block); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
return $Block; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
9 |
|
private function slugifyHeader($Block) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
9 |
|
$id = strtolower($Block['element']['text']); |
|
58
|
9 |
|
$id = str_replace(' ', '-', $id); |
|
59
|
9 |
|
$id = preg_replace('/[^0-9a-zA-Z-_]/', '', $id); |
|
60
|
|
|
|
|
61
|
9 |
|
return preg_replace('/-+/', '-', $id); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
3 |
|
protected function blockFencedCodeComplete($block) |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
// The class has a `language-` prefix, remove this to get the language |
|
67
|
3 |
|
if (isset($block['element']['text']['attributes']) && Service::getParameter(Configuration::HIGHLIGHTER_ENABLED)) |
|
68
|
3 |
|
{ |
|
69
|
2 |
|
$cssClass = &$block['element']['text']['attributes']['class']; |
|
70
|
2 |
|
$language = substr($cssClass, 9); |
|
71
|
2 |
|
$cssClass = 'hljs ' . $cssClass; |
|
72
|
|
|
|
|
73
|
|
|
try |
|
74
|
|
|
{ |
|
75
|
2 |
|
$highlighted = $this->highlighter->highlight($language, $block['element']['text']['text']); |
|
76
|
1 |
|
$block['element']['text']['text'] = $highlighted->value; |
|
77
|
|
|
|
|
78
|
|
|
// Only return the block if Highlighter knew the language and how to handle it. |
|
79
|
1 |
|
return $block; |
|
80
|
|
|
} |
|
81
|
|
|
// Exception thrown when language not supported |
|
82
|
1 |
|
catch (\DomainException $exception) |
|
83
|
|
|
{ |
|
84
|
1 |
|
trigger_error("An unsupported language ($language) was detected in a code block", E_USER_WARNING); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
return parent::blockFencedCodeComplete($block); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
This check marks parameter names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.