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