1<?php
2/**
3 * Plugin: Displays a link list in a nice way
4 *
5 * Syntax: <menu col=2,align=center,caption="headline">
6 *           <item>name|description|link|image</item>
7 *         </menu>
8 *
9 * Options have to be separated by comma.
10 * col (opt)     The number of columns of the menu. Allowed are 1-4, default is 1
11 * align (opt)   Alignment of the menu. Allowed are "left", "center" or "right",
12 *               default is "left"
13 * caption (opt) Headline of the menu, default is none
14 *
15 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
16 * @author     Matthias Grimm <matthiasgrimm@users.sourceforge.net>
17 * @author     Frank Schiebel <frank@linuxmuster.net>
18 */
19
20if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
21if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
22require_once(DOKU_PLUGIN.'syntax.php');
23
24/**
25 * All DokuWiki plugins to extend the parser/rendering mechanism
26 * need to inherit from this class
27 */
28class syntax_plugin_menu extends DokuWiki_Syntax_Plugin {
29
30    var $rcmd = array();  /**< Command array for the renderer */
31
32    function __construct() {
33    }
34
35
36   /**
37    * Get the type of syntax this plugin defines.
38    *
39    * The type of this plugin is "protected". It has a start and an end
40    * token and no other wiki commands shall be parsed between them.
41    *
42    * @param none
43    * @return String <tt>'protected'</tt>.
44    * @public
45    * @static
46    */
47    function getType(){
48        return 'protected';
49    }
50
51   /**
52    * Define how this plugin is handled regarding paragraphs.
53    *
54    * <p>
55    * This method is important for correct XHTML nesting. It returns
56    * one of the following values:
57    * </p>
58    * <dl>
59    * <dt>normal</dt><dd>The plugin can be used inside paragraphs.</dd>
60    * <dt>block</dt><dd>Open paragraphs need to be closed before
61    * plugin output.</dd>
62    * <dt>stack</dt><dd>Special case: Plugin wraps other paragraphs.</dd>
63    * </dl>
64    * @param none
65    * @return String <tt>'block'</tt>.
66    * @public
67    * @static
68    */
69    function getPType(){
70        return 'block';
71    }
72
73   /**
74    * Where to sort in?
75    *
76    * Sort the plugin in just behind the formating tokens
77    *
78    * @param none
79    * @return Integer <tt>135</tt>.
80    * @public
81    * @static
82    */
83    function getSort(){
84        return 135;
85    }
86
87   /**
88    * Connect lookup pattern to lexer.
89    *
90    * @param $aMode String The desired rendermode.
91    * @return none
92    * @public
93    * @see render()
94    */
95    function connectTo($mode) {
96       $this->Lexer->addEntryPattern('<menu>(?=.*?</menu.*?>)',$mode,'plugin_menu');
97       $this->Lexer->addEntryPattern('<menu\s[^\r\n\|]*?>(?=.*?</menu.*?>)',$mode,'plugin_menu');
98    }
99
100    function postConnect() {
101      $this->Lexer->addPattern('<item>.+?</item>','plugin_menu');
102      $this->Lexer->addExitPattern('</menu>','plugin_menu');
103    }
104
105   /**
106    * Handler to prepare matched data for the rendering process.
107    *
108    * <p>
109    * The <tt>$aState</tt> parameter gives the type of pattern
110    * which triggered the call to this method:
111    * </p>
112    * <dl>
113    * <dt>DOKU_LEXER_ENTER</dt>
114    * <dd>a pattern set by <tt>addEntryPattern()</tt></dd>
115    * <dt>DOKU_LEXER_MATCHED</dt>
116    * <dd>a pattern set by <tt>addPattern()</tt></dd>
117    * <dt>DOKU_LEXER_EXIT</dt>
118    * <dd> a pattern set by <tt>addExitPattern()</tt></dd>
119    * <dt>DOKU_LEXER_SPECIAL</dt>
120    * <dd>a pattern set by <tt>addSpecialPattern()</tt></dd>
121    * <dt>DOKU_LEXER_UNMATCHED</dt>
122    * <dd>ordinary text encountered within the plugin's syntax mode
123    * which doesn't match any pattern.</dd>
124    * </dl>
125    * @param $aMatch String The text matched by the patterns.
126    * @param $aState Integer The lexer state for the match.
127    * @param $aPos Integer The character position of the matched text.
128    * @param $aHandler Object Reference to the Doku_Handler object.
129    * @return Integer The current lexer state for the match.
130    * @public
131    * @see render()
132    * @static
133    */
134    function handle($match, $state, $pos, Doku_Handler $handler)
135    {
136        switch ($state) {
137            case DOKU_LEXER_ENTER:
138                $this->_reset();        // reset object;
139
140                $opts = $this->_parseOptions(trim(substr($match,5,-1)));
141                $col = $opts['col'];
142                if (!empty($col) && is_numeric($col) && $col > 0 && $col < 5)
143                        $this->rcmd['columns'] = $col;
144
145                if (isset($opts['align'])) {
146                    if ($opts['align'] == "left")   $this->rcmd['float'] = "left";
147                    if ($opts['align'] == "center") $this->rcmd['float'] = "center";
148                    if ($opts['align'] == "right")  $this->rcmd['float'] = "right";
149                }
150                if (isset($opts['valign'])) {
151                    if ($opts['valign'] == "top")   $this->rcmd['valign'] = "vtop";
152                    if ($opts['valign'] == "center") $this->rcmd['valign'] = "vcenter";
153                    if ($opts['valign'] == "bottom")  $this->rcmd['valign'] = "vbottom";
154                }
155                if (!empty($opts['caption']))
156                    $this->rcmd['caption'] = hsc($opts['caption']);
157                if (!empty($opts['type']))
158                    $this->rcmd['type'] = hsc($opts['type']);
159                if (!empty($opts['width']))
160                    $this->rcmd['width'] = hsc($opts['width']);
161                $this->rcmd['wrap'] = !empty($opts['wrap']);
162            break;
163          case DOKU_LEXER_MATCHED:
164
165                $menuitem = preg_split('/\|/', trim(substr($match,6,-7)));
166
167                $title = hsc($menuitem[0]);
168                if (substr($menuitem[2],0,2) == "{{")
169                    $link = $this->_itemimage($menuitem[2], $title);
170                else
171                    $link = $this->_itemLink($menuitem[2], $title);
172                $image = $this->_itemimage($menuitem[3], $title);
173
174                $this->rcmd['items'][] = array("image" => $image,
175                                               "link"  => $link,
176                                               "descr" => hsc($menuitem[1]));
177
178                if (!empty($opts['width'])) {
179                    // find out how much space the biggest menu item needs
180                    $titlelen = mb_strlen($menuitem[0], "UTF-8");
181                    if ($titlelen > $this->rcmd['width'])
182                        $this->rcmd['width'] = $titlelen;
183                }
184            break;
185          case DOKU_LEXER_EXIT:
186              // give the menu a convinient width. IE6 needs more space here than Firefox
187              if (!empty($opts['width'])) {
188                  $this->rcmd['width'] += 5;
189              }
190              return $this->rcmd;
191          default:
192            break;
193        }
194        return array();
195    }
196
197    function _reset()
198    {
199        $this->rcmd = array();
200        $this->rcmd['columns'] = 1;
201        $this->rcmd['float']   = "left";
202        $this->rcmd['valign']  = "vtop";
203    }
204
205    function _itemlink($match, $title) {
206        // Strip the opening and closing markup
207        $link = preg_replace(array('/^\[\[/','/\]\]$/u'),'',$match);
208
209        // Split title from URL
210        $link = explode('|',$link,2);
211        $ref  = trim($link[0]);
212
213        //decide which kind of link it is
214        if ( preg_match('/^[a-zA-Z0-9\.]+>{1}.*$/u',$ref) ) {
215            // Interwiki
216            $interwiki = explode('>',$ref,2);
217            return array('interwikilink',
218                         array($ref,$title,strtolower($interwiki[0]),$interwiki[1]));
219        } elseif ( preg_match('/^\\\\\\\\[\w.:?\-;,]+?\\\\/u',$ref) ) {
220            // Windows Share
221            return array('windowssharelink', array($ref,$title));
222        } elseif ( preg_match('#^([a-z0-9\-\.+]+?)://#i',$ref) ) {
223            // external link (accepts all protocols)
224            return array('externallink', array($ref,$title));
225        } elseif ( preg_match('<'.PREG_PATTERN_VALID_EMAIL.'>',$ref) ) {
226            // E-Mail (pattern above is defined in inc/mail.php)
227            return array('emaillink', array($ref,$title));
228        } elseif ( preg_match('!^#.+!',$ref) ) {
229            // local link
230            return array('locallink', array(substr($ref,1),$title));
231        } else {
232            // internal link
233            return array('internallink', array($ref,$title));
234        }
235    }
236
237    function _itemimage($match, $title) {
238        $p = Doku_Handler_Parse_Media($match);
239
240        return array($p['type'],
241                     array($p['src'], $title, $p['align'], $p['width'],
242                     $p['height'], $p['cache'], $p['linking']));
243    }
244
245   /**
246    * Handle the actual output creation.
247    *
248    * <p>
249    * The method checks for the given <tt>$aFormat</tt> and returns
250    * <tt>FALSE</tt> when a format isn't supported. <tt>$aRenderer</tt>
251    * contains a reference to the renderer object which is currently
252    * handling the rendering. The contents of <tt>$aData</tt> is the
253    * return value of the <tt>handle()</tt> method.
254    * </p>
255    * @param $aFormat String The output format to generate.
256    * @param $aRenderer Object A reference to the renderer object.
257    * @param $aData Array The data created by the <tt>handle()</tt>
258    * method.
259    * @return Boolean <tt>TRUE</tt> if rendered successfully, or
260    * <tt>FALSE</tt> otherwise.
261    * @public
262    * @see handle()
263    */
264    function render($mode, Doku_Renderer $renderer, $data) {
265
266        if (empty($data)) return false;
267
268        if($mode == 'xhtml'){
269            if ($data['type'] != "menubar"){
270                    $renderer->doc .= '<div class="menu menu'.$data['float'].' menu'.$data['valign'].'"';
271                    $renderer->doc .= ' style="width:' . $data['width'] . '">'."\n";
272                    if (isset($data['caption']))
273                        $renderer->doc .= '<p class="caption">'.$data['caption'].'</p>'."\n";
274
275                    $width = floor(100 / $data['columns']) . '%';
276
277                    foreach($data['items'] as $item) {
278                        $renderer->doc .= '<div class="menuitem" style="width:' . $width . '">'."\n";
279
280                        // create <img .. /> tag
281                        list($type, $args) = $item['image'];
282                        list($ext,$mime,$dl) = mimetype($args[0]);
283                        $class = ($ext == 'png') ? ' png' : NULL;
284                        $img = $renderer->_media($args[0],$args[1],$class,$args[3],$args[4],$args[5]);
285
286                        // create <a href= .. /> tag
287                        list($type, $args) = $item['link'];
288                        $link = $this->_getLink($type, $args, $renderer);
289                        $link['title'] = $args[1];
290
291                        $link['name']  = $img;
292                        $renderer->doc .= $renderer->_formatLink($link);
293
294                        $link['name']  = '<span class="menutext">'.$args[1].'</span>';
295                        $renderer->doc .= '<div class="menutextcontainer">'."\n";
296                        $renderer->doc .= $renderer->_formatLink($link);
297                        $renderer->doc .= '<p class="menudesc">'.$item['descr'].'</p>';
298                        $renderer->doc .= '</div>'."\n";
299
300                        $renderer->doc .= '</div>'."\n";
301                    }
302
303                    $renderer->doc .= '</div>'."\n";
304
305                    // Clear left/right floats, unless the 'wrap' setting is enabled.
306                    if (!$data['wrap'] && ($data['float'] == "right" || $data['float'] == "left"))
307                        $renderer->doc .= '<p style="clear:both;" />';
308
309                    return true;
310            }
311            // menubar mode: 1 row with small captions
312            if ($data['type'] == "menubar"){
313                    $renderer->doc .= '<div id="menu"><ul class="menubar">'."\n";
314                  //  if (isset($data['caption']))
315                  //      $renderer->doc .= '<p class="caption">'.$data['caption'].'</p>'."\n";
316
317                    foreach($data['items'] as $item) {
318                        $renderer->doc .= '<li>'."\n";
319
320                        // create <img .. /> tag
321                        list($type, $args) = $item['image'];
322                        list($ext,$mime,$dl) = mimetype($args[0]);
323                        $class = ($ext == 'png') ? ' png' : NULL;
324                        $img = $renderer->_media($args[0],$item['descr'],$class,$args[3],$args[4],$args[5]);
325
326                        // create <a href= .. /> tag
327                        list($type, $args) = $item['link'];
328                        $link = $this->_getLink($type, $args, $renderer);
329                        $link['title'] = $args[1];
330
331                        $link['name']  = $img;
332                        $renderer->doc .= $renderer->_formatLink($link);
333
334                        $link['name']  = '<p class="menutext">'.$args[1].'</p>';
335                        $renderer->doc .= $renderer->_formatLink($link);
336                        //$renderer->doc .= '<p class="menudesc">'.$item['descr'].'</p>';
337                        $renderer->doc .= '</li>';
338                    }
339
340                    $renderer->doc .= '</ul></div>'."\n";
341
342                    return true;
343            }
344
345        }
346        return false;
347    }
348
349    function _createLink($url, $target=NULL)
350    {
351        global $conf;
352
353        $link = array();
354        $link['class']  = '';
355        $link['style']  = '';
356        $link['pre']    = '';
357        $link['suf']    = '';
358        $link['more']   = '';
359        $link['title']  = '';
360        $link['name']   = '';
361        $link['url']    = $url;
362
363        $link['target'] = $target == NULL ? '' : $conf['target'][$target];
364        if ($target == 'interwiki' && strpos($url,DOKU_URL) === 0) {
365            //we stay at the same server, so use local target
366            $link['target'] = $conf['target']['wiki'];
367        }
368
369        return $link;
370    }
371
372    function _getLink($type, $args, &$renderer)
373    {
374        global $ID;
375        global $conf;
376
377        $check = false;
378        $exists = false;
379
380        switch ($type) {
381        case 'interwikilink':
382            $url  = $renderer->_resolveInterWiki($args[2],$args[3]);
383            $link = $this->_createLink($url, 'interwiki');
384            break;
385        case 'windowssharelink':
386            $url  = str_replace('\\','/',$args[0]);
387            $url  = 'file:///'.$url;
388            $link = $this->_createLink($url, 'windows');
389            break;
390        case 'externallink':
391            $link = $this->_createLink($args[0], 'extern');
392            break;
393        case 'emaillink':
394            $address = $renderer->_xmlEntities($args[0]);
395            $address = obfuscate($address);
396            if ($conf['mailguard'] == 'visible')
397                 $address = rawurlencode($address);
398
399            $link = $this->_createLink('mailto:'.$address);
400            $link['class'] = 'JSnocheck';
401            break;
402        case 'locallink':
403            $hash = sectionID($args[0], $check);
404            $link = $this->_createLink('#'.$hash);
405            $link['class'] = "wikilink1";
406            break;
407        case 'internallink':
408            resolve_pageid(getNS($ID),$args[0],$exists);
409            $url  = wl($args[0]);
410            list($id, $hash) = sexplode('#',$args[0],2);
411            if (!empty($hash)) $hash = sectionID($hash, $check);
412            if ($hash) $url .= '#'.$hash;    //keep hash anchor
413
414            $link = $this->_createLink($url, 'wiki');
415            $link['class'] = $exists ? 'wikilink1' : 'wikilink2';
416            break;
417        case 'internalmedia':
418            resolve_mediaid(getNS($ID),$args[0], $exists);
419            $url  = ml($args[0],array('id'=>$ID,'cache'=>$args[5]),true);
420            $link = $this->_createLink($url);
421            if (!$exists) $link['class'] = 'wikilink2';
422            break;
423        case 'externalmedia':
424            $url  = ml($args[0],array('cache'=>$args[5]));
425            $link = $this->_createLink($url);
426            break;
427        }
428        return $link;
429    }
430
431   /**
432    * Parse menu options
433    *
434    *
435    * @param $string String Option string from <menu> tag.
436    * @return array of options (name >= option). the array will be empty
437    *         if no options are found
438    * @private
439    */
440    function _parseOptions($string) {
441		$data = array();
442
443		$dq    = false;
444		$iskey = true;
445		$key   = '';
446		$val   = '';
447
448		$len = strlen($string);
449		for ($i=0; $i<=$len; $i++) {
450			// done for this one?
451			if ((isset($string[$i]) && $string[$i] == ',') || $i == $len) {
452				$key = trim($key);
453				$val = trim($val);
454				if($key && $val) $data[strtolower($key)] = $val;
455				$iskey = true;
456				$key = '';
457				$val = '';
458				continue;
459			}
460
461			// double quotes
462			if ($string[$i] == '"') {
463				$dq = $dq ? false : true;
464				continue;
465			}
466
467			// key value separator
468			if ($string[$i] == '=' && !$dq && $iskey) {
469				$iskey = false;
470				continue;
471			}
472
473			// default
474			if ($iskey)
475				$key .= $string[$i];
476			else
477				$val .= $string[$i];
478		}
479		return $data;
480    }
481
482}
483
484//Setup VIM: ex: et ts=4 enc=utf-8 :
485?>
486