1<?php 2if(!defined('DOKU_INC')) die('meh.'); 3if (!defined('DOKU_PARSER_EOL')) define('DOKU_PARSER_EOL',"\n"); // add this to make handling test cases simpler 4 5class Doku_Handler { 6 7 var $Renderer = null; 8 9 var $CallWriter = null; 10 11 var $calls = array(); 12 13 var $status = array( 14 'section' => false, 15 'doublequote' => 0, 16 ); 17 18 var $rewriteBlocks = true; 19 20 function __construct() { 21 $this->CallWriter = new Doku_Handler_CallWriter($this); 22 } 23 24 /** 25 * @param string $handler 26 * @param mixed $args 27 * @param integer|string $pos 28 */ 29 function _addCall($handler, $args, $pos) { 30 $call = array($handler,$args, $pos); 31 $this->CallWriter->writeCall($call); 32 } 33 34 function addPluginCall($plugin, $args, $state, $pos, $match) { 35 $call = array('plugin',array($plugin, $args, $state, $match), $pos); 36 $this->CallWriter->writeCall($call); 37 } 38 39 function _finalize(){ 40 41 $this->CallWriter->finalise(); 42 43 if ( $this->status['section'] ) { 44 $last_call = end($this->calls); 45 array_push($this->calls,array('section_close',array(), $last_call[2])); 46 } 47 48 if ( $this->rewriteBlocks ) { 49 $B = new Doku_Handler_Block(); 50 $this->calls = $B->process($this->calls); 51 } 52 53 trigger_event('PARSER_HANDLER_DONE',$this); 54 55 array_unshift($this->calls,array('document_start',array(),0)); 56 $last_call = end($this->calls); 57 array_push($this->calls,array('document_end',array(),$last_call[2])); 58 } 59 60 /** 61 * fetch the current call and advance the pointer to the next one 62 * 63 * @return bool|mixed 64 */ 65 function fetch() { 66 $call = current($this->calls); 67 if($call !== false) { 68 next($this->calls); //advance the pointer 69 return $call; 70 } 71 return false; 72 } 73 74 75 /** 76 * Special plugin handler 77 * 78 * This handler is called for all modes starting with 'plugin_'. 79 * An additional parameter with the plugin name is passed 80 * 81 * @author Andreas Gohr <andi@splitbrain.org> 82 * 83 * @param string|integer $match 84 * @param string|integer $state 85 * @param integer $pos 86 * @param $pluginname 87 * 88 * @return bool 89 */ 90 function plugin($match, $state, $pos, $pluginname){ 91 $data = array($match); 92 /** @var DokuWiki_Syntax_Plugin $plugin */ 93 $plugin = plugin_load('syntax',$pluginname); 94 if($plugin != null){ 95 $data = $plugin->handle($match, $state, $pos, $this); 96 } 97 if ($data !== false) { 98 $this->addPluginCall($pluginname,$data,$state,$pos,$match); 99 } 100 return true; 101 } 102 103 function base($match, $state, $pos) { 104 switch ( $state ) { 105 case DOKU_LEXER_UNMATCHED: 106 $this->_addCall('cdata',array($match), $pos); 107 return true; 108 break; 109 } 110 } 111 112 function header($match, $state, $pos) { 113 // get level and title 114 $title = trim($match); 115 $level = 7 - strspn($title,'='); 116 if($level < 1) $level = 1; 117 $title = trim($title,'='); 118 $title = trim($title); 119 120 if ($this->status['section']) $this->_addCall('section_close',array(),$pos); 121 122 $this->_addCall('header',array($title,$level,$pos), $pos); 123 124 $this->_addCall('section_open',array($level),$pos); 125 $this->status['section'] = true; 126 return true; 127 } 128 129 function notoc($match, $state, $pos) { 130 $this->_addCall('notoc',array(),$pos); 131 return true; 132 } 133 134 function nocache($match, $state, $pos) { 135 $this->_addCall('nocache',array(),$pos); 136 return true; 137 } 138 139 function linebreak($match, $state, $pos) { 140 $this->_addCall('linebreak',array(),$pos); 141 return true; 142 } 143 144 function eol($match, $state, $pos) { 145 $this->_addCall('eol',array(),$pos); 146 return true; 147 } 148 149 function hr($match, $state, $pos) { 150 $this->_addCall('hr',array(),$pos); 151 return true; 152 } 153 154 /** 155 * @param string|integer $match 156 * @param string|integer $state 157 * @param integer $pos 158 * @param string $name 159 */ 160 function _nestingTag($match, $state, $pos, $name) { 161 switch ( $state ) { 162 case DOKU_LEXER_ENTER: 163 $this->_addCall($name.'_open', array(), $pos); 164 break; 165 case DOKU_LEXER_EXIT: 166 $this->_addCall($name.'_close', array(), $pos); 167 break; 168 case DOKU_LEXER_UNMATCHED: 169 $this->_addCall('cdata',array($match), $pos); 170 break; 171 } 172 } 173 174 function strong($match, $state, $pos) { 175 $this->_nestingTag($match, $state, $pos, 'strong'); 176 return true; 177 } 178 179 function emphasis($match, $state, $pos) { 180 $this->_nestingTag($match, $state, $pos, 'emphasis'); 181 return true; 182 } 183 184 function underline($match, $state, $pos) { 185 $this->_nestingTag($match, $state, $pos, 'underline'); 186 return true; 187 } 188 189 function monospace($match, $state, $pos) { 190 $this->_nestingTag($match, $state, $pos, 'monospace'); 191 return true; 192 } 193 194 function subscript($match, $state, $pos) { 195 $this->_nestingTag($match, $state, $pos, 'subscript'); 196 return true; 197 } 198 199 function superscript($match, $state, $pos) { 200 $this->_nestingTag($match, $state, $pos, 'superscript'); 201 return true; 202 } 203 204 function deleted($match, $state, $pos) { 205 $this->_nestingTag($match, $state, $pos, 'deleted'); 206 return true; 207 } 208 209 210 function footnote($match, $state, $pos) { 211// $this->_nestingTag($match, $state, $pos, 'footnote'); 212 if (!isset($this->_footnote)) $this->_footnote = false; 213 214 switch ( $state ) { 215 case DOKU_LEXER_ENTER: 216 // footnotes can not be nested - however due to limitations in lexer it can't be prevented 217 // we will still enter a new footnote mode, we just do nothing 218 if ($this->_footnote) { 219 $this->_addCall('cdata',array($match), $pos); 220 break; 221 } 222 223 $this->_footnote = true; 224 225 $ReWriter = new Doku_Handler_Nest($this->CallWriter,'footnote_close'); 226 $this->CallWriter = & $ReWriter; 227 $this->_addCall('footnote_open', array(), $pos); 228 break; 229 case DOKU_LEXER_EXIT: 230 // check whether we have already exitted the footnote mode, can happen if the modes were nested 231 if (!$this->_footnote) { 232 $this->_addCall('cdata',array($match), $pos); 233 break; 234 } 235 236 $this->_footnote = false; 237 238 $this->_addCall('footnote_close', array(), $pos); 239 $this->CallWriter->process(); 240 $ReWriter = & $this->CallWriter; 241 $this->CallWriter = & $ReWriter->CallWriter; 242 break; 243 case DOKU_LEXER_UNMATCHED: 244 $this->_addCall('cdata', array($match), $pos); 245 break; 246 } 247 return true; 248 } 249 250 function listblock($match, $state, $pos) { 251 switch ( $state ) { 252 case DOKU_LEXER_ENTER: 253 $ReWriter = new Doku_Handler_List($this->CallWriter); 254 $this->CallWriter = & $ReWriter; 255 $this->_addCall('list_open', array($match), $pos); 256 break; 257 case DOKU_LEXER_EXIT: 258 $this->_addCall('list_close', array(), $pos); 259 $this->CallWriter->process(); 260 $ReWriter = & $this->CallWriter; 261 $this->CallWriter = & $ReWriter->CallWriter; 262 break; 263 case DOKU_LEXER_MATCHED: 264 $this->_addCall('list_item', array($match), $pos); 265 break; 266 case DOKU_LEXER_UNMATCHED: 267 $this->_addCall('cdata', array($match), $pos); 268 break; 269 } 270 return true; 271 } 272 273 function unformatted($match, $state, $pos) { 274 if ( $state == DOKU_LEXER_UNMATCHED ) { 275 $this->_addCall('unformatted',array($match), $pos); 276 } 277 return true; 278 } 279 280 function php($match, $state, $pos) { 281 global $conf; 282 if ( $state == DOKU_LEXER_UNMATCHED ) { 283 $this->_addCall('php',array($match), $pos); 284 } 285 return true; 286 } 287 288 function phpblock($match, $state, $pos) { 289 global $conf; 290 if ( $state == DOKU_LEXER_UNMATCHED ) { 291 $this->_addCall('phpblock',array($match), $pos); 292 } 293 return true; 294 } 295 296 function html($match, $state, $pos) { 297 global $conf; 298 if ( $state == DOKU_LEXER_UNMATCHED ) { 299 $this->_addCall('html',array($match), $pos); 300 } 301 return true; 302 } 303 304 function htmlblock($match, $state, $pos) { 305 global $conf; 306 if ( $state == DOKU_LEXER_UNMATCHED ) { 307 $this->_addCall('htmlblock',array($match), $pos); 308 } 309 return true; 310 } 311 312 function preformatted($match, $state, $pos) { 313 switch ( $state ) { 314 case DOKU_LEXER_ENTER: 315 $ReWriter = new Doku_Handler_Preformatted($this->CallWriter); 316 $this->CallWriter = $ReWriter; 317 $this->_addCall('preformatted_start',array(), $pos); 318 break; 319 case DOKU_LEXER_EXIT: 320 $this->_addCall('preformatted_end',array(), $pos); 321 $this->CallWriter->process(); 322 $ReWriter = & $this->CallWriter; 323 $this->CallWriter = & $ReWriter->CallWriter; 324 break; 325 case DOKU_LEXER_MATCHED: 326 $this->_addCall('preformatted_newline',array(), $pos); 327 break; 328 case DOKU_LEXER_UNMATCHED: 329 $this->_addCall('preformatted_content',array($match), $pos); 330 break; 331 } 332 333 return true; 334 } 335 336 function quote($match, $state, $pos) { 337 338 switch ( $state ) { 339 340 case DOKU_LEXER_ENTER: 341 $ReWriter = new Doku_Handler_Quote($this->CallWriter); 342 $this->CallWriter = & $ReWriter; 343 $this->_addCall('quote_start',array($match), $pos); 344 break; 345 346 case DOKU_LEXER_EXIT: 347 $this->_addCall('quote_end',array(), $pos); 348 $this->CallWriter->process(); 349 $ReWriter = & $this->CallWriter; 350 $this->CallWriter = & $ReWriter->CallWriter; 351 break; 352 353 case DOKU_LEXER_MATCHED: 354 $this->_addCall('quote_newline',array($match), $pos); 355 break; 356 357 case DOKU_LEXER_UNMATCHED: 358 $this->_addCall('cdata',array($match), $pos); 359 break; 360 361 } 362 363 return true; 364 } 365 366 function file($match, $state, $pos) { 367 return $this->code($match, $state, $pos, 'file'); 368 } 369 370 function code($match, $state, $pos, $type='code') { 371 if ( $state == DOKU_LEXER_UNMATCHED ) { 372 $matches = explode('>',$match,2); 373 374 $param = preg_split('/\s+/', $matches[0], 2, PREG_SPLIT_NO_EMPTY); 375 while(count($param) < 2) array_push($param, null); 376 377 // We shortcut html here. 378 if ($param[0] == 'html') $param[0] = 'html4strict'; 379 if ($param[0] == '-') $param[0] = null; 380 array_unshift($param, $matches[1]); 381 382 $this->_addCall($type, $param, $pos); 383 } 384 return true; 385 } 386 387 function acronym($match, $state, $pos) { 388 $this->_addCall('acronym',array($match), $pos); 389 return true; 390 } 391 392 function smiley($match, $state, $pos) { 393 $this->_addCall('smiley',array($match), $pos); 394 return true; 395 } 396 397 function wordblock($match, $state, $pos) { 398 $this->_addCall('wordblock',array($match), $pos); 399 return true; 400 } 401 402 function entity($match, $state, $pos) { 403 $this->_addCall('entity',array($match), $pos); 404 return true; 405 } 406 407 function multiplyentity($match, $state, $pos) { 408 preg_match_all('/\d+/',$match,$matches); 409 $this->_addCall('multiplyentity',array($matches[0][0],$matches[0][1]), $pos); 410 return true; 411 } 412 413 function singlequoteopening($match, $state, $pos) { 414 $this->_addCall('singlequoteopening',array(), $pos); 415 return true; 416 } 417 418 function singlequoteclosing($match, $state, $pos) { 419 $this->_addCall('singlequoteclosing',array(), $pos); 420 return true; 421 } 422 423 function apostrophe($match, $state, $pos) { 424 $this->_addCall('apostrophe',array(), $pos); 425 return true; 426 } 427 428 function doublequoteopening($match, $state, $pos) { 429 $this->_addCall('doublequoteopening',array(), $pos); 430 $this->status['doublequote']++; 431 return true; 432 } 433 434 function doublequoteclosing($match, $state, $pos) { 435 if ($this->status['doublequote'] <= 0) { 436 $this->doublequoteopening($match, $state, $pos); 437 } else { 438 $this->_addCall('doublequoteclosing',array(), $pos); 439 $this->status['doublequote'] = max(0, --$this->status['doublequote']); 440 } 441 return true; 442 } 443 444 function camelcaselink($match, $state, $pos) { 445 $this->_addCall('camelcaselink',array($match), $pos); 446 return true; 447 } 448 449 /* 450 */ 451 function internallink($match, $state, $pos) { 452 // Strip the opening and closing markup 453 $link = preg_replace(array('/^\[\[/','/\]\]$/u'),'',$match); 454 455 // Split title from URL 456 $link = explode('|',$link,2); 457 if ( !isset($link[1]) ) { 458 $link[1] = null; 459 } else if ( preg_match('/^\{\{[^\}]+\}\}$/',$link[1]) ) { 460 // If the title is an image, convert it to an array containing the image details 461 $link[1] = Doku_Handler_Parse_Media($link[1]); 462 } 463 $link[0] = trim($link[0]); 464 465 //decide which kind of link it is 466 467 if ( link_isinterwiki($link[0]) ) { 468 // Interwiki 469 $interwiki = explode('>',$link[0],2); 470 $this->_addCall( 471 'interwikilink', 472 array($link[0],$link[1],strtolower($interwiki[0]),$interwiki[1]), 473 $pos 474 ); 475 }elseif ( preg_match('/^\\\\\\\\[^\\\\]+?\\\\/u',$link[0]) ) { 476 // Windows Share 477 $this->_addCall( 478 'windowssharelink', 479 array($link[0],$link[1]), 480 $pos 481 ); 482 }elseif ( preg_match('#^([a-z0-9\-\.+]+?)://#i',$link[0]) ) { 483 // external link (accepts all protocols) 484 $this->_addCall( 485 'externallink', 486 array($link[0],$link[1]), 487 $pos 488 ); 489 }elseif ( preg_match('<'.PREG_PATTERN_VALID_EMAIL.'>',$link[0]) ) { 490 // E-Mail (pattern above is defined in inc/mail.php) 491 $this->_addCall( 492 'emaillink', 493 array($link[0],$link[1]), 494 $pos 495 ); 496 }elseif ( preg_match('!^#.+!',$link[0]) ){ 497 // local link 498 $this->_addCall( 499 'locallink', 500 array(substr($link[0],1),$link[1]), 501 $pos 502 ); 503 }else{ 504 // internal link 505 $this->_addCall( 506 'internallink', 507 array($link[0],$link[1]), 508 $pos 509 ); 510 } 511 512 return true; 513 } 514 515 function filelink($match, $state, $pos) { 516 $this->_addCall('filelink',array($match, null), $pos); 517 return true; 518 } 519 520 function windowssharelink($match, $state, $pos) { 521 $this->_addCall('windowssharelink',array($match, null), $pos); 522 return true; 523 } 524 525 function media($match, $state, $pos) { 526 $p = Doku_Handler_Parse_Media($match); 527 528 $this->_addCall( 529 $p['type'], 530 array($p['src'], $p['title'], $p['align'], $p['width'], 531 $p['height'], $p['cache'], $p['linking']), 532 $pos 533 ); 534 return true; 535 } 536 537 function rss($match, $state, $pos) { 538 $link = preg_replace(array('/^\{\{rss>/','/\}\}$/'),'',$match); 539 540 // get params 541 list($link,$params) = explode(' ',$link,2); 542 543 $p = array(); 544 if(preg_match('/\b(\d+)\b/',$params,$match)){ 545 $p['max'] = $match[1]; 546 }else{ 547 $p['max'] = 8; 548 } 549 $p['reverse'] = (preg_match('/rev/',$params)); 550 $p['author'] = (preg_match('/\b(by|author)/',$params)); 551 $p['date'] = (preg_match('/\b(date)/',$params)); 552 $p['details'] = (preg_match('/\b(desc|detail)/',$params)); 553 $p['nosort'] = (preg_match('/\b(nosort)\b/',$params)); 554 555 if (preg_match('/\b(\d+)([dhm])\b/',$params,$match)) { 556 $period = array('d' => 86400, 'h' => 3600, 'm' => 60); 557 $p['refresh'] = max(600,$match[1]*$period[$match[2]]); // n * period in seconds, minimum 10 minutes 558 } else { 559 $p['refresh'] = 14400; // default to 4 hours 560 } 561 562 $this->_addCall('rss',array($link,$p),$pos); 563 return true; 564 } 565 566 function externallink($match, $state, $pos) { 567 $url = $match; 568 $title = null; 569 570 // add protocol on simple short URLs 571 if(substr($url,0,3) == 'ftp' && (substr($url,0,6) != 'ftp://')){ 572 $title = $url; 573 $url = 'ftp://'.$url; 574 } 575 if(substr($url,0,3) == 'www' && (substr($url,0,7) != 'http://')){ 576 $title = $url; 577 $url = 'http://'.$url; 578 } 579 580 $this->_addCall('externallink',array($url, $title), $pos); 581 return true; 582 } 583 584 function emaillink($match, $state, $pos) { 585 $email = preg_replace(array('/^</','/>$/'),'',$match); 586 $this->_addCall('emaillink',array($email, null), $pos); 587 return true; 588 } 589 590 function table($match, $state, $pos) { 591 switch ( $state ) { 592 593 case DOKU_LEXER_ENTER: 594 595 $ReWriter = new Doku_Handler_Table($this->CallWriter); 596 $this->CallWriter = & $ReWriter; 597 598 $this->_addCall('table_start', array($pos + 1), $pos); 599 if ( trim($match) == '^' ) { 600 $this->_addCall('tableheader', array(), $pos); 601 } else { 602 $this->_addCall('tablecell', array(), $pos); 603 } 604 break; 605 606 case DOKU_LEXER_EXIT: 607 $this->_addCall('table_end', array($pos), $pos); 608 $this->CallWriter->process(); 609 $ReWriter = & $this->CallWriter; 610 $this->CallWriter = & $ReWriter->CallWriter; 611 break; 612 613 case DOKU_LEXER_UNMATCHED: 614 if ( trim($match) != '' ) { 615 $this->_addCall('cdata',array($match), $pos); 616 } 617 break; 618 619 case DOKU_LEXER_MATCHED: 620 if ( $match == ' ' ){ 621 $this->_addCall('cdata', array($match), $pos); 622 } else if ( preg_match('/:::/',$match) ) { 623 $this->_addCall('rowspan', array($match), $pos); 624 } else if ( preg_match('/\t+/',$match) ) { 625 $this->_addCall('table_align', array($match), $pos); 626 } else if ( preg_match('/ {2,}/',$match) ) { 627 $this->_addCall('table_align', array($match), $pos); 628 } else if ( $match == "\n|" ) { 629 $this->_addCall('table_row', array(), $pos); 630 $this->_addCall('tablecell', array(), $pos); 631 } else if ( $match == "\n^" ) { 632 $this->_addCall('table_row', array(), $pos); 633 $this->_addCall('tableheader', array(), $pos); 634 } else if ( $match == '|' ) { 635 $this->_addCall('tablecell', array(), $pos); 636 } else if ( $match == '^' ) { 637 $this->_addCall('tableheader', array(), $pos); 638 } 639 break; 640 } 641 return true; 642 } 643} 644 645//------------------------------------------------------------------------ 646function Doku_Handler_Parse_Media($match) { 647 648 // Strip the opening and closing markup 649 $link = preg_replace(array('/^\{\{/','/\}\}$/u'),'',$match); 650 651 // Split title from URL 652 $link = explode('|',$link,2); 653 654 // Check alignment 655 $ralign = (bool)preg_match('/^ /',$link[0]); 656 $lalign = (bool)preg_match('/ $/',$link[0]); 657 658 // Logic = what's that ;)... 659 if ( $lalign & $ralign ) { 660 $align = 'center'; 661 } else if ( $ralign ) { 662 $align = 'right'; 663 } else if ( $lalign ) { 664 $align = 'left'; 665 } else { 666 $align = null; 667 } 668 669 // The title... 670 if ( !isset($link[1]) ) { 671 $link[1] = null; 672 } 673 674 //remove aligning spaces 675 $link[0] = trim($link[0]); 676 677 //split into src and parameters (using the very last questionmark) 678 $pos = strrpos($link[0], '?'); 679 if($pos !== false){ 680 $src = substr($link[0],0,$pos); 681 $param = substr($link[0],$pos+1); 682 }else{ 683 $src = $link[0]; 684 $param = ''; 685 } 686 687 //parse width and height 688 if(preg_match('#(\d+)(x(\d+))?#i',$param,$size)){ 689 !empty($size[1]) ? $w = $size[1] : $w = null; 690 !empty($size[3]) ? $h = $size[3] : $h = null; 691 } else { 692 $w = null; 693 $h = null; 694 } 695 696 //get linking command 697 if(preg_match('/nolink/i',$param)){ 698 $linking = 'nolink'; 699 }else if(preg_match('/direct/i',$param)){ 700 $linking = 'direct'; 701 }else if(preg_match('/linkonly/i',$param)){ 702 $linking = 'linkonly'; 703 }else{ 704 $linking = 'details'; 705 } 706 707 //get caching command 708 if (preg_match('/(nocache|recache)/i',$param,$cachemode)){ 709 $cache = $cachemode[1]; 710 }else{ 711 $cache = 'cache'; 712 } 713 714 // Check whether this is a local or remote image or interwiki 715 if (media_isexternal($src) || link_isinterwiki($src)){ 716 $call = 'externalmedia'; 717 } else { 718 $call = 'internalmedia'; 719 } 720 721 $params = array( 722 'type'=>$call, 723 'src'=>$src, 724 'title'=>$link[1], 725 'align'=>$align, 726 'width'=>$w, 727 'height'=>$h, 728 'cache'=>$cache, 729 'linking'=>$linking, 730 ); 731 732 return $params; 733} 734 735//------------------------------------------------------------------------ 736interface Doku_Handler_CallWriter_Interface { 737 public function writeCall($call); 738 public function writeCalls($calls); 739 public function finalise(); 740} 741 742class Doku_Handler_CallWriter implements Doku_Handler_CallWriter_Interface { 743 744 var $Handler; 745 746 /** 747 * @param Doku_Handler $Handler 748 */ 749 function __construct(Doku_Handler $Handler) { 750 $this->Handler = $Handler; 751 } 752 753 function writeCall($call) { 754 $this->Handler->calls[] = $call; 755 } 756 757 function writeCalls($calls) { 758 $this->Handler->calls = array_merge($this->Handler->calls, $calls); 759 } 760 761 // function is required, but since this call writer is first/highest in 762 // the chain it is not required to do anything 763 function finalise() { 764 unset($this->Handler); 765 } 766} 767 768//------------------------------------------------------------------------ 769/** 770 * Generic call writer class to handle nesting of rendering instructions 771 * within a render instruction. Also see nest() method of renderer base class 772 * 773 * @author Chris Smith <chris@jalakai.co.uk> 774 */ 775class Doku_Handler_Nest implements Doku_Handler_CallWriter_Interface { 776 777 var $CallWriter; 778 var $calls = array(); 779 780 var $closingInstruction; 781 782 /** 783 * constructor 784 * 785 * @param Doku_Handler_CallWriter $CallWriter the renderers current call writer 786 * @param string $close closing instruction name, this is required to properly terminate the 787 * syntax mode if the document ends without a closing pattern 788 */ 789 function __construct(Doku_Handler_CallWriter_Interface $CallWriter, $close="nest_close") { 790 $this->CallWriter = $CallWriter; 791 792 $this->closingInstruction = $close; 793 } 794 795 function writeCall($call) { 796 $this->calls[] = $call; 797 } 798 799 function writeCalls($calls) { 800 $this->calls = array_merge($this->calls, $calls); 801 } 802 803 function finalise() { 804 $last_call = end($this->calls); 805 $this->writeCall(array($this->closingInstruction,array(), $last_call[2])); 806 807 $this->process(); 808 $this->CallWriter->finalise(); 809 unset($this->CallWriter); 810 } 811 812 function process() { 813 // merge consecutive cdata 814 $unmerged_calls = $this->calls; 815 $this->calls = array(); 816 817 foreach ($unmerged_calls as $call) $this->addCall($call); 818 819 $first_call = reset($this->calls); 820 $this->CallWriter->writeCall(array("nest", array($this->calls), $first_call[2])); 821 } 822 823 function addCall($call) { 824 $key = count($this->calls); 825 if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) { 826 $this->calls[$key-1][1][0] .= $call[1][0]; 827 } else if ($call[0] == 'eol') { 828 // do nothing (eol shouldn't be allowed, to counter preformatted fix in #1652 & #1699) 829 } else { 830 $this->calls[] = $call; 831 } 832 } 833} 834 835class Doku_Handler_List implements Doku_Handler_CallWriter_Interface { 836 837 var $CallWriter; 838 839 var $calls = array(); 840 var $listCalls = array(); 841 var $listStack = array(); 842 843 const NODE = 1; 844 845 function __construct(Doku_Handler_CallWriter_Interface $CallWriter) { 846 $this->CallWriter = $CallWriter; 847 } 848 849 function writeCall($call) { 850 $this->calls[] = $call; 851 } 852 853 // Probably not needed but just in case... 854 function writeCalls($calls) { 855 $this->calls = array_merge($this->calls, $calls); 856# $this->CallWriter->writeCalls($this->calls); 857 } 858 859 function finalise() { 860 $last_call = end($this->calls); 861 $this->writeCall(array('list_close',array(), $last_call[2])); 862 863 $this->process(); 864 $this->CallWriter->finalise(); 865 unset($this->CallWriter); 866 } 867 868 //------------------------------------------------------------------------ 869 function process() { 870 871 foreach ( $this->calls as $call ) { 872 switch ($call[0]) { 873 case 'list_item': 874 $this->listOpen($call); 875 break; 876 case 'list_open': 877 $this->listStart($call); 878 break; 879 case 'list_close': 880 $this->listEnd($call); 881 break; 882 default: 883 $this->listContent($call); 884 break; 885 } 886 } 887 888 $this->CallWriter->writeCalls($this->listCalls); 889 } 890 891 //------------------------------------------------------------------------ 892 function listStart($call) { 893 $depth = $this->interpretSyntax($call[1][0], $listType); 894 895 $this->initialDepth = $depth; 896 // array(list type, current depth, index of current listitem_open) 897 $this->listStack[] = array($listType, $depth, 1); 898 899 $this->listCalls[] = array('list'.$listType.'_open',array(),$call[2]); 900 $this->listCalls[] = array('listitem_open',array(1),$call[2]); 901 $this->listCalls[] = array('listcontent_open',array(),$call[2]); 902 } 903 904 //------------------------------------------------------------------------ 905 function listEnd($call) { 906 $closeContent = true; 907 908 while ( $list = array_pop($this->listStack) ) { 909 if ( $closeContent ) { 910 $this->listCalls[] = array('listcontent_close',array(),$call[2]); 911 $closeContent = false; 912 } 913 $this->listCalls[] = array('listitem_close',array(),$call[2]); 914 $this->listCalls[] = array('list'.$list[0].'_close', array(), $call[2]); 915 } 916 } 917 918 //------------------------------------------------------------------------ 919 function listOpen($call) { 920 $depth = $this->interpretSyntax($call[1][0], $listType); 921 $end = end($this->listStack); 922 $key = key($this->listStack); 923 924 // Not allowed to be shallower than initialDepth 925 if ( $depth < $this->initialDepth ) { 926 $depth = $this->initialDepth; 927 } 928 929 //------------------------------------------------------------------------ 930 if ( $depth == $end[1] ) { 931 932 // Just another item in the list... 933 if ( $listType == $end[0] ) { 934 $this->listCalls[] = array('listcontent_close',array(),$call[2]); 935 $this->listCalls[] = array('listitem_close',array(),$call[2]); 936 $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]); 937 $this->listCalls[] = array('listcontent_open',array(),$call[2]); 938 939 // new list item, update list stack's index into current listitem_open 940 $this->listStack[$key][2] = count($this->listCalls) - 2; 941 942 // Switched list type... 943 } else { 944 945 $this->listCalls[] = array('listcontent_close',array(),$call[2]); 946 $this->listCalls[] = array('listitem_close',array(),$call[2]); 947 $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]); 948 $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]); 949 $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]); 950 $this->listCalls[] = array('listcontent_open',array(),$call[2]); 951 952 array_pop($this->listStack); 953 $this->listStack[] = array($listType, $depth, count($this->listCalls) - 2); 954 } 955 956 //------------------------------------------------------------------------ 957 // Getting deeper... 958 } else if ( $depth > $end[1] ) { 959 960 $this->listCalls[] = array('listcontent_close',array(),$call[2]); 961 $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]); 962 $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]); 963 $this->listCalls[] = array('listcontent_open',array(),$call[2]); 964 965 // set the node/leaf state of this item's parent listitem_open to NODE 966 $this->listCalls[$this->listStack[$key][2]][1][1] = self::NODE; 967 968 $this->listStack[] = array($listType, $depth, count($this->listCalls) - 2); 969 970 //------------------------------------------------------------------------ 971 // Getting shallower ( $depth < $end[1] ) 972 } else { 973 $this->listCalls[] = array('listcontent_close',array(),$call[2]); 974 $this->listCalls[] = array('listitem_close',array(),$call[2]); 975 $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]); 976 977 // Throw away the end - done 978 array_pop($this->listStack); 979 980 while (1) { 981 $end = end($this->listStack); 982 $key = key($this->listStack); 983 984 if ( $end[1] <= $depth ) { 985 986 // Normalize depths 987 $depth = $end[1]; 988 989 $this->listCalls[] = array('listitem_close',array(),$call[2]); 990 991 if ( $end[0] == $listType ) { 992 $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]); 993 $this->listCalls[] = array('listcontent_open',array(),$call[2]); 994 995 // new list item, update list stack's index into current listitem_open 996 $this->listStack[$key][2] = count($this->listCalls) - 2; 997 998 } else { 999 // Switching list type... 1000 $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]); 1001 $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]); 1002 $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]); 1003 $this->listCalls[] = array('listcontent_open',array(),$call[2]); 1004 1005 array_pop($this->listStack); 1006 $this->listStack[] = array($listType, $depth, count($this->listCalls) - 2); 1007 } 1008 1009 break; 1010 1011 // Haven't dropped down far enough yet.... ( $end[1] > $depth ) 1012 } else { 1013 1014 $this->listCalls[] = array('listitem_close',array(),$call[2]); 1015 $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]); 1016 1017 array_pop($this->listStack); 1018 1019 } 1020 1021 } 1022 1023 } 1024 } 1025 1026 //------------------------------------------------------------------------ 1027 function listContent($call) { 1028 $this->listCalls[] = $call; 1029 } 1030 1031 //------------------------------------------------------------------------ 1032 function interpretSyntax($match, & $type) { 1033 if ( substr($match,-1) == '*' ) { 1034 $type = 'u'; 1035 } else { 1036 $type = 'o'; 1037 } 1038 // Is the +1 needed? It used to be count(explode(...)) 1039 // but I don't think the number is seen outside this handler 1040 return substr_count(str_replace("\t",' ',$match), ' ') + 1; 1041 } 1042} 1043 1044//------------------------------------------------------------------------ 1045class Doku_Handler_Preformatted implements Doku_Handler_CallWriter_Interface { 1046 1047 var $CallWriter; 1048 1049 var $calls = array(); 1050 var $pos; 1051 var $text =''; 1052 1053 1054 1055 function __construct(Doku_Handler_CallWriter_Interface $CallWriter) { 1056 $this->CallWriter = $CallWriter; 1057 } 1058 1059 function writeCall($call) { 1060 $this->calls[] = $call; 1061 } 1062 1063 // Probably not needed but just in case... 1064 function writeCalls($calls) { 1065 $this->calls = array_merge($this->calls, $calls); 1066# $this->CallWriter->writeCalls($this->calls); 1067 } 1068 1069 function finalise() { 1070 $last_call = end($this->calls); 1071 $this->writeCall(array('preformatted_end',array(), $last_call[2])); 1072 1073 $this->process(); 1074 $this->CallWriter->finalise(); 1075 unset($this->CallWriter); 1076 } 1077 1078 function process() { 1079 foreach ( $this->calls as $call ) { 1080 switch ($call[0]) { 1081 case 'preformatted_start': 1082 $this->pos = $call[2]; 1083 break; 1084 case 'preformatted_newline': 1085 $this->text .= "\n"; 1086 break; 1087 case 'preformatted_content': 1088 $this->text .= $call[1][0]; 1089 break; 1090 case 'preformatted_end': 1091 if (trim($this->text)) { 1092 $this->CallWriter->writeCall(array('preformatted',array($this->text),$this->pos)); 1093 } 1094 // see FS#1699 & FS#1652, add 'eol' instructions to ensure proper triggering of following p_open 1095 $this->CallWriter->writeCall(array('eol',array(),$this->pos)); 1096 $this->CallWriter->writeCall(array('eol',array(),$this->pos)); 1097 break; 1098 } 1099 } 1100 } 1101 1102} 1103 1104//------------------------------------------------------------------------ 1105class Doku_Handler_Quote implements Doku_Handler_CallWriter_Interface { 1106 1107 var $CallWriter; 1108 1109 var $calls = array(); 1110 1111 var $quoteCalls = array(); 1112 1113 function __construct(Doku_Handler_CallWriter_Interface $CallWriter) { 1114 $this->CallWriter = $CallWriter; 1115 } 1116 1117 function writeCall($call) { 1118 $this->calls[] = $call; 1119 } 1120 1121 // Probably not needed but just in case... 1122 function writeCalls($calls) { 1123 $this->calls = array_merge($this->calls, $calls); 1124 } 1125 1126 function finalise() { 1127 $last_call = end($this->calls); 1128 $this->writeCall(array('quote_end',array(), $last_call[2])); 1129 1130 $this->process(); 1131 $this->CallWriter->finalise(); 1132 unset($this->CallWriter); 1133 } 1134 1135 function process() { 1136 1137 $quoteDepth = 1; 1138 1139 foreach ( $this->calls as $call ) { 1140 switch ($call[0]) { 1141 1142 case 'quote_start': 1143 1144 $this->quoteCalls[] = array('quote_open',array(),$call[2]); 1145 1146 case 'quote_newline': 1147 1148 $quoteLength = $this->getDepth($call[1][0]); 1149 1150 if ( $quoteLength > $quoteDepth ) { 1151 $quoteDiff = $quoteLength - $quoteDepth; 1152 for ( $i = 1; $i <= $quoteDiff; $i++ ) { 1153 $this->quoteCalls[] = array('quote_open',array(),$call[2]); 1154 } 1155 } else if ( $quoteLength < $quoteDepth ) { 1156 $quoteDiff = $quoteDepth - $quoteLength; 1157 for ( $i = 1; $i <= $quoteDiff; $i++ ) { 1158 $this->quoteCalls[] = array('quote_close',array(),$call[2]); 1159 } 1160 } else { 1161 if ($call[0] != 'quote_start') $this->quoteCalls[] = array('linebreak',array(),$call[2]); 1162 } 1163 1164 $quoteDepth = $quoteLength; 1165 1166 break; 1167 1168 case 'quote_end': 1169 1170 if ( $quoteDepth > 1 ) { 1171 $quoteDiff = $quoteDepth - 1; 1172 for ( $i = 1; $i <= $quoteDiff; $i++ ) { 1173 $this->quoteCalls[] = array('quote_close',array(),$call[2]); 1174 } 1175 } 1176 1177 $this->quoteCalls[] = array('quote_close',array(),$call[2]); 1178 1179 $this->CallWriter->writeCalls($this->quoteCalls); 1180 break; 1181 1182 default: 1183 $this->quoteCalls[] = $call; 1184 break; 1185 } 1186 } 1187 } 1188 1189 function getDepth($marker) { 1190 preg_match('/>{1,}/', $marker, $matches); 1191 $quoteLength = strlen($matches[0]); 1192 return $quoteLength; 1193 } 1194} 1195 1196//------------------------------------------------------------------------ 1197class Doku_Handler_Table implements Doku_Handler_CallWriter_Interface { 1198 1199 var $CallWriter; 1200 1201 var $calls = array(); 1202 var $tableCalls = array(); 1203 var $maxCols = 0; 1204 var $maxRows = 1; 1205 var $currentCols = 0; 1206 var $firstCell = false; 1207 var $lastCellType = 'tablecell'; 1208 var $inTableHead = true; 1209 var $currentRow = array('tableheader' => 0, 'tablecell' => 0); 1210 var $countTableHeadRows = 0; 1211 1212 function __construct(Doku_Handler_CallWriter_Interface $CallWriter) { 1213 $this->CallWriter = $CallWriter; 1214 } 1215 1216 function writeCall($call) { 1217 $this->calls[] = $call; 1218 } 1219 1220 // Probably not needed but just in case... 1221 function writeCalls($calls) { 1222 $this->calls = array_merge($this->calls, $calls); 1223 } 1224 1225 function finalise() { 1226 $last_call = end($this->calls); 1227 $this->writeCall(array('table_end',array(), $last_call[2])); 1228 1229 $this->process(); 1230 $this->CallWriter->finalise(); 1231 unset($this->CallWriter); 1232 } 1233 1234 //------------------------------------------------------------------------ 1235 function process() { 1236 foreach ( $this->calls as $call ) { 1237 switch ( $call[0] ) { 1238 case 'table_start': 1239 $this->tableStart($call); 1240 break; 1241 case 'table_row': 1242 $this->tableRowClose($call); 1243 $this->tableRowOpen(array('tablerow_open',$call[1],$call[2])); 1244 break; 1245 case 'tableheader': 1246 case 'tablecell': 1247 $this->tableCell($call); 1248 break; 1249 case 'table_end': 1250 $this->tableRowClose($call); 1251 $this->tableEnd($call); 1252 break; 1253 default: 1254 $this->tableDefault($call); 1255 break; 1256 } 1257 } 1258 $this->CallWriter->writeCalls($this->tableCalls); 1259 } 1260 1261 function tableStart($call) { 1262 $this->tableCalls[] = array('table_open',$call[1],$call[2]); 1263 $this->tableCalls[] = array('tablerow_open',array(),$call[2]); 1264 $this->firstCell = true; 1265 } 1266 1267 function tableEnd($call) { 1268 $this->tableCalls[] = array('table_close',$call[1],$call[2]); 1269 $this->finalizeTable(); 1270 } 1271 1272 function tableRowOpen($call) { 1273 $this->tableCalls[] = $call; 1274 $this->currentCols = 0; 1275 $this->firstCell = true; 1276 $this->lastCellType = 'tablecell'; 1277 $this->maxRows++; 1278 if ($this->inTableHead) { 1279 $this->currentRow = array('tablecell' => 0, 'tableheader' => 0); 1280 } 1281 } 1282 1283 function tableRowClose($call) { 1284 if ($this->inTableHead && ($this->inTableHead = $this->isTableHeadRow())) { 1285 $this->countTableHeadRows++; 1286 } 1287 // Strip off final cell opening and anything after it 1288 while ( $discard = array_pop($this->tableCalls ) ) { 1289 1290 if ( $discard[0] == 'tablecell_open' || $discard[0] == 'tableheader_open') { 1291 break; 1292 } 1293 if (!empty($this->currentRow[$discard[0]])) { 1294 $this->currentRow[$discard[0]]--; 1295 } 1296 } 1297 $this->tableCalls[] = array('tablerow_close', array(), $call[2]); 1298 1299 if ( $this->currentCols > $this->maxCols ) { 1300 $this->maxCols = $this->currentCols; 1301 } 1302 } 1303 1304 function isTableHeadRow() { 1305 $td = $this->currentRow['tablecell']; 1306 $th = $this->currentRow['tableheader']; 1307 1308 if (!$th || $td > 2) return false; 1309 if (2*$td > $th) return false; 1310 1311 return true; 1312 } 1313 1314 function tableCell($call) { 1315 if ($this->inTableHead) { 1316 $this->currentRow[$call[0]]++; 1317 } 1318 if ( !$this->firstCell ) { 1319 1320 // Increase the span 1321 $lastCall = end($this->tableCalls); 1322 1323 // A cell call which follows an open cell means an empty cell so span 1324 if ( $lastCall[0] == 'tablecell_open' || $lastCall[0] == 'tableheader_open' ) { 1325 $this->tableCalls[] = array('colspan',array(),$call[2]); 1326 1327 } 1328 1329 $this->tableCalls[] = array($this->lastCellType.'_close',array(),$call[2]); 1330 $this->tableCalls[] = array($call[0].'_open',array(1,null,1),$call[2]); 1331 $this->lastCellType = $call[0]; 1332 1333 } else { 1334 1335 $this->tableCalls[] = array($call[0].'_open',array(1,null,1),$call[2]); 1336 $this->lastCellType = $call[0]; 1337 $this->firstCell = false; 1338 1339 } 1340 1341 $this->currentCols++; 1342 } 1343 1344 function tableDefault($call) { 1345 $this->tableCalls[] = $call; 1346 } 1347 1348 function finalizeTable() { 1349 1350 // Add the max cols and rows to the table opening 1351 if ( $this->tableCalls[0][0] == 'table_open' ) { 1352 // Adjust to num cols not num col delimeters 1353 $this->tableCalls[0][1][] = $this->maxCols - 1; 1354 $this->tableCalls[0][1][] = $this->maxRows; 1355 $this->tableCalls[0][1][] = array_shift($this->tableCalls[0][1]); 1356 } else { 1357 trigger_error('First element in table call list is not table_open'); 1358 } 1359 1360 $lastRow = 0; 1361 $lastCell = 0; 1362 $cellKey = array(); 1363 $toDelete = array(); 1364 1365 // if still in tableheader, then there can be no table header 1366 // as all rows can't be within <THEAD> 1367 if ($this->inTableHead) { 1368 $this->inTableHead = false; 1369 $this->countTableHeadRows = 0; 1370 } 1371 1372 // Look for the colspan elements and increment the colspan on the 1373 // previous non-empty opening cell. Once done, delete all the cells 1374 // that contain colspans 1375 for ($key = 0 ; $key < count($this->tableCalls) ; ++$key) { 1376 $call = $this->tableCalls[$key]; 1377 1378 switch ($call[0]) { 1379 case 'table_open' : 1380 if($this->countTableHeadRows) { 1381 array_splice($this->tableCalls, $key+1, 0, array( 1382 array('tablethead_open', array(), $call[2])) 1383 ); 1384 } 1385 break; 1386 1387 case 'tablerow_open': 1388 1389 $lastRow++; 1390 $lastCell = 0; 1391 break; 1392 1393 case 'tablecell_open': 1394 case 'tableheader_open': 1395 1396 $lastCell++; 1397 $cellKey[$lastRow][$lastCell] = $key; 1398 break; 1399 1400 case 'table_align': 1401 1402 $prev = in_array($this->tableCalls[$key-1][0], array('tablecell_open', 'tableheader_open')); 1403 $next = in_array($this->tableCalls[$key+1][0], array('tablecell_close', 'tableheader_close')); 1404 // If the cell is empty, align left 1405 if ($prev && $next) { 1406 $this->tableCalls[$key-1][1][1] = 'left'; 1407 1408 // If the previous element was a cell open, align right 1409 } elseif ($prev) { 1410 $this->tableCalls[$key-1][1][1] = 'right'; 1411 1412 // If the next element is the close of an element, align either center or left 1413 } elseif ( $next) { 1414 if ( $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] == 'right' ) { 1415 $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] = 'center'; 1416 } else { 1417 $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] = 'left'; 1418 } 1419 1420 } 1421 1422 // Now convert the whitespace back to cdata 1423 $this->tableCalls[$key][0] = 'cdata'; 1424 break; 1425 1426 case 'colspan': 1427 1428 $this->tableCalls[$key-1][1][0] = false; 1429 1430 for($i = $key-2; $i >= $cellKey[$lastRow][1]; $i--) { 1431 1432 if ( $this->tableCalls[$i][0] == 'tablecell_open' || $this->tableCalls[$i][0] == 'tableheader_open' ) { 1433 1434 if ( false !== $this->tableCalls[$i][1][0] ) { 1435 $this->tableCalls[$i][1][0]++; 1436 break; 1437 } 1438 1439 } 1440 } 1441 1442 $toDelete[] = $key-1; 1443 $toDelete[] = $key; 1444 $toDelete[] = $key+1; 1445 break; 1446 1447 case 'rowspan': 1448 1449 if ( $this->tableCalls[$key-1][0] == 'cdata' ) { 1450 // ignore rowspan if previous call was cdata (text mixed with :::) we don't have to check next call as that wont match regex 1451 $this->tableCalls[$key][0] = 'cdata'; 1452 1453 } else { 1454 1455 $spanning_cell = null; 1456 1457 // can't cross thead/tbody boundary 1458 if (!$this->countTableHeadRows || ($lastRow-1 != $this->countTableHeadRows)) { 1459 for($i = $lastRow-1; $i > 0; $i--) { 1460 1461 if ( $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tablecell_open' || $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tableheader_open' ) { 1462 1463 if ($this->tableCalls[$cellKey[$i][$lastCell]][1][2] >= $lastRow - $i) { 1464 $spanning_cell = $i; 1465 break; 1466 } 1467 1468 } 1469 } 1470 } 1471 if (is_null($spanning_cell)) { 1472 // No spanning cell found, so convert this cell to 1473 // an empty one to avoid broken tables 1474 $this->tableCalls[$key][0] = 'cdata'; 1475 $this->tableCalls[$key][1][0] = ''; 1476 continue; 1477 } 1478 $this->tableCalls[$cellKey[$spanning_cell][$lastCell]][1][2]++; 1479 1480 $this->tableCalls[$key-1][1][2] = false; 1481 1482 $toDelete[] = $key-1; 1483 $toDelete[] = $key; 1484 $toDelete[] = $key+1; 1485 } 1486 break; 1487 1488 case 'tablerow_close': 1489 1490 // Fix broken tables by adding missing cells 1491 $moreCalls = array(); 1492 while (++$lastCell < $this->maxCols) { 1493 $moreCalls[] = array('tablecell_open', array(1, null, 1), $call[2]); 1494 $moreCalls[] = array('cdata', array(''), $call[2]); 1495 $moreCalls[] = array('tablecell_close', array(), $call[2]); 1496 } 1497 $moreCallsLength = count($moreCalls); 1498 if($moreCallsLength) { 1499 array_splice($this->tableCalls, $key, 0, $moreCalls); 1500 $key += $moreCallsLength; 1501 } 1502 1503 if($this->countTableHeadRows == $lastRow) { 1504 array_splice($this->tableCalls, $key+1, 0, array( 1505 array('tablethead_close', array(), $call[2]))); 1506 } 1507 break; 1508 1509 } 1510 } 1511 1512 // condense cdata 1513 $cnt = count($this->tableCalls); 1514 for( $key = 0; $key < $cnt; $key++){ 1515 if($this->tableCalls[$key][0] == 'cdata'){ 1516 $ckey = $key; 1517 $key++; 1518 while($this->tableCalls[$key][0] == 'cdata'){ 1519 $this->tableCalls[$ckey][1][0] .= $this->tableCalls[$key][1][0]; 1520 $toDelete[] = $key; 1521 $key++; 1522 } 1523 continue; 1524 } 1525 } 1526 1527 foreach ( $toDelete as $delete ) { 1528 unset($this->tableCalls[$delete]); 1529 } 1530 $this->tableCalls = array_values($this->tableCalls); 1531 } 1532} 1533 1534 1535/** 1536 * Handler for paragraphs 1537 * 1538 * @author Harry Fuecks <hfuecks@gmail.com> 1539 */ 1540class Doku_Handler_Block { 1541 var $calls = array(); 1542 var $skipEol = false; 1543 var $inParagraph = false; 1544 1545 // Blocks these should not be inside paragraphs 1546 var $blockOpen = array( 1547 'header', 1548 'listu_open','listo_open','listitem_open','listcontent_open', 1549 'table_open','tablerow_open','tablecell_open','tableheader_open','tablethead_open', 1550 'quote_open', 1551 'code','file','hr','preformatted','rss', 1552 'htmlblock','phpblock', 1553 'footnote_open', 1554 ); 1555 1556 var $blockClose = array( 1557 'header', 1558 'listu_close','listo_close','listitem_close','listcontent_close', 1559 'table_close','tablerow_close','tablecell_close','tableheader_close','tablethead_close', 1560 'quote_close', 1561 'code','file','hr','preformatted','rss', 1562 'htmlblock','phpblock', 1563 'footnote_close', 1564 ); 1565 1566 // Stacks can contain paragraphs 1567 var $stackOpen = array( 1568 'section_open', 1569 ); 1570 1571 var $stackClose = array( 1572 'section_close', 1573 ); 1574 1575 1576 /** 1577 * Constructor. Adds loaded syntax plugins to the block and stack 1578 * arrays 1579 * 1580 * @author Andreas Gohr <andi@splitbrain.org> 1581 */ 1582 function __construct(){ 1583 global $DOKU_PLUGINS; 1584 //check if syntax plugins were loaded 1585 if(empty($DOKU_PLUGINS['syntax'])) return; 1586 foreach($DOKU_PLUGINS['syntax'] as $n => $p){ 1587 $ptype = $p->getPType(); 1588 if($ptype == 'block'){ 1589 $this->blockOpen[] = 'plugin_'.$n; 1590 $this->blockClose[] = 'plugin_'.$n; 1591 }elseif($ptype == 'stack'){ 1592 $this->stackOpen[] = 'plugin_'.$n; 1593 $this->stackClose[] = 'plugin_'.$n; 1594 } 1595 } 1596 } 1597 1598 function openParagraph($pos){ 1599 if ($this->inParagraph) return; 1600 $this->calls[] = array('p_open',array(), $pos); 1601 $this->inParagraph = true; 1602 $this->skipEol = true; 1603 } 1604 1605 /** 1606 * Close a paragraph if needed 1607 * 1608 * This function makes sure there are no empty paragraphs on the stack 1609 * 1610 * @author Andreas Gohr <andi@splitbrain.org> 1611 * 1612 * @param string|integer $pos 1613 */ 1614 function closeParagraph($pos){ 1615 if (!$this->inParagraph) return; 1616 // look back if there was any content - we don't want empty paragraphs 1617 $content = ''; 1618 $ccount = count($this->calls); 1619 for($i=$ccount-1; $i>=0; $i--){ 1620 if($this->calls[$i][0] == 'p_open'){ 1621 break; 1622 }elseif($this->calls[$i][0] == 'cdata'){ 1623 $content .= $this->calls[$i][1][0]; 1624 }else{ 1625 $content = 'found markup'; 1626 break; 1627 } 1628 } 1629 1630 if(trim($content)==''){ 1631 //remove the whole paragraph 1632 //array_splice($this->calls,$i); // <- this is much slower than the loop below 1633 for($x=$ccount; $x>$i; $x--) array_pop($this->calls); 1634 }else{ 1635 // remove ending linebreaks in the paragraph 1636 $i=count($this->calls)-1; 1637 if ($this->calls[$i][0] == 'cdata') $this->calls[$i][1][0] = rtrim($this->calls[$i][1][0],DOKU_PARSER_EOL); 1638 $this->calls[] = array('p_close',array(), $pos); 1639 } 1640 1641 $this->inParagraph = false; 1642 $this->skipEol = true; 1643 } 1644 1645 function addCall($call) { 1646 $key = count($this->calls); 1647 if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) { 1648 $this->calls[$key-1][1][0] .= $call[1][0]; 1649 } else { 1650 $this->calls[] = $call; 1651 } 1652 } 1653 1654 // simple version of addCall, without checking cdata 1655 function storeCall($call) { 1656 $this->calls[] = $call; 1657 } 1658 1659 /** 1660 * Processes the whole instruction stack to open and close paragraphs 1661 * 1662 * @author Harry Fuecks <hfuecks@gmail.com> 1663 * @author Andreas Gohr <andi@splitbrain.org> 1664 * 1665 * @param array $calls 1666 * 1667 * @return array 1668 */ 1669 function process($calls) { 1670 // open first paragraph 1671 $this->openParagraph(0); 1672 foreach ( $calls as $key => $call ) { 1673 $cname = $call[0]; 1674 if ($cname == 'plugin') { 1675 $cname='plugin_'.$call[1][0]; 1676 $plugin = true; 1677 $plugin_open = (($call[1][2] == DOKU_LEXER_ENTER) || ($call[1][2] == DOKU_LEXER_SPECIAL)); 1678 $plugin_close = (($call[1][2] == DOKU_LEXER_EXIT) || ($call[1][2] == DOKU_LEXER_SPECIAL)); 1679 } else { 1680 $plugin = false; 1681 } 1682 /* stack */ 1683 if ( in_array($cname,$this->stackClose ) && (!$plugin || $plugin_close)) { 1684 $this->closeParagraph($call[2]); 1685 $this->storeCall($call); 1686 $this->openParagraph($call[2]); 1687 continue; 1688 } 1689 if ( in_array($cname,$this->stackOpen ) && (!$plugin || $plugin_open) ) { 1690 $this->closeParagraph($call[2]); 1691 $this->storeCall($call); 1692 $this->openParagraph($call[2]); 1693 continue; 1694 } 1695 /* block */ 1696 // If it's a substition it opens and closes at the same call. 1697 // To make sure next paragraph is correctly started, let close go first. 1698 if ( in_array($cname, $this->blockClose) && (!$plugin || $plugin_close)) { 1699 $this->closeParagraph($call[2]); 1700 $this->storeCall($call); 1701 $this->openParagraph($call[2]); 1702 continue; 1703 } 1704 if ( in_array($cname, $this->blockOpen) && (!$plugin || $plugin_open)) { 1705 $this->closeParagraph($call[2]); 1706 $this->storeCall($call); 1707 continue; 1708 } 1709 /* eol */ 1710 if ( $cname == 'eol' ) { 1711 // Check this isn't an eol instruction to skip... 1712 if ( !$this->skipEol ) { 1713 // Next is EOL => double eol => mark as paragraph 1714 if ( isset($calls[$key+1]) && $calls[$key+1][0] == 'eol' ) { 1715 $this->closeParagraph($call[2]); 1716 $this->openParagraph($call[2]); 1717 } else { 1718 //if this is just a single eol make a space from it 1719 $this->addCall(array('cdata',array(DOKU_PARSER_EOL), $call[2])); 1720 } 1721 } 1722 continue; 1723 } 1724 /* normal */ 1725 $this->addCall($call); 1726 $this->skipEol = false; 1727 } 1728 // close last paragraph 1729 $call = end($this->calls); 1730 $this->closeParagraph($call[2]); 1731 return $this->calls; 1732 } 1733} 1734 1735//Setup VIM: ex: et ts=4 : 1736