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