forked from N-Parsons/grav-plugin-glossary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglossary.php
71 lines (62 loc) · 1.82 KB
/
glossary.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
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
use RocketTheme\Toolbox\Event\Event;
class GlossaryPlugin extends Plugin
{
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPageInitialized' => ['onPageInitialized', 0],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onGetPageTemplates' => ['onGetPageTemplates', 0],
];
}
/**
* Add templates to Twig lookup paths
*/
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__.'/templates';
}
/**
* Add templates to Grav Admin
*/
public function onGetPageTemplates(Event $event)
{
$event->types->scanTemplates(__DIR__."/templates");
}
/**
* Insert abbreviations into the raw content
*
* Taken from https://github.com/asmeikal/grav-plugin-acronyms (MIT License).
* Minor modifications have been made to adapt it for the current use case.
*/
public function insertAbbreviations(Event $event)
{
return;
}
/**
* Expose glossary terms to JavaScript
*/
public function onPageInitialized()
{
// Ensure this method runs only on the front-end
if ($this->isAdmin()) {
return;
}
// Get config
$pluginConfig = $this->config->get('plugins.glossary', array());
// Check if abbreviations are enabled and get terms
if (!empty($pluginConfig['abbreviations']) && !empty($pluginConfig['definitions'])) {
$terms = $pluginConfig['definitions'];
// Add script with glossary terms to the head section
$this->grav['assets']->addInlineJs(
'window.glossaryTerms = ' . json_encode($terms) . ';'
);
}
}
}