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