/
action.php
66 lines (58 loc) · 1.97 KB
/
action.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
<?php
/**
* hiddenheader Plugin for DokuWiki / action.php
*
* This would be easier and more performant as a syntax plugin, but .
* See the
*
*
* @author Eli Fenton
*/
if (!defined('DOKU_INC')) {die();}
if (!defined('DOKU_PLUGIN')) {define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');}
require_once DOKU_PLUGIN . 'action.php';
class action_plugin_hiddenheader extends DokuWiki_Action_Plugin {
function register(Doku_Event_Handler $controller) {
$controller->register_hook('PARSER_WIKITEXT_PREPROCESS', 'AFTER', $this, 'handlePreprocess');
$controller->register_hook('RENDERER_CONTENT_POSTPROCESS', 'BEFORE', $this, 'handlePostProcess');
$controller->register_hook('TPL_TOC_RENDER', 'BEFORE', $this, 'handleToc');
}
function handlePreprocess(&$event, $param) {
// should accumulate in hash
if ($this->hidden) {
return;
}
preg_match_all('/==+\%hide\s*([^=]+)/', $event->data, $m);
$this->hidden = $m && count($m[1]) > 0 ? array_map('trim', $m[1]) : null;
if ($this->hidden) {
$event->data = preg_replace('/(==+)\%hide/', '$1', $event->data);
}
}
//TODO Bug: If you use plugin:include to inlcude a page with hidden headers, this processing doesn't happen.
// The only way to fix this is to use a syntax plugin, or do it before save
function handlePostProcess(&$event, $param) {
if ($this->hidden)
foreach ($this->hidden as $h) {
$event->data[1] = preg_replace('/(<h\d[^>]*)(>(?:<a name[^>]*>|)' . trim($h) . '(?:<\/a>|)<\/h\d>)/', '$1 style="display:none"$2', $event->data[1]);
}
}
function handleToc(&$event, $param) {
if ($this->hidden) {
$map = array();
foreach ($this->hidden as $h) {
$map[trim($h)] = 1;
}
// Remove all hidden headers from the TOC.
$newdata = array();
foreach ($event->data as $d) {
if (!$map[$d['hid']]) {
$newdata[] = $d;
}
}
global $conf;
$minItemsToShow = $conf['tocminheads'] ?: 0;
$event->data = count($newdata) < $minItemsToShow ? array() : $newdata;
}
}
var $hidden;
}