forked from Sommerregen/grav-plugin-toc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toc.php
211 lines (187 loc) · 5.43 KB
/
toc.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
<?php
/**
* Toc v1.3.1
*
* This plugin automagically generates a (minified) Table of Contents
* based on special markers in the document and adds it into the
* resulting HTML document.
*
* Dual licensed under the MIT or GPL Version 3 licenses, see LICENSE.
* http://benjamin-regler.de/license/
*
* @package Toc
* @version 1.3.1
* @link <https://github.com/sommerregen/grav-plugin-external-links>
* @author Benjamin Regler <sommerregen@benjamin-regler.de>
* @copyright 2015, Benjamin Regler
* @license <http://opensource.org/licenses/MIT> MIT
* @license <http://opensource.org/licenses/GPL-3.0> GPLv3
*/
namespace Grav\Plugin;
use Grav\Common\Plugin;
use Grav\Common\Data\Data;
use Grav\Common\Page\Page;
use Grav\Plugin\Shortcodes;
use RocketTheme\Toolbox\Event\Event;
/**
* Toc
*
* This plugin automagically generates a (minified) Table of Contents
* based on special markers in the document and adds it into the
* resulting HTML document.
*/
class TocPlugin extends Plugin
{
/** ---------------------------
* Private/protected properties
* ----------------------------
*/
/**
* Instance of Toc class
*
* @var object
*/
protected $toc;
/** -------------
* Public methods
* --------------
*/
/**
* Return a list of subscribed events.
*
* @return array The list of events of the plugin of the form
* 'name' => ['method_name', priority].
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
];
}
/**
* Initialize configuration
*/
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
$this->active = false;
return;
}
if ($this->config->get('plugins.toc.enabled')) {
$this->enable([
// Page
'onPageContentProcessed' => ['onPageContentProcessed', 0],
// Twig
'onTwigInitialized' => ['onTwigInitialized', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
// Shortcodes
'onShortcodesInitialized' => ['onShortcodesInitialized', 0]
]);
}
}
/**
* Apply TOC filter to content, when each page has not been
* cached yet.
*
* @param Event $event The event when 'onPageContentProcessed' was
* fired.
*/
public function onPageContentProcessed(Event $event)
{
/** @var Page $page */
$page = $event['page'];
$config = $this->mergeConfig($page);
if ($config->get('enabled') && $config->get('process')) {
// Get content, apply TocFilter and save modified page content
$content = $page->getRawContent();
$page->setRawContent(
$this->tocFilter($content, $config->toArray(), $page)
);
}
}
/**
* Initialize Twig configuration and filters.
*/
public function onTwigInitialized()
{
// Expose tocFilter
$this->grav['twig']->twig()->addFilter(
new \Twig_SimpleFilter('toc', [$this, 'tocFilter'], ['is_safe' => ['html']])
);
}
/**
* Add current directory to Twig lookup paths.
*/
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
/**
* Set needed variables to display a table of contents.
*/
public function onTwigSiteVariables() {
if ($this->config->get('plugins.toc.built_in_css')) {
$this->grav['assets']->add('plugin://toc/assets/css/toc.css');
}
}
/**
* Filter to automatically create a (minified) table of contents.
*
* @param string $content The content to be filtered
* @param array $options Array of options for the TOC filter
*
* @return string The content with inserted anchor- and
* permalinks in headings and table of contents
* blocks
*/
public function tocFilter($content, $params = [])
{
// Get custom user configuration
$page = func_num_args() > 2 ? func_get_arg(2) : $this->grav['page'];
$config = $this->mergeConfig($page, true, $params);
// Render Toc
return $this->init()->render($content, $config, $page);
}
/**
* Register {{% toc %}}, {{% minitoc %}} and {{% tocify %}} shortcodes.
*
* @param Event $event An event object.
*/
public function onShortcodesInitialized(Event $event)
{
$toc = $this->init();
$function = function($event) {
// Update header to bypass evaluation
if (isset($event['page']->header()->toc->enabled)) {
$event['page']->header()->toc->enabled = true;
}
return '['.strtoupper($event['tag']).']';
};
$shortcodes = [
new Shortcodes\InlineShortcode('toc', $function),
new Shortcodes\InlineShortcode('minitoc', $function),
// new Shortcodes\BlockShortcode('tocify', [$toc, 'tocifyShortcode'])
];
// Register {{% toc %}}, {{% minitoc %}} and {{% tocify %}} shortcode
$event['shortcodes']->register($shortcodes);
}
/** -------------------------------
* Private/protected helper methods
* --------------------------------
*/
/**
* Initialize plugin and all dependencies.
*
* @return \Grav\Plugin\ExternalLinks Returns ExternalLinks instance.
*/
protected function init()
{
if (!$this->toc) {
// Initialize Toc class
require_once(__DIR__ . '/classes/Toc.php');
$this->toc = new Toc();
}
return $this->toc;
}
}