-
Notifications
You must be signed in to change notification settings - Fork 4
/
tidyhtml.php
111 lines (96 loc) · 3.12 KB
/
tidyhtml.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
<?php
namespace Grav\Plugin;
use \Grav\Common\Plugin;
/**
* Tidyhtml GRAV plugin
*
* @package Grav\Plugin
*
* @author Clemens Queissner <clemens.queissner@cq-design.de> @sourcesoldier
* @since 2015-11-07
*/
class TidyhtmlPlugin extends Plugin
{
const PLUGIN_CONFIG_PATH = 'plugins.tidyhtml';
/** -------------
* 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 and checking for presence of tidy
*/
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
$this->active = false;
return;
}
if(extension_loaded('tidy')) {
$this->enable([
'onOutputGenerated' => ['onOutputGenerated', 0]
]);
}
}
/**
* Retrieves the actual output intend and parses it to tidyPHP for cleanup
* cleaned up content the gets set to the grav context again.
*/
public function onOutputGenerated()
{
if($this->skipCurrentSite($this->grav['uri']->path())) {
return;
}
$originOutput = $this->grav->output;
$config = array(
'indent' => $this->_getConfigSetting('indent'),
'drop-empty-elements' => $this->_getConfigSetting('drop_empty'),
'indent-spaces' => $this->_getConfigSetting('indent_spaces'),
'wrap' => $this->_getConfigSetting('wrap'),
'hide-comments' => $this->_getConfigSetting('hide_comments'),
'new-blocklevel-tags' => implode(' ', $this->_getConfigSetting('blocklevel_tags')),
'new-empty-tags' => implode(' ', $this->_getConfigSetting('empty_tags')),
'new-inline-tags' => implode(' ', $this->_getConfigSetting('inline_tags')),
'newline' => 'LF',
);
/** @var tidy $tidy */
$tidy = tidy_parse_string($originOutput, $config, 'UTF8');
if ($this->_getConfigSetting('repair') === true) {
$tidy->cleanRepair();
}
$this->grav->output = $tidy;
}
/**
* Checks if the passed url path ist configure to be ignored
*
* @param string $path
* @return bool
*/
public function skipCurrentSite($path) {
$ignoredSites = (array) $this->_getConfigSetting('pages');
if(in_array($path, $ignoredSites)) {
return true;
}
return false;
}
/**
* Gets values for a specific config node of the plugin
*
* @param string $node
* @return mixed
*/
protected function _getConfigSetting($node) {
return $this->config->get(self::PLUGIN_CONFIG_PATH . '.' . $node);
}
}