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 __; |
11
|
|
|
use allejo\stakx\RuntimeStatus; |
12
|
|
|
use allejo\stakx\Service; |
13
|
|
|
use Highlight\Highlighter; |
14
|
|
|
|
15
|
|
|
class MarkdownEngine extends \ParsedownExtra implements MarkupEngineInterface |
16
|
|
|
{ |
17
|
|
|
protected $highlighter; |
18
|
|
|
|
19
|
87 |
|
public function __construct() |
20
|
|
|
{ |
21
|
87 |
|
parent::__construct(); |
22
|
|
|
|
23
|
87 |
|
$this->highlighter = new Highlighter(); |
24
|
87 |
|
} |
25
|
|
|
|
26
|
11 |
|
protected function blockHeader($Line) |
27
|
|
|
{ |
28
|
11 |
|
$Block = parent::blockHeader($Line); |
29
|
|
|
|
30
|
11 |
|
if (isset($Block['element']['text'])) |
31
|
|
|
{ |
32
|
11 |
|
$Block['element']['attributes']['id'] = $this->slugifyHeader($Block); |
33
|
|
|
} |
34
|
|
|
|
35
|
11 |
|
return $Block; |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
3 |
|
protected function blockSetextHeader($Line, array $Block = null) |
39
|
|
|
{ |
40
|
3 |
|
$Block = parent::blockSetextHeader($Line, $Block); |
|
|
|
|
41
|
|
|
|
42
|
3 |
|
if (isset($Block['element']['name'])) |
43
|
|
|
{ |
44
|
1 |
|
$element = $Block['element']['name']; |
45
|
|
|
|
46
|
1 |
|
if (in_array($element, ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'])) |
47
|
|
|
{ |
48
|
1 |
|
$Block['element']['attributes']['id'] = $this->slugifyHeader($Block); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
return $Block; |
53
|
|
|
} |
54
|
|
|
|
55
|
11 |
|
private function slugifyHeader($Block) |
56
|
|
|
{ |
57
|
11 |
|
return __::slug($Block['element']['text']); |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
protected function blockFencedCodeComplete($block) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
// The class has a `language-` prefix, remove this to get the language |
63
|
3 |
|
if (isset($block['element']['text']['attributes']) && Service::hasRunTimeFlag(RuntimeStatus::USING_HIGHLIGHTER)) |
64
|
|
|
{ |
65
|
2 |
|
$cssClass = $block['element']['text']['attributes']['class']; |
66
|
2 |
|
$language = substr($cssClass, 9); |
67
|
|
|
|
68
|
|
|
try |
69
|
|
|
{ |
70
|
2 |
|
$highlighted = $this->highlighter->highlight($language, $block['element']['text']['text']); |
71
|
1 |
|
$block['markup'] = "<pre><code class=\"hljs ${cssClass}\">" . $highlighted->value . '</code></pre>'; |
72
|
|
|
|
73
|
|
|
// Only return the block if Highlighter knew the language and how to handle it. |
74
|
1 |
|
return $block; |
75
|
|
|
} |
76
|
|
|
// Exception thrown when language not supported |
77
|
1 |
|
catch (\DomainException $exception) |
78
|
|
|
{ |
79
|
1 |
|
trigger_error("An unsupported language ($language) was detected in a code block", E_USER_WARNING); |
80
|
|
|
} |
81
|
|
|
catch (\Exception $e) |
82
|
|
|
{ |
83
|
|
|
trigger_error('An error has occurred in the highlight.php language definition files', 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
|
76 |
|
return 'markdown'; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
public function getExtensions() |
106
|
|
|
{ |
107
|
|
|
return [ |
108
|
76 |
|
'md', |
109
|
|
|
'mdown', |
110
|
|
|
'markdown', |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.