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