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