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