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