1<?php
2
3/**
4 * Plugin QnA: Header syntax
5 *
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author     Mykola Ostrovskyy <dwpforge@gmail.com>
8 */
9
10class syntax_plugin_qna_header extends DokuWiki_Syntax_Plugin {
11
12    private $mode;
13
14    /**
15     * Constructor
16     */
17    public function __construct() {
18        $this->mode = substr(get_class($this), 7);
19    }
20
21    /**
22     * What kind of syntax are we?
23     */
24    public function getType() {
25        return 'baseonly';
26    }
27
28    public function getPType() {
29        return 'block';
30    }
31
32    /**
33     * Where to sort in?
34     */
35    public function getSort() {
36        return 50;
37    }
38
39    /**
40     *
41     */
42    public function connectTo($mode) {
43        $this->Lexer->addSpecialPattern('[ \t]*=\?={1,4}[^=\n][^\n]+={3,}[ \t]*(?=\n)', $mode, $this->mode);
44    }
45
46    /**
47     *
48     */
49    function handle($match, $state, $pos, Doku_Handler $handler) {
50        if ($state == DOKU_LEXER_SPECIAL) {
51            $match = preg_replace('/^(\s*=)\?/', '$1=', $match);
52
53            $handler->header($match, $state, $pos);
54
55            $data = array('dummy');
56        }
57        else {
58            $data = false;
59        }
60
61        return $data;
62    }
63
64    /**
65     *
66     */
67    function render($mode, Doku_Renderer $renderer, $data) {
68        if ($mode == 'xhtml') {
69            switch ($data[0]) {
70                case 'open':
71                    $renderer->doc .= DOKU_LF . '<div class="qna-header">';
72                    break;
73
74                case 'close':
75                    $renderer->doc .= '</div>' . DOKU_LF;
76                    break;
77            }
78
79            return true;
80        }
81        elseif ($mode == 'metadata') {
82            if ($data[0] == 'open') {
83                $meta['title'] = $data[1];
84                $meta['id'] = $data[2];
85                $meta['level'] = $data[3];
86                $meta['class'] = 'header';
87
88                $renderer->meta['description']['tableofquestions'][] = $meta;
89            }
90
91            return true;
92        }
93
94        return false;
95    }
96}
97