Adds numbered headings to DokuWiki without changing the actual behavior of the standard headings.
Compatible with DokuWiki
2009-12-25
Download the plugin package and unpack it to your plugin-directory (lib/plugins/). This will create a new folder called numberedheadings.
You may use the following links (at your own risk), these links are provided for convenience to use with the DokuWiki plugin manager: numberedheadings.tar.gz (3kB) or numberedheadings.zip (4kB)
To use this plugin just add a '-' between the '=' and the actual heading:
====== - Level 1 Headline ====== ===== - Level 2 Headline ===== ==== - Level 3 Headline ==== ==== - Level 3 Headline ==== ===== - Level 2 Headline ===== ==== - Level 3 Headline ==== ...
The output should then look like this:
1 Level 1 Headline
1.1 Level 2 Headline
1.1.1 Level 3 Headline
1.1.2 Level 3 Headline
1.2 Level 2 Headline
1.2.1 Level 3 Headline
...
The user can also set the outline level with the following markup (Thanks to Martin Klima):
=== - #<number> Heading text ===
For example:
====== - #3 Level 1 Headline ====== ===== - Level 2 Headline =====
will be rendered
3 Level 1 Headline
3.1 Level 2 Headline
The plugin can be configured within the DokuWiki configuration manager available through the admin menu.
| startlevel | level to start with numbered headings | default = 21) |
|---|---|---|
| tailingdot | show tailing dot after numbers (e.g. 1.2.) | default = 0 |
The example above generates the following output when startlevel=2 & tailingdot=1:
Level 1 Headline
1. Level 2 Headline
1.1. Level 3 Headline
1.2. Level 3 Headline
2. Level 2 Headline
2.1. Level 3 Headline
...
You can also override the startlevel in a per-page basis (Thanks to mat). Just insert this code to your page:2)
{{startlevel>n}}
Where n is a number between 1 and 5, so your numbering will start in the heading level specified there.
This is the actual plugin-code.
<?php /** * Plugin Numbered Headings: Plugin to add numbered headings to DokuWiki-Syntax * * Usage: ====== - Heading Level 1====== * ===== - Heading Level 2 ===== * ===== - Heading Level 2 ===== * ... * * => 1 Heading Level 1 * 1.1 Heading Level 2 * 1.2 Heading Level 2 * ... * * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) * @author Lars J. Metz <dokuwiki@meistermetz.de> */ if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); require_once(DOKU_PLUGIN.'syntax.php'); class syntax_plugin_numberedheadings extends DokuWiki_Syntax_Plugin { // is now set in configuration manager var $startlevel = 0; // level to start with numbered headings (default = 2) var $tailingdot = 0; // show a tailing dot after numbers (default = 0) var $levels = array( '======'=>1, '====='=>2, '===='=>3, '==='=>4, '=='=>5); var $headingCount = array( 1=>0, 2=>0, 3=>0, 4=>0, 5=>0); function syntax_plugin_numberedheadings() { $this->startlevel = $this->getConf('startlevel'); $this->tailingdot = $this->getConf('tailingdot'); } function getInfo(){ return array( 'author' => 'Lars J. Metz', 'email' => 'dokuwiki@meistermetz.de', 'date' => '2010-05-12', 'name' => 'Plugin: Numbered Headings', 'desc' => 'Adds numbered headings to DokuWiki', 'url' => 'http://www.dokuwiki.org/plugin:NumberedHeadings'); } function getType(){ return 'substition'; } function connectTo($mode) { $this->Lexer->addSpecialPattern( '{{header>[1-5]}}', $mode, 'plugin_numberedheadings'); // added new parameter (matches the parameter name for better recognition) $this->Lexer->addSpecialPattern( '{{startlevel>[1-5]}}', $mode, 'plugin_numberedheadings'); $this->Lexer->addSpecialPattern( '^[ \t]*={2,6}\s?\-[^\n]+={2,6}[ \t]*(?=\n)', $mode, 'plugin_numberedheadings'); } function getSort() { return 45; } function handle($match, $state, $pos, &$handler){ // obtain the startlevel from the page if defined if (preg_match('/{{[a-z]{6,10}>([1-5]+)}}/', $match, $startlevel)) { $this->startlevel = $startlevel[1]; return true; } // define the level of the heading preg_match('/(={2,})/', $match, $heading); $level = $this->levels[$heading[1]]; // obtain the startnumber if defined if (preg_match('/#([0-9]+)\s/', $match, $startnumber) && ($startnumber[1]) > 0) { $this->headingCount[$level] = $startnumber[1]; //delete the startnumber-setting markup from string $match = preg_replace('/#[0-9]+\s/', ' ', $match); } else { // increment the number of the heading $this->headingCount[$level]++; } // build the actual number $headingNumber = ''; for ($i=$this->startlevel;$i<=5;$i++) { // reset the number of the subheadings if ($i>$level) { $this->headingCount[$i] = 0; } // build the number of the heading $headingNumber .= ($this->headingCount[$i]!=0) ? $this->headingCount[$i].'.' : ''; } // delete the tailing dot if wished (default) $headingNumber = ($this->tailingdot) ? $headingNumber : substr($headingNumber,0,-1); // insert the number... $match = preg_replace('/(={2,}\s?)\-/', '${1}'.$headingNumber, $match); // ... and return to original behavior $handler->header($match, $state, $pos); return true; } function render($format, &$renderer, $data) { //do nothing (already done by original render-method) } } //Setup VIM: ex: et ts=4 enc=utf-8 :
<?php /** * Options for the NumberedHeading Plugin * * @author Lars J. Metz <dokuwiki@meistermetz.de> */ $conf['startlevel'] = 2; // level to start with numbered headings $conf['tailingdot'] = 0; // show a tailing dot after the numbers
<?php /** * Metadata for the NumberedHeading Plugin * * @author Lars J. Metz <dokuwiki@meistermetz.de> */ $meta['startlevel'] = array('multichoice', '_choices' => array(1, 2 ,3, 4, 5)); $meta['tailingdot'] = array('onoff');
<?php /** * German language file * * @author Lars J. Metz <dokuwiki@meistermetz.de> */ // for the configuration manager $lang['startlevel'] = 'Nummerierung der Überschriften ab Level beginnen'; $lang['tailingdot'] = 'Punkt nach letzter Zahl anzeigen (z.B. 1.2.)';
<?php /** * English language file * * @author <dokuwiki@meistermetz.de> */ // for the configuration manager $lang['startlevel'] = 'level to start with numbered headings'; $lang['tailingdot'] = 'show tailing dot after numbers (e.g. 1.2.)';
startlevel and tailingdot out to configuration (refactored the code)Feel free to make any comments regarding my plugin… or write me an email (Lars J. Metz).
This is so basic, it should be in the basic installation of DokuWiki IMHO… diftong@gmx.net
Would be good to bring the start level out to configuration.
Done. — Lars J. Metz 2010/05/12 17:50
Could a similar system be used to format the numbered lists in nested decimal style instead of the crazy roman numerals and stuff? Better with just changing the css maybe? If so any ideas how best to do that? Armin.
Great thing, just what I was looking for, there is just one issue in combination with page-includes (include), they are not renumbered correctly, for each include page they start with 1. The same for headlines in the original page. 2006-09-19 23:31 Dirk 'el loco' Haage
I'm currently working on a fix, so that included pages are numbered correctly. I'll release it soon… 2006-09-27 09:45 Lars J. Metz
Unfortunately I couldn't do anything here, but it works fine with page-includes (include) at the moment Lars J. Metz 2010/05/12 17:51
Hi, I'm having a hard time making DokuWiki to view the saved files after the plugin is installed. When I click save, everything dissapears. I have to load from the startpage again, because the reload doesn't work (postdata). Do you have any idea why this plugin does this? If I remove the plugin DokuWiki works well 2006-11-17 13:20 Robert Popa
I saw this same behavior when I added Debian backports, and got DokuWiki-0.0.20061106-1~bpo.1 It looks like the fix is to remove the trailing?>(last line) from the script above. That fixed it for me, and matches the syntax.php for all the other plugins I have installed. 2006-11-21 11:51 Rob Walsh
For your information: I just wrote a different approach for numbered headings (per CSS). — Anika Henke 2008-01-06 16:02
Per page start level
I've just merged the code for adding the ability to set the startlevel in a per-page basis. Just added the first line of connectTo and the first if in handle.
Also, added the instructions for using.
— mat, 04-09-08
Thanks — Lars J. Metz 2010/05/12 18:08
I have added the ability for the user to set the outline level with the following markup:
=== - #<number> Heading text ===
For example:
=== - #3 Heading level 1 === == - Heading level 2 ===
will be rendered
=== 3 Heading level 1 === == 3.1 Heading level 2 ===
Added it in the Usage-Section. Thanks — Lars J. Metz 2010/05/12 18:08
Some modification. For example:
=== Chapter - Heading level 1 ===
will be rendered
=== Chapter 1 Heading level 1 ===
And
=== Chapter -#2 Heading level 1 ===
will be rendered
=== Chapter 2 Heading level 1 ===
Just replace some regular expressions:
//line 68: $this->Lexer->addSpecialPattern( '^[ \t]*={2,6}[^\n\-]*\-[^\n]+={2,6}[ \t]*(?=\n)', //line 90 to line 94: if (preg_match('/#([0-9]+)/', $match, $startnumber) && ($startnumber[1]) > 0) { $this->headingCount[$level] = $startnumber[1]; //delete the startnumber-setting markup from string $match = preg_replace('/#[0-9]+/', '', $match); //line 119: $match = preg_replace('/(={2,}[^\-]*)\-/', '${1}'.$headingNumber, $match);
But this will make '-' unusable in headings. To avoid the problem, change like this
//line 68: $this->Lexer->addSpecialPattern( '^[ \t]*={2,6}[^\n\-]*\s{1}\-\s{1}[^\n]+={2,6}[ \t]*(?=\n)', //line 90 to line 94 and line 119: same as above
Now,
=== SST-2003 ===
will be still
=== SST-2003 ===
and
=== SST - 2003 ===
will be
=== SST 1 2003 ===
— lainme 2010/06/20 03:24
Seems like the changes in Rincewind metadata handling means that the numbering sometimes doesn't start from 0 anymore. Any plans to update for the official new release? - Senorandy 2011/05/03 11:01
Fantastic plugin! I was wondering if anyone knows what I would need to modify to add tags like
<span></span>
around the heading numbers. This would allow them to be differently styled, positioned, and even do cool things like section numbering outside the margins of printed documents (what I'm trying to do.)
I have tried messing with the code myself, but any html I add shows as text, and not interpreted as real html code. Could anyone figure this out? - kououken 2012/02/07 14:32