1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* This file is part of the O2System Framework package.
|
4
|
|
|
*
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE
|
6
|
|
|
* file that was distributed with this source code.
|
7
|
|
|
*
|
8
|
|
|
* @author Steeve Andrian Salim
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim
|
10
|
|
|
*/
|
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------
|
13
|
|
|
|
14
|
|
|
namespace O2System\Parser\String\Adapters;
|
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------
|
17
|
|
|
|
18
|
|
|
use O2System\Parser\String\Abstracts\AbstractAdapter;
|
19
|
|
|
use O2System\Spl\Exceptions\RuntimeException;
|
20
|
|
|
|
21
|
|
|
/**
|
22
|
|
|
* Class Markdown
|
23
|
|
|
*
|
24
|
|
|
* This class driver for Parse Markdown Code for O2System PHP Framework templating system.
|
25
|
|
|
*
|
26
|
|
|
* @package O2System\Parser\Drivers
|
27
|
|
|
*/
|
28
|
|
|
class Markdown extends AbstractAdapter
|
29
|
|
|
{
|
30
|
|
|
/**
|
31
|
|
|
* Markdown::MARKDOWN_BASIC
|
32
|
|
|
*
|
33
|
|
|
* @var int
|
34
|
|
|
*/
|
35
|
|
|
const MARKDOWN_BASIC = 0;
|
36
|
|
|
|
37
|
|
|
/**
|
38
|
|
|
* Markdown::MARKDOWN_GITHUB
|
39
|
|
|
*
|
40
|
|
|
* @var int
|
41
|
|
|
*/
|
42
|
|
|
const MARKDOWN_GITHUB = 1;
|
43
|
|
|
|
44
|
|
|
/**
|
45
|
|
|
* Markdown::MARKDOWN_EXTRA
|
46
|
|
|
*
|
47
|
|
|
* @var int
|
48
|
|
|
*/
|
49
|
|
|
const MARKDOWN_EXTRA = 2;
|
50
|
|
|
|
51
|
|
|
/**
|
52
|
|
|
* Markdown::$flavour
|
53
|
|
|
*
|
54
|
|
|
* @var int
|
55
|
|
|
*/
|
56
|
|
|
private $flavour = 0;
|
57
|
|
|
|
58
|
|
|
// ------------------------------------------------------------------------
|
59
|
|
|
|
60
|
|
|
/**
|
61
|
|
|
* Markdown::initialize
|
62
|
|
|
*
|
63
|
|
|
* @param array $config
|
64
|
|
|
*
|
65
|
|
|
* @return static
|
66
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
67
|
|
|
*/
|
68
|
|
|
public function initialize(array $config = [])
|
69
|
|
|
{
|
70
|
|
|
$config = array_merge($this->config, $config);
|
71
|
|
|
|
72
|
|
|
if (empty($this->engine)) {
|
73
|
|
|
if ($this->isSupported()) {
|
74
|
|
|
$this->engine = \cebe\markdown\Markdown();
|
|
|
|
|
75
|
|
|
|
76
|
|
|
if (isset($config[ 'flavour' ])) {
|
77
|
|
|
$this->setFlavour($config[ 'flavour' ]);
|
78
|
|
|
}
|
79
|
|
|
} else {
|
80
|
|
|
throw new RuntimeException(
|
81
|
|
|
'PARSER_E_THIRD_PARTY',
|
82
|
|
|
0,
|
83
|
|
|
['Markdown Parser by Carsten Brandt', 'https://github.com/cebe/markdown']
|
84
|
|
|
);
|
85
|
|
|
}
|
86
|
|
|
}
|
87
|
|
|
|
88
|
|
|
return $this;
|
89
|
|
|
}
|
90
|
|
|
|
91
|
|
|
// ------------------------------------------------------------------------
|
92
|
|
|
|
93
|
|
|
/**
|
94
|
|
|
* Markdown::isSupported
|
95
|
|
|
*
|
96
|
|
|
* Checks if this template engine is supported on this system.
|
97
|
|
|
*
|
98
|
|
|
* @return bool
|
99
|
|
|
*/
|
100
|
|
|
public function isSupported()
|
101
|
|
|
{
|
102
|
|
|
if (class_exists('\cebe\markdown\Markdown')) {
|
103
|
|
|
return true;
|
104
|
|
|
}
|
105
|
|
|
|
106
|
|
|
return false;
|
107
|
|
|
}
|
108
|
|
|
|
109
|
|
|
public function setFlavour($flavour)
|
110
|
|
|
{
|
111
|
|
|
if (is_int($flavour) AND in_array($flavour, range(0, 2))) {
|
112
|
|
|
$this->flavour = $flavour;
|
113
|
|
|
|
114
|
|
|
return true;
|
115
|
|
|
}
|
116
|
|
|
|
117
|
|
|
return false;
|
118
|
|
|
}
|
119
|
|
|
|
120
|
|
|
// ------------------------------------------------------------------------
|
121
|
|
|
|
122
|
|
|
/**
|
123
|
|
|
* Markdown::parse
|
124
|
|
|
*
|
125
|
|
|
* @param array $vars Variable to be parsed.
|
126
|
|
|
*
|
127
|
|
|
* @return string
|
128
|
|
|
*/
|
129
|
|
|
public function parse(array $vars = [])
|
130
|
|
|
{
|
131
|
|
|
switch ($this->flavour) {
|
132
|
|
|
default:
|
133
|
|
|
case self::MARKDOWN_BASIC:
|
134
|
|
|
|
135
|
|
|
return $this->engine->parse($this->string);
|
136
|
|
|
|
137
|
|
|
break;
|
|
|
|
|
138
|
|
|
|
139
|
|
|
case self::MARKDOWN_GITHUB:
|
140
|
|
|
|
141
|
|
|
$parser = new \cebe\markdown\GithubMarkdown();
|
|
|
|
|
142
|
|
|
|
143
|
|
|
return $parser->parse($this->string);
|
144
|
|
|
|
145
|
|
|
break;
|
146
|
|
|
|
147
|
|
|
case self::MARKDOWN_EXTRA:
|
148
|
|
|
|
149
|
|
|
$parser = new \cebe\markdown\MarkdownExtra();
|
|
|
|
|
150
|
|
|
|
151
|
|
|
return $parser->parse($this->string);
|
152
|
|
|
|
153
|
|
|
break;
|
154
|
|
|
}
|
155
|
|
|
}
|
156
|
|
|
|
157
|
|
|
// ------------------------------------------------------------------------
|
158
|
|
|
|
159
|
|
|
/**
|
160
|
|
|
* Markdown::isValidEngine
|
161
|
|
|
*
|
162
|
|
|
* Checks if is a valid Object Engine.
|
163
|
|
|
*
|
164
|
|
|
* @param object $engine Engine Object Resource.
|
165
|
|
|
*
|
166
|
|
|
* @return bool
|
167
|
|
|
*/
|
168
|
|
|
protected function isValidEngine($engine)
|
169
|
|
|
{
|
170
|
|
|
if ($engine instanceof \cebe\markdown\Markdown) {
|
|
|
|
|
171
|
|
|
return true;
|
172
|
|
|
}
|
173
|
|
|
|
174
|
|
|
return false;
|
175
|
|
|
}
|
176
|
|
|
} |