register_hook('RENDERER_CONTENT_POSTPROCESS', 'AFTER', $this, 'handle_renderer_content_postprocess', array()); $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handle_tpl_metaheader_output', array()); } /** * Replace our placeholder with the actual toc content */ function handle_renderer_content_postprocess(&$event, $param) { global $TOC; if ($TOC) { dbglog($event->data[1], 'html'); $KEY_WORD = ''; // find first toc entry $pos = strpos($event->data[1], $KEY_WORD); $pos_end = strpos($event->data[1], $KEY_WORD_END, $pos); // as long tocs found while ($pos > 0) { $TOC2=$TOC; // get arguments for toc $begin_level = -1; $end_level = -1; $autons = 0; $namespace = ''; $match = substr($event->data[1], $pos, ($pos_end - $pos)); $spacer = strpos($match, '>'); $param_ans = 'ans:'; // param autonamespace $param_ns = 'ns:'; // param namespace $param_bl = 'bl:'; // param begin level $param_el = 'el:'; // param end level if ($spacer > 0 ) // check if contains parmeters { $param_string = preg_split('/>/u', $match, 2); $params = preg_split('/ /u', $param_string[1]); foreach ($params as &$param) { dbglog("param: $param"); if ($param_ans === substr($param, 0, strlen($param_ans))) // check if should use auto namespace { $autons = intval(substr($param, strlen($param_ans))); } else if ($param_ns === substr($param, 0, strlen($param_ns))) // check if should use namespace { $namespace = substr($param, strlen($param_ns)); } else if ($param_bl === substr($param, 0, strlen($param_bl))) // check if should use begin level { $begin_level = intval(substr($param, strlen($param_bl))); } else if ($param_el === substr($param, 0, strlen($param_el))) // check if should use end level { $end_level = intval(substr($param, strlen($param_el))); } } dbglog("bl: $begin_level"); dbglog("el: $end_level"); dbglog("ans: $autons"); dbglog("namespace: $namespace"); } $head_level = 0; $head_id = ''; // check if we use the auto head namepsace function if ($autons == 1) { // search for previours head $head = strrpos($event->data[1], 'data[1]) - $pos)); // check if head found if ($head) { // find id $head_id_key_start = strpos($event->data[1], 'id="', $head) + 4; $head_id_key_fin = strpos($event->data[1], '"', $head_id_key_start); $head_id = substr($event->data[1], $head_id_key_start , $head_id_key_fin - $head_id_key_start); dbglog("found head pos: name: $head_id"); } } else { $head_id = $namespace; } // check if head namespace selected if ($head_id == '') { $in_section = true; } else { $in_section = false; } foreach ($TOC2 as &$element) { $element_level = intval($element['level']); $element_id = substr($element['link'], 1); dbglog("element: $element_level $element_id"); // check if its the head if ($element_id == $head_id) { $in_section = true; $head_level = $element_level; unset($TOC2[array_search($element, $TOC2)]); } else { // check if outside of head level if ($element_level <= $head_level) { $in_section = false; } // filter begin level if ($element_level < $head_level + $begin_level) { unset($TOC2[array_search($element, $TOC2)]); } // filter end level relative to head level if ($end_level > 0 and $element_level > $end_level + $head_level) { unset($TOC2[array_search($element, $TOC2)]); } } if (!$in_section) { unset($TOC2[array_search($element, $TOC2)]); } else { //modify level to begin on top $element['level']= $element['level'] - $head_level - 1; } } // remove unselected $TOC2 = array_values($TOC2); // build html toc $html = '
' . html_buildlist($TOC2, 'inlinetoc2', 'html_list_inlinetoc2') . '
'; // replace placeholder $event->data[1] = substr_replace($event->data[1], $html, $pos, ($pos_end - $pos) + strlen($KEY_WORD_END)); // get next pos $pos = strpos($event->data[1], $KEY_WORD, $pos + 1); $pos_end = strpos($event->data[1], $KEY_WORD_END, $pos); } } } /** * Include javascript */ function handle_tpl_metaheader_output(&$event, $param) { $event->data["script"][] = array ( "type" => "text/javascript", "src" => DOKU_BASE . "lib/plugins/inlinetoc/inlinetoc.js", "_data" => "", ); } } /** * Callback for html_buildlist. * Builds list items with inlinetoc2 printable class instead of dokuwiki's toc class which isn't printable. */ function html_list_inlinetoc2($item){ if(isset($item['hid'])){ $link = '#'.$item['hid']; }else{ $link = $item['link']; } return ''. hsc($item['title']).''; }