1<?php
2/**
3 * InToc-Plugin: Renders the page's toc inside the page content
4 * Reworked form broken https://www.dokuwiki.org/plugin:inlinetoc
5 *
6 * @license GPL v2 (http://www.gnu.org/licenses/gpl.html)
7 * @author  Vincent Tscherter and previous
8 */
9
10use dokuwiki\Extention\ActionPlugin;
11
12class action_plugin_intoc extends DokuWiki_Action_Plugin {
13
14    /**
15     * Register event handlers
16     */
17    function register(Doku_Event_Handler $controller) {
18        $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'handle_act_render', array());
19        $controller->register_hook('RENDERER_CONTENT_POSTPROCESS', 'AFTER', $this, 'handle_renderer_content_postprocess', array());
20    }
21
22	/**
23	 * Make sure the other toc is not printed
24	 */
25    function handle_act_render(&$event, $param) {
26        global $ID;
27        global $INFO;
28        if (p_get_metadata($ID, 'movetoc')) {
29            $INFO['prependTOC'] = false;
30        }
31    }
32
33    /**
34     * Replace our placeholder with the actual toc content
35     */
36    function handle_renderer_content_postprocess(&$event, $param) {
37        global $TOC;
38        global $lang;
39        if ($TOC) {
40            $html = '<details open class="intoc"><summary>'.$lang['toc'].'</summary>'
41                . html_buildlist($TOC, 'intoc', array($this, 'html_list_intoc'))
42                . '</details>';
43            $event->data[1] = str_replace('<!-- INTOCTARGET -->',
44                                          $html,
45                                          $event->data[1]);
46        }
47    }
48
49
50	/**
51	 * Callback for html_buildlist.
52	 * Builds list items with intoc printable class instead of dokuwiki's toc class which isn't printable.
53	 */
54	function html_list_intoc($item){
55	    if(isset($item['hid'])){
56	        $link = '#'.$item['hid'];
57	    }else{
58	        $link = $item['link'];
59	    }
60	    return '<span class="li"><a href="'.$link.'" class="intoc">'. hsc($item['title']).'</a></span>';
61	}
62}