forked from sourcesoldier/grav-plugin-tidyhtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtidyhtml.php
76 lines (67 loc) · 2.27 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
<?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
{
/** -------------
* 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()
{
$originOutput = $this->grav->output;
$config = array(
'indent' => $this->config->get('plugins.tidyhtml.indent'),
'indent-spaces' => $this->config->get('plugins.tidyhtml.indent_spaces'),
'wrap' => $this->config->get('plugins.tidyhtml.wrap'),
'hide-comments' => $this->config->get('plugins.tidyhtml.hide_comments'),
'new-blocklevel-tags' => implode(' ', $this->config->get('plugins.tidyhtml.blocklevel_tags')),
'new-empty-tags' => implode(' ', $this->config->get('plugins.tidyhtml.empty_tags')),
'new-inline-tags' => implode(' ', $this->config->get('plugins.tidyhtml.inline_tags')),
'newline' => 'LF',
);
/** @var tidy $tidy */
$tidy = tidy_parse_string($originOutput, $config, 'UTF8');
$tidy->cleanRepair();
$this->grav->output = $tidy;
}
}