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