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