1<?php 2/** 3 * Render Plugin for XHTML without details link for internal images. 4 * 5 * @author i-net software <tools@inetsoftware.de> 6 */ 7 8if(!defined('DOKU_INC')) die(); 9if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 10 11require_once DOKU_INC . 'inc/parser/xhtml.php'; 12 13/** 14 * The Renderer 15 */ 16class renderer_plugin_nodetailsxhtml extends Doku_Renderer_xhtml { 17 18 private $acronymsExchanged = null; 19 private $hasSeenHeader = false; 20 private $scriptmode = false; 21 22 private $startlevel = 0; // level to start with numbered headings (default = 2) 23 private $levels = array( '======'=>1, 24 '====='=>2, 25 '===='=>3, 26 '==='=>4, 27 '=='=>5 28 ); 29 30 public $sectionLevel = 0; 31 public $info = array( 32 'cache' => true, // may the rendered result cached? 33 'toc' => true, // render the TOC? 34 'forceTOC' => false, // shall I force the TOC? 35 'scriptmode' => false, // In scriptmode, some tags will not be encoded => '<%', '%>' 36 ); 37 38 public $headingCount = array( 1=>0, 39 2=>0, 40 3=>0, 41 4=>0, 42 5=>0 43 ); 44 45 /** 46 * return some info 47 */ 48 function getInfo(){ 49 return confToHash(dirname(__FILE__).'/plugin.info.txt'); 50 } 51 52 function canRender($format) { 53 return ($format=='xhtml'); 54 } 55 56 function document_start() { 57 global $TOC, $ID, $INFO, $conf; 58 59 parent::document_start(); 60 61 // Cheating in again 62 $meta = p_get_metadata($ID, null, false); // 2010-10-23 This should be save to use 63 64 if (isset($meta['toc']['toptoclevel'])) { 65 $conf['toptoclevel'] = $meta['toc']['toptoclevel']; 66 } 67 if (isset($meta['toc']['maxtoclevel'])) { 68 $conf['maxtoclevel'] = $meta['toc']['maxtoclevel']; 69 } 70 if (isset($meta['toc']['toptoclevel'])||isset($INFO['meta']['toc']['maxtoclevel'])) { 71 $conf['tocminheads'] = 1; 72 } 73 74 $newMeta = $meta['description']; 75 if ( is_array($newMeta) && !empty( $newMeta['tableofcontents'] ) && count($newMeta['tableofcontents']) > 1 ) { 76 // $TOC = $this->toc = $newMeta; // 2010-08-23 doubled the TOC 77 $TOC = $newMeta['tableofcontents']; 78 } 79 } 80 81 function document_end() { 82 83 parent::document_end(); 84 85 // Prepare the TOC 86 global $TOC, $ID; 87 $meta = array(); 88 89 $forceToc = $this->info['forceTOC'] || p_get_metadata($ID, 'internal forceTOC', false); 90 91 // NOTOC, and no forceTOC 92 if ( $this->info['toc'] === false && !$forceToc ) { 93 $TOC = $this->toc = array(); 94 $meta['internal']['toc'] = false; 95 $meta['description']['tableofcontents'] = array(); 96 $meta['internal']['forceTOC'] = false; 97 98 } else if ( $forceToc || (utf8_strlen(strip_tags($this->doc)) >= $this->getConf('documentlengthfortoc') && count($this->toc) > 1 ) ) { 99 $TOC = $this->toc; 100 // This is a little bit like cheating ... but this will force the TOC into the metadata 101 $meta = array(); 102 $meta['internal']['toc'] = true; 103 $meta['internal']['forceTOC'] = $forceToc; 104 $meta['description']['tableofcontents'] = $TOC; 105 } 106 107 // allways write new metadata 108 p_set_metadata($ID, $meta); 109 110 // make sure there are no empty blocks 111 $this->doc = preg_replace('#<(div|section|article) class=".*?level\d.*?">\s*</(div|section|article)>#','',$this->doc); 112 } 113 114 function header($text, $level, $pos) { 115 global $conf; 116 global $ID; 117 global $INFO; 118 119 if($text) { 120 121 // Check Text for hint about a CSS style class 122 $class = ""; 123 if ( preg_match("/^class:(.*?)>(.*?)$/", $text, $matches) ) { 124 $class = ' ' . $this->_xmlEntities($matches[1]); 125 $text = $matches[2]; 126 } 127 128 /* There should be no class for "sectioneditX" if there is no edit perm */ 129 $maxLevel = $conf['maxseclevel']; 130 if ( $INFO['perm'] <= AUTH_READ ) 131 { 132 $conf['maxseclevel'] = 0; 133 } 134 135 $headingNumber = ''; 136 $useNumbered = p_get_metadata($ID, 'usenumberedheading', true); // 2011-02-07 This should be save to use 137 if ( $this->getConf('usenumberedheading') || !empty($useNumbered) || !empty($INFO['meta']['usenumberedheading']) || isset($_REQUEST['usenumberedheading'])) { 138 139 // increment the number of the heading 140 $this->headingCount[$level]++; 141 142 // build the actual number 143 for ($i=1;$i<=5;$i++) { 144 145 // reset the number of the subheadings 146 if ($i>$level) { 147 $this->headingCount[$i] = 0; 148 } 149 150 // build the number of the heading 151 $headingNumber .= $this->headingCount[$i] . '.'; 152 } 153 154 $headingNumber = preg_replace("/(\.0)+\.?$/", '', $headingNumber) . ' '; 155 } 156 157 $doc = $this->doc; 158 $this->doc = ""; 159 160 parent::header($headingNumber . $text, $level, $pos); 161 162 $matches = []; 163 preg_match("/id=\"(.*?)\"/", $this->doc, $matches); 164 if ( count($matches) > 1 ) { 165 $this->doc = preg_replace("/id=\".*?\"/", '', $this->doc); 166 $this->doc = DOKU_LF.'<a id="'. $matches[1] .'" class="head-anchor" style="visibility:hidden"></a>'.DOKU_LF . $this->doc; 167 } 168 169 if ( $this->getConf('useSectionArticle') ) { 170 $this->doc = $doc . preg_replace("/(<h([1-9]))/", "<".($this->sectionLevel<1?'section':'article')." class=\"level\\2{$class}\">\\1", $this->doc); 171 } else { 172 $this->doc = $doc . $this->doc; 173 } 174 175 $conf['maxseclevel'] = $maxLevel; 176 177 } else if ( $INFO['perm'] > AUTH_READ ) { 178 179 if ( $hasSeenHeader ) $this->finishSectionEdit($pos); 180 181 // write the header 182 $name = $this->startSectionEdit($pos, array( 'target' => 'section_empty', 'name' => rand() . $level)); 183 if ( $this->getConf('useSectionArticle') ) { 184 $this->doc .= '<'.($this->sectionLevel<1?'section':'article').' class="'.$name.'">'; 185 } 186 187 $this->doc .= DOKU_LF.'<a name="'. $name .'" class="' . $name . '" ></a>'.DOKU_LF; 188 } 189 190 $hasSeenHeader = true; 191 } 192 193 public function finishSectionEdit($end = null, $hid = null) { 194 global $INFO; 195 if ( $INFO['perm'] > AUTH_READ ) 196 { 197 return parent::finishSectionEdit($end, $hid); 198 } 199 } 200 201 public function startSectionEdit($start, $data) { 202 global $INFO; 203 if ( $INFO['perm'] > AUTH_READ ) 204 { 205 return parent::startSectionEdit($start, $data); 206 } 207 208 return ""; 209 } 210 211 function section_close() { 212 $this->sectionLevel--; 213 $this->doc .= DOKU_LF.'</div>'.DOKU_LF; 214 if ( $this->getConf('useSectionArticle') ) { 215 $this->doc .= '</'.($this->sectionLevel<1?'section':'article').'>'.DOKU_LF; 216 } 217 } 218 219 function section_open($level) { 220 $this->sectionLevel++; 221 return parent::section_open($level); 222 } 223 224 function internalmedia ($src, $title=null, $align=null, $width=null, 225 $height=null, $cache=null, $linking=null, $return=NULL) { 226 global $ID; 227 list($src,$hash) = explode('#',$src,2); 228 resolve_mediaid(getNS($ID),$src, $exists); 229 230 $noLink = false; 231 $render = ($linking == 'linkonly') ? false : true; 232 $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render); 233 234 list($ext,$mime,$dl) = mimetype($src); 235 if(substr($mime,0,5) == 'image' && $render){ 236 $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),($linking=='direct')); 237 if ( substr($mime,0,5) == 'image' && $linking='details' ) { $noLink = true;} 238 }elseif($mime == 'application/x-shockwave-flash' && $render){ 239 // don't link flash movies 240 $noLink = true; 241 }else{ 242 // add file icons 243 $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext); 244 $link['class'] .= ' mediafile mf_'.$class; 245 $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),true); 246 } 247 248 if($hash) $link['url'] .= '#'.$hash; 249 250 //markup non existing files 251 if (!$exists) 252 $link['class'] .= ' wikilink2'; 253 254 //output formatted 255 if ($linking == 'nolink' || $noLink) $this->doc .= $link['name']; 256 else $this->doc .= $this->_formatLink($link); 257 } 258 259 /** 260 * Render an internal Wiki Link 261 * 262 * $search,$returnonly & $linktype are not for the renderer but are used 263 * elsewhere - no need to implement them in other renderers 264 * 265 * @author Andreas Gohr <andi@splitbrain.org> 266 */ 267 function internallink($id, $name = null, $search=null,$returnonly=false,$linktype='content') { 268 global $conf; 269 global $ID; 270 global $INFO; 271 272 $params = ''; 273 $parts = explode('?', $id, 2); 274 if (count($parts) === 2) { 275 $id = $parts[0]; 276 $params = $parts[1]; 277 } 278 279 // For empty $id we need to know the current $ID 280 // We need this check because _simpleTitle needs 281 // correct $id and resolve_pageid() use cleanID($id) 282 // (some things could be lost) 283 if ($id === '') { 284 $id = $ID; 285 } 286 287 // default name is based on $id as given 288 $default = $this->_simpleTitle($id); 289 290 // now first resolve and clean up the $id 291 resolve_pageid(getNS($ID),$id,$exists); 292 293 $name = $this->_getLinkTitle($name, $default, $isImage, $id, $linktype); 294 if ( !$isImage ) { 295 if ( $exists ) { 296 $class='wikilink1'; 297 } else { 298 $class='wikilink2'; 299 $link['rel']='nofollow'; 300 } 301 } else { 302 $class='media'; 303 } 304 305 //keep hash anchor 306 list($id,$hash) = explode('#',$id,2); 307 if(!empty($hash)) $hash = $this->_headerToLink($hash); 308 309 //prepare for formating 310 $link['target'] = $conf['target']['wiki']; 311 $link['style'] = ''; 312 $link['pre'] = ''; 313 $link['suf'] = ''; 314 // highlight link to current page 315 if ($id == $INFO['id']) { 316 $link['pre'] = '<span class="curid">'; 317 $link['suf'] = '</span>'; 318 } 319 $link['more'] = ''; 320 $link['class'] = $class; 321 $link['url'] = wl($id, $params); 322 $link['name'] = $name; 323 $link['title'] = $this->_getLinkTitle(null, $default, $isImage, $id, $linktype); 324 //add search string 325 if($search){ 326 ($conf['userewrite']) ? $link['url'].='?' : $link['url'].='&'; 327 if(is_array($search)){ 328 $search = array_map('rawurlencode',$search); 329 $link['url'] .= 's[]='.join('&s[]=',$search); 330 }else{ 331 $link['url'] .= 's='.rawurlencode($search); 332 } 333 } 334 335 //keep hash 336 if($hash) $link['url'].='#'.$hash; 337 338 //output formatted 339 if($returnonly){ 340 return $this->_formatLink($link); 341 }else{ 342 $this->doc .= $this->_formatLink($link); 343 } 344 } 345 346 function locallink($hash, $name = NULL, $returnonly = false){ 347 global $ID; 348 $name = $this->_getLinkTitle($name, $hash, $isImage); 349 $hash = $this->_headerToLink($hash); 350 $title = $name; 351 $this->doc .= '<a href="#'.$hash.'" title="'.$title.'" class="wikilink1">'; 352 $this->doc .= $name; 353 $this->doc .= '</a>'; 354 } 355 356 function acronym($acronym) { 357 358 if ( empty($this->acronymsExchanged) ) { 359 $this->acronymsExchanged = $this->acronyms; 360 $this->acronyms = array(); 361 362 foreach( $this->acronymsExchanged as $key => $value ) { 363 $this->acronyms[str_replace('_', ' ', $key)] = $value; 364 } 365 } 366 367 parent::acronym($acronym); 368 } 369 370 function entity($entity) { 371 372 if ( array_key_exists($entity, $this->entities) ) { 373 $entity = $this->entities[$entity]; 374 } 375 376 $this->doc .= $this->_xmlEntities($entity); 377 } 378 379 function _xmlEntities($string) { 380 381 // No double encode ... 382 $string = htmlspecialchars($string, ENT_QUOTES, 'UTF-8', false); 383 // $string = parent::_xmlEntities($string); 384 $string = htmlentities($string, 8, 'UTF-8'); 385 $string = $this->superentities($string); 386 387 if ( $this->info['scriptmode'] ) { 388 $string = str_replace( array( "<%", "%>", "<?", "?>"), 389 array( "<%", "%>", "<?", "?>"), 390 $string); 391 } 392 393 return $string; 394 } 395 396 // Unicode-proof htmlentities. 397 // Returns 'normal' chars as chars and weirdos as numeric html entites. 398 function superentities( $str ){ 399 // get rid of existing entities else double-escape 400 $str2 = ''; 401 $str = html_entity_decode(stripslashes($str),ENT_QUOTES,'UTF-8'); 402 $ar = preg_split('/(?<!^)(?!$)(?!\n)/u', $str ); // return array of every multi-byte character 403 foreach ($ar as $c){ 404 $o = ord($c); 405 if ( // (strlen($c) > 1) || /* multi-byte [unicode] */ 406 ($o > 127) // || /* <- control / latin weirdos -> */ 407 // ($o <32 || $o > 126) || /* <- control / latin weirdos -> */ 408 // ($o >33 && $o < 40) ||/* quotes + ambersand */ 409 // ($o >59 && $o < 63) /* html */ 410 411 ) { 412 // convert to numeric entity 413 $c = mb_encode_numericentity($c,array (0x0, 0xffff, 0, 0xffff), 'UTF-8'); 414 } 415 $str2 .= $c; 416 } 417 return $str2; 418 } 419 420 /** 421 * Renders internal and external media 422 * 423 * @author Andreas Gohr <andi@splitbrain.org> 424 * @param string $src media ID 425 * @param string $title descriptive text 426 * @param string $align left|center|right 427 * @param int $width width of media in pixel 428 * @param int $height height of media in pixel 429 * @param string $cache cache|recache|nocache 430 * @param bool $render should the media be embedded inline or just linked 431 * @return string 432 */ 433 function _media($src, $title = null, $align = null, $w = null, 434 $h = null, $cache = null, $render = true) { 435 436 list($ext, $mime) = mimetype($src); 437 if(substr($mime, 0, 5) == 'image') { 438 439 $info = @getimagesize(mediaFN($src)); //get original size 440 $srcset = []; 441 442 if($info !== false) { 443 444 $origWidth = $info[0]; 445 $origHeight = $info[1]; 446 447 if ( !$w && !$h ) $w = $info[0]; 448 if(!$h) $h = round(($w * $info[1]) / $info[0]); 449 if(!$w) $w = round(($h * $info[0]) / $info[1]); 450 451 // There is a two times image 452 if ( 2*2/3*$w <= $origWidth ) { // If the image is at least 1.6 times as large ... 453 $srcset[] = ml($src, array('w' => 2*$w, 'h' => 2*$h, 'cache' => $cache, 'rev'=>$this->_getLastMediaRevisionAt($src))) . ' 2x'; 454 } else { 455 456 // Check for alternate image 457 $ext = strrpos($src, '.'); 458 $additionalSrc = substr( $src, 0, $ext) . '@2x.' . substr($src, $ext+1); 459 460 $additionalInfo = @getimagesize(mediaFN($additionalSrc)); //get original size 461 if ( $additionalInfo !== false ) { 462 // Image exists 463 $srcset[] = ml($additionalSrc, array('w' => 2*$w, 'h' => 2*$h, 'cache' => $cache, 'rev'=>$this->_getLastMediaRevisionAt($srcSetURL))) . ' 2x'; 464 } 465 } 466 467 $ret = parent::_media($src, $title, $align, $w, $h, $cache, $render); 468 if ( count($srcset) > 0 ) { 469 return str_replace("/>", ' srcset="' . implode(',', $srcset) . '" />', $ret ); 470 } else { 471 return $ret; 472 } 473 } 474 } 475 476 return parent::_media($src, $title, $align, $w, $h, $cache, $render); 477 } 478} 479 480//Setup VIM: ex: et ts=4 enc=utf-8 : 481