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