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 if ($opts['align'] == "left") $this->rcmd['float'] = "left"; 177 if ($opts['align'] == "center") $this->rcmd['float'] = "center"; 178 if ($opts['align'] == "right") $this->rcmd['float'] = "right"; 179 if ($opts['valign'] == "top") $this->rcmd['valign'] = "vtop"; 180 if ($opts['valign'] == "center") $this->rcmd['valign'] = "vcenter"; 181 if ($opts['valign'] == "bottom") $this->rcmd['valign'] = "vbottom"; 182 if (!empty($opts['caption'])) 183 $this->rcmd['caption'] = hsc($opts['caption']); 184 if (!empty($opts['type'])) 185 $this->rcmd['type'] = hsc($opts['type']); 186 if (!empty($opts['width'])) 187 $this->rcmd['width'] = hsc($opts['width']); 188 $this->rcmd['wrap'] = !empty($opts['wrap']); 189 break; 190 case DOKU_LEXER_MATCHED: 191 192 $menuitem = preg_split('/\|/', trim(substr($match,6,-7))); 193 194 $title = hsc($menuitem[0]); 195 if (substr($menuitem[2],0,2) == "{{") 196 $link = $this->_itemimage($menuitem[2], $title); 197 else 198 $link = $this->_itemLink($menuitem[2], $title); 199 $image = $this->_itemimage($menuitem[3], $title); 200 201 $this->rcmd['items'][] = array("image" => $image, 202 "link" => $link, 203 "descr" => hsc($menuitem[1])); 204 205 if (!empty($opts['width'])) { 206 // find out how much space the biggest menu item needs 207 $titlelen = mb_strlen($menuitem[0], "UTF-8"); 208 if ($titlelen > $this->rcmd['width']) 209 $this->rcmd['width'] = $titlelen; 210 } 211 break; 212 case DOKU_LEXER_EXIT: 213 // give the menu a convinient width. IE6 needs more space here than Firefox 214 if (!empty($opts['width'])) { 215 $this->rcmd['width'] += 5; 216 } 217 return $this->rcmd; 218 default: 219 break; 220 } 221 return array(); 222 } 223 224 function _reset() 225 { 226 $this->rcmd = array(); 227 $this->rcmd['columns'] = 1; 228 $this->rcmd['float'] = "left"; 229 $this->rcmd['valign'] = "vtop"; 230 } 231 232 function _itemlink($match, $title) { 233 // Strip the opening and closing markup 234 $link = preg_replace(array('/^\[\[/','/\]\]$/u'),'',$match); 235 236 // Split title from URL 237 $link = explode('|',$link,2); 238 $ref = trim($link[0]); 239 240 //decide which kind of link it is 241 if ( preg_match('/^[a-zA-Z0-9\.]+>{1}.*$/u',$ref) ) { 242 // Interwiki 243 $interwiki = explode('>',$ref,2); 244 return array('interwikilink', 245 array($ref,$title,strtolower($interwiki[0]),$interwiki[1])); 246 } elseif ( preg_match('/^\\\\\\\\[\w.:?\-;,]+?\\\\/u',$ref) ) { 247 // Windows Share 248 return array('windowssharelink', array($ref,$title)); 249 } elseif ( preg_match('#^([a-z0-9\-\.+]+?)://#i',$ref) ) { 250 // external link (accepts all protocols) 251 return array('externallink', array($ref,$title)); 252 } elseif ( preg_match('<'.PREG_PATTERN_VALID_EMAIL.'>',$ref) ) { 253 // E-Mail (pattern above is defined in inc/mail.php) 254 return array('emaillink', array($ref,$title)); 255 } elseif ( preg_match('!^#.+!',$ref) ) { 256 // local link 257 return array('locallink', array(substr($ref,1),$title)); 258 } else { 259 // internal link 260 return array('internallink', array($ref,$title)); 261 } 262 } 263 264 function _itemimage($match, $title) { 265 $p = Doku_Handler_Parse_Media($match); 266 267 return array($p['type'], 268 array($p['src'], $title, $p['align'], $p['width'], 269 $p['height'], $p['cache'], $p['linking'])); 270 } 271 272 /** 273 * Handle the actual output creation. 274 * 275 * <p> 276 * The method checks for the given <tt>$aFormat</tt> and returns 277 * <tt>FALSE</tt> when a format isn't supported. <tt>$aRenderer</tt> 278 * contains a reference to the renderer object which is currently 279 * handling the rendering. The contents of <tt>$aData</tt> is the 280 * return value of the <tt>handle()</tt> method. 281 * </p> 282 * @param $aFormat String The output format to generate. 283 * @param $aRenderer Object A reference to the renderer object. 284 * @param $aData Array The data created by the <tt>handle()</tt> 285 * method. 286 * @return Boolean <tt>TRUE</tt> if rendered successfully, or 287 * <tt>FALSE</tt> otherwise. 288 * @public 289 * @see handle() 290 */ 291 function render($mode, Doku_Renderer $renderer, $data) { 292 293 if (empty($data)) return false; 294 295 if($mode == 'xhtml'){ 296 if ($data['type'] != "menubar"){ 297 $renderer->doc .= '<div class="menu menu'.$data['float'].' menu'.$data['valign'].'"'; 298 $renderer->doc .= ' style="width:' . $data['width'] . '">'."\n"; 299 if (isset($data['caption'])) 300 $renderer->doc .= '<p class="caption">'.$data['caption'].'</p>'."\n"; 301 302 $width = floor(100 / $data['columns']) . '%'; 303 304 foreach($data['items'] as $item) { 305 $renderer->doc .= '<div class="menuitem" style="width:' . $width . '">'."\n"; 306 307 // create <img .. /> tag 308 list($type, $args) = $item['image']; 309 list($ext,$mime,$dl) = mimetype($args[0]); 310 $class = ($ext == 'png') ? ' png' : NULL; 311 $img = $renderer->_media($args[0],$args[1],$class,$args[3],$args[4],$args[5]); 312 313 // create <a href= .. /> tag 314 list($type, $args) = $item['link']; 315 $link = $this->_getLink($type, $args, $renderer); 316 $link['title'] = $args[1]; 317 318 $link['name'] = $img; 319 $renderer->doc .= $renderer->_formatLink($link); 320 321 $link['name'] = '<span class="menutext">'.$args[1].'</span>'; 322 $renderer->doc .= '<div class="menutextcontainer">'."\n"; 323 $renderer->doc .= $renderer->_formatLink($link); 324 $renderer->doc .= '<p class="menudesc">'.$item['descr'].'</p>'; 325 $renderer->doc .= '</div>'."\n"; 326 327 $renderer->doc .= '</div>'."\n"; 328 } 329 330 $renderer->doc .= '</div>'."\n"; 331 332 // Clear left/right floats, unless the 'wrap' setting is enabled. 333 if (!$data['wrap'] && ($data['float'] == "right" || $data['float'] == "left")) 334 $renderer->doc .= '<p style="clear:both;" />'; 335 336 return true; 337 } 338 // menubar mode: 1 row with small captions 339 if ($data['type'] == "menubar"){ 340 $renderer->doc .= '<div id="menu"><ul class="menubar">'."\n"; 341 // if (isset($data['caption'])) 342 // $renderer->doc .= '<p class="caption">'.$data['caption'].'</p>'."\n"; 343 344 foreach($data['items'] as $item) { 345 $renderer->doc .= '<li>'."\n"; 346 347 // create <img .. /> tag 348 list($type, $args) = $item['image']; 349 list($ext,$mime,$dl) = mimetype($args[0]); 350 $class = ($ext == 'png') ? ' png' : NULL; 351 $img = $renderer->_media($args[0],$item['descr'],$class,$args[3],$args[4],$args[5]); 352 353 // create <a href= .. /> tag 354 list($type, $args) = $item['link']; 355 $link = $this->_getLink($type, $args, $renderer); 356 $link['title'] = $args[1]; 357 358 $link['name'] = $img; 359 $renderer->doc .= $renderer->_formatLink($link); 360 361 $link['name'] = '<p class="menutext">'.$args[1].'</p>'; 362 $renderer->doc .= $renderer->_formatLink($link); 363 //$renderer->doc .= '<p class="menudesc">'.$item['descr'].'</p>'; 364 $renderer->doc .= '</li>'; 365 } 366 367 $renderer->doc .= '</ul></div>'."\n"; 368 369 return true; 370 } 371 372 } 373 return false; 374 } 375 376 function _createLink($url, $target=NULL) 377 { 378 global $conf; 379 380 $link = array(); 381 $link['class'] = ''; 382 $link['style'] = ''; 383 $link['pre'] = ''; 384 $link['suf'] = ''; 385 $link['more'] = ''; 386 $link['title'] = ''; 387 $link['name'] = ''; 388 $link['url'] = $url; 389 390 $link['target'] = $target == NULL ? '' : $conf['target'][$target]; 391 if ($target == 'interwiki' && strpos($url,DOKU_URL) === 0) { 392 //we stay at the same server, so use local target 393 $link['target'] = $conf['target']['wiki']; 394 } 395 396 return $link; 397 } 398 399 function _getLink($type, $args, &$renderer) 400 { 401 global $ID; 402 global $conf; 403 404 $check = false; 405 $exists = false; 406 407 switch ($type) { 408 case 'interwikilink': 409 $url = $renderer->_resolveInterWiki($args[2],$args[3]); 410 $link = $this->_createLink($url, 'interwiki'); 411 break; 412 case 'windowssharelink': 413 $url = str_replace('\\','/',$args[0]); 414 $url = 'file:///'.$url; 415 $link = $this->_createLink($url, 'windows'); 416 break; 417 case 'externallink': 418 $link = $this->_createLink($args[0], 'extern'); 419 break; 420 case 'emaillink': 421 $address = $renderer->_xmlEntities($args[0]); 422 $address = obfuscate($address); 423 if ($conf['mailguard'] == 'visible') 424 $address = rawurlencode($address); 425 426 $link = $this->_createLink('mailto:'.$address); 427 $link['class'] = 'JSnocheck'; 428 break; 429 case 'locallink': 430 $hash = sectionID($args[0], $check); 431 $link = $this->_createLink('#'.$hash); 432 $link['class'] = "wikilink1"; 433 break; 434 case 'internallink': 435 resolve_pageid(getNS($ID),$args[0],$exists); 436 $url = wl($args[0]); 437 list($id,$hash) = explode('#',$args[0],2); 438 if (!empty($hash)) $hash = sectionID($hash, $check); 439 if ($hash) $url .= '#'.$hash; //keep hash anchor 440 441 $link = $this->_createLink($url, 'wiki'); 442 $link['class'] = $exists ? 'wikilink1' : 'wikilink2'; 443 break; 444 case 'internalmedia': 445 resolve_mediaid(getNS($ID),$args[0], $exists); 446 $url = ml($args[0],array('id'=>$ID,'cache'=>$args[5]),true); 447 $link = $this->_createLink($url); 448 if (!$exists) $link['class'] = 'wikilink2'; 449 break; 450 case 'externalmedia': 451 $url = ml($args[0],array('cache'=>$args[5])); 452 $link = $this->_createLink($url); 453 break; 454 } 455 return $link; 456 } 457 458 /** 459 * Parse menu options 460 * 461 * 462 * @param $string String Option string from <menu> tag. 463 * @return array of options (name >= option). the array will be empty 464 * if no options are found 465 * @private 466 */ 467 function _parseOptions($string) { 468 $data = array(); 469 470 $dq = false; 471 $iskey = true; 472 $key = ''; 473 $val = ''; 474 475 $len = strlen($string); 476 for ($i=0; $i<=$len; $i++) { 477 // done for this one? 478 if ($string[$i] == ',' || $i == $len) { 479 $key = trim($key); 480 $val = trim($val); 481 if($key && $val) $data[strtolower($key)] = $val; 482 $iskey = true; 483 $key = ''; 484 $val = ''; 485 continue; 486 } 487 488 // double quotes 489 if ($string[$i] == '"') { 490 $dq = $dq ? false : true; 491 continue; 492 } 493 494 // key value separator 495 if ($string[$i] == '=' && !$dq && $iskey) { 496 $iskey = false; 497 continue; 498 } 499 500 // default 501 if ($iskey) 502 $key .= $string[$i]; 503 else 504 $val .= $string[$i]; 505 } 506 return $data; 507 } 508 509} 510 511//Setup VIM: ex: et ts=4 enc=utf-8 : 512?> 513