-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathimgcaptions.php
135 lines (128 loc) · 3.67 KB
/
imgcaptions.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
<?php
/**
* ImgCaptions Plugin
*
* PHP version 7
*
* @category Extensions
* @package Grav
* @subpackage Scholar
* @author Ole Vik <git@olevik.net>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://github.com/OleVik/grav-plugin-imgcaptions
*/
namespace Grav\Plugin;
use Grav\Common\Plugin;
use Grav\Common\Utils;
use Grav\Common\Page\Page;
use Grav\Common\Page\Pages;
use Grav\Common\Twig\Twig;
use RocketTheme\Toolbox\Event\Event;
use Grav\Plugin\ImgCaptionsPlugin\API\HTML;
use Grav\Plugin\ImgCaptionsPlugin\API\Markdown;
use Grav\Plugin\ImgCaptionsPlugin\API\Source;
use Grav\Plugin\ImgCaptionsPlugin\API\Regex;
/**
* Turns the title-attribute in img-elements into figure-elements with a figcaption
*
* Class ImgCaptionsPlugin
*
* @category Extensions
* @package Grav\Plugin
* @author Ole Vik <git@olevik.net>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://github.com/OleVik/grav-plugin-imgcaptions
*/
class ImgCaptionsPlugin extends Plugin
{
/**
* Protected variables
*
* @var string $mode Grav cache setting
*/
protected $mode;
/**
* Register events with Grav
*
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
/**
* Initialize the plugin
*
* @return array
*/
public function onPluginsInitialized()
{
if ($this->isAdmin() || $this->config->get('plugins.imgcaptions.enabled') !== true) {
return;
}
if ($this->config->get('system.pages.type') == "flex") {
return;
}
$config = (array) $this->config->get('plugins.imgcaptions');
$this->mode = $config['mode'];
if (!isset($config['event'])) {
if ($this->mode == 'markdown') {
$event = 'onPageContentRaw';
} elseif ($this->mode == 'html') {
$event = 'onPageContentProcessed';
}
} else {
$event = $config['event'];
}
$this->enable(
[
$event => ['output', 0],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0]
]
);
}
/**
* Finds images in page content and rewraps as figures with figcaptions
*
* @param Event $event Instance of RocketTheme\Toolbox\Event\Event
*
* @return void
*/
public function output(Event $event)
{
$Page = $event['page'];
$config = (array) $this->config->get('plugins.imgcaptions');
$header = (array) $Page->header();
if (isset($header['imgcaptions'])) {
$config = Utils::arrayMergeRecursiveUnique(
$config,
$header['imgcaptions']
);
}
if ($config['enabled'] !== true) {
return;
}
include __DIR__ . '/vendor/autoload.php';
$Source = new Source($Page, $this->grav['pages']);
$content = $Page->getRawContent();
if ($this->mode == 'markdown') {
$Markdown = new Markdown($this->grav['twig'], $Source);
$content = $Markdown->render($content);
} elseif ($this->mode == 'html') {
$HTML = new HTML($this->grav['twig'], $Source);
$content = $HTML->render($content);
}
$Page->setRawContent($content);
}
/**
* Add current directory to twig lookup paths.
*
* @return void
*/
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
}