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