xref: /dokuwiki/inc/parser/handler.php (revision eb13b20f8f5809ddeb740a7293f37976cdb1ea49)
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 file($match, $state, $pos) {
327        if ( $state == DOKU_LEXER_UNMATCHED ) {
328            $this->_addCall('file',array($match), $pos);
329        }
330        return true;
331    }
332
333    function quote($match, $state, $pos) {
334
335        switch ( $state ) {
336
337            case DOKU_LEXER_ENTER:
338                $ReWriter = & new Doku_Handler_Quote($this->CallWriter);
339                $this->CallWriter = & $ReWriter;
340                $this->_addCall('quote_start',array($match), $pos);
341            break;
342
343            case DOKU_LEXER_EXIT:
344                $this->_addCall('quote_end',array(), $pos);
345                $this->CallWriter->process();
346                $ReWriter = & $this->CallWriter;
347                $this->CallWriter = & $ReWriter->CallWriter;
348            break;
349
350            case DOKU_LEXER_MATCHED:
351                $this->_addCall('quote_newline',array($match), $pos);
352            break;
353
354            case DOKU_LEXER_UNMATCHED:
355                $this->_addCall('cdata',array($match), $pos);
356            break;
357
358        }
359
360        return true;
361    }
362
363    function code($match, $state, $pos) {
364        switch ( $state ) {
365            case DOKU_LEXER_UNMATCHED:
366                $matches = preg_split('/>/u',$match,2);
367                $matches[0] = trim($matches[0]);
368                if ( trim($matches[0]) == '' ) {
369                    $matches[0] = NULL;
370                }
371                # $matches[0] contains name of programming language
372                # if available, We shortcut html here.
373                if($matches[0] == 'html') $matches[0] = 'html4strict';
374                $this->_addCall(
375                        'code',
376                        array($matches[1],$matches[0]),
377                        $pos
378                    );
379            break;
380        }
381        return true;
382    }
383
384    function acronym($match, $state, $pos) {
385        $this->_addCall('acronym',array($match), $pos);
386        return true;
387    }
388
389    function smiley($match, $state, $pos) {
390        $this->_addCall('smiley',array($match), $pos);
391        return true;
392    }
393
394    function wordblock($match, $state, $pos) {
395        $this->_addCall('wordblock',array($match), $pos);
396        return true;
397    }
398
399    function entity($match, $state, $pos) {
400        $this->_addCall('entity',array($match), $pos);
401        return true;
402    }
403
404    function multiplyentity($match, $state, $pos) {
405        preg_match_all('/\d+/',$match,$matches);
406        $this->_addCall('multiplyentity',array($matches[0][0],$matches[0][1]), $pos);
407        return true;
408    }
409
410    function singlequoteopening($match, $state, $pos) {
411        $this->_addCall('singlequoteopening',array(), $pos);
412        return true;
413    }
414
415    function singlequoteclosing($match, $state, $pos) {
416        $this->_addCall('singlequoteclosing',array(), $pos);
417        return true;
418    }
419
420    function apostrophe($match, $state, $pos) {
421        $this->_addCall('apostrophe',array(), $pos);
422        return true;
423    }
424
425    function doublequoteopening($match, $state, $pos) {
426        $this->_addCall('doublequoteopening',array(), $pos);
427        return true;
428    }
429
430    function doublequoteclosing($match, $state, $pos) {
431        $this->_addCall('doublequoteclosing',array(), $pos);
432        return true;
433    }
434
435    function camelcaselink($match, $state, $pos) {
436        $this->_addCall('camelcaselink',array($match), $pos);
437        return true;
438    }
439
440    /*
441    */
442    function internallink($match, $state, $pos) {
443        // Strip the opening and closing markup
444        $link = preg_replace(array('/^\[\[/','/\]\]$/u'),'',$match);
445
446        // Split title from URL
447        $link = preg_split('/\|/u',$link,2);
448        if ( !isset($link[1]) ) {
449            $link[1] = NULL;
450        } else if ( preg_match('/^\{\{[^\}]+\}\}$/',$link[1]) ) {
451            // If the title is an image, convert it to an array containing the image details
452            $link[1] = Doku_Handler_Parse_Media($link[1]);
453        }
454        $link[0] = trim($link[0]);
455
456        //decide which kind of link it is
457
458        if ( preg_match('/^[a-zA-Z0-9\.]+>{1}.*$/u',$link[0]) ) {
459        // Interwiki
460            $interwiki = preg_split('/>/u',$link[0]);
461            $this->_addCall(
462                'interwikilink',
463                array($link[0],$link[1],strtolower($interwiki[0]),$interwiki[1]),
464                $pos
465                );
466        }elseif ( preg_match('/^\\\\\\\\[\w.:?\-;,]+?\\\\/u',$link[0]) ) {
467        // Windows Share
468            $this->_addCall(
469                'windowssharelink',
470                array($link[0],$link[1]),
471                $pos
472                );
473        }elseif ( preg_match('#^([a-z0-9\-\.+]+?)://#i',$link[0]) ) {
474        // external link (accepts all protocols)
475            $this->_addCall(
476                    'externallink',
477                    array($link[0],$link[1]),
478                    $pos
479                    );
480        }elseif ( preg_match('<'.PREG_PATTERN_VALID_EMAIL.'>',$link[0]) ) {
481        // E-Mail (pattern above is defined in inc/mail.php)
482            $this->_addCall(
483                'emaillink',
484                array($link[0],$link[1]),
485                $pos
486                );
487        }elseif ( preg_match('!^#.+!',$link[0]) ){
488        // local link
489            $this->_addCall(
490                'locallink',
491                array(substr($link[0],1),$link[1]),
492                $pos
493                );
494        }else{
495        // internal link
496            $this->_addCall(
497                'internallink',
498                array($link[0],$link[1]),
499                $pos
500                );
501        }
502
503        return true;
504    }
505
506    function filelink($match, $state, $pos) {
507        $this->_addCall('filelink',array($match, NULL), $pos);
508        return true;
509    }
510
511    function windowssharelink($match, $state, $pos) {
512        $this->_addCall('windowssharelink',array($match, NULL), $pos);
513        return true;
514    }
515
516    function media($match, $state, $pos) {
517        $p = Doku_Handler_Parse_Media($match);
518
519        $this->_addCall(
520              $p['type'],
521              array($p['src'], $p['title'], $p['align'], $p['width'],
522                     $p['height'], $p['cache'], $p['linking']),
523              $pos
524             );
525        return true;
526    }
527
528    function rss($match, $state, $pos) {
529        $link = preg_replace(array('/^\{\{rss>/','/\}\}$/'),'',$match);
530
531        // get params
532        list($link,$params) = explode(' ',$link,2);
533
534        $p = array();
535        if(preg_match('/\b(\d+)\b/',$params,$match)){
536            $p['max'] = $match[1];
537        }else{
538            $p['max'] = 8;
539        }
540        $p['reverse'] = (preg_match('/rev/',$params));
541        $p['author']  = (preg_match('/\b(by|author)/',$params));
542        $p['date']    = (preg_match('/\b(date)/',$params));
543        $p['details'] = (preg_match('/\b(desc|detail)/',$params));
544
545        if (preg_match('/\b(\d+)([dhm])\b/',$params,$match)) {
546          $period = array('d' => 86400, 'h' => 3600, 'm' => 60);
547          $p['refresh'] = max(600,$match[1]*$period[$match[2]]);  // n * period in seconds, minimum 10 minutes
548        } else {
549          $p['refresh'] = 14400;   // default to 4 hours
550        }
551
552        $this->_addCall('rss',array($link,$p),$pos);
553        return true;
554    }
555
556    function externallink($match, $state, $pos) {
557        $url   = $match;
558        $title = null;
559
560        // add protocol on simple short URLs
561        if(substr($url,0,3) == 'ftp' && (substr($url,0,6) != 'ftp://')){
562            $title = $url;
563            $url   = 'ftp://'.$url;
564        }
565        if(substr($url,0,3) == 'www' && (substr($url,0,7) != 'http://')){
566            $title = $url;
567            $url = 'http://'.$url;
568        }
569
570        $this->_addCall('externallink',array($url, $title), $pos);
571        return true;
572    }
573
574    function emaillink($match, $state, $pos) {
575        $email = preg_replace(array('/^</','/>$/'),'',$match);
576        $this->_addCall('emaillink',array($email, NULL), $pos);
577        return true;
578    }
579
580    function table($match, $state, $pos) {
581        switch ( $state ) {
582
583            case DOKU_LEXER_ENTER:
584
585                $ReWriter = & new Doku_Handler_Table($this->CallWriter);
586                $this->CallWriter = & $ReWriter;
587
588                $this->_addCall('table_start', array(), $pos);
589                //$this->_addCall('table_row', array(), $pos);
590                if ( trim($match) == '^' ) {
591                    $this->_addCall('tableheader', array(), $pos);
592                } else {
593                    $this->_addCall('tablecell', array(), $pos);
594                }
595            break;
596
597            case DOKU_LEXER_EXIT:
598                $this->_addCall('table_end', array(), $pos);
599                $this->CallWriter->process();
600                $ReWriter = & $this->CallWriter;
601                $this->CallWriter = & $ReWriter->CallWriter;
602            break;
603
604            case DOKU_LEXER_UNMATCHED:
605                if ( trim($match) != '' ) {
606                    $this->_addCall('cdata',array($match), $pos);
607                }
608            break;
609
610            case DOKU_LEXER_MATCHED:
611                if ( $match == ' ' ){
612                    $this->_addCall('cdata', array($match), $pos);
613                } else if ( preg_match('/\t+/',$match) ) {
614                    $this->_addCall('table_align', array($match), $pos);
615                } else if ( preg_match('/ {2,}/',$match) ) {
616                    $this->_addCall('table_align', array($match), $pos);
617                } else if ( $match == "\n|" ) {
618                    $this->_addCall('table_row', array(), $pos);
619                    $this->_addCall('tablecell', array(), $pos);
620                } else if ( $match == "\n^" ) {
621                    $this->_addCall('table_row', array(), $pos);
622                    $this->_addCall('tableheader', array(), $pos);
623                } else if ( $match == '|' ) {
624                    $this->_addCall('tablecell', array(), $pos);
625                } else if ( $match == '^' ) {
626                    $this->_addCall('tableheader', array(), $pos);
627                }
628            break;
629        }
630        return true;
631    }
632}
633
634//------------------------------------------------------------------------
635function Doku_Handler_Parse_Media($match) {
636
637    // Strip the opening and closing markup
638    $link = preg_replace(array('/^\{\{/','/\}\}$/u'),'',$match);
639
640    // Split title from URL
641    $link = preg_split('/\|/u',$link,2);
642
643
644    // Check alignment
645    $ralign = (bool)preg_match('/^ /',$link[0]);
646    $lalign = (bool)preg_match('/ $/',$link[0]);
647
648    // Logic = what's that ;)...
649    if ( $lalign & $ralign ) {
650        $align = 'center';
651    } else if ( $ralign ) {
652        $align = 'right';
653    } else if ( $lalign ) {
654        $align = 'left';
655    } else {
656        $align = NULL;
657    }
658
659    // The title...
660    if ( !isset($link[1]) ) {
661        $link[1] = NULL;
662    }
663
664    //remove aligning spaces
665    $link[0] = trim($link[0]);
666
667    //split into src and parameters (using the very last questionmark)
668    $pos = strrpos($link[0], '?');
669    if($pos !== false){
670        $src   = substr($link[0],0,$pos);
671        $param = substr($link[0],$pos+1);
672    }else{
673        $src   = $link[0];
674        $param = '';
675    }
676
677    //parse width and height
678    if(preg_match('#(\d+)(x(\d+))?#i',$param,$size)){
679        ($size[1]) ? $w = $size[1] : $w = NULL;
680        ($size[3]) ? $h = $size[3] : $h = NULL;
681    } else {
682        $w = NULL;
683        $h = NULL;
684    }
685
686    //get linking command
687    if(preg_match('/nolink/i',$param)){
688        $linking = 'nolink';
689    }else if(preg_match('/direct/i',$param)){
690        $linking = 'direct';
691    }else if(preg_match('/linkonly/i',$param)){
692        $linking = 'linkonly';
693    }else{
694        $linking = 'details';
695    }
696
697    //get caching command
698    if (preg_match('/(nocache|recache)/i',$param,$cachemode)){
699        $cache = $cachemode[1];
700    }else{
701        $cache = 'cache';
702    }
703
704    // Check whether this is a local or remote image
705    if ( preg_match('#^(https?|ftp)#i',$src) ) {
706        $call = 'externalmedia';
707    } else {
708        $call = 'internalmedia';
709    }
710
711    $params = array(
712        'type'=>$call,
713        'src'=>$src,
714        'title'=>$link[1],
715        'align'=>$align,
716        'width'=>$w,
717        'height'=>$h,
718        'cache'=>$cache,
719        'linking'=>$linking,
720    );
721
722    return $params;
723}
724
725//------------------------------------------------------------------------
726class Doku_Handler_CallWriter {
727
728    var $Handler;
729
730    function Doku_Handler_CallWriter(& $Handler) {
731        $this->Handler = & $Handler;
732    }
733
734    function writeCall($call) {
735        $this->Handler->calls[] = $call;
736    }
737
738    function writeCalls($calls) {
739        $this->Handler->calls = array_merge($this->Handler->calls, $calls);
740    }
741
742    // function is required, but since this call writer is first/highest in
743    // the chain it is not required to do anything
744    function finalise() {
745    }
746}
747
748//------------------------------------------------------------------------
749/**
750 * Generic call writer class to handle nesting of rendering instructions
751 * within a render instruction. Also see nest() method of renderer base class
752 *
753 * @author    Chris Smith <chris@jalakai.co.uk>
754 */
755class Doku_Handler_Nest {
756
757    var $CallWriter;
758    var $calls = array();
759
760    var $closingInstruction;
761
762    /**
763     * constructor
764     *
765     * @param  object     $CallWriter     the renderers current call writer
766     * @param  string     $close          closing instruction name, this is required to properly terminate the
767     *                                    syntax mode if the document ends without a closing pattern
768     */
769    function Doku_Handler_Nest(& $CallWriter, $close="nest_close") {
770        $this->CallWriter = & $CallWriter;
771
772        $this->closingInstruction = $close;
773    }
774
775    function writeCall($call) {
776        $this->calls[] = $call;
777    }
778
779    function writeCalls($calls) {
780        $this->calls = array_merge($this->calls, $calls);
781    }
782
783    function finalise() {
784        $last_call = end($this->calls);
785        $this->writeCall(array($this->closingInstruction,array(), $last_call[2]));
786
787        $this->process();
788        $this->CallWriter->finalise();
789    }
790
791    function process() {
792        // merge consecutive cdata
793        $unmerged_calls = $this->calls;
794        $this->calls = array();
795
796        foreach ($unmerged_calls as $call) $this->addCall($call);
797
798        $first_call = reset($this->calls);
799        $this->CallWriter->writeCall(array("nest", array($this->calls), $first_call[2]));
800    }
801
802    function addCall($call) {
803        $key = count($this->calls);
804        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
805            $this->calls[$key-1][1][0] .= $call[1][0];
806        } else {
807            $this->calls[] = $call;
808        }
809    }
810}
811
812class Doku_Handler_List {
813
814    var $CallWriter;
815
816    var $calls = array();
817    var $listCalls = array();
818    var $listStack = array();
819
820    function Doku_Handler_List(& $CallWriter) {
821        $this->CallWriter = & $CallWriter;
822    }
823
824    function writeCall($call) {
825        $this->calls[] = $call;
826    }
827
828    // Probably not needed but just in case...
829    function writeCalls($calls) {
830        $this->calls = array_merge($this->calls, $calls);
831#        $this->CallWriter->writeCalls($this->calls);
832    }
833
834    function finalise() {
835        $last_call = end($this->calls);
836        $this->writeCall(array('list_close',array(), $last_call[2]));
837
838        $this->process();
839        $this->CallWriter->finalise();
840    }
841
842    //------------------------------------------------------------------------
843    function process() {
844
845        foreach ( $this->calls as $call ) {
846            switch ($call[0]) {
847                case 'list_item':
848                    $this->listOpen($call);
849                break;
850                case 'list_open':
851                    $this->listStart($call);
852                break;
853                case 'list_close':
854                    $this->listEnd($call);
855                break;
856                default:
857                    $this->listContent($call);
858                break;
859            }
860        }
861
862        $this->CallWriter->writeCalls($this->listCalls);
863    }
864
865    //------------------------------------------------------------------------
866    function listStart($call) {
867        $depth = $this->interpretSyntax($call[1][0], $listType);
868
869        $this->initialDepth = $depth;
870        $this->listStack[] = array($listType, $depth);
871
872        $this->listCalls[] = array('list'.$listType.'_open',array(),$call[2]);
873        $this->listCalls[] = array('listitem_open',array(1),$call[2]);
874        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
875    }
876
877    //------------------------------------------------------------------------
878    function listEnd($call) {
879        $closeContent = true;
880
881        while ( $list = array_pop($this->listStack) ) {
882            if ( $closeContent ) {
883                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
884                $closeContent = false;
885            }
886            $this->listCalls[] = array('listitem_close',array(),$call[2]);
887            $this->listCalls[] = array('list'.$list[0].'_close', array(), $call[2]);
888        }
889    }
890
891    //------------------------------------------------------------------------
892    function listOpen($call) {
893        $depth = $this->interpretSyntax($call[1][0], $listType);
894        $end = end($this->listStack);
895
896        // Not allowed to be shallower than initialDepth
897        if ( $depth < $this->initialDepth ) {
898            $depth = $this->initialDepth;
899        }
900
901        //------------------------------------------------------------------------
902        if ( $depth == $end[1] ) {
903
904            // Just another item in the list...
905            if ( $listType == $end[0] ) {
906                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
907                $this->listCalls[] = array('listitem_close',array(),$call[2]);
908                $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
909                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
910
911            // Switched list type...
912            } else {
913
914                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
915                $this->listCalls[] = array('listitem_close',array(),$call[2]);
916                $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
917                $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
918                $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
919                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
920
921                array_pop($this->listStack);
922                $this->listStack[] = array($listType, $depth);
923            }
924
925        //------------------------------------------------------------------------
926        // Getting deeper...
927        } else if ( $depth > $end[1] ) {
928
929            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
930            $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
931            $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
932            $this->listCalls[] = array('listcontent_open',array(),$call[2]);
933
934            $this->listStack[] = array($listType, $depth);
935
936        //------------------------------------------------------------------------
937        // Getting shallower ( $depth < $end[1] )
938        } else {
939            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
940            $this->listCalls[] = array('listitem_close',array(),$call[2]);
941            $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
942
943            // Throw away the end - done
944            array_pop($this->listStack);
945
946            while (1) {
947                $end = end($this->listStack);
948
949                if ( $end[1] <= $depth ) {
950
951                    // Normalize depths
952                    $depth = $end[1];
953
954                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
955
956                    if ( $end[0] == $listType ) {
957                        $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
958                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
959
960                    } else {
961                        // Switching list type...
962                        $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
963                        $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
964                        $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
965                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
966
967                        array_pop($this->listStack);
968                        $this->listStack[] = array($listType, $depth);
969                    }
970
971                    break;
972
973                // Haven't dropped down far enough yet.... ( $end[1] > $depth )
974                } else {
975
976                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
977                    $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
978
979                    array_pop($this->listStack);
980
981                }
982
983            }
984
985        }
986    }
987
988    //------------------------------------------------------------------------
989    function listContent($call) {
990        $this->listCalls[] = $call;
991    }
992
993    //------------------------------------------------------------------------
994    function interpretSyntax($match, & $type) {
995        if ( substr($match,-1) == '*' ) {
996            $type = 'u';
997        } else {
998            $type = 'o';
999        }
1000        return count(explode('  ',str_replace("\t",'  ',$match)));
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                break;
1054            }
1055        }
1056    }
1057
1058}
1059
1060//------------------------------------------------------------------------
1061class Doku_Handler_Quote {
1062
1063    var $CallWriter;
1064
1065    var $calls = array();
1066
1067    var $quoteCalls = array();
1068
1069    function Doku_Handler_Quote(& $CallWriter) {
1070        $this->CallWriter = & $CallWriter;
1071    }
1072
1073    function writeCall($call) {
1074        $this->calls[] = $call;
1075    }
1076
1077    // Probably not needed but just in case...
1078    function writeCalls($calls) {
1079        $this->calls = array_merge($this->calls, $calls);
1080    }
1081
1082    function finalise() {
1083        $last_call = end($this->calls);
1084        $this->writeCall(array('quote_end',array(), $last_call[2]));
1085
1086        $this->process();
1087        $this->CallWriter->finalise();
1088    }
1089
1090    function process() {
1091
1092        $quoteDepth = 1;
1093
1094        foreach ( $this->calls as $call ) {
1095            switch ($call[0]) {
1096
1097                case 'quote_start':
1098
1099                    $this->quoteCalls[] = array('quote_open',array(),$call[2]);
1100
1101                case 'quote_newline':
1102
1103                    $quoteLength = $this->getDepth($call[1][0]);
1104
1105                    if ( $quoteLength > $quoteDepth ) {
1106                        $quoteDiff = $quoteLength - $quoteDepth;
1107                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
1108                            $this->quoteCalls[] = array('quote_open',array(),$call[2]);
1109                        }
1110                    } else if ( $quoteLength < $quoteDepth ) {
1111                        $quoteDiff = $quoteDepth - $quoteLength;
1112                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
1113                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
1114                        }
1115                    } else {
1116                        if ($call[0] != 'quote_start') $this->quoteCalls[] = array('linebreak',array(),$call[2]);
1117                    }
1118
1119                    $quoteDepth = $quoteLength;
1120
1121                break;
1122
1123                case 'quote_end':
1124
1125                    if ( $quoteDepth > 1 ) {
1126                        $quoteDiff = $quoteDepth - 1;
1127                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
1128                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
1129                        }
1130                    }
1131
1132                    $this->quoteCalls[] = array('quote_close',array(),$call[2]);
1133
1134                    $this->CallWriter->writeCalls($this->quoteCalls);
1135                break;
1136
1137                default:
1138                    $this->quoteCalls[] = $call;
1139                break;
1140            }
1141        }
1142    }
1143
1144    function getDepth($marker) {
1145        preg_match('/>{1,}/', $marker, $matches);
1146        $quoteLength = strlen($matches[0]);
1147        return $quoteLength;
1148    }
1149}
1150
1151//------------------------------------------------------------------------
1152class Doku_Handler_Table {
1153
1154    var $CallWriter;
1155
1156    var $calls = array();
1157    var $tableCalls = array();
1158    var $maxCols = 0;
1159    var $maxRows = 1;
1160    var $currentCols = 0;
1161    var $firstCell = false;
1162    var $lastCellType = 'tablecell';
1163
1164    function Doku_Handler_Table(& $CallWriter) {
1165        $this->CallWriter = & $CallWriter;
1166    }
1167
1168    function writeCall($call) {
1169        $this->calls[] = $call;
1170    }
1171
1172    // Probably not needed but just in case...
1173    function writeCalls($calls) {
1174        $this->calls = array_merge($this->calls, $calls);
1175    }
1176
1177    function finalise() {
1178        $last_call = end($this->calls);
1179        $this->writeCall(array('table_end',array(), $last_call[2]));
1180
1181        $this->process();
1182        $this->CallWriter->finalise();
1183    }
1184
1185    //------------------------------------------------------------------------
1186    function process() {
1187        foreach ( $this->calls as $call ) {
1188            switch ( $call[0] ) {
1189                case 'table_start':
1190                    $this->tableStart($call);
1191                break;
1192                case 'table_row':
1193                    $this->tableRowClose(array('tablerow_close',$call[1],$call[2]));
1194                    $this->tableRowOpen(array('tablerow_open',$call[1],$call[2]));
1195                break;
1196                case 'tableheader':
1197                case 'tablecell':
1198                    $this->tableCell($call);
1199                break;
1200                case 'table_end':
1201                    $this->tableRowClose(array('tablerow_close',$call[1],$call[2]));
1202                    $this->tableEnd($call);
1203                break;
1204                default:
1205                    $this->tableDefault($call);
1206                break;
1207            }
1208        }
1209        $this->CallWriter->writeCalls($this->tableCalls);
1210    }
1211
1212    function tableStart($call) {
1213        $this->tableCalls[] = array('table_open',array(),$call[2]);
1214        $this->tableCalls[] = array('tablerow_open',array(),$call[2]);
1215        $this->firstCell = true;
1216    }
1217
1218    function tableEnd($call) {
1219        $this->tableCalls[] = array('table_close',array(),$call[2]);
1220        $this->finalizeTable();
1221    }
1222
1223    function tableRowOpen($call) {
1224        $this->tableCalls[] = $call;
1225        $this->currentCols = 0;
1226        $this->firstCell = true;
1227        $this->lastCellType = 'tablecell';
1228        $this->maxRows++;
1229    }
1230
1231    function tableRowClose($call) {
1232        // Strip off final cell opening and anything after it
1233        while ( $discard = array_pop($this->tableCalls ) ) {
1234
1235            if ( $discard[0] == 'tablecell_open' || $discard[0] == 'tableheader_open') {
1236
1237                // Its a spanning element - put it back and close it
1238                if ( $discard[1][0] > 1 ) {
1239
1240                    $this->tableCalls[] = $discard;
1241                    if ( strstr($discard[0],'cell') ) {
1242                        $name = 'tablecell';
1243                    } else {
1244                        $name = 'tableheader';
1245                    }
1246                    $this->tableCalls[] = array($name.'_close',array(),$call[2]);
1247                }
1248
1249                break;
1250            }
1251        }
1252        $this->tableCalls[] = $call;
1253
1254        if ( $this->currentCols > $this->maxCols ) {
1255            $this->maxCols = $this->currentCols;
1256        }
1257    }
1258
1259    function tableCell($call) {
1260        if ( !$this->firstCell ) {
1261
1262            // Increase the span
1263            $lastCall = end($this->tableCalls);
1264
1265            // A cell call which follows an open cell means an empty cell so span
1266            if ( $lastCall[0] == 'tablecell_open' || $lastCall[0] == 'tableheader_open' ) {
1267                 $this->tableCalls[] = array('colspan',array(),$call[2]);
1268
1269            }
1270
1271            $this->tableCalls[] = array($this->lastCellType.'_close',array(),$call[2]);
1272            $this->tableCalls[] = array($call[0].'_open',array(1,NULL),$call[2]);
1273            $this->lastCellType = $call[0];
1274
1275        } else {
1276
1277            $this->tableCalls[] = array($call[0].'_open',array(1,NULL),$call[2]);
1278            $this->lastCellType = $call[0];
1279            $this->firstCell = false;
1280
1281        }
1282
1283        $this->currentCols++;
1284    }
1285
1286    function tableDefault($call) {
1287        $this->tableCalls[] = $call;
1288    }
1289
1290    function finalizeTable() {
1291
1292        // Add the max cols and rows to the table opening
1293        if ( $this->tableCalls[0][0] == 'table_open' ) {
1294            // Adjust to num cols not num col delimeters
1295            $this->tableCalls[0][1][] = $this->maxCols - 1;
1296            $this->tableCalls[0][1][] = $this->maxRows;
1297        } else {
1298            trigger_error('First element in table call list is not table_open');
1299        }
1300
1301        $lastRow = 0;
1302        $lastCell = 0;
1303        $toDelete = array();
1304
1305        // Look for the colspan elements and increment the colspan on the
1306        // previous non-empty opening cell. Once done, delete all the cells
1307        // that contain colspans
1308        foreach ( $this->tableCalls as $key => $call ) {
1309
1310            if ( $call[0] == 'tablerow_open' ) {
1311
1312                $lastRow = $key;
1313
1314            } else if ( $call[0] == 'tablecell_open' || $call[0] == 'tableheader_open' ) {
1315
1316                $lastCell = $key;
1317
1318            } else if ( $call[0] == 'table_align' ) {
1319
1320                // If the previous element was a cell open, align right
1321                if ( $this->tableCalls[$key-1][0] == 'tablecell_open' || $this->tableCalls[$key-1][0] == 'tableheader_open' ) {
1322                    $this->tableCalls[$key-1][1][1] = 'right';
1323
1324                // If the next element if the close of an element, align either center or left
1325                } else if ( $this->tableCalls[$key+1][0] == 'tablecell_close' || $this->tableCalls[$key+1][0] == 'tableheader_close' ) {
1326                    if ( $this->tableCalls[$lastCell][1][1] == 'right' ) {
1327                        $this->tableCalls[$lastCell][1][1] = 'center';
1328                    } else {
1329                        $this->tableCalls[$lastCell][1][1] = 'left';
1330                    }
1331
1332                }
1333
1334                // Now convert the whitespace back to cdata
1335                $this->tableCalls[$key][0] = 'cdata';
1336
1337            } else if ( $call[0] == 'colspan' ) {
1338
1339                $this->tableCalls[$key-1][1][0] = false;
1340
1341                for($i = $key-2; $i > $lastRow; $i--) {
1342
1343                    if ( $this->tableCalls[$i][0] == 'tablecell_open' || $this->tableCalls[$i][0] == 'tableheader_open' ) {
1344
1345                        if ( false !== $this->tableCalls[$i][1][0] ) {
1346                            $this->tableCalls[$i][1][0]++;
1347                            break;
1348                        }
1349
1350
1351                    }
1352                }
1353
1354                $toDelete[] = $key-1;
1355                $toDelete[] = $key;
1356                $toDelete[] = $key+1;
1357            }
1358        }
1359
1360
1361        // condense cdata
1362        $cnt = count($this->tableCalls);
1363        for( $key = 0; $key < $cnt; $key++){
1364            if($this->tableCalls[$key][0] == 'cdata'){
1365                $ckey = $key;
1366                $key++;
1367                while($this->tableCalls[$key][0] == 'cdata'){
1368                    $this->tableCalls[$ckey][1][0] .= $this->tableCalls[$key][1][0];
1369                    $toDelete[] = $key;
1370                    $key++;
1371                }
1372                continue;
1373            }
1374        }
1375
1376        foreach ( $toDelete as $delete ) {
1377            unset($this->tableCalls[$delete]);
1378        }
1379        $this->tableCalls = array_values($this->tableCalls);
1380    }
1381}
1382
1383//------------------------------------------------------------------------
1384class Doku_Handler_Section {
1385
1386    function process($calls) {
1387
1388        $sectionCalls = array();
1389        $inSection = false;
1390
1391        foreach ( $calls as $call ) {
1392
1393            if ( $call[0] == 'header' ) {
1394
1395                if ( $inSection ) {
1396                    $sectionCalls[] = array('section_close',array(), $call[2]);
1397                }
1398
1399                $sectionCalls[] = $call;
1400                $sectionCalls[] = array('section_open',array($call[1][1]), $call[2]);
1401                $inSection = true;
1402
1403            } else {
1404
1405                if ($call[0] == 'section_open' )  {
1406                    $inSection = true;
1407                } else if ($call[0] == 'section_open' ) {
1408                    $inSection = false;
1409                }
1410                $sectionCalls[] = $call;
1411            }
1412        }
1413
1414        if ( $inSection ) {
1415            $sectionCalls[] = array('section_close',array(), $call[2]);
1416        }
1417
1418        return $sectionCalls;
1419    }
1420
1421}
1422
1423/**
1424 * Handler for paragraphs
1425 *
1426 * @author Harry Fuecks <hfuecks@gmail.com>
1427 */
1428class Doku_Handler_Block {
1429
1430    var $calls = array();
1431
1432    var $blockStack = array();
1433
1434    var $inParagraph = false;
1435    var $atStart = true;
1436    var $skipEolKey = -1;
1437
1438    // Blocks these should not be inside paragraphs
1439    var $blockOpen = array(
1440            'header',
1441            'listu_open','listo_open','listitem_open','listcontent_open',
1442            'table_open','tablerow_open','tablecell_open','tableheader_open',
1443            'quote_open',
1444            'section_open', // Needed to prevent p_open between header and section_open
1445            'code','file','hr','preformatted','rss',
1446            'htmlblock','phpblock',
1447        );
1448
1449    var $blockClose = array(
1450            'header',
1451            'listu_close','listo_close','listitem_close','listcontent_close',
1452            'table_close','tablerow_close','tablecell_close','tableheader_close',
1453            'quote_close',
1454            'section_close', // Needed to prevent p_close after section_close
1455            'code','file','hr','preformatted','rss',
1456            'htmlblock','phpblock',
1457        );
1458
1459    // Stacks can contain paragraphs
1460    var $stackOpen = array(
1461        'footnote_open','section_open',
1462        );
1463
1464    var $stackClose = array(
1465        'footnote_close','section_close',
1466        );
1467
1468
1469    /**
1470     * Constructor. Adds loaded syntax plugins to the block and stack
1471     * arrays
1472     *
1473     * @author Andreas Gohr <andi@splitbrain.org>
1474     */
1475    function Doku_Handler_Block(){
1476        global $DOKU_PLUGINS;
1477        //check if syntax plugins were loaded
1478        if(empty($DOKU_PLUGINS['syntax'])) return;
1479        foreach($DOKU_PLUGINS['syntax'] as $n => $p){
1480            $ptype = $p->getPType();
1481            if($ptype == 'block'){
1482                $this->blockOpen[]  = 'plugin_'.$n;
1483                $this->blockClose[] = 'plugin_'.$n;
1484            }elseif($ptype == 'stack'){
1485                $this->stackOpen[]  = 'plugin_'.$n;
1486                $this->stackClose[] = 'plugin_'.$n;
1487            }
1488        }
1489    }
1490
1491    /**
1492     * Close a paragraph if needed
1493     *
1494     * This function makes sure there are no empty paragraphs on the stack
1495     *
1496     * @author Andreas Gohr <andi@splitbrain.org>
1497     */
1498    function closeParagraph($pos){
1499        // look back if there was any content - we don't want empty paragraphs
1500        $content = '';
1501        for($i=count($this->calls)-1; $i>=0; $i--){
1502            if($this->calls[$i][0] == 'p_open'){
1503                break;
1504            }elseif($this->calls[$i][0] == 'cdata'){
1505                $content .= $this->calls[$i][1][0];
1506            }else{
1507                $content = 'found markup';
1508                break;
1509            }
1510        }
1511
1512        if(trim($content)==''){
1513            //remove the whole paragraph
1514            array_splice($this->calls,$i);
1515        }else{
1516            if ($this->calls[count($this->calls)-1][0] == 'section_edit') {
1517                $tmp = array_pop($this->calls);
1518                $this->calls[] = array('p_close',array(), $pos);
1519                $this->calls[] = $tmp;
1520            } else {
1521                $this->calls[] = array('p_close',array(), $pos);
1522            }
1523        }
1524
1525        $this->inParagraph = false;
1526    }
1527
1528    /**
1529     * Processes the whole instruction stack to open and close paragraphs
1530     *
1531     * @author Harry Fuecks <hfuecks@gmail.com>
1532     * @author Andreas Gohr <andi@splitbrain.org>
1533     * @todo   This thing is really messy and should be rewritten
1534     */
1535    function process($calls) {
1536        foreach ( $calls as $key => $call ) {
1537            $cname = $call[0];
1538            if($cname == 'plugin') {
1539                $cname='plugin_'.$call[1][0];
1540
1541                $plugin = true;
1542                $plugin_open = (($call[1][2] == DOKU_LEXER_ENTER) || ($call[1][2] == DOKU_LEXER_SPECIAL));
1543                $plugin_close = (($call[1][2] == DOKU_LEXER_EXIT) || ($call[1][2] == DOKU_LEXER_SPECIAL));
1544            } else {
1545                $plugin = false;
1546            }
1547
1548            // Process blocks which are stack like... (contain linefeeds)
1549            if ( in_array($cname,$this->stackOpen ) && (!$plugin || $plugin_open) ) {
1550
1551                $this->calls[] = $call;
1552
1553                // Hack - footnotes shouldn't immediately contain a p_open
1554                if ( $cname != 'footnote_open' ) {
1555                    $this->addToStack();
1556                } else {
1557                    $this->addToStack(false);
1558                }
1559                continue;
1560            }
1561
1562            if ( in_array($cname,$this->stackClose ) && (!$plugin || $plugin_close)) {
1563
1564                if ( $this->inParagraph ) {
1565                    $this->closeParagraph($call[2]);
1566                }
1567                $this->calls[] = $call;
1568                $this->removeFromStack();
1569                continue;
1570            }
1571
1572            if ( !$this->atStart ) {
1573
1574                if ( $cname == 'eol' ) {
1575
1576                    // Check this isn't an eol instruction to skip...
1577                    if ( $this->skipEolKey != $key ) {
1578                        // Look to see if the next instruction is an EOL
1579                        if ( isset($calls[$key+1]) && $calls[$key+1][0] == 'eol' ) {
1580
1581                            if ( $this->inParagraph ) {
1582                                //$this->calls[] = array('p_close',array(), $call[2]);
1583                                $this->closeParagraph($call[2]);
1584                            }
1585
1586                            $this->calls[] = array('p_open',array(), $call[2]);
1587                            $this->inParagraph = true;
1588
1589
1590                            // Mark the next instruction for skipping
1591                            $this->skipEolKey = $key+1;
1592
1593                        }else{
1594                            //if this is just a single eol make a space from it
1595                            $this->addCall(array('cdata',array(DOKU_PARSER_EOL), $call[2]));
1596                        }
1597                    }
1598
1599
1600                } else {
1601
1602                    $storeCall = true;
1603                    if ( $this->inParagraph && (in_array($cname, $this->blockOpen) && (!$plugin || $plugin_open))) {
1604                        $this->closeParagraph($call[2]);
1605                        $this->calls[] = $call;
1606                        $storeCall = false;
1607                    }
1608
1609                    if ( in_array($cname, $this->blockClose) && (!$plugin || $plugin_close)) {
1610                        if ( $this->inParagraph ) {
1611                            $this->closeParagraph($call[2]);
1612                        }
1613                        if ( $storeCall ) {
1614                            $this->calls[] = $call;
1615                            $storeCall = false;
1616                        }
1617
1618                        // This really sucks and suggests this whole class sucks but...
1619                        if ( isset($calls[$key+1])) {
1620                            $cname_plusone = $calls[$key+1][0];
1621                            if ($cname_plusone == 'plugin') {
1622                                $cname_plusone = 'plugin'.$calls[$key+1][1][0];
1623
1624                                // plugin test, true if plugin has a state which precludes it requiring blockOpen or blockClose
1625                                $plugin_plusone = true;
1626                                $plugin_test = ($call[$key+1][1][2] == DOKU_LEXER_MATCHED) || ($call[$key+1][1][2] == DOKU_LEXER_MATCHED);
1627                            } else {
1628                                $plugin_plusone = false;
1629                            }
1630                            if ((!in_array($cname_plusone, $this->blockOpen) && !in_array($cname_plusone, $this->blockClose)) ||
1631                                ($plugin_plusone && $plugin_test)
1632                                ) {
1633
1634                                $this->calls[] = array('p_open',array(), $call[2]);
1635                                $this->inParagraph = true;
1636                            }
1637                        }
1638                    }
1639
1640                    if ( $storeCall ) {
1641                        $this->addCall($call);
1642                    }
1643
1644                }
1645
1646
1647            } else {
1648
1649                // Unless there's already a block at the start, start a paragraph
1650                if ( !in_array($cname,$this->blockOpen) ) {
1651                    $this->calls[] = array('p_open',array(), $call[2]);
1652                    if ( $call[0] != 'eol' ) {
1653                        $this->calls[] = $call;
1654                    }
1655                    $this->atStart = false;
1656                    $this->inParagraph = true;
1657                } else {
1658                    $this->addCall($call);
1659                    $this->atStart = false;
1660                }
1661
1662            }
1663
1664        }
1665
1666        if ( $this->inParagraph ) {
1667            if ( $cname == 'p_open' ) {
1668                // Ditch the last call
1669                array_pop($this->calls);
1670            } else if ( !in_array($cname, $this->blockClose) ) {
1671                //$this->calls[] = array('p_close',array(), $call[2]);
1672                $this->closeParagraph($call[2]);
1673            } else {
1674                $last_call = array_pop($this->calls);
1675                //$this->calls[] = array('p_close',array(), $call[2]);
1676                $this->closeParagraph($call[2]);
1677                $this->calls[] = $last_call;
1678            }
1679        }
1680
1681        return $this->calls;
1682    }
1683
1684    function addToStack($newStart = true) {
1685        $this->blockStack[] = array($this->atStart, $this->inParagraph);
1686        $this->atStart = $newStart;
1687        $this->inParagraph = false;
1688    }
1689
1690    function removeFromStack() {
1691        $state = array_pop($this->blockStack);
1692        $this->atStart = $state[0];
1693        $this->inParagraph = $state[1];
1694    }
1695
1696    function addCall($call) {
1697        $key = count($this->calls);
1698        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
1699            $this->calls[$key-1][1][0] .= $call[1][0];
1700        } else {
1701            $this->calls[] = $call;
1702        }
1703    }
1704}
1705
1706//Setup VIM: ex: et ts=4 enc=utf-8 :
1707