1<?php
2
3/**
4 * Plugin Style: 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/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_styler 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 'container';
26    }
27
28    /**
29     * What kind of syntax do we allow (optional)
30     * @return string[]
31     */
32    public function getAllowedTypes()
33    {
34        return array('container', 'substition', 'protected', 'disabled', 'formatting', 'paragraphs');
35    }
36
37    /**
38     * Define how this plugin is handled regarding paragraphs.
39     * @return string
40     */
41    public function getPType()
42    {
43        return 'stack';
44    }
45
46    /**
47     * Where to sort in?
48     * @return int
49     */
50    public function getSort()
51    {
52        return 205;
53    }
54
55    /**
56     * Connect lookup pattern to lexer.
57     * @param $mode String The desired rendermode.
58     */
59    public function connectTo($mode)
60    {
61        $this->Lexer->addEntryPattern('<style.*?>(?=.*?\x3C/style\x3E)', $mode, 'plugin_styler_styler');
62        $this->Lexer->addEntryPattern('<quote.*?>(?=.*?\x3C/quote\x3E)', $mode, 'plugin_styler_styler');
63        $this->Lexer->addEntryPattern('<epigraph.*?>(?=.*?\x3C/epigraph\x3E)', $mode, 'plugin_styler_styler');
64    }
65
66    /**
67     * Second pattern to say when the parser is leaving your syntax mode.
68     */
69    public function postConnect()
70    {
71        $this->Lexer->addExitPattern('</style>', 'plugin_styler_styler');
72        $this->Lexer->addExitPattern('</quote>', 'plugin_styler_styler');
73        $this->Lexer->addExitPattern('</epigraph>', 'plugin_styler_styler');
74    }
75
76
77    /**
78     * Handler to prepare matched data for the rendering process.
79     * @param $match String The text matched by the patterns.
80     * @param $state Integer The lexer state for the match.
81     * @param $pos Integer The character position of the matched text.
82     * @param $handler Doku_Handler  Reference to the Doku_Handler object.
83     * @return array
84     */
85    public function handle($match, $state, $pos, Doku_Handler $handler)
86    {
87        global $conf;
88        switch ($state) {
89            case DOKU_LEXER_ENTER:
90                $match = str_replace(array('<', '>'), array('', ''), $match);
91                $attrib = preg_split('/\s+/', strtolower($match));
92                if ($attrib[0]) {
93                    return array(array_shift($attrib), $state, $attrib);
94                } else {
95                    return array($match, $state, array());
96                }
97            case DOKU_LEXER_UNMATCHED:
98                return array($match, $state, array());
99            case DOKU_LEXER_EXIT:
100                return array('', $state, array());
101        }
102        return array();
103    }
104
105    /**
106     * Handle the actual output creation.
107     * @param $mode String The output format to generate.
108     * @param $renderer Doku_Renderer A reference to the renderer object.
109     * @param $data Array The data created by the handle() method.
110     * @return bool
111     */
112    public function render($mode, Doku_Renderer $renderer, $data)
113    {
114        global $st;
115        global $et;
116        global $conf;
117        global $prt;
118        if ($mode == 'xhtml') {
119            switch ($data[1]) {
120                case DOKU_LEXER_ENTER:
121                    $class = '';
122                    foreach (
123                        array(
124                            'left',
125                            'right',
126                            'center',
127                            'justify',
128                            'box',
129                            'float-left',
130                            'float-right',
131                            'background'
132                        ) as $v
133                    ) {
134                        if (in_array($v, $data[2])) {
135                            $class .= ' styler-' . $v;
136                        }
137                    }
138                    $renderer->doc .= "</p>\n"; // It is hack
139                    if ($data[0] == 'quote') {
140                        $renderer->doc .= '<div class="styler-quote' . $class . '">';
141                    } elseif ($data[0] == 'epigraph') {
142                        $renderer->doc .= '<div class="epigraph' . $class . '">';
143                    } else {
144                        $renderer->doc .= '<div class="styler' . $class . '">';
145                    }
146                    break;
147                case DOKU_LEXER_UNMATCHED:
148                    $renderer->doc .= htmlspecialchars($data[0]);
149                    break;
150                case DOKU_LEXER_EXIT:
151                    $renderer->doc .= "</div>\n<p>"; // "</p>" and "\n</p>" is hack
152                    break;
153            }
154            return true;
155        }
156        return false;
157    }
158}
159