1<?php
2/**
3 * Creole Plugin, header component: Creole style headers
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Esther Brunner <wikidesign@gmail.com>
7 */
8
9/**
10 * All DokuWiki plugins to extend the parser/rendering mechanism
11 * need to inherit from this class
12 */
13class syntax_plugin_creole_header extends DokuWiki_Syntax_Plugin {
14    var $eventhandler = NULL;
15
16    function getType() { return 'container'; }
17    function getPType() { return 'block'; }
18    function getSort() { return 49; }
19
20    function getAllowedTypes() {
21        return array('formatting', 'substition', 'disabled', 'protected');
22    }
23
24    function preConnect() {
25        $this->Lexer->addSpecialPattern(
26                '(?m)^[ \t]*=+[^\n]+=*[ \t]*$',
27                'base',
28                'plugin_creole_header'
29                );
30        $this->eventhandler = plugin_load('helper', 'creole_eventhandler');
31    }
32
33    /**
34     * Constructor.
35     */
36    public function __construct() {
37        $this->eventhandler = plugin_load('helper', 'creole_eventhandler');
38    }
39
40    function handle($match, $state, $pos, Doku_Handler $handler) {
41        global $conf;
42
43        // get level and title
44        $title = trim($match);
45        if (($this->getConf('precedence') == 'dokuwiki')
46                && ($title[strlen($title) - 1] == '=')) { // DokuWiki
47            $level = 7 - strspn($title, '=');
48        } else {                                   // Creole
49            $level = strspn($title, '=');
50        }
51        if ($level < 1) $level = 1;
52        elseif ($level > 5) $level = 5;
53        $title = trim($title, '=');
54        $title = trim($title);
55
56        $this->eventhandler->notifyEvent('insert', 'header', 'header', $pos, $match, $handler);
57
58        if ($handler->getStatus('section')) $handler->addCall('section_close', array(), $pos);
59
60        $handler->addCall('header', array($title, $level, $pos), $pos);
61
62        $handler->addCall('section_open', array($level), $pos);
63        $handler->setStatus('section', true) ;
64        return true;
65    }
66
67    function render($mode, Doku_Renderer $renderer, $data) {
68        return true;
69    }
70}
71// vim:ts=4:sw=4:et:enc=utf-8:
72