/
static-generator.php
463 lines (444 loc) · 14.6 KB
/
static-generator.php
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
<?php
/**
* StaticGenerator Plugin
*
* PHP version 7
*
* @category Extensions
* @package Grav
* @subpackage StaticGeneratorPlugin
* @author Ole Vik <git@olevik.net>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://github.com/OleVik/grav-plugin-static-generator
*/
namespace Grav\Plugin;
use Grav\Common\Grav;
use Grav\Common\Plugin;
use Grav\Common\Utils;
use Grav\Common\Inflector;
use Grav\Framework\File\YamlFile;
use Grav\Framework\File\Formatter\YamlFormatter;
use RocketTheme\Toolbox\Event\Event;
use Grav\Plugin\StaticGenerator\Data;
use Grav\Plugin\StaticGenerator\Timer;
use Grav\Plugin\StaticGenerator\Utilities;
use Grav\Plugin\StaticGenerator\Data\SSEData;
use Grav\Plugin\StaticGenerator\Config\SSEConfig;
use Grav\Plugin\StaticGenerator\Collection\SSECollection;
/**
* Persist Data and Pages from Grav
*
* PHP version 7
*
* @category Extensions
* @package Grav\Plugin\StaticGeneratorPlugin
* @author Ole Vik <git@olevik.net>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://github.com/OleVik/grav-plugin-static-generator
*/
class StaticGeneratorPlugin extends Plugin
{
/**
* Path for Presets Page
*
* @var string
*/
protected $route = 'presets';
/**
* Register intial event and libraries
*
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
'onGetPageBlueprints' => ['onGetPageBlueprints', 0]
];
}
/**
* Initialize the plugin and events
*
* @return void
*/
public function onPluginsInitialized()
{
if ($this->isAdmin() && $this->config->get('plugins.static-generator.admin')) {
$this->enable(
[
'onTwigTemplatePaths' => ['onTwigAdminTemplatePaths', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
'onAdminMenu' => ['onAdminMenu', 0],
'onAdminTaskExecute' => ['onAdminTaskExecute', 0]
]
);
}
}
public function onTwigAdminTemplatePaths()
{
$data = array();
$data['blueprints://'] = $this->grav['locator']->findResource('blueprints://');
foreach ($this->grav['locator']->getSchemes() as $stream) {
$data['schemes'][$stream] = Utils::url($stream . '://');
}
foreach ($this->grav['streams']->getStreams() as $name => $stream) {
$data['streams'][$name] = Utils::url($name . '://');
}
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
/**
* Register blueprints
*
* @param Event $event Instance of RocketTheme\Toolbox\Event\Event.
*
* @return void
*/
public function onGetPageBlueprints(Event $event)
{
$types = $event->types;
$types->scanBlueprints('plugins://' . $this->name . '/blueprints');
}
/**
* Register button in Admin Quick Tray
*
* @return void
*/
public function onAdminMenu()
{
if ($this->config->get('plugins.static-generator.quick_tray')) {
$index = [
'authorize' => $this->config->get('plugins.static-generator.quick_tray_permissions'),
'hint' => $this->grav['language']->translate(
['PLUGIN_STATIC_GENERATOR.ADMIN.INDEX.HINT']
),
'class' => 'static-generator-index',
'icon' => 'fa-bolt'
];
$this->grav['twig']->plugins_quick_tray['static-generator-index'] = $index;
$content = [
'authorize' => $this->config->get('plugins.static-generator.quick_tray_permissions'),
'hint' => $this->grav['language']->translate(
['PLUGIN_STATIC_GENERATOR.ADMIN.CONTENT.HINT']
),
'class' => 'static-generator-content',
'icon' => 'fa-archive'
];
$this->grav['twig']->plugins_quick_tray['static-generator-content'] = $content;
}
}
/**
* Handle Task-call from Admin
*
* @param Event $event Instance of RocketTheme\Toolbox\Event\Event.
*
* @return void
*/
public function onAdminTaskExecute(Event $event)
{
if (
$event['method'] == 'taskStaticGeneratorIndex' ||
$event['method'] == 'taskStaticGeneratorContent'
) {
if (!$event['controller']->authorizeTask($event['method'], ['admin.maintenance', 'admin.super'])) {
header('HTTP/1.0 403 Forbidden');
echo '403 Forbidden';
exit;
}
$mode = filter_input(
INPUT_GET,
'mode',
FILTER_SANITIZE_FULL_SPECIAL_CHARS
);
$route = filter_input(
INPUT_GET,
'route',
FILTER_SANITIZE_FULL_SPECIAL_CHARS
);
$slug = Inflector::hyphenize($route);
if (empty($slug)) {
$slug = 'index';
}
self::storeIndex(
urldecode($mode),
'/' . urldecode($route)
);
} elseif ($event['method'] == 'taskCopyPreset') {
if (!$event['controller']->authorizeTask('copyPreset', ['admin.maintenance', 'admin.super'])) {
header('HTTP/1.0 403 Forbidden');
echo '403 Forbidden';
exit;
}
$preset = filter_input(
INPUT_GET,
'preset',
FILTER_SANITIZE_FULL_SPECIAL_CHARS
);
self::copyPreset(
$preset,
$this->grav['config']->get('plugins.static-generator.content'),
$this->grav['locator']->findResource('config://')
);
} elseif ($event['method'] == 'taskGenerateFromPreset') {
if (!$event['controller']->authorizeTask('generateFromPreset', ['admin.maintenance', 'admin.super'])) {
header('HTTP/1.0 403 Forbidden');
echo '403 Forbidden';
exit;
}
$preset = filter_input(
INPUT_GET,
'preset',
FILTER_SANITIZE_FULL_SPECIAL_CHARS
);
self::generateFromPreset(
$preset,
$this->grav['locator']->findResource(
$this->grav['config']->get('plugins.static-generator.content')
. '/presets/' . $preset
)
);
}
}
/**
* Mirror Config
*
* @param string $name Preset name.
* @param string $location Location to store Config in.
* @param string $source Source to copy from.
* @param boolean $force Forcefully save.
*
* @return void
*/
public static function copyPreset(
string $name,
string $location,
string $source,
bool $force = true
): void {
try {
SSEData::headers();
$Timer = new Timer();
$Data = new SSEConfig();
$Data->mirror(
$name,
$location,
$source,
$Timer,
$force
);
SSEData::finish();
} catch (\Exception $e) {
throw new \Exception($e);
}
}
/**
* Generatic static site from preset
*
* @param string $name Preset name.
* @param string $location Location to store Data in.
* @param boolean $force Forcefully save.
*
* @return void
*/
public static function generateFromPreset(
string $name,
string $location,
bool $force = true
): void {
// WIP
exit;
try {
SSEData::headers();
$Timer = new Timer();
$Data = new SSECollection('@root', '/', $location);
$Data->setup($name);
$Data->collection();
SSEData::finish();
} catch (\Exception $e) {
throw new \Exception($e);
}
}
/**
* Create and store Data Index
*
* @param string $mode Mode of operation.
* @param string $route Route to Page.
*
* @return void
*/
public static function storeIndex(string $mode, string $route)
{
$config = Grav::instance()['config']->get('plugins.static-generator');
$location = $config[$mode];
try {
SSEData::headers();
$Timer = new Timer();
$Data = new SSEData(
$mode == 'content' ? true : false,
$config['content_max_length']
);
$Data->setup();
$route = $Data->verify($route);
$Data->index($route);
$slug = Inflector::hyphenize($route);
$Data->teardown($location, $slug, $Data->data, $Timer);
SSEData::finish();
} catch (\Exception $e) {
throw new \Exception($e);
}
}
/**
* Register assets
*
* @return void
*/
public function onTwigSiteVariables()
{
$formatter = new YamlFormatter;
$file = new YamlFile(
$this->grav['locator']->findResource(
'plugin://' . $this->name . '/languages.yaml',
true,
true
),
$formatter
);
$translation = array();
foreach (array_keys(Utils::arrayFlattenDotNotation($file->load())) as $key) {
$key = str_replace('en.', '', $key);
$translation[$key] = $this->grav['language']->translate([$key]);
}
$this->grav['assets']->addInlineJs(
'const StaticGeneratorTranslation = ' . json_encode(
Utils::arrayUnflattenDotNotation($translation)['PLUGIN_STATIC_GENERATOR']
) . ';'
);
if ($this->config->get('plugins.static-generator.js')) {
$this->grav['assets']->addJs(
'plugin://static-generator/node_modules/eventsource/example/eventsource-polyfill.js'
);
$this->grav['assets']->addJs(
'plugin://static-generator/js/site-generator.admin.js'
);
}
if ($this->config->get('plugins.static-generator.css')) {
$this->grav['assets']->addInlineCss(
'pre.static-generator-command[data-header] {
position: relative;
margin: 1rem 1.5rem 0 1.5rem;
padding: 1.75rem 0.75rem 0.75rem 0.75rem;
white-space: pre-wrap;
}
pre.static-generator-command[data-header]:before {
content: attr(data-header);
display: block;
position: absolute;
top: 0;
right: 0;
left: 0;
background-color: #666666;
padding: 0 0.5rem;
font: bold 11px/20px Arial,Sans-Serif;
color: #ffffff;
}'
);
}
}
/**
* Get search files
*
* @param string $mode Mode of operation
*
* @return array Associative array of files
*/
public static function getSearchFiles(string $mode): array
{
$config = Grav::instance()['config']->get('plugins.static-generator');
$target = $config[$mode];
$files = Utilities::filesFinder($target, ['js']);
$searchFiles = array('' => 'None');
foreach ($files as $file) {
$name = $file->getFilename();
$searchFiles[$target . '/' . $name] = $name;
}
return $searchFiles;
}
/**
* Get Blueprint fields
*
* @param string $path Path to blueprint
* @param string $prefix Optional key-prefix
*
* @return array Associative, nested array of settings
*/
public static function getBlueprintFields(string $path, string $prefix = ''): array
{
$config = Grav::instance()['config'];
$locator = Grav::instance()['locator'];
$formatter = new YamlFormatter;
$file = new YamlFile($locator->findResource($path, true, true), $formatter);
$return = array();
foreach ($file->load() as $name => $data) {
if (is_array($data)) {
foreach ($data as $key => $property) {
if (Utils::contains($key, '@')) {
$key = str_replace(['data-', '@'], '', $key);
if (is_string($property)) {
$data[$key] = call_user_func_array(
$property,
[]
);
} elseif (is_array($property)) {
$data[$key] = call_user_func_array(
$property[0],
array_slice($property, 1, count($property) - 1, true)
);
}
}
}
}
if (!isset($data['name'])) {
$data['name'] = $prefix . $name;
}
if (!isset($data['default']) && $config->get('theme.' . $name)) {
$data['default'] = $config->get('theme.' . $name);
}
$return[$prefix . $name] = $data;
}
return $return;
}
/**
* Get available Admin permissions
*
* @return array Key-value array of permissions
*/
public static function getAdminPermissionsBlueprint(): array
{
$return = array();
if (!isset(Grav::instance()['admin'])) {
return $return;
}
if (\method_exists(Grav::instance()['admin'], 'getPermissions')) {
$permissions = Grav::instance()['admin']->getPermissions();
} elseif (\method_exists(Grav::instance()['permissions'], 'getInstances')) {
$permissions = Grav::instance()['permissions']->getInstances();
}
if (is_array($permissions) && !empty($permissions)) {
foreach (array_keys($permissions) as $permission) {
$return[] = [
'text' => $permission,
'value' => $permission
];
}
}
return $return;
}
/**
* Composer autoload.
*
* @return \Composer\Autoload\ClassLoader
*/
public function autoload(): \Composer\Autoload\ClassLoader
{
return require __DIR__ . '/vendor/autoload.php';
}
}