xref: /dokuwiki/inc/parser/handler.php (revision 82d616353e4c3680d88f083eb6f88fe68de92904)
10cecf9d5Sandi<?php
200976812SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../../').'/');
30cecf9d5Sandi
452fe2bfbSChris Smithif (!defined('DOKU_PARSER_EOL')) define('DOKU_PARSER_EOL',"\n");   // add this to make handling test cases simpler
5a9c1d2d2SChris Smith
60cecf9d5Sandiclass Doku_Handler {
70cecf9d5Sandi
80cecf9d5Sandi    var $Renderer = NULL;
90cecf9d5Sandi
100cecf9d5Sandi    var $CallWriter = NULL;
110cecf9d5Sandi
120cecf9d5Sandi    var $calls = array();
130cecf9d5Sandi
14e1c10e4dSchris    var $status = array(
1544881bd0Shenning.noren        'section' => false,
1635dae8b0SBen Coburn        'section_edit_start' => -1,
1735dae8b0SBen Coburn        'section_edit_level' => 1,
1835dae8b0SBen Coburn        'section_edit_title' => ''
190cecf9d5Sandi    );
200cecf9d5Sandi
2144881bd0Shenning.noren    var $rewriteBlocks = true;
22b7c441b9SHarry Fuecks
230cecf9d5Sandi    function Doku_Handler() {
240cecf9d5Sandi        $this->CallWriter = & new Doku_Handler_CallWriter($this);
250cecf9d5Sandi    }
260cecf9d5Sandi
27433bef32Sandi    function _addCall($handler, $args, $pos) {
280cecf9d5Sandi        $call = array($handler,$args, $pos);
290cecf9d5Sandi        $this->CallWriter->writeCall($call);
300cecf9d5Sandi    }
310cecf9d5Sandi
32*82d61635Spierre.spring    function addPluginCall($plugin, $args, $state, $pos, $match) {
33*82d61635Spierre.spring        $call = array('plugin',array($plugin, $args, $state, $match), $pos);
3404ebd214Schris        $this->CallWriter->writeCall($call);
3504ebd214Schris    }
3604ebd214Schris
37433bef32Sandi    function _finalize(){
38e1c10e4dSchris
39f4f02a0fSchris        $this->CallWriter->finalise();
40f4f02a0fSchris
41e1c10e4dSchris        if ( $this->status['section'] ) {
42e1c10e4dSchris           $last_call = end($this->calls);
43e1c10e4dSchris           array_push($this->calls,array('section_close',array(), $last_call[2]));
44b203781fSBen Coburn           if ($this->status['section_edit_start']>1) {
45b203781fSBen Coburn               // ignore last edit section if there is only one header
46b203781fSBen 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]));
47b203781fSBen Coburn           }
480cecf9d5Sandi        }
490cecf9d5Sandi
50b7c441b9SHarry Fuecks        if ( $this->rewriteBlocks ) {
510cecf9d5Sandi            $B = & new Doku_Handler_Block();
520cecf9d5Sandi            $this->calls = $B->process($this->calls);
53b7c441b9SHarry Fuecks        }
54e0ad864eSchris
5524bb549bSchris        trigger_event('PARSER_HANDLER_DONE',$this);
560cecf9d5Sandi
570cecf9d5Sandi        array_unshift($this->calls,array('document_start',array(),0));
580cecf9d5Sandi        $last_call = end($this->calls);
590cecf9d5Sandi        array_push($this->calls,array('document_end',array(),$last_call[2]));
600cecf9d5Sandi    }
610cecf9d5Sandi
620cecf9d5Sandi    function fetch() {
630cecf9d5Sandi        $call = each($this->calls);
640cecf9d5Sandi        if ( $call ) {
650cecf9d5Sandi            return $call['value'];
660cecf9d5Sandi        }
6744881bd0Shenning.noren        return false;
680cecf9d5Sandi    }
69ee20e7d1Sandi
70ee20e7d1Sandi
71ee20e7d1Sandi    /**
72ee20e7d1Sandi     * Special plugin handler
73ee20e7d1Sandi     *
74ee20e7d1Sandi     * This handler is called for all modes starting with 'plugin_'.
75ee20e7d1Sandi     * An additional parameter with the plugin name is passed
76ee20e7d1Sandi     *
77ee20e7d1Sandi     * @author Andreas Gohr <andi@splitbrain.org>
78ee20e7d1Sandi     */
79ee20e7d1Sandi    function plugin($match, $state, $pos, $pluginname){
80ee20e7d1Sandi        $data = array($match);
81a46d0d65SAndreas Gohr        $plugin =& plugin_load('syntax',$pluginname);
82a46d0d65SAndreas Gohr        if($plugin != null){
83f02a7d06Schris            $data = $plugin->handle($match, $state, $pos, $this);
84ee20e7d1Sandi        }
8513ecfb18SChris Smith        if ($data !== false) {
86*82d61635Spierre.spring          $this->addPluginCall($pluginname,$data,$state,$pos,$match);
8713ecfb18SChris Smith        }
8844881bd0Shenning.noren        return true;
89ee20e7d1Sandi    }
900cecf9d5Sandi
910cecf9d5Sandi    function base($match, $state, $pos) {
920cecf9d5Sandi        switch ( $state ) {
930cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
94433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
9544881bd0Shenning.noren                return true;
960cecf9d5Sandi            break;
970cecf9d5Sandi        }
980cecf9d5Sandi    }
990cecf9d5Sandi
1000cecf9d5Sandi    function header($match, $state, $pos) {
101b203781fSBen Coburn        global $conf;
102b203781fSBen Coburn
103d7e8115fSAndreas Gohr        // get level and title
104a4a2d4cfSAndreas Gohr        $title = trim($match);
105a4a2d4cfSAndreas Gohr        $level = 7 - strspn($title,'=');
106d7e8115fSAndreas Gohr        if($level < 1) $level = 1;
107a4a2d4cfSAndreas Gohr        $title = trim($title,'=');
108a4a2d4cfSAndreas Gohr        $title = trim($title);
1090cecf9d5Sandi
110e1c10e4dSchris        if ($this->status['section']) $this->_addCall('section_close',array(),$pos);
111e1c10e4dSchris
112b203781fSBen Coburn        if ($level<=$conf['maxseclevel']) {
11335dae8b0SBen Coburn            $this->_addCall('section_edit',array($this->status['section_edit_start'], $pos-1, $this->status['section_edit_level'], $this->status['section_edit_title']), $pos);
11435dae8b0SBen Coburn            $this->status['section_edit_start'] = $pos;
11535dae8b0SBen Coburn            $this->status['section_edit_level'] = $level;
11635dae8b0SBen Coburn            $this->status['section_edit_title'] = $title;
117b203781fSBen Coburn        }
11835dae8b0SBen Coburn
119433bef32Sandi        $this->_addCall('header',array($title,$level,$pos), $pos);
120e1c10e4dSchris
121e1c10e4dSchris        $this->_addCall('section_open',array($level),$pos);
12244881bd0Shenning.noren        $this->status['section'] = true;
12344881bd0Shenning.noren        return true;
1240cecf9d5Sandi    }
1250cecf9d5Sandi
1260cecf9d5Sandi    function notoc($match, $state, $pos) {
127e41c4da9SAndreas Gohr        $this->_addCall('notoc',array(),$pos);
12844881bd0Shenning.noren        return true;
1290cecf9d5Sandi    }
1300cecf9d5Sandi
1319dc2c2afSandi    function nocache($match, $state, $pos) {
1329dc2c2afSandi        $this->_addCall('nocache',array(),$pos);
13344881bd0Shenning.noren        return true;
1349dc2c2afSandi    }
1359dc2c2afSandi
1360cecf9d5Sandi    function linebreak($match, $state, $pos) {
137433bef32Sandi        $this->_addCall('linebreak',array(),$pos);
13844881bd0Shenning.noren        return true;
1390cecf9d5Sandi    }
1400cecf9d5Sandi
1410cecf9d5Sandi    function eol($match, $state, $pos) {
142433bef32Sandi        $this->_addCall('eol',array(),$pos);
14344881bd0Shenning.noren        return true;
1440cecf9d5Sandi    }
1450cecf9d5Sandi
1460cecf9d5Sandi    function hr($match, $state, $pos) {
147433bef32Sandi        $this->_addCall('hr',array(),$pos);
14844881bd0Shenning.noren        return true;
1490cecf9d5Sandi    }
1500cecf9d5Sandi
151433bef32Sandi    function _nestingTag($match, $state, $pos, $name) {
1520cecf9d5Sandi        switch ( $state ) {
1530cecf9d5Sandi            case DOKU_LEXER_ENTER:
154433bef32Sandi                $this->_addCall($name.'_open', array(), $pos);
1550cecf9d5Sandi            break;
1560cecf9d5Sandi            case DOKU_LEXER_EXIT:
157433bef32Sandi                $this->_addCall($name.'_close', array(), $pos);
1580cecf9d5Sandi            break;
1590cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
160433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
1610cecf9d5Sandi            break;
1620cecf9d5Sandi        }
1630cecf9d5Sandi    }
1640cecf9d5Sandi
1650cecf9d5Sandi    function strong($match, $state, $pos) {
166433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'strong');
16744881bd0Shenning.noren        return true;
1680cecf9d5Sandi    }
1690cecf9d5Sandi
1700cecf9d5Sandi    function emphasis($match, $state, $pos) {
171433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'emphasis');
17244881bd0Shenning.noren        return true;
1730cecf9d5Sandi    }
1740cecf9d5Sandi
1750cecf9d5Sandi    function underline($match, $state, $pos) {
176433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'underline');
17744881bd0Shenning.noren        return true;
1780cecf9d5Sandi    }
1790cecf9d5Sandi
1800cecf9d5Sandi    function monospace($match, $state, $pos) {
181433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'monospace');
18244881bd0Shenning.noren        return true;
1830cecf9d5Sandi    }
1840cecf9d5Sandi
1850cecf9d5Sandi    function subscript($match, $state, $pos) {
186433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'subscript');
18744881bd0Shenning.noren        return true;
1880cecf9d5Sandi    }
1890cecf9d5Sandi
1900cecf9d5Sandi    function superscript($match, $state, $pos) {
191433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'superscript');
19244881bd0Shenning.noren        return true;
1930cecf9d5Sandi    }
1940cecf9d5Sandi
1950cecf9d5Sandi    function deleted($match, $state, $pos) {
196433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'deleted');
19744881bd0Shenning.noren        return true;
1980cecf9d5Sandi    }
1990cecf9d5Sandi
2000cecf9d5Sandi
2010cecf9d5Sandi    function footnote($match, $state, $pos) {
2025587e44cSchris//        $this->_nestingTag($match, $state, $pos, 'footnote');
203742c66f8Schris        if (!isset($this->_footnote)) $this->_footnote = false;
2042fe7363dSchris
2055587e44cSchris        switch ( $state ) {
2065587e44cSchris            case DOKU_LEXER_ENTER:
2072fe7363dSchris                // footnotes can not be nested - however due to limitations in lexer it can't be prevented
2082fe7363dSchris                // we will still enter a new footnote mode, we just do nothing
209742c66f8Schris                if ($this->_footnote) {
2102fe7363dSchris                  $this->_addCall('cdata',array($match), $pos);
2112fe7363dSchris                  break;
2122fe7363dSchris                }
2132fe7363dSchris
214742c66f8Schris                $this->_footnote = true;
2152fe7363dSchris
2165587e44cSchris                $ReWriter = & new Doku_Handler_Nest($this->CallWriter,'footnote_close');
2175587e44cSchris                $this->CallWriter = & $ReWriter;
2184a26ad85Schris                $this->_addCall('footnote_open', array(), $pos);
2195587e44cSchris            break;
2205587e44cSchris            case DOKU_LEXER_EXIT:
2212fe7363dSchris                // check whether we have already exitted the footnote mode, can happen if the modes were nested
222742c66f8Schris                if (!$this->_footnote) {
2232fe7363dSchris                  $this->_addCall('cdata',array($match), $pos);
2242fe7363dSchris                  break;
2252fe7363dSchris                }
2262fe7363dSchris
227742c66f8Schris                $this->_footnote = false;
2282fe7363dSchris
2295587e44cSchris                $this->_addCall('footnote_close', array(), $pos);
2305587e44cSchris                $this->CallWriter->process();
2315587e44cSchris                $ReWriter = & $this->CallWriter;
2325587e44cSchris                $this->CallWriter = & $ReWriter->CallWriter;
2335587e44cSchris            break;
2345587e44cSchris            case DOKU_LEXER_UNMATCHED:
2355587e44cSchris                $this->_addCall('cdata', array($match), $pos);
2365587e44cSchris            break;
2375587e44cSchris        }
23844881bd0Shenning.noren        return true;
2390cecf9d5Sandi    }
2400cecf9d5Sandi
2410cecf9d5Sandi    function listblock($match, $state, $pos) {
2420cecf9d5Sandi        switch ( $state ) {
2430cecf9d5Sandi            case DOKU_LEXER_ENTER:
2440cecf9d5Sandi                $ReWriter = & new Doku_Handler_List($this->CallWriter);
2450cecf9d5Sandi                $this->CallWriter = & $ReWriter;
246433bef32Sandi                $this->_addCall('list_open', array($match), $pos);
2470cecf9d5Sandi            break;
2480cecf9d5Sandi            case DOKU_LEXER_EXIT:
249433bef32Sandi                $this->_addCall('list_close', array(), $pos);
2500cecf9d5Sandi                $this->CallWriter->process();
2510cecf9d5Sandi                $ReWriter = & $this->CallWriter;
2520cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
2530cecf9d5Sandi            break;
2540cecf9d5Sandi            case DOKU_LEXER_MATCHED:
255433bef32Sandi                $this->_addCall('list_item', array($match), $pos);
2560cecf9d5Sandi            break;
2570cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
258433bef32Sandi                $this->_addCall('cdata', array($match), $pos);
2590cecf9d5Sandi            break;
2600cecf9d5Sandi        }
26144881bd0Shenning.noren        return true;
2620cecf9d5Sandi    }
2630cecf9d5Sandi
2640cecf9d5Sandi    function unformatted($match, $state, $pos) {
2650cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
266433bef32Sandi            $this->_addCall('unformatted',array($match), $pos);
2670cecf9d5Sandi        }
26844881bd0Shenning.noren        return true;
2690cecf9d5Sandi    }
2700cecf9d5Sandi
2710cecf9d5Sandi    function php($match, $state, $pos) {
272df9add72Schris        global $conf;
2730cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
274433bef32Sandi            $this->_addCall('php',array($match), $pos);
2750cecf9d5Sandi        }
27644881bd0Shenning.noren        return true;
2770cecf9d5Sandi    }
2780cecf9d5Sandi
27907f89c3cSAnika Henke    function phpblock($match, $state, $pos) {
28007f89c3cSAnika Henke        global $conf;
28107f89c3cSAnika Henke        if ( $state == DOKU_LEXER_UNMATCHED ) {
28207f89c3cSAnika Henke            $this->_addCall('phpblock',array($match), $pos);
28307f89c3cSAnika Henke        }
28407f89c3cSAnika Henke        return true;
28507f89c3cSAnika Henke    }
28607f89c3cSAnika Henke
2870cecf9d5Sandi    function html($match, $state, $pos) {
288df9add72Schris        global $conf;
2890cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
290433bef32Sandi            $this->_addCall('html',array($match), $pos);
2910cecf9d5Sandi        }
29244881bd0Shenning.noren        return true;
2930cecf9d5Sandi    }
2940cecf9d5Sandi
29507f89c3cSAnika Henke    function htmlblock($match, $state, $pos) {
29607f89c3cSAnika Henke        global $conf;
29707f89c3cSAnika Henke        if ( $state == DOKU_LEXER_UNMATCHED ) {
29807f89c3cSAnika Henke            $this->_addCall('htmlblock',array($match), $pos);
29907f89c3cSAnika Henke        }
30007f89c3cSAnika Henke        return true;
30107f89c3cSAnika Henke    }
30207f89c3cSAnika Henke
3030cecf9d5Sandi    function preformatted($match, $state, $pos) {
3040cecf9d5Sandi        switch ( $state ) {
3050cecf9d5Sandi            case DOKU_LEXER_ENTER:
3060cecf9d5Sandi                $ReWriter = & new Doku_Handler_Preformatted($this->CallWriter);
3070cecf9d5Sandi                $this->CallWriter = & $ReWriter;
308433bef32Sandi                $this->_addCall('preformatted_start',array(), $pos);
3090cecf9d5Sandi            break;
3100cecf9d5Sandi            case DOKU_LEXER_EXIT:
311433bef32Sandi                $this->_addCall('preformatted_end',array(), $pos);
3120cecf9d5Sandi                $this->CallWriter->process();
3130cecf9d5Sandi                $ReWriter = & $this->CallWriter;
3140cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
3150cecf9d5Sandi            break;
3160cecf9d5Sandi            case DOKU_LEXER_MATCHED:
317433bef32Sandi                $this->_addCall('preformatted_newline',array(), $pos);
3180cecf9d5Sandi            break;
3190cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
320433bef32Sandi                $this->_addCall('preformatted_content',array($match), $pos);
3210cecf9d5Sandi            break;
3220cecf9d5Sandi        }
3230cecf9d5Sandi
32444881bd0Shenning.noren        return true;
3250cecf9d5Sandi    }
3260cecf9d5Sandi
3270cecf9d5Sandi    function file($match, $state, $pos) {
3280cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
329433bef32Sandi            $this->_addCall('file',array($match), $pos);
3300cecf9d5Sandi        }
33144881bd0Shenning.noren        return true;
3320cecf9d5Sandi    }
3330cecf9d5Sandi
3340cecf9d5Sandi    function quote($match, $state, $pos) {
3350cecf9d5Sandi
3360cecf9d5Sandi        switch ( $state ) {
3370cecf9d5Sandi
3380cecf9d5Sandi            case DOKU_LEXER_ENTER:
3390cecf9d5Sandi                $ReWriter = & new Doku_Handler_Quote($this->CallWriter);
3400cecf9d5Sandi                $this->CallWriter = & $ReWriter;
341433bef32Sandi                $this->_addCall('quote_start',array($match), $pos);
3420cecf9d5Sandi            break;
3430cecf9d5Sandi
3440cecf9d5Sandi            case DOKU_LEXER_EXIT:
345433bef32Sandi                $this->_addCall('quote_end',array(), $pos);
3460cecf9d5Sandi                $this->CallWriter->process();
3470cecf9d5Sandi                $ReWriter = & $this->CallWriter;
3480cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
3490cecf9d5Sandi            break;
3500cecf9d5Sandi
3510cecf9d5Sandi            case DOKU_LEXER_MATCHED:
352433bef32Sandi                $this->_addCall('quote_newline',array($match), $pos);
3530cecf9d5Sandi            break;
3540cecf9d5Sandi
3550cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
356433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
3570cecf9d5Sandi            break;
3580cecf9d5Sandi
3590cecf9d5Sandi        }
3600cecf9d5Sandi
36144881bd0Shenning.noren        return true;
3620cecf9d5Sandi    }
3630cecf9d5Sandi
3640cecf9d5Sandi    function code($match, $state, $pos) {
3650cecf9d5Sandi        switch ( $state ) {
3660cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
3670cecf9d5Sandi                $matches = preg_split('/>/u',$match,2);
3680cecf9d5Sandi                $matches[0] = trim($matches[0]);
3690cecf9d5Sandi                if ( trim($matches[0]) == '' ) {
3700cecf9d5Sandi                    $matches[0] = NULL;
3710cecf9d5Sandi                }
3720cecf9d5Sandi                # $matches[0] contains name of programming language
373b5bef19bSandi                # if available, We shortcut html here.
374b5bef19bSandi                if($matches[0] == 'html') $matches[0] = 'html4strict';
375433bef32Sandi                $this->_addCall(
3760cecf9d5Sandi                        'code',
3770cecf9d5Sandi                        array($matches[1],$matches[0]),
3780cecf9d5Sandi                        $pos
3790cecf9d5Sandi                    );
3800cecf9d5Sandi            break;
3810cecf9d5Sandi        }
38244881bd0Shenning.noren        return true;
3830cecf9d5Sandi    }
3840cecf9d5Sandi
3850cecf9d5Sandi    function acronym($match, $state, $pos) {
386433bef32Sandi        $this->_addCall('acronym',array($match), $pos);
38744881bd0Shenning.noren        return true;
3880cecf9d5Sandi    }
3890cecf9d5Sandi
3900cecf9d5Sandi    function smiley($match, $state, $pos) {
391433bef32Sandi        $this->_addCall('smiley',array($match), $pos);
39244881bd0Shenning.noren        return true;
3930cecf9d5Sandi    }
3940cecf9d5Sandi
3950cecf9d5Sandi    function wordblock($match, $state, $pos) {
396433bef32Sandi        $this->_addCall('wordblock',array($match), $pos);
39744881bd0Shenning.noren        return true;
3980cecf9d5Sandi    }
3990cecf9d5Sandi
4000cecf9d5Sandi    function entity($match, $state, $pos) {
401433bef32Sandi        $this->_addCall('entity',array($match), $pos);
40244881bd0Shenning.noren        return true;
4030cecf9d5Sandi    }
4040cecf9d5Sandi
4050cecf9d5Sandi    function multiplyentity($match, $state, $pos) {
4060cecf9d5Sandi        preg_match_all('/\d+/',$match,$matches);
407433bef32Sandi        $this->_addCall('multiplyentity',array($matches[0][0],$matches[0][1]), $pos);
40844881bd0Shenning.noren        return true;
4090cecf9d5Sandi    }
4100cecf9d5Sandi
4110cecf9d5Sandi    function singlequoteopening($match, $state, $pos) {
412433bef32Sandi        $this->_addCall('singlequoteopening',array(), $pos);
41344881bd0Shenning.noren        return true;
4140cecf9d5Sandi    }
4150cecf9d5Sandi
4160cecf9d5Sandi    function singlequoteclosing($match, $state, $pos) {
417433bef32Sandi        $this->_addCall('singlequoteclosing',array(), $pos);
41844881bd0Shenning.noren        return true;
4190cecf9d5Sandi    }
4200cecf9d5Sandi
42157d757d1SAndreas Gohr    function apostrophe($match, $state, $pos) {
42257d757d1SAndreas Gohr        $this->_addCall('apostrophe',array(), $pos);
42357d757d1SAndreas Gohr        return true;
42457d757d1SAndreas Gohr    }
42557d757d1SAndreas Gohr
4260cecf9d5Sandi    function doublequoteopening($match, $state, $pos) {
427433bef32Sandi        $this->_addCall('doublequoteopening',array(), $pos);
42844881bd0Shenning.noren        return true;
4290cecf9d5Sandi    }
4300cecf9d5Sandi
4310cecf9d5Sandi    function doublequoteclosing($match, $state, $pos) {
432433bef32Sandi        $this->_addCall('doublequoteclosing',array(), $pos);
43344881bd0Shenning.noren        return true;
4340cecf9d5Sandi    }
4350cecf9d5Sandi
4360cecf9d5Sandi    function camelcaselink($match, $state, $pos) {
437433bef32Sandi        $this->_addCall('camelcaselink',array($match), $pos);
43844881bd0Shenning.noren        return true;
4390cecf9d5Sandi    }
4400cecf9d5Sandi
4410cecf9d5Sandi    /*
4420cecf9d5Sandi    */
4430cecf9d5Sandi    function internallink($match, $state, $pos) {
4440cecf9d5Sandi        // Strip the opening and closing markup
4450cecf9d5Sandi        $link = preg_replace(array('/^\[\[/','/\]\]$/u'),'',$match);
4460cecf9d5Sandi
4470cecf9d5Sandi        // Split title from URL
4480cecf9d5Sandi        $link = preg_split('/\|/u',$link,2);
4490cecf9d5Sandi        if ( !isset($link[1]) ) {
4500cecf9d5Sandi            $link[1] = NULL;
4510cecf9d5Sandi        } else if ( preg_match('/^\{\{[^\}]+\}\}$/',$link[1]) ) {
4525578eb8fSandi            // If the title is an image, convert it to an array containing the image details
453b625487dSandi            $link[1] = Doku_Handler_Parse_Media($link[1]);
4540cecf9d5Sandi        }
4550b7c14c2Sandi        $link[0] = trim($link[0]);
4560cecf9d5Sandi
4570e1c636eSandi        //decide which kind of link it is
4580e1c636eSandi
459e08dda3fSAndreas Gohr        if ( preg_match('/^[a-zA-Z0-9\.]+>{1}.*$/u',$link[0]) ) {
4600e1c636eSandi        // Interwiki
4610cecf9d5Sandi            $interwiki = preg_split('/>/u',$link[0]);
462433bef32Sandi            $this->_addCall(
4630cecf9d5Sandi                'interwikilink',
4640cecf9d5Sandi                array($link[0],$link[1],strtolower($interwiki[0]),$interwiki[1]),
4650cecf9d5Sandi                $pos
4660cecf9d5Sandi                );
4670b7c14c2Sandi        }elseif ( preg_match('/^\\\\\\\\[\w.:?\-;,]+?\\\\/u',$link[0]) ) {
4680e1c636eSandi        // Windows Share
469433bef32Sandi            $this->_addCall(
4700cecf9d5Sandi                'windowssharelink',
4710cecf9d5Sandi                array($link[0],$link[1]),
4720cecf9d5Sandi                $pos
4730cecf9d5Sandi                );
4744468cb4cSAndreas Gohr        }elseif ( preg_match('#^([a-z0-9\-\.+]+?)://#i',$link[0]) ) {
4750e1c636eSandi        // external link (accepts all protocols)
476433bef32Sandi            $this->_addCall(
4770cecf9d5Sandi                    'externallink',
4780cecf9d5Sandi                    array($link[0],$link[1]),
4790cecf9d5Sandi                    $pos
4800cecf9d5Sandi                    );
4810a1d30bfSchris        }elseif ( preg_match('<'.PREG_PATTERN_VALID_EMAIL.'>',$link[0]) ) {
4820a1d30bfSchris        // E-Mail (pattern above is defined in inc/mail.php)
483a6755281Sandi            $this->_addCall(
484a6755281Sandi                'emaillink',
485a6755281Sandi                array($link[0],$link[1]),
486a6755281Sandi                $pos
487a6755281Sandi                );
4880b7c14c2Sandi        }elseif ( preg_match('!^#.+!',$link[0]) ){
4890b7c14c2Sandi        // local link
4900b7c14c2Sandi            $this->_addCall(
4910b7c14c2Sandi                'locallink',
4920b7c14c2Sandi                array(substr($link[0],1),$link[1]),
4930b7c14c2Sandi                $pos
4940b7c14c2Sandi                );
4950e1c636eSandi        }else{
4960e1c636eSandi        // internal link
497433bef32Sandi            $this->_addCall(
4980e1c636eSandi                'internallink',
4990e1c636eSandi                array($link[0],$link[1]),
5000e1c636eSandi                $pos
5010e1c636eSandi                );
5020cecf9d5Sandi        }
5030e1c636eSandi
50444881bd0Shenning.noren        return true;
5050cecf9d5Sandi    }
5060cecf9d5Sandi
5070cecf9d5Sandi    function filelink($match, $state, $pos) {
508433bef32Sandi        $this->_addCall('filelink',array($match, NULL), $pos);
50944881bd0Shenning.noren        return true;
5100cecf9d5Sandi    }
5110cecf9d5Sandi
5120cecf9d5Sandi    function windowssharelink($match, $state, $pos) {
513433bef32Sandi        $this->_addCall('windowssharelink',array($match, NULL), $pos);
51444881bd0Shenning.noren        return true;
5150cecf9d5Sandi    }
5160cecf9d5Sandi
5170cecf9d5Sandi    function media($match, $state, $pos) {
5180cecf9d5Sandi        $p = Doku_Handler_Parse_Media($match);
5190cecf9d5Sandi
520433bef32Sandi        $this->_addCall(
5210cecf9d5Sandi              $p['type'],
522dc673a5bSjoe.lapp              array($p['src'], $p['title'], $p['align'], $p['width'],
523dc673a5bSjoe.lapp                     $p['height'], $p['cache'], $p['linking']),
5240cecf9d5Sandi              $pos
5250cecf9d5Sandi             );
52644881bd0Shenning.noren        return true;
5270cecf9d5Sandi    }
5280cecf9d5Sandi
529b625487dSandi    function rss($match, $state, $pos) {
530b625487dSandi        $link = preg_replace(array('/^\{\{rss>/','/\}\}$/'),'',$match);
5313db95becSAndreas Gohr
5323db95becSAndreas Gohr        // get params
5333db95becSAndreas Gohr        list($link,$params) = explode(' ',$link,2);
5343db95becSAndreas Gohr
5353db95becSAndreas Gohr        $p = array();
5363db95becSAndreas Gohr        if(preg_match('/\b(\d+)\b/',$params,$match)){
5373db95becSAndreas Gohr            $p['max'] = $match[1];
5383db95becSAndreas Gohr        }else{
5393db95becSAndreas Gohr            $p['max'] = 8;
5403db95becSAndreas Gohr        }
5413db95becSAndreas Gohr        $p['reverse'] = (preg_match('/rev/',$params));
5423db95becSAndreas Gohr        $p['author']  = (preg_match('/\b(by|author)/',$params));
5433db95becSAndreas Gohr        $p['date']    = (preg_match('/\b(date)/',$params));
5443db95becSAndreas Gohr        $p['details'] = (preg_match('/\b(desc|detail)/',$params));
5453db95becSAndreas Gohr
5460a69dff7Schris        if (preg_match('/\b(\d+)([dhm])\b/',$params,$match)) {
5470a69dff7Schris          $period = array('d' => 86400, 'h' => 3600, 'm' => 60);
5480a69dff7Schris          $p['refresh'] = max(600,$match[1]*$period[$match[2]]);  // n * period in seconds, minimum 10 minutes
5490a69dff7Schris        } else {
5500a69dff7Schris          $p['refresh'] = 14400;   // default to 4 hours
5510a69dff7Schris        }
5520a69dff7Schris
5533db95becSAndreas Gohr        $this->_addCall('rss',array($link,$p),$pos);
55444881bd0Shenning.noren        return true;
555b625487dSandi    }
556b625487dSandi
5570cecf9d5Sandi    function externallink($match, $state, $pos) {
558da9f31c5SAndreas Gohr        $url   = $match;
559da9f31c5SAndreas Gohr        $title = null;
5600cecf9d5Sandi
561da9f31c5SAndreas Gohr        // add protocol on simple short URLs
562da9f31c5SAndreas Gohr        if(substr($url,0,3) == 'ftp' && (substr($url,0,6) != 'ftp://')){
563da9f31c5SAndreas Gohr            $title = $url;
564da9f31c5SAndreas Gohr            $url   = 'ftp://'.$url;
565da9f31c5SAndreas Gohr        }
566da9f31c5SAndreas Gohr        if(substr($url,0,3) == 'www' && (substr($url,0,7) != 'http://')){
567da9f31c5SAndreas Gohr            $title = $url;
568da9f31c5SAndreas Gohr            $url = 'http://'.$url;
569da9f31c5SAndreas Gohr        }
570da9f31c5SAndreas Gohr
571da9f31c5SAndreas Gohr        $this->_addCall('externallink',array($url, $title), $pos);
57244881bd0Shenning.noren        return true;
5730cecf9d5Sandi    }
5740cecf9d5Sandi
57571352defSandi    function emaillink($match, $state, $pos) {
5760cecf9d5Sandi        $email = preg_replace(array('/^</','/>$/'),'',$match);
577433bef32Sandi        $this->_addCall('emaillink',array($email, NULL), $pos);
57844881bd0Shenning.noren        return true;
5790cecf9d5Sandi    }
5800cecf9d5Sandi
5810cecf9d5Sandi    function table($match, $state, $pos) {
5820cecf9d5Sandi        switch ( $state ) {
5830cecf9d5Sandi
5840cecf9d5Sandi            case DOKU_LEXER_ENTER:
5850cecf9d5Sandi
5860cecf9d5Sandi                $ReWriter = & new Doku_Handler_Table($this->CallWriter);
5870cecf9d5Sandi                $this->CallWriter = & $ReWriter;
5880cecf9d5Sandi
589433bef32Sandi                $this->_addCall('table_start', array(), $pos);
590433bef32Sandi                //$this->_addCall('table_row', array(), $pos);
5910cecf9d5Sandi                if ( trim($match) == '^' ) {
592433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
5930cecf9d5Sandi                } else {
594433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
5950cecf9d5Sandi                }
5960cecf9d5Sandi            break;
5970cecf9d5Sandi
5980cecf9d5Sandi            case DOKU_LEXER_EXIT:
599433bef32Sandi                $this->_addCall('table_end', array(), $pos);
6000cecf9d5Sandi                $this->CallWriter->process();
6010cecf9d5Sandi                $ReWriter = & $this->CallWriter;
6020cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
6030cecf9d5Sandi            break;
6040cecf9d5Sandi
6050cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
6060cecf9d5Sandi                if ( trim($match) != '' ) {
607433bef32Sandi                    $this->_addCall('cdata',array($match), $pos);
6080cecf9d5Sandi                }
6090cecf9d5Sandi            break;
6100cecf9d5Sandi
6110cecf9d5Sandi            case DOKU_LEXER_MATCHED:
6129ab75d9eSAndreas Gohr                if ( $match == ' ' ){
6139ab75d9eSAndreas Gohr                    $this->_addCall('cdata', array($match), $pos);
614e205b721SAndreas Gohr                } else if ( preg_match('/\t+/',$match) ) {
6159ab75d9eSAndreas Gohr                    $this->_addCall('table_align', array($match), $pos);
616e205b721SAndreas Gohr                } else if ( preg_match('/ {2,}/',$match) ) {
617433bef32Sandi                    $this->_addCall('table_align', array($match), $pos);
6180cecf9d5Sandi                } else if ( $match == "\n|" ) {
619433bef32Sandi                    $this->_addCall('table_row', array(), $pos);
620433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
6210cecf9d5Sandi                } else if ( $match == "\n^" ) {
622433bef32Sandi                    $this->_addCall('table_row', array(), $pos);
623433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
6240cecf9d5Sandi                } else if ( $match == '|' ) {
625433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
6260cecf9d5Sandi                } else if ( $match == '^' ) {
627433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
6280cecf9d5Sandi                }
6290cecf9d5Sandi            break;
6300cecf9d5Sandi        }
63144881bd0Shenning.noren        return true;
6320cecf9d5Sandi    }
6330cecf9d5Sandi}
6340cecf9d5Sandi
6350cecf9d5Sandi//------------------------------------------------------------------------
6360cecf9d5Sandifunction Doku_Handler_Parse_Media($match) {
6370cecf9d5Sandi
6380cecf9d5Sandi    // Strip the opening and closing markup
6390cecf9d5Sandi    $link = preg_replace(array('/^\{\{/','/\}\}$/u'),'',$match);
6400cecf9d5Sandi
6410cecf9d5Sandi    // Split title from URL
6420cecf9d5Sandi    $link = preg_split('/\|/u',$link,2);
6430cecf9d5Sandi
6440cecf9d5Sandi
6450cecf9d5Sandi    // Check alignment
6460cecf9d5Sandi    $ralign = (bool)preg_match('/^ /',$link[0]);
6470cecf9d5Sandi    $lalign = (bool)preg_match('/ $/',$link[0]);
6480cecf9d5Sandi
6490cecf9d5Sandi    // Logic = what's that ;)...
6500cecf9d5Sandi    if ( $lalign & $ralign ) {
6510cecf9d5Sandi        $align = 'center';
6520cecf9d5Sandi    } else if ( $ralign ) {
6530cecf9d5Sandi        $align = 'right';
6540cecf9d5Sandi    } else if ( $lalign ) {
6550cecf9d5Sandi        $align = 'left';
6560cecf9d5Sandi    } else {
6570cecf9d5Sandi        $align = NULL;
6580cecf9d5Sandi    }
6590cecf9d5Sandi
6600cecf9d5Sandi    // The title...
6610cecf9d5Sandi    if ( !isset($link[1]) ) {
6620cecf9d5Sandi        $link[1] = NULL;
6630cecf9d5Sandi    }
6640cecf9d5Sandi
6654826ab45Sandi    //remove aligning spaces
6664826ab45Sandi    $link[0] = trim($link[0]);
6670cecf9d5Sandi
6684826ab45Sandi    //split into src and parameters (using the very last questionmark)
6694826ab45Sandi    $pos = strrpos($link[0], '?');
6704826ab45Sandi    if($pos !== false){
6714826ab45Sandi        $src   = substr($link[0],0,$pos);
6724826ab45Sandi        $param = substr($link[0],$pos+1);
6730cecf9d5Sandi    }else{
6744826ab45Sandi        $src   = $link[0];
6754826ab45Sandi        $param = '';
6760cecf9d5Sandi    }
6770cecf9d5Sandi
6784826ab45Sandi    //parse width and height
6794826ab45Sandi    if(preg_match('#(\d+)(x(\d+))?#i',$param,$size)){
6804826ab45Sandi        ($size[1]) ? $w = $size[1] : $w = NULL;
6814826ab45Sandi        ($size[3]) ? $h = $size[3] : $h = NULL;
682fc1c55b1Shfuecks    } else {
683fc1c55b1Shfuecks        $w = NULL;
684fc1c55b1Shfuecks        $h = NULL;
6850cecf9d5Sandi    }
6860cecf9d5Sandi
687dc673a5bSjoe.lapp    //get linking command
688d35ab615Shenning.noren    if(preg_match('/nolink/i',$param)){
689dc673a5bSjoe.lapp        $linking = 'nolink';
690d35ab615Shenning.noren    }else if(preg_match('/direct/i',$param)){
691dc673a5bSjoe.lapp        $linking = 'direct';
6928acb3108SAndreas Gohr    }else if(preg_match('/linkonly/i',$param)){
6938acb3108SAndreas Gohr        $linking = 'linkonly';
694dc673a5bSjoe.lapp    }else{
695dc673a5bSjoe.lapp        $linking = 'details';
696dc673a5bSjoe.lapp    }
697dc673a5bSjoe.lapp
6984826ab45Sandi    //get caching command
6994826ab45Sandi    if (preg_match('/(nocache|recache)/i',$param,$cachemode)){
7004826ab45Sandi        $cache = $cachemode[1];
7010cecf9d5Sandi    }else{
7024826ab45Sandi        $cache = 'cache';
7030cecf9d5Sandi    }
7040cecf9d5Sandi
7050cecf9d5Sandi    // Check whether this is a local or remote image
7064826ab45Sandi    if ( preg_match('#^(https?|ftp)#i',$src) ) {
7074826ab45Sandi        $call = 'externalmedia';
7080cecf9d5Sandi    } else {
7094826ab45Sandi        $call = 'internalmedia';
7100cecf9d5Sandi    }
7110cecf9d5Sandi
7120cecf9d5Sandi    $params = array(
7130cecf9d5Sandi        'type'=>$call,
7144826ab45Sandi        'src'=>$src,
7150cecf9d5Sandi        'title'=>$link[1],
7160cecf9d5Sandi        'align'=>$align,
7174826ab45Sandi        'width'=>$w,
7184826ab45Sandi        'height'=>$h,
7190cecf9d5Sandi        'cache'=>$cache,
720dc673a5bSjoe.lapp        'linking'=>$linking,
7210cecf9d5Sandi    );
7220cecf9d5Sandi
7230cecf9d5Sandi    return $params;
7240cecf9d5Sandi}
7250cecf9d5Sandi
7260cecf9d5Sandi//------------------------------------------------------------------------
7270cecf9d5Sandiclass Doku_Handler_CallWriter {
7280cecf9d5Sandi
7290cecf9d5Sandi    var $Handler;
7300cecf9d5Sandi
7310cecf9d5Sandi    function Doku_Handler_CallWriter(& $Handler) {
7320cecf9d5Sandi        $this->Handler = & $Handler;
7330cecf9d5Sandi    }
7340cecf9d5Sandi
7350cecf9d5Sandi    function writeCall($call) {
7360cecf9d5Sandi        $this->Handler->calls[] = $call;
7370cecf9d5Sandi    }
7380cecf9d5Sandi
7390cecf9d5Sandi    function writeCalls($calls) {
7400cecf9d5Sandi        $this->Handler->calls = array_merge($this->Handler->calls, $calls);
7410cecf9d5Sandi    }
742f4f02a0fSchris
743f4f02a0fSchris    // function is required, but since this call writer is first/highest in
744f4f02a0fSchris    // the chain it is not required to do anything
745f4f02a0fSchris    function finalise() {
746f4f02a0fSchris    }
7470cecf9d5Sandi}
7480cecf9d5Sandi
7490cecf9d5Sandi//------------------------------------------------------------------------
7505587e44cSchris/**
7515587e44cSchris * Generic call writer class to handle nesting of rendering instructions
7525587e44cSchris * within a render instruction. Also see nest() method of renderer base class
7535587e44cSchris *
7545587e44cSchris * @author    Chris Smith <chris@jalakai.co.uk>
7555587e44cSchris */
7565587e44cSchrisclass Doku_Handler_Nest {
7575587e44cSchris
7585587e44cSchris    var $CallWriter;
7595587e44cSchris    var $calls = array();
7605587e44cSchris
7615587e44cSchris    var $closingInstruction;
7625587e44cSchris
7635587e44cSchris    /**
7645587e44cSchris     * constructor
7655587e44cSchris     *
7665587e44cSchris     * @param  object     $CallWriter     the renderers current call writer
7675587e44cSchris     * @param  string     $close          closing instruction name, this is required to properly terminate the
7685587e44cSchris     *                                    syntax mode if the document ends without a closing pattern
7695587e44cSchris     */
7705587e44cSchris    function Doku_Handler_Nest(& $CallWriter, $close="nest_close") {
7715587e44cSchris        $this->CallWriter = & $CallWriter;
7725587e44cSchris
7735587e44cSchris        $this->closingInstruction = $close;
7745587e44cSchris    }
7755587e44cSchris
7765587e44cSchris    function writeCall($call) {
7775587e44cSchris        $this->calls[] = $call;
7785587e44cSchris    }
7795587e44cSchris
7805587e44cSchris    function writeCalls($calls) {
7815587e44cSchris        $this->calls = array_merge($this->calls, $calls);
7825587e44cSchris    }
7835587e44cSchris
7845587e44cSchris    function finalise() {
7855587e44cSchris        $last_call = end($this->calls);
7865587e44cSchris        $this->writeCall(array($this->closingInstruction,array(), $last_call[2]));
7875587e44cSchris
7885587e44cSchris        $this->process();
7895587e44cSchris        $this->CallWriter->finalise();
7905587e44cSchris    }
7915587e44cSchris
7925587e44cSchris    function process() {
79341624b31SChris Smith        // merge consecutive cdata
79441624b31SChris Smith        $unmerged_calls = $this->calls;
79541624b31SChris Smith        $this->calls = array();
79641624b31SChris Smith
79741624b31SChris Smith        foreach ($unmerged_calls as $call) $this->addCall($call);
79841624b31SChris Smith
7995587e44cSchris        $first_call = reset($this->calls);
8005587e44cSchris        $this->CallWriter->writeCall(array("nest", array($this->calls), $first_call[2]));
8015587e44cSchris    }
80241624b31SChris Smith
80341624b31SChris Smith    function addCall($call) {
80441624b31SChris Smith        $key = count($this->calls);
80541624b31SChris Smith        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
80641624b31SChris Smith            $this->calls[$key-1][1][0] .= $call[1][0];
80741624b31SChris Smith        } else {
80841624b31SChris Smith            $this->calls[] = $call;
80941624b31SChris Smith        }
81041624b31SChris Smith    }
8115587e44cSchris}
8125587e44cSchris
8130cecf9d5Sandiclass Doku_Handler_List {
8140cecf9d5Sandi
8150cecf9d5Sandi    var $CallWriter;
8160cecf9d5Sandi
8170cecf9d5Sandi    var $calls = array();
8180cecf9d5Sandi    var $listCalls = array();
8190cecf9d5Sandi    var $listStack = array();
8200cecf9d5Sandi
8210cecf9d5Sandi    function Doku_Handler_List(& $CallWriter) {
8220cecf9d5Sandi        $this->CallWriter = & $CallWriter;
8230cecf9d5Sandi    }
8240cecf9d5Sandi
8250cecf9d5Sandi    function writeCall($call) {
8260cecf9d5Sandi        $this->calls[] = $call;
8270cecf9d5Sandi    }
8280cecf9d5Sandi
8290cecf9d5Sandi    // Probably not needed but just in case...
8300cecf9d5Sandi    function writeCalls($calls) {
8310cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
832f4f02a0fSchris#        $this->CallWriter->writeCalls($this->calls);
833f4f02a0fSchris    }
834f4f02a0fSchris
835f4f02a0fSchris    function finalise() {
836f4f02a0fSchris        $last_call = end($this->calls);
837f4f02a0fSchris        $this->writeCall(array('list_close',array(), $last_call[2]));
838f4f02a0fSchris
839f4f02a0fSchris        $this->process();
840f4f02a0fSchris        $this->CallWriter->finalise();
8410cecf9d5Sandi    }
8420cecf9d5Sandi
8430cecf9d5Sandi    //------------------------------------------------------------------------
8440cecf9d5Sandi    function process() {
845f4f02a0fSchris
8460cecf9d5Sandi        foreach ( $this->calls as $call ) {
8470cecf9d5Sandi            switch ($call[0]) {
8480cecf9d5Sandi                case 'list_item':
8490cecf9d5Sandi                    $this->listOpen($call);
8500cecf9d5Sandi                break;
8510cecf9d5Sandi                case 'list_open':
8520cecf9d5Sandi                    $this->listStart($call);
8530cecf9d5Sandi                break;
8540cecf9d5Sandi                case 'list_close':
8550cecf9d5Sandi                    $this->listEnd($call);
8560cecf9d5Sandi                break;
8570cecf9d5Sandi                default:
8580cecf9d5Sandi                    $this->listContent($call);
8590cecf9d5Sandi                break;
8600cecf9d5Sandi            }
8610cecf9d5Sandi        }
8620cecf9d5Sandi
8630cecf9d5Sandi        $this->CallWriter->writeCalls($this->listCalls);
8640cecf9d5Sandi    }
8650cecf9d5Sandi
8660cecf9d5Sandi    //------------------------------------------------------------------------
8670cecf9d5Sandi    function listStart($call) {
8680cecf9d5Sandi        $depth = $this->interpretSyntax($call[1][0], $listType);
8690cecf9d5Sandi
8700cecf9d5Sandi        $this->initialDepth = $depth;
8710cecf9d5Sandi        $this->listStack[] = array($listType, $depth);
8720cecf9d5Sandi
8730cecf9d5Sandi        $this->listCalls[] = array('list'.$listType.'_open',array(),$call[2]);
8740cecf9d5Sandi        $this->listCalls[] = array('listitem_open',array(1),$call[2]);
8750cecf9d5Sandi        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
8760cecf9d5Sandi    }
8770cecf9d5Sandi
8780cecf9d5Sandi    //------------------------------------------------------------------------
8790cecf9d5Sandi    function listEnd($call) {
88044881bd0Shenning.noren        $closeContent = true;
8810cecf9d5Sandi
8820cecf9d5Sandi        while ( $list = array_pop($this->listStack) ) {
8830cecf9d5Sandi            if ( $closeContent ) {
8840cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
88544881bd0Shenning.noren                $closeContent = false;
8860cecf9d5Sandi            }
8870cecf9d5Sandi            $this->listCalls[] = array('listitem_close',array(),$call[2]);
8880cecf9d5Sandi            $this->listCalls[] = array('list'.$list[0].'_close', array(), $call[2]);
8890cecf9d5Sandi        }
8900cecf9d5Sandi    }
8910cecf9d5Sandi
8920cecf9d5Sandi    //------------------------------------------------------------------------
8930cecf9d5Sandi    function listOpen($call) {
8940cecf9d5Sandi        $depth = $this->interpretSyntax($call[1][0], $listType);
8950cecf9d5Sandi        $end = end($this->listStack);
8960cecf9d5Sandi
8970cecf9d5Sandi        // Not allowed to be shallower than initialDepth
8980cecf9d5Sandi        if ( $depth < $this->initialDepth ) {
8990cecf9d5Sandi            $depth = $this->initialDepth;
9000cecf9d5Sandi        }
9010cecf9d5Sandi
9020cecf9d5Sandi        //------------------------------------------------------------------------
9030cecf9d5Sandi        if ( $depth == $end[1] ) {
9040cecf9d5Sandi
9050cecf9d5Sandi            // Just another item in the list...
9060cecf9d5Sandi            if ( $listType == $end[0] ) {
9070cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9080cecf9d5Sandi                $this->listCalls[] = array('listitem_close',array(),$call[2]);
9090cecf9d5Sandi                $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
9100cecf9d5Sandi                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9110cecf9d5Sandi
9120cecf9d5Sandi            // Switched list type...
9130cecf9d5Sandi            } else {
9140cecf9d5Sandi
9150cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9160cecf9d5Sandi                $this->listCalls[] = array('listitem_close',array(),$call[2]);
9170cecf9d5Sandi                $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
9180cecf9d5Sandi                $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9190cecf9d5Sandi                $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9200cecf9d5Sandi                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9210cecf9d5Sandi
9220cecf9d5Sandi                array_pop($this->listStack);
9230cecf9d5Sandi                $this->listStack[] = array($listType, $depth);
9240cecf9d5Sandi            }
9250cecf9d5Sandi
9260cecf9d5Sandi        //------------------------------------------------------------------------
9270cecf9d5Sandi        // Getting deeper...
9280cecf9d5Sandi        } else if ( $depth > $end[1] ) {
9290cecf9d5Sandi
9300cecf9d5Sandi            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9310cecf9d5Sandi            $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9320cecf9d5Sandi            $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9330cecf9d5Sandi            $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9340cecf9d5Sandi
9350cecf9d5Sandi            $this->listStack[] = array($listType, $depth);
9360cecf9d5Sandi
9370cecf9d5Sandi        //------------------------------------------------------------------------
9380cecf9d5Sandi        // Getting shallower ( $depth < $end[1] )
9390cecf9d5Sandi        } else {
9400cecf9d5Sandi            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9410cecf9d5Sandi            $this->listCalls[] = array('listitem_close',array(),$call[2]);
9420cecf9d5Sandi            $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
9430cecf9d5Sandi
9440cecf9d5Sandi            // Throw away the end - done
9450cecf9d5Sandi            array_pop($this->listStack);
9460cecf9d5Sandi
9470cecf9d5Sandi            while (1) {
9480cecf9d5Sandi                $end = end($this->listStack);
9490cecf9d5Sandi
9500cecf9d5Sandi                if ( $end[1] <= $depth ) {
9510cecf9d5Sandi
9520cecf9d5Sandi                    // Normalize depths
9530cecf9d5Sandi                    $depth = $end[1];
9540cecf9d5Sandi
9550cecf9d5Sandi                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
9560cecf9d5Sandi
9570cecf9d5Sandi                    if ( $end[0] == $listType ) {
9580cecf9d5Sandi                        $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
9590cecf9d5Sandi                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9600cecf9d5Sandi
9610cecf9d5Sandi                    } else {
9620cecf9d5Sandi                        // Switching list type...
9630cecf9d5Sandi                        $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
9640cecf9d5Sandi                        $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9650cecf9d5Sandi                        $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9660cecf9d5Sandi                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9670cecf9d5Sandi
9680cecf9d5Sandi                        array_pop($this->listStack);
9690cecf9d5Sandi                        $this->listStack[] = array($listType, $depth);
9700cecf9d5Sandi                    }
9710cecf9d5Sandi
9720cecf9d5Sandi                    break;
9730cecf9d5Sandi
9740cecf9d5Sandi                // Haven't dropped down far enough yet.... ( $end[1] > $depth )
9750cecf9d5Sandi                } else {
9760cecf9d5Sandi
9770cecf9d5Sandi                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
9780cecf9d5Sandi                    $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
9790cecf9d5Sandi
9800cecf9d5Sandi                    array_pop($this->listStack);
9810cecf9d5Sandi
9820cecf9d5Sandi                }
9830cecf9d5Sandi
9840cecf9d5Sandi            }
9850cecf9d5Sandi
9860cecf9d5Sandi        }
9870cecf9d5Sandi    }
9880cecf9d5Sandi
9890cecf9d5Sandi    //------------------------------------------------------------------------
9900cecf9d5Sandi    function listContent($call) {
9910cecf9d5Sandi        $this->listCalls[] = $call;
9920cecf9d5Sandi    }
9930cecf9d5Sandi
9940cecf9d5Sandi    //------------------------------------------------------------------------
9950cecf9d5Sandi    function interpretSyntax($match, & $type) {
9960cecf9d5Sandi        if ( substr($match,-1) == '*' ) {
9970cecf9d5Sandi            $type = 'u';
9980cecf9d5Sandi        } else {
9990cecf9d5Sandi            $type = 'o';
10000cecf9d5Sandi        }
10010cecf9d5Sandi        return count(explode('  ',str_replace("\t",'  ',$match)));
10020cecf9d5Sandi    }
10030cecf9d5Sandi}
10040cecf9d5Sandi
10050cecf9d5Sandi//------------------------------------------------------------------------
10060cecf9d5Sandiclass Doku_Handler_Preformatted {
10070cecf9d5Sandi
10080cecf9d5Sandi    var $CallWriter;
10090cecf9d5Sandi
10100cecf9d5Sandi    var $calls = array();
10110cecf9d5Sandi    var $pos;
10120cecf9d5Sandi    var $text ='';
10130cecf9d5Sandi
10140cecf9d5Sandi
10150cecf9d5Sandi
10160cecf9d5Sandi    function Doku_Handler_Preformatted(& $CallWriter) {
10170cecf9d5Sandi        $this->CallWriter = & $CallWriter;
10180cecf9d5Sandi    }
10190cecf9d5Sandi
10200cecf9d5Sandi    function writeCall($call) {
10210cecf9d5Sandi        $this->calls[] = $call;
10220cecf9d5Sandi    }
10230cecf9d5Sandi
10240cecf9d5Sandi    // Probably not needed but just in case...
10250cecf9d5Sandi    function writeCalls($calls) {
10260cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1027f4f02a0fSchris#        $this->CallWriter->writeCalls($this->calls);
1028f4f02a0fSchris    }
1029f4f02a0fSchris
1030f4f02a0fSchris    function finalise() {
1031f4f02a0fSchris        $last_call = end($this->calls);
1032f4f02a0fSchris        $this->writeCall(array('preformatted_end',array(), $last_call[2]));
1033f4f02a0fSchris
1034f4f02a0fSchris        $this->process();
1035f4f02a0fSchris        $this->CallWriter->finalise();
10360cecf9d5Sandi    }
10370cecf9d5Sandi
10380cecf9d5Sandi    function process() {
10390cecf9d5Sandi        foreach ( $this->calls as $call ) {
10400cecf9d5Sandi            switch ($call[0]) {
10410cecf9d5Sandi                case 'preformatted_start':
10420cecf9d5Sandi                    $this->pos = $call[2];
10430cecf9d5Sandi                break;
10440cecf9d5Sandi                case 'preformatted_newline':
10450cecf9d5Sandi                    $this->text .= "\n";
10460cecf9d5Sandi                break;
10470cecf9d5Sandi                case 'preformatted_content':
10480cecf9d5Sandi                    $this->text .= $call[1][0];
10490cecf9d5Sandi                break;
10500cecf9d5Sandi                case 'preformatted_end':
105193a34bf3SChris Smith                    if (trim($this->text)) {
10520cecf9d5Sandi                      $this->CallWriter->writeCall(array('preformatted',array($this->text),$this->pos));
105393a34bf3SChris Smith                    }
10540cecf9d5Sandi                break;
10550cecf9d5Sandi            }
10560cecf9d5Sandi        }
10570cecf9d5Sandi    }
1058f4f02a0fSchris
10590cecf9d5Sandi}
10600cecf9d5Sandi
10610cecf9d5Sandi//------------------------------------------------------------------------
10620cecf9d5Sandiclass Doku_Handler_Quote {
10630cecf9d5Sandi
10640cecf9d5Sandi    var $CallWriter;
10650cecf9d5Sandi
10660cecf9d5Sandi    var $calls = array();
10670cecf9d5Sandi
10680cecf9d5Sandi    var $quoteCalls = array();
10690cecf9d5Sandi
10700cecf9d5Sandi    function Doku_Handler_Quote(& $CallWriter) {
10710cecf9d5Sandi        $this->CallWriter = & $CallWriter;
10720cecf9d5Sandi    }
10730cecf9d5Sandi
10740cecf9d5Sandi    function writeCall($call) {
10750cecf9d5Sandi        $this->calls[] = $call;
10760cecf9d5Sandi    }
10770cecf9d5Sandi
10780cecf9d5Sandi    // Probably not needed but just in case...
10790cecf9d5Sandi    function writeCalls($calls) {
10800cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1081f4f02a0fSchris    }
1082f4f02a0fSchris
1083f4f02a0fSchris    function finalise() {
1084f4f02a0fSchris        $last_call = end($this->calls);
1085f4f02a0fSchris        $this->writeCall(array('quote_end',array(), $last_call[2]));
1086f4f02a0fSchris
1087f4f02a0fSchris        $this->process();
1088f4f02a0fSchris        $this->CallWriter->finalise();
10890cecf9d5Sandi    }
10900cecf9d5Sandi
10910cecf9d5Sandi    function process() {
10920cecf9d5Sandi
10930cecf9d5Sandi        $quoteDepth = 1;
10940cecf9d5Sandi
10950cecf9d5Sandi        foreach ( $this->calls as $call ) {
10960cecf9d5Sandi            switch ($call[0]) {
10970cecf9d5Sandi
10980cecf9d5Sandi                case 'quote_start':
10990cecf9d5Sandi
11000cecf9d5Sandi                    $this->quoteCalls[] = array('quote_open',array(),$call[2]);
11010cecf9d5Sandi
11020cecf9d5Sandi                case 'quote_newline':
11030cecf9d5Sandi
11040cecf9d5Sandi                    $quoteLength = $this->getDepth($call[1][0]);
11050cecf9d5Sandi
11060cecf9d5Sandi                    if ( $quoteLength > $quoteDepth ) {
11070cecf9d5Sandi                        $quoteDiff = $quoteLength - $quoteDepth;
11080cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11090cecf9d5Sandi                            $this->quoteCalls[] = array('quote_open',array(),$call[2]);
11100cecf9d5Sandi                        }
11110cecf9d5Sandi                    } else if ( $quoteLength < $quoteDepth ) {
11120cecf9d5Sandi                        $quoteDiff = $quoteDepth - $quoteLength;
11130cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11140cecf9d5Sandi                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11150cecf9d5Sandi                        }
111626426c64Schris                    } else {
111726426c64Schris                        if ($call[0] != 'quote_start') $this->quoteCalls[] = array('linebreak',array(),$call[2]);
11180cecf9d5Sandi                    }
11190cecf9d5Sandi
11200cecf9d5Sandi                    $quoteDepth = $quoteLength;
11210cecf9d5Sandi
11220cecf9d5Sandi                break;
11230cecf9d5Sandi
11240cecf9d5Sandi                case 'quote_end':
11250cecf9d5Sandi
11260cecf9d5Sandi                    if ( $quoteDepth > 1 ) {
11270cecf9d5Sandi                        $quoteDiff = $quoteDepth - 1;
11280cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11290cecf9d5Sandi                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11300cecf9d5Sandi                        }
11310cecf9d5Sandi                    }
11320cecf9d5Sandi
11330cecf9d5Sandi                    $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11340cecf9d5Sandi
11350cecf9d5Sandi                    $this->CallWriter->writeCalls($this->quoteCalls);
11360cecf9d5Sandi                break;
11370cecf9d5Sandi
11380cecf9d5Sandi                default:
11390cecf9d5Sandi                    $this->quoteCalls[] = $call;
11400cecf9d5Sandi                break;
11410cecf9d5Sandi            }
11420cecf9d5Sandi        }
11430cecf9d5Sandi    }
11440cecf9d5Sandi
11450cecf9d5Sandi    function getDepth($marker) {
11460cecf9d5Sandi        preg_match('/>{1,}/', $marker, $matches);
11470cecf9d5Sandi        $quoteLength = strlen($matches[0]);
11480cecf9d5Sandi        return $quoteLength;
11490cecf9d5Sandi    }
11500cecf9d5Sandi}
11510cecf9d5Sandi
11520cecf9d5Sandi//------------------------------------------------------------------------
11530cecf9d5Sandiclass Doku_Handler_Table {
11540cecf9d5Sandi
11550cecf9d5Sandi    var $CallWriter;
11560cecf9d5Sandi
11570cecf9d5Sandi    var $calls = array();
11580cecf9d5Sandi    var $tableCalls = array();
11590cecf9d5Sandi    var $maxCols = 0;
11600cecf9d5Sandi    var $maxRows = 1;
11610cecf9d5Sandi    var $currentCols = 0;
116244881bd0Shenning.noren    var $firstCell = false;
11630cecf9d5Sandi    var $lastCellType = 'tablecell';
11640cecf9d5Sandi
11650cecf9d5Sandi    function Doku_Handler_Table(& $CallWriter) {
11660cecf9d5Sandi        $this->CallWriter = & $CallWriter;
11670cecf9d5Sandi    }
11680cecf9d5Sandi
11690cecf9d5Sandi    function writeCall($call) {
11700cecf9d5Sandi        $this->calls[] = $call;
11710cecf9d5Sandi    }
11720cecf9d5Sandi
11730cecf9d5Sandi    // Probably not needed but just in case...
11740cecf9d5Sandi    function writeCalls($calls) {
11750cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1176f4f02a0fSchris    }
1177f4f02a0fSchris
1178f4f02a0fSchris    function finalise() {
1179f4f02a0fSchris        $last_call = end($this->calls);
1180f4f02a0fSchris        $this->writeCall(array('table_end',array(), $last_call[2]));
1181f4f02a0fSchris
1182f4f02a0fSchris        $this->process();
1183f4f02a0fSchris        $this->CallWriter->finalise();
11840cecf9d5Sandi    }
11850cecf9d5Sandi
11860cecf9d5Sandi    //------------------------------------------------------------------------
11870cecf9d5Sandi    function process() {
11880cecf9d5Sandi        foreach ( $this->calls as $call ) {
11890cecf9d5Sandi            switch ( $call[0] ) {
11900cecf9d5Sandi                case 'table_start':
11910cecf9d5Sandi                    $this->tableStart($call);
11920cecf9d5Sandi                break;
11930cecf9d5Sandi                case 'table_row':
11940cecf9d5Sandi                    $this->tableRowClose(array('tablerow_close',$call[1],$call[2]));
11950cecf9d5Sandi                    $this->tableRowOpen(array('tablerow_open',$call[1],$call[2]));
11960cecf9d5Sandi                break;
11970cecf9d5Sandi                case 'tableheader':
11980cecf9d5Sandi                case 'tablecell':
11990cecf9d5Sandi                    $this->tableCell($call);
12000cecf9d5Sandi                break;
12010cecf9d5Sandi                case 'table_end':
12020cecf9d5Sandi                    $this->tableRowClose(array('tablerow_close',$call[1],$call[2]));
12030cecf9d5Sandi                    $this->tableEnd($call);
12040cecf9d5Sandi                break;
12050cecf9d5Sandi                default:
12060cecf9d5Sandi                    $this->tableDefault($call);
12070cecf9d5Sandi                break;
12080cecf9d5Sandi            }
12090cecf9d5Sandi        }
12100cecf9d5Sandi        $this->CallWriter->writeCalls($this->tableCalls);
12110cecf9d5Sandi    }
12120cecf9d5Sandi
12130cecf9d5Sandi    function tableStart($call) {
12140cecf9d5Sandi        $this->tableCalls[] = array('table_open',array(),$call[2]);
12150cecf9d5Sandi        $this->tableCalls[] = array('tablerow_open',array(),$call[2]);
121644881bd0Shenning.noren        $this->firstCell = true;
12170cecf9d5Sandi    }
12180cecf9d5Sandi
12190cecf9d5Sandi    function tableEnd($call) {
12200cecf9d5Sandi        $this->tableCalls[] = array('table_close',array(),$call[2]);
12210cecf9d5Sandi        $this->finalizeTable();
12220cecf9d5Sandi    }
12230cecf9d5Sandi
12240cecf9d5Sandi    function tableRowOpen($call) {
12250cecf9d5Sandi        $this->tableCalls[] = $call;
12260cecf9d5Sandi        $this->currentCols = 0;
122744881bd0Shenning.noren        $this->firstCell = true;
12280cecf9d5Sandi        $this->lastCellType = 'tablecell';
12290cecf9d5Sandi        $this->maxRows++;
12300cecf9d5Sandi    }
12310cecf9d5Sandi
12320cecf9d5Sandi    function tableRowClose($call) {
12330cecf9d5Sandi        // Strip off final cell opening and anything after it
12340cecf9d5Sandi        while ( $discard = array_pop($this->tableCalls ) ) {
12350cecf9d5Sandi
12360cecf9d5Sandi            if ( $discard[0] == 'tablecell_open' || $discard[0] == 'tableheader_open') {
12370cecf9d5Sandi
12380cecf9d5Sandi                // Its a spanning element - put it back and close it
12390cecf9d5Sandi                if ( $discard[1][0] > 1 ) {
12400cecf9d5Sandi
12410cecf9d5Sandi                    $this->tableCalls[] = $discard;
12420cecf9d5Sandi                    if ( strstr($discard[0],'cell') ) {
12430cecf9d5Sandi                        $name = 'tablecell';
12440cecf9d5Sandi                    } else {
12450cecf9d5Sandi                        $name = 'tableheader';
12460cecf9d5Sandi                    }
12470cecf9d5Sandi                    $this->tableCalls[] = array($name.'_close',array(),$call[2]);
12480cecf9d5Sandi                }
12490cecf9d5Sandi
12500cecf9d5Sandi                break;
12510cecf9d5Sandi            }
12520cecf9d5Sandi        }
12530cecf9d5Sandi        $this->tableCalls[] = $call;
12540cecf9d5Sandi
12550cecf9d5Sandi        if ( $this->currentCols > $this->maxCols ) {
12560cecf9d5Sandi            $this->maxCols = $this->currentCols;
12570cecf9d5Sandi        }
12580cecf9d5Sandi    }
12590cecf9d5Sandi
12600cecf9d5Sandi    function tableCell($call) {
12610cecf9d5Sandi        if ( !$this->firstCell ) {
12620cecf9d5Sandi
12630cecf9d5Sandi            // Increase the span
12640cecf9d5Sandi            $lastCall = end($this->tableCalls);
12650cecf9d5Sandi
12660cecf9d5Sandi            // A cell call which follows an open cell means an empty cell so span
12670cecf9d5Sandi            if ( $lastCall[0] == 'tablecell_open' || $lastCall[0] == 'tableheader_open' ) {
12680cecf9d5Sandi                 $this->tableCalls[] = array('colspan',array(),$call[2]);
12690cecf9d5Sandi
12700cecf9d5Sandi            }
12710cecf9d5Sandi
12720cecf9d5Sandi            $this->tableCalls[] = array($this->lastCellType.'_close',array(),$call[2]);
12730cecf9d5Sandi            $this->tableCalls[] = array($call[0].'_open',array(1,NULL),$call[2]);
12740cecf9d5Sandi            $this->lastCellType = $call[0];
12750cecf9d5Sandi
12760cecf9d5Sandi        } else {
12770cecf9d5Sandi
12780cecf9d5Sandi            $this->tableCalls[] = array($call[0].'_open',array(1,NULL),$call[2]);
12790cecf9d5Sandi            $this->lastCellType = $call[0];
128044881bd0Shenning.noren            $this->firstCell = false;
12810cecf9d5Sandi
12820cecf9d5Sandi        }
12830cecf9d5Sandi
12840cecf9d5Sandi        $this->currentCols++;
12850cecf9d5Sandi    }
12860cecf9d5Sandi
12870cecf9d5Sandi    function tableDefault($call) {
12880cecf9d5Sandi        $this->tableCalls[] = $call;
12890cecf9d5Sandi    }
12900cecf9d5Sandi
12910cecf9d5Sandi    function finalizeTable() {
12920cecf9d5Sandi
12930cecf9d5Sandi        // Add the max cols and rows to the table opening
12940cecf9d5Sandi        if ( $this->tableCalls[0][0] == 'table_open' ) {
12950cecf9d5Sandi            // Adjust to num cols not num col delimeters
12960cecf9d5Sandi            $this->tableCalls[0][1][] = $this->maxCols - 1;
12970cecf9d5Sandi            $this->tableCalls[0][1][] = $this->maxRows;
12980cecf9d5Sandi        } else {
12990cecf9d5Sandi            trigger_error('First element in table call list is not table_open');
13000cecf9d5Sandi        }
13010cecf9d5Sandi
13020cecf9d5Sandi        $lastRow = 0;
13030cecf9d5Sandi        $lastCell = 0;
13040cecf9d5Sandi        $toDelete = array();
13050cecf9d5Sandi
13060cecf9d5Sandi        // Look for the colspan elements and increment the colspan on the
13070cecf9d5Sandi        // previous non-empty opening cell. Once done, delete all the cells
13080cecf9d5Sandi        // that contain colspans
13090cecf9d5Sandi        foreach ( $this->tableCalls as $key => $call ) {
13100cecf9d5Sandi
13110cecf9d5Sandi            if ( $call[0] == 'tablerow_open' ) {
13120cecf9d5Sandi
13130cecf9d5Sandi                $lastRow = $key;
13140cecf9d5Sandi
13150cecf9d5Sandi            } else if ( $call[0] == 'tablecell_open' || $call[0] == 'tableheader_open' ) {
13160cecf9d5Sandi
13170cecf9d5Sandi                $lastCell = $key;
13180cecf9d5Sandi
13190cecf9d5Sandi            } else if ( $call[0] == 'table_align' ) {
13200cecf9d5Sandi
13210cecf9d5Sandi                // If the previous element was a cell open, align right
13220cecf9d5Sandi                if ( $this->tableCalls[$key-1][0] == 'tablecell_open' || $this->tableCalls[$key-1][0] == 'tableheader_open' ) {
13230cecf9d5Sandi                    $this->tableCalls[$key-1][1][1] = 'right';
13240cecf9d5Sandi
13250cecf9d5Sandi                // If the next element if the close of an element, align either center or left
13260cecf9d5Sandi                } else if ( $this->tableCalls[$key+1][0] == 'tablecell_close' || $this->tableCalls[$key+1][0] == 'tableheader_close' ) {
13270cecf9d5Sandi                    if ( $this->tableCalls[$lastCell][1][1] == 'right' ) {
13280cecf9d5Sandi                        $this->tableCalls[$lastCell][1][1] = 'center';
13290cecf9d5Sandi                    } else {
13300cecf9d5Sandi                        $this->tableCalls[$lastCell][1][1] = 'left';
13310cecf9d5Sandi                    }
13320cecf9d5Sandi
13330cecf9d5Sandi                }
13340cecf9d5Sandi
13350cecf9d5Sandi                // Now convert the whitespace back to cdata
13360cecf9d5Sandi                $this->tableCalls[$key][0] = 'cdata';
13370cecf9d5Sandi
13380cecf9d5Sandi            } else if ( $call[0] == 'colspan' ) {
13390cecf9d5Sandi
134044881bd0Shenning.noren                $this->tableCalls[$key-1][1][0] = false;
13410cecf9d5Sandi
13420cecf9d5Sandi                for($i = $key-2; $i > $lastRow; $i--) {
13430cecf9d5Sandi
13440cecf9d5Sandi                    if ( $this->tableCalls[$i][0] == 'tablecell_open' || $this->tableCalls[$i][0] == 'tableheader_open' ) {
13450cecf9d5Sandi
134644881bd0Shenning.noren                        if ( false !== $this->tableCalls[$i][1][0] ) {
13470cecf9d5Sandi                            $this->tableCalls[$i][1][0]++;
13480cecf9d5Sandi                            break;
13490cecf9d5Sandi                        }
13500cecf9d5Sandi
13510cecf9d5Sandi
13520cecf9d5Sandi                    }
13530cecf9d5Sandi                }
13540cecf9d5Sandi
13550cecf9d5Sandi                $toDelete[] = $key-1;
13560cecf9d5Sandi                $toDelete[] = $key;
13570cecf9d5Sandi                $toDelete[] = $key+1;
13580cecf9d5Sandi            }
13590cecf9d5Sandi        }
13600cecf9d5Sandi
13619ab75d9eSAndreas Gohr
13629ab75d9eSAndreas Gohr        // condense cdata
13639ab75d9eSAndreas Gohr        $cnt = count($this->tableCalls);
13649ab75d9eSAndreas Gohr        for( $key = 0; $key < $cnt; $key++){
13659ab75d9eSAndreas Gohr            if($this->tableCalls[$key][0] == 'cdata'){
13669ab75d9eSAndreas Gohr                $ckey = $key;
13679ab75d9eSAndreas Gohr                $key++;
13689ab75d9eSAndreas Gohr                while($this->tableCalls[$key][0] == 'cdata'){
13699ab75d9eSAndreas Gohr                    $this->tableCalls[$ckey][1][0] .= $this->tableCalls[$key][1][0];
13709ab75d9eSAndreas Gohr                    $toDelete[] = $key;
13719ab75d9eSAndreas Gohr                    $key++;
13729ab75d9eSAndreas Gohr                }
13739ab75d9eSAndreas Gohr                continue;
13749ab75d9eSAndreas Gohr            }
13759ab75d9eSAndreas Gohr        }
13769ab75d9eSAndreas Gohr
13770cecf9d5Sandi        foreach ( $toDelete as $delete ) {
13780cecf9d5Sandi            unset($this->tableCalls[$delete]);
13790cecf9d5Sandi        }
13800cecf9d5Sandi        $this->tableCalls = array_values($this->tableCalls);
13810cecf9d5Sandi    }
13820cecf9d5Sandi}
13830cecf9d5Sandi
13840cecf9d5Sandi//------------------------------------------------------------------------
13850cecf9d5Sandiclass Doku_Handler_Section {
13860cecf9d5Sandi
13870cecf9d5Sandi    function process($calls) {
13880cecf9d5Sandi
13890cecf9d5Sandi        $sectionCalls = array();
139044881bd0Shenning.noren        $inSection = false;
13910cecf9d5Sandi
13920cecf9d5Sandi        foreach ( $calls as $call ) {
13930cecf9d5Sandi
13940cecf9d5Sandi            if ( $call[0] == 'header' ) {
13950cecf9d5Sandi
13960cecf9d5Sandi                if ( $inSection ) {
13970cecf9d5Sandi                    $sectionCalls[] = array('section_close',array(), $call[2]);
13980cecf9d5Sandi                }
13990cecf9d5Sandi
14000cecf9d5Sandi                $sectionCalls[] = $call;
14010cecf9d5Sandi                $sectionCalls[] = array('section_open',array($call[1][1]), $call[2]);
140244881bd0Shenning.noren                $inSection = true;
14030cecf9d5Sandi
14040cecf9d5Sandi            } else {
1405e1c10e4dSchris
1406e1c10e4dSchris                if ($call[0] == 'section_open' )  {
140744881bd0Shenning.noren                    $inSection = true;
1408e1c10e4dSchris                } else if ($call[0] == 'section_open' ) {
140944881bd0Shenning.noren                    $inSection = false;
1410e1c10e4dSchris                }
14110cecf9d5Sandi                $sectionCalls[] = $call;
14120cecf9d5Sandi            }
14130cecf9d5Sandi        }
14140cecf9d5Sandi
14150cecf9d5Sandi        if ( $inSection ) {
14160cecf9d5Sandi            $sectionCalls[] = array('section_close',array(), $call[2]);
14170cecf9d5Sandi        }
14180cecf9d5Sandi
14190cecf9d5Sandi        return $sectionCalls;
14200cecf9d5Sandi    }
14210cecf9d5Sandi
14220cecf9d5Sandi}
14230cecf9d5Sandi
14242a27e99aSandi/**
14252a27e99aSandi * Handler for paragraphs
14262a27e99aSandi *
14270b7c14c2Sandi * @author Harry Fuecks <hfuecks@gmail.com>
14282a27e99aSandi */
14290cecf9d5Sandiclass Doku_Handler_Block {
14300cecf9d5Sandi
14310cecf9d5Sandi    var $calls = array();
14320cecf9d5Sandi
14330cecf9d5Sandi    var $blockStack = array();
14340cecf9d5Sandi
143544881bd0Shenning.noren    var $inParagraph = false;
143644881bd0Shenning.noren    var $atStart = true;
143758b56c06Sandi    var $skipEolKey = -1;
14380cecf9d5Sandi
1439af146da0Sandi    // Blocks these should not be inside paragraphs
14400cecf9d5Sandi    var $blockOpen = array(
14410cecf9d5Sandi            'header',
1442df9add72Schris            'listu_open','listo_open','listitem_open','listcontent_open',
14430cecf9d5Sandi            'table_open','tablerow_open','tablecell_open','tableheader_open',
14440cecf9d5Sandi            'quote_open',
14450cecf9d5Sandi            'section_open', // Needed to prevent p_open between header and section_open
144676aa94b7Schris            'code','file','hr','preformatted','rss',
144707f89c3cSAnika Henke            'htmlblock','phpblock',
14480cecf9d5Sandi        );
14490cecf9d5Sandi
14500cecf9d5Sandi    var $blockClose = array(
14510cecf9d5Sandi            'header',
1452df9add72Schris            'listu_close','listo_close','listitem_close','listcontent_close',
14530cecf9d5Sandi            'table_close','tablerow_close','tablecell_close','tableheader_close',
14540cecf9d5Sandi            'quote_close',
14550cecf9d5Sandi            'section_close', // Needed to prevent p_close after section_close
145676aa94b7Schris            'code','file','hr','preformatted','rss',
145707f89c3cSAnika Henke            'htmlblock','phpblock',
14580cecf9d5Sandi        );
14590cecf9d5Sandi
1460af146da0Sandi    // Stacks can contain paragraphs
14610cecf9d5Sandi    var $stackOpen = array(
14620cecf9d5Sandi        'footnote_open','section_open',
14630cecf9d5Sandi        );
14640cecf9d5Sandi
14650cecf9d5Sandi    var $stackClose = array(
14660cecf9d5Sandi        'footnote_close','section_close',
14670cecf9d5Sandi        );
14680cecf9d5Sandi
1469af146da0Sandi
1470af146da0Sandi    /**
1471af146da0Sandi     * Constructor. Adds loaded syntax plugins to the block and stack
1472af146da0Sandi     * arrays
1473af146da0Sandi     *
1474af146da0Sandi     * @author Andreas Gohr <andi@splitbrain.org>
1475af146da0Sandi     */
1476af146da0Sandi    function Doku_Handler_Block(){
1477af146da0Sandi        global $DOKU_PLUGINS;
1478af146da0Sandi        //check if syntax plugins were loaded
147903c4aec3Schris        if(empty($DOKU_PLUGINS['syntax'])) return;
1480af146da0Sandi        foreach($DOKU_PLUGINS['syntax'] as $n => $p){
1481af146da0Sandi            $ptype = $p->getPType();
1482af146da0Sandi            if($ptype == 'block'){
1483af146da0Sandi                $this->blockOpen[]  = 'plugin_'.$n;
1484af146da0Sandi                $this->blockClose[] = 'plugin_'.$n;
1485af146da0Sandi            }elseif($ptype == 'stack'){
1486af146da0Sandi                $this->stackOpen[]  = 'plugin_'.$n;
1487af146da0Sandi                $this->stackClose[] = 'plugin_'.$n;
1488af146da0Sandi            }
1489af146da0Sandi        }
1490af146da0Sandi    }
1491af146da0Sandi
14922a27e99aSandi    /**
14932a27e99aSandi     * Close a paragraph if needed
14942a27e99aSandi     *
14952a27e99aSandi     * This function makes sure there are no empty paragraphs on the stack
14962a27e99aSandi     *
14972a27e99aSandi     * @author Andreas Gohr <andi@splitbrain.org>
14982a27e99aSandi     */
1499506ae684Sandi    function closeParagraph($pos){
1500506ae684Sandi        // look back if there was any content - we don't want empty paragraphs
1501506ae684Sandi        $content = '';
1502506ae684Sandi        for($i=count($this->calls)-1; $i>=0; $i--){
1503506ae684Sandi            if($this->calls[$i][0] == 'p_open'){
1504506ae684Sandi                break;
1505506ae684Sandi            }elseif($this->calls[$i][0] == 'cdata'){
1506506ae684Sandi                $content .= $this->calls[$i][1][0];
1507506ae684Sandi            }else{
1508506ae684Sandi                $content = 'found markup';
1509506ae684Sandi                break;
1510506ae684Sandi            }
1511506ae684Sandi        }
1512506ae684Sandi
1513506ae684Sandi        if(trim($content)==''){
1514506ae684Sandi            //remove the whole paragraph
1515506ae684Sandi            array_splice($this->calls,$i);
1516506ae684Sandi        }else{
151729d015e3SBen Coburn            if ($this->calls[count($this->calls)-1][0] == 'section_edit') {
151829d015e3SBen Coburn                $tmp = array_pop($this->calls);
1519506ae684Sandi                $this->calls[] = array('p_close',array(), $pos);
152029d015e3SBen Coburn                $this->calls[] = $tmp;
152129d015e3SBen Coburn            } else {
152229d015e3SBen Coburn                $this->calls[] = array('p_close',array(), $pos);
152329d015e3SBen Coburn            }
1524506ae684Sandi        }
1525e1c10e4dSchris
152644881bd0Shenning.noren        $this->inParagraph = false;
1527506ae684Sandi    }
1528506ae684Sandi
15292a27e99aSandi    /**
15302a27e99aSandi     * Processes the whole instruction stack to open and close paragraphs
15312a27e99aSandi     *
15320b7c14c2Sandi     * @author Harry Fuecks <hfuecks@gmail.com>
15332a27e99aSandi     * @author Andreas Gohr <andi@splitbrain.org>
15342a27e99aSandi     * @todo   This thing is really messy and should be rewritten
15352a27e99aSandi     */
15360cecf9d5Sandi    function process($calls) {
15370cecf9d5Sandi        foreach ( $calls as $key => $call ) {
1538f0891737Sandi            $cname = $call[0];
1539e1c10e4dSchris            if($cname == 'plugin') {
1540e1c10e4dSchris                $cname='plugin_'.$call[1][0];
1541e1c10e4dSchris
1542e1c10e4dSchris                $plugin = true;
1543e1c10e4dSchris                $plugin_open = (($call[1][2] == DOKU_LEXER_ENTER) || ($call[1][2] == DOKU_LEXER_SPECIAL));
1544e1c10e4dSchris                $plugin_close = (($call[1][2] == DOKU_LEXER_EXIT) || ($call[1][2] == DOKU_LEXER_SPECIAL));
1545e1c10e4dSchris            } else {
1546e1c10e4dSchris                $plugin = false;
1547e1c10e4dSchris            }
15480cecf9d5Sandi
15490cecf9d5Sandi            // Process blocks which are stack like... (contain linefeeds)
1550e1c10e4dSchris            if ( in_array($cname,$this->stackOpen ) && (!$plugin || $plugin_open) ) {
1551e1c10e4dSchris
15520cecf9d5Sandi                $this->calls[] = $call;
15530cecf9d5Sandi
15540cecf9d5Sandi                // Hack - footnotes shouldn't immediately contain a p_open
1555f0891737Sandi                if ( $cname != 'footnote_open' ) {
15560cecf9d5Sandi                    $this->addToStack();
15570cecf9d5Sandi                } else {
155844881bd0Shenning.noren                    $this->addToStack(false);
15590cecf9d5Sandi                }
15600cecf9d5Sandi                continue;
15610cecf9d5Sandi            }
15620cecf9d5Sandi
1563e1c10e4dSchris            if ( in_array($cname,$this->stackClose ) && (!$plugin || $plugin_close)) {
15640cecf9d5Sandi
15650cecf9d5Sandi                if ( $this->inParagraph ) {
1566506ae684Sandi                    $this->closeParagraph($call[2]);
15670cecf9d5Sandi                }
15680cecf9d5Sandi                $this->calls[] = $call;
15690cecf9d5Sandi                $this->removeFromStack();
15700cecf9d5Sandi                continue;
15710cecf9d5Sandi            }
15720cecf9d5Sandi
15730cecf9d5Sandi            if ( !$this->atStart ) {
15740cecf9d5Sandi
1575f0891737Sandi                if ( $cname == 'eol' ) {
15760cecf9d5Sandi
1577e1c10e4dSchris                    // Check this isn't an eol instruction to skip...
157858b56c06Sandi                    if ( $this->skipEolKey != $key ) {
1579e1c10e4dSchris                        // Look to see if the next instruction is an EOL
158058b56c06Sandi                        if ( isset($calls[$key+1]) && $calls[$key+1][0] == 'eol' ) {
158158b56c06Sandi
158258b56c06Sandi                            if ( $this->inParagraph ) {
1583506ae684Sandi                                //$this->calls[] = array('p_close',array(), $call[2]);
1584506ae684Sandi                                $this->closeParagraph($call[2]);
158558b56c06Sandi                            }
158658b56c06Sandi
158758b56c06Sandi                            $this->calls[] = array('p_open',array(), $call[2]);
158844881bd0Shenning.noren                            $this->inParagraph = true;
158958b56c06Sandi
159058b56c06Sandi
1591e1c10e4dSchris                            // Mark the next instruction for skipping
159258b56c06Sandi                            $this->skipEolKey = $key+1;
159358b56c06Sandi
159458b56c06Sandi                        }else{
159558b56c06Sandi                            //if this is just a single eol make a space from it
159641624b31SChris Smith                            $this->addCall(array('cdata',array(DOKU_PARSER_EOL), $call[2]));
159758b56c06Sandi                        }
159858b56c06Sandi                    }
159958b56c06Sandi
16000cecf9d5Sandi
16010cecf9d5Sandi                } else {
16020cecf9d5Sandi
160344881bd0Shenning.noren                    $storeCall = true;
1604e1c10e4dSchris                    if ( $this->inParagraph && (in_array($cname, $this->blockOpen) && (!$plugin || $plugin_open))) {
1605506ae684Sandi                        $this->closeParagraph($call[2]);
16060cecf9d5Sandi                        $this->calls[] = $call;
160744881bd0Shenning.noren                        $storeCall = false;
16080cecf9d5Sandi                    }
16090cecf9d5Sandi
1610e1c10e4dSchris                    if ( in_array($cname, $this->blockClose) && (!$plugin || $plugin_close)) {
16110cecf9d5Sandi                        if ( $this->inParagraph ) {
1612506ae684Sandi                            $this->closeParagraph($call[2]);
16130cecf9d5Sandi                        }
16140cecf9d5Sandi                        if ( $storeCall ) {
16150cecf9d5Sandi                            $this->calls[] = $call;
161644881bd0Shenning.noren                            $storeCall = false;
16170cecf9d5Sandi                        }
16180cecf9d5Sandi
16190cecf9d5Sandi                        // This really sucks and suggests this whole class sucks but...
1620e1c10e4dSchris                        if ( isset($calls[$key+1])) {
1621e1c10e4dSchris                            $cname_plusone = $calls[$key+1][0];
1622e1c10e4dSchris                            if ($cname_plusone == 'plugin') {
1623e1c10e4dSchris                                $cname_plusone = 'plugin'.$calls[$key+1][1][0];
1624e1c10e4dSchris
1625e1c10e4dSchris                                // plugin test, true if plugin has a state which precludes it requiring blockOpen or blockClose
1626e1c10e4dSchris                                $plugin_plusone = true;
1627e1c10e4dSchris                                $plugin_test = ($call[$key+1][1][2] == DOKU_LEXER_MATCHED) || ($call[$key+1][1][2] == DOKU_LEXER_MATCHED);
1628e1c10e4dSchris                            } else {
1629e1c10e4dSchris                                $plugin_plusone = false;
1630e1c10e4dSchris                            }
1631e1c10e4dSchris                            if ((!in_array($cname_plusone, $this->blockOpen) && !in_array($cname_plusone, $this->blockClose)) ||
1632e1c10e4dSchris                                ($plugin_plusone && $plugin_test)
16330cecf9d5Sandi                                ) {
16340cecf9d5Sandi
16350cecf9d5Sandi                                $this->calls[] = array('p_open',array(), $call[2]);
163644881bd0Shenning.noren                                $this->inParagraph = true;
16370cecf9d5Sandi                            }
16380cecf9d5Sandi                        }
1639e1c10e4dSchris                    }
16400cecf9d5Sandi
16410cecf9d5Sandi                    if ( $storeCall ) {
164241624b31SChris Smith                        $this->addCall($call);
16430cecf9d5Sandi                    }
16440cecf9d5Sandi
16450cecf9d5Sandi                }
16460cecf9d5Sandi
16470cecf9d5Sandi
16480cecf9d5Sandi            } else {
16490cecf9d5Sandi
16500cecf9d5Sandi                // Unless there's already a block at the start, start a paragraph
1651f0891737Sandi                if ( !in_array($cname,$this->blockOpen) ) {
16520cecf9d5Sandi                    $this->calls[] = array('p_open',array(), $call[2]);
16530cecf9d5Sandi                    if ( $call[0] != 'eol' ) {
16540cecf9d5Sandi                        $this->calls[] = $call;
16550cecf9d5Sandi                    }
165644881bd0Shenning.noren                    $this->atStart = false;
165744881bd0Shenning.noren                    $this->inParagraph = true;
16580cecf9d5Sandi                } else {
165941624b31SChris Smith                    $this->addCall($call);
166044881bd0Shenning.noren                    $this->atStart = false;
16610cecf9d5Sandi                }
16620cecf9d5Sandi
16630cecf9d5Sandi            }
16640cecf9d5Sandi
16650cecf9d5Sandi        }
16660cecf9d5Sandi
16670cecf9d5Sandi        if ( $this->inParagraph ) {
1668f0891737Sandi            if ( $cname == 'p_open' ) {
16690cecf9d5Sandi                // Ditch the last call
16700cecf9d5Sandi                array_pop($this->calls);
1671f0891737Sandi            } else if ( !in_array($cname, $this->blockClose) ) {
1672506ae684Sandi                //$this->calls[] = array('p_close',array(), $call[2]);
1673506ae684Sandi                $this->closeParagraph($call[2]);
16740cecf9d5Sandi            } else {
16750cecf9d5Sandi                $last_call = array_pop($this->calls);
1676506ae684Sandi                //$this->calls[] = array('p_close',array(), $call[2]);
1677506ae684Sandi                $this->closeParagraph($call[2]);
16780cecf9d5Sandi                $this->calls[] = $last_call;
16790cecf9d5Sandi            }
16800cecf9d5Sandi        }
16810cecf9d5Sandi
16820cecf9d5Sandi        return $this->calls;
16830cecf9d5Sandi    }
16840cecf9d5Sandi
168544881bd0Shenning.noren    function addToStack($newStart = true) {
16860cecf9d5Sandi        $this->blockStack[] = array($this->atStart, $this->inParagraph);
16870cecf9d5Sandi        $this->atStart = $newStart;
168844881bd0Shenning.noren        $this->inParagraph = false;
16890cecf9d5Sandi    }
16900cecf9d5Sandi
16910cecf9d5Sandi    function removeFromStack() {
16920cecf9d5Sandi        $state = array_pop($this->blockStack);
16930cecf9d5Sandi        $this->atStart = $state[0];
16940cecf9d5Sandi        $this->inParagraph = $state[1];
16950cecf9d5Sandi    }
169641624b31SChris Smith
169741624b31SChris Smith    function addCall($call) {
169841624b31SChris Smith        $key = count($this->calls);
169941624b31SChris Smith        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
170041624b31SChris Smith            $this->calls[$key-1][1][0] .= $call[1][0];
170141624b31SChris Smith        } else {
170241624b31SChris Smith            $this->calls[] = $call;
170341624b31SChris Smith        }
170441624b31SChris Smith    }
17050cecf9d5Sandi}
17062a27e99aSandi
17074826ab45Sandi//Setup VIM: ex: et ts=4 enc=utf-8 :
1708