xref: /dokuwiki/inc/parser/handler.php (revision 6faaead86be36e84bff4062af115119428f35166)
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{
690        $linking = 'details';
691    }
692
693    //get caching command
694    if (preg_match('/(nocache|recache)/i',$param,$cachemode)){
695        $cache = $cachemode[1];
696    }else{
697        $cache = 'cache';
698    }
699
700    // Check whether this is a local or remote image
701    if ( preg_match('#^(https?|ftp)#i',$src) ) {
702        $call = 'externalmedia';
703    } else {
704        $call = 'internalmedia';
705    }
706
707    $params = array(
708        'type'=>$call,
709        'src'=>$src,
710        'title'=>$link[1],
711        'align'=>$align,
712        'width'=>$w,
713        'height'=>$h,
714        'cache'=>$cache,
715        'linking'=>$linking,
716    );
717
718    return $params;
719}
720
721//------------------------------------------------------------------------
722class Doku_Handler_CallWriter {
723
724    var $Handler;
725
726    function Doku_Handler_CallWriter(& $Handler) {
727        $this->Handler = & $Handler;
728    }
729
730    function writeCall($call) {
731        $this->Handler->calls[] = $call;
732    }
733
734    function writeCalls($calls) {
735        $this->Handler->calls = array_merge($this->Handler->calls, $calls);
736    }
737
738    // function is required, but since this call writer is first/highest in
739    // the chain it is not required to do anything
740    function finalise() {
741    }
742}
743
744//------------------------------------------------------------------------
745/**
746 * Generic call writer class to handle nesting of rendering instructions
747 * within a render instruction. Also see nest() method of renderer base class
748 *
749 * @author    Chris Smith <chris@jalakai.co.uk>
750 */
751class Doku_Handler_Nest {
752
753    var $CallWriter;
754    var $calls = array();
755
756    var $closingInstruction;
757
758    /**
759     * constructor
760     *
761     * @param  object     $CallWriter     the renderers current call writer
762     * @param  string     $close          closing instruction name, this is required to properly terminate the
763     *                                    syntax mode if the document ends without a closing pattern
764     */
765    function Doku_Handler_Nest(& $CallWriter, $close="nest_close") {
766        $this->CallWriter = & $CallWriter;
767
768        $this->closingInstruction = $close;
769    }
770
771    function writeCall($call) {
772        $this->calls[] = $call;
773    }
774
775    function writeCalls($calls) {
776        $this->calls = array_merge($this->calls, $calls);
777    }
778
779    function finalise() {
780        $last_call = end($this->calls);
781        $this->writeCall(array($this->closingInstruction,array(), $last_call[2]));
782
783        $this->process();
784        $this->CallWriter->finalise();
785    }
786
787    function process() {
788        $first_call = reset($this->calls);
789        $this->CallWriter->writeCall(array("nest", array($this->calls), $first_call[2]));
790    }
791}
792
793class Doku_Handler_List {
794
795    var $CallWriter;
796
797    var $calls = array();
798    var $listCalls = array();
799    var $listStack = array();
800
801    function Doku_Handler_List(& $CallWriter) {
802        $this->CallWriter = & $CallWriter;
803    }
804
805    function writeCall($call) {
806        $this->calls[] = $call;
807    }
808
809    // Probably not needed but just in case...
810    function writeCalls($calls) {
811        $this->calls = array_merge($this->calls, $calls);
812#        $this->CallWriter->writeCalls($this->calls);
813    }
814
815    function finalise() {
816        $last_call = end($this->calls);
817        $this->writeCall(array('list_close',array(), $last_call[2]));
818
819        $this->process();
820        $this->CallWriter->finalise();
821    }
822
823    //------------------------------------------------------------------------
824    function process() {
825
826        foreach ( $this->calls as $call ) {
827            switch ($call[0]) {
828                case 'list_item':
829                    $this->listOpen($call);
830                break;
831                case 'list_open':
832                    $this->listStart($call);
833                break;
834                case 'list_close':
835                    $this->listEnd($call);
836                break;
837                default:
838                    $this->listContent($call);
839                break;
840            }
841        }
842
843        $this->CallWriter->writeCalls($this->listCalls);
844    }
845
846    //------------------------------------------------------------------------
847    function listStart($call) {
848        $depth = $this->interpretSyntax($call[1][0], $listType);
849
850        $this->initialDepth = $depth;
851        $this->listStack[] = array($listType, $depth);
852
853        $this->listCalls[] = array('list'.$listType.'_open',array(),$call[2]);
854        $this->listCalls[] = array('listitem_open',array(1),$call[2]);
855        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
856    }
857
858    //------------------------------------------------------------------------
859    function listEnd($call) {
860        $closeContent = true;
861
862        while ( $list = array_pop($this->listStack) ) {
863            if ( $closeContent ) {
864                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
865                $closeContent = false;
866            }
867            $this->listCalls[] = array('listitem_close',array(),$call[2]);
868            $this->listCalls[] = array('list'.$list[0].'_close', array(), $call[2]);
869        }
870    }
871
872    //------------------------------------------------------------------------
873    function listOpen($call) {
874        $depth = $this->interpretSyntax($call[1][0], $listType);
875        $end = end($this->listStack);
876
877        // Not allowed to be shallower than initialDepth
878        if ( $depth < $this->initialDepth ) {
879            $depth = $this->initialDepth;
880        }
881
882        //------------------------------------------------------------------------
883        if ( $depth == $end[1] ) {
884
885            // Just another item in the list...
886            if ( $listType == $end[0] ) {
887                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
888                $this->listCalls[] = array('listitem_close',array(),$call[2]);
889                $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
890                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
891
892            // Switched list type...
893            } else {
894
895                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
896                $this->listCalls[] = array('listitem_close',array(),$call[2]);
897                $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
898                $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
899                $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
900                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
901
902                array_pop($this->listStack);
903                $this->listStack[] = array($listType, $depth);
904            }
905
906        //------------------------------------------------------------------------
907        // Getting deeper...
908        } else if ( $depth > $end[1] ) {
909
910            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
911            $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
912            $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
913            $this->listCalls[] = array('listcontent_open',array(),$call[2]);
914
915            $this->listStack[] = array($listType, $depth);
916
917        //------------------------------------------------------------------------
918        // Getting shallower ( $depth < $end[1] )
919        } else {
920            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
921            $this->listCalls[] = array('listitem_close',array(),$call[2]);
922            $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
923
924            // Throw away the end - done
925            array_pop($this->listStack);
926
927            while (1) {
928                $end = end($this->listStack);
929
930                if ( $end[1] <= $depth ) {
931
932                    // Normalize depths
933                    $depth = $end[1];
934
935                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
936
937                    if ( $end[0] == $listType ) {
938                        $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
939                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
940
941                    } else {
942                        // Switching list type...
943                        $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
944                        $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
945                        $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
946                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
947
948                        array_pop($this->listStack);
949                        $this->listStack[] = array($listType, $depth);
950                    }
951
952                    break;
953
954                // Haven't dropped down far enough yet.... ( $end[1] > $depth )
955                } else {
956
957                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
958                    $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
959
960                    array_pop($this->listStack);
961
962                }
963
964            }
965
966        }
967    }
968
969    //------------------------------------------------------------------------
970    function listContent($call) {
971        $this->listCalls[] = $call;
972    }
973
974    //------------------------------------------------------------------------
975    function interpretSyntax($match, & $type) {
976        if ( substr($match,-1) == '*' ) {
977            $type = 'u';
978        } else {
979            $type = 'o';
980        }
981        return count(explode('  ',str_replace("\t",'  ',$match)));
982    }
983}
984
985//------------------------------------------------------------------------
986class Doku_Handler_Preformatted {
987
988    var $CallWriter;
989
990    var $calls = array();
991    var $pos;
992    var $text ='';
993
994
995
996    function Doku_Handler_Preformatted(& $CallWriter) {
997        $this->CallWriter = & $CallWriter;
998    }
999
1000    function writeCall($call) {
1001        $this->calls[] = $call;
1002    }
1003
1004    // Probably not needed but just in case...
1005    function writeCalls($calls) {
1006        $this->calls = array_merge($this->calls, $calls);
1007#        $this->CallWriter->writeCalls($this->calls);
1008    }
1009
1010    function finalise() {
1011        $last_call = end($this->calls);
1012        $this->writeCall(array('preformatted_end',array(), $last_call[2]));
1013
1014        $this->process();
1015        $this->CallWriter->finalise();
1016    }
1017
1018    function process() {
1019        foreach ( $this->calls as $call ) {
1020            switch ($call[0]) {
1021                case 'preformatted_start':
1022                    $this->pos = $call[2];
1023                break;
1024                case 'preformatted_newline':
1025                    $this->text .= "\n";
1026                break;
1027                case 'preformatted_content':
1028                    $this->text .= $call[1][0];
1029                break;
1030                case 'preformatted_end':
1031                    $this->CallWriter->writeCall(array('preformatted',array($this->text),$this->pos));
1032                break;
1033            }
1034        }
1035    }
1036
1037}
1038
1039//------------------------------------------------------------------------
1040class Doku_Handler_Quote {
1041
1042    var $CallWriter;
1043
1044    var $calls = array();
1045
1046    var $quoteCalls = array();
1047
1048    function Doku_Handler_Quote(& $CallWriter) {
1049        $this->CallWriter = & $CallWriter;
1050    }
1051
1052    function writeCall($call) {
1053        $this->calls[] = $call;
1054    }
1055
1056    // Probably not needed but just in case...
1057    function writeCalls($calls) {
1058        $this->calls = array_merge($this->calls, $calls);
1059#        $this->CallWriter->writeCalls($this->calls);
1060    }
1061
1062    function finalise() {
1063        $last_call = end($this->calls);
1064        $this->writeCall(array('quote_end',array(), $last_call[2]));
1065
1066        $this->process();
1067        $this->CallWriter->finalise();
1068    }
1069
1070    function process() {
1071
1072        $quoteDepth = 1;
1073
1074        foreach ( $this->calls as $call ) {
1075            switch ($call[0]) {
1076
1077                case 'quote_start':
1078
1079                    $this->quoteCalls[] = array('quote_open',array(),$call[2]);
1080
1081                case 'quote_newline':
1082
1083                    $quoteLength = $this->getDepth($call[1][0]);
1084
1085                    if ( $quoteLength > $quoteDepth ) {
1086                        $quoteDiff = $quoteLength - $quoteDepth;
1087                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
1088                            $this->quoteCalls[] = array('quote_open',array(),$call[2]);
1089                        }
1090                    } else if ( $quoteLength < $quoteDepth ) {
1091                        $quoteDiff = $quoteDepth - $quoteLength;
1092                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
1093                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
1094                        }
1095                    } else {
1096                        if ($call[0] != 'quote_start') $this->quoteCalls[] = array('linebreak',array(),$call[2]);
1097                    }
1098
1099                    $quoteDepth = $quoteLength;
1100
1101                break;
1102
1103                case 'quote_end':
1104
1105                    if ( $quoteDepth > 1 ) {
1106                        $quoteDiff = $quoteDepth - 1;
1107                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
1108                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
1109                        }
1110                    }
1111
1112                    $this->quoteCalls[] = array('quote_close',array(),$call[2]);
1113
1114                    $this->CallWriter->writeCalls($this->quoteCalls);
1115                break;
1116
1117                default:
1118                    $this->quoteCalls[] = $call;
1119                break;
1120            }
1121        }
1122    }
1123
1124    function getDepth($marker) {
1125        preg_match('/>{1,}/', $marker, $matches);
1126        $quoteLength = strlen($matches[0]);
1127        return $quoteLength;
1128    }
1129}
1130
1131//------------------------------------------------------------------------
1132class Doku_Handler_Table {
1133
1134    var $CallWriter;
1135
1136    var $calls = array();
1137    var $tableCalls = array();
1138    var $maxCols = 0;
1139    var $maxRows = 1;
1140    var $currentCols = 0;
1141    var $firstCell = false;
1142    var $lastCellType = 'tablecell';
1143
1144    function Doku_Handler_Table(& $CallWriter) {
1145        $this->CallWriter = & $CallWriter;
1146    }
1147
1148    function writeCall($call) {
1149        $this->calls[] = $call;
1150    }
1151
1152    // Probably not needed but just in case...
1153    function writeCalls($calls) {
1154        $this->calls = array_merge($this->calls, $calls);
1155#        $this->CallWriter->writeCalls($this->calls);
1156    }
1157
1158    function finalise() {
1159        $last_call = end($this->calls);
1160        $this->writeCall(array('table_end',array(), $last_call[2]));
1161
1162        $this->process();
1163        $this->CallWriter->finalise();
1164    }
1165
1166    //------------------------------------------------------------------------
1167    function process() {
1168        foreach ( $this->calls as $call ) {
1169            switch ( $call[0] ) {
1170                case 'table_start':
1171                    $this->tableStart($call);
1172                break;
1173                case 'table_row':
1174                    $this->tableRowClose(array('tablerow_close',$call[1],$call[2]));
1175                    $this->tableRowOpen(array('tablerow_open',$call[1],$call[2]));
1176                break;
1177                case 'tableheader':
1178                case 'tablecell':
1179                    $this->tableCell($call);
1180                break;
1181                case 'table_end':
1182                    $this->tableRowClose(array('tablerow_close',$call[1],$call[2]));
1183                    $this->tableEnd($call);
1184                break;
1185                default:
1186                    $this->tableDefault($call);
1187                break;
1188            }
1189        }
1190        $this->CallWriter->writeCalls($this->tableCalls);
1191    }
1192
1193    function tableStart($call) {
1194        $this->tableCalls[] = array('table_open',array(),$call[2]);
1195        $this->tableCalls[] = array('tablerow_open',array(),$call[2]);
1196        $this->firstCell = true;
1197    }
1198
1199    function tableEnd($call) {
1200        $this->tableCalls[] = array('table_close',array(),$call[2]);
1201        $this->finalizeTable();
1202    }
1203
1204    function tableRowOpen($call) {
1205        $this->tableCalls[] = $call;
1206        $this->currentCols = 0;
1207        $this->firstCell = true;
1208        $this->lastCellType = 'tablecell';
1209        $this->maxRows++;
1210    }
1211
1212    function tableRowClose($call) {
1213        // Strip off final cell opening and anything after it
1214        while ( $discard = array_pop($this->tableCalls ) ) {
1215
1216            if ( $discard[0] == 'tablecell_open' || $discard[0] == 'tableheader_open') {
1217
1218                // Its a spanning element - put it back and close it
1219                if ( $discard[1][0] > 1 ) {
1220
1221                    $this->tableCalls[] = $discard;
1222                    if ( strstr($discard[0],'cell') ) {
1223                        $name = 'tablecell';
1224                    } else {
1225                        $name = 'tableheader';
1226                    }
1227                    $this->tableCalls[] = array($name.'_close',array(),$call[2]);
1228                }
1229
1230                break;
1231            }
1232        }
1233        $this->tableCalls[] = $call;
1234
1235        if ( $this->currentCols > $this->maxCols ) {
1236            $this->maxCols = $this->currentCols;
1237        }
1238    }
1239
1240    function tableCell($call) {
1241        if ( !$this->firstCell ) {
1242
1243            // Increase the span
1244            $lastCall = end($this->tableCalls);
1245
1246            // A cell call which follows an open cell means an empty cell so span
1247            if ( $lastCall[0] == 'tablecell_open' || $lastCall[0] == 'tableheader_open' ) {
1248                 $this->tableCalls[] = array('colspan',array(),$call[2]);
1249
1250            }
1251
1252            $this->tableCalls[] = array($this->lastCellType.'_close',array(),$call[2]);
1253            $this->tableCalls[] = array($call[0].'_open',array(1,NULL),$call[2]);
1254            $this->lastCellType = $call[0];
1255
1256        } else {
1257
1258            $this->tableCalls[] = array($call[0].'_open',array(1,NULL),$call[2]);
1259            $this->lastCellType = $call[0];
1260            $this->firstCell = false;
1261
1262        }
1263
1264        $this->currentCols++;
1265    }
1266
1267    function tableDefault($call) {
1268        $this->tableCalls[] = $call;
1269    }
1270
1271    function finalizeTable() {
1272
1273        // Add the max cols and rows to the table opening
1274        if ( $this->tableCalls[0][0] == 'table_open' ) {
1275            // Adjust to num cols not num col delimeters
1276            $this->tableCalls[0][1][] = $this->maxCols - 1;
1277            $this->tableCalls[0][1][] = $this->maxRows;
1278        } else {
1279            trigger_error('First element in table call list is not table_open');
1280        }
1281
1282        $lastRow = 0;
1283        $lastCell = 0;
1284        $toDelete = array();
1285
1286        // Look for the colspan elements and increment the colspan on the
1287        // previous non-empty opening cell. Once done, delete all the cells
1288        // that contain colspans
1289        foreach ( $this->tableCalls as $key => $call ) {
1290
1291            if ( $call[0] == 'tablerow_open' ) {
1292
1293                $lastRow = $key;
1294
1295            } else if ( $call[0] == 'tablecell_open' || $call[0] == 'tableheader_open' ) {
1296
1297                $lastCell = $key;
1298
1299            } else if ( $call[0] == 'table_align' ) {
1300
1301                // If the previous element was a cell open, align right
1302                if ( $this->tableCalls[$key-1][0] == 'tablecell_open' || $this->tableCalls[$key-1][0] == 'tableheader_open' ) {
1303                    $this->tableCalls[$key-1][1][1] = 'right';
1304
1305                // If the next element if the close of an element, align either center or left
1306                } else if ( $this->tableCalls[$key+1][0] == 'tablecell_close' || $this->tableCalls[$key+1][0] == 'tableheader_close' ) {
1307                    if ( $this->tableCalls[$lastCell][1][1] == 'right' ) {
1308                        $this->tableCalls[$lastCell][1][1] = 'center';
1309                    } else {
1310                        $this->tableCalls[$lastCell][1][1] = 'left';
1311                    }
1312
1313                }
1314
1315                // Now convert the whitespace back to cdata
1316                $this->tableCalls[$key][0] = 'cdata';
1317
1318            } else if ( $call[0] == 'colspan' ) {
1319
1320                $this->tableCalls[$key-1][1][0] = false;
1321
1322                for($i = $key-2; $i > $lastRow; $i--) {
1323
1324                    if ( $this->tableCalls[$i][0] == 'tablecell_open' || $this->tableCalls[$i][0] == 'tableheader_open' ) {
1325
1326                        if ( false !== $this->tableCalls[$i][1][0] ) {
1327                            $this->tableCalls[$i][1][0]++;
1328                            break;
1329                        }
1330
1331
1332                    }
1333                }
1334
1335                $toDelete[] = $key-1;
1336                $toDelete[] = $key;
1337                $toDelete[] = $key+1;
1338            }
1339        }
1340
1341
1342        // condense cdata
1343        $cnt = count($this->tableCalls);
1344        for( $key = 0; $key < $cnt; $key++){
1345            if($this->tableCalls[$key][0] == 'cdata'){
1346                $ckey = $key;
1347                $key++;
1348                while($this->tableCalls[$key][0] == 'cdata'){
1349                    $this->tableCalls[$ckey][1][0] .= $this->tableCalls[$key][1][0];
1350                    $toDelete[] = $key;
1351                    $key++;
1352                }
1353                continue;
1354            }
1355        }
1356
1357        foreach ( $toDelete as $delete ) {
1358            unset($this->tableCalls[$delete]);
1359        }
1360        $this->tableCalls = array_values($this->tableCalls);
1361    }
1362}
1363
1364//------------------------------------------------------------------------
1365class Doku_Handler_Section {
1366
1367    function process($calls) {
1368
1369        $sectionCalls = array();
1370        $inSection = false;
1371
1372        foreach ( $calls as $call ) {
1373
1374            if ( $call[0] == 'header' ) {
1375
1376                if ( $inSection ) {
1377                    $sectionCalls[] = array('section_close',array(), $call[2]);
1378                }
1379
1380                $sectionCalls[] = $call;
1381                $sectionCalls[] = array('section_open',array($call[1][1]), $call[2]);
1382                $inSection = true;
1383
1384            } else {
1385
1386                if ($call[0] == 'section_open' )  {
1387                    $inSection = true;
1388                } else if ($call[0] == 'section_open' ) {
1389                    $inSection = false;
1390                }
1391                $sectionCalls[] = $call;
1392            }
1393        }
1394
1395        if ( $inSection ) {
1396            $sectionCalls[] = array('section_close',array(), $call[2]);
1397        }
1398
1399        return $sectionCalls;
1400    }
1401
1402}
1403
1404/**
1405 * Handler for paragraphs
1406 *
1407 * @author Harry Fuecks <hfuecks@gmail.com>
1408 */
1409class Doku_Handler_Block {
1410
1411    var $calls = array();
1412
1413    var $blockStack = array();
1414
1415    var $inParagraph = false;
1416    var $atStart = true;
1417    var $skipEolKey = -1;
1418
1419    // Blocks these should not be inside paragraphs
1420    var $blockOpen = array(
1421            'header',
1422            'listu_open','listo_open','listitem_open','listcontent_open',
1423            'table_open','tablerow_open','tablecell_open','tableheader_open',
1424            'quote_open',
1425            'section_open', // Needed to prevent p_open between header and section_open
1426            'code','file','hr','preformatted','rss',
1427            'htmlblock','phpblock',
1428        );
1429
1430    var $blockClose = array(
1431            'header',
1432            'listu_close','listo_close','listitem_close','listcontent_close',
1433            'table_close','tablerow_close','tablecell_close','tableheader_close',
1434            'quote_close',
1435            'section_close', // Needed to prevent p_close after section_close
1436            'code','file','hr','preformatted','rss',
1437            'htmlblock','phpblock',
1438        );
1439
1440    // Stacks can contain paragraphs
1441    var $stackOpen = array(
1442        'footnote_open','section_open',
1443        );
1444
1445    var $stackClose = array(
1446        'footnote_close','section_close',
1447        );
1448
1449
1450    /**
1451     * Constructor. Adds loaded syntax plugins to the block and stack
1452     * arrays
1453     *
1454     * @author Andreas Gohr <andi@splitbrain.org>
1455     */
1456    function Doku_Handler_Block(){
1457        global $DOKU_PLUGINS;
1458        //check if syntax plugins were loaded
1459        if(empty($DOKU_PLUGINS['syntax'])) return;
1460        foreach($DOKU_PLUGINS['syntax'] as $n => $p){
1461            $ptype = $p->getPType();
1462            if($ptype == 'block'){
1463                $this->blockOpen[]  = 'plugin_'.$n;
1464                $this->blockClose[] = 'plugin_'.$n;
1465            }elseif($ptype == 'stack'){
1466                $this->stackOpen[]  = 'plugin_'.$n;
1467                $this->stackClose[] = 'plugin_'.$n;
1468            }
1469        }
1470    }
1471
1472    /**
1473     * Close a paragraph if needed
1474     *
1475     * This function makes sure there are no empty paragraphs on the stack
1476     *
1477     * @author Andreas Gohr <andi@splitbrain.org>
1478     */
1479    function closeParagraph($pos){
1480        // look back if there was any content - we don't want empty paragraphs
1481        $content = '';
1482        for($i=count($this->calls)-1; $i>=0; $i--){
1483            if($this->calls[$i][0] == 'p_open'){
1484                break;
1485            }elseif($this->calls[$i][0] == 'cdata'){
1486                $content .= $this->calls[$i][1][0];
1487            }else{
1488                $content = 'found markup';
1489                break;
1490            }
1491        }
1492
1493        if(trim($content)==''){
1494            //remove the whole paragraph
1495            array_splice($this->calls,$i);
1496        }else{
1497            if ($this->calls[count($this->calls)-1][0] == 'section_edit') {
1498                $tmp = array_pop($this->calls);
1499                $this->calls[] = array('p_close',array(), $pos);
1500                $this->calls[] = $tmp;
1501            } else {
1502                $this->calls[] = array('p_close',array(), $pos);
1503            }
1504        }
1505
1506        $this->inParagraph = false;
1507    }
1508
1509    /**
1510     * Processes the whole instruction stack to open and close paragraphs
1511     *
1512     * @author Harry Fuecks <hfuecks@gmail.com>
1513     * @author Andreas Gohr <andi@splitbrain.org>
1514     * @todo   This thing is really messy and should be rewritten
1515     */
1516    function process($calls) {
1517        foreach ( $calls as $key => $call ) {
1518            $cname = $call[0];
1519            if($cname == 'plugin') {
1520                $cname='plugin_'.$call[1][0];
1521
1522                $plugin = true;
1523                $plugin_open = (($call[1][2] == DOKU_LEXER_ENTER) || ($call[1][2] == DOKU_LEXER_SPECIAL));
1524                $plugin_close = (($call[1][2] == DOKU_LEXER_EXIT) || ($call[1][2] == DOKU_LEXER_SPECIAL));
1525            } else {
1526                $plugin = false;
1527            }
1528
1529            // Process blocks which are stack like... (contain linefeeds)
1530            if ( in_array($cname,$this->stackOpen ) && (!$plugin || $plugin_open) ) {
1531
1532                $this->calls[] = $call;
1533
1534                // Hack - footnotes shouldn't immediately contain a p_open
1535                if ( $cname != 'footnote_open' ) {
1536                    $this->addToStack();
1537                } else {
1538                    $this->addToStack(false);
1539                }
1540                continue;
1541            }
1542
1543            if ( in_array($cname,$this->stackClose ) && (!$plugin || $plugin_close)) {
1544
1545                if ( $this->inParagraph ) {
1546                    $this->closeParagraph($call[2]);
1547                }
1548                $this->calls[] = $call;
1549                $this->removeFromStack();
1550                continue;
1551            }
1552
1553            if ( !$this->atStart ) {
1554
1555                if ( $cname == 'eol' ) {
1556
1557                    // Check this isn't an eol instruction to skip...
1558                    if ( $this->skipEolKey != $key ) {
1559                        // Look to see if the next instruction is an EOL
1560                        if ( isset($calls[$key+1]) && $calls[$key+1][0] == 'eol' ) {
1561
1562                            if ( $this->inParagraph ) {
1563                                //$this->calls[] = array('p_close',array(), $call[2]);
1564                                $this->closeParagraph($call[2]);
1565                            }
1566
1567                            $this->calls[] = array('p_open',array(), $call[2]);
1568                            $this->inParagraph = true;
1569
1570
1571                            // Mark the next instruction for skipping
1572                            $this->skipEolKey = $key+1;
1573
1574                        }else{
1575                            //if this is just a single eol make a space from it
1576                            $this->calls[] = array('cdata',array(" "), $call[2]);
1577                        }
1578                    }
1579
1580
1581                } else {
1582
1583                    $storeCall = true;
1584                    if ( $this->inParagraph && (in_array($cname, $this->blockOpen) && (!$plugin || $plugin_open))) {
1585                        $this->closeParagraph($call[2]);
1586                        $this->calls[] = $call;
1587                        $storeCall = false;
1588                    }
1589
1590                    if ( in_array($cname, $this->blockClose) && (!$plugin || $plugin_close)) {
1591                        if ( $this->inParagraph ) {
1592                            $this->closeParagraph($call[2]);
1593                        }
1594                        if ( $storeCall ) {
1595                            $this->calls[] = $call;
1596                            $storeCall = false;
1597                        }
1598
1599                        // This really sucks and suggests this whole class sucks but...
1600                        if ( isset($calls[$key+1])) {
1601                            $cname_plusone = $calls[$key+1][0];
1602                            if ($cname_plusone == 'plugin') {
1603                                $cname_plusone = 'plugin'.$calls[$key+1][1][0];
1604
1605                                // plugin test, true if plugin has a state which precludes it requiring blockOpen or blockClose
1606                                $plugin_plusone = true;
1607                                $plugin_test = ($call[$key+1][1][2] == DOKU_LEXER_MATCHED) || ($call[$key+1][1][2] == DOKU_LEXER_MATCHED);
1608                            } else {
1609                                $plugin_plusone = false;
1610                            }
1611                            if ((!in_array($cname_plusone, $this->blockOpen) && !in_array($cname_plusone, $this->blockClose)) ||
1612                                ($plugin_plusone && $plugin_test)
1613                                ) {
1614
1615                                $this->calls[] = array('p_open',array(), $call[2]);
1616                                $this->inParagraph = true;
1617                            }
1618                        }
1619                    }
1620
1621                    if ( $storeCall ) {
1622                        $this->calls[] = $call;
1623                    }
1624
1625                }
1626
1627
1628            } else {
1629
1630                // Unless there's already a block at the start, start a paragraph
1631                if ( !in_array($cname,$this->blockOpen) ) {
1632                    $this->calls[] = array('p_open',array(), $call[2]);
1633                    if ( $call[0] != 'eol' ) {
1634                        $this->calls[] = $call;
1635                    }
1636                    $this->atStart = false;
1637                    $this->inParagraph = true;
1638                } else {
1639                    $this->calls[] = $call;
1640                    $this->atStart = false;
1641                }
1642
1643            }
1644
1645        }
1646
1647        if ( $this->inParagraph ) {
1648            if ( $cname == 'p_open' ) {
1649                // Ditch the last call
1650                array_pop($this->calls);
1651            } else if ( !in_array($cname, $this->blockClose) ) {
1652                //$this->calls[] = array('p_close',array(), $call[2]);
1653                $this->closeParagraph($call[2]);
1654            } else {
1655                $last_call = array_pop($this->calls);
1656                //$this->calls[] = array('p_close',array(), $call[2]);
1657                $this->closeParagraph($call[2]);
1658                $this->calls[] = $last_call;
1659            }
1660        }
1661
1662        return $this->calls;
1663    }
1664
1665    function addToStack($newStart = true) {
1666        $this->blockStack[] = array($this->atStart, $this->inParagraph);
1667        $this->atStart = $newStart;
1668        $this->inParagraph = false;
1669    }
1670
1671    function removeFromStack() {
1672        $state = array_pop($this->blockStack);
1673        $this->atStart = $state[0];
1674        $this->inParagraph = $state[1];
1675    }
1676}
1677
1678//Setup VIM: ex: et ts=4 enc=utf-8 :
1679