1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2018 Vladimir Jimenez |
5
|
|
|
* @license https://github.com/allejo/stakx/blob/master/LICENSE.md MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace allejo\stakx\MarkupEngine; |
9
|
|
|
|
10
|
|
|
use allejo\stakx\Configuration; |
11
|
|
|
use allejo\stakx\Service; |
12
|
|
|
use Highlight\Highlighter; |
13
|
|
|
|
14
|
|
|
class MarkdownEngine extends \ParsedownExtra implements MarkupEngine |
15
|
|
|
{ |
16
|
|
|
protected $highlighter; |
17
|
|
|
|
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
|
|
parent::__construct(); |
21
|
|
|
|
22
|
|
|
$this->highlighter = new Highlighter(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function blockHeader($Line) |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$Block = parent::blockHeader($Line); |
28
|
|
|
|
29
|
|
|
if (isset($Block['element']['text'])) |
30
|
|
|
{ |
31
|
|
|
$Block['element']['attributes']['id'] = $this->slugifyHeader($Block); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $Block; |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function blockSetextHeader($Line, array $Block = null) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$Block = parent::blockSetextHeader($Line, $Block); |
|
|
|
|
40
|
|
|
|
41
|
|
|
if (isset($Block['element']['name'])) |
42
|
|
|
{ |
43
|
|
|
$element = $Block['element']['name']; |
44
|
|
|
|
45
|
|
|
if (in_array($element, ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'])) |
46
|
|
|
{ |
47
|
|
|
$Block['element']['attributes']['id'] = $this->slugifyHeader($Block); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $Block; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function slugifyHeader($Block) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$id = strtolower($Block['element']['text']); |
57
|
|
|
$id = str_replace(' ', '-', $id); |
58
|
|
|
$id = preg_replace('/[^0-9a-zA-Z-_]/', '', $id); |
59
|
|
|
|
60
|
|
|
return preg_replace('/-+/', '-', $id); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function blockFencedCodeComplete($block) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
// The class has a `language-` prefix, remove this to get the language |
66
|
|
|
if (isset($block['element']['text']['attributes']) && Service::getParameter(Configuration::HIGHLIGHTER_ENABLED)) |
67
|
|
|
{ |
68
|
|
|
$cssClass = &$block['element']['text']['attributes']['class']; |
69
|
|
|
$language = substr($cssClass, 9); |
70
|
|
|
$cssClass = 'hljs ' . $cssClass; |
71
|
|
|
|
72
|
|
|
try |
73
|
|
|
{ |
74
|
|
|
$highlighted = $this->highlighter->highlight($language, $block['element']['text']['text']); |
75
|
|
|
$block['element']['text']['text'] = $highlighted->value; |
76
|
|
|
|
77
|
|
|
// Only return the block if Highlighter knew the language and how to handle it. |
78
|
|
|
return $block; |
79
|
|
|
} |
80
|
|
|
// Exception thrown when language not supported |
81
|
|
|
catch (\DomainException $exception) |
82
|
|
|
{ |
83
|
|
|
trigger_error("An unsupported language ($language) was detected in a code block", E_USER_WARNING); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return parent::blockFencedCodeComplete($block); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/// |
91
|
|
|
// MarkupEngine Implementation |
92
|
|
|
/// |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function getTemplateTag() |
98
|
|
|
{ |
99
|
|
|
return 'markdown'; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
public function getExtensions() |
106
|
|
|
{ |
107
|
|
|
return [ |
108
|
|
|
'md', |
109
|
|
|
'mdown', |
110
|
|
|
'markdown', |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
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
.