DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:numberedheadings

This is an old revision of the document!


Plugin Numbered Headings

Compatible with DokuWiki

2013-06-03, 2009-12-25, 2014-09-29 "Hrun", 2015-08-10 "Detritus"

plugin Adds numbered headings to DokuWiki without changing the actual behavior of the standard headings.

Last updated on
2010-05-12
Provides
Syntax
Conflicts with
ckgedit, htag, include

This extension has not been updated in over 2 years. It may no longer be maintained or supported and may have compatibility issues.

Tagged with headings, style

Installation

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)

Usage

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

Configuration

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.

Code

This is the actual plugin-code.

lib/plugins/numberedheadings/syntax.php

syntax.php
<?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, Doku_Handler $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, Doku_Renderer $renderer, $data) {
        //do nothing (already done by original render-method)
    }
}
//Setup VIM: ex: et ts=4 enc=utf-8 :

Juergen_aus_Zuendorf 2018-02-06 16:40
→ Changes for compatibility to PHP7:
“&$handler” and “&$renderer” to “Doku_Handler $handler” and “Doku_Renderer $renderer”

lib/plugins/numberedheadings/conf/default.php

default.php
<?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

lib/plugins/numberedheadings/conf/metadata.php

metadata.php
<?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');

lib/plugins/numberedheadings/lang/de/settings.php

settings.php
<?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.)';

lib/plugins/numberedheadings/lang/en/settings.php

settings.php
<?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.)';

Revision List

  • 2006-08-25 — Published Plugin
  • 2008-09-04 — Added startlevel per-page setting (mat)
  • 2010-05-11 — Added the availability to show a tailingdot
  • 2010-05-12 — Brought startlevel and tailingdot out to configuration (refactored the code)

Discussion

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 ===

Martin Klima

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


Suggestion / Request

Great plugin - Can I make a request to:
- Include an option by wiki, to do this automatically (without the hyphens) for all pages
- Disable option by page, when enabled at wiki level
- Enable the option by page by a setting at the top of the page

Thanks!

I just developed this feature for my own needs. The code is available for download here. Feedbacks are welcome. So this version adds:

  • an option to enable numbering by default on every page
  • the possibility to use {{enable_numbering}} or {{disable_numbering}}

Frusterick Manners: frustrating problem

It is so nice, but:

Warning: Declaration of syntax_plugin_numberedheadings::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /www/htdocs/wxxxxx/xxxxx.de/dokuwiki/lib/plugins/numberedheadings/syntax.php on line 130

Warning: Declaration of syntax_plugin_numberedheadings::render($format, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /www/htdocs/wxxxxx/xxxxx.de/dokuwiki/lib/plugins/numberedheadings/syntax.php on line 130

Warning: Cannot modify header information - headers already sent by (output started at /www/htdocs/wxxxxx/xxxxx.de/dokuwiki/lib/plugins/numberedheadings/syntax.php:130) in /www/htdocs/wxxxxx/xxxxx.de/dokuwiki/inc/actions.php on line 210

Warning: Cannot modify header information - headers already sent by (output started at /www/htdocs/wxxxxx/xxxxx.de/dokuwiki/lib/plugins/numberedheadings/syntax.php:130) in /www/htdocs/wxxxxx/xxxxx.de/dokuwiki/lib/tpl/dokuwiki/main.php on line 12

Only with the downloaded version are these problems, the version of the syntax.php shown here seems to be updated and ok: Doku_Handler $handler; Doku_Renderer $renderer; are the trouble preventers.

1)
As I use a sidebar-index showing the first heading instead of the filename, I configured the plugin to start with the numbered headings from H2 on. Otherwise the numbers would be shown in the index… but feel free to change the level to 1.
2)
{ {header>n}} will still work fine, but “startlevel” is more speaking
plugin/numberedheadings.1518958014.txt.gz · Last modified: 2018-02-18 13:46 by guenter

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