xref: /dokuwiki/inc/parser/handler.php (revision d85fc3586a46a48ee5896c125d867fb1aaa33b25)
1<?php
2if(!defined('DOKU_INC')) die('meh.');
3if (!defined('DOKU_PARSER_EOL')) define('DOKU_PARSER_EOL',"\n");   // add this to make handling test cases simpler
4
5class Doku_Handler {
6
7    var $Renderer = NULL;
8
9    var $CallWriter = NULL;
10
11    var $calls = array();
12
13    var $status = array(
14        'section' => false,
15        'section_edit_start' => -1,
16        'section_edit_level' => 1,
17        'section_edit_title' => ''
18    );
19
20    var $rewriteBlocks = true;
21
22    function Doku_Handler() {
23        $this->CallWriter = & new Doku_Handler_CallWriter($this);
24    }
25
26    function _addCall($handler, $args, $pos) {
27        $call = array($handler,$args, $pos);
28        $this->CallWriter->writeCall($call);
29    }
30
31    function addPluginCall($plugin, $args, $state, $pos, $match) {
32        $call = array('plugin',array($plugin, $args, $state, $match), $pos);
33        $this->CallWriter->writeCall($call);
34    }
35
36    function _finalize(){
37
38        $this->CallWriter->finalise();
39
40        if ( $this->status['section'] ) {
41           $last_call = end($this->calls);
42           array_push($this->calls,array('section_close',array(), $last_call[2]));
43           if ($this->status['section_edit_start']>1) {
44               // ignore last edit section if there is only one header
45               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]));
46           }
47        }
48
49        if ( $this->rewriteBlocks ) {
50            $B = & new Doku_Handler_Block();
51            $this->calls = $B->process($this->calls);
52        }
53
54        trigger_event('PARSER_HANDLER_DONE',$this);
55
56        array_unshift($this->calls,array('document_start',array(),0));
57        $last_call = end($this->calls);
58        array_push($this->calls,array('document_end',array(),$last_call[2]));
59    }
60
61    function fetch() {
62        $call = each($this->calls);
63        if ( $call ) {
64            return $call['value'];
65        }
66        return false;
67    }
68
69
70    /**
71     * Special plugin handler
72     *
73     * This handler is called for all modes starting with 'plugin_'.
74     * An additional parameter with the plugin name is passed
75     *
76     * @author Andreas Gohr <andi@splitbrain.org>
77     */
78    function plugin($match, $state, $pos, $pluginname){
79        $data = array($match);
80        $plugin =& plugin_load('syntax',$pluginname);
81        if($plugin != null){
82            $data = $plugin->handle($match, $state, $pos, $this);
83        }
84        if ($data !== false) {
85          $this->addPluginCall($pluginname,$data,$state,$pos,$match);
86        }
87        return true;
88    }
89
90    function base($match, $state, $pos) {
91        switch ( $state ) {
92            case DOKU_LEXER_UNMATCHED:
93                $this->_addCall('cdata',array($match), $pos);
94                return true;
95            break;
96        }
97    }
98
99    function header($match, $state, $pos) {
100        global $conf;
101
102        // get level and title
103        $title = trim($match);
104        $level = 7 - strspn($title,'=');
105        if($level < 1) $level = 1;
106        $title = trim($title,'=');
107        $title = trim($title);
108
109        if ($this->status['section']) $this->_addCall('section_close',array(),$pos);
110
111        if ($level<=$conf['maxseclevel']) {
112            $this->_addCall('section_edit',array($this->status['section_edit_start'], $pos-1, $this->status['section_edit_level'], $this->status['section_edit_title']), $pos);
113            $this->status['section_edit_start'] = $pos;
114            $this->status['section_edit_level'] = $level;
115            $this->status['section_edit_title'] = $title;
116        }
117
118        $this->_addCall('header',array($title,$level,$pos), $pos);
119
120        $this->_addCall('section_open',array($level),$pos);
121        $this->status['section'] = true;
122        return true;
123    }
124
125    function notoc($match, $state, $pos) {
126        $this->_addCall('notoc',array(),$pos);
127        return true;
128    }
129
130    function nocache($match, $state, $pos) {
131        $this->_addCall('nocache',array(),$pos);
132        return true;
133    }
134
135    function linebreak($match, $state, $pos) {
136        $this->_addCall('linebreak',array(),$pos);
137        return true;
138    }
139
140    function eol($match, $state, $pos) {
141        $this->_addCall('eol',array(),$pos);
142        return true;
143    }
144
145    function hr($match, $state, $pos) {
146        $this->_addCall('hr',array(),$pos);
147        return true;
148    }
149
150    function _nestingTag($match, $state, $pos, $name) {
151        switch ( $state ) {
152            case DOKU_LEXER_ENTER:
153                $this->_addCall($name.'_open', array(), $pos);
154            break;
155            case DOKU_LEXER_EXIT:
156                $this->_addCall($name.'_close', array(), $pos);
157            break;
158            case DOKU_LEXER_UNMATCHED:
159                $this->_addCall('cdata',array($match), $pos);
160            break;
161        }
162    }
163
164    function strong($match, $state, $pos) {
165        $this->_nestingTag($match, $state, $pos, 'strong');
166        return true;
167    }
168
169    function emphasis($match, $state, $pos) {
170        $this->_nestingTag($match, $state, $pos, 'emphasis');
171        return true;
172    }
173
174    function underline($match, $state, $pos) {
175        $this->_nestingTag($match, $state, $pos, 'underline');
176        return true;
177    }
178
179    function monospace($match, $state, $pos) {
180        $this->_nestingTag($match, $state, $pos, 'monospace');
181        return true;
182    }
183
184    function subscript($match, $state, $pos) {
185        $this->_nestingTag($match, $state, $pos, 'subscript');
186        return true;
187    }
188
189    function superscript($match, $state, $pos) {
190        $this->_nestingTag($match, $state, $pos, 'superscript');
191        return true;
192    }
193
194    function deleted($match, $state, $pos) {
195        $this->_nestingTag($match, $state, $pos, 'deleted');
196        return true;
197    }
198
199
200    function footnote($match, $state, $pos) {
201//        $this->_nestingTag($match, $state, $pos, 'footnote');
202        if (!isset($this->_footnote)) $this->_footnote = false;
203
204        switch ( $state ) {
205            case DOKU_LEXER_ENTER:
206                // footnotes can not be nested - however due to limitations in lexer it can't be prevented
207                // we will still enter a new footnote mode, we just do nothing
208                if ($this->_footnote) {
209                  $this->_addCall('cdata',array($match), $pos);
210                  break;
211                }
212
213                $this->_footnote = true;
214
215                $ReWriter = & new Doku_Handler_Nest($this->CallWriter,'footnote_close');
216                $this->CallWriter = & $ReWriter;
217                $this->_addCall('footnote_open', array(), $pos);
218            break;
219            case DOKU_LEXER_EXIT:
220                // check whether we have already exitted the footnote mode, can happen if the modes were nested
221                if (!$this->_footnote) {
222                  $this->_addCall('cdata',array($match), $pos);
223                  break;
224                }
225
226                $this->_footnote = false;
227
228                $this->_addCall('footnote_close', array(), $pos);
229                $this->CallWriter->process();
230                $ReWriter = & $this->CallWriter;
231                $this->CallWriter = & $ReWriter->CallWriter;
232            break;
233            case DOKU_LEXER_UNMATCHED:
234                $this->_addCall('cdata', array($match), $pos);
235            break;
236        }
237        return true;
238    }
239
240    function listblock($match, $state, $pos) {
241        switch ( $state ) {
242            case DOKU_LEXER_ENTER:
243                $ReWriter = & new Doku_Handler_List($this->CallWriter);
244                $this->CallWriter = & $ReWriter;
245                $this->_addCall('list_open', array($match), $pos);
246            break;
247            case DOKU_LEXER_EXIT:
248                $this->_addCall('list_close', array(), $pos);
249                $this->CallWriter->process();
250                $ReWriter = & $this->CallWriter;
251                $this->CallWriter = & $ReWriter->CallWriter;
252            break;
253            case DOKU_LEXER_MATCHED:
254                $this->_addCall('list_item', array($match), $pos);
255            break;
256            case DOKU_LEXER_UNMATCHED:
257                $this->_addCall('cdata', array($match), $pos);
258            break;
259        }
260        return true;
261    }
262
263    function unformatted($match, $state, $pos) {
264        if ( $state == DOKU_LEXER_UNMATCHED ) {
265            $this->_addCall('unformatted',array($match), $pos);
266        }
267        return true;
268    }
269
270    function php($match, $state, $pos) {
271        global $conf;
272        if ( $state == DOKU_LEXER_UNMATCHED ) {
273            $this->_addCall('php',array($match), $pos);
274        }
275        return true;
276    }
277
278    function phpblock($match, $state, $pos) {
279        global $conf;
280        if ( $state == DOKU_LEXER_UNMATCHED ) {
281            $this->_addCall('phpblock',array($match), $pos);
282        }
283        return true;
284    }
285
286    function html($match, $state, $pos) {
287        global $conf;
288        if ( $state == DOKU_LEXER_UNMATCHED ) {
289            $this->_addCall('html',array($match), $pos);
290        }
291        return true;
292    }
293
294    function htmlblock($match, $state, $pos) {
295        global $conf;
296        if ( $state == DOKU_LEXER_UNMATCHED ) {
297            $this->_addCall('htmlblock',array($match), $pos);
298        }
299        return true;
300    }
301
302    function preformatted($match, $state, $pos) {
303        switch ( $state ) {
304            case DOKU_LEXER_ENTER:
305                $ReWriter = & new Doku_Handler_Preformatted($this->CallWriter);
306                $this->CallWriter = & $ReWriter;
307                $this->_addCall('preformatted_start',array(), $pos);
308            break;
309            case DOKU_LEXER_EXIT:
310                $this->_addCall('preformatted_end',array(), $pos);
311                $this->CallWriter->process();
312                $ReWriter = & $this->CallWriter;
313                $this->CallWriter = & $ReWriter->CallWriter;
314            break;
315            case DOKU_LEXER_MATCHED:
316                $this->_addCall('preformatted_newline',array(), $pos);
317            break;
318            case DOKU_LEXER_UNMATCHED:
319                $this->_addCall('preformatted_content',array($match), $pos);
320            break;
321        }
322
323        return true;
324    }
325
326    function quote($match, $state, $pos) {
327
328        switch ( $state ) {
329
330            case DOKU_LEXER_ENTER:
331                $ReWriter = & new Doku_Handler_Quote($this->CallWriter);
332                $this->CallWriter = & $ReWriter;
333                $this->_addCall('quote_start',array($match), $pos);
334            break;
335
336            case DOKU_LEXER_EXIT:
337                $this->_addCall('quote_end',array(), $pos);
338                $this->CallWriter->process();
339                $ReWriter = & $this->CallWriter;
340                $this->CallWriter = & $ReWriter->CallWriter;
341            break;
342
343            case DOKU_LEXER_MATCHED:
344                $this->_addCall('quote_newline',array($match), $pos);
345            break;
346
347            case DOKU_LEXER_UNMATCHED:
348                $this->_addCall('cdata',array($match), $pos);
349            break;
350
351        }
352
353        return true;
354    }
355
356    function file($match, $state, $pos) {
357        return $this->code($match, $state, $pos, 'file');
358    }
359
360    function code($match, $state, $pos, $type='code') {
361        if ( $state == DOKU_LEXER_UNMATCHED ) {
362            $matches = explode('>',$match,2);
363            $matches[0] = trim($matches[0]);
364
365            list($language,$filename) = explode(' ',$matches[0],2);
366            $language = trim($language);
367            $filename = trim($filename);
368            if ( $language == '' )  $language = null;
369            if ( $language == '-' ) $language = null;
370            if ( $filename == '' )  $filename = null;
371            # We shortcut html here.
372            if($language == 'html') $language = 'html4strict';
373            $this->_addCall(
374                    $type,
375                    array($matches[1],$language,$filename),
376                    $pos
377                );
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 = explode('|',$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-Z0-9\.]+>{1}.*$/u',$link[0]) ) {
457        // Interwiki
458            $interwiki = explode('>',$link[0],2);
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 = explode('|',$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        // merge consecutive cdata
791        $unmerged_calls = $this->calls;
792        $this->calls = array();
793
794        foreach ($unmerged_calls as $call) $this->addCall($call);
795
796        $first_call = reset($this->calls);
797        $this->CallWriter->writeCall(array("nest", array($this->calls), $first_call[2]));
798    }
799
800    function addCall($call) {
801        $key = count($this->calls);
802        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
803            $this->calls[$key-1][1][0] .= $call[1][0];
804        } else {
805            $this->calls[] = $call;
806        }
807    }
808}
809
810class Doku_Handler_List {
811
812    var $CallWriter;
813
814    var $calls = array();
815    var $listCalls = array();
816    var $listStack = array();
817
818    function Doku_Handler_List(& $CallWriter) {
819        $this->CallWriter = & $CallWriter;
820    }
821
822    function writeCall($call) {
823        $this->calls[] = $call;
824    }
825
826    // Probably not needed but just in case...
827    function writeCalls($calls) {
828        $this->calls = array_merge($this->calls, $calls);
829#        $this->CallWriter->writeCalls($this->calls);
830    }
831
832    function finalise() {
833        $last_call = end($this->calls);
834        $this->writeCall(array('list_close',array(), $last_call[2]));
835
836        $this->process();
837        $this->CallWriter->finalise();
838    }
839
840    //------------------------------------------------------------------------
841    function process() {
842
843        foreach ( $this->calls as $call ) {
844            switch ($call[0]) {
845                case 'list_item':
846                    $this->listOpen($call);
847                break;
848                case 'list_open':
849                    $this->listStart($call);
850                break;
851                case 'list_close':
852                    $this->listEnd($call);
853                break;
854                default:
855                    $this->listContent($call);
856                break;
857            }
858        }
859
860        $this->CallWriter->writeCalls($this->listCalls);
861    }
862
863    //------------------------------------------------------------------------
864    function listStart($call) {
865        $depth = $this->interpretSyntax($call[1][0], $listType);
866
867        $this->initialDepth = $depth;
868        $this->listStack[] = array($listType, $depth);
869
870        $this->listCalls[] = array('list'.$listType.'_open',array(),$call[2]);
871        $this->listCalls[] = array('listitem_open',array(1),$call[2]);
872        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
873    }
874
875    //------------------------------------------------------------------------
876    function listEnd($call) {
877        $closeContent = true;
878
879        while ( $list = array_pop($this->listStack) ) {
880            if ( $closeContent ) {
881                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
882                $closeContent = false;
883            }
884            $this->listCalls[] = array('listitem_close',array(),$call[2]);
885            $this->listCalls[] = array('list'.$list[0].'_close', array(), $call[2]);
886        }
887    }
888
889    //------------------------------------------------------------------------
890    function listOpen($call) {
891        $depth = $this->interpretSyntax($call[1][0], $listType);
892        $end = end($this->listStack);
893
894        // Not allowed to be shallower than initialDepth
895        if ( $depth < $this->initialDepth ) {
896            $depth = $this->initialDepth;
897        }
898
899        //------------------------------------------------------------------------
900        if ( $depth == $end[1] ) {
901
902            // Just another item in the list...
903            if ( $listType == $end[0] ) {
904                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
905                $this->listCalls[] = array('listitem_close',array(),$call[2]);
906                $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
907                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
908
909            // Switched list type...
910            } else {
911
912                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
913                $this->listCalls[] = array('listitem_close',array(),$call[2]);
914                $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
915                $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
916                $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
917                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
918
919                array_pop($this->listStack);
920                $this->listStack[] = array($listType, $depth);
921            }
922
923        //------------------------------------------------------------------------
924        // Getting deeper...
925        } else if ( $depth > $end[1] ) {
926
927            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
928            $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
929            $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
930            $this->listCalls[] = array('listcontent_open',array(),$call[2]);
931
932            $this->listStack[] = array($listType, $depth);
933
934        //------------------------------------------------------------------------
935        // Getting shallower ( $depth < $end[1] )
936        } else {
937            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
938            $this->listCalls[] = array('listitem_close',array(),$call[2]);
939            $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
940
941            // Throw away the end - done
942            array_pop($this->listStack);
943
944            while (1) {
945                $end = end($this->listStack);
946
947                if ( $end[1] <= $depth ) {
948
949                    // Normalize depths
950                    $depth = $end[1];
951
952                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
953
954                    if ( $end[0] == $listType ) {
955                        $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
956                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
957
958                    } else {
959                        // Switching list type...
960                        $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
961                        $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
962                        $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
963                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
964
965                        array_pop($this->listStack);
966                        $this->listStack[] = array($listType, $depth);
967                    }
968
969                    break;
970
971                // Haven't dropped down far enough yet.... ( $end[1] > $depth )
972                } else {
973
974                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
975                    $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
976
977                    array_pop($this->listStack);
978
979                }
980
981            }
982
983        }
984    }
985
986    //------------------------------------------------------------------------
987    function listContent($call) {
988        $this->listCalls[] = $call;
989    }
990
991    //------------------------------------------------------------------------
992    function interpretSyntax($match, & $type) {
993        if ( substr($match,-1) == '*' ) {
994            $type = 'u';
995        } else {
996            $type = 'o';
997        }
998        // Is the +1 needed? It used to be count(explode(...))
999        // but I don't think the number is seen outside this handler
1000        return substr_count(str_replace("\t",'  ',$match), '  ') + 1;
1001    }
1002}
1003
1004//------------------------------------------------------------------------
1005class Doku_Handler_Preformatted {
1006
1007    var $CallWriter;
1008
1009    var $calls = array();
1010    var $pos;
1011    var $text ='';
1012
1013
1014
1015    function Doku_Handler_Preformatted(& $CallWriter) {
1016        $this->CallWriter = & $CallWriter;
1017    }
1018
1019    function writeCall($call) {
1020        $this->calls[] = $call;
1021    }
1022
1023    // Probably not needed but just in case...
1024    function writeCalls($calls) {
1025        $this->calls = array_merge($this->calls, $calls);
1026#        $this->CallWriter->writeCalls($this->calls);
1027    }
1028
1029    function finalise() {
1030        $last_call = end($this->calls);
1031        $this->writeCall(array('preformatted_end',array(), $last_call[2]));
1032
1033        $this->process();
1034        $this->CallWriter->finalise();
1035    }
1036
1037    function process() {
1038        foreach ( $this->calls as $call ) {
1039            switch ($call[0]) {
1040                case 'preformatted_start':
1041                    $this->pos = $call[2];
1042                break;
1043                case 'preformatted_newline':
1044                    $this->text .= "\n";
1045                break;
1046                case 'preformatted_content':
1047                    $this->text .= $call[1][0];
1048                break;
1049                case 'preformatted_end':
1050                    if (trim($this->text)) {
1051                      $this->CallWriter->writeCall(array('preformatted',array($this->text),$this->pos));
1052                    }
1053                    // see FS#1699 & FS#1652, add 'eol' instructions to ensure proper triggering of following p_open
1054                    $this->CallWriter->writeCall(array('eol',array(),$this->pos));
1055                    $this->CallWriter->writeCall(array('eol',array(),$this->pos));
1056                break;
1057            }
1058        }
1059    }
1060
1061}
1062
1063//------------------------------------------------------------------------
1064class Doku_Handler_Quote {
1065
1066    var $CallWriter;
1067
1068    var $calls = array();
1069
1070    var $quoteCalls = array();
1071
1072    function Doku_Handler_Quote(& $CallWriter) {
1073        $this->CallWriter = & $CallWriter;
1074    }
1075
1076    function writeCall($call) {
1077        $this->calls[] = $call;
1078    }
1079
1080    // Probably not needed but just in case...
1081    function writeCalls($calls) {
1082        $this->calls = array_merge($this->calls, $calls);
1083    }
1084
1085    function finalise() {
1086        $last_call = end($this->calls);
1087        $this->writeCall(array('quote_end',array(), $last_call[2]));
1088
1089        $this->process();
1090        $this->CallWriter->finalise();
1091    }
1092
1093    function process() {
1094
1095        $quoteDepth = 1;
1096
1097        foreach ( $this->calls as $call ) {
1098            switch ($call[0]) {
1099
1100                case 'quote_start':
1101
1102                    $this->quoteCalls[] = array('quote_open',array(),$call[2]);
1103
1104                case 'quote_newline':
1105
1106                    $quoteLength = $this->getDepth($call[1][0]);
1107
1108                    if ( $quoteLength > $quoteDepth ) {
1109                        $quoteDiff = $quoteLength - $quoteDepth;
1110                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
1111                            $this->quoteCalls[] = array('quote_open',array(),$call[2]);
1112                        }
1113                    } else if ( $quoteLength < $quoteDepth ) {
1114                        $quoteDiff = $quoteDepth - $quoteLength;
1115                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
1116                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
1117                        }
1118                    } else {
1119                        if ($call[0] != 'quote_start') $this->quoteCalls[] = array('linebreak',array(),$call[2]);
1120                    }
1121
1122                    $quoteDepth = $quoteLength;
1123
1124                break;
1125
1126                case 'quote_end':
1127
1128                    if ( $quoteDepth > 1 ) {
1129                        $quoteDiff = $quoteDepth - 1;
1130                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
1131                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
1132                        }
1133                    }
1134
1135                    $this->quoteCalls[] = array('quote_close',array(),$call[2]);
1136
1137                    $this->CallWriter->writeCalls($this->quoteCalls);
1138                break;
1139
1140                default:
1141                    $this->quoteCalls[] = $call;
1142                break;
1143            }
1144        }
1145    }
1146
1147    function getDepth($marker) {
1148        preg_match('/>{1,}/', $marker, $matches);
1149        $quoteLength = strlen($matches[0]);
1150        return $quoteLength;
1151    }
1152}
1153
1154//------------------------------------------------------------------------
1155class Doku_Handler_Table {
1156
1157    var $CallWriter;
1158
1159    var $calls = array();
1160    var $tableCalls = array();
1161    var $maxCols = 0;
1162    var $maxRows = 1;
1163    var $currentCols = 0;
1164    var $firstCell = false;
1165    var $lastCellType = 'tablecell';
1166
1167    function Doku_Handler_Table(& $CallWriter) {
1168        $this->CallWriter = & $CallWriter;
1169    }
1170
1171    function writeCall($call) {
1172        $this->calls[] = $call;
1173    }
1174
1175    // Probably not needed but just in case...
1176    function writeCalls($calls) {
1177        $this->calls = array_merge($this->calls, $calls);
1178    }
1179
1180    function finalise() {
1181        $last_call = end($this->calls);
1182        $this->writeCall(array('table_end',array(), $last_call[2]));
1183
1184        $this->process();
1185        $this->CallWriter->finalise();
1186    }
1187
1188    //------------------------------------------------------------------------
1189    function process() {
1190        foreach ( $this->calls as $call ) {
1191            switch ( $call[0] ) {
1192                case 'table_start':
1193                    $this->tableStart($call);
1194                break;
1195                case 'table_row':
1196                    $this->tableRowClose(array('tablerow_close',$call[1],$call[2]));
1197                    $this->tableRowOpen(array('tablerow_open',$call[1],$call[2]));
1198                break;
1199                case 'tableheader':
1200                case 'tablecell':
1201                    $this->tableCell($call);
1202                break;
1203                case 'table_end':
1204                    $this->tableRowClose(array('tablerow_close',$call[1],$call[2]));
1205                    $this->tableEnd($call);
1206                break;
1207                default:
1208                    $this->tableDefault($call);
1209                break;
1210            }
1211        }
1212        $this->CallWriter->writeCalls($this->tableCalls);
1213    }
1214
1215    function tableStart($call) {
1216        $this->tableCalls[] = array('table_open',array(),$call[2]);
1217        $this->tableCalls[] = array('tablerow_open',array(),$call[2]);
1218        $this->firstCell = true;
1219    }
1220
1221    function tableEnd($call) {
1222        $this->tableCalls[] = array('table_close',array(),$call[2]);
1223        $this->finalizeTable();
1224    }
1225
1226    function tableRowOpen($call) {
1227        $this->tableCalls[] = $call;
1228        $this->currentCols = 0;
1229        $this->firstCell = true;
1230        $this->lastCellType = 'tablecell';
1231        $this->maxRows++;
1232    }
1233
1234    function tableRowClose($call) {
1235        // Strip off final cell opening and anything after it
1236        while ( $discard = array_pop($this->tableCalls ) ) {
1237
1238            if ( $discard[0] == 'tablecell_open' || $discard[0] == 'tableheader_open') {
1239
1240                // Its a spanning element - put it back and close it
1241                if ( $discard[1][0] > 1 ) {
1242
1243                    $this->tableCalls[] = $discard;
1244                    if ( strstr($discard[0],'cell') ) {
1245                        $name = 'tablecell';
1246                    } else {
1247                        $name = 'tableheader';
1248                    }
1249                    $this->tableCalls[] = array($name.'_close',array(),$call[2]);
1250                }
1251
1252                break;
1253            }
1254        }
1255        $this->tableCalls[] = $call;
1256
1257        if ( $this->currentCols > $this->maxCols ) {
1258            $this->maxCols = $this->currentCols;
1259        }
1260    }
1261
1262    function tableCell($call) {
1263        if ( !$this->firstCell ) {
1264
1265            // Increase the span
1266            $lastCall = end($this->tableCalls);
1267
1268            // A cell call which follows an open cell means an empty cell so span
1269            if ( $lastCall[0] == 'tablecell_open' || $lastCall[0] == 'tableheader_open' ) {
1270                 $this->tableCalls[] = array('colspan',array(),$call[2]);
1271
1272            }
1273
1274            $this->tableCalls[] = array($this->lastCellType.'_close',array(),$call[2]);
1275            $this->tableCalls[] = array($call[0].'_open',array(1,NULL),$call[2]);
1276            $this->lastCellType = $call[0];
1277
1278        } else {
1279
1280            $this->tableCalls[] = array($call[0].'_open',array(1,NULL),$call[2]);
1281            $this->lastCellType = $call[0];
1282            $this->firstCell = false;
1283
1284        }
1285
1286        $this->currentCols++;
1287    }
1288
1289    function tableDefault($call) {
1290        $this->tableCalls[] = $call;
1291    }
1292
1293    function finalizeTable() {
1294
1295        // Add the max cols and rows to the table opening
1296        if ( $this->tableCalls[0][0] == 'table_open' ) {
1297            // Adjust to num cols not num col delimeters
1298            $this->tableCalls[0][1][] = $this->maxCols - 1;
1299            $this->tableCalls[0][1][] = $this->maxRows;
1300        } else {
1301            trigger_error('First element in table call list is not table_open');
1302        }
1303
1304        $lastRow = 0;
1305        $lastCell = 0;
1306        $toDelete = array();
1307
1308        // Look for the colspan elements and increment the colspan on the
1309        // previous non-empty opening cell. Once done, delete all the cells
1310        // that contain colspans
1311        foreach ( $this->tableCalls as $key => $call ) {
1312
1313            if ( $call[0] == 'tablerow_open' ) {
1314
1315                $lastRow = $key;
1316
1317            } else if ( $call[0] == 'tablecell_open' || $call[0] == 'tableheader_open' ) {
1318
1319                $lastCell = $key;
1320
1321            } else if ( $call[0] == 'table_align' ) {
1322
1323                // If the previous element was a cell open, align right
1324                if ( $this->tableCalls[$key-1][0] == 'tablecell_open' || $this->tableCalls[$key-1][0] == 'tableheader_open' ) {
1325                    $this->tableCalls[$key-1][1][1] = 'right';
1326
1327                // If the next element if the close of an element, align either center or left
1328                } else if ( $this->tableCalls[$key+1][0] == 'tablecell_close' || $this->tableCalls[$key+1][0] == 'tableheader_close' ) {
1329                    if ( $this->tableCalls[$lastCell][1][1] == 'right' ) {
1330                        $this->tableCalls[$lastCell][1][1] = 'center';
1331                    } else {
1332                        $this->tableCalls[$lastCell][1][1] = 'left';
1333                    }
1334
1335                }
1336
1337                // Now convert the whitespace back to cdata
1338                $this->tableCalls[$key][0] = 'cdata';
1339
1340            } else if ( $call[0] == 'colspan' ) {
1341
1342                $this->tableCalls[$key-1][1][0] = false;
1343
1344                for($i = $key-2; $i > $lastRow; $i--) {
1345
1346                    if ( $this->tableCalls[$i][0] == 'tablecell_open' || $this->tableCalls[$i][0] == 'tableheader_open' ) {
1347
1348                        if ( false !== $this->tableCalls[$i][1][0] ) {
1349                            $this->tableCalls[$i][1][0]++;
1350                            break;
1351                        }
1352
1353
1354                    }
1355                }
1356
1357                $toDelete[] = $key-1;
1358                $toDelete[] = $key;
1359                $toDelete[] = $key+1;
1360            }
1361        }
1362
1363
1364        // condense cdata
1365        $cnt = count($this->tableCalls);
1366        for( $key = 0; $key < $cnt; $key++){
1367            if($this->tableCalls[$key][0] == 'cdata'){
1368                $ckey = $key;
1369                $key++;
1370                while($this->tableCalls[$key][0] == 'cdata'){
1371                    $this->tableCalls[$ckey][1][0] .= $this->tableCalls[$key][1][0];
1372                    $toDelete[] = $key;
1373                    $key++;
1374                }
1375                continue;
1376            }
1377        }
1378
1379        foreach ( $toDelete as $delete ) {
1380            unset($this->tableCalls[$delete]);
1381        }
1382        $this->tableCalls = array_values($this->tableCalls);
1383    }
1384}
1385
1386//------------------------------------------------------------------------
1387class Doku_Handler_Section {
1388
1389    function process($calls) {
1390
1391        $sectionCalls = array();
1392        $inSection = false;
1393
1394        foreach ( $calls as $call ) {
1395
1396            if ( $call[0] == 'header' ) {
1397
1398                if ( $inSection ) {
1399                    $sectionCalls[] = array('section_close',array(), $call[2]);
1400                }
1401
1402                $sectionCalls[] = $call;
1403                $sectionCalls[] = array('section_open',array($call[1][1]), $call[2]);
1404                $inSection = true;
1405
1406            } else {
1407
1408                if ($call[0] == 'section_open' )  {
1409                    $inSection = true;
1410                } else if ($call[0] == 'section_open' ) {
1411                    $inSection = false;
1412                }
1413                $sectionCalls[] = $call;
1414            }
1415        }
1416
1417        if ( $inSection ) {
1418            $sectionCalls[] = array('section_close',array(), $call[2]);
1419        }
1420
1421        return $sectionCalls;
1422    }
1423
1424}
1425
1426/**
1427 * Handler for paragraphs
1428 *
1429 * @author Harry Fuecks <hfuecks@gmail.com>
1430 */
1431class Doku_Handler_Block {
1432
1433    var $calls = array();
1434
1435    var $blockStack = array();
1436
1437    var $inParagraph = false;
1438    var $atStart = true;
1439    var $skipEolKey = -1;
1440
1441    // Blocks these should not be inside paragraphs
1442    var $blockOpen = array(
1443            'header',
1444            'listu_open','listo_open','listitem_open','listcontent_open',
1445            'table_open','tablerow_open','tablecell_open','tableheader_open',
1446            'quote_open',
1447            'section_open', // Needed to prevent p_open between header and section_open
1448            'code','file','hr','preformatted','rss',
1449            'htmlblock','phpblock',
1450        );
1451
1452    var $blockClose = array(
1453            'header',
1454            'listu_close','listo_close','listitem_close','listcontent_close',
1455            'table_close','tablerow_close','tablecell_close','tableheader_close',
1456            'quote_close',
1457            'section_close', // Needed to prevent p_close after section_close
1458            'code','file','hr','preformatted','rss',
1459            'htmlblock','phpblock',
1460        );
1461
1462    // Stacks can contain paragraphs
1463    var $stackOpen = array(
1464        'footnote_open','section_open',
1465        );
1466
1467    var $stackClose = array(
1468        'footnote_close','section_close',
1469        );
1470
1471
1472    /**
1473     * Constructor. Adds loaded syntax plugins to the block and stack
1474     * arrays
1475     *
1476     * @author Andreas Gohr <andi@splitbrain.org>
1477     */
1478    function Doku_Handler_Block(){
1479        global $DOKU_PLUGINS;
1480        //check if syntax plugins were loaded
1481        if(empty($DOKU_PLUGINS['syntax'])) return;
1482        foreach($DOKU_PLUGINS['syntax'] as $n => $p){
1483            $ptype = $p->getPType();
1484            if($ptype == 'block'){
1485                $this->blockOpen[]  = 'plugin_'.$n;
1486                $this->blockClose[] = 'plugin_'.$n;
1487            }elseif($ptype == 'stack'){
1488                $this->stackOpen[]  = 'plugin_'.$n;
1489                $this->stackClose[] = 'plugin_'.$n;
1490            }
1491        }
1492    }
1493
1494    /**
1495     * Close a paragraph if needed
1496     *
1497     * This function makes sure there are no empty paragraphs on the stack
1498     *
1499     * @author Andreas Gohr <andi@splitbrain.org>
1500     */
1501    function closeParagraph($pos){
1502        // look back if there was any content - we don't want empty paragraphs
1503        $content = '';
1504        for($i=count($this->calls)-1; $i>=0; $i--){
1505            if($this->calls[$i][0] == 'p_open'){
1506                break;
1507            }elseif($this->calls[$i][0] == 'cdata'){
1508                $content .= $this->calls[$i][1][0];
1509            }else{
1510                $content = 'found markup';
1511                break;
1512            }
1513        }
1514
1515        if(trim($content)==''){
1516            //remove the whole paragraph
1517            array_splice($this->calls,$i);
1518        }else{
1519            if ($this->calls[count($this->calls)-1][0] == 'section_edit') {
1520                $tmp = array_pop($this->calls);
1521                $this->calls[] = array('p_close',array(), $pos);
1522                $this->calls[] = $tmp;
1523            } else {
1524                $this->calls[] = array('p_close',array(), $pos);
1525            }
1526        }
1527
1528        $this->inParagraph = false;
1529    }
1530
1531    /**
1532     * Processes the whole instruction stack to open and close paragraphs
1533     *
1534     * @author Harry Fuecks <hfuecks@gmail.com>
1535     * @author Andreas Gohr <andi@splitbrain.org>
1536     * @todo   This thing is really messy and should be rewritten
1537     */
1538    function process($calls) {
1539        foreach ( $calls as $key => $call ) {
1540            $cname = $call[0];
1541            if($cname == 'plugin') {
1542                $cname='plugin_'.$call[1][0];
1543
1544                $plugin = true;
1545                $plugin_open = (($call[1][2] == DOKU_LEXER_ENTER) || ($call[1][2] == DOKU_LEXER_SPECIAL));
1546                $plugin_close = (($call[1][2] == DOKU_LEXER_EXIT) || ($call[1][2] == DOKU_LEXER_SPECIAL));
1547            } else {
1548                $plugin = false;
1549            }
1550
1551            // Process blocks which are stack like... (contain linefeeds)
1552            if ( in_array($cname,$this->stackOpen ) && (!$plugin || $plugin_open) ) {
1553
1554                $this->calls[] = $call;
1555
1556                // Hack - footnotes shouldn't immediately contain a p_open
1557                if ( $cname != 'footnote_open' ) {
1558                    $this->addToStack();
1559                } else {
1560                    $this->addToStack(false);
1561                }
1562                continue;
1563            }
1564
1565            if ( in_array($cname,$this->stackClose ) && (!$plugin || $plugin_close)) {
1566
1567                if ( $this->inParagraph ) {
1568                    $this->closeParagraph($call[2]);
1569                }
1570                $this->calls[] = $call;
1571                $this->removeFromStack();
1572                continue;
1573            }
1574
1575            if ( !$this->atStart ) {
1576
1577                if ( $cname == 'eol' ) {
1578
1579                    // Check this isn't an eol instruction to skip...
1580                    if ( $this->skipEolKey != $key ) {
1581                        // Look to see if the next instruction is an EOL
1582                        if ( isset($calls[$key+1]) && $calls[$key+1][0] == 'eol' ) {
1583
1584                            if ( $this->inParagraph ) {
1585                                //$this->calls[] = array('p_close',array(), $call[2]);
1586                                $this->closeParagraph($call[2]);
1587                            }
1588
1589                            $this->calls[] = array('p_open',array(), $call[2]);
1590                            $this->inParagraph = true;
1591
1592
1593                            // Mark the next instruction for skipping
1594                            $this->skipEolKey = $key+1;
1595
1596                        }else{
1597                            //if this is just a single eol make a space from it
1598                            $this->addCall(array('cdata',array(DOKU_PARSER_EOL), $call[2]));
1599                        }
1600                    }
1601
1602
1603                } else {
1604
1605                    $storeCall = true;
1606                    if ( $this->inParagraph && (in_array($cname, $this->blockOpen) && (!$plugin || $plugin_open))) {
1607                        $this->closeParagraph($call[2]);
1608                        $this->calls[] = $call;
1609                        $storeCall = false;
1610                    }
1611
1612                    if ( in_array($cname, $this->blockClose) && (!$plugin || $plugin_close)) {
1613                        if ( $this->inParagraph ) {
1614                            $this->closeParagraph($call[2]);
1615                        }
1616                        if ( $storeCall ) {
1617                            $this->calls[] = $call;
1618                            $storeCall = false;
1619                        }
1620
1621                        // This really sucks and suggests this whole class sucks but...
1622                        if ( isset($calls[$key+1])) {
1623                            $cname_plusone = $calls[$key+1][0];
1624                            if ($cname_plusone == 'plugin') {
1625                                $cname_plusone = 'plugin'.$calls[$key+1][1][0];
1626
1627                                // plugin test, true if plugin has a state which precludes it requiring blockOpen or blockClose
1628                                $plugin_plusone = true;
1629                                $plugin_test = ($call[$key+1][1][2] == DOKU_LEXER_MATCHED) || ($call[$key+1][1][2] == DOKU_LEXER_MATCHED);
1630                            } else {
1631                                $plugin_plusone = false;
1632                            }
1633                            if ((!in_array($cname_plusone, $this->blockOpen) && !in_array($cname_plusone, $this->blockClose)) ||
1634                                ($plugin_plusone && $plugin_test)
1635                                ) {
1636
1637                                $this->calls[] = array('p_open',array(), $call[2]);
1638                                $this->inParagraph = true;
1639                            }
1640                        }
1641                    }
1642
1643                    if ( $storeCall ) {
1644                        $this->addCall($call);
1645                    }
1646
1647                }
1648
1649
1650            } else {
1651
1652                // Unless there's already a block at the start, start a paragraph
1653                if ( !in_array($cname,$this->blockOpen) ) {
1654                    $this->calls[] = array('p_open',array(), $call[2]);
1655                    if ( $call[0] != 'eol' ) {
1656                        $this->calls[] = $call;
1657                    }
1658                    $this->atStart = false;
1659                    $this->inParagraph = true;
1660                } else {
1661                    $this->addCall($call);
1662                    $this->atStart = false;
1663                }
1664
1665            }
1666
1667        }
1668
1669        if ( $this->inParagraph ) {
1670            if ( $cname == 'p_open' ) {
1671                // Ditch the last call
1672                array_pop($this->calls);
1673            } else if ( !in_array($cname, $this->blockClose) ) {
1674                //$this->calls[] = array('p_close',array(), $call[2]);
1675                $this->closeParagraph($call[2]);
1676            } else {
1677                $last_call = array_pop($this->calls);
1678                //$this->calls[] = array('p_close',array(), $call[2]);
1679                $this->closeParagraph($call[2]);
1680                $this->calls[] = $last_call;
1681            }
1682        }
1683
1684        return $this->calls;
1685    }
1686
1687    function addToStack($newStart = true) {
1688        $this->blockStack[] = array($this->atStart, $this->inParagraph);
1689        $this->atStart = $newStart;
1690        $this->inParagraph = false;
1691    }
1692
1693    function removeFromStack() {
1694        $state = array_pop($this->blockStack);
1695        $this->atStart = $state[0];
1696        $this->inParagraph = $state[1];
1697    }
1698
1699    function addCall($call) {
1700        $key = count($this->calls);
1701        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
1702            $this->calls[$key-1][1][0] .= $call[1][0];
1703        } else {
1704            $this->calls[] = $call;
1705        }
1706    }
1707}
1708
1709//Setup VIM: ex: et ts=4 enc=utf-8 :
1710