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