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