-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscholar.php
572 lines (550 loc) · 17.7 KB
/
scholar.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
<?php
/**
* Scholar Theme
*
* PHP version 7
*
* @category Extensions
* @package Grav
* @subpackage Scholar
* @author Ole Vik <[email protected]>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://github.com/OleVik/grav-theme-scholar
*/
namespace Grav\Theme;
use Grav\Common\Grav;
use Grav\Common\Theme;
use Grav\Common\Utils;
use Grav\Common\Inflector;
use Grav\Framework\File\YamlFile;
use Grav\Framework\File\Formatter\YamlFormatter;
use Grav\Theme\Scholar\Utilities;
use RocketTheme\Toolbox\Event\Event;
/**
* Scholar Theme
*
* Class Scholar
*
* @category Extensions
* @package Grav\Theme
* @author Ole Vik <[email protected]>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://github.com/OleVik/grav-theme-scholar
*/
class Scholar extends Theme
{
/**
* Register intial event and libraries
*
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onThemeInitialized' => ['onThemeInitialized', 0]
];
}
/**
* Initialize the theme and events
*
* @return void
*/
public function onThemeInitialized()
{
if (!$this->config->get('themes.scholar.enabled')) {
return;
}
if ($this->config->get('system.pages.type') == "flex") {
return;
}
if ($this->config->get('system.debugger.enabled')) {
$this->grav['debugger']->startTimer('scholar', 'Scholar');
}
if ($this->isAdmin() && $this->config->get('plugins.admin')) {
$this->enable(
[
'onGetPageBlueprints' => ['onGetPageBlueprints', 0],
'onGetPageTemplates' => ['onGetPageTemplates', 0]
]
);
} else {
$this->enable(
[
'onPagesInitialized' => ['onPagesInitialized', 0],
'onPageInitialized' => ['onPageInitialized', 0],
'onTwigExtensions' => ['onTwigExtensions', 0],
'onTwigTemplatePaths' => ['templates', 0],
'onTwigSiteVariables' => ['transportTaxonomyTranslations', 0]
]
);
}
$this->schemas();
if (isset($this->grav['shortcode'])) {
$this->registerShortcodes();
}
if ($this->config->get('system.debugger.enabled')) {
$this->grav['debugger']->stopTimer('scholar');
}
}
/**
* Get configuration blueprints
*
* @param string $path Path to blueprint
* @param string $prefix Optional key-prefix
*
* @return array Associative, nested array of settings
*/
public static function getConfigBlueprintFields(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) {
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 styles
*
* @return array Associative array of styles
*/
public static function getStylesBlueprint(): array
{
include __DIR__ . '/vendor/autoload.php';
$styles = array('' => 'Blank');
$styles = array('metal' => 'Metal');
$stylesFolders = Utils::arrayMergeRecursiveUnique(
Utilities::filesFinder('theme://css/styles', ['css']),
Utilities::filesFinder('user://themes/scholar/css/styles', ['css'])
);
foreach (array_unique($stylesFolders) as $style) {
$name = $style->getBasename('.' . $style->getExtension());
$styles[$name] = Inflector::titleize($name);
}
return $styles;
}
/**
* Get enabled components
*
* @return array
*/
public static function getComponentsBlueprint(): array
{
include __DIR__ . '/vendor/autoload.php';
$componentFolders = Utilities::foldersFinder(
[
'theme://components',
'user://themes/scholar/components'
]
);
$components = array();
foreach (array_unique($componentFolders) as $component) {
$components[] = [
'text' => ucfirst($component),
'value' => $component
];
}
return $components;
}
/**
* Get Highlighter themes
*
* @return array Associative array of styles
*/
public static function getHighlighterThemeBlueprint(): array
{
include __DIR__ . '/vendor/autoload.php';
$stylesFolders = Utils::arrayMergeRecursiveUnique(
Utilities::filesFinder('theme://css/highlighter', ['css']),
Utilities::filesFinder('user://themes/scholar/css/highlighter', ['css'])
);
$styles = array();
foreach (array_unique($stylesFolders) as $style) {
$name = $style->getBasename('.' . $style->getExtension());
$name = str_replace(['enlighterjs.', '.min'], '', $name);
$styles[$name] = Inflector::titleize($name);
}
return $styles;
}
/**
* Register blueprints
*
* @param Event $event Instance of RocketTheme\Toolbox\Event\Event.
*
* @return void
*/
public function onGetPageBlueprints(Event $event)
{
if (
$this->config->get('theme.components') !== null ||
!is_array($this->config->get('theme.components'))
) {
return;
}
foreach ($this->config->get('themes.scholar.components') as $component) {
$componentFolder = Utilities::folderFinder(
'blueprints',
[
'theme://components/' . $component,
'user://themes/scholar/components/' . $component
]
);
if ($componentFolder) {
$folder = $this->grav['locator']->findResource(
'theme://components/' . $component,
true,
true
);
if (is_dir($folder)) {
$event->types->scanBlueprints($folder);
}
}
}
}
/**
* Register Page blueprints
*
* @param Event $event Instance of RocketTheme\Toolbox\Event\Event.
*
* @return void
*/
public function onGetPageTemplates(Event $event)
{
if (
$this->config->get('theme.components') !== null ||
!is_array($this->config->get('theme.components'))
) {
return;
}
foreach ($this->config->get('themes.scholar.components') as $component) {
$folder = $this->grav['locator']->findResource(
'theme://components/' . $component,
true,
true
);
if (is_dir($folder)) {
$event->types->scanTemplates($folder);
}
}
}
/**
* Handle API
*
* @return void
*/
public function onPagesInitialized()
{
if ($this->grav['config']->get('theme.router')) {
$Router = self::getInstance(
$this->config->get(
'theme.api.router',
'Router\Router'
),
$this->grav
);
new $Router($this->grav);
}
}
/**
* Handle current Page
*
* @return void
*/
public function onPageInitialized()
{
if (
isset($this->grav['page']->header()->theme)
&& !empty($this->grav['page']->header()->theme)
) {
$this->grav['config']->set(
'theme',
array_merge(
$this->grav['config']->get('theme'),
$this->grav['page']->header()->theme
)
);
}
if ($this->grav['config']->get('theme.debug')) {
$this->grav['assets']->addJs(
$this->grav['locator']->findResource(
'theme://node_modules/@khanacademy/tota11y/dist/tota11y.min.js',
false,
true
)
);
}
if ($this->grav['config']->get('theme.linked_data')) {
$call = self::getInstance(
$this->grav['config']->get(
'theme.api.linked_data.default',
'LinkedData\PageLinkedData'
),
$this->grav['language'],
$this->grav['config']
);
if ($this->grav['page']->template() == 'cv') {
$call = self::getInstance(
$this->grav['config']->get(
'theme.api.linked_data.cv',
'LinkedData\CVLinkedData'
),
$this->grav['language'],
$this->grav['config']
);
}
$call->buildSchema($this->grav['page']);
$this->grav['assets']->addInlineJs(
$call::getSchema(
$call->data,
key($call::getType($this->grav['page']->template())),
true
),
['type' => 'application/ld+json']
);
}
}
/**
* Add taxonomy to Twig variables
*
* @return void
*/
public function transportTaxonomyTranslations()
{
$this->grav['twig']->twig_vars['site_taxonomy'] = $this->config->get('site.taxonomies');
$language = $this->grav['language']->getActive() ?? 'en';
$dateFormats = $this->config->get('system.pages.dateformat');
$dateFormatsJS = function () use ($dateFormats) {
$return = '';
foreach ($dateFormats as $key => $value) {
$return .= $key . ': "' . $value . '", ';
}
return rtrim($return, ", ");
};
$locator = $this->grav['locator'];
$formatter = new YamlFormatter;
$file = new YamlFile($locator->findResource('theme://languages.yaml', true, true), $formatter);
$translationStrings = array();
foreach (array_keys(Utils::arrayFlattenDotNotation($file->load())) as $key) {
$key = str_replace('en.', '', $key);
$translationStrings[$key] = $this->grav['language']->translate([$key]);
}
if ($this->grav['page']->template() == 'search') {
$searchRoute = $this->grav['page']->url(true, true, true);
} else {
$searchRoute = $this->grav['uri']->rootUrl(true) .
$this->config->get('themes.scholar.routes.search');
}
$this->grav['assets']->addInlineJs(
'const systemLanguage = "' . $language . '";' . "\n" .
'const systemDateformat = {' . $dateFormatsJS() . '};' . "\n" .
'const siteTaxonomy = [\'' . implode("','", $this->config->get('site.taxonomies')) . '\'];' . "\n" .
'const searchRoute = "' . $searchRoute . '";' . "\n" .
'const ScholarTranslation = ' . json_encode(Utils::arrayUnflattenDotNotation($translationStrings)['THEME_SCHOLAR']) . ';'
);
}
/**
* Register templates dynamically
*
* @return void
*/
public function templates()
{
if (
$this->config->get('theme.components') === null ||
!is_array($this->config->get('theme.components'))
) {
return;
}
$locator = $this->grav['locator'];
foreach ($this->config->get('theme.components') as $component) {
$target = Utilities::folderFinder(
$component,
[
'theme://components',
'user://themes/scholar/components'
]
);
$this->grav['twig']->twig_paths[] = $locator->findResource($target);
}
}
/**
* Register Schemas dynamically
*
* @return void
*/
public function schemas()
{
if (
$this->config->get('theme.components') !== null ||
!is_array($this->config->get('theme.components'))
) {
$this->grav['config']->set('theme.schema.default', 'CreativeWork');
$this->grav['config']->set('theme.schema.types.default', 'CreativeWork');
return;
}
$locator = $this->grav['locator'];
$formatter = new YamlFormatter;
$target = Utilities::fileFinder(
'schema.yaml',
[
'theme://components',
'user://themes/scholar/components'
]
);
$file = $locator->findResource(
$target,
true,
true
);
$YamlFile = new YamlFile(
$file,
$formatter
);
$data = $YamlFile->load();
if (isset($data['default'])) {
$this->grav['config']->set('theme.schema.default', $data['default']);
}
if (isset($data['types'])) {
foreach ($data['types'] as $schema => $data) {
$this->grav['config']->set('theme.schema.types.' . $schema, $data);
}
}
foreach ($this->config->get('theme.components') as $component) {
$target = Utilities::fileFinder(
'schema.yaml',
[
'theme://components/' . $component,
'user://themes/scholar/components/' . $component
]
);
$file = $locator->findResource(
$target,
true,
true
);
if (file_exists($file)) {
$YamlFile = new YamlFile(
$file,
$formatter
);
foreach ($YamlFile->load() as $schema => $data) {
$this->grav['config']->set('theme.schema.types.' . $schema, $data);
}
}
}
}
/**
* Initialize Twig Extensions
*
* @return void
*/
public function onTwigExtensions()
{
include_once __DIR__ . '/twig/ScholarTwigExtensions.php';
$this->grav['twig']->twig->addExtension(new ScholarTwigExtensions());
}
/**
* Initialize Shortcodes
*
* @return void
*/
public function registerShortcodes()
{
$this->grav['shortcode']->registerAllShortcodes(__DIR__ . '/shortcodes');
if (
$this->config->get('theme.components') !== null ||
!is_array($this->config->get('theme.components'))
) {
return;
}
foreach ($this->config->get('theme.components') as $component) {
$path = 'theme://components/' . $component . '/shortcodes';
if (is_dir($this->grav['locator']->findResource($path))) {
$this->grav['shortcode']->registerAllShortcodes($path);
}
}
}
/**
* Get Fully Qualified Class Name
*
* @return string FQCN
*/
public static function FQCN(): string
{
return __CLASS__;
}
/**
* Get class instance
*
* @param string $class Class name
* @param mixed ...$args Class arguments
*
* @return mixed Class instance
*/
public static function getInstance(string $class, ...$args)
{
$caller = '\\' . self::FQCN() . '\\' . $class;
return new $caller(...$args);
}
/**
* Get class names for blueprints
*
* @param string $key Needle to search for
*
* @return array Blueprint-friendly list of class names
*/
public static function getClassNames(string $key)
{
$classes = \HaydenPierce\ClassFinder\ClassFinder::getClassesInNamespace(
'Grav\Theme',
\HaydenPierce\ClassFinder\ClassFinder::RECURSIVE_MODE
);
$language = Grav::instance()['language'];
$matches = preg_grep('/' . $key . '/i', $classes);
$options = [
'' => $language->translate(['THEME_SCHOLAR.GENERIC.NONE'])
];
foreach ($matches as $match) {
if (Utils::contains($match, 'Abstract') || Utils::contains($match, 'Interface')) {
continue;
}
$match = str_replace(self::FQCN() . '\\', '', $match);
$options[$match] = $match;
}
return $options;
}
/**
* Composer autoload.
*
* @return \Composer\Autoload\ClassLoader
*/
public function autoload(): \Composer\Autoload\ClassLoader
{
return require __DIR__ . '/vendor/autoload.php';
}
}