xref: /plugin/mikioplugin/syntax/core.php (revision 17e6d63900b7250e4c2aa308b63914ffea16ab55)
1<?php
2/**
3 * Mikio Core Syntax Plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     James Collins <james.collins@outlook.com.au>
7 */
8
9if (!defined('DOKU_INC')) die();
10if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11
12
13class syntax_plugin_mikioplugin_core extends DokuWiki_Syntax_Plugin {
14    public $pattern_entry       = '';
15    public $pattern_exit        = '';
16    public $tag                 = '';
17    public $noEndTag            = false;
18    public $defaults            = array();
19    public $options             = array();
20    public $values              = array();
21    public $incClasses          = array('shadow');
22
23
24    function __construct() {
25        if(count($this->incClasses) > 0) {
26            $this->options = array_merge($this->options, $this->incClasses);
27        }
28    }
29
30    public function getType() {
31        return 'formatting';
32    }
33
34
35    public function getAllowedTypes() { return array('formatting', 'substition', 'disabled'); }
36    public function getSort(){ return 32; }
37
38
39    public function connectTo($mode) {
40        if($this->pattern_entry == '' && $this->tag != '') {
41            if($this->noEndTag) {
42                $this->pattern_entry = '<(?:' . strtoupper($this->tag) . '|' . strtolower($this->tag) . ').*?>';
43            } else {
44                $this->pattern_entry = '<(?:' . strtoupper($this->tag) . '|' . strtolower($this->tag) . ').*?>(?=.*?</(?:' . strtoupper($this->tag) . '|' . strtolower($this->tag) . ')>)';
45            }
46        }
47
48        if($this->pattern_entry != '') {
49            if($this->noEndTag) {
50                $this->Lexer->addSpecialPattern($this->pattern_entry, $mode, 'plugin_mikioplugin_'.$this->getPluginComponent());
51            } else {
52                $this->Lexer->addEntryPattern($this->pattern_entry, $mode, 'plugin_mikioplugin_'.$this->getPluginComponent());
53            }
54        }
55    }
56
57
58    public function postConnect() {
59        if(!$this->noEndTag) {
60            if($this->pattern_exit == '' && $this->tag != '') {
61                $this->pattern_exit = '</(?:' . strtoupper($this->tag) . '|' . strtolower($this->tag) . ')>';
62            }
63
64            if($this->pattern_exit != '') {
65                $this->Lexer->addExitPattern($this->pattern_exit, 'plugin_mikioplugin_'.$this->getPluginComponent());
66            }
67        }
68    }
69
70    public function handle($match, $state, $pos, Doku_Handler $handler){
71        switch($state) {
72            case DOKU_LEXER_ENTER:
73            case DOKU_LEXER_SPECIAL:
74                $optionlist = preg_split('/\s(?=([^"]*"[^"]*")*[^"]*$)/', substr($match, strlen($this->tag) + 1, -1));
75
76                $options_clean = array();
77
78                $options = array();
79                foreach($optionlist as $item) {
80                    $i = strpos($item, '=');
81                    if($i !== false) {
82                        $value = substr($item, $i + 1);
83
84                        if(substr($value, 0, 1) == '"') $value = substr($value, 1);
85                        if(substr($value, -1) == '"') $value = substr($value, 0, -1);
86
87                        $options[substr($item, 0, $i)] = $value;
88                    } else {
89                        $options[$item] = true;
90                    }
91                }
92
93                foreach($this->options as $item => $value) {
94                    if(is_string($value)) {
95                        if(array_key_exists($value, $options)) {
96                            $options_clean[$value] = $options[$value];
97                        } else {
98                            $options_clean[$value] = false;
99                        }
100                    } else if(is_array($value)) {
101                        foreach($value as $avalue) {
102                            if(array_key_exists($avalue, $options)) {
103                                $options_clean[$item] = $avalue;
104                            }
105                        }
106                    }
107                }
108
109                foreach($this->defaults as $item => $value) {
110                    if(array_key_exists($item, $options_clean) == false) {
111                        $options_clean[$item] = $value;
112                    }
113                }
114
115                $this->values = $options_clean;
116
117                return array($state, $options_clean);
118
119            case DOKU_LEXER_UNMATCHED:
120                return array($state, $match);
121
122            case DOKU_LEXER_EXIT:
123                return array($state, '');
124        }
125
126        return array();
127    }
128
129
130    public function render_lexer_enter(Doku_Renderer $renderer, $data) {
131
132    }
133
134
135    public function render_lexer_unmatched(Doku_Renderer $renderer, $data) {
136        $renderer->doc .= $renderer->_xmlEntities($data);
137    }
138
139
140    public function render_lexer_exit(Doku_Renderer $renderer, $data) {
141
142    }
143
144
145    public function render_lexer_special(Doku_Renderer $renderer, $data) {
146
147    }
148
149
150    public function render($mode, Doku_Renderer $renderer, $data) {
151        if($mode == 'xhtml'){
152            list($state,$match) = $data;
153
154            switch ($state) {
155                case DOKU_LEXER_ENTER:
156                    $this->render_lexer_enter($renderer, $match);
157                    return true;
158
159                case DOKU_LEXER_UNMATCHED :
160                    $this->render_lexer_unmatched($renderer, $match);
161                    return true;
162
163                case DOKU_LEXER_EXIT :
164                    $this->render_lexer_exit($renderer, $match);
165                    return true;
166
167                    case DOKU_LEXER_SPECIAL:
168                        $this->render_lexer_special($renderer, $match);
169                        return true;
170            }
171
172            return true;
173        }
174
175        return false;
176    }
177
178
179    public function buildClassString($options, $classes, $prefix) {
180        $s = array();
181
182        foreach($classes as $item) {
183            if(array_key_exists($item, $options) && $options[$item] !== false) {
184                $classname = $item;
185
186                if(is_string($options[$item])) {
187                    $classname = $options[$item];
188                }
189
190                if(is_string($prefix)) {
191                    $classname = $prefix . $classname;
192                } else if(is_array($prefix)) {
193                    foreach($prefix as $pitem => $pvalue) {
194                        if(is_string($pvalue)) {
195                            if($pvalue == $item) {
196                                if(is_string($options[$item])) {
197                                    $classname = $pitem . $options[$item];
198                                } else {
199                                    $classname = $pitem . $item;
200                                }
201                            }
202                        }
203
204                        if(is_array($pvalue)) {
205                            foreach($pvalue as $ppitem) {
206                                if($ppitem == $item) {
207                                    if(is_string($options[$item])) {
208                                        $classname = $pitem . $options[$item];
209                                    } else {
210                                        $classname = $pitem . $item;
211                                    }
212                                }
213                            }
214                        }
215                    }
216                }
217
218                $s[] = $classname;
219            }
220        }
221
222        foreach($this->incClasses as $item) {
223            if(array_key_exists($item, $options) && $options[$item] == true) $s[] = $item;
224        }
225
226        $s = ' ' . implode(' ', $s);
227        return $s;
228    }
229
230
231    public function getMediaFile($str) {
232        $i = strpos($str, '?');
233        if($i !== false) $str = substr($str, 0, $i);
234
235        $str = preg_replace('/[^\da-zA-Z:_.]+/', '', $str);
236
237        return(tpl_getMediaFile(array($str), false));
238    }
239
240
241    public function setAttr(&$attrList, $attr, $data, $newAttrName='', $newAttrVal='') {
242        if(array_key_exists($attr, $data) && $data[$attr] !== false) {
243            $value = $data[$attr];
244
245            if($newAttrName != '') $attr = $newAttrName;
246            if($newAttrVal != '') {
247                $newAttrVal = str_replace('%%VALUE%%', $value, $newAttrVal);
248                if(strpos($newAttrVal, '%%MEDIA%%') !== false) {
249                    $newAttrVal = str_replace('%%MEDIA%%', $this->getMediaFile($value), $newAttrVal);
250                }
251
252                $value = $newAttrVal;
253            }
254
255            $attrList[$attr] = $value;
256        }
257    }
258
259
260    public function listAttr($attrName, $attrs) {
261        $s = '';
262
263        if(count($attrs) > 0) {
264            foreach($attrs as $item => $value) {
265                $s .= $item . ':' . $value . ';';
266            }
267
268            $s = $attrName . '="' . $s . '" ';
269        }
270
271        return $s;
272    }
273
274
275    public function syntaxRender(Doku_Renderer $renderer, $className, $text) {
276        $class = new $className;
277
278        $class->render_lexer_enter($renderer, null);
279        $renderer->doc .= $text;
280        $class->render_lexer_exit($renderer, null);
281    }
282}