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