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