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