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