1<?php
2/**
3 @brief   DokuPlusPlus Plugin: inserts automatic numbering where required
4 @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
5 @author  Luis Machuca Bezzaza
6 */
7// must be run within Dokuwiki
8if(!defined('DOKU_INC')) die();
9if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
10require_once(DOKU_PLUGIN.'syntax.php');
11
12class syntax_plugin_dokupp extends DokuWiki_Syntax_Plugin {
13
14    function getType() { return 'substition'; }
15    function getSort() { return 49; }
16    function connectTo($mode) {
17        // setup formatting
18        $this->Lexer->addSpecialPattern('@#:%[0-9]*u@', $mode, 'plugin_dokupp');
19        // select counter
20        $this->Lexer->addSpecialPattern('@#[a-z]*=\d*@', $mode, 'plugin_dokupp');
21        // use current counter
22        $this->Lexer->addSpecialPattern('@#@', $mode, 'plugin_dokupp');
23    }
24
25    function handle($match, $state, $pos, &$handler) {
26        $match = substr($match, 1, -1); // strip markup
27        static $format= '%u';
28        if ('#' === $match) return array('N', $format); //< use and increment current counter
29        else if (preg_match('/#[a-z]*=\d*/', $match)) { //< select counter, set amount
30            //$match= explode('=', $match);
31            return explode('=', $match);
32        }
33        else {
34            list ($match, $format) = explode(':', $match);
35            return array(null, $format);
36        }
37        return null; // we shouldn't reach here!
38    }
39
40    function render($mode, &$renderer, $data) {
41        global $ID;
42        global $INFO;
43        global $conf;
44        static $N= array('_0' => 0);
45        static $S= '';
46        list ($match, $arg) = $data;
47        if ('xhtml' === $mode) {
48            if ('N' === $match) {
49                // usage of counter
50                $renderer->doc .= '<span class="dokupp-counter">';
51                $renderer->doc .=sprintf($arg, $N[$S]);
52                $renderer->doc .= '</span>';
53                ++$N[$S];
54            } else if ($match === null) {
55                // nothing to do, taken care by $format in handler
56            } else {
57                // $match has something, should be the name of a counter, $data has the amount
58                if ($match==='') $match= '_0'; // set default counter if no name given
59                if ($arg==='') $arg= $N[$match]; // preserve amount if no amount given
60                $S= $match;
61                $N[$S] = intval($arg);
62            }
63            return true;
64        }
65        return false;
66
67    }
68}
69