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