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