|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright 2018 Vladimir Jimenez |
|
5
|
|
|
* @license https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace allejo\stakx\MarkupEngine; |
|
9
|
|
|
|
|
10
|
|
|
use allejo\stakx\RuntimeStatus; |
|
11
|
|
|
use allejo\stakx\Service; |
|
12
|
|
|
use Highlight\Highlighter; |
|
13
|
|
|
|
|
14
|
|
|
class MarkdownEngine extends \ParsedownExtra implements MarkupEngineInterface |
|
15
|
|
|
{ |
|
16
|
|
|
protected $highlighter; |
|
17
|
|
|
|
|
18
|
87 |
|
public function __construct() |
|
19
|
|
|
{ |
|
20
|
87 |
|
parent::__construct(); |
|
21
|
|
|
|
|
22
|
87 |
|
$this->highlighter = new Highlighter(); |
|
23
|
87 |
|
} |
|
24
|
|
|
|
|
25
|
11 |
|
protected function blockHeader($Line) |
|
26
|
|
|
{ |
|
27
|
11 |
|
$Block = parent::blockHeader($Line); |
|
28
|
|
|
|
|
29
|
11 |
|
if (isset($Block['element']['text'])) |
|
30
|
|
|
{ |
|
31
|
11 |
|
$Block['element']['attributes']['id'] = $this->slugifyHeader($Block); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
11 |
|
return $Block; |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
3 |
|
protected function blockSetextHeader($Line, array $Block = null) |
|
38
|
|
|
{ |
|
39
|
3 |
|
$Block = parent::blockSetextHeader($Line, $Block); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
3 |
|
if (isset($Block['element']['name'])) |
|
42
|
|
|
{ |
|
43
|
1 |
|
$element = $Block['element']['name']; |
|
44
|
|
|
|
|
45
|
1 |
|
if (in_array($element, ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'])) |
|
46
|
|
|
{ |
|
47
|
1 |
|
$Block['element']['attributes']['id'] = $this->slugifyHeader($Block); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
3 |
|
return $Block; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
11 |
|
private function slugifyHeader($Block) |
|
55
|
|
|
{ |
|
56
|
11 |
|
$id = strtolower($Block['element']['text']); |
|
57
|
11 |
|
$id = str_replace(' ', '-', $id); |
|
58
|
11 |
|
$id = preg_replace('/[^0-9a-zA-Z-_]/', '', $id); |
|
59
|
|
|
|
|
60
|
11 |
|
return preg_replace('/-+/', '-', $id); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
3 |
|
protected function blockFencedCodeComplete($block) |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
// The class has a `language-` prefix, remove this to get the language |
|
66
|
3 |
|
if (isset($block['element']['text']['attributes']) && Service::hasRunTimeFlag(RuntimeStatus::USING_HIGHLIGHTER)) |
|
67
|
|
|
{ |
|
68
|
2 |
|
$cssClass = $block['element']['text']['attributes']['class']; |
|
69
|
2 |
|
$language = substr($cssClass, 9); |
|
70
|
|
|
|
|
71
|
|
|
try |
|
72
|
|
|
{ |
|
73
|
2 |
|
$highlighted = $this->highlighter->highlight($language, $block['element']['text']['text']); |
|
74
|
1 |
|
$block['markup'] = "<pre><code class=\"hljs ${cssClass}\">" . $highlighted->value . '</code></pre>'; |
|
75
|
|
|
|
|
76
|
|
|
// Only return the block if Highlighter knew the language and how to handle it. |
|
77
|
1 |
|
return $block; |
|
78
|
|
|
} |
|
79
|
|
|
// Exception thrown when language not supported |
|
80
|
1 |
|
catch (\DomainException $exception) |
|
81
|
|
|
{ |
|
82
|
1 |
|
trigger_error("An unsupported language ($language) was detected in a code block", E_USER_WARNING); |
|
83
|
|
|
} |
|
84
|
|
|
catch (\Exception $e) |
|
85
|
|
|
{ |
|
86
|
|
|
trigger_error('An error has occurred in the highlight.php language definition files', E_USER_WARNING); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return parent::blockFencedCodeComplete($block); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/// |
|
94
|
|
|
// MarkupEngine Implementation |
|
95
|
|
|
/// |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* {@inheritdoc} |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getTemplateTag() |
|
101
|
|
|
{ |
|
102
|
76 |
|
return 'markdown'; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* {@inheritdoc} |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getExtensions() |
|
109
|
|
|
{ |
|
110
|
|
|
return [ |
|
111
|
76 |
|
'md', |
|
112
|
|
|
'mdown', |
|
113
|
|
|
'markdown', |
|
114
|
|
|
]; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.