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