1<?php
2
3/**
4 * Plugin Style/Verse: More styles for dokuwiki
5 * Format: see README
6 *
7 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author     Ivan A-R <ivan@iar.spb.ru>
9 * @page       http://iar.spb.ru/en/projects/doku/styler
10 * @version    0.2
11 */
12
13/**
14 * All DokuWiki plugins to extend the parser/rendering mechanism
15 * need to inherit from this class
16 */
17class syntax_plugin_styler_verse extends DokuWiki_Syntax_Plugin
18{
19    /**
20     * Get the type of syntax this plugin defines.
21     * @return string
22     */
23    public function getType()
24    {
25        return 'protected';
26    }
27
28    /**
29     * Define how this plugin is handled regarding paragraphs.
30     * @return string
31     */
32    public function getPType()
33    {
34        return 'block';
35    }
36
37    /**
38     * Where to sort in?
39     * @return int
40     */
41    public function getSort()
42    {
43        return 205;
44    }
45
46    /**
47     * Connect lookup pattern to lexer.
48     * @param $mode String The desired rendermode.
49     */
50    public function connectTo($mode)
51    {
52        $this->Lexer->addEntryPattern('<verse.*?>(?=.*?\x3C/verse\x3E)', $mode, 'plugin_styler_verse');
53    }
54
55    /**
56     * Second pattern to say when the parser is leaving your syntax mode.
57     */
58    public function postConnect()
59    {
60        $this->Lexer->addExitPattern('</verse>', 'plugin_styler_verse');
61    }
62
63    /**
64     * Handler to prepare matched data for the rendering process.
65     * @param $match String The text matched by the patterns.
66     * @param $state Integer The lexer state for the match.
67     * @param $pos Integer The character position of the matched text.
68     * @param $handler Doku_Handler Reference to the Doku_Handler object.
69     * @return array
70     */
71    public function handle($match, $state, $pos, Doku_Handler $handler)
72    {
73        global $conf;
74        switch ($state) {
75            case DOKU_LEXER_ENTER:
76                $match = str_replace(array('<', '>'), array('', ''), $match);
77                $attrib = preg_split('/\s+/', strtolower($match));
78                if ($attrib[0]) {
79                    return array(array_shift($attrib), $state, $attrib);
80                } else {
81                    return array($match, $state, array());
82                }
83            case DOKU_LEXER_UNMATCHED:
84                return array($match, $state, array());
85            case DOKU_LEXER_EXIT:
86                return array('', $state, array());
87        }
88        return array();
89    }
90
91    /**
92     * Handle the actual output creation.
93     * @param $mode String The output format to generate.
94     * @param $renderer Doku_Renderer A reference to the renderer object.
95     * @param $data Array The data created by the handle() method.
96     * @return bool
97     */
98    public function render($mode, Doku_Renderer $renderer, $data)
99    {
100        global $st;
101        global $et;
102        global $conf;
103        global $prt;
104        if ($mode == 'xhtml') {
105            switch ($data[1]) {
106                case DOKU_LEXER_ENTER:
107                    $class = '';
108                    foreach (
109                        array(
110                            'left',
111                            'right',
112                            'center',
113                            'justify',
114                            'box',
115                            'float-left',
116                            'float-right',
117                            'background'
118                        ) as $v
119                    ) {
120                        if (in_array($v, $data[2])) {
121                            $class .= ' styler-' . $v;
122                        }
123                    }
124                    $renderer->doc .= '<div class="verse' . $class . '">' . "\n" . '<pre>';
125                    break;
126                case DOKU_LEXER_UNMATCHED:
127                    $result = preg_replace("/\b([A-H][#]?[m]?[75]?)\b/m", "<span>\\1</span>", $data[0]);
128                    $renderer->doc .= htmlspecialchars($result);
129                    break;
130                case DOKU_LEXER_EXIT:
131                    $renderer->doc .= "</pre>\n</div>";// "</p>" and "\n</p>" is hack
132                    break;
133            }
134            return true;
135        }
136        return false;
137    }
138}
139