xref: /dokuwiki/inc/parser/handler.php (revision 469ad2731e70126fa961494dc9c8d558c77326fb)
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                //$this->_addCall('table_row', array(), $pos);
588                if ( trim($match) == '^' ) {
589                    $this->_addCall('tableheader', array(), $pos);
590                } else {
591                    $this->_addCall('tablecell', array(), $pos);
592                }
593            break;
594
595            case DOKU_LEXER_EXIT:
596                $this->_addCall('table_end', array(), $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('/\t+/',$match) ) {
612                    $this->_addCall('table_align', array($match), $pos);
613                } else if ( preg_match('/ {2,}/',$match) ) {
614                    $this->_addCall('table_align', array($match), $pos);
615                } else if ( $match == "\n|" ) {
616                    $this->_addCall('table_row', array(), $pos);
617                    $this->_addCall('tablecell', array(), $pos);
618                } else if ( $match == "\n^" ) {
619                    $this->_addCall('table_row', array(), $pos);
620                    $this->_addCall('tableheader', array(), $pos);
621                } else if ( $match == '|' ) {
622                    $this->_addCall('tablecell', array(), $pos);
623                } else if ( $match == '^' ) {
624                    $this->_addCall('tableheader', array(), $pos);
625                }
626            break;
627        }
628        return true;
629    }
630}
631
632//------------------------------------------------------------------------
633function Doku_Handler_Parse_Media($match) {
634
635    // Strip the opening and closing markup
636    $link = preg_replace(array('/^\{\{/','/\}\}$/u'),'',$match);
637
638    // Split title from URL
639    $link = explode('|',$link,2);
640
641
642    // Check alignment
643    $ralign = (bool)preg_match('/^ /',$link[0]);
644    $lalign = (bool)preg_match('/ $/',$link[0]);
645
646    // Logic = what's that ;)...
647    if ( $lalign & $ralign ) {
648        $align = 'center';
649    } else if ( $ralign ) {
650        $align = 'right';
651    } else if ( $lalign ) {
652        $align = 'left';
653    } else {
654        $align = NULL;
655    }
656
657    // The title...
658    if ( !isset($link[1]) ) {
659        $link[1] = NULL;
660    }
661
662    //remove aligning spaces
663    $link[0] = trim($link[0]);
664
665    //split into src and parameters (using the very last questionmark)
666    $pos = strrpos($link[0], '?');
667    if($pos !== false){
668        $src   = substr($link[0],0,$pos);
669        $param = substr($link[0],$pos+1);
670    }else{
671        $src   = $link[0];
672        $param = '';
673    }
674
675    //parse width and height
676    if(preg_match('#(\d+)(x(\d+))?#i',$param,$size)){
677        ($size[1]) ? $w = $size[1] : $w = NULL;
678        ($size[3]) ? $h = $size[3] : $h = NULL;
679    } else {
680        $w = NULL;
681        $h = NULL;
682    }
683
684    //get linking command
685    if(preg_match('/nolink/i',$param)){
686        $linking = 'nolink';
687    }else if(preg_match('/direct/i',$param)){
688        $linking = 'direct';
689    }else if(preg_match('/linkonly/i',$param)){
690        $linking = 'linkonly';
691    }else{
692        $linking = 'details';
693    }
694
695    //get caching command
696    if (preg_match('/(nocache|recache)/i',$param,$cachemode)){
697        $cache = $cachemode[1];
698    }else{
699        $cache = 'cache';
700    }
701
702    // Check whether this is a local or remote image
703    if ( preg_match('#^(https?|ftp)#i',$src) ) {
704        $call = 'externalmedia';
705    } else {
706        $call = 'internalmedia';
707    }
708
709    $params = array(
710        'type'=>$call,
711        'src'=>$src,
712        'title'=>$link[1],
713        'align'=>$align,
714        'width'=>$w,
715        'height'=>$h,
716        'cache'=>$cache,
717        'linking'=>$linking,
718    );
719
720    return $params;
721}
722
723//------------------------------------------------------------------------
724class Doku_Handler_CallWriter {
725
726    var $Handler;
727
728    function Doku_Handler_CallWriter(& $Handler) {
729        $this->Handler = & $Handler;
730    }
731
732    function writeCall($call) {
733        $this->Handler->calls[] = $call;
734    }
735
736    function writeCalls($calls) {
737        $this->Handler->calls = array_merge($this->Handler->calls, $calls);
738    }
739
740    // function is required, but since this call writer is first/highest in
741    // the chain it is not required to do anything
742    function finalise() {
743    }
744}
745
746//------------------------------------------------------------------------
747/**
748 * Generic call writer class to handle nesting of rendering instructions
749 * within a render instruction. Also see nest() method of renderer base class
750 *
751 * @author    Chris Smith <chris@jalakai.co.uk>
752 */
753class Doku_Handler_Nest {
754
755    var $CallWriter;
756    var $calls = array();
757
758    var $closingInstruction;
759
760    /**
761     * constructor
762     *
763     * @param  object     $CallWriter     the renderers current call writer
764     * @param  string     $close          closing instruction name, this is required to properly terminate the
765     *                                    syntax mode if the document ends without a closing pattern
766     */
767    function Doku_Handler_Nest(& $CallWriter, $close="nest_close") {
768        $this->CallWriter = & $CallWriter;
769
770        $this->closingInstruction = $close;
771    }
772
773    function writeCall($call) {
774        $this->calls[] = $call;
775    }
776
777    function writeCalls($calls) {
778        $this->calls = array_merge($this->calls, $calls);
779    }
780
781    function finalise() {
782        $last_call = end($this->calls);
783        $this->writeCall(array($this->closingInstruction,array(), $last_call[2]));
784
785        $this->process();
786        $this->CallWriter->finalise();
787    }
788
789    function process() {
790        // merge consecutive cdata
791        $unmerged_calls = $this->calls;
792        $this->calls = array();
793
794        foreach ($unmerged_calls as $call) $this->addCall($call);
795
796        $first_call = reset($this->calls);
797        $this->CallWriter->writeCall(array("nest", array($this->calls), $first_call[2]));
798    }
799
800    function addCall($call) {
801        $key = count($this->calls);
802        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
803            $this->calls[$key-1][1][0] .= $call[1][0];
804        } else {
805            $this->calls[] = $call;
806        }
807    }
808}
809
810class Doku_Handler_List {
811
812    var $CallWriter;
813
814    var $calls = array();
815    var $listCalls = array();
816    var $listStack = array();
817
818    function Doku_Handler_List(& $CallWriter) {
819        $this->CallWriter = & $CallWriter;
820    }
821
822    function writeCall($call) {
823        $this->calls[] = $call;
824    }
825
826    // Probably not needed but just in case...
827    function writeCalls($calls) {
828        $this->calls = array_merge($this->calls, $calls);
829#        $this->CallWriter->writeCalls($this->calls);
830    }
831
832    function finalise() {
833        $last_call = end($this->calls);
834        $this->writeCall(array('list_close',array(), $last_call[2]));
835
836        $this->process();
837        $this->CallWriter->finalise();
838    }
839
840    //------------------------------------------------------------------------
841    function process() {
842
843        foreach ( $this->calls as $call ) {
844            switch ($call[0]) {
845                case 'list_item':
846                    $this->listOpen($call);
847                break;
848                case 'list_open':
849                    $this->listStart($call);
850                break;
851                case 'list_close':
852                    $this->listEnd($call);
853                break;
854                default:
855                    $this->listContent($call);
856                break;
857            }
858        }
859
860        $this->CallWriter->writeCalls($this->listCalls);
861    }
862
863    //------------------------------------------------------------------------
864    function listStart($call) {
865        $depth = $this->interpretSyntax($call[1][0], $listType);
866
867        $this->initialDepth = $depth;
868        $this->listStack[] = array($listType, $depth);
869
870        $this->listCalls[] = array('list'.$listType.'_open',array(),$call[2]);
871        $this->listCalls[] = array('listitem_open',array(1),$call[2]);
872        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
873    }
874
875    //------------------------------------------------------------------------
876    function listEnd($call) {
877        $closeContent = true;
878
879        while ( $list = array_pop($this->listStack) ) {
880            if ( $closeContent ) {
881                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
882                $closeContent = false;
883            }
884            $this->listCalls[] = array('listitem_close',array(),$call[2]);
885            $this->listCalls[] = array('list'.$list[0].'_close', array(), $call[2]);
886        }
887    }
888
889    //------------------------------------------------------------------------
890    function listOpen($call) {
891        $depth = $this->interpretSyntax($call[1][0], $listType);
892        $end = end($this->listStack);
893
894        // Not allowed to be shallower than initialDepth
895        if ( $depth < $this->initialDepth ) {
896            $depth = $this->initialDepth;
897        }
898
899        //------------------------------------------------------------------------
900        if ( $depth == $end[1] ) {
901
902            // Just another item in the list...
903            if ( $listType == $end[0] ) {
904                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
905                $this->listCalls[] = array('listitem_close',array(),$call[2]);
906                $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
907                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
908
909            // Switched list type...
910            } else {
911
912                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
913                $this->listCalls[] = array('listitem_close',array(),$call[2]);
914                $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
915                $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
916                $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
917                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
918
919                array_pop($this->listStack);
920                $this->listStack[] = array($listType, $depth);
921            }
922
923        //------------------------------------------------------------------------
924        // Getting deeper...
925        } else if ( $depth > $end[1] ) {
926
927            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
928            $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
929            $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
930            $this->listCalls[] = array('listcontent_open',array(),$call[2]);
931
932            $this->listStack[] = array($listType, $depth);
933
934        //------------------------------------------------------------------------
935        // Getting shallower ( $depth < $end[1] )
936        } else {
937            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
938            $this->listCalls[] = array('listitem_close',array(),$call[2]);
939            $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
940
941            // Throw away the end - done
942            array_pop($this->listStack);
943
944            while (1) {
945                $end = end($this->listStack);
946
947                if ( $end[1] <= $depth ) {
948
949                    // Normalize depths
950                    $depth = $end[1];
951
952                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
953
954                    if ( $end[0] == $listType ) {
955                        $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
956                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
957
958                    } else {
959                        // Switching list type...
960                        $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
961                        $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
962                        $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
963                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
964
965                        array_pop($this->listStack);
966                        $this->listStack[] = array($listType, $depth);
967                    }
968
969                    break;
970
971                // Haven't dropped down far enough yet.... ( $end[1] > $depth )
972                } else {
973
974                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
975                    $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
976
977                    array_pop($this->listStack);
978
979                }
980
981            }
982
983        }
984    }
985
986    //------------------------------------------------------------------------
987    function listContent($call) {
988        $this->listCalls[] = $call;
989    }
990
991    //------------------------------------------------------------------------
992    function interpretSyntax($match, & $type) {
993        if ( substr($match,-1) == '*' ) {
994            $type = 'u';
995        } else {
996            $type = 'o';
997        }
998        // Is the +1 needed? It used to be count(explode(...))
999        // but I don't think the number is seen outside this handler
1000        return substr_count(str_replace("\t",'  ',$match), '  ') + 1;
1001    }
1002}
1003
1004//------------------------------------------------------------------------
1005class Doku_Handler_Preformatted {
1006
1007    var $CallWriter;
1008
1009    var $calls = array();
1010    var $pos;
1011    var $text ='';
1012
1013
1014
1015    function Doku_Handler_Preformatted(& $CallWriter) {
1016        $this->CallWriter = & $CallWriter;
1017    }
1018
1019    function writeCall($call) {
1020        $this->calls[] = $call;
1021    }
1022
1023    // Probably not needed but just in case...
1024    function writeCalls($calls) {
1025        $this->calls = array_merge($this->calls, $calls);
1026#        $this->CallWriter->writeCalls($this->calls);
1027    }
1028
1029    function finalise() {
1030        $last_call = end($this->calls);
1031        $this->writeCall(array('preformatted_end',array(), $last_call[2]));
1032
1033        $this->process();
1034        $this->CallWriter->finalise();
1035    }
1036
1037    function process() {
1038        foreach ( $this->calls as $call ) {
1039            switch ($call[0]) {
1040                case 'preformatted_start':
1041                    $this->pos = $call[2];
1042                break;
1043                case 'preformatted_newline':
1044                    $this->text .= "\n";
1045                break;
1046                case 'preformatted_content':
1047                    $this->text .= $call[1][0];
1048                break;
1049                case 'preformatted_end':
1050                    if (trim($this->text)) {
1051                      $this->CallWriter->writeCall(array('preformatted',array($this->text),$this->pos));
1052                    }
1053                break;
1054            }
1055        }
1056    }
1057
1058}
1059
1060//------------------------------------------------------------------------
1061class Doku_Handler_Quote {
1062
1063    var $CallWriter;
1064
1065    var $calls = array();
1066
1067    var $quoteCalls = array();
1068
1069    function Doku_Handler_Quote(& $CallWriter) {
1070        $this->CallWriter = & $CallWriter;
1071    }
1072
1073    function writeCall($call) {
1074        $this->calls[] = $call;
1075    }
1076
1077    // Probably not needed but just in case...
1078    function writeCalls($calls) {
1079        $this->calls = array_merge($this->calls, $calls);
1080    }
1081
1082    function finalise() {
1083        $last_call = end($this->calls);
1084        $this->writeCall(array('quote_end',array(), $last_call[2]));
1085
1086        $this->process();
1087        $this->CallWriter->finalise();
1088    }
1089
1090    function process() {
1091
1092        $quoteDepth = 1;
1093
1094        foreach ( $this->calls as $call ) {
1095            switch ($call[0]) {
1096
1097                case 'quote_start':
1098
1099                    $this->quoteCalls[] = array('quote_open',array(),$call[2]);
1100
1101                case 'quote_newline':
1102
1103                    $quoteLength = $this->getDepth($call[1][0]);
1104
1105                    if ( $quoteLength > $quoteDepth ) {
1106                        $quoteDiff = $quoteLength - $quoteDepth;
1107                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
1108                            $this->quoteCalls[] = array('quote_open',array(),$call[2]);
1109                        }
1110                    } else if ( $quoteLength < $quoteDepth ) {
1111                        $quoteDiff = $quoteDepth - $quoteLength;
1112                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
1113                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
1114                        }
1115                    } else {
1116                        if ($call[0] != 'quote_start') $this->quoteCalls[] = array('linebreak',array(),$call[2]);
1117                    }
1118
1119                    $quoteDepth = $quoteLength;
1120
1121                break;
1122
1123                case 'quote_end':
1124
1125                    if ( $quoteDepth > 1 ) {
1126                        $quoteDiff = $quoteDepth - 1;
1127                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
1128                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
1129                        }
1130                    }
1131
1132                    $this->quoteCalls[] = array('quote_close',array(),$call[2]);
1133
1134                    $this->CallWriter->writeCalls($this->quoteCalls);
1135                break;
1136
1137                default:
1138                    $this->quoteCalls[] = $call;
1139                break;
1140            }
1141        }
1142    }
1143
1144    function getDepth($marker) {
1145        preg_match('/>{1,}/', $marker, $matches);
1146        $quoteLength = strlen($matches[0]);
1147        return $quoteLength;
1148    }
1149}
1150
1151//------------------------------------------------------------------------
1152class Doku_Handler_Table {
1153
1154    var $CallWriter;
1155
1156    var $calls = array();
1157    var $tableCalls = array();
1158    var $maxCols = 0;
1159    var $maxRows = 1;
1160    var $currentCols = 0;
1161    var $firstCell = false;
1162    var $lastCellType = 'tablecell';
1163
1164    function Doku_Handler_Table(& $CallWriter) {
1165        $this->CallWriter = & $CallWriter;
1166    }
1167
1168    function writeCall($call) {
1169        $this->calls[] = $call;
1170    }
1171
1172    // Probably not needed but just in case...
1173    function writeCalls($calls) {
1174        $this->calls = array_merge($this->calls, $calls);
1175    }
1176
1177    function finalise() {
1178        $last_call = end($this->calls);
1179        $this->writeCall(array('table_end',array(), $last_call[2]));
1180
1181        $this->process();
1182        $this->CallWriter->finalise();
1183    }
1184
1185    //------------------------------------------------------------------------
1186    function process() {
1187        foreach ( $this->calls as $call ) {
1188            switch ( $call[0] ) {
1189                case 'table_start':
1190                    $this->tableStart($call);
1191                break;
1192                case 'table_row':
1193                    $this->tableRowClose(array('tablerow_close',$call[1],$call[2]));
1194                    $this->tableRowOpen(array('tablerow_open',$call[1],$call[2]));
1195                break;
1196                case 'tableheader':
1197                case 'tablecell':
1198                    $this->tableCell($call);
1199                break;
1200                case 'table_end':
1201                    $this->tableRowClose(array('tablerow_close',$call[1],$call[2]));
1202                    $this->tableEnd($call);
1203                break;
1204                default:
1205                    $this->tableDefault($call);
1206                break;
1207            }
1208        }
1209        $this->CallWriter->writeCalls($this->tableCalls);
1210    }
1211
1212    function tableStart($call) {
1213        $this->tableCalls[] = array('table_open',array(),$call[2]);
1214        $this->tableCalls[] = array('tablerow_open',array(),$call[2]);
1215        $this->firstCell = true;
1216    }
1217
1218    function tableEnd($call) {
1219        $this->tableCalls[] = array('table_close',array(),$call[2]);
1220        $this->finalizeTable();
1221    }
1222
1223    function tableRowOpen($call) {
1224        $this->tableCalls[] = $call;
1225        $this->currentCols = 0;
1226        $this->firstCell = true;
1227        $this->lastCellType = 'tablecell';
1228        $this->maxRows++;
1229    }
1230
1231    function tableRowClose($call) {
1232        // Strip off final cell opening and anything after it
1233        while ( $discard = array_pop($this->tableCalls ) ) {
1234
1235            if ( $discard[0] == 'tablecell_open' || $discard[0] == 'tableheader_open') {
1236
1237                // Its a spanning element - put it back and close it
1238                if ( $discard[1][0] > 1 ) {
1239
1240                    $this->tableCalls[] = $discard;
1241                    if ( strstr($discard[0],'cell') ) {
1242                        $name = 'tablecell';
1243                    } else {
1244                        $name = 'tableheader';
1245                    }
1246                    $this->tableCalls[] = array($name.'_close',array(),$call[2]);
1247                }
1248
1249                break;
1250            }
1251        }
1252        $this->tableCalls[] = $call;
1253
1254        if ( $this->currentCols > $this->maxCols ) {
1255            $this->maxCols = $this->currentCols;
1256        }
1257    }
1258
1259    function tableCell($call) {
1260        if ( !$this->firstCell ) {
1261
1262            // Increase the span
1263            $lastCall = end($this->tableCalls);
1264
1265            // A cell call which follows an open cell means an empty cell so span
1266            if ( $lastCall[0] == 'tablecell_open' || $lastCall[0] == 'tableheader_open' ) {
1267                 $this->tableCalls[] = array('colspan',array(),$call[2]);
1268
1269            }
1270
1271            $this->tableCalls[] = array($this->lastCellType.'_close',array(),$call[2]);
1272            $this->tableCalls[] = array($call[0].'_open',array(1,NULL),$call[2]);
1273            $this->lastCellType = $call[0];
1274
1275        } else {
1276
1277            $this->tableCalls[] = array($call[0].'_open',array(1,NULL),$call[2]);
1278            $this->lastCellType = $call[0];
1279            $this->firstCell = false;
1280
1281        }
1282
1283        $this->currentCols++;
1284    }
1285
1286    function tableDefault($call) {
1287        $this->tableCalls[] = $call;
1288    }
1289
1290    function finalizeTable() {
1291
1292        // Add the max cols and rows to the table opening
1293        if ( $this->tableCalls[0][0] == 'table_open' ) {
1294            // Adjust to num cols not num col delimeters
1295            $this->tableCalls[0][1][] = $this->maxCols - 1;
1296            $this->tableCalls[0][1][] = $this->maxRows;
1297        } else {
1298            trigger_error('First element in table call list is not table_open');
1299        }
1300
1301        $lastRow = 0;
1302        $lastCell = 0;
1303        $toDelete = array();
1304
1305        // Look for the colspan elements and increment the colspan on the
1306        // previous non-empty opening cell. Once done, delete all the cells
1307        // that contain colspans
1308        foreach ( $this->tableCalls as $key => $call ) {
1309
1310            if ( $call[0] == 'tablerow_open' ) {
1311
1312                $lastRow = $key;
1313
1314            } else if ( $call[0] == 'tablecell_open' || $call[0] == 'tableheader_open' ) {
1315
1316                $lastCell = $key;
1317
1318            } else if ( $call[0] == 'table_align' ) {
1319
1320                // If the previous element was a cell open, align right
1321                if ( $this->tableCalls[$key-1][0] == 'tablecell_open' || $this->tableCalls[$key-1][0] == 'tableheader_open' ) {
1322                    $this->tableCalls[$key-1][1][1] = 'right';
1323
1324                // If the next element if the close of an element, align either center or left
1325                } else if ( $this->tableCalls[$key+1][0] == 'tablecell_close' || $this->tableCalls[$key+1][0] == 'tableheader_close' ) {
1326                    if ( $this->tableCalls[$lastCell][1][1] == 'right' ) {
1327                        $this->tableCalls[$lastCell][1][1] = 'center';
1328                    } else {
1329                        $this->tableCalls[$lastCell][1][1] = 'left';
1330                    }
1331
1332                }
1333
1334                // Now convert the whitespace back to cdata
1335                $this->tableCalls[$key][0] = 'cdata';
1336
1337            } else if ( $call[0] == 'colspan' ) {
1338
1339                $this->tableCalls[$key-1][1][0] = false;
1340
1341                for($i = $key-2; $i > $lastRow; $i--) {
1342
1343                    if ( $this->tableCalls[$i][0] == 'tablecell_open' || $this->tableCalls[$i][0] == 'tableheader_open' ) {
1344
1345                        if ( false !== $this->tableCalls[$i][1][0] ) {
1346                            $this->tableCalls[$i][1][0]++;
1347                            break;
1348                        }
1349
1350
1351                    }
1352                }
1353
1354                $toDelete[] = $key-1;
1355                $toDelete[] = $key;
1356                $toDelete[] = $key+1;
1357            }
1358        }
1359
1360
1361        // condense cdata
1362        $cnt = count($this->tableCalls);
1363        for( $key = 0; $key < $cnt; $key++){
1364            if($this->tableCalls[$key][0] == 'cdata'){
1365                $ckey = $key;
1366                $key++;
1367                while($this->tableCalls[$key][0] == 'cdata'){
1368                    $this->tableCalls[$ckey][1][0] .= $this->tableCalls[$key][1][0];
1369                    $toDelete[] = $key;
1370                    $key++;
1371                }
1372                continue;
1373            }
1374        }
1375
1376        foreach ( $toDelete as $delete ) {
1377            unset($this->tableCalls[$delete]);
1378        }
1379        $this->tableCalls = array_values($this->tableCalls);
1380    }
1381}
1382
1383//------------------------------------------------------------------------
1384class Doku_Handler_Section {
1385
1386    function process($calls) {
1387
1388        $sectionCalls = array();
1389        $inSection = false;
1390
1391        foreach ( $calls as $call ) {
1392
1393            if ( $call[0] == 'header' ) {
1394
1395                if ( $inSection ) {
1396                    $sectionCalls[] = array('section_close',array(), $call[2]);
1397                }
1398
1399                $sectionCalls[] = $call;
1400                $sectionCalls[] = array('section_open',array($call[1][1]), $call[2]);
1401                $inSection = true;
1402
1403            } else {
1404
1405                if ($call[0] == 'section_open' )  {
1406                    $inSection = true;
1407                } else if ($call[0] == 'section_open' ) {
1408                    $inSection = false;
1409                }
1410                $sectionCalls[] = $call;
1411            }
1412        }
1413
1414        if ( $inSection ) {
1415            $sectionCalls[] = array('section_close',array(), $call[2]);
1416        }
1417
1418        return $sectionCalls;
1419    }
1420
1421}
1422
1423/**
1424 * Handler for paragraphs
1425 *
1426 * @author Harry Fuecks <hfuecks@gmail.com>
1427 */
1428class Doku_Handler_Block {
1429
1430    var $calls = array();
1431
1432    var $blockStack = array();
1433
1434    var $inParagraph = false;
1435    var $atStart = true;
1436    var $skipEolKey = -1;
1437
1438    // Blocks these should not be inside paragraphs
1439    var $blockOpen = array(
1440            'header',
1441            'listu_open','listo_open','listitem_open','listcontent_open',
1442            'table_open','tablerow_open','tablecell_open','tableheader_open',
1443            'quote_open',
1444            'section_open', // Needed to prevent p_open between header and section_open
1445            'code','file','hr','preformatted','rss',
1446            'htmlblock','phpblock',
1447        );
1448
1449    var $blockClose = array(
1450            'header',
1451            'listu_close','listo_close','listitem_close','listcontent_close',
1452            'table_close','tablerow_close','tablecell_close','tableheader_close',
1453            'quote_close',
1454            'section_close', // Needed to prevent p_close after section_close
1455            'code','file','hr','preformatted','rss',
1456            'htmlblock','phpblock',
1457        );
1458
1459    // Stacks can contain paragraphs
1460    var $stackOpen = array(
1461        'footnote_open','section_open',
1462        );
1463
1464    var $stackClose = array(
1465        'footnote_close','section_close',
1466        );
1467
1468
1469    /**
1470     * Constructor. Adds loaded syntax plugins to the block and stack
1471     * arrays
1472     *
1473     * @author Andreas Gohr <andi@splitbrain.org>
1474     */
1475    function Doku_Handler_Block(){
1476        global $DOKU_PLUGINS;
1477        //check if syntax plugins were loaded
1478        if(empty($DOKU_PLUGINS['syntax'])) return;
1479        foreach($DOKU_PLUGINS['syntax'] as $n => $p){
1480            $ptype = $p->getPType();
1481            if($ptype == 'block'){
1482                $this->blockOpen[]  = 'plugin_'.$n;
1483                $this->blockClose[] = 'plugin_'.$n;
1484            }elseif($ptype == 'stack'){
1485                $this->stackOpen[]  = 'plugin_'.$n;
1486                $this->stackClose[] = 'plugin_'.$n;
1487            }
1488        }
1489    }
1490
1491    /**
1492     * Close a paragraph if needed
1493     *
1494     * This function makes sure there are no empty paragraphs on the stack
1495     *
1496     * @author Andreas Gohr <andi@splitbrain.org>
1497     */
1498    function closeParagraph($pos){
1499        // look back if there was any content - we don't want empty paragraphs
1500        $content = '';
1501        for($i=count($this->calls)-1; $i>=0; $i--){
1502            if($this->calls[$i][0] == 'p_open'){
1503                break;
1504            }elseif($this->calls[$i][0] == 'cdata'){
1505                $content .= $this->calls[$i][1][0];
1506            }else{
1507                $content = 'found markup';
1508                break;
1509            }
1510        }
1511
1512        if(trim($content)==''){
1513            //remove the whole paragraph
1514            array_splice($this->calls,$i);
1515        }else{
1516            if ($this->calls[count($this->calls)-1][0] == 'section_edit') {
1517                $tmp = array_pop($this->calls);
1518                $this->calls[] = array('p_close',array(), $pos);
1519                $this->calls[] = $tmp;
1520            } else {
1521                $this->calls[] = array('p_close',array(), $pos);
1522            }
1523        }
1524
1525        $this->inParagraph = false;
1526    }
1527
1528    /**
1529     * Processes the whole instruction stack to open and close paragraphs
1530     *
1531     * @author Harry Fuecks <hfuecks@gmail.com>
1532     * @author Andreas Gohr <andi@splitbrain.org>
1533     * @todo   This thing is really messy and should be rewritten
1534     */
1535    function process($calls) {
1536        foreach ( $calls as $key => $call ) {
1537            $cname = $call[0];
1538            if($cname == 'plugin') {
1539                $cname='plugin_'.$call[1][0];
1540
1541                $plugin = true;
1542                $plugin_open = (($call[1][2] == DOKU_LEXER_ENTER) || ($call[1][2] == DOKU_LEXER_SPECIAL));
1543                $plugin_close = (($call[1][2] == DOKU_LEXER_EXIT) || ($call[1][2] == DOKU_LEXER_SPECIAL));
1544            } else {
1545                $plugin = false;
1546            }
1547
1548            // Process blocks which are stack like... (contain linefeeds)
1549            if ( in_array($cname,$this->stackOpen ) && (!$plugin || $plugin_open) ) {
1550
1551                $this->calls[] = $call;
1552
1553                // Hack - footnotes shouldn't immediately contain a p_open
1554                if ( $cname != 'footnote_open' ) {
1555                    $this->addToStack();
1556                } else {
1557                    $this->addToStack(false);
1558                }
1559                continue;
1560            }
1561
1562            if ( in_array($cname,$this->stackClose ) && (!$plugin || $plugin_close)) {
1563
1564                if ( $this->inParagraph ) {
1565                    $this->closeParagraph($call[2]);
1566                }
1567                $this->calls[] = $call;
1568                $this->removeFromStack();
1569                continue;
1570            }
1571
1572            if ( !$this->atStart ) {
1573
1574                if ( $cname == 'eol' ) {
1575
1576                    // Check this isn't an eol instruction to skip...
1577                    if ( $this->skipEolKey != $key ) {
1578                        // Look to see if the next instruction is an EOL
1579                        if ( isset($calls[$key+1]) && $calls[$key+1][0] == 'eol' ) {
1580
1581                            if ( $this->inParagraph ) {
1582                                //$this->calls[] = array('p_close',array(), $call[2]);
1583                                $this->closeParagraph($call[2]);
1584                            }
1585
1586                            $this->calls[] = array('p_open',array(), $call[2]);
1587                            $this->inParagraph = true;
1588
1589
1590                            // Mark the next instruction for skipping
1591                            $this->skipEolKey = $key+1;
1592
1593                        }else{
1594                            //if this is just a single eol make a space from it
1595                            $this->addCall(array('cdata',array(DOKU_PARSER_EOL), $call[2]));
1596                        }
1597                    }
1598
1599
1600                } else {
1601
1602                    $storeCall = true;
1603                    if ( $this->inParagraph && (in_array($cname, $this->blockOpen) && (!$plugin || $plugin_open))) {
1604                        $this->closeParagraph($call[2]);
1605                        $this->calls[] = $call;
1606                        $storeCall = false;
1607                    }
1608
1609                    if ( in_array($cname, $this->blockClose) && (!$plugin || $plugin_close)) {
1610                        if ( $this->inParagraph ) {
1611                            $this->closeParagraph($call[2]);
1612                        }
1613                        if ( $storeCall ) {
1614                            $this->calls[] = $call;
1615                            $storeCall = false;
1616                        }
1617
1618                        // This really sucks and suggests this whole class sucks but...
1619                        if ( isset($calls[$key+1])) {
1620                            $cname_plusone = $calls[$key+1][0];
1621                            if ($cname_plusone == 'plugin') {
1622                                $cname_plusone = 'plugin'.$calls[$key+1][1][0];
1623
1624                                // plugin test, true if plugin has a state which precludes it requiring blockOpen or blockClose
1625                                $plugin_plusone = true;
1626                                $plugin_test = ($call[$key+1][1][2] == DOKU_LEXER_MATCHED) || ($call[$key+1][1][2] == DOKU_LEXER_MATCHED);
1627                            } else {
1628                                $plugin_plusone = false;
1629                            }
1630                            if ((!in_array($cname_plusone, $this->blockOpen) && !in_array($cname_plusone, $this->blockClose)) ||
1631                                ($plugin_plusone && $plugin_test)
1632                                ) {
1633
1634                                $this->calls[] = array('p_open',array(), $call[2]);
1635                                $this->inParagraph = true;
1636                            }
1637                        }
1638                    }
1639
1640                    if ( $storeCall ) {
1641                        $this->addCall($call);
1642                    }
1643
1644                }
1645
1646
1647            } else {
1648
1649                // Unless there's already a block at the start, start a paragraph
1650                if ( !in_array($cname,$this->blockOpen) ) {
1651                    $this->calls[] = array('p_open',array(), $call[2]);
1652                    if ( $call[0] != 'eol' ) {
1653                        $this->calls[] = $call;
1654                    }
1655                    $this->atStart = false;
1656                    $this->inParagraph = true;
1657                } else {
1658                    $this->addCall($call);
1659                    $this->atStart = false;
1660                }
1661
1662            }
1663
1664        }
1665
1666        if ( $this->inParagraph ) {
1667            if ( $cname == 'p_open' ) {
1668                // Ditch the last call
1669                array_pop($this->calls);
1670            } else if ( !in_array($cname, $this->blockClose) ) {
1671                //$this->calls[] = array('p_close',array(), $call[2]);
1672                $this->closeParagraph($call[2]);
1673            } else {
1674                $last_call = array_pop($this->calls);
1675                //$this->calls[] = array('p_close',array(), $call[2]);
1676                $this->closeParagraph($call[2]);
1677                $this->calls[] = $last_call;
1678            }
1679        }
1680
1681        return $this->calls;
1682    }
1683
1684    function addToStack($newStart = true) {
1685        $this->blockStack[] = array($this->atStart, $this->inParagraph);
1686        $this->atStart = $newStart;
1687        $this->inParagraph = false;
1688    }
1689
1690    function removeFromStack() {
1691        $state = array_pop($this->blockStack);
1692        $this->atStart = $state[0];
1693        $this->inParagraph = $state[1];
1694    }
1695
1696    function addCall($call) {
1697        $key = count($this->calls);
1698        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
1699            $this->calls[$key-1][1][0] .= $call[1][0];
1700        } else {
1701            $this->calls[] = $call;
1702        }
1703    }
1704}
1705
1706//Setup VIM: ex: et ts=4 enc=utf-8 :
1707