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