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