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 function Doku_Handler_CallWriter(& $Handler) { 715 $this->Handler = & $Handler; 716 } 717 718 function writeCall($call) { 719 $this->Handler->calls[] = $call; 720 } 721 722 function writeCalls($calls) { 723 $this->Handler->calls = array_merge($this->Handler->calls, $calls); 724 } 725 726 // function is required, but since this call writer is first/highest in 727 // the chain it is not required to do anything 728 function finalise() { 729 unset($this->Handler); 730 } 731} 732 733//------------------------------------------------------------------------ 734/** 735 * Generic call writer class to handle nesting of rendering instructions 736 * within a render instruction. Also see nest() method of renderer base class 737 * 738 * @author Chris Smith <chris@jalakai.co.uk> 739 */ 740class Doku_Handler_Nest { 741 742 var $CallWriter; 743 var $calls = array(); 744 745 var $closingInstruction; 746 747 /** 748 * constructor 749 * 750 * @param object $CallWriter the renderers current call writer 751 * @param string $close closing instruction name, this is required to properly terminate the 752 * syntax mode if the document ends without a closing pattern 753 */ 754 function Doku_Handler_Nest(& $CallWriter, $close="nest_close") { 755 $this->CallWriter = & $CallWriter; 756 757 $this->closingInstruction = $close; 758 } 759 760 function writeCall($call) { 761 $this->calls[] = $call; 762 } 763 764 function writeCalls($calls) { 765 $this->calls = array_merge($this->calls, $calls); 766 } 767 768 function finalise() { 769 $last_call = end($this->calls); 770 $this->writeCall(array($this->closingInstruction,array(), $last_call[2])); 771 772 $this->process(); 773 $this->CallWriter->finalise(); 774 unset($this->CallWriter); 775 } 776 777 function process() { 778 // merge consecutive cdata 779 $unmerged_calls = $this->calls; 780 $this->calls = array(); 781 782 foreach ($unmerged_calls as $call) $this->addCall($call); 783 784 $first_call = reset($this->calls); 785 $this->CallWriter->writeCall(array("nest", array($this->calls), $first_call[2])); 786 } 787 788 function addCall($call) { 789 $key = count($this->calls); 790 if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) { 791 $this->calls[$key-1][1][0] .= $call[1][0]; 792 } else if ($call[0] == 'eol') { 793 // do nothing (eol shouldn't be allowed, to counter preformatted fix in #1652 & #1699) 794 } else { 795 $this->calls[] = $call; 796 } 797 } 798} 799 800class Doku_Handler_List { 801 802 var $CallWriter; 803 804 var $calls = array(); 805 var $listCalls = array(); 806 var $listStack = array(); 807 808 function Doku_Handler_List(& $CallWriter) { 809 $this->CallWriter = & $CallWriter; 810 } 811 812 function writeCall($call) { 813 $this->calls[] = $call; 814 } 815 816 // Probably not needed but just in case... 817 function writeCalls($calls) { 818 $this->calls = array_merge($this->calls, $calls); 819# $this->CallWriter->writeCalls($this->calls); 820 } 821 822 function finalise() { 823 $last_call = end($this->calls); 824 $this->writeCall(array('list_close',array(), $last_call[2])); 825 826 $this->process(); 827 $this->CallWriter->finalise(); 828 unset($this->CallWriter); 829 } 830 831 //------------------------------------------------------------------------ 832 function process() { 833 834 foreach ( $this->calls as $call ) { 835 switch ($call[0]) { 836 case 'list_item': 837 $this->listOpen($call); 838 break; 839 case 'list_open': 840 $this->listStart($call); 841 break; 842 case 'list_close': 843 $this->listEnd($call); 844 break; 845 default: 846 $this->listContent($call); 847 break; 848 } 849 } 850 851 $this->CallWriter->writeCalls($this->listCalls); 852 } 853 854 //------------------------------------------------------------------------ 855 function listStart($call) { 856 $depth = $this->interpretSyntax($call[1][0], $listType); 857 858 $this->initialDepth = $depth; 859 $this->listStack[] = array($listType, $depth); 860 861 $this->listCalls[] = array('list'.$listType.'_open',array(),$call[2]); 862 $this->listCalls[] = array('listitem_open',array(1),$call[2]); 863 $this->listCalls[] = array('listcontent_open',array(),$call[2]); 864 } 865 866 //------------------------------------------------------------------------ 867 function listEnd($call) { 868 $closeContent = true; 869 870 while ( $list = array_pop($this->listStack) ) { 871 if ( $closeContent ) { 872 $this->listCalls[] = array('listcontent_close',array(),$call[2]); 873 $closeContent = false; 874 } 875 $this->listCalls[] = array('listitem_close',array(),$call[2]); 876 $this->listCalls[] = array('list'.$list[0].'_close', array(), $call[2]); 877 } 878 } 879 880 //------------------------------------------------------------------------ 881 function listOpen($call) { 882 $depth = $this->interpretSyntax($call[1][0], $listType); 883 $end = end($this->listStack); 884 885 // Not allowed to be shallower than initialDepth 886 if ( $depth < $this->initialDepth ) { 887 $depth = $this->initialDepth; 888 } 889 890 //------------------------------------------------------------------------ 891 if ( $depth == $end[1] ) { 892 893 // Just another item in the list... 894 if ( $listType == $end[0] ) { 895 $this->listCalls[] = array('listcontent_close',array(),$call[2]); 896 $this->listCalls[] = array('listitem_close',array(),$call[2]); 897 $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]); 898 $this->listCalls[] = array('listcontent_open',array(),$call[2]); 899 900 // Switched list type... 901 } else { 902 903 $this->listCalls[] = array('listcontent_close',array(),$call[2]); 904 $this->listCalls[] = array('listitem_close',array(),$call[2]); 905 $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]); 906 $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]); 907 $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]); 908 $this->listCalls[] = array('listcontent_open',array(),$call[2]); 909 910 array_pop($this->listStack); 911 $this->listStack[] = array($listType, $depth); 912 } 913 914 //------------------------------------------------------------------------ 915 // Getting deeper... 916 } else if ( $depth > $end[1] ) { 917 918 $this->listCalls[] = array('listcontent_close',array(),$call[2]); 919 $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]); 920 $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]); 921 $this->listCalls[] = array('listcontent_open',array(),$call[2]); 922 923 $this->listStack[] = array($listType, $depth); 924 925 //------------------------------------------------------------------------ 926 // Getting shallower ( $depth < $end[1] ) 927 } else { 928 $this->listCalls[] = array('listcontent_close',array(),$call[2]); 929 $this->listCalls[] = array('listitem_close',array(),$call[2]); 930 $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]); 931 932 // Throw away the end - done 933 array_pop($this->listStack); 934 935 while (1) { 936 $end = end($this->listStack); 937 938 if ( $end[1] <= $depth ) { 939 940 // Normalize depths 941 $depth = $end[1]; 942 943 $this->listCalls[] = array('listitem_close',array(),$call[2]); 944 945 if ( $end[0] == $listType ) { 946 $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]); 947 $this->listCalls[] = array('listcontent_open',array(),$call[2]); 948 949 } else { 950 // Switching list type... 951 $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]); 952 $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]); 953 $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]); 954 $this->listCalls[] = array('listcontent_open',array(),$call[2]); 955 956 array_pop($this->listStack); 957 $this->listStack[] = array($listType, $depth); 958 } 959 960 break; 961 962 // Haven't dropped down far enough yet.... ( $end[1] > $depth ) 963 } else { 964 965 $this->listCalls[] = array('listitem_close',array(),$call[2]); 966 $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]); 967 968 array_pop($this->listStack); 969 970 } 971 972 } 973 974 } 975 } 976 977 //------------------------------------------------------------------------ 978 function listContent($call) { 979 $this->listCalls[] = $call; 980 } 981 982 //------------------------------------------------------------------------ 983 function interpretSyntax($match, & $type) { 984 if ( substr($match,-1) == '*' ) { 985 $type = 'u'; 986 } else { 987 $type = 'o'; 988 } 989 // Is the +1 needed? It used to be count(explode(...)) 990 // but I don't think the number is seen outside this handler 991 return substr_count(str_replace("\t",' ',$match), ' ') + 1; 992 } 993} 994 995//------------------------------------------------------------------------ 996class Doku_Handler_Preformatted { 997 998 var $CallWriter; 999 1000 var $calls = array(); 1001 var $pos; 1002 var $text =''; 1003 1004 1005 1006 function Doku_Handler_Preformatted(& $CallWriter) { 1007 $this->CallWriter = & $CallWriter; 1008 } 1009 1010 function writeCall($call) { 1011 $this->calls[] = $call; 1012 } 1013 1014 // Probably not needed but just in case... 1015 function writeCalls($calls) { 1016 $this->calls = array_merge($this->calls, $calls); 1017# $this->CallWriter->writeCalls($this->calls); 1018 } 1019 1020 function finalise() { 1021 $last_call = end($this->calls); 1022 $this->writeCall(array('preformatted_end',array(), $last_call[2])); 1023 1024 $this->process(); 1025 $this->CallWriter->finalise(); 1026 unset($this->CallWriter); 1027 } 1028 1029 function process() { 1030 foreach ( $this->calls as $call ) { 1031 switch ($call[0]) { 1032 case 'preformatted_start': 1033 $this->pos = $call[2]; 1034 break; 1035 case 'preformatted_newline': 1036 $this->text .= "\n"; 1037 break; 1038 case 'preformatted_content': 1039 $this->text .= $call[1][0]; 1040 break; 1041 case 'preformatted_end': 1042 if (trim($this->text)) { 1043 $this->CallWriter->writeCall(array('preformatted',array($this->text),$this->pos)); 1044 } 1045 // see FS#1699 & FS#1652, add 'eol' instructions to ensure proper triggering of following p_open 1046 $this->CallWriter->writeCall(array('eol',array(),$this->pos)); 1047 $this->CallWriter->writeCall(array('eol',array(),$this->pos)); 1048 break; 1049 } 1050 } 1051 } 1052 1053} 1054 1055//------------------------------------------------------------------------ 1056class Doku_Handler_Quote { 1057 1058 var $CallWriter; 1059 1060 var $calls = array(); 1061 1062 var $quoteCalls = array(); 1063 1064 function Doku_Handler_Quote(& $CallWriter) { 1065 $this->CallWriter = & $CallWriter; 1066 } 1067 1068 function writeCall($call) { 1069 $this->calls[] = $call; 1070 } 1071 1072 // Probably not needed but just in case... 1073 function writeCalls($calls) { 1074 $this->calls = array_merge($this->calls, $calls); 1075 } 1076 1077 function finalise() { 1078 $last_call = end($this->calls); 1079 $this->writeCall(array('quote_end',array(), $last_call[2])); 1080 1081 $this->process(); 1082 $this->CallWriter->finalise(); 1083 unset($this->CallWriter); 1084 } 1085 1086 function process() { 1087 1088 $quoteDepth = 1; 1089 1090 foreach ( $this->calls as $call ) { 1091 switch ($call[0]) { 1092 1093 case 'quote_start': 1094 1095 $this->quoteCalls[] = array('quote_open',array(),$call[2]); 1096 1097 case 'quote_newline': 1098 1099 $quoteLength = $this->getDepth($call[1][0]); 1100 1101 if ( $quoteLength > $quoteDepth ) { 1102 $quoteDiff = $quoteLength - $quoteDepth; 1103 for ( $i = 1; $i <= $quoteDiff; $i++ ) { 1104 $this->quoteCalls[] = array('quote_open',array(),$call[2]); 1105 } 1106 } else if ( $quoteLength < $quoteDepth ) { 1107 $quoteDiff = $quoteDepth - $quoteLength; 1108 for ( $i = 1; $i <= $quoteDiff; $i++ ) { 1109 $this->quoteCalls[] = array('quote_close',array(),$call[2]); 1110 } 1111 } else { 1112 if ($call[0] != 'quote_start') $this->quoteCalls[] = array('linebreak',array(),$call[2]); 1113 } 1114 1115 $quoteDepth = $quoteLength; 1116 1117 break; 1118 1119 case 'quote_end': 1120 1121 if ( $quoteDepth > 1 ) { 1122 $quoteDiff = $quoteDepth - 1; 1123 for ( $i = 1; $i <= $quoteDiff; $i++ ) { 1124 $this->quoteCalls[] = array('quote_close',array(),$call[2]); 1125 } 1126 } 1127 1128 $this->quoteCalls[] = array('quote_close',array(),$call[2]); 1129 1130 $this->CallWriter->writeCalls($this->quoteCalls); 1131 break; 1132 1133 default: 1134 $this->quoteCalls[] = $call; 1135 break; 1136 } 1137 } 1138 } 1139 1140 function getDepth($marker) { 1141 preg_match('/>{1,}/', $marker, $matches); 1142 $quoteLength = strlen($matches[0]); 1143 return $quoteLength; 1144 } 1145} 1146 1147//------------------------------------------------------------------------ 1148class Doku_Handler_Table { 1149 1150 var $CallWriter; 1151 1152 var $calls = array(); 1153 var $tableCalls = array(); 1154 var $maxCols = 0; 1155 var $maxRows = 1; 1156 var $currentCols = 0; 1157 var $firstCell = false; 1158 var $lastCellType = 'tablecell'; 1159 var $inTableHead = true; 1160 var $currentRow = array('tableheader' => 0, 'tablecell' => 0); 1161 var $countTableHeadRows = 0; 1162 1163 function Doku_Handler_Table(& $CallWriter) { 1164 $this->CallWriter = & $CallWriter; 1165 } 1166 1167 function writeCall($call) { 1168 $this->calls[] = $call; 1169 } 1170 1171 // Probably not needed but just in case... 1172 function writeCalls($calls) { 1173 $this->calls = array_merge($this->calls, $calls); 1174 } 1175 1176 function finalise() { 1177 $last_call = end($this->calls); 1178 $this->writeCall(array('table_end',array(), $last_call[2])); 1179 1180 $this->process(); 1181 $this->CallWriter->finalise(); 1182 unset($this->CallWriter); 1183 } 1184 1185 //------------------------------------------------------------------------ 1186 function process() { 1187 foreach ( $this->calls as $call ) { 1188 switch ( $call[0] ) { 1189 case 'table_start': 1190 $this->tableStart($call); 1191 break; 1192 case 'table_row': 1193 $this->tableRowClose($call); 1194 $this->tableRowOpen(array('tablerow_open',$call[1],$call[2])); 1195 break; 1196 case 'tableheader': 1197 case 'tablecell': 1198 $this->tableCell($call); 1199 break; 1200 case 'table_end': 1201 $this->tableRowClose($call); 1202 $this->tableEnd($call); 1203 break; 1204 default: 1205 $this->tableDefault($call); 1206 break; 1207 } 1208 } 1209 $this->CallWriter->writeCalls($this->tableCalls); 1210 } 1211 1212 function tableStart($call) { 1213 $this->tableCalls[] = array('table_open',$call[1],$call[2]); 1214 $this->tableCalls[] = array('tablerow_open',array(),$call[2]); 1215 $this->firstCell = true; 1216 } 1217 1218 function tableEnd($call) { 1219 $this->tableCalls[] = array('table_close',$call[1],$call[2]); 1220 $this->finalizeTable(); 1221 } 1222 1223 function tableRowOpen($call) { 1224 $this->tableCalls[] = $call; 1225 $this->currentCols = 0; 1226 $this->firstCell = true; 1227 $this->lastCellType = 'tablecell'; 1228 $this->maxRows++; 1229 if ($this->inTableHead) { 1230 $this->currentRow = array('tablecell' => 0, 'tableheader' => 0); 1231 } 1232 } 1233 1234 function tableRowClose($call) { 1235 if ($this->inTableHead && ($this->inTableHead = $this->isTableHeadRow())) { 1236 $this->countTableHeadRows++; 1237 } 1238 // Strip off final cell opening and anything after it 1239 while ( $discard = array_pop($this->tableCalls ) ) { 1240 1241 if ( $discard[0] == 'tablecell_open' || $discard[0] == 'tableheader_open') { 1242 break; 1243 } 1244 if (!empty($this->currentRow[$discard[0]])) { 1245 $this->currentRow[$discard[0]]--; 1246 } 1247 } 1248 $this->tableCalls[] = array('tablerow_close', array(), $call[2]); 1249 1250 if ( $this->currentCols > $this->maxCols ) { 1251 $this->maxCols = $this->currentCols; 1252 } 1253 } 1254 1255 function isTableHeadRow() { 1256 $td = $this->currentRow['tablecell']; 1257 $th = $this->currentRow['tableheader']; 1258 1259 if (!$th || $td > 2) return false; 1260 if (2*$td > $th) return false; 1261 1262 return true; 1263 } 1264 1265 function tableCell($call) { 1266 if ($this->inTableHead) { 1267 $this->currentRow[$call[0]]++; 1268 } 1269 if ( !$this->firstCell ) { 1270 1271 // Increase the span 1272 $lastCall = end($this->tableCalls); 1273 1274 // A cell call which follows an open cell means an empty cell so span 1275 if ( $lastCall[0] == 'tablecell_open' || $lastCall[0] == 'tableheader_open' ) { 1276 $this->tableCalls[] = array('colspan',array(),$call[2]); 1277 1278 } 1279 1280 $this->tableCalls[] = array($this->lastCellType.'_close',array(),$call[2]); 1281 $this->tableCalls[] = array($call[0].'_open',array(1,null,1),$call[2]); 1282 $this->lastCellType = $call[0]; 1283 1284 } else { 1285 1286 $this->tableCalls[] = array($call[0].'_open',array(1,null,1),$call[2]); 1287 $this->lastCellType = $call[0]; 1288 $this->firstCell = false; 1289 1290 } 1291 1292 $this->currentCols++; 1293 } 1294 1295 function tableDefault($call) { 1296 $this->tableCalls[] = $call; 1297 } 1298 1299 function finalizeTable() { 1300 1301 // Add the max cols and rows to the table opening 1302 if ( $this->tableCalls[0][0] == 'table_open' ) { 1303 // Adjust to num cols not num col delimeters 1304 $this->tableCalls[0][1][] = $this->maxCols - 1; 1305 $this->tableCalls[0][1][] = $this->maxRows; 1306 $this->tableCalls[0][1][] = array_shift($this->tableCalls[0][1]); 1307 } else { 1308 trigger_error('First element in table call list is not table_open'); 1309 } 1310 1311 $lastRow = 0; 1312 $lastCell = 0; 1313 $cellKey = array(); 1314 $toDelete = array(); 1315 1316 // if still in tableheader, then there can be no table header 1317 // as all rows can't be within <THEAD> 1318 if ($this->inTableHead) { 1319 $this->inTableHead = false; 1320 $this->countTableHeadRows = 0; 1321 } 1322 1323 // Look for the colspan elements and increment the colspan on the 1324 // previous non-empty opening cell. Once done, delete all the cells 1325 // that contain colspans 1326 for ($key = 0 ; $key < count($this->tableCalls) ; ++$key) { 1327 $call = $this->tableCalls[$key]; 1328 1329 switch ($call[0]) { 1330 case 'table_open' : 1331 if($this->countTableHeadRows) { 1332 array_splice($this->tableCalls, $key+1, 0, array( 1333 array('tablethead_open', array(), $call[2])) 1334 ); 1335 } 1336 break; 1337 1338 case 'tablerow_open': 1339 1340 $lastRow++; 1341 $lastCell = 0; 1342 break; 1343 1344 case 'tablecell_open': 1345 case 'tableheader_open': 1346 1347 $lastCell++; 1348 $cellKey[$lastRow][$lastCell] = $key; 1349 break; 1350 1351 case 'table_align': 1352 1353 $prev = in_array($this->tableCalls[$key-1][0], array('tablecell_open', 'tableheader_open')); 1354 $next = in_array($this->tableCalls[$key+1][0], array('tablecell_close', 'tableheader_close')); 1355 // If the cell is empty, align left 1356 if ($prev && $next) { 1357 $this->tableCalls[$key-1][1][1] = 'left'; 1358 1359 // If the previous element was a cell open, align right 1360 } elseif ($prev) { 1361 $this->tableCalls[$key-1][1][1] = 'right'; 1362 1363 // If the next element is the close of an element, align either center or left 1364 } elseif ( $next) { 1365 if ( $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] == 'right' ) { 1366 $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] = 'center'; 1367 } else { 1368 $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] = 'left'; 1369 } 1370 1371 } 1372 1373 // Now convert the whitespace back to cdata 1374 $this->tableCalls[$key][0] = 'cdata'; 1375 break; 1376 1377 case 'colspan': 1378 1379 $this->tableCalls[$key-1][1][0] = false; 1380 1381 for($i = $key-2; $i >= $cellKey[$lastRow][1]; $i--) { 1382 1383 if ( $this->tableCalls[$i][0] == 'tablecell_open' || $this->tableCalls[$i][0] == 'tableheader_open' ) { 1384 1385 if ( false !== $this->tableCalls[$i][1][0] ) { 1386 $this->tableCalls[$i][1][0]++; 1387 break; 1388 } 1389 1390 } 1391 } 1392 1393 $toDelete[] = $key-1; 1394 $toDelete[] = $key; 1395 $toDelete[] = $key+1; 1396 break; 1397 1398 case 'rowspan': 1399 1400 if ( $this->tableCalls[$key-1][0] == 'cdata' ) { 1401 // ignore rowspan if previous call was cdata (text mixed with :::) we don't have to check next call as that wont match regex 1402 $this->tableCalls[$key][0] = 'cdata'; 1403 1404 } else { 1405 1406 $spanning_cell = null; 1407 1408 // can't cross thead/tbody boundary 1409 if (!$this->countTableHeadRows || ($lastRow-1 != $this->countTableHeadRows)) { 1410 for($i = $lastRow-1; $i > 0; $i--) { 1411 1412 if ( $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tablecell_open' || $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tableheader_open' ) { 1413 1414 if ($this->tableCalls[$cellKey[$i][$lastCell]][1][2] >= $lastRow - $i) { 1415 $spanning_cell = $i; 1416 break; 1417 } 1418 1419 } 1420 } 1421 } 1422 if (is_null($spanning_cell)) { 1423 // No spanning cell found, so convert this cell to 1424 // an empty one to avoid broken tables 1425 $this->tableCalls[$key][0] = 'cdata'; 1426 $this->tableCalls[$key][1][0] = ''; 1427 continue; 1428 } 1429 $this->tableCalls[$cellKey[$spanning_cell][$lastCell]][1][2]++; 1430 1431 $this->tableCalls[$key-1][1][2] = false; 1432 1433 $toDelete[] = $key-1; 1434 $toDelete[] = $key; 1435 $toDelete[] = $key+1; 1436 } 1437 break; 1438 1439 case 'tablerow_close': 1440 1441 // Fix broken tables by adding missing cells 1442 while (++$lastCell < $this->maxCols) { 1443 array_splice($this->tableCalls, $key, 0, array( 1444 array('tablecell_open', array(1, null, 1), $call[2]), 1445 array('cdata', array(''), $call[2]), 1446 array('tablecell_close', array(), $call[2]))); 1447 $key += 3; 1448 } 1449 1450 if($this->countTableHeadRows == $lastRow) { 1451 array_splice($this->tableCalls, $key+1, 0, array( 1452 array('tablethead_close', array(), $call[2]))); 1453 } 1454 break; 1455 1456 } 1457 } 1458 1459 // condense cdata 1460 $cnt = count($this->tableCalls); 1461 for( $key = 0; $key < $cnt; $key++){ 1462 if($this->tableCalls[$key][0] == 'cdata'){ 1463 $ckey = $key; 1464 $key++; 1465 while($this->tableCalls[$key][0] == 'cdata'){ 1466 $this->tableCalls[$ckey][1][0] .= $this->tableCalls[$key][1][0]; 1467 $toDelete[] = $key; 1468 $key++; 1469 } 1470 continue; 1471 } 1472 } 1473 1474 foreach ( $toDelete as $delete ) { 1475 unset($this->tableCalls[$delete]); 1476 } 1477 $this->tableCalls = array_values($this->tableCalls); 1478 } 1479} 1480 1481 1482/** 1483 * Handler for paragraphs 1484 * 1485 * @author Harry Fuecks <hfuecks@gmail.com> 1486 */ 1487class Doku_Handler_Block { 1488 var $calls = array(); 1489 var $skipEol = false; 1490 var $inParagraph = false; 1491 1492 // Blocks these should not be inside paragraphs 1493 var $blockOpen = array( 1494 'header', 1495 'listu_open','listo_open','listitem_open','listcontent_open', 1496 'table_open','tablerow_open','tablecell_open','tableheader_open','tablethead_open', 1497 'quote_open', 1498 'code','file','hr','preformatted','rss', 1499 'htmlblock','phpblock', 1500 'footnote_open', 1501 ); 1502 1503 var $blockClose = array( 1504 'header', 1505 'listu_close','listo_close','listitem_close','listcontent_close', 1506 'table_close','tablerow_close','tablecell_close','tableheader_close','tablethead_close', 1507 'quote_close', 1508 'code','file','hr','preformatted','rss', 1509 'htmlblock','phpblock', 1510 'footnote_close', 1511 ); 1512 1513 // Stacks can contain paragraphs 1514 var $stackOpen = array( 1515 'section_open', 1516 ); 1517 1518 var $stackClose = array( 1519 'section_close', 1520 ); 1521 1522 1523 /** 1524 * Constructor. Adds loaded syntax plugins to the block and stack 1525 * arrays 1526 * 1527 * @author Andreas Gohr <andi@splitbrain.org> 1528 */ 1529 function Doku_Handler_Block(){ 1530 global $DOKU_PLUGINS; 1531 //check if syntax plugins were loaded 1532 if(empty($DOKU_PLUGINS['syntax'])) return; 1533 foreach($DOKU_PLUGINS['syntax'] as $n => $p){ 1534 $ptype = $p->getPType(); 1535 if($ptype == 'block'){ 1536 $this->blockOpen[] = 'plugin_'.$n; 1537 $this->blockClose[] = 'plugin_'.$n; 1538 }elseif($ptype == 'stack'){ 1539 $this->stackOpen[] = 'plugin_'.$n; 1540 $this->stackClose[] = 'plugin_'.$n; 1541 } 1542 } 1543 } 1544 1545 function openParagraph($pos){ 1546 if ($this->inParagraph) return; 1547 $this->calls[] = array('p_open',array(), $pos); 1548 $this->inParagraph = true; 1549 $this->skipEol = true; 1550 } 1551 1552 /** 1553 * Close a paragraph if needed 1554 * 1555 * This function makes sure there are no empty paragraphs on the stack 1556 * 1557 * @author Andreas Gohr <andi@splitbrain.org> 1558 */ 1559 function closeParagraph($pos){ 1560 if (!$this->inParagraph) return; 1561 // look back if there was any content - we don't want empty paragraphs 1562 $content = ''; 1563 $ccount = count($this->calls); 1564 for($i=$ccount-1; $i>=0; $i--){ 1565 if($this->calls[$i][0] == 'p_open'){ 1566 break; 1567 }elseif($this->calls[$i][0] == 'cdata'){ 1568 $content .= $this->calls[$i][1][0]; 1569 }else{ 1570 $content = 'found markup'; 1571 break; 1572 } 1573 } 1574 1575 if(trim($content)==''){ 1576 //remove the whole paragraph 1577 //array_splice($this->calls,$i); // <- this is much slower than the loop below 1578 for($x=$ccount; $x>$i; $x--) array_pop($this->calls); 1579 }else{ 1580 // remove ending linebreaks in the paragraph 1581 $i=count($this->calls)-1; 1582 if ($this->calls[$i][0] == 'cdata') $this->calls[$i][1][0] = rtrim($this->calls[$i][1][0],DOKU_PARSER_EOL); 1583 $this->calls[] = array('p_close',array(), $pos); 1584 } 1585 1586 $this->inParagraph = false; 1587 $this->skipEol = true; 1588 } 1589 1590 function addCall($call) { 1591 $key = count($this->calls); 1592 if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) { 1593 $this->calls[$key-1][1][0] .= $call[1][0]; 1594 } else { 1595 $this->calls[] = $call; 1596 } 1597 } 1598 1599 // simple version of addCall, without checking cdata 1600 function storeCall($call) { 1601 $this->calls[] = $call; 1602 } 1603 1604 /** 1605 * Processes the whole instruction stack to open and close paragraphs 1606 * 1607 * @author Harry Fuecks <hfuecks@gmail.com> 1608 * @author Andreas Gohr <andi@splitbrain.org> 1609 */ 1610 function process($calls) { 1611 // open first paragraph 1612 $this->openParagraph(0); 1613 foreach ( $calls as $key => $call ) { 1614 $cname = $call[0]; 1615 if ($cname == 'plugin') { 1616 $cname='plugin_'.$call[1][0]; 1617 $plugin = true; 1618 $plugin_open = (($call[1][2] == DOKU_LEXER_ENTER) || ($call[1][2] == DOKU_LEXER_SPECIAL)); 1619 $plugin_close = (($call[1][2] == DOKU_LEXER_EXIT) || ($call[1][2] == DOKU_LEXER_SPECIAL)); 1620 } else { 1621 $plugin = false; 1622 } 1623 /* stack */ 1624 if ( in_array($cname,$this->stackClose ) && (!$plugin || $plugin_close)) { 1625 $this->closeParagraph($call[2]); 1626 $this->storeCall($call); 1627 $this->openParagraph($call[2]); 1628 continue; 1629 } 1630 if ( in_array($cname,$this->stackOpen ) && (!$plugin || $plugin_open) ) { 1631 $this->closeParagraph($call[2]); 1632 $this->storeCall($call); 1633 $this->openParagraph($call[2]); 1634 continue; 1635 } 1636 /* block */ 1637 // If it's a substition it opens and closes at the same call. 1638 // To make sure next paragraph is correctly started, let close go first. 1639 if ( in_array($cname, $this->blockClose) && (!$plugin || $plugin_close)) { 1640 $this->closeParagraph($call[2]); 1641 $this->storeCall($call); 1642 $this->openParagraph($call[2]); 1643 continue; 1644 } 1645 if ( in_array($cname, $this->blockOpen) && (!$plugin || $plugin_open)) { 1646 $this->closeParagraph($call[2]); 1647 $this->storeCall($call); 1648 continue; 1649 } 1650 /* eol */ 1651 if ( $cname == 'eol' ) { 1652 // Check this isn't an eol instruction to skip... 1653 if ( !$this->skipEol ) { 1654 // Next is EOL => double eol => mark as paragraph 1655 if ( isset($calls[$key+1]) && $calls[$key+1][0] == 'eol' ) { 1656 $this->closeParagraph($call[2]); 1657 $this->openParagraph($call[2]); 1658 } else { 1659 //if this is just a single eol make a space from it 1660 $this->addCall(array('cdata',array(DOKU_PARSER_EOL), $call[2])); 1661 } 1662 } 1663 continue; 1664 } 1665 /* normal */ 1666 $this->addCall($call); 1667 $this->skipEol = false; 1668 } 1669 // close last paragraph 1670 $call = end($this->calls); 1671 $this->closeParagraph($call[2]); 1672 return $this->calls; 1673 } 1674} 1675 1676//Setup VIM: ex: et ts=4 : 1677