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