-
Notifications
You must be signed in to change notification settings - Fork 5
/
presentation.php
596 lines (568 loc) · 20.2 KB
/
presentation.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
<?php
/**
* Presentation Plugin
*
* PHP version 7
*
* @category Extensions
* @package Grav
* @subpackage Presentation
* @author Ole Vik <[email protected]>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://github.com/OleVik/grav-plugin-presentation
*/
namespace Grav\Plugin;
use Grav\Common\Grav;
use Grav\Common\Plugin;
use Grav\Common\Utils;
use Grav\Common\Uri;
use Grav\Common\Inflector;
use Grav\Common\Page\Page;
use Grav\Common\Page\Pages;
use Grav\Common\Page\Media;
use Grav\Common\Page\Collection;
use RocketTheme\Toolbox\Event\Event;
use Grav\Plugin\PresentationPlugin\API\Content;
use Grav\Plugin\PresentationPlugin\API\Parser;
use Grav\Plugin\PresentationPlugin\API\Transport;
use Grav\Plugin\PresentationPlugin\API\Poll;
use Grav\Plugin\PresentationPlugin\Utilities;
/**
* Creates slides using Reveal.js
*
* Class PresentationPlugin
*
* @category Extensions
* @package Grav\Plugin
* @author Ole Vik <[email protected]>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://github.com/OleVik/grav-plugin-presentation
*/
class PresentationPlugin extends Plugin
{
/**
* Protected variables
*
* @var bool $cache Grav cache setting
* @var Transport $transport Transport API
* @var Parser $parser Parser API
* @var Content $content Content API
*/
protected $cache;
protected $cacheTwig;
protected $transport;
protected $parser;
protected $content;
/**
* Register intial event and libraries
*
* @return array
*/
public static function getSubscribedEvents()
{
include __DIR__ . '/vendor/autoload.php';
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
];
}
/**
* Initialize the plugin and events
*
* @return void
*/
public function onPluginsInitialized()
{
if ($this->config->get('plugins.presentation.enabled') != true) {
return;
}
if ($this->config->get('system.debugger.enabled')) {
$this->grav['debugger']->startTimer('presentation', 'Presentation');
}
if ($this->isAdmin() && $this->config->get('plugins.admin')) {
$this->enable(
[
'onPagesInitialized' => ['handleAPI', 0],
'onGetPageTemplates' => ['onGetPageTemplates', 0],
'onTwigSiteVariables' => ['twigBaseUrl', 0],
'onAssetsInitialized' => ['onAdminPagesAssetsInitialized', 0]
]
);
}
$this->cache = $this->grav['config']->get('system.cache.enabled');
$this->cacheTwig = $this->grav['config']->get('system.pages.never_cache_twig');
$this->enable(
[
'onShortcodeHandlers' => ['onShortcodeHandlers', 0],
'onPageInitialized' => ['pagePreCache', 0],
'onPageContentProcessed' => ['pageIteration', -10],
'onTwigExtensions' => ['onTwigExtensions', 0],
'onTwigTemplatePaths' => ['templates', 0],
'onTwigSiteVariables' => ['twigBaseUrl', 0],
'onPagesInitialized' => ['handleAPI', 0],
'onAssetsInitialized' => ['onAssetsInitialized', 0],
'onShutdown' => ['onShutdown', 0]
]
);
if ($this->config->get('system.debugger.enabled')) {
$this->grav['debugger']->stopTimer('presentation');
}
}
/**
* Declare config from plugin-config
*
* @return array Plugin configuration
*/
public function config()
{
if ($this->config->get('plugins.presentation')) {
return (array) $this->config->get('plugins.presentation');
}
return false;
}
/**
* Disable caching for related templates
*
* @return void
*/
public function pagePreCache()
{
if ($this->grav['page']->template() == 'presentation' || $this->grav['page']->template() == 'slide') {
$this->grav['config']->set('system.cache.enabled', false);
$this->grav['config']->set('system.pages.never_cache_twig', true);
}
}
/**
* Construct the page
*
* @return void
*/
public function pageIteration()
{
$grav = $this->grav;
$config = $this->config();
if ($grav['page']->template() == 'presentation' || $grav['page']->template() == 'slide') {
if (!isset($this->grav['twig']->twig_vars['reveal_init'])) {
$baseUrl = $this->grav['uri']->rootUrl(true);
$header = (array) $grav['page']->header();
if (isset($header['presentation'])) {
$config = Utils::arrayMergeRecursiveUnique(
$config,
$header['presentation']
);
}
$this->transport = $this->getAPIInstance(
$config['transport']
);
$this->parser = $this->getAPIInstance(
$config['parser'],
$config,
$this->transport
);
$this->content = $this->getAPIInstance(
$config['content'],
$this->grav,
$config,
$this->parser,
$this->transport
);
$styles = $this->config->get('plugins.presentation.style') ??
$this->config->get('plugins.presentation.styles') ??
[];
if (!empty($styles) && is_array($styles) && Utils::arrayIsAssociative($styles)) {
$this->parser->processor(
$styles,
'presentation',
(array) $grav['page']
);
}
$tree = $this->content->buildTree($grav['page']->route());
$slides = $this->content->buildContent($tree);
$grav['page']->setRawContent($slides);
$menu = $this->content->buildMenu($tree);
$menu = Utilities::flattenArray($menu, 1);
$options = Utilities::parseAmbiguousArrayValues(
$this->config->get('plugins.presentation.options')
);
$options = json_encode($options, JSON_PRETTY_PRINT);
$breakpoints = json_encode(
$this->config->get('plugins.presentation.breakpoints')
);
$this->grav['twig']->twig_vars['reveal_init'] = $options;
$grav['assets']->addInlineJs('const reveal_init = ' . $options . ';', null, 'presentation');
$this->grav['twig']->twig_vars['presentation_menu'] = $menu;
$this->grav['twig']->twig_vars['presentation_breakpoints'] = $breakpoints;
if ($grav['page']->template() == 'presentation') {
$grav['assets']->addInlineCss($this->transport->getStyles(), null, 'presentation');
}
}
}
}
/**
* Handle API
*
* @return void
*/
public function handleAPI()
{
$uri = $this->grav['uri'];
$page = $this->grav['page'];
$plugins = $this->config->get('plugins');
if ($uri->path() == '/' . $this->config->get('plugins.presentation.api_route')) {
if ($_GET['action'] == 'poll') {
$this->handlePollAPI(
$uri,
$page,
(array) $this->config->get('plugins.presentation')
);
}
}
if (isset($plugins['admin']) && $plugins['admin']['enabled'] == true) {
$adminRoute = $this->config->get('plugins')['admin']['route'];
if ($uri->path() == $adminRoute . '/' . $this->config->get('plugins.presentation.api_route')) {
if ($_GET['action'] == 'save') {
$this->handleSaveAPI();
}
}
}
}
/**
* Handle Save API
*
* @return void
*/
public function handleSaveAPI()
{
if (!$this->isAdmin() || empty($_POST)) {
return;
}
header('Content-Type: application/json');
header("allow-control-access-origin: * ");
header('HTTP/1.1 200 OK');
try {
$post = file_get_contents('php://input') ?? $_POST;
$post = json_decode($post, true);
$pages = Grav::instance()['pages'];
$page = $pages->find('/' . $post['route']);
$page->rawMarkdown(base64_decode($post['content']));
$page->save();
echo '200 OK';
} catch (\Exception $e) {
echo $e;
}
exit();
}
/**
* Handle Poll API
*
* @param array $config Plugin configuration
*
* @return void
*/
public function handlePollAPI($config)
{
if (isset($config['sync']) && $config['sync'] == 'poll') {
set_time_limit(0);
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Pragma: no-cache');
if (!isset($_GET['mode'])) {
header('HTTP/1.1 400 Bad Request');
exit('400 Bad Request');
}
$res = Grav::instance()['locator'];
$target = $res->findResource('cache://') . '/presentation';
include_once __DIR__ . '/API/PollInterface.php';
include_once __DIR__ . '/API/Poll.php';
$poll = new Poll($target, 'Poll.json');
gc_enable();
if (!isset($config['token']) || empty($config['token'])) {
return;
}
if ($_GET['mode'] == 'set' && isset($_GET['data'])) {
Utilities::authorize($config['token']);
$poll->remove();
header('Content-Type:text/plain');
header('HTTP/1.1 202 Accepted');
$poll->set(urldecode($_GET['data']));
} elseif ($_GET['mode'] == 'get') {
header('Content-Type: application/json');
header('HTTP/1.1 200 OK');
$poll->get();
} elseif ($_GET['mode'] == 'remove') {
Utilities::authorize($config['token']);
header('Content-Type:text/plain');
header('HTTP/1.1 200 OK');
$poll->remove();
}
$poll = null;
unset($poll);
gc_collect_cycles();
gc_disable();
exit();
}
}
/**
* Add templates-directory to Twig paths
*
* @return void
*/
public function templates()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
/**
* Add root URL to Twig vars
*
* @return void
*/
public function twigBaseUrl()
{
$uri = $this->grav['uri']->rootUrl(true);
$this->grav['twig']->twig_vars['presentation_base_url'] = $uri;
}
/**
* Reset cache on shutdown
*
* @return void
*/
public function onShutdown()
{
$this->grav['config']->set('system.cache.enabled', $this->cache);
$this->grav['config']->set('system.pages.never_cache_twig', $this->cacheTwig);
}
/**
* Add Twig Extensions
*
* @return void
*/
public function onTwigExtensions()
{
include_once __DIR__ . '/twig/CallStaticExtension.php';
$this->grav['twig']->twig->addExtension(new PresentationPlugin\CallStaticTwigExtension());
include_once __DIR__ . '/twig/FileFinderExtension.php';
$this->grav['twig']->twig->addExtension(new PresentationPlugin\FileFinderTwigExtension());
}
/**
* Register Page templates
*
* @param Event $event RocketTheme\Toolbox\Event\Event
*
* @return void
*/
public function onGetPageTemplates(Event $event)
{
$types = $event->types;
$locator = Grav::instance()['locator'];
$locations = [
'plugin://' . $this->name . '/blueprints/',
'theme://blueprints/',
'user://blueprints/'
];
foreach ($locations as $location) {
$types->register(
'presentation',
$locator->findResource($location . 'presentation.yaml')
);
$types->register(
'slide',
$locator->findResource($location . 'slide.yaml')
);
}
}
/**
* Get API Instance
*
* @param string $class Class name
* @param mixed ...$args Class arguments
*
* @return mixed Class Instance
*/
public function getAPIInstance(string $class, ...$args)
{
$caller = '\Grav\Plugin\PresentationPlugin\API\\' . $class;
return new $caller(...$args);
}
/**
* Get list of modular scales
*
* @return array List of modular scales
*/
public static function getModularScale()
{
return array(
['name' => 'unison', 'ratio' => '1:1', 'numerical' => 1],
['name' => 'minor second', 'ratio' => '15:16', 'numerical' => 1.067],
['name' => 'major second', 'ratio' => '8:9', 'numerical' => 1.125],
['name' => 'minor third', 'ratio' => '5:6', 'numerical' => 1.2],
['name' => 'major third', 'ratio' => '4:5', 'numerical' => 1.25],
['name' => 'perfect fourth', 'ratio' => '3:4', 'numerical' => 1.333],
['name' => 'aug. fourth / dim. fifth', 'ratio' => '1:√2', 'numerical' => 1.414],
['name' => 'perfect fifth', 'ratio' => '2:3', 'numerical' => 1.5],
['name' => 'minor sixth', 'ratio' => '5:8', 'numerical' => 1.6],
['name' => 'golden section', 'ratio' => '1:1.618', 'numerical' => 1.618],
['name' => 'major sixth', 'ratio' => '3:5', 'numerical' => 1.667],
['name' => 'minor seventh', 'ratio' => '9:16', 'numerical' => 1.778],
['name' => 'major seventh', 'ratio' => '8:15', 'numerical' => 1.875],
['name' => 'octave', 'ratio' => '1:2', 'numerical' => 2],
['name' => 'major tenth', 'ratio' => '2:5', 'numerical' => 2.5],
['name' => 'major eleventh', 'ratio' => '3:8', 'numerical' => 2.667],
['name' => 'major twelfth', 'ratio' => '1:3', 'numerical' => 3],
['name' => 'double octave', 'ratio' => '1:4', 'numerical' => 4]
);
}
/**
* Parse modular scales for blueprints
*
* @return array Blueprint-friendly list of modular scales
*/
public static function getModularScaleBlueprintOptions()
{
$options = ['' => 'None'];
foreach (self::getModularScale() as $scale) {
$options[(string) $scale['numerical']] = $scale['numerical'] . ' (' . ucwords($scale['name']) . ', ' . $scale['ratio'] . ')';
}
return $options;
}
/**
* Get class names for blueprints
*
* @param string $key Needle to search for
*
* @return array Blueprint-friendly list of class names
*/
public static function getClassNamesBlueprintOptions(string $key)
{
$regex = '/Grav\\\\Plugin\\\\PresentationPlugin\\\\API\\\\(?<api>.*)/i';
$classes = preg_grep($regex, get_declared_classes());
$matches = preg_grep('/' . $key . '/i', $classes);
$options = [
'' => 'None',
$key => $key
];
foreach ($matches as $match) {
$match = str_replace('Grav\Plugin\PresentationPlugin\API\\', '', $match);
$options[$match] = $match;
}
return $options;
}
/**
* Get reveal.js themes
*
* @return array Associative array of styles
*/
public static function getRevealThemes()
{
include __DIR__ . '/vendor/autoload.php';
$inflector = new Inflector();
$options = array('none' => 'None');
$path = 'user://plugins/presentation/node_modules/reveal.js/css/theme';
$location = Grav::instance()['locator']->findResource($path, true);
if (!$location) {
return $options;
}
$files = Utilities::filesFinder($location, ['css']);
if (empty($files)) {
return $options;
}
foreach ($files as $file) {
$key = $file->getBasename('.' . $file->getExtension());
$options[$key] = $inflector->titleize($key);
}
return $options;
}
/**
* Initialize shortcodes
*
* @return void
*/
public function onShortcodeHandlers()
{
$this->grav['shortcode']->registerAllShortcodes(__DIR__ . '/shortcodes');
}
/**
* Add admin assets
*
* @return void
*/
public function onAdminPagesAssetsInitialized()
{
$uri = $this->grav['uri'];
$res = Grav::instance()['locator'];
$path = $res->findResource('plugin://' . $this->name, false);
$adminRoute = $this->config->get('plugins.admin.route');
if (!Utils::contains($uri->path(), $adminRoute . '/pages')) {
return;
}
if ($this->config->get('plugins.presentation.admin_async_save') !== true) {
return;
}
$adminRoute = $uri->rootUrl(true) . $adminRoute;
$inlineJsConstants = array(
'presentationAPIRoute = "' . $adminRoute . '/' . $this->config->get('plugins.presentation.api_route') . '"',
'presentationAPITimeout = ' . ($this->config->get('plugins.presentation.poll_timeout') ?: 2000) * 2.5,
'presentationAPIRetryLimit = ' . ($this->config->get('plugins.presentation.poll_retry_limit') ?: 10),
'presentationAdminAsyncSave = ' . ($this->config->get('plugins.presentation.admin_async_save') ?: 0),
'presentationAdminAsyncSaveTyping = ' . ($this->config->get('plugins.presentation.admin_async_save_typing') ?: 0)
);
$inlineJs = '';
foreach ($inlineJsConstants as $constant) {
$inlineJs .= 'const ' . $constant . ';' . "\n";
}
$this->grav['assets']->addInlineJs($inlineJs);
$this->grav['assets']->addJs(
$path . '/js/save.js'
);
$this->grav['assets']->addJs(
$path . '/node_modules/axios/dist/axios.min.js'
);
$this->grav['assets']->addJs(
$path . '/node_modules/js-base64/base64.min.js'
);
$this->grav['assets']->addJs(
$path . '/node_modules/codemirror/lib/codemirror.js'
);
}
/**
* Add general assets
*
* @return void
*/
public function onAssetsInitialized()
{
if ($this->config->get('plugins.presentation.textsizing')
&& $this->config->get('plugins.presentation.breakpoints')
&& !empty($this->config->get('plugins.presentation.breakpoints'))
) {
$css = '';
$element = '.reveal .slides section section, .reveal.center .slides section section';
$breakpoints = array_keys(
$this->config->get('plugins.presentation.breakpoints')
);
$sizes = array_values(
$this->config->get('plugins.presentation.breakpoints')
);
for ($i = 0; $i < count($breakpoints); $i++) {
$css .= '@media screen and ';
if ($i == 0) {
$css .= '(min-width: 0px) and ';
$css .= '(max-width:' . (intval($breakpoints[$i + 1]) - 1) . 'px) ';
} else {
$css .= '(min-width:' . $breakpoints[$i] . 'px) ';
}
$css .= '{' . $element . '{font-size:' . $sizes[$i] . 'px !important;}}';
$css .= "\n";
}
$this->grav['assets']->addInlineCss($css, null, 'critical');
}
$iframe = '.presentation-iframe {
width: 100%;
width: -moz-available;
width: -webkit-fill-available;
width: fill-available;
height: 100%;
height: -moz-available;
height: -webkit-fill-available;
height: fill-available;
}';
$this->grav['assets']->addInlineCss($iframe);
}
}