-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathShortcodeManager.php
More file actions
323 lines (284 loc) · 8.33 KB
/
ShortcodeManager.php
File metadata and controls
323 lines (284 loc) · 8.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
namespace Grav\Plugin;
use Grav\Common\Data\Data;
use Grav\Common\Grav;
use Grav\Common\Page\Page;
use Guzzle\Common\Exception\UnexpectedValueException;
use Thunder\Shortcode\EventContainer\EventContainer;
use Thunder\Shortcode\HandlerContainer\HandlerContainer;
use Thunder\Shortcode\Processor\Processor;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
use Thunder\Shortcode\Syntax\CommonSyntax;
class ShortcodeManager
{
/** @var Grav $grav */
protected $grav;
/** @var Page $page */
protected $page;
/** @var HandlerContainer $handlers */
protected $handlers;
/** @var EventContainer $events */
protected $events;
protected $assets;
protected $states;
protected $objects;
/**
* initialize some internal instance variables
* @param Page $page
*/
public function __construct()
{
$this->grav = Grav::instance();
$this->config = $this->grav['config'];
$this->handlers = new HandlerContainer();
$this->events = new EventContainer();
$this->states = [];
$this->assets = [];
$this->objects = [];
}
/**
* add CSS and JS assets to the Manager so that they can be saved to cache
* for subsequent cached pages
*
* @param mixed $action the type of asset, JS or CSS, or an array of stuff
* @param string $asset the asset path in question
*/
public function addAssets($action, $asset)
{
if (is_array($action)) {
$this->assets['add'] [] = $action;
} else {
if (isset($this->assets[$action])) {
if (in_array($asset, $this->assets[$action])) {
return;
}
}
$this->assets[$action] [] = $asset;
}
}
/**
* return a multi-dimensional array of all the assets
*
* @return array the assets array
*/
public function getAssets()
{
return $this->assets;
}
/**
* reset the assets
*/
public function resetAssets()
{
$this->assets = [];
}
/**
* adds ad object
* @param $key the key to look up the object
* @param $object the object to store
*/
public function addObject($key, $object)
{
$new = [$object->name() => $object];
if (array_key_exists($key, $this->objects)) {
$current = (array)$this->objects[$key];
$this->objects[$key] = $current + $new;
} else {
$this->objects[$key] = $new;
}
}
/**
* sets all the objects
* @param $object the objects array
*/
public function setObjects($objects)
{
$this->objects = $objects;
}
/**
* return all the objects
* @return array the objects array
*/
public function getObjects() {
return $this->objects;
}
/**
* reset the objects
*/
public function resetObjects()
{
$this->objects = [];
}
/**
* returns the current handler container object
*
* @return HandlerContainer
*/
public function getHandlers()
{
return $this->handlers;
}
/**
* returns the current event container object
*
* @return EventContainer
*/
public function getEvents()
{
return $this->events;
}
/**
* register an individual shortcode with the manager so it can be
* operated on by the Shortcode library
*
* @param string $name the name of the shortcode (should match the classname)
* @param string $directory directory where the shortcode is located
*/
public function registerShortcode($name, $directory)
{
$path = rtrim($directory, '/').'/'.$name;
require_once($path);
$name = "Grav\\Plugin\\Shortcodes\\" . basename($name, '.php');
if (class_exists($name)) {
$shortcode = new $name();
$shortcode->init();
}
}
/**
* register all files as shortcodes in a particular directory
* @param string $directory directory where the shortcodes are located
*/
public function registerAllShortcodes($directory)
{
try {
foreach (new \DirectoryIterator($directory) as $file) {
if ($file->isDot()) {
continue;
}
$this->registerShortcode($file->getFilename(), $directory);
}
} catch (\UnexpectedValueException $e) {
Grav::instance()['log']->error('ShortcodeCore Plugin: Directory not found => ' . $directory);
}
}
/**
* setup the markdown parser to handle shortcodes properly
*
* @param mixed $markdown the markdown parser object
*/
public function setupMarkdown($markdown)
{
$markdown->addBlockType('[', 'ShortCodes', true, false, null, true);
$markdown->blockShortCodes = function($Line) {
$valid_shortcodes = implode('|', $this->handlers->getNames());
$regex = '/^\[\/?(?:'.$valid_shortcodes.')[^\]]*\]$/';
if (preg_match($regex, $Line['body'], $matches)) {
$Block = ['element' => [
'rawHtml' => $Line['body']
]];
return $Block;
}
};
}
/**
* process the content by running over all the known shortcodes with the
* chosen parser
*
* @param Page $page the page to work on
* @param Data $config configuration merged with the page config
*/
public function processContent(Page $page, Data $config)
{
$parser = $this->getParser($config->get('parser'));
if ($page && $config->get('enabled')) {
$this->page = $page;
$content = $page->getRawContent();
$processor = new Processor(new $parser(new CommonSyntax()), $this->handlers);
$processor = $processor->withEventContainer($this->events);
$processed_content = $processor->process($content);
return $processed_content;
}
}
/**
* Allow the processing of shortcodes directly on a string
* For example when used by Twig directly
*
* @param $str
* @return string
*/
public function processShortcodes($str)
{
$parser = $this->getParser($this->config->get('parser'));
$processor = new Processor(new $parser(new CommonSyntax()), $this->handlers);
$processed_string = $processor->process($str);
return $processed_string;
}
/**
* set a state of a particular shortcode with a hash for retrieval later
*
* @param string $hash a unique hash code
* @param ShortcodeInterface $shortcode the shortcode to store
*/
public function setStates($hash, ShortcodeInterface $shortcode)
{
$this->states[$hash][] = $shortcode;
}
/**
* returns the shortcode of a specific hash
*
* @param string $hash unique id of state
* @return ShortcodeInterface shortcode stored for this hash
*/
public function getStates($hash)
{
if (array_key_exists($hash, $this->states)) {
return $this->states[$hash];
}
}
/**
* helper method to create a unique shortcode based on the content
*
* @param ShortcodeInterface $shortcode
* @return string
*/
public function getId(ShortcodeInterface $shortcode)
{
return substr(md5($shortcode->getShortcodeText()), -10);
}
/**
* Sets the current page context
*
* @param Page $page
*/
public function setPage(Page $page)
{
$this->page = $page;
}
/** gets the current page context if set */
public function getPage()
{
return $this->page;
}
/**
* Get the appropriate parser object
*
* @param $parser
* @return string
*/
protected function getParser($parser)
{
switch($parser)
{
case 'regular':
$parser = 'Thunder\Shortcode\Parser\RegularParser';
break;
case 'wordpress':
$parser = 'Thunder\Shortcode\Parser\WordpressParser';
break;
default:
$parser = 'Thunder\Shortcode\Parser\RegexParser';
break;
}
return $parser;
}
}