DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:inlinetoc

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
plugin:inlinetoc [2013-07-27 22:38] – [inlinetoc.js jQuery version for Adora Belle and Weatherwax] Andreoneplugin:inlinetoc [2018-06-04 22:40] (current) – [Installation] Klap-in
Line 1: Line 1:
-====== inlinetoc Plugin ======+====== InlineTOC Plugin ======
  
 ---- plugin ---- ---- plugin ----
-description: Renders the toc of a page inside the page content, a la Mediawiki+description: Renders the toc of a page inside the page content, like Mediawiki
 author     : Andreone author     : Andreone
 email      : nfauvet@free.fr email      : nfauvet@free.fr
 type       : syntax, action type       : syntax, action
-lastupdate : 2013-07-27 +lastupdate : 2015-06-19 
-compatible : 2010-11-07, 2011-05-25, Angua, Adora Belle, Weatherwax+compatible : 2010-11-07, 2011-05-25, Angua, Adora Belle, Weatherwax, Hrun
 depends    :  depends    : 
 conflicts  : conflicts  :
-similar    : toc+similar    : toc,tocselect
 tags       : toc, mediawiki tags       : toc, mediawiki
  
-downloadurl: http://github.com/andreone/dokuwiki_inlinetoc/zipball/master +downloadurl: https://github.com/andreone/dokuwiki_inlinetoc/zipball/master 
-bugtracker : http://github.com/andreone/dokuwiki_inlinetoc/issues +bugtracker : https://github.com/andreone/dokuwiki_inlinetoc/issues 
-sourcerepo : http://github.com/andreone/dokuwiki_inlinetoc/+sourcerepo : https://github.com/andreone/dokuwiki_inlinetoc/
 donationurl:  donationurl: 
 ---- ----
Line 21: Line 21:
 ===== Installation ===== ===== Installation =====
  
-Install the plugin using the [[plugin:plugin|Plugin Manager]] and the download URL above, which points to latest version of the plugin. Refer to [[:Plugins]] on how to install plugins manually.+Search and install the plugin using the [[plugin:extension|Extension Manager]]. Refer to [[:Plugins]] on how to install plugins manually.
  
 ===== Examples/Usage ===== ===== Examples/Usage =====
Line 29: Line 29:
 Sample result: Sample result:
  
-{{https://github.com/downloads/Andreone/dokuwiki_inlinetoc/sample.png?1000|Click to view full size}}+{{https://github.com/downloads/Andreone/dokuwiki_inlinetoc/sample.png|Click to view full size}}
  
 ===== Notes ===== ===== Notes =====
Line 39: Line 39:
 ===== Change Log ===== ===== Change Log =====
  
 +  * **2015-06-19**
 +    * merge pull request
 +      * Set PType to avoid an open 'p'
 +      * Use DokuWikis way to disable the original TOC
   * **2013-07-27**   * **2013-07-27**
     * fixed to work with recent versions of Dokuwiki - cf [[https://github.com/Andreone/dokuwiki_inlinetoc/issues/2|issue on github]]     * fixed to work with recent versions of Dokuwiki - cf [[https://github.com/Andreone/dokuwiki_inlinetoc/issues/2|issue on github]]
Line 50: Line 54:
   * **2011-05-30**   * **2011-05-30**
     * Initial release     * Initial release
 +
 ===== Wishes ===== ===== Wishes =====
 +
   - Can this plugin be enabled for whole wiki?\\ Not really because it's built as a replacement plugin, not a general purpose plugin. It could be adapted but I believe it would make more sense to make a completely separate plugin for that purpose. <sub>Andreone 30/12/2011</sub>\\ \\    - Can this plugin be enabled for whole wiki?\\ Not really because it's built as a replacement plugin, not a general purpose plugin. It could be adapted but I believe it would make more sense to make a completely separate plugin for that purpose. <sub>Andreone 30/12/2011</sub>\\ \\ 
   - Could it be possible to limit the displayed level of headings?\\ e.g. ''{''''{''INLINETOC 3''}''''}'' will only display level1...level3 in the TOC <sub>Joachim 11.01.2012</sub>\\ As the plugin directly use Dokuwiki's TOC, I don't think I can do that. I will think about this however. <sub>Andreone 13/01/2012</sub>   - Could it be possible to limit the displayed level of headings?\\ e.g. ''{''''{''INLINETOC 3''}''''}'' will only display level1...level3 in the TOC <sub>Joachim 11.01.2012</sub>\\ As the plugin directly use Dokuwiki's TOC, I don't think I can do that. I will think about this however. <sub>Andreone 13/01/2012</sub>
 +
 +===== Improvements =====
 +
 +This improvement allows to create multiple tocs inside of the document (different deeps and start points)
 +
 +==== Syntax ====
 +
 +<code> {{INLINETOC}} </code>
 +=> normal inlinetoc as you know
 +<code> {{INLINETOC> ns }} </code>
 +=> inlinetoc beginning at current header(namespace in document)
 +<code> {{INLINETOC> ns:specified_namespace }} </code>
 +=> inlinetoc beginning at specified header(namespace in document)
 +<code> {{INLINETOC> el:1 }} </code>
 +=> normal inlinetoc with endlevel (deep) of 1
 +<code> {{INLINETOC> ns el:1 }} </code>
 +=> inlinetoc beginning at current header(namespace in document) with endlevel (deep) of 1
 +
 +==== Code ====
 +
 +<file php action.php>
 +<?php
 +/**
 + * InlineTOC-Plugin: Renders the page's toc inside the page content
 + *
 + * @license GPL v2 (http://www.gnu.org/licenses/gpl.html)
 + * @author  Andreone
 + */
 +
 +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_inlinetoc extends DokuWiki_Action_Plugin {
 +
 +    /**
 +     * Register event handlers
 +     */
 +    function register(&$controller) {
 +        $controller->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 = '<!-- INLINETOCPLACEHOLDER ';
 + $KEY_WORD_END = '-->';
 +
 + // 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], '<h', - (strlen($event->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 = '<div id="inlinetoc2" class="inlinetoc2">' . html_buildlist($TOC2, 'inlinetoc2', 'html_list_inlinetoc2') . '</div>';
 +
 + // 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 '<span class="li"><a href="'.$link.'" class="inlinetoc2">'. hsc($item['title']).'</a></span>';
 +}
 +
 +</file>
 +
 +<file php syntax.php>
 +<?php
 +/**
 + * InlineTOC-Plugin: Renders the page's toc inside the page content
 + *
 + * @license GPL v2 (http://www.gnu.org/licenses/gpl.html)
 + * @author  Andreone
 + */
 +
 +if (!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../../') . '/');
 +require_once(DOKU_INC . 'inc/init.php');
 +if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
 +require_once(DOKU_PLUGIN . 'syntax.php');
 +
 +class syntax_plugin_inlinetoc extends DokuWiki_Syntax_Plugin {
 +
 +    /**
 +     * What kind of syntax are we?
 +     */
 +    function getType() {
 +        return 'substition';
 +    }
 +
 +    /**
 +     * Where to sort in? (took the same as for ~~NOTOC~~)
 +     */
 +    function getSort() {
 +        return 30;
 +    }
 +
 +    /**
 +     * Connect pattern to lexer
 +     */
 +    function connectTo($mode) {
 + // to keep compatible to old dokuwiki pages
 + $this->Lexer->addSpecialPattern('{{INLINETOC}}', $mode, 'plugin_inlinetoc');
 +    $this->Lexer->addSpecialPattern('{{INLINETOC>.+?}}', $mode, 'plugin_inlinetoc');
 +    }
 +
 +    /**
 +     * Handle the match
 +     */
 +
 + function handle($match, $state, $pos, &$handler) 
 + {
 +
 + $begin_level = -1; 
 + $end_level = -1;
 + $autons = false;
 + $namespace = '';
 +
 +
 + $spacer = strpos($match, '>');
 +
 + $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_ns === substr($param, 0, strlen($param_ns))) // check if should use namespace
 + {
 + dbglog("contains namespace");
 + $namespace = substr($param, strlen($param_ns) + 1);
 +
 + if ($namespace === false or $namespace === ''
 + {
 + dbglog("use autons");
 + $autons = true;
 + }
 +
 + dbglog("ns: |$namespace|");  
 + }
 + else if ($param_bl === substr($param, 0, strlen($param_bl))) // check if should use begin level 
 + {
 + dbglog("contains begin level");
 + $begin = intval(substr($param, strlen($param_bl)));
 + }
 + else if ($param_el === substr($param, 0, strlen($param_el))) // check if should use end level 
 + {
 + dbglog("contains end level");
 + $end = intval(substr($param, strlen($param_el)));
 + }
 + }
 + }
 +
 + dbglog($namespace, "namespace:");
 +        
 + return array(
 + 'begin' => $begin,
 + 'end' => $end,
 + 'autons' => $autons,
 + 'namespace' => $namespace);
 + 
 + }
 +
 +    /**
 +     * Add placeholder to cached page (will be replaced by action component)
 +     */
 + function render($mode, &$renderer, $data) 
 + {
 + dbglog($data, "render data");
 +         
 + $begin_level = $data['begin']; 
 + $end_level = $data['end'];
 + $autons = $data['autons'];
 + $namespace = $data['namespace'];
 +
 + $renderer->doc .= "<!-- INLINETOCPLACEHOLDER >  bl:$begin_level el:$end_level ans:$autons ns:$namespace -->";
 +
 + }
 +}
 +</file>
 +
 +==== About ====
 +
 +We have needed this functions in our company for internal documentation and it would be nice to see those features in your next release. 
 +
 +Best regards and thanks for your work,
 + --- [[user>hop3l3ss1990|hop3l3ss1990]] //2014-08-20 09:39//
 +
 ===== Discussion ===== ===== Discussion =====
  
Line 66: Line 445:
  
 ==== Hiding Dokuwiki TOC for Adora Belle ==== ==== Hiding Dokuwiki TOC for Adora Belle ====
 +
 Until the plugin is updated, here is what you'll have to change in order to hide the Dokuwiki TOC. Edit  inlinetoc.js: Until the plugin is updated, here is what you'll have to change in order to hide the Dokuwiki TOC. Edit  inlinetoc.js:
  
Line 77: Line 457:
  
 ==== inlinetoc.js jQuery version for Adora Belle and Weatherwax (solved) ==== ==== inlinetoc.js jQuery version for Adora Belle and Weatherwax (solved) ====
 +
 Here is a jQuery solution to make DokuWiki TOC invisible. Here is a jQuery solution to make DokuWiki TOC invisible.
  
Line 96: Line 477:
  
 >Thank you both of you for providing a working solution during my coma. -- [[user>andreone|Andreone]]// 2013/07/27 22:30// >Thank you both of you for providing a working solution during my coma. -- [[user>andreone|Andreone]]// 2013/07/27 22:30//
 +
 ==== Problem: no output on screen, INLINETOC not working? (solved) ==== ==== Problem: no output on screen, INLINETOC not working? (solved) ====
 +
 I inserted <code>{{INLINETOC}}</code> at the beginning of a page, but no toc is generated.\\  I inserted <code>{{INLINETOC}}</code> at the beginning of a page, but no toc is generated.\\ 
 Any idea would goes wrong?  <sub>//24.08.2011 Joachim//</sub> Any idea would goes wrong?  <sub>//24.08.2011 Joachim//</sub>
Line 111: Line 494:
 **not working** **not working**
 <code> <code>
 +
 ====== plugin inlinetoc ====== ====== plugin inlinetoc ======
  
Line 164: Line 548:
  
 ==== Disabling numbering (solved) ==== ==== Disabling numbering (solved) ====
-I use the [[http://www.dokuwiki.org/plugin:numberedheadings|numberedheadings plugin]] to number the headings of the pages.+ 
 +I use the [[plugin:numberedheadings|numberedheadings plugin]] to number the headings of the pages.
  
 In print output, INLINETOC also numbers the headings (first number): In print output, INLINETOC also numbers the headings (first number):
 +
 <code> <code>
 1. Header 1 1. Header 1
Line 175: Line 561:
 etc. etc.
 </code> </code>
 +
 On screen, a bullet point is shown. On screen, a bullet point is shown.
  
Line 186: Line 573:
     _margin-left: 2em;     _margin-left: 2em;
 } }
 +
 </code> </code>
 +
 +> **Solved 2015-05-14 / Skyscraper** disable list-style-type - edit file dokuwiki/lib/plugins/inlinetoc/all.css
 +<code>
 +change from 
 +
 +div.inlinetoc2 ul {
 +    list-style-type: decimal; 
 +    line-height: 1.5em;      
 +    _margin-left: 2em; 
 +}     
 +
 +to
 +
 +div.inlinetoc2 ul {
 +    list-style-type: none; 
 +    line-height: 1.5em;      
 +    _margin-left: 2em; 
 +}     
 +</code>
 +
 +==== Bootstrap3 template ====
 +<del>[[https://github.com/Andreone/dokuwiki_inlinetoc/commit/73d753d824b4627735ae9de55e1c7e7d8da00b11|The new way to disable dokuwiki TOC]] doesn't work with Bootstrap3 template. Switch to back to the previous version will do.</del> -- changed by [[user>coastGNU|coastGNU]] //2015-11-11//  
 +
 +Bootstrap3 template [[https://github.com/LotarProject/dokuwiki-template-bootstrap3/issues/115|supports inlinetoc]]
 +
 + --- [[user>coastGNU|coastGNU]] //2015-11-11 14:26//
plugin/inlinetoc.1374957531.txt.gz · Last modified: 2013-07-27 22:38 by Andreone

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki