|
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\Manager\AssetManager; |
|
12
|
|
|
use allejo\stakx\Markup\AssetHandlerTrait; |
|
13
|
|
|
use allejo\stakx\Markup\SyntaxHighlighterTrait; |
|
14
|
|
|
use allejo\stakx\RuntimeStatus; |
|
15
|
|
|
use allejo\stakx\Service; |
|
16
|
|
|
use Highlight\Highlighter; |
|
17
|
|
|
|
|
18
|
|
|
class MarkdownEngine extends \ParsedownExtra implements MarkupEngineInterface |
|
19
|
|
|
{ |
|
20
|
|
|
use AssetHandlerTrait; |
|
21
|
|
|
use SyntaxHighlighterTrait; |
|
22
|
|
|
|
|
23
|
91 |
|
public function __construct(AssetManager $assetManager) |
|
24
|
|
|
{ |
|
25
|
91 |
|
parent::__construct(); |
|
26
|
|
|
|
|
27
|
91 |
|
$this->highlighter = new Highlighter(); |
|
28
|
91 |
|
$this->assetManager = $assetManager; |
|
29
|
91 |
|
} |
|
30
|
|
|
|
|
31
|
20 |
|
public function parse($text, $contentItem = null) |
|
32
|
|
|
{ |
|
33
|
20 |
|
$this->contentItem = $contentItem; |
|
34
|
|
|
|
|
35
|
20 |
|
return parent::parse($text); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
11 |
|
protected function blockHeader($Line) |
|
39
|
|
|
{ |
|
40
|
11 |
|
$Block = parent::blockHeader($Line); |
|
41
|
|
|
|
|
42
|
11 |
|
if (isset($Block['element']['text'])) |
|
43
|
|
|
{ |
|
44
|
11 |
|
$Block['element']['attributes']['id'] = $this->slugifyHeader($Block); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
11 |
|
return $Block; |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
3 |
|
protected function blockSetextHeader($Line, array $Block = null) |
|
|
|
|
|
|
51
|
|
|
{ |
|
52
|
|
|
// @TODO Remove this `@` operator in an update to Parsedown and ParsedownExtra |
|
53
|
|
|
// https://wiki.php.net/rfc/notice-for-non-valid-array-container |
|
54
|
|
|
// https://github.com/erusev/parsedown-extra/issues/134 |
|
55
|
3 |
|
$Block = @parent::blockSetextHeader($Line, $Block); |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
3 |
|
if (isset($Block['element']['name'])) |
|
58
|
|
|
{ |
|
59
|
1 |
|
$element = $Block['element']['name']; |
|
60
|
|
|
|
|
61
|
1 |
|
if (in_array($element, ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'])) |
|
62
|
|
|
{ |
|
63
|
1 |
|
$Block['element']['attributes']['id'] = $this->slugifyHeader($Block); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
return $Block; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
7 |
|
protected function blockFencedCodeComplete($block) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
// The class has a `language-` prefix, remove this to get the language |
|
73
|
7 |
|
if (isset($block['element']['text']['attributes']) && Service::hasRunTimeFlag(RuntimeStatus::USING_HIGHLIGHTER)) |
|
74
|
|
|
{ |
|
75
|
6 |
|
$cssClass = $block['element']['text']['attributes']['class']; |
|
76
|
6 |
|
$block['markup'] = $this->highlightCode($cssClass, $block['element']['text']['text']); |
|
77
|
|
|
|
|
78
|
5 |
|
return $block; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
return parent::blockFencedCodeComplete($block); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function inlineImage($Excerpt) |
|
85
|
|
|
{ |
|
86
|
|
|
$imageBlock = parent::inlineImage($Excerpt); |
|
87
|
|
|
|
|
88
|
|
|
if ($imageBlock !== null) |
|
89
|
|
|
{ |
|
90
|
|
|
$imageSrc = trim($imageBlock['element']['attributes']['src']); |
|
91
|
|
|
|
|
92
|
|
|
$this->registerAsset($imageSrc); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $imageBlock; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
11 |
|
private function slugifyHeader($Block) |
|
99
|
|
|
{ |
|
100
|
11 |
|
return __::slug($Block['element']['text']); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/// |
|
104
|
|
|
// MarkupEngine Implementation |
|
105
|
|
|
/// |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* {@inheritdoc} |
|
109
|
|
|
*/ |
|
110
|
76 |
|
public function getTemplateTag() |
|
111
|
|
|
{ |
|
112
|
76 |
|
return 'markdown'; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* {@inheritdoc} |
|
117
|
|
|
*/ |
|
118
|
76 |
|
public function getExtensions() |
|
119
|
|
|
{ |
|
120
|
|
|
return [ |
|
121
|
76 |
|
'md', |
|
122
|
|
|
'mdown', |
|
123
|
|
|
'markdown', |
|
124
|
|
|
]; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
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.