xref: /dokuwiki/inc/parser/handler.php (revision 009768124df70258806ec3120189432d1b2bb912)
10cecf9d5Sandi<?php
2*00976812SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../../').'/');
30cecf9d5Sandi
40cecf9d5Sandiclass Doku_Handler {
50cecf9d5Sandi
60cecf9d5Sandi    var $Renderer = NULL;
70cecf9d5Sandi
80cecf9d5Sandi    var $CallWriter = NULL;
90cecf9d5Sandi
100cecf9d5Sandi    var $calls = array();
110cecf9d5Sandi
12e1c10e4dSchris    var $status = array(
1344881bd0Shenning.noren        'section' => false,
1435dae8b0SBen Coburn        'section_edit_start' => -1,
1535dae8b0SBen Coburn        'section_edit_level' => 1,
1635dae8b0SBen Coburn        'section_edit_title' => ''
170cecf9d5Sandi    );
180cecf9d5Sandi
1944881bd0Shenning.noren    var $rewriteBlocks = true;
20b7c441b9SHarry Fuecks
210cecf9d5Sandi    function Doku_Handler() {
220cecf9d5Sandi        $this->CallWriter = & new Doku_Handler_CallWriter($this);
230cecf9d5Sandi    }
240cecf9d5Sandi
25433bef32Sandi    function _addCall($handler, $args, $pos) {
260cecf9d5Sandi        $call = array($handler,$args, $pos);
270cecf9d5Sandi        $this->CallWriter->writeCall($call);
280cecf9d5Sandi    }
290cecf9d5Sandi
3004ebd214Schris    function addPluginCall($plugin, $args, $state, $pos) {
3104ebd214Schris        $call = array('plugin',array($plugin, $args, $state), $pos);
3204ebd214Schris        $this->CallWriter->writeCall($call);
3304ebd214Schris    }
3404ebd214Schris
35433bef32Sandi    function _finalize(){
36e1c10e4dSchris
37f4f02a0fSchris        $this->CallWriter->finalise();
38f4f02a0fSchris
39e1c10e4dSchris        if ( $this->status['section'] ) {
40e1c10e4dSchris           $last_call = end($this->calls);
41e1c10e4dSchris           array_push($this->calls,array('section_close',array(), $last_call[2]));
42b203781fSBen Coburn           if ($this->status['section_edit_start']>1) {
43b203781fSBen Coburn               // ignore last edit section if there is only one header
44b203781fSBen Coburn               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]));
45b203781fSBen Coburn           }
460cecf9d5Sandi        }
470cecf9d5Sandi
48b7c441b9SHarry Fuecks        if ( $this->rewriteBlocks ) {
490cecf9d5Sandi            $B = & new Doku_Handler_Block();
500cecf9d5Sandi            $this->calls = $B->process($this->calls);
51b7c441b9SHarry Fuecks        }
52e0ad864eSchris
5324bb549bSchris        trigger_event('PARSER_HANDLER_DONE',$this);
540cecf9d5Sandi
550cecf9d5Sandi        array_unshift($this->calls,array('document_start',array(),0));
560cecf9d5Sandi        $last_call = end($this->calls);
570cecf9d5Sandi        array_push($this->calls,array('document_end',array(),$last_call[2]));
580cecf9d5Sandi    }
590cecf9d5Sandi
600cecf9d5Sandi    function fetch() {
610cecf9d5Sandi        $call = each($this->calls);
620cecf9d5Sandi        if ( $call ) {
630cecf9d5Sandi            return $call['value'];
640cecf9d5Sandi        }
6544881bd0Shenning.noren        return false;
660cecf9d5Sandi    }
67ee20e7d1Sandi
68ee20e7d1Sandi
69ee20e7d1Sandi    /**
70ee20e7d1Sandi     * Special plugin handler
71ee20e7d1Sandi     *
72ee20e7d1Sandi     * This handler is called for all modes starting with 'plugin_'.
73ee20e7d1Sandi     * An additional parameter with the plugin name is passed
74ee20e7d1Sandi     *
75ee20e7d1Sandi     * @author Andreas Gohr <andi@splitbrain.org>
76ee20e7d1Sandi     */
77ee20e7d1Sandi    function plugin($match, $state, $pos, $pluginname){
78ee20e7d1Sandi        $data = array($match);
79a46d0d65SAndreas Gohr        $plugin =& plugin_load('syntax',$pluginname);
80a46d0d65SAndreas Gohr        if($plugin != null){
81f02a7d06Schris            $data = $plugin->handle($match, $state, $pos, $this);
82ee20e7d1Sandi        }
8304ebd214Schris        $this->addPluginCall($pluginname,$data,$state,$pos);
8444881bd0Shenning.noren        return true;
85ee20e7d1Sandi    }
860cecf9d5Sandi
870cecf9d5Sandi    function base($match, $state, $pos) {
880cecf9d5Sandi        switch ( $state ) {
890cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
90433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
9144881bd0Shenning.noren                return true;
920cecf9d5Sandi            break;
930cecf9d5Sandi
940cecf9d5Sandi        }
950cecf9d5Sandi    }
960cecf9d5Sandi
970cecf9d5Sandi    function header($match, $state, $pos) {
98b203781fSBen Coburn        global $conf;
99b203781fSBen Coburn
100d7e8115fSAndreas Gohr        // get level and title
101a4a2d4cfSAndreas Gohr        $title = trim($match);
102a4a2d4cfSAndreas Gohr        $level = 7 - strspn($title,'=');
103d7e8115fSAndreas Gohr        if($level < 1) $level = 1;
104a4a2d4cfSAndreas Gohr        $title = trim($title,'=');
105a4a2d4cfSAndreas Gohr        $title = trim($title);
1060cecf9d5Sandi
107e1c10e4dSchris        if ($this->status['section']) $this->_addCall('section_close',array(),$pos);
108e1c10e4dSchris
109b203781fSBen Coburn        if ($level<=$conf['maxseclevel']) {
11035dae8b0SBen Coburn            $this->_addCall('section_edit',array($this->status['section_edit_start'], $pos-1, $this->status['section_edit_level'], $this->status['section_edit_title']), $pos);
11135dae8b0SBen Coburn            $this->status['section_edit_start'] = $pos;
11235dae8b0SBen Coburn            $this->status['section_edit_level'] = $level;
11335dae8b0SBen Coburn            $this->status['section_edit_title'] = $title;
114b203781fSBen Coburn        }
11535dae8b0SBen Coburn
116433bef32Sandi        $this->_addCall('header',array($title,$level,$pos), $pos);
117e1c10e4dSchris
118e1c10e4dSchris        $this->_addCall('section_open',array($level),$pos);
11944881bd0Shenning.noren        $this->status['section'] = true;
12044881bd0Shenning.noren        return true;
1210cecf9d5Sandi    }
1220cecf9d5Sandi
1230cecf9d5Sandi    function notoc($match, $state, $pos) {
124e41c4da9SAndreas Gohr        $this->_addCall('notoc',array(),$pos);
12544881bd0Shenning.noren        return true;
1260cecf9d5Sandi    }
1270cecf9d5Sandi
1289dc2c2afSandi    function nocache($match, $state, $pos) {
1299dc2c2afSandi        $this->_addCall('nocache',array(),$pos);
13044881bd0Shenning.noren        return true;
1319dc2c2afSandi    }
1329dc2c2afSandi
1330cecf9d5Sandi    function linebreak($match, $state, $pos) {
134433bef32Sandi        $this->_addCall('linebreak',array(),$pos);
13544881bd0Shenning.noren        return true;
1360cecf9d5Sandi    }
1370cecf9d5Sandi
1380cecf9d5Sandi    function eol($match, $state, $pos) {
139433bef32Sandi        $this->_addCall('eol',array(),$pos);
14044881bd0Shenning.noren        return true;
1410cecf9d5Sandi    }
1420cecf9d5Sandi
1430cecf9d5Sandi    function hr($match, $state, $pos) {
144433bef32Sandi        $this->_addCall('hr',array(),$pos);
14544881bd0Shenning.noren        return true;
1460cecf9d5Sandi    }
1470cecf9d5Sandi
148433bef32Sandi    function _nestingTag($match, $state, $pos, $name) {
1490cecf9d5Sandi        switch ( $state ) {
1500cecf9d5Sandi            case DOKU_LEXER_ENTER:
151433bef32Sandi                $this->_addCall($name.'_open', array(), $pos);
1520cecf9d5Sandi            break;
1530cecf9d5Sandi            case DOKU_LEXER_EXIT:
154433bef32Sandi                $this->_addCall($name.'_close', array(), $pos);
1550cecf9d5Sandi            break;
1560cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
157433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
1580cecf9d5Sandi            break;
1590cecf9d5Sandi        }
1600cecf9d5Sandi    }
1610cecf9d5Sandi
1620cecf9d5Sandi    function strong($match, $state, $pos) {
163433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'strong');
16444881bd0Shenning.noren        return true;
1650cecf9d5Sandi    }
1660cecf9d5Sandi
1670cecf9d5Sandi    function emphasis($match, $state, $pos) {
168433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'emphasis');
16944881bd0Shenning.noren        return true;
1700cecf9d5Sandi    }
1710cecf9d5Sandi
1720cecf9d5Sandi    function underline($match, $state, $pos) {
173433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'underline');
17444881bd0Shenning.noren        return true;
1750cecf9d5Sandi    }
1760cecf9d5Sandi
1770cecf9d5Sandi    function monospace($match, $state, $pos) {
178433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'monospace');
17944881bd0Shenning.noren        return true;
1800cecf9d5Sandi    }
1810cecf9d5Sandi
1820cecf9d5Sandi    function subscript($match, $state, $pos) {
183433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'subscript');
18444881bd0Shenning.noren        return true;
1850cecf9d5Sandi    }
1860cecf9d5Sandi
1870cecf9d5Sandi    function superscript($match, $state, $pos) {
188433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'superscript');
18944881bd0Shenning.noren        return true;
1900cecf9d5Sandi    }
1910cecf9d5Sandi
1920cecf9d5Sandi    function deleted($match, $state, $pos) {
193433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'deleted');
19444881bd0Shenning.noren        return true;
1950cecf9d5Sandi    }
1960cecf9d5Sandi
1970cecf9d5Sandi
1980cecf9d5Sandi    function footnote($match, $state, $pos) {
1995587e44cSchris//        $this->_nestingTag($match, $state, $pos, 'footnote');
200742c66f8Schris        if (!isset($this->_footnote)) $this->_footnote = false;
2012fe7363dSchris
2025587e44cSchris        switch ( $state ) {
2035587e44cSchris            case DOKU_LEXER_ENTER:
2042fe7363dSchris                // footnotes can not be nested - however due to limitations in lexer it can't be prevented
2052fe7363dSchris                // we will still enter a new footnote mode, we just do nothing
206742c66f8Schris                if ($this->_footnote) {
2072fe7363dSchris                  $this->_addCall('cdata',array($match), $pos);
2082fe7363dSchris                  break;
2092fe7363dSchris                }
2102fe7363dSchris
211742c66f8Schris                $this->_footnote = true;
2122fe7363dSchris
2135587e44cSchris                $ReWriter = & new Doku_Handler_Nest($this->CallWriter,'footnote_close');
2145587e44cSchris                $this->CallWriter = & $ReWriter;
2154a26ad85Schris                $this->_addCall('footnote_open', array(), $pos);
2165587e44cSchris            break;
2175587e44cSchris            case DOKU_LEXER_EXIT:
2182fe7363dSchris                // check whether we have already exitted the footnote mode, can happen if the modes were nested
219742c66f8Schris                if (!$this->_footnote) {
2202fe7363dSchris                  $this->_addCall('cdata',array($match), $pos);
2212fe7363dSchris                  break;
2222fe7363dSchris                }
2232fe7363dSchris
224742c66f8Schris                $this->_footnote = false;
2252fe7363dSchris
2265587e44cSchris                $this->_addCall('footnote_close', array(), $pos);
2275587e44cSchris                $this->CallWriter->process();
2285587e44cSchris                $ReWriter = & $this->CallWriter;
2295587e44cSchris                $this->CallWriter = & $ReWriter->CallWriter;
2305587e44cSchris            break;
2315587e44cSchris            case DOKU_LEXER_UNMATCHED:
2325587e44cSchris                $this->_addCall('cdata', array($match), $pos);
2335587e44cSchris            break;
2345587e44cSchris        }
23544881bd0Shenning.noren        return true;
2360cecf9d5Sandi    }
2370cecf9d5Sandi
2380cecf9d5Sandi    function listblock($match, $state, $pos) {
2390cecf9d5Sandi        switch ( $state ) {
2400cecf9d5Sandi            case DOKU_LEXER_ENTER:
2410cecf9d5Sandi                $ReWriter = & new Doku_Handler_List($this->CallWriter);
2420cecf9d5Sandi                $this->CallWriter = & $ReWriter;
243433bef32Sandi                $this->_addCall('list_open', array($match), $pos);
2440cecf9d5Sandi            break;
2450cecf9d5Sandi            case DOKU_LEXER_EXIT:
246433bef32Sandi                $this->_addCall('list_close', array(), $pos);
2470cecf9d5Sandi                $this->CallWriter->process();
2480cecf9d5Sandi                $ReWriter = & $this->CallWriter;
2490cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
2500cecf9d5Sandi            break;
2510cecf9d5Sandi            case DOKU_LEXER_MATCHED:
252433bef32Sandi                $this->_addCall('list_item', array($match), $pos);
2530cecf9d5Sandi            break;
2540cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
255433bef32Sandi                $this->_addCall('cdata', array($match), $pos);
2560cecf9d5Sandi            break;
2570cecf9d5Sandi        }
25844881bd0Shenning.noren        return true;
2590cecf9d5Sandi    }
2600cecf9d5Sandi
2610cecf9d5Sandi    function unformatted($match, $state, $pos) {
2620cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
263433bef32Sandi            $this->_addCall('unformatted',array($match), $pos);
2640cecf9d5Sandi        }
26544881bd0Shenning.noren        return true;
2660cecf9d5Sandi    }
2670cecf9d5Sandi
2680cecf9d5Sandi    function php($match, $state, $pos) {
269df9add72Schris        global $conf;
2700cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
271df9add72Schris            if ($conf['phpok']) {
272433bef32Sandi                $this->_addCall('php',array($match), $pos);
273df9add72Schris            } else {
274df9add72Schris                $this->_addCall('file',array($match), $pos);
275df9add72Schris            }
2760cecf9d5Sandi        }
27744881bd0Shenning.noren        return true;
2780cecf9d5Sandi    }
2790cecf9d5Sandi
28007f89c3cSAnika Henke    function phpblock($match, $state, $pos) {
28107f89c3cSAnika Henke        global $conf;
28207f89c3cSAnika Henke        if ( $state == DOKU_LEXER_UNMATCHED ) {
28307f89c3cSAnika Henke            if ($conf['phpok']) {
28407f89c3cSAnika Henke                $this->_addCall('phpblock',array($match), $pos);
28507f89c3cSAnika Henke            } else {
28607f89c3cSAnika Henke                $this->_addCall('file',array($match), $pos);
28707f89c3cSAnika Henke            }
28807f89c3cSAnika Henke        }
28907f89c3cSAnika Henke        return true;
29007f89c3cSAnika Henke    }
29107f89c3cSAnika Henke
2920cecf9d5Sandi    function html($match, $state, $pos) {
293df9add72Schris        global $conf;
2940cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
295df9add72Schris            if($conf['htmlok']){
296433bef32Sandi                $this->_addCall('html',array($match), $pos);
297df9add72Schris            } else {
298df9add72Schris                $this->_addCall('file',array($match), $pos);
299df9add72Schris            }
3000cecf9d5Sandi        }
30144881bd0Shenning.noren        return true;
3020cecf9d5Sandi    }
3030cecf9d5Sandi
30407f89c3cSAnika Henke    function htmlblock($match, $state, $pos) {
30507f89c3cSAnika Henke        global $conf;
30607f89c3cSAnika Henke        if ( $state == DOKU_LEXER_UNMATCHED ) {
30707f89c3cSAnika Henke            if($conf['htmlok']){
30807f89c3cSAnika Henke                $this->_addCall('htmlblock',array($match), $pos);
30907f89c3cSAnika Henke            } else {
31007f89c3cSAnika Henke                $this->_addCall('file',array($match), $pos);
31107f89c3cSAnika Henke            }
31207f89c3cSAnika Henke        }
31307f89c3cSAnika Henke        return true;
31407f89c3cSAnika Henke    }
31507f89c3cSAnika Henke
3160cecf9d5Sandi    function preformatted($match, $state, $pos) {
3170cecf9d5Sandi        switch ( $state ) {
3180cecf9d5Sandi            case DOKU_LEXER_ENTER:
3190cecf9d5Sandi                $ReWriter = & new Doku_Handler_Preformatted($this->CallWriter);
3200cecf9d5Sandi                $this->CallWriter = & $ReWriter;
321433bef32Sandi                $this->_addCall('preformatted_start',array(), $pos);
3220cecf9d5Sandi            break;
3230cecf9d5Sandi            case DOKU_LEXER_EXIT:
324433bef32Sandi                $this->_addCall('preformatted_end',array(), $pos);
3250cecf9d5Sandi                $this->CallWriter->process();
3260cecf9d5Sandi                $ReWriter = & $this->CallWriter;
3270cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
3280cecf9d5Sandi            break;
3290cecf9d5Sandi            case DOKU_LEXER_MATCHED:
330433bef32Sandi                $this->_addCall('preformatted_newline',array(), $pos);
3310cecf9d5Sandi            break;
3320cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
333433bef32Sandi                $this->_addCall('preformatted_content',array($match), $pos);
3340cecf9d5Sandi            break;
3350cecf9d5Sandi        }
3360cecf9d5Sandi
33744881bd0Shenning.noren        return true;
3380cecf9d5Sandi    }
3390cecf9d5Sandi
3400cecf9d5Sandi    function file($match, $state, $pos) {
3410cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
342433bef32Sandi            $this->_addCall('file',array($match), $pos);
3430cecf9d5Sandi        }
34444881bd0Shenning.noren        return true;
3450cecf9d5Sandi    }
3460cecf9d5Sandi
3470cecf9d5Sandi    function quote($match, $state, $pos) {
3480cecf9d5Sandi
3490cecf9d5Sandi        switch ( $state ) {
3500cecf9d5Sandi
3510cecf9d5Sandi            case DOKU_LEXER_ENTER:
3520cecf9d5Sandi                $ReWriter = & new Doku_Handler_Quote($this->CallWriter);
3530cecf9d5Sandi                $this->CallWriter = & $ReWriter;
354433bef32Sandi                $this->_addCall('quote_start',array($match), $pos);
3550cecf9d5Sandi            break;
3560cecf9d5Sandi
3570cecf9d5Sandi            case DOKU_LEXER_EXIT:
358433bef32Sandi                $this->_addCall('quote_end',array(), $pos);
3590cecf9d5Sandi                $this->CallWriter->process();
3600cecf9d5Sandi                $ReWriter = & $this->CallWriter;
3610cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
3620cecf9d5Sandi            break;
3630cecf9d5Sandi
3640cecf9d5Sandi            case DOKU_LEXER_MATCHED:
365433bef32Sandi                $this->_addCall('quote_newline',array($match), $pos);
3660cecf9d5Sandi            break;
3670cecf9d5Sandi
3680cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
369433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
3700cecf9d5Sandi            break;
3710cecf9d5Sandi
3720cecf9d5Sandi        }
3730cecf9d5Sandi
37444881bd0Shenning.noren        return true;
3750cecf9d5Sandi    }
3760cecf9d5Sandi
3770cecf9d5Sandi    function code($match, $state, $pos) {
3780cecf9d5Sandi        switch ( $state ) {
3790cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
3800cecf9d5Sandi                $matches = preg_split('/>/u',$match,2);
3810cecf9d5Sandi                $matches[0] = trim($matches[0]);
3820cecf9d5Sandi                if ( trim($matches[0]) == '' ) {
3830cecf9d5Sandi                    $matches[0] = NULL;
3840cecf9d5Sandi                }
3850cecf9d5Sandi                # $matches[0] contains name of programming language
386b5bef19bSandi                # if available, We shortcut html here.
387b5bef19bSandi                if($matches[0] == 'html') $matches[0] = 'html4strict';
388433bef32Sandi                $this->_addCall(
3890cecf9d5Sandi                        'code',
3900cecf9d5Sandi                        array($matches[1],$matches[0]),
3910cecf9d5Sandi                        $pos
3920cecf9d5Sandi                    );
3930cecf9d5Sandi            break;
3940cecf9d5Sandi        }
39544881bd0Shenning.noren        return true;
3960cecf9d5Sandi    }
3970cecf9d5Sandi
3980cecf9d5Sandi    function acronym($match, $state, $pos) {
399433bef32Sandi        $this->_addCall('acronym',array($match), $pos);
40044881bd0Shenning.noren        return true;
4010cecf9d5Sandi    }
4020cecf9d5Sandi
4030cecf9d5Sandi    function smiley($match, $state, $pos) {
404433bef32Sandi        $this->_addCall('smiley',array($match), $pos);
40544881bd0Shenning.noren        return true;
4060cecf9d5Sandi    }
4070cecf9d5Sandi
4080cecf9d5Sandi    function wordblock($match, $state, $pos) {
409433bef32Sandi        $this->_addCall('wordblock',array($match), $pos);
41044881bd0Shenning.noren        return true;
4110cecf9d5Sandi    }
4120cecf9d5Sandi
4130cecf9d5Sandi    function entity($match, $state, $pos) {
414433bef32Sandi        $this->_addCall('entity',array($match), $pos);
41544881bd0Shenning.noren        return true;
4160cecf9d5Sandi    }
4170cecf9d5Sandi
4180cecf9d5Sandi    function multiplyentity($match, $state, $pos) {
4190cecf9d5Sandi        preg_match_all('/\d+/',$match,$matches);
420433bef32Sandi        $this->_addCall('multiplyentity',array($matches[0][0],$matches[0][1]), $pos);
42144881bd0Shenning.noren        return true;
4220cecf9d5Sandi    }
4230cecf9d5Sandi
4240cecf9d5Sandi    function singlequoteopening($match, $state, $pos) {
425433bef32Sandi        $this->_addCall('singlequoteopening',array(), $pos);
42644881bd0Shenning.noren        return true;
4270cecf9d5Sandi    }
4280cecf9d5Sandi
4290cecf9d5Sandi    function singlequoteclosing($match, $state, $pos) {
430433bef32Sandi        $this->_addCall('singlequoteclosing',array(), $pos);
43144881bd0Shenning.noren        return true;
4320cecf9d5Sandi    }
4330cecf9d5Sandi
43457d757d1SAndreas Gohr    function apostrophe($match, $state, $pos) {
43557d757d1SAndreas Gohr        $this->_addCall('apostrophe',array(), $pos);
43657d757d1SAndreas Gohr        return true;
43757d757d1SAndreas Gohr    }
43857d757d1SAndreas Gohr
4390cecf9d5Sandi    function doublequoteopening($match, $state, $pos) {
440433bef32Sandi        $this->_addCall('doublequoteopening',array(), $pos);
44144881bd0Shenning.noren        return true;
4420cecf9d5Sandi    }
4430cecf9d5Sandi
4440cecf9d5Sandi    function doublequoteclosing($match, $state, $pos) {
445433bef32Sandi        $this->_addCall('doublequoteclosing',array(), $pos);
44644881bd0Shenning.noren        return true;
4470cecf9d5Sandi    }
4480cecf9d5Sandi
4490cecf9d5Sandi    function camelcaselink($match, $state, $pos) {
450433bef32Sandi        $this->_addCall('camelcaselink',array($match), $pos);
45144881bd0Shenning.noren        return true;
4520cecf9d5Sandi    }
4530cecf9d5Sandi
4540cecf9d5Sandi    /*
4550cecf9d5Sandi    */
4560cecf9d5Sandi    function internallink($match, $state, $pos) {
4570cecf9d5Sandi        // Strip the opening and closing markup
4580cecf9d5Sandi        $link = preg_replace(array('/^\[\[/','/\]\]$/u'),'',$match);
4590cecf9d5Sandi
4600cecf9d5Sandi        // Split title from URL
4610cecf9d5Sandi        $link = preg_split('/\|/u',$link,2);
4620cecf9d5Sandi        if ( !isset($link[1]) ) {
4630cecf9d5Sandi            $link[1] = NULL;
4640cecf9d5Sandi        } else if ( preg_match('/^\{\{[^\}]+\}\}$/',$link[1]) ) {
4655578eb8fSandi            // If the title is an image, convert it to an array containing the image details
466b625487dSandi            $link[1] = Doku_Handler_Parse_Media($link[1]);
4670cecf9d5Sandi        }
4680b7c14c2Sandi        $link[0] = trim($link[0]);
4690cecf9d5Sandi
4700e1c636eSandi        //decide which kind of link it is
4710e1c636eSandi
4721c88f0b1SRobby Cornelissen        if ( preg_match('/^[a-zA-Z\.]+>{1}.*$/u',$link[0]) ) {
4730e1c636eSandi        // Interwiki
4740cecf9d5Sandi            $interwiki = preg_split('/>/u',$link[0]);
475433bef32Sandi            $this->_addCall(
4760cecf9d5Sandi                'interwikilink',
4770cecf9d5Sandi                array($link[0],$link[1],strtolower($interwiki[0]),$interwiki[1]),
4780cecf9d5Sandi                $pos
4790cecf9d5Sandi                );
4800b7c14c2Sandi        }elseif ( preg_match('/^\\\\\\\\[\w.:?\-;,]+?\\\\/u',$link[0]) ) {
4810e1c636eSandi        // Windows Share
482433bef32Sandi            $this->_addCall(
4830cecf9d5Sandi                'windowssharelink',
4840cecf9d5Sandi                array($link[0],$link[1]),
4850cecf9d5Sandi                $pos
4860cecf9d5Sandi                );
4874468cb4cSAndreas Gohr        }elseif ( preg_match('#^([a-z0-9\-\.+]+?)://#i',$link[0]) ) {
4880e1c636eSandi        // external link (accepts all protocols)
489433bef32Sandi            $this->_addCall(
4900cecf9d5Sandi                    'externallink',
4910cecf9d5Sandi                    array($link[0],$link[1]),
4920cecf9d5Sandi                    $pos
4930cecf9d5Sandi                    );
4940a1d30bfSchris        }elseif ( preg_match('<'.PREG_PATTERN_VALID_EMAIL.'>',$link[0]) ) {
4950a1d30bfSchris        // E-Mail (pattern above is defined in inc/mail.php)
496a6755281Sandi            $this->_addCall(
497a6755281Sandi                'emaillink',
498a6755281Sandi                array($link[0],$link[1]),
499a6755281Sandi                $pos
500a6755281Sandi                );
5010b7c14c2Sandi        }elseif ( preg_match('!^#.+!',$link[0]) ){
5020b7c14c2Sandi        // local link
5030b7c14c2Sandi            $this->_addCall(
5040b7c14c2Sandi                'locallink',
5050b7c14c2Sandi                array(substr($link[0],1),$link[1]),
5060b7c14c2Sandi                $pos
5070b7c14c2Sandi                );
5080e1c636eSandi        }else{
5090e1c636eSandi        // internal link
510433bef32Sandi            $this->_addCall(
5110e1c636eSandi                'internallink',
5120e1c636eSandi                array($link[0],$link[1]),
5130e1c636eSandi                $pos
5140e1c636eSandi                );
5150cecf9d5Sandi        }
5160e1c636eSandi
51744881bd0Shenning.noren        return true;
5180cecf9d5Sandi    }
5190cecf9d5Sandi
5200cecf9d5Sandi    function filelink($match, $state, $pos) {
521433bef32Sandi        $this->_addCall('filelink',array($match, NULL), $pos);
52244881bd0Shenning.noren        return true;
5230cecf9d5Sandi    }
5240cecf9d5Sandi
5250cecf9d5Sandi    function windowssharelink($match, $state, $pos) {
526433bef32Sandi        $this->_addCall('windowssharelink',array($match, NULL), $pos);
52744881bd0Shenning.noren        return true;
5280cecf9d5Sandi    }
5290cecf9d5Sandi
5300cecf9d5Sandi    function media($match, $state, $pos) {
5310cecf9d5Sandi        $p = Doku_Handler_Parse_Media($match);
5320cecf9d5Sandi
533433bef32Sandi        $this->_addCall(
5340cecf9d5Sandi              $p['type'],
535dc673a5bSjoe.lapp              array($p['src'], $p['title'], $p['align'], $p['width'],
536dc673a5bSjoe.lapp                     $p['height'], $p['cache'], $p['linking']),
5370cecf9d5Sandi              $pos
5380cecf9d5Sandi             );
53944881bd0Shenning.noren        return true;
5400cecf9d5Sandi    }
5410cecf9d5Sandi
542b625487dSandi    function rss($match, $state, $pos) {
543b625487dSandi        $link = preg_replace(array('/^\{\{rss>/','/\}\}$/'),'',$match);
5443db95becSAndreas Gohr
5453db95becSAndreas Gohr        // get params
5463db95becSAndreas Gohr        list($link,$params) = explode(' ',$link,2);
5473db95becSAndreas Gohr
5483db95becSAndreas Gohr        $p = array();
5493db95becSAndreas Gohr        if(preg_match('/\b(\d+)\b/',$params,$match)){
5503db95becSAndreas Gohr            $p['max'] = $match[1];
5513db95becSAndreas Gohr        }else{
5523db95becSAndreas Gohr            $p['max'] = 8;
5533db95becSAndreas Gohr        }
5543db95becSAndreas Gohr        $p['reverse'] = (preg_match('/rev/',$params));
5553db95becSAndreas Gohr        $p['author']  = (preg_match('/\b(by|author)/',$params));
5563db95becSAndreas Gohr        $p['date']    = (preg_match('/\b(date)/',$params));
5573db95becSAndreas Gohr        $p['details'] = (preg_match('/\b(desc|detail)/',$params));
5583db95becSAndreas Gohr
5590a69dff7Schris        if (preg_match('/\b(\d+)([dhm])\b/',$params,$match)) {
5600a69dff7Schris          $period = array('d' => 86400, 'h' => 3600, 'm' => 60);
5610a69dff7Schris          $p['refresh'] = max(600,$match[1]*$period[$match[2]]);  // n * period in seconds, minimum 10 minutes
5620a69dff7Schris        } else {
5630a69dff7Schris          $p['refresh'] = 14400;   // default to 4 hours
5640a69dff7Schris        }
5650a69dff7Schris
5663db95becSAndreas Gohr        $this->_addCall('rss',array($link,$p),$pos);
56744881bd0Shenning.noren        return true;
568b625487dSandi    }
569b625487dSandi
5700cecf9d5Sandi    function externallink($match, $state, $pos) {
571da9f31c5SAndreas Gohr        $url   = $match;
572da9f31c5SAndreas Gohr        $title = null;
5730cecf9d5Sandi
574da9f31c5SAndreas Gohr        // add protocol on simple short URLs
575da9f31c5SAndreas Gohr        if(substr($url,0,3) == 'ftp' && (substr($url,0,6) != 'ftp://')){
576da9f31c5SAndreas Gohr            $title = $url;
577da9f31c5SAndreas Gohr            $url   = 'ftp://'.$url;
578da9f31c5SAndreas Gohr        }
579da9f31c5SAndreas Gohr        if(substr($url,0,3) == 'www' && (substr($url,0,7) != 'http://')){
580da9f31c5SAndreas Gohr            $title = $url;
581da9f31c5SAndreas Gohr            $url = 'http://'.$url;
582da9f31c5SAndreas Gohr        }
583da9f31c5SAndreas Gohr
584da9f31c5SAndreas Gohr        $this->_addCall('externallink',array($url, $title), $pos);
58544881bd0Shenning.noren        return true;
5860cecf9d5Sandi    }
5870cecf9d5Sandi
58871352defSandi    function emaillink($match, $state, $pos) {
5890cecf9d5Sandi        $email = preg_replace(array('/^</','/>$/'),'',$match);
590433bef32Sandi        $this->_addCall('emaillink',array($email, NULL), $pos);
59144881bd0Shenning.noren        return true;
5920cecf9d5Sandi    }
5930cecf9d5Sandi
5940cecf9d5Sandi    function table($match, $state, $pos) {
5950cecf9d5Sandi        switch ( $state ) {
5960cecf9d5Sandi
5970cecf9d5Sandi            case DOKU_LEXER_ENTER:
5980cecf9d5Sandi
5990cecf9d5Sandi                $ReWriter = & new Doku_Handler_Table($this->CallWriter);
6000cecf9d5Sandi                $this->CallWriter = & $ReWriter;
6010cecf9d5Sandi
602433bef32Sandi                $this->_addCall('table_start', array(), $pos);
603433bef32Sandi                //$this->_addCall('table_row', array(), $pos);
6040cecf9d5Sandi                if ( trim($match) == '^' ) {
605433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
6060cecf9d5Sandi                } else {
607433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
6080cecf9d5Sandi                }
6090cecf9d5Sandi            break;
6100cecf9d5Sandi
6110cecf9d5Sandi            case DOKU_LEXER_EXIT:
612433bef32Sandi                $this->_addCall('table_end', array(), $pos);
6130cecf9d5Sandi                $this->CallWriter->process();
6140cecf9d5Sandi                $ReWriter = & $this->CallWriter;
6150cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
6160cecf9d5Sandi            break;
6170cecf9d5Sandi
6180cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
6190cecf9d5Sandi                if ( trim($match) != '' ) {
620433bef32Sandi                    $this->_addCall('cdata',array($match), $pos);
6210cecf9d5Sandi                }
6220cecf9d5Sandi            break;
6230cecf9d5Sandi
6240cecf9d5Sandi            case DOKU_LEXER_MATCHED:
6259ab75d9eSAndreas Gohr                if ( $match == ' ' ){
6269ab75d9eSAndreas Gohr                    $this->_addCall('cdata', array($match), $pos);
627e205b721SAndreas Gohr                } else if ( preg_match('/\t+/',$match) ) {
6289ab75d9eSAndreas Gohr                    $this->_addCall('table_align', array($match), $pos);
629e205b721SAndreas Gohr                } else if ( preg_match('/ {2,}/',$match) ) {
630433bef32Sandi                    $this->_addCall('table_align', array($match), $pos);
6310cecf9d5Sandi                } else if ( $match == "\n|" ) {
632433bef32Sandi                    $this->_addCall('table_row', array(), $pos);
633433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
6340cecf9d5Sandi                } else if ( $match == "\n^" ) {
635433bef32Sandi                    $this->_addCall('table_row', array(), $pos);
636433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
6370cecf9d5Sandi                } else if ( $match == '|' ) {
638433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
6390cecf9d5Sandi                } else if ( $match == '^' ) {
640433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
6410cecf9d5Sandi                }
6420cecf9d5Sandi            break;
6430cecf9d5Sandi        }
64444881bd0Shenning.noren        return true;
6450cecf9d5Sandi    }
6460cecf9d5Sandi}
6470cecf9d5Sandi
6480cecf9d5Sandi//------------------------------------------------------------------------
6490cecf9d5Sandifunction Doku_Handler_Parse_Media($match) {
6500cecf9d5Sandi
6510cecf9d5Sandi    // Strip the opening and closing markup
6520cecf9d5Sandi    $link = preg_replace(array('/^\{\{/','/\}\}$/u'),'',$match);
6530cecf9d5Sandi
6540cecf9d5Sandi    // Split title from URL
6550cecf9d5Sandi    $link = preg_split('/\|/u',$link,2);
6560cecf9d5Sandi
6570cecf9d5Sandi
6580cecf9d5Sandi    // Check alignment
6590cecf9d5Sandi    $ralign = (bool)preg_match('/^ /',$link[0]);
6600cecf9d5Sandi    $lalign = (bool)preg_match('/ $/',$link[0]);
6610cecf9d5Sandi
6620cecf9d5Sandi    // Logic = what's that ;)...
6630cecf9d5Sandi    if ( $lalign & $ralign ) {
6640cecf9d5Sandi        $align = 'center';
6650cecf9d5Sandi    } else if ( $ralign ) {
6660cecf9d5Sandi        $align = 'right';
6670cecf9d5Sandi    } else if ( $lalign ) {
6680cecf9d5Sandi        $align = 'left';
6690cecf9d5Sandi    } else {
6700cecf9d5Sandi        $align = NULL;
6710cecf9d5Sandi    }
6720cecf9d5Sandi
6730cecf9d5Sandi    // The title...
6740cecf9d5Sandi    if ( !isset($link[1]) ) {
6750cecf9d5Sandi        $link[1] = NULL;
6760cecf9d5Sandi    }
6770cecf9d5Sandi
6784826ab45Sandi    //remove aligning spaces
6794826ab45Sandi    $link[0] = trim($link[0]);
6800cecf9d5Sandi
6814826ab45Sandi    //split into src and parameters (using the very last questionmark)
6824826ab45Sandi    $pos = strrpos($link[0], '?');
6834826ab45Sandi    if($pos !== false){
6844826ab45Sandi        $src   = substr($link[0],0,$pos);
6854826ab45Sandi        $param = substr($link[0],$pos+1);
6860cecf9d5Sandi    }else{
6874826ab45Sandi        $src   = $link[0];
6884826ab45Sandi        $param = '';
6890cecf9d5Sandi    }
6900cecf9d5Sandi
6914826ab45Sandi    //parse width and height
6924826ab45Sandi    if(preg_match('#(\d+)(x(\d+))?#i',$param,$size)){
6934826ab45Sandi        ($size[1]) ? $w = $size[1] : $w = NULL;
6944826ab45Sandi        ($size[3]) ? $h = $size[3] : $h = NULL;
695fc1c55b1Shfuecks    } else {
696fc1c55b1Shfuecks        $w = NULL;
697fc1c55b1Shfuecks        $h = NULL;
6980cecf9d5Sandi    }
6990cecf9d5Sandi
700dc673a5bSjoe.lapp    //get linking command
701d35ab615Shenning.noren    if(preg_match('/nolink/i',$param)){
702dc673a5bSjoe.lapp        $linking = 'nolink';
703d35ab615Shenning.noren    }else if(preg_match('/direct/i',$param)){
704dc673a5bSjoe.lapp        $linking = 'direct';
705dc673a5bSjoe.lapp    }else{
706dc673a5bSjoe.lapp        $linking = 'details';
707dc673a5bSjoe.lapp    }
708dc673a5bSjoe.lapp
7094826ab45Sandi    //get caching command
7104826ab45Sandi    if (preg_match('/(nocache|recache)/i',$param,$cachemode)){
7114826ab45Sandi        $cache = $cachemode[1];
7120cecf9d5Sandi    }else{
7134826ab45Sandi        $cache = 'cache';
7140cecf9d5Sandi    }
7150cecf9d5Sandi
7160cecf9d5Sandi    // Check whether this is a local or remote image
7174826ab45Sandi    if ( preg_match('#^(https?|ftp)#i',$src) ) {
7184826ab45Sandi        $call = 'externalmedia';
7190cecf9d5Sandi    } else {
7204826ab45Sandi        $call = 'internalmedia';
7210cecf9d5Sandi    }
7220cecf9d5Sandi
7230cecf9d5Sandi    $params = array(
7240cecf9d5Sandi        'type'=>$call,
7254826ab45Sandi        'src'=>$src,
7260cecf9d5Sandi        'title'=>$link[1],
7270cecf9d5Sandi        'align'=>$align,
7284826ab45Sandi        'width'=>$w,
7294826ab45Sandi        'height'=>$h,
7300cecf9d5Sandi        'cache'=>$cache,
731dc673a5bSjoe.lapp        'linking'=>$linking,
7320cecf9d5Sandi    );
7330cecf9d5Sandi
7340cecf9d5Sandi    return $params;
7350cecf9d5Sandi}
7360cecf9d5Sandi
7370cecf9d5Sandi//------------------------------------------------------------------------
7380cecf9d5Sandiclass Doku_Handler_CallWriter {
7390cecf9d5Sandi
7400cecf9d5Sandi    var $Handler;
7410cecf9d5Sandi
7420cecf9d5Sandi    function Doku_Handler_CallWriter(& $Handler) {
7430cecf9d5Sandi        $this->Handler = & $Handler;
7440cecf9d5Sandi    }
7450cecf9d5Sandi
7460cecf9d5Sandi    function writeCall($call) {
7470cecf9d5Sandi        $this->Handler->calls[] = $call;
7480cecf9d5Sandi    }
7490cecf9d5Sandi
7500cecf9d5Sandi    function writeCalls($calls) {
7510cecf9d5Sandi        $this->Handler->calls = array_merge($this->Handler->calls, $calls);
7520cecf9d5Sandi    }
753f4f02a0fSchris
754f4f02a0fSchris    // function is required, but since this call writer is first/highest in
755f4f02a0fSchris    // the chain it is not required to do anything
756f4f02a0fSchris    function finalise() {
757f4f02a0fSchris    }
7580cecf9d5Sandi}
7590cecf9d5Sandi
7600cecf9d5Sandi//------------------------------------------------------------------------
7615587e44cSchris/**
7625587e44cSchris * Generic call writer class to handle nesting of rendering instructions
7635587e44cSchris * within a render instruction. Also see nest() method of renderer base class
7645587e44cSchris *
7655587e44cSchris * @author    Chris Smith <chris@jalakai.co.uk>
7665587e44cSchris */
7675587e44cSchrisclass Doku_Handler_Nest {
7685587e44cSchris
7695587e44cSchris    var $CallWriter;
7705587e44cSchris    var $calls = array();
7715587e44cSchris
7725587e44cSchris    var $closingInstruction;
7735587e44cSchris
7745587e44cSchris    /**
7755587e44cSchris     * constructor
7765587e44cSchris     *
7775587e44cSchris     * @param  object     $CallWriter     the renderers current call writer
7785587e44cSchris     * @param  string     $close          closing instruction name, this is required to properly terminate the
7795587e44cSchris     *                                    syntax mode if the document ends without a closing pattern
7805587e44cSchris     */
7815587e44cSchris    function Doku_Handler_Nest(& $CallWriter, $close="nest_close") {
7825587e44cSchris        $this->CallWriter = & $CallWriter;
7835587e44cSchris
7845587e44cSchris        $this->closingInstruction = $close;
7855587e44cSchris    }
7865587e44cSchris
7875587e44cSchris    function writeCall($call) {
7885587e44cSchris        $this->calls[] = $call;
7895587e44cSchris    }
7905587e44cSchris
7915587e44cSchris    function writeCalls($calls) {
7925587e44cSchris        $this->calls = array_merge($this->calls, $calls);
7935587e44cSchris    }
7945587e44cSchris
7955587e44cSchris    function finalise() {
7965587e44cSchris        $last_call = end($this->calls);
7975587e44cSchris        $this->writeCall(array($this->closingInstruction,array(), $last_call[2]));
7985587e44cSchris
7995587e44cSchris        $this->process();
8005587e44cSchris        $this->CallWriter->finalise();
8015587e44cSchris    }
8025587e44cSchris
8035587e44cSchris    function process() {
8045587e44cSchris        $first_call = reset($this->calls);
8055587e44cSchris        $this->CallWriter->writeCall(array("nest", array($this->calls), $first_call[2]));
8065587e44cSchris    }
8075587e44cSchris}
8085587e44cSchris
8090cecf9d5Sandiclass Doku_Handler_List {
8100cecf9d5Sandi
8110cecf9d5Sandi    var $CallWriter;
8120cecf9d5Sandi
8130cecf9d5Sandi    var $calls = array();
8140cecf9d5Sandi    var $listCalls = array();
8150cecf9d5Sandi    var $listStack = array();
8160cecf9d5Sandi
8170cecf9d5Sandi    function Doku_Handler_List(& $CallWriter) {
8180cecf9d5Sandi        $this->CallWriter = & $CallWriter;
8190cecf9d5Sandi    }
8200cecf9d5Sandi
8210cecf9d5Sandi    function writeCall($call) {
8220cecf9d5Sandi        $this->calls[] = $call;
8230cecf9d5Sandi    }
8240cecf9d5Sandi
8250cecf9d5Sandi    // Probably not needed but just in case...
8260cecf9d5Sandi    function writeCalls($calls) {
8270cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
828f4f02a0fSchris#        $this->CallWriter->writeCalls($this->calls);
829f4f02a0fSchris    }
830f4f02a0fSchris
831f4f02a0fSchris    function finalise() {
832f4f02a0fSchris        $last_call = end($this->calls);
833f4f02a0fSchris        $this->writeCall(array('list_close',array(), $last_call[2]));
834f4f02a0fSchris
835f4f02a0fSchris        $this->process();
836f4f02a0fSchris        $this->CallWriter->finalise();
8370cecf9d5Sandi    }
8380cecf9d5Sandi
8390cecf9d5Sandi    //------------------------------------------------------------------------
8400cecf9d5Sandi    function process() {
841f4f02a0fSchris
8420cecf9d5Sandi        foreach ( $this->calls as $call ) {
8430cecf9d5Sandi            switch ($call[0]) {
8440cecf9d5Sandi                case 'list_item':
8450cecf9d5Sandi                    $this->listOpen($call);
8460cecf9d5Sandi                break;
8470cecf9d5Sandi                case 'list_open':
8480cecf9d5Sandi                    $this->listStart($call);
8490cecf9d5Sandi                break;
8500cecf9d5Sandi                case 'list_close':
8510cecf9d5Sandi                    $this->listEnd($call);
8520cecf9d5Sandi                break;
8530cecf9d5Sandi                default:
8540cecf9d5Sandi                    $this->listContent($call);
8550cecf9d5Sandi                break;
8560cecf9d5Sandi            }
8570cecf9d5Sandi        }
8580cecf9d5Sandi
8590cecf9d5Sandi        $this->CallWriter->writeCalls($this->listCalls);
8600cecf9d5Sandi    }
8610cecf9d5Sandi
8620cecf9d5Sandi    //------------------------------------------------------------------------
8630cecf9d5Sandi    function listStart($call) {
8640cecf9d5Sandi        $depth = $this->interpretSyntax($call[1][0], $listType);
8650cecf9d5Sandi
8660cecf9d5Sandi        $this->initialDepth = $depth;
8670cecf9d5Sandi        $this->listStack[] = array($listType, $depth);
8680cecf9d5Sandi
8690cecf9d5Sandi        $this->listCalls[] = array('list'.$listType.'_open',array(),$call[2]);
8700cecf9d5Sandi        $this->listCalls[] = array('listitem_open',array(1),$call[2]);
8710cecf9d5Sandi        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
8720cecf9d5Sandi    }
8730cecf9d5Sandi
8740cecf9d5Sandi    //------------------------------------------------------------------------
8750cecf9d5Sandi    function listEnd($call) {
87644881bd0Shenning.noren        $closeContent = true;
8770cecf9d5Sandi
8780cecf9d5Sandi        while ( $list = array_pop($this->listStack) ) {
8790cecf9d5Sandi            if ( $closeContent ) {
8800cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
88144881bd0Shenning.noren                $closeContent = false;
8820cecf9d5Sandi            }
8830cecf9d5Sandi            $this->listCalls[] = array('listitem_close',array(),$call[2]);
8840cecf9d5Sandi            $this->listCalls[] = array('list'.$list[0].'_close', array(), $call[2]);
8850cecf9d5Sandi        }
8860cecf9d5Sandi    }
8870cecf9d5Sandi
8880cecf9d5Sandi    //------------------------------------------------------------------------
8890cecf9d5Sandi    function listOpen($call) {
8900cecf9d5Sandi        $depth = $this->interpretSyntax($call[1][0], $listType);
8910cecf9d5Sandi        $end = end($this->listStack);
8920cecf9d5Sandi
8930cecf9d5Sandi        // Not allowed to be shallower than initialDepth
8940cecf9d5Sandi        if ( $depth < $this->initialDepth ) {
8950cecf9d5Sandi            $depth = $this->initialDepth;
8960cecf9d5Sandi        }
8970cecf9d5Sandi
8980cecf9d5Sandi        //------------------------------------------------------------------------
8990cecf9d5Sandi        if ( $depth == $end[1] ) {
9000cecf9d5Sandi
9010cecf9d5Sandi            // Just another item in the list...
9020cecf9d5Sandi            if ( $listType == $end[0] ) {
9030cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9040cecf9d5Sandi                $this->listCalls[] = array('listitem_close',array(),$call[2]);
9050cecf9d5Sandi                $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
9060cecf9d5Sandi                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9070cecf9d5Sandi
9080cecf9d5Sandi            // Switched list type...
9090cecf9d5Sandi            } else {
9100cecf9d5Sandi
9110cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9120cecf9d5Sandi                $this->listCalls[] = array('listitem_close',array(),$call[2]);
9130cecf9d5Sandi                $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
9140cecf9d5Sandi                $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9150cecf9d5Sandi                $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9160cecf9d5Sandi                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9170cecf9d5Sandi
9180cecf9d5Sandi                array_pop($this->listStack);
9190cecf9d5Sandi                $this->listStack[] = array($listType, $depth);
9200cecf9d5Sandi            }
9210cecf9d5Sandi
9220cecf9d5Sandi        //------------------------------------------------------------------------
9230cecf9d5Sandi        // Getting deeper...
9240cecf9d5Sandi        } else if ( $depth > $end[1] ) {
9250cecf9d5Sandi
9260cecf9d5Sandi            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9270cecf9d5Sandi            $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9280cecf9d5Sandi            $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9290cecf9d5Sandi            $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9300cecf9d5Sandi
9310cecf9d5Sandi            $this->listStack[] = array($listType, $depth);
9320cecf9d5Sandi
9330cecf9d5Sandi        //------------------------------------------------------------------------
9340cecf9d5Sandi        // Getting shallower ( $depth < $end[1] )
9350cecf9d5Sandi        } else {
9360cecf9d5Sandi            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9370cecf9d5Sandi            $this->listCalls[] = array('listitem_close',array(),$call[2]);
9380cecf9d5Sandi            $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
9390cecf9d5Sandi
9400cecf9d5Sandi            // Throw away the end - done
9410cecf9d5Sandi            array_pop($this->listStack);
9420cecf9d5Sandi
9430cecf9d5Sandi            while (1) {
9440cecf9d5Sandi                $end = end($this->listStack);
9450cecf9d5Sandi
9460cecf9d5Sandi                if ( $end[1] <= $depth ) {
9470cecf9d5Sandi
9480cecf9d5Sandi                    // Normalize depths
9490cecf9d5Sandi                    $depth = $end[1];
9500cecf9d5Sandi
9510cecf9d5Sandi                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
9520cecf9d5Sandi
9530cecf9d5Sandi                    if ( $end[0] == $listType ) {
9540cecf9d5Sandi                        $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
9550cecf9d5Sandi                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9560cecf9d5Sandi
9570cecf9d5Sandi                    } else {
9580cecf9d5Sandi                        // Switching list type...
9590cecf9d5Sandi                        $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
9600cecf9d5Sandi                        $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9610cecf9d5Sandi                        $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9620cecf9d5Sandi                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9630cecf9d5Sandi
9640cecf9d5Sandi                        array_pop($this->listStack);
9650cecf9d5Sandi                        $this->listStack[] = array($listType, $depth);
9660cecf9d5Sandi                    }
9670cecf9d5Sandi
9680cecf9d5Sandi                    break;
9690cecf9d5Sandi
9700cecf9d5Sandi                // Haven't dropped down far enough yet.... ( $end[1] > $depth )
9710cecf9d5Sandi                } else {
9720cecf9d5Sandi
9730cecf9d5Sandi                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
9740cecf9d5Sandi                    $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
9750cecf9d5Sandi
9760cecf9d5Sandi                    array_pop($this->listStack);
9770cecf9d5Sandi
9780cecf9d5Sandi                }
9790cecf9d5Sandi
9800cecf9d5Sandi            }
9810cecf9d5Sandi
9820cecf9d5Sandi        }
9830cecf9d5Sandi    }
9840cecf9d5Sandi
9850cecf9d5Sandi    //------------------------------------------------------------------------
9860cecf9d5Sandi    function listContent($call) {
9870cecf9d5Sandi        $this->listCalls[] = $call;
9880cecf9d5Sandi    }
9890cecf9d5Sandi
9900cecf9d5Sandi    //------------------------------------------------------------------------
9910cecf9d5Sandi    function interpretSyntax($match, & $type) {
9920cecf9d5Sandi        if ( substr($match,-1) == '*' ) {
9930cecf9d5Sandi            $type = 'u';
9940cecf9d5Sandi        } else {
9950cecf9d5Sandi            $type = 'o';
9960cecf9d5Sandi        }
9970cecf9d5Sandi        return count(explode('  ',str_replace("\t",'  ',$match)));
9980cecf9d5Sandi    }
9990cecf9d5Sandi}
10000cecf9d5Sandi
10010cecf9d5Sandi//------------------------------------------------------------------------
10020cecf9d5Sandiclass Doku_Handler_Preformatted {
10030cecf9d5Sandi
10040cecf9d5Sandi    var $CallWriter;
10050cecf9d5Sandi
10060cecf9d5Sandi    var $calls = array();
10070cecf9d5Sandi    var $pos;
10080cecf9d5Sandi    var $text ='';
10090cecf9d5Sandi
10100cecf9d5Sandi
10110cecf9d5Sandi
10120cecf9d5Sandi    function Doku_Handler_Preformatted(& $CallWriter) {
10130cecf9d5Sandi        $this->CallWriter = & $CallWriter;
10140cecf9d5Sandi    }
10150cecf9d5Sandi
10160cecf9d5Sandi    function writeCall($call) {
10170cecf9d5Sandi        $this->calls[] = $call;
10180cecf9d5Sandi    }
10190cecf9d5Sandi
10200cecf9d5Sandi    // Probably not needed but just in case...
10210cecf9d5Sandi    function writeCalls($calls) {
10220cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1023f4f02a0fSchris#        $this->CallWriter->writeCalls($this->calls);
1024f4f02a0fSchris    }
1025f4f02a0fSchris
1026f4f02a0fSchris    function finalise() {
1027f4f02a0fSchris        $last_call = end($this->calls);
1028f4f02a0fSchris        $this->writeCall(array('preformatted_end',array(), $last_call[2]));
1029f4f02a0fSchris
1030f4f02a0fSchris        $this->process();
1031f4f02a0fSchris        $this->CallWriter->finalise();
10320cecf9d5Sandi    }
10330cecf9d5Sandi
10340cecf9d5Sandi    function process() {
10350cecf9d5Sandi        foreach ( $this->calls as $call ) {
10360cecf9d5Sandi            switch ($call[0]) {
10370cecf9d5Sandi                case 'preformatted_start':
10380cecf9d5Sandi                    $this->pos = $call[2];
10390cecf9d5Sandi                break;
10400cecf9d5Sandi                case 'preformatted_newline':
10410cecf9d5Sandi                    $this->text .= "\n";
10420cecf9d5Sandi                break;
10430cecf9d5Sandi                case 'preformatted_content':
10440cecf9d5Sandi                    $this->text .= $call[1][0];
10450cecf9d5Sandi                break;
10460cecf9d5Sandi                case 'preformatted_end':
10470cecf9d5Sandi                    $this->CallWriter->writeCall(array('preformatted',array($this->text),$this->pos));
10480cecf9d5Sandi                break;
10490cecf9d5Sandi            }
10500cecf9d5Sandi        }
10510cecf9d5Sandi    }
1052f4f02a0fSchris
10530cecf9d5Sandi}
10540cecf9d5Sandi
10550cecf9d5Sandi//------------------------------------------------------------------------
10560cecf9d5Sandiclass Doku_Handler_Quote {
10570cecf9d5Sandi
10580cecf9d5Sandi    var $CallWriter;
10590cecf9d5Sandi
10600cecf9d5Sandi    var $calls = array();
10610cecf9d5Sandi
10620cecf9d5Sandi    var $quoteCalls = array();
10630cecf9d5Sandi
10640cecf9d5Sandi    function Doku_Handler_Quote(& $CallWriter) {
10650cecf9d5Sandi        $this->CallWriter = & $CallWriter;
10660cecf9d5Sandi    }
10670cecf9d5Sandi
10680cecf9d5Sandi    function writeCall($call) {
10690cecf9d5Sandi        $this->calls[] = $call;
10700cecf9d5Sandi    }
10710cecf9d5Sandi
10720cecf9d5Sandi    // Probably not needed but just in case...
10730cecf9d5Sandi    function writeCalls($calls) {
10740cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1075f4f02a0fSchris#        $this->CallWriter->writeCalls($this->calls);
1076f4f02a0fSchris    }
1077f4f02a0fSchris
1078f4f02a0fSchris    function finalise() {
1079f4f02a0fSchris        $last_call = end($this->calls);
1080f4f02a0fSchris        $this->writeCall(array('quote_end',array(), $last_call[2]));
1081f4f02a0fSchris
1082f4f02a0fSchris        $this->process();
1083f4f02a0fSchris        $this->CallWriter->finalise();
10840cecf9d5Sandi    }
10850cecf9d5Sandi
10860cecf9d5Sandi    function process() {
10870cecf9d5Sandi
10880cecf9d5Sandi        $quoteDepth = 1;
10890cecf9d5Sandi
10900cecf9d5Sandi        foreach ( $this->calls as $call ) {
10910cecf9d5Sandi            switch ($call[0]) {
10920cecf9d5Sandi
10930cecf9d5Sandi                case 'quote_start':
10940cecf9d5Sandi
10950cecf9d5Sandi                    $this->quoteCalls[] = array('quote_open',array(),$call[2]);
10960cecf9d5Sandi
10970cecf9d5Sandi                case 'quote_newline':
10980cecf9d5Sandi
10990cecf9d5Sandi                    $quoteLength = $this->getDepth($call[1][0]);
11000cecf9d5Sandi
11010cecf9d5Sandi                    if ( $quoteLength > $quoteDepth ) {
11020cecf9d5Sandi                        $quoteDiff = $quoteLength - $quoteDepth;
11030cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11040cecf9d5Sandi                            $this->quoteCalls[] = array('quote_open',array(),$call[2]);
11050cecf9d5Sandi                        }
11060cecf9d5Sandi                    } else if ( $quoteLength < $quoteDepth ) {
11070cecf9d5Sandi                        $quoteDiff = $quoteDepth - $quoteLength;
11080cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11090cecf9d5Sandi                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11100cecf9d5Sandi                        }
111126426c64Schris                    } else {
111226426c64Schris                        if ($call[0] != 'quote_start') $this->quoteCalls[] = array('linebreak',array(),$call[2]);
11130cecf9d5Sandi                    }
11140cecf9d5Sandi
11150cecf9d5Sandi                    $quoteDepth = $quoteLength;
11160cecf9d5Sandi
11170cecf9d5Sandi                break;
11180cecf9d5Sandi
11190cecf9d5Sandi                case 'quote_end':
11200cecf9d5Sandi
11210cecf9d5Sandi                    if ( $quoteDepth > 1 ) {
11220cecf9d5Sandi                        $quoteDiff = $quoteDepth - 1;
11230cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11240cecf9d5Sandi                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11250cecf9d5Sandi                        }
11260cecf9d5Sandi                    }
11270cecf9d5Sandi
11280cecf9d5Sandi                    $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11290cecf9d5Sandi
11300cecf9d5Sandi                    $this->CallWriter->writeCalls($this->quoteCalls);
11310cecf9d5Sandi                break;
11320cecf9d5Sandi
11330cecf9d5Sandi                default:
11340cecf9d5Sandi                    $this->quoteCalls[] = $call;
11350cecf9d5Sandi                break;
11360cecf9d5Sandi            }
11370cecf9d5Sandi        }
11380cecf9d5Sandi    }
11390cecf9d5Sandi
11400cecf9d5Sandi    function getDepth($marker) {
11410cecf9d5Sandi        preg_match('/>{1,}/', $marker, $matches);
11420cecf9d5Sandi        $quoteLength = strlen($matches[0]);
11430cecf9d5Sandi        return $quoteLength;
11440cecf9d5Sandi    }
11450cecf9d5Sandi}
11460cecf9d5Sandi
11470cecf9d5Sandi//------------------------------------------------------------------------
11480cecf9d5Sandiclass Doku_Handler_Table {
11490cecf9d5Sandi
11500cecf9d5Sandi    var $CallWriter;
11510cecf9d5Sandi
11520cecf9d5Sandi    var $calls = array();
11530cecf9d5Sandi    var $tableCalls = array();
11540cecf9d5Sandi    var $maxCols = 0;
11550cecf9d5Sandi    var $maxRows = 1;
11560cecf9d5Sandi    var $currentCols = 0;
115744881bd0Shenning.noren    var $firstCell = false;
11580cecf9d5Sandi    var $lastCellType = 'tablecell';
11590cecf9d5Sandi
11600cecf9d5Sandi    function Doku_Handler_Table(& $CallWriter) {
11610cecf9d5Sandi        $this->CallWriter = & $CallWriter;
11620cecf9d5Sandi    }
11630cecf9d5Sandi
11640cecf9d5Sandi    function writeCall($call) {
11650cecf9d5Sandi        $this->calls[] = $call;
11660cecf9d5Sandi    }
11670cecf9d5Sandi
11680cecf9d5Sandi    // Probably not needed but just in case...
11690cecf9d5Sandi    function writeCalls($calls) {
11700cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1171f4f02a0fSchris#        $this->CallWriter->writeCalls($this->calls);
1172f4f02a0fSchris    }
1173f4f02a0fSchris
1174f4f02a0fSchris    function finalise() {
1175f4f02a0fSchris        $last_call = end($this->calls);
1176f4f02a0fSchris        $this->writeCall(array('table_end',array(), $last_call[2]));
1177f4f02a0fSchris
1178f4f02a0fSchris        $this->process();
1179f4f02a0fSchris        $this->CallWriter->finalise();
11800cecf9d5Sandi    }
11810cecf9d5Sandi
11820cecf9d5Sandi    //------------------------------------------------------------------------
11830cecf9d5Sandi    function process() {
11840cecf9d5Sandi        foreach ( $this->calls as $call ) {
11850cecf9d5Sandi            switch ( $call[0] ) {
11860cecf9d5Sandi                case 'table_start':
11870cecf9d5Sandi                    $this->tableStart($call);
11880cecf9d5Sandi                break;
11890cecf9d5Sandi                case 'table_row':
11900cecf9d5Sandi                    $this->tableRowClose(array('tablerow_close',$call[1],$call[2]));
11910cecf9d5Sandi                    $this->tableRowOpen(array('tablerow_open',$call[1],$call[2]));
11920cecf9d5Sandi                break;
11930cecf9d5Sandi                case 'tableheader':
11940cecf9d5Sandi                case 'tablecell':
11950cecf9d5Sandi                    $this->tableCell($call);
11960cecf9d5Sandi                break;
11970cecf9d5Sandi                case 'table_end':
11980cecf9d5Sandi                    $this->tableRowClose(array('tablerow_close',$call[1],$call[2]));
11990cecf9d5Sandi                    $this->tableEnd($call);
12000cecf9d5Sandi                break;
12010cecf9d5Sandi                default:
12020cecf9d5Sandi                    $this->tableDefault($call);
12030cecf9d5Sandi                break;
12040cecf9d5Sandi            }
12050cecf9d5Sandi        }
12060cecf9d5Sandi        $this->CallWriter->writeCalls($this->tableCalls);
12070cecf9d5Sandi    }
12080cecf9d5Sandi
12090cecf9d5Sandi    function tableStart($call) {
12100cecf9d5Sandi        $this->tableCalls[] = array('table_open',array(),$call[2]);
12110cecf9d5Sandi        $this->tableCalls[] = array('tablerow_open',array(),$call[2]);
121244881bd0Shenning.noren        $this->firstCell = true;
12130cecf9d5Sandi    }
12140cecf9d5Sandi
12150cecf9d5Sandi    function tableEnd($call) {
12160cecf9d5Sandi        $this->tableCalls[] = array('table_close',array(),$call[2]);
12170cecf9d5Sandi        $this->finalizeTable();
12180cecf9d5Sandi    }
12190cecf9d5Sandi
12200cecf9d5Sandi    function tableRowOpen($call) {
12210cecf9d5Sandi        $this->tableCalls[] = $call;
12220cecf9d5Sandi        $this->currentCols = 0;
122344881bd0Shenning.noren        $this->firstCell = true;
12240cecf9d5Sandi        $this->lastCellType = 'tablecell';
12250cecf9d5Sandi        $this->maxRows++;
12260cecf9d5Sandi    }
12270cecf9d5Sandi
12280cecf9d5Sandi    function tableRowClose($call) {
12290cecf9d5Sandi        // Strip off final cell opening and anything after it
12300cecf9d5Sandi        while ( $discard = array_pop($this->tableCalls ) ) {
12310cecf9d5Sandi
12320cecf9d5Sandi            if ( $discard[0] == 'tablecell_open' || $discard[0] == 'tableheader_open') {
12330cecf9d5Sandi
12340cecf9d5Sandi                // Its a spanning element - put it back and close it
12350cecf9d5Sandi                if ( $discard[1][0] > 1 ) {
12360cecf9d5Sandi
12370cecf9d5Sandi                    $this->tableCalls[] = $discard;
12380cecf9d5Sandi                    if ( strstr($discard[0],'cell') ) {
12390cecf9d5Sandi                        $name = 'tablecell';
12400cecf9d5Sandi                    } else {
12410cecf9d5Sandi                        $name = 'tableheader';
12420cecf9d5Sandi                    }
12430cecf9d5Sandi                    $this->tableCalls[] = array($name.'_close',array(),$call[2]);
12440cecf9d5Sandi                }
12450cecf9d5Sandi
12460cecf9d5Sandi                break;
12470cecf9d5Sandi            }
12480cecf9d5Sandi        }
12490cecf9d5Sandi        $this->tableCalls[] = $call;
12500cecf9d5Sandi
12510cecf9d5Sandi        if ( $this->currentCols > $this->maxCols ) {
12520cecf9d5Sandi            $this->maxCols = $this->currentCols;
12530cecf9d5Sandi        }
12540cecf9d5Sandi    }
12550cecf9d5Sandi
12560cecf9d5Sandi    function tableCell($call) {
12570cecf9d5Sandi        if ( !$this->firstCell ) {
12580cecf9d5Sandi
12590cecf9d5Sandi            // Increase the span
12600cecf9d5Sandi            $lastCall = end($this->tableCalls);
12610cecf9d5Sandi
12620cecf9d5Sandi            // A cell call which follows an open cell means an empty cell so span
12630cecf9d5Sandi            if ( $lastCall[0] == 'tablecell_open' || $lastCall[0] == 'tableheader_open' ) {
12640cecf9d5Sandi                 $this->tableCalls[] = array('colspan',array(),$call[2]);
12650cecf9d5Sandi
12660cecf9d5Sandi            }
12670cecf9d5Sandi
12680cecf9d5Sandi            $this->tableCalls[] = array($this->lastCellType.'_close',array(),$call[2]);
12690cecf9d5Sandi            $this->tableCalls[] = array($call[0].'_open',array(1,NULL),$call[2]);
12700cecf9d5Sandi            $this->lastCellType = $call[0];
12710cecf9d5Sandi
12720cecf9d5Sandi        } else {
12730cecf9d5Sandi
12740cecf9d5Sandi            $this->tableCalls[] = array($call[0].'_open',array(1,NULL),$call[2]);
12750cecf9d5Sandi            $this->lastCellType = $call[0];
127644881bd0Shenning.noren            $this->firstCell = false;
12770cecf9d5Sandi
12780cecf9d5Sandi        }
12790cecf9d5Sandi
12800cecf9d5Sandi        $this->currentCols++;
12810cecf9d5Sandi    }
12820cecf9d5Sandi
12830cecf9d5Sandi    function tableDefault($call) {
12840cecf9d5Sandi        $this->tableCalls[] = $call;
12850cecf9d5Sandi    }
12860cecf9d5Sandi
12870cecf9d5Sandi    function finalizeTable() {
12880cecf9d5Sandi
12890cecf9d5Sandi        // Add the max cols and rows to the table opening
12900cecf9d5Sandi        if ( $this->tableCalls[0][0] == 'table_open' ) {
12910cecf9d5Sandi            // Adjust to num cols not num col delimeters
12920cecf9d5Sandi            $this->tableCalls[0][1][] = $this->maxCols - 1;
12930cecf9d5Sandi            $this->tableCalls[0][1][] = $this->maxRows;
12940cecf9d5Sandi        } else {
12950cecf9d5Sandi            trigger_error('First element in table call list is not table_open');
12960cecf9d5Sandi        }
12970cecf9d5Sandi
12980cecf9d5Sandi        $lastRow = 0;
12990cecf9d5Sandi        $lastCell = 0;
13000cecf9d5Sandi        $toDelete = array();
13010cecf9d5Sandi
13020cecf9d5Sandi        // Look for the colspan elements and increment the colspan on the
13030cecf9d5Sandi        // previous non-empty opening cell. Once done, delete all the cells
13040cecf9d5Sandi        // that contain colspans
13050cecf9d5Sandi        foreach ( $this->tableCalls as $key => $call ) {
13060cecf9d5Sandi
13070cecf9d5Sandi            if ( $call[0] == 'tablerow_open' ) {
13080cecf9d5Sandi
13090cecf9d5Sandi                $lastRow = $key;
13100cecf9d5Sandi
13110cecf9d5Sandi            } else if ( $call[0] == 'tablecell_open' || $call[0] == 'tableheader_open' ) {
13120cecf9d5Sandi
13130cecf9d5Sandi                $lastCell = $key;
13140cecf9d5Sandi
13150cecf9d5Sandi            } else if ( $call[0] == 'table_align' ) {
13160cecf9d5Sandi
13170cecf9d5Sandi                // If the previous element was a cell open, align right
13180cecf9d5Sandi                if ( $this->tableCalls[$key-1][0] == 'tablecell_open' || $this->tableCalls[$key-1][0] == 'tableheader_open' ) {
13190cecf9d5Sandi                    $this->tableCalls[$key-1][1][1] = 'right';
13200cecf9d5Sandi
13210cecf9d5Sandi                // If the next element if the close of an element, align either center or left
13220cecf9d5Sandi                } else if ( $this->tableCalls[$key+1][0] == 'tablecell_close' || $this->tableCalls[$key+1][0] == 'tableheader_close' ) {
13230cecf9d5Sandi                    if ( $this->tableCalls[$lastCell][1][1] == 'right' ) {
13240cecf9d5Sandi                        $this->tableCalls[$lastCell][1][1] = 'center';
13250cecf9d5Sandi                    } else {
13260cecf9d5Sandi                        $this->tableCalls[$lastCell][1][1] = 'left';
13270cecf9d5Sandi                    }
13280cecf9d5Sandi
13290cecf9d5Sandi                }
13300cecf9d5Sandi
13310cecf9d5Sandi                // Now convert the whitespace back to cdata
13320cecf9d5Sandi                $this->tableCalls[$key][0] = 'cdata';
13330cecf9d5Sandi
13340cecf9d5Sandi            } else if ( $call[0] == 'colspan' ) {
13350cecf9d5Sandi
133644881bd0Shenning.noren                $this->tableCalls[$key-1][1][0] = false;
13370cecf9d5Sandi
13380cecf9d5Sandi                for($i = $key-2; $i > $lastRow; $i--) {
13390cecf9d5Sandi
13400cecf9d5Sandi                    if ( $this->tableCalls[$i][0] == 'tablecell_open' || $this->tableCalls[$i][0] == 'tableheader_open' ) {
13410cecf9d5Sandi
134244881bd0Shenning.noren                        if ( false !== $this->tableCalls[$i][1][0] ) {
13430cecf9d5Sandi                            $this->tableCalls[$i][1][0]++;
13440cecf9d5Sandi                            break;
13450cecf9d5Sandi                        }
13460cecf9d5Sandi
13470cecf9d5Sandi
13480cecf9d5Sandi                    }
13490cecf9d5Sandi                }
13500cecf9d5Sandi
13510cecf9d5Sandi                $toDelete[] = $key-1;
13520cecf9d5Sandi                $toDelete[] = $key;
13530cecf9d5Sandi                $toDelete[] = $key+1;
13540cecf9d5Sandi            }
13550cecf9d5Sandi        }
13560cecf9d5Sandi
13579ab75d9eSAndreas Gohr
13589ab75d9eSAndreas Gohr        // condense cdata
13599ab75d9eSAndreas Gohr        $cnt = count($this->tableCalls);
13609ab75d9eSAndreas Gohr        for( $key = 0; $key < $cnt; $key++){
13619ab75d9eSAndreas Gohr            if($this->tableCalls[$key][0] == 'cdata'){
13629ab75d9eSAndreas Gohr                $ckey = $key;
13639ab75d9eSAndreas Gohr                $key++;
13649ab75d9eSAndreas Gohr                while($this->tableCalls[$key][0] == 'cdata'){
13659ab75d9eSAndreas Gohr                    $this->tableCalls[$ckey][1][0] .= $this->tableCalls[$key][1][0];
13669ab75d9eSAndreas Gohr                    $toDelete[] = $key;
13679ab75d9eSAndreas Gohr                    $key++;
13689ab75d9eSAndreas Gohr                }
13699ab75d9eSAndreas Gohr                continue;
13709ab75d9eSAndreas Gohr            }
13719ab75d9eSAndreas Gohr        }
13729ab75d9eSAndreas Gohr
13730cecf9d5Sandi        foreach ( $toDelete as $delete ) {
13740cecf9d5Sandi            unset($this->tableCalls[$delete]);
13750cecf9d5Sandi        }
13760cecf9d5Sandi        $this->tableCalls = array_values($this->tableCalls);
13770cecf9d5Sandi    }
13780cecf9d5Sandi}
13790cecf9d5Sandi
13800cecf9d5Sandi//------------------------------------------------------------------------
13810cecf9d5Sandiclass Doku_Handler_Section {
13820cecf9d5Sandi
13830cecf9d5Sandi    function process($calls) {
13840cecf9d5Sandi
13850cecf9d5Sandi        $sectionCalls = array();
138644881bd0Shenning.noren        $inSection = false;
13870cecf9d5Sandi
13880cecf9d5Sandi        foreach ( $calls as $call ) {
13890cecf9d5Sandi
13900cecf9d5Sandi            if ( $call[0] == 'header' ) {
13910cecf9d5Sandi
13920cecf9d5Sandi                if ( $inSection ) {
13930cecf9d5Sandi                    $sectionCalls[] = array('section_close',array(), $call[2]);
13940cecf9d5Sandi                }
13950cecf9d5Sandi
13960cecf9d5Sandi                $sectionCalls[] = $call;
13970cecf9d5Sandi                $sectionCalls[] = array('section_open',array($call[1][1]), $call[2]);
139844881bd0Shenning.noren                $inSection = true;
13990cecf9d5Sandi
14000cecf9d5Sandi            } else {
1401e1c10e4dSchris
1402e1c10e4dSchris                if ($call[0] == 'section_open' )  {
140344881bd0Shenning.noren                    $inSection = true;
1404e1c10e4dSchris                } else if ($call[0] == 'section_open' ) {
140544881bd0Shenning.noren                    $inSection = false;
1406e1c10e4dSchris                }
14070cecf9d5Sandi                $sectionCalls[] = $call;
14080cecf9d5Sandi            }
14090cecf9d5Sandi        }
14100cecf9d5Sandi
14110cecf9d5Sandi        if ( $inSection ) {
14120cecf9d5Sandi            $sectionCalls[] = array('section_close',array(), $call[2]);
14130cecf9d5Sandi        }
14140cecf9d5Sandi
14150cecf9d5Sandi        return $sectionCalls;
14160cecf9d5Sandi    }
14170cecf9d5Sandi
14180cecf9d5Sandi}
14190cecf9d5Sandi
14202a27e99aSandi/**
14212a27e99aSandi * Handler for paragraphs
14222a27e99aSandi *
14230b7c14c2Sandi * @author Harry Fuecks <hfuecks@gmail.com>
14242a27e99aSandi */
14250cecf9d5Sandiclass Doku_Handler_Block {
14260cecf9d5Sandi
14270cecf9d5Sandi    var $calls = array();
14280cecf9d5Sandi
14290cecf9d5Sandi    var $blockStack = array();
14300cecf9d5Sandi
143144881bd0Shenning.noren    var $inParagraph = false;
143244881bd0Shenning.noren    var $atStart = true;
143358b56c06Sandi    var $skipEolKey = -1;
14340cecf9d5Sandi
1435af146da0Sandi    // Blocks these should not be inside paragraphs
14360cecf9d5Sandi    var $blockOpen = array(
14370cecf9d5Sandi            'header',
1438df9add72Schris            'listu_open','listo_open','listitem_open','listcontent_open',
14390cecf9d5Sandi            'table_open','tablerow_open','tablecell_open','tableheader_open',
14400cecf9d5Sandi            'quote_open',
14410cecf9d5Sandi            'section_open', // Needed to prevent p_open between header and section_open
144276aa94b7Schris            'code','file','hr','preformatted','rss',
144307f89c3cSAnika Henke            'htmlblock','phpblock',
14440cecf9d5Sandi        );
14450cecf9d5Sandi
14460cecf9d5Sandi    var $blockClose = array(
14470cecf9d5Sandi            'header',
1448df9add72Schris            'listu_close','listo_close','listitem_close','listcontent_close',
14490cecf9d5Sandi            'table_close','tablerow_close','tablecell_close','tableheader_close',
14500cecf9d5Sandi            'quote_close',
14510cecf9d5Sandi            'section_close', // Needed to prevent p_close after section_close
145276aa94b7Schris            'code','file','hr','preformatted','rss',
145307f89c3cSAnika Henke            'htmlblock','phpblock',
14540cecf9d5Sandi        );
14550cecf9d5Sandi
1456af146da0Sandi    // Stacks can contain paragraphs
14570cecf9d5Sandi    var $stackOpen = array(
14580cecf9d5Sandi        'footnote_open','section_open',
14590cecf9d5Sandi        );
14600cecf9d5Sandi
14610cecf9d5Sandi    var $stackClose = array(
14620cecf9d5Sandi        'footnote_close','section_close',
14630cecf9d5Sandi        );
14640cecf9d5Sandi
1465af146da0Sandi
1466af146da0Sandi    /**
1467af146da0Sandi     * Constructor. Adds loaded syntax plugins to the block and stack
1468af146da0Sandi     * arrays
1469af146da0Sandi     *
1470af146da0Sandi     * @author Andreas Gohr <andi@splitbrain.org>
1471af146da0Sandi     */
1472af146da0Sandi    function Doku_Handler_Block(){
1473af146da0Sandi        global $DOKU_PLUGINS;
1474af146da0Sandi        //check if syntax plugins were loaded
147503c4aec3Schris        if(empty($DOKU_PLUGINS['syntax'])) return;
1476af146da0Sandi        foreach($DOKU_PLUGINS['syntax'] as $n => $p){
1477af146da0Sandi            $ptype = $p->getPType();
1478af146da0Sandi            if($ptype == 'block'){
1479af146da0Sandi                $this->blockOpen[]  = 'plugin_'.$n;
1480af146da0Sandi                $this->blockClose[] = 'plugin_'.$n;
1481af146da0Sandi            }elseif($ptype == 'stack'){
1482af146da0Sandi                $this->stackOpen[]  = 'plugin_'.$n;
1483af146da0Sandi                $this->stackClose[] = 'plugin_'.$n;
1484af146da0Sandi            }
1485af146da0Sandi        }
1486af146da0Sandi    }
1487af146da0Sandi
14882a27e99aSandi    /**
14892a27e99aSandi     * Close a paragraph if needed
14902a27e99aSandi     *
14912a27e99aSandi     * This function makes sure there are no empty paragraphs on the stack
14922a27e99aSandi     *
14932a27e99aSandi     * @author Andreas Gohr <andi@splitbrain.org>
14942a27e99aSandi     */
1495506ae684Sandi    function closeParagraph($pos){
1496506ae684Sandi        // look back if there was any content - we don't want empty paragraphs
1497506ae684Sandi        $content = '';
1498506ae684Sandi        for($i=count($this->calls)-1; $i>=0; $i--){
1499506ae684Sandi            if($this->calls[$i][0] == 'p_open'){
1500506ae684Sandi                break;
1501506ae684Sandi            }elseif($this->calls[$i][0] == 'cdata'){
1502506ae684Sandi                $content .= $this->calls[$i][1][0];
1503506ae684Sandi            }else{
1504506ae684Sandi                $content = 'found markup';
1505506ae684Sandi                break;
1506506ae684Sandi            }
1507506ae684Sandi        }
1508506ae684Sandi
1509506ae684Sandi        if(trim($content)==''){
1510506ae684Sandi            //remove the whole paragraph
1511506ae684Sandi            array_splice($this->calls,$i);
1512506ae684Sandi        }else{
151329d015e3SBen Coburn            if ($this->calls[count($this->calls)-1][0] == 'section_edit') {
151429d015e3SBen Coburn                $tmp = array_pop($this->calls);
1515506ae684Sandi                $this->calls[] = array('p_close',array(), $pos);
151629d015e3SBen Coburn                $this->calls[] = $tmp;
151729d015e3SBen Coburn            } else {
151829d015e3SBen Coburn                $this->calls[] = array('p_close',array(), $pos);
151929d015e3SBen Coburn            }
1520506ae684Sandi        }
1521e1c10e4dSchris
152244881bd0Shenning.noren        $this->inParagraph = false;
1523506ae684Sandi    }
1524506ae684Sandi
15252a27e99aSandi    /**
15262a27e99aSandi     * Processes the whole instruction stack to open and close paragraphs
15272a27e99aSandi     *
15280b7c14c2Sandi     * @author Harry Fuecks <hfuecks@gmail.com>
15292a27e99aSandi     * @author Andreas Gohr <andi@splitbrain.org>
15302a27e99aSandi     * @todo   This thing is really messy and should be rewritten
15312a27e99aSandi     */
15320cecf9d5Sandi    function process($calls) {
15330cecf9d5Sandi        foreach ( $calls as $key => $call ) {
1534f0891737Sandi            $cname = $call[0];
1535e1c10e4dSchris            if($cname == 'plugin') {
1536e1c10e4dSchris                $cname='plugin_'.$call[1][0];
1537e1c10e4dSchris
1538e1c10e4dSchris                $plugin = true;
1539e1c10e4dSchris                $plugin_open = (($call[1][2] == DOKU_LEXER_ENTER) || ($call[1][2] == DOKU_LEXER_SPECIAL));
1540e1c10e4dSchris                $plugin_close = (($call[1][2] == DOKU_LEXER_EXIT) || ($call[1][2] == DOKU_LEXER_SPECIAL));
1541e1c10e4dSchris            } else {
1542e1c10e4dSchris                $plugin = false;
1543e1c10e4dSchris            }
15440cecf9d5Sandi
15450cecf9d5Sandi            // Process blocks which are stack like... (contain linefeeds)
1546e1c10e4dSchris            if ( in_array($cname,$this->stackOpen ) && (!$plugin || $plugin_open) ) {
1547e1c10e4dSchris
15480cecf9d5Sandi                $this->calls[] = $call;
15490cecf9d5Sandi
15500cecf9d5Sandi                // Hack - footnotes shouldn't immediately contain a p_open
1551f0891737Sandi                if ( $cname != 'footnote_open' ) {
15520cecf9d5Sandi                    $this->addToStack();
15530cecf9d5Sandi                } else {
155444881bd0Shenning.noren                    $this->addToStack(false);
15550cecf9d5Sandi                }
15560cecf9d5Sandi                continue;
15570cecf9d5Sandi            }
15580cecf9d5Sandi
1559e1c10e4dSchris            if ( in_array($cname,$this->stackClose ) && (!$plugin || $plugin_close)) {
15600cecf9d5Sandi
15610cecf9d5Sandi                if ( $this->inParagraph ) {
1562506ae684Sandi                    $this->closeParagraph($call[2]);
15630cecf9d5Sandi                }
15640cecf9d5Sandi                $this->calls[] = $call;
15650cecf9d5Sandi                $this->removeFromStack();
15660cecf9d5Sandi                continue;
15670cecf9d5Sandi            }
15680cecf9d5Sandi
15690cecf9d5Sandi            if ( !$this->atStart ) {
15700cecf9d5Sandi
1571f0891737Sandi                if ( $cname == 'eol' ) {
15720cecf9d5Sandi
1573e1c10e4dSchris                    // Check this isn't an eol instruction to skip...
157458b56c06Sandi                    if ( $this->skipEolKey != $key ) {
1575e1c10e4dSchris                        // Look to see if the next instruction is an EOL
157658b56c06Sandi                        if ( isset($calls[$key+1]) && $calls[$key+1][0] == 'eol' ) {
157758b56c06Sandi
157858b56c06Sandi                            if ( $this->inParagraph ) {
1579506ae684Sandi                                //$this->calls[] = array('p_close',array(), $call[2]);
1580506ae684Sandi                                $this->closeParagraph($call[2]);
158158b56c06Sandi                            }
158258b56c06Sandi
158358b56c06Sandi                            $this->calls[] = array('p_open',array(), $call[2]);
158444881bd0Shenning.noren                            $this->inParagraph = true;
158558b56c06Sandi
158658b56c06Sandi
1587e1c10e4dSchris                            // Mark the next instruction for skipping
158858b56c06Sandi                            $this->skipEolKey = $key+1;
158958b56c06Sandi
159058b56c06Sandi                        }else{
159158b56c06Sandi                            //if this is just a single eol make a space from it
159258b56c06Sandi                            $this->calls[] = array('cdata',array(" "), $call[2]);
159358b56c06Sandi                        }
159458b56c06Sandi                    }
159558b56c06Sandi
15960cecf9d5Sandi
15970cecf9d5Sandi                } else {
15980cecf9d5Sandi
159944881bd0Shenning.noren                    $storeCall = true;
1600e1c10e4dSchris                    if ( $this->inParagraph && (in_array($cname, $this->blockOpen) && (!$plugin || $plugin_open))) {
1601506ae684Sandi                        $this->closeParagraph($call[2]);
16020cecf9d5Sandi                        $this->calls[] = $call;
160344881bd0Shenning.noren                        $storeCall = false;
16040cecf9d5Sandi                    }
16050cecf9d5Sandi
1606e1c10e4dSchris                    if ( in_array($cname, $this->blockClose) && (!$plugin || $plugin_close)) {
16070cecf9d5Sandi                        if ( $this->inParagraph ) {
1608506ae684Sandi                            $this->closeParagraph($call[2]);
16090cecf9d5Sandi                        }
16100cecf9d5Sandi                        if ( $storeCall ) {
16110cecf9d5Sandi                            $this->calls[] = $call;
161244881bd0Shenning.noren                            $storeCall = false;
16130cecf9d5Sandi                        }
16140cecf9d5Sandi
16150cecf9d5Sandi                        // This really sucks and suggests this whole class sucks but...
1616e1c10e4dSchris                        if ( isset($calls[$key+1])) {
1617e1c10e4dSchris                            $cname_plusone = $calls[$key+1][0];
1618e1c10e4dSchris                            if ($cname_plusone == 'plugin') {
1619e1c10e4dSchris                                $cname_plusone = 'plugin'.$calls[$key+1][1][0];
1620e1c10e4dSchris
1621e1c10e4dSchris                                // plugin test, true if plugin has a state which precludes it requiring blockOpen or blockClose
1622e1c10e4dSchris                                $plugin_plusone = true;
1623e1c10e4dSchris                                $plugin_test = ($call[$key+1][1][2] == DOKU_LEXER_MATCHED) || ($call[$key+1][1][2] == DOKU_LEXER_MATCHED);
1624e1c10e4dSchris                            } else {
1625e1c10e4dSchris                                $plugin_plusone = false;
1626e1c10e4dSchris                            }
1627e1c10e4dSchris                            if ((!in_array($cname_plusone, $this->blockOpen) && !in_array($cname_plusone, $this->blockClose)) ||
1628e1c10e4dSchris                                ($plugin_plusone && $plugin_test)
16290cecf9d5Sandi                                ) {
16300cecf9d5Sandi
16310cecf9d5Sandi                                $this->calls[] = array('p_open',array(), $call[2]);
163244881bd0Shenning.noren                                $this->inParagraph = true;
16330cecf9d5Sandi                            }
16340cecf9d5Sandi                        }
1635e1c10e4dSchris                    }
16360cecf9d5Sandi
16370cecf9d5Sandi                    if ( $storeCall ) {
16380cecf9d5Sandi                        $this->calls[] = $call;
16390cecf9d5Sandi                    }
16400cecf9d5Sandi
16410cecf9d5Sandi                }
16420cecf9d5Sandi
16430cecf9d5Sandi
16440cecf9d5Sandi            } else {
16450cecf9d5Sandi
16460cecf9d5Sandi                // Unless there's already a block at the start, start a paragraph
1647f0891737Sandi                if ( !in_array($cname,$this->blockOpen) ) {
16480cecf9d5Sandi                    $this->calls[] = array('p_open',array(), $call[2]);
16490cecf9d5Sandi                    if ( $call[0] != 'eol' ) {
16500cecf9d5Sandi                        $this->calls[] = $call;
16510cecf9d5Sandi                    }
165244881bd0Shenning.noren                    $this->atStart = false;
165344881bd0Shenning.noren                    $this->inParagraph = true;
16540cecf9d5Sandi                } else {
16550cecf9d5Sandi                    $this->calls[] = $call;
165644881bd0Shenning.noren                    $this->atStart = false;
16570cecf9d5Sandi                }
16580cecf9d5Sandi
16590cecf9d5Sandi            }
16600cecf9d5Sandi
16610cecf9d5Sandi        }
16620cecf9d5Sandi
16630cecf9d5Sandi        if ( $this->inParagraph ) {
1664f0891737Sandi            if ( $cname == 'p_open' ) {
16650cecf9d5Sandi                // Ditch the last call
16660cecf9d5Sandi                array_pop($this->calls);
1667f0891737Sandi            } else if ( !in_array($cname, $this->blockClose) ) {
1668506ae684Sandi                //$this->calls[] = array('p_close',array(), $call[2]);
1669506ae684Sandi                $this->closeParagraph($call[2]);
16700cecf9d5Sandi            } else {
16710cecf9d5Sandi                $last_call = array_pop($this->calls);
1672506ae684Sandi                //$this->calls[] = array('p_close',array(), $call[2]);
1673506ae684Sandi                $this->closeParagraph($call[2]);
16740cecf9d5Sandi                $this->calls[] = $last_call;
16750cecf9d5Sandi            }
16760cecf9d5Sandi        }
16770cecf9d5Sandi
16780cecf9d5Sandi        return $this->calls;
16790cecf9d5Sandi    }
16800cecf9d5Sandi
168144881bd0Shenning.noren    function addToStack($newStart = true) {
16820cecf9d5Sandi        $this->blockStack[] = array($this->atStart, $this->inParagraph);
16830cecf9d5Sandi        $this->atStart = $newStart;
168444881bd0Shenning.noren        $this->inParagraph = false;
16850cecf9d5Sandi    }
16860cecf9d5Sandi
16870cecf9d5Sandi    function removeFromStack() {
16880cecf9d5Sandi        $state = array_pop($this->blockStack);
16890cecf9d5Sandi        $this->atStart = $state[0];
16900cecf9d5Sandi        $this->inParagraph = $state[1];
16910cecf9d5Sandi    }
16920cecf9d5Sandi}
16932a27e99aSandi
16944826ab45Sandi//Setup VIM: ex: et ts=4 enc=utf-8 :
1695