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