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