xref: /dokuwiki/inc/parser/handler.php (revision fa8adffee31ae8c6e5563badf3926e523032f5e2)
10cecf9d5Sandi<?php
2*fa8adffeSAndreas Gohrif(!defined('DOKU_INC')) die('meh.');
352fe2bfbSChris Smithif (!defined('DOKU_PARSER_EOL')) define('DOKU_PARSER_EOL',"\n");   // add this to make handling test cases simpler
4a9c1d2d2SChris Smith
50cecf9d5Sandiclass Doku_Handler {
60cecf9d5Sandi
70cecf9d5Sandi    var $Renderer = NULL;
80cecf9d5Sandi
90cecf9d5Sandi    var $CallWriter = NULL;
100cecf9d5Sandi
110cecf9d5Sandi    var $calls = array();
120cecf9d5Sandi
13e1c10e4dSchris    var $status = array(
1444881bd0Shenning.noren        'section' => false,
1535dae8b0SBen Coburn        'section_edit_start' => -1,
1635dae8b0SBen Coburn        'section_edit_level' => 1,
1735dae8b0SBen Coburn        'section_edit_title' => ''
180cecf9d5Sandi    );
190cecf9d5Sandi
2044881bd0Shenning.noren    var $rewriteBlocks = true;
21b7c441b9SHarry Fuecks
220cecf9d5Sandi    function Doku_Handler() {
230cecf9d5Sandi        $this->CallWriter = & new Doku_Handler_CallWriter($this);
240cecf9d5Sandi    }
250cecf9d5Sandi
26433bef32Sandi    function _addCall($handler, $args, $pos) {
270cecf9d5Sandi        $call = array($handler,$args, $pos);
280cecf9d5Sandi        $this->CallWriter->writeCall($call);
290cecf9d5Sandi    }
300cecf9d5Sandi
3182d61635Spierre.spring    function addPluginCall($plugin, $args, $state, $pos, $match) {
3282d61635Spierre.spring        $call = array('plugin',array($plugin, $args, $state, $match), $pos);
3304ebd214Schris        $this->CallWriter->writeCall($call);
3404ebd214Schris    }
3504ebd214Schris
36433bef32Sandi    function _finalize(){
37e1c10e4dSchris
38f4f02a0fSchris        $this->CallWriter->finalise();
39f4f02a0fSchris
40e1c10e4dSchris        if ( $this->status['section'] ) {
41e1c10e4dSchris           $last_call = end($this->calls);
42e1c10e4dSchris           array_push($this->calls,array('section_close',array(), $last_call[2]));
43b203781fSBen Coburn           if ($this->status['section_edit_start']>1) {
44b203781fSBen Coburn               // ignore last edit section if there is only one header
45b203781fSBen 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]));
46b203781fSBen Coburn           }
470cecf9d5Sandi        }
480cecf9d5Sandi
49b7c441b9SHarry Fuecks        if ( $this->rewriteBlocks ) {
500cecf9d5Sandi            $B = & new Doku_Handler_Block();
510cecf9d5Sandi            $this->calls = $B->process($this->calls);
52b7c441b9SHarry Fuecks        }
53e0ad864eSchris
5424bb549bSchris        trigger_event('PARSER_HANDLER_DONE',$this);
550cecf9d5Sandi
560cecf9d5Sandi        array_unshift($this->calls,array('document_start',array(),0));
570cecf9d5Sandi        $last_call = end($this->calls);
580cecf9d5Sandi        array_push($this->calls,array('document_end',array(),$last_call[2]));
590cecf9d5Sandi    }
600cecf9d5Sandi
610cecf9d5Sandi    function fetch() {
620cecf9d5Sandi        $call = each($this->calls);
630cecf9d5Sandi        if ( $call ) {
640cecf9d5Sandi            return $call['value'];
650cecf9d5Sandi        }
6644881bd0Shenning.noren        return false;
670cecf9d5Sandi    }
68ee20e7d1Sandi
69ee20e7d1Sandi
70ee20e7d1Sandi    /**
71ee20e7d1Sandi     * Special plugin handler
72ee20e7d1Sandi     *
73ee20e7d1Sandi     * This handler is called for all modes starting with 'plugin_'.
74ee20e7d1Sandi     * An additional parameter with the plugin name is passed
75ee20e7d1Sandi     *
76ee20e7d1Sandi     * @author Andreas Gohr <andi@splitbrain.org>
77ee20e7d1Sandi     */
78ee20e7d1Sandi    function plugin($match, $state, $pos, $pluginname){
79ee20e7d1Sandi        $data = array($match);
80a46d0d65SAndreas Gohr        $plugin =& plugin_load('syntax',$pluginname);
81a46d0d65SAndreas Gohr        if($plugin != null){
82f02a7d06Schris            $data = $plugin->handle($match, $state, $pos, $this);
83ee20e7d1Sandi        }
8413ecfb18SChris Smith        if ($data !== false) {
8582d61635Spierre.spring          $this->addPluginCall($pluginname,$data,$state,$pos,$match);
8613ecfb18SChris Smith        }
8744881bd0Shenning.noren        return true;
88ee20e7d1Sandi    }
890cecf9d5Sandi
900cecf9d5Sandi    function base($match, $state, $pos) {
910cecf9d5Sandi        switch ( $state ) {
920cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
93433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
9444881bd0Shenning.noren                return true;
950cecf9d5Sandi            break;
960cecf9d5Sandi        }
970cecf9d5Sandi    }
980cecf9d5Sandi
990cecf9d5Sandi    function header($match, $state, $pos) {
100b203781fSBen Coburn        global $conf;
101b203781fSBen Coburn
102d7e8115fSAndreas Gohr        // get level and title
103a4a2d4cfSAndreas Gohr        $title = trim($match);
104a4a2d4cfSAndreas Gohr        $level = 7 - strspn($title,'=');
105d7e8115fSAndreas Gohr        if($level < 1) $level = 1;
106a4a2d4cfSAndreas Gohr        $title = trim($title,'=');
107a4a2d4cfSAndreas Gohr        $title = trim($title);
1080cecf9d5Sandi
109e1c10e4dSchris        if ($this->status['section']) $this->_addCall('section_close',array(),$pos);
110e1c10e4dSchris
111b203781fSBen Coburn        if ($level<=$conf['maxseclevel']) {
11235dae8b0SBen Coburn            $this->_addCall('section_edit',array($this->status['section_edit_start'], $pos-1, $this->status['section_edit_level'], $this->status['section_edit_title']), $pos);
11335dae8b0SBen Coburn            $this->status['section_edit_start'] = $pos;
11435dae8b0SBen Coburn            $this->status['section_edit_level'] = $level;
11535dae8b0SBen Coburn            $this->status['section_edit_title'] = $title;
116b203781fSBen Coburn        }
11735dae8b0SBen Coburn
118433bef32Sandi        $this->_addCall('header',array($title,$level,$pos), $pos);
119e1c10e4dSchris
120e1c10e4dSchris        $this->_addCall('section_open',array($level),$pos);
12144881bd0Shenning.noren        $this->status['section'] = true;
12244881bd0Shenning.noren        return true;
1230cecf9d5Sandi    }
1240cecf9d5Sandi
1250cecf9d5Sandi    function notoc($match, $state, $pos) {
126e41c4da9SAndreas Gohr        $this->_addCall('notoc',array(),$pos);
12744881bd0Shenning.noren        return true;
1280cecf9d5Sandi    }
1290cecf9d5Sandi
1309dc2c2afSandi    function nocache($match, $state, $pos) {
1319dc2c2afSandi        $this->_addCall('nocache',array(),$pos);
13244881bd0Shenning.noren        return true;
1339dc2c2afSandi    }
1349dc2c2afSandi
1350cecf9d5Sandi    function linebreak($match, $state, $pos) {
136433bef32Sandi        $this->_addCall('linebreak',array(),$pos);
13744881bd0Shenning.noren        return true;
1380cecf9d5Sandi    }
1390cecf9d5Sandi
1400cecf9d5Sandi    function eol($match, $state, $pos) {
141433bef32Sandi        $this->_addCall('eol',array(),$pos);
14244881bd0Shenning.noren        return true;
1430cecf9d5Sandi    }
1440cecf9d5Sandi
1450cecf9d5Sandi    function hr($match, $state, $pos) {
146433bef32Sandi        $this->_addCall('hr',array(),$pos);
14744881bd0Shenning.noren        return true;
1480cecf9d5Sandi    }
1490cecf9d5Sandi
150433bef32Sandi    function _nestingTag($match, $state, $pos, $name) {
1510cecf9d5Sandi        switch ( $state ) {
1520cecf9d5Sandi            case DOKU_LEXER_ENTER:
153433bef32Sandi                $this->_addCall($name.'_open', array(), $pos);
1540cecf9d5Sandi            break;
1550cecf9d5Sandi            case DOKU_LEXER_EXIT:
156433bef32Sandi                $this->_addCall($name.'_close', array(), $pos);
1570cecf9d5Sandi            break;
1580cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
159433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
1600cecf9d5Sandi            break;
1610cecf9d5Sandi        }
1620cecf9d5Sandi    }
1630cecf9d5Sandi
1640cecf9d5Sandi    function strong($match, $state, $pos) {
165433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'strong');
16644881bd0Shenning.noren        return true;
1670cecf9d5Sandi    }
1680cecf9d5Sandi
1690cecf9d5Sandi    function emphasis($match, $state, $pos) {
170433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'emphasis');
17144881bd0Shenning.noren        return true;
1720cecf9d5Sandi    }
1730cecf9d5Sandi
1740cecf9d5Sandi    function underline($match, $state, $pos) {
175433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'underline');
17644881bd0Shenning.noren        return true;
1770cecf9d5Sandi    }
1780cecf9d5Sandi
1790cecf9d5Sandi    function monospace($match, $state, $pos) {
180433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'monospace');
18144881bd0Shenning.noren        return true;
1820cecf9d5Sandi    }
1830cecf9d5Sandi
1840cecf9d5Sandi    function subscript($match, $state, $pos) {
185433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'subscript');
18644881bd0Shenning.noren        return true;
1870cecf9d5Sandi    }
1880cecf9d5Sandi
1890cecf9d5Sandi    function superscript($match, $state, $pos) {
190433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'superscript');
19144881bd0Shenning.noren        return true;
1920cecf9d5Sandi    }
1930cecf9d5Sandi
1940cecf9d5Sandi    function deleted($match, $state, $pos) {
195433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'deleted');
19644881bd0Shenning.noren        return true;
1970cecf9d5Sandi    }
1980cecf9d5Sandi
1990cecf9d5Sandi
2000cecf9d5Sandi    function footnote($match, $state, $pos) {
2015587e44cSchris//        $this->_nestingTag($match, $state, $pos, 'footnote');
202742c66f8Schris        if (!isset($this->_footnote)) $this->_footnote = false;
2032fe7363dSchris
2045587e44cSchris        switch ( $state ) {
2055587e44cSchris            case DOKU_LEXER_ENTER:
2062fe7363dSchris                // footnotes can not be nested - however due to limitations in lexer it can't be prevented
2072fe7363dSchris                // we will still enter a new footnote mode, we just do nothing
208742c66f8Schris                if ($this->_footnote) {
2092fe7363dSchris                  $this->_addCall('cdata',array($match), $pos);
2102fe7363dSchris                  break;
2112fe7363dSchris                }
2122fe7363dSchris
213742c66f8Schris                $this->_footnote = true;
2142fe7363dSchris
2155587e44cSchris                $ReWriter = & new Doku_Handler_Nest($this->CallWriter,'footnote_close');
2165587e44cSchris                $this->CallWriter = & $ReWriter;
2174a26ad85Schris                $this->_addCall('footnote_open', array(), $pos);
2185587e44cSchris            break;
2195587e44cSchris            case DOKU_LEXER_EXIT:
2202fe7363dSchris                // check whether we have already exitted the footnote mode, can happen if the modes were nested
221742c66f8Schris                if (!$this->_footnote) {
2222fe7363dSchris                  $this->_addCall('cdata',array($match), $pos);
2232fe7363dSchris                  break;
2242fe7363dSchris                }
2252fe7363dSchris
226742c66f8Schris                $this->_footnote = false;
2272fe7363dSchris
2285587e44cSchris                $this->_addCall('footnote_close', array(), $pos);
2295587e44cSchris                $this->CallWriter->process();
2305587e44cSchris                $ReWriter = & $this->CallWriter;
2315587e44cSchris                $this->CallWriter = & $ReWriter->CallWriter;
2325587e44cSchris            break;
2335587e44cSchris            case DOKU_LEXER_UNMATCHED:
2345587e44cSchris                $this->_addCall('cdata', array($match), $pos);
2355587e44cSchris            break;
2365587e44cSchris        }
23744881bd0Shenning.noren        return true;
2380cecf9d5Sandi    }
2390cecf9d5Sandi
2400cecf9d5Sandi    function listblock($match, $state, $pos) {
2410cecf9d5Sandi        switch ( $state ) {
2420cecf9d5Sandi            case DOKU_LEXER_ENTER:
2430cecf9d5Sandi                $ReWriter = & new Doku_Handler_List($this->CallWriter);
2440cecf9d5Sandi                $this->CallWriter = & $ReWriter;
245433bef32Sandi                $this->_addCall('list_open', array($match), $pos);
2460cecf9d5Sandi            break;
2470cecf9d5Sandi            case DOKU_LEXER_EXIT:
248433bef32Sandi                $this->_addCall('list_close', array(), $pos);
2490cecf9d5Sandi                $this->CallWriter->process();
2500cecf9d5Sandi                $ReWriter = & $this->CallWriter;
2510cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
2520cecf9d5Sandi            break;
2530cecf9d5Sandi            case DOKU_LEXER_MATCHED:
254433bef32Sandi                $this->_addCall('list_item', array($match), $pos);
2550cecf9d5Sandi            break;
2560cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
257433bef32Sandi                $this->_addCall('cdata', array($match), $pos);
2580cecf9d5Sandi            break;
2590cecf9d5Sandi        }
26044881bd0Shenning.noren        return true;
2610cecf9d5Sandi    }
2620cecf9d5Sandi
2630cecf9d5Sandi    function unformatted($match, $state, $pos) {
2640cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
265433bef32Sandi            $this->_addCall('unformatted',array($match), $pos);
2660cecf9d5Sandi        }
26744881bd0Shenning.noren        return true;
2680cecf9d5Sandi    }
2690cecf9d5Sandi
2700cecf9d5Sandi    function php($match, $state, $pos) {
271df9add72Schris        global $conf;
2720cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
273433bef32Sandi            $this->_addCall('php',array($match), $pos);
2740cecf9d5Sandi        }
27544881bd0Shenning.noren        return true;
2760cecf9d5Sandi    }
2770cecf9d5Sandi
27807f89c3cSAnika Henke    function phpblock($match, $state, $pos) {
27907f89c3cSAnika Henke        global $conf;
28007f89c3cSAnika Henke        if ( $state == DOKU_LEXER_UNMATCHED ) {
28107f89c3cSAnika Henke            $this->_addCall('phpblock',array($match), $pos);
28207f89c3cSAnika Henke        }
28307f89c3cSAnika Henke        return true;
28407f89c3cSAnika Henke    }
28507f89c3cSAnika Henke
2860cecf9d5Sandi    function html($match, $state, $pos) {
287df9add72Schris        global $conf;
2880cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
289433bef32Sandi            $this->_addCall('html',array($match), $pos);
2900cecf9d5Sandi        }
29144881bd0Shenning.noren        return true;
2920cecf9d5Sandi    }
2930cecf9d5Sandi
29407f89c3cSAnika Henke    function htmlblock($match, $state, $pos) {
29507f89c3cSAnika Henke        global $conf;
29607f89c3cSAnika Henke        if ( $state == DOKU_LEXER_UNMATCHED ) {
29707f89c3cSAnika Henke            $this->_addCall('htmlblock',array($match), $pos);
29807f89c3cSAnika Henke        }
29907f89c3cSAnika Henke        return true;
30007f89c3cSAnika Henke    }
30107f89c3cSAnika Henke
3020cecf9d5Sandi    function preformatted($match, $state, $pos) {
3030cecf9d5Sandi        switch ( $state ) {
3040cecf9d5Sandi            case DOKU_LEXER_ENTER:
3050cecf9d5Sandi                $ReWriter = & new Doku_Handler_Preformatted($this->CallWriter);
3060cecf9d5Sandi                $this->CallWriter = & $ReWriter;
307433bef32Sandi                $this->_addCall('preformatted_start',array(), $pos);
3080cecf9d5Sandi            break;
3090cecf9d5Sandi            case DOKU_LEXER_EXIT:
310433bef32Sandi                $this->_addCall('preformatted_end',array(), $pos);
3110cecf9d5Sandi                $this->CallWriter->process();
3120cecf9d5Sandi                $ReWriter = & $this->CallWriter;
3130cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
3140cecf9d5Sandi            break;
3150cecf9d5Sandi            case DOKU_LEXER_MATCHED:
316433bef32Sandi                $this->_addCall('preformatted_newline',array(), $pos);
3170cecf9d5Sandi            break;
3180cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
319433bef32Sandi                $this->_addCall('preformatted_content',array($match), $pos);
3200cecf9d5Sandi            break;
3210cecf9d5Sandi        }
3220cecf9d5Sandi
32344881bd0Shenning.noren        return true;
3240cecf9d5Sandi    }
3250cecf9d5Sandi
3260cecf9d5Sandi    function file($match, $state, $pos) {
3270cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
328433bef32Sandi            $this->_addCall('file',array($match), $pos);
3290cecf9d5Sandi        }
33044881bd0Shenning.noren        return true;
3310cecf9d5Sandi    }
3320cecf9d5Sandi
3330cecf9d5Sandi    function quote($match, $state, $pos) {
3340cecf9d5Sandi
3350cecf9d5Sandi        switch ( $state ) {
3360cecf9d5Sandi
3370cecf9d5Sandi            case DOKU_LEXER_ENTER:
3380cecf9d5Sandi                $ReWriter = & new Doku_Handler_Quote($this->CallWriter);
3390cecf9d5Sandi                $this->CallWriter = & $ReWriter;
340433bef32Sandi                $this->_addCall('quote_start',array($match), $pos);
3410cecf9d5Sandi            break;
3420cecf9d5Sandi
3430cecf9d5Sandi            case DOKU_LEXER_EXIT:
344433bef32Sandi                $this->_addCall('quote_end',array(), $pos);
3450cecf9d5Sandi                $this->CallWriter->process();
3460cecf9d5Sandi                $ReWriter = & $this->CallWriter;
3470cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
3480cecf9d5Sandi            break;
3490cecf9d5Sandi
3500cecf9d5Sandi            case DOKU_LEXER_MATCHED:
351433bef32Sandi                $this->_addCall('quote_newline',array($match), $pos);
3520cecf9d5Sandi            break;
3530cecf9d5Sandi
3540cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
355433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
3560cecf9d5Sandi            break;
3570cecf9d5Sandi
3580cecf9d5Sandi        }
3590cecf9d5Sandi
36044881bd0Shenning.noren        return true;
3610cecf9d5Sandi    }
3620cecf9d5Sandi
3630cecf9d5Sandi    function code($match, $state, $pos) {
3640cecf9d5Sandi        switch ( $state ) {
3650cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
3660cecf9d5Sandi                $matches = preg_split('/>/u',$match,2);
3670cecf9d5Sandi                $matches[0] = trim($matches[0]);
3680cecf9d5Sandi                if ( trim($matches[0]) == '' ) {
3690cecf9d5Sandi                    $matches[0] = NULL;
3700cecf9d5Sandi                }
3710cecf9d5Sandi                # $matches[0] contains name of programming language
372b5bef19bSandi                # if available, We shortcut html here.
373b5bef19bSandi                if($matches[0] == 'html') $matches[0] = 'html4strict';
374433bef32Sandi                $this->_addCall(
3750cecf9d5Sandi                        'code',
3760cecf9d5Sandi                        array($matches[1],$matches[0]),
3770cecf9d5Sandi                        $pos
3780cecf9d5Sandi                    );
3790cecf9d5Sandi            break;
3800cecf9d5Sandi        }
38144881bd0Shenning.noren        return true;
3820cecf9d5Sandi    }
3830cecf9d5Sandi
3840cecf9d5Sandi    function acronym($match, $state, $pos) {
385433bef32Sandi        $this->_addCall('acronym',array($match), $pos);
38644881bd0Shenning.noren        return true;
3870cecf9d5Sandi    }
3880cecf9d5Sandi
3890cecf9d5Sandi    function smiley($match, $state, $pos) {
390433bef32Sandi        $this->_addCall('smiley',array($match), $pos);
39144881bd0Shenning.noren        return true;
3920cecf9d5Sandi    }
3930cecf9d5Sandi
3940cecf9d5Sandi    function wordblock($match, $state, $pos) {
395433bef32Sandi        $this->_addCall('wordblock',array($match), $pos);
39644881bd0Shenning.noren        return true;
3970cecf9d5Sandi    }
3980cecf9d5Sandi
3990cecf9d5Sandi    function entity($match, $state, $pos) {
400433bef32Sandi        $this->_addCall('entity',array($match), $pos);
40144881bd0Shenning.noren        return true;
4020cecf9d5Sandi    }
4030cecf9d5Sandi
4040cecf9d5Sandi    function multiplyentity($match, $state, $pos) {
4050cecf9d5Sandi        preg_match_all('/\d+/',$match,$matches);
406433bef32Sandi        $this->_addCall('multiplyentity',array($matches[0][0],$matches[0][1]), $pos);
40744881bd0Shenning.noren        return true;
4080cecf9d5Sandi    }
4090cecf9d5Sandi
4100cecf9d5Sandi    function singlequoteopening($match, $state, $pos) {
411433bef32Sandi        $this->_addCall('singlequoteopening',array(), $pos);
41244881bd0Shenning.noren        return true;
4130cecf9d5Sandi    }
4140cecf9d5Sandi
4150cecf9d5Sandi    function singlequoteclosing($match, $state, $pos) {
416433bef32Sandi        $this->_addCall('singlequoteclosing',array(), $pos);
41744881bd0Shenning.noren        return true;
4180cecf9d5Sandi    }
4190cecf9d5Sandi
42057d757d1SAndreas Gohr    function apostrophe($match, $state, $pos) {
42157d757d1SAndreas Gohr        $this->_addCall('apostrophe',array(), $pos);
42257d757d1SAndreas Gohr        return true;
42357d757d1SAndreas Gohr    }
42457d757d1SAndreas Gohr
4250cecf9d5Sandi    function doublequoteopening($match, $state, $pos) {
426433bef32Sandi        $this->_addCall('doublequoteopening',array(), $pos);
42744881bd0Shenning.noren        return true;
4280cecf9d5Sandi    }
4290cecf9d5Sandi
4300cecf9d5Sandi    function doublequoteclosing($match, $state, $pos) {
431433bef32Sandi        $this->_addCall('doublequoteclosing',array(), $pos);
43244881bd0Shenning.noren        return true;
4330cecf9d5Sandi    }
4340cecf9d5Sandi
4350cecf9d5Sandi    function camelcaselink($match, $state, $pos) {
436433bef32Sandi        $this->_addCall('camelcaselink',array($match), $pos);
43744881bd0Shenning.noren        return true;
4380cecf9d5Sandi    }
4390cecf9d5Sandi
4400cecf9d5Sandi    /*
4410cecf9d5Sandi    */
4420cecf9d5Sandi    function internallink($match, $state, $pos) {
4430cecf9d5Sandi        // Strip the opening and closing markup
4440cecf9d5Sandi        $link = preg_replace(array('/^\[\[/','/\]\]$/u'),'',$match);
4450cecf9d5Sandi
4460cecf9d5Sandi        // Split title from URL
4470cecf9d5Sandi        $link = preg_split('/\|/u',$link,2);
4480cecf9d5Sandi        if ( !isset($link[1]) ) {
4490cecf9d5Sandi            $link[1] = NULL;
4500cecf9d5Sandi        } else if ( preg_match('/^\{\{[^\}]+\}\}$/',$link[1]) ) {
4515578eb8fSandi            // If the title is an image, convert it to an array containing the image details
452b625487dSandi            $link[1] = Doku_Handler_Parse_Media($link[1]);
4530cecf9d5Sandi        }
4540b7c14c2Sandi        $link[0] = trim($link[0]);
4550cecf9d5Sandi
4560e1c636eSandi        //decide which kind of link it is
4570e1c636eSandi
458e08dda3fSAndreas Gohr        if ( preg_match('/^[a-zA-Z0-9\.]+>{1}.*$/u',$link[0]) ) {
4590e1c636eSandi        // Interwiki
4600cecf9d5Sandi            $interwiki = preg_split('/>/u',$link[0]);
461433bef32Sandi            $this->_addCall(
4620cecf9d5Sandi                'interwikilink',
4630cecf9d5Sandi                array($link[0],$link[1],strtolower($interwiki[0]),$interwiki[1]),
4640cecf9d5Sandi                $pos
4650cecf9d5Sandi                );
4660b7c14c2Sandi        }elseif ( preg_match('/^\\\\\\\\[\w.:?\-;,]+?\\\\/u',$link[0]) ) {
4670e1c636eSandi        // Windows Share
468433bef32Sandi            $this->_addCall(
4690cecf9d5Sandi                'windowssharelink',
4700cecf9d5Sandi                array($link[0],$link[1]),
4710cecf9d5Sandi                $pos
4720cecf9d5Sandi                );
4734468cb4cSAndreas Gohr        }elseif ( preg_match('#^([a-z0-9\-\.+]+?)://#i',$link[0]) ) {
4740e1c636eSandi        // external link (accepts all protocols)
475433bef32Sandi            $this->_addCall(
4760cecf9d5Sandi                    'externallink',
4770cecf9d5Sandi                    array($link[0],$link[1]),
4780cecf9d5Sandi                    $pos
4790cecf9d5Sandi                    );
4800a1d30bfSchris        }elseif ( preg_match('<'.PREG_PATTERN_VALID_EMAIL.'>',$link[0]) ) {
4810a1d30bfSchris        // E-Mail (pattern above is defined in inc/mail.php)
482a6755281Sandi            $this->_addCall(
483a6755281Sandi                'emaillink',
484a6755281Sandi                array($link[0],$link[1]),
485a6755281Sandi                $pos
486a6755281Sandi                );
4870b7c14c2Sandi        }elseif ( preg_match('!^#.+!',$link[0]) ){
4880b7c14c2Sandi        // local link
4890b7c14c2Sandi            $this->_addCall(
4900b7c14c2Sandi                'locallink',
4910b7c14c2Sandi                array(substr($link[0],1),$link[1]),
4920b7c14c2Sandi                $pos
4930b7c14c2Sandi                );
4940e1c636eSandi        }else{
4950e1c636eSandi        // internal link
496433bef32Sandi            $this->_addCall(
4970e1c636eSandi                'internallink',
4980e1c636eSandi                array($link[0],$link[1]),
4990e1c636eSandi                $pos
5000e1c636eSandi                );
5010cecf9d5Sandi        }
5020e1c636eSandi
50344881bd0Shenning.noren        return true;
5040cecf9d5Sandi    }
5050cecf9d5Sandi
5060cecf9d5Sandi    function filelink($match, $state, $pos) {
507433bef32Sandi        $this->_addCall('filelink',array($match, NULL), $pos);
50844881bd0Shenning.noren        return true;
5090cecf9d5Sandi    }
5100cecf9d5Sandi
5110cecf9d5Sandi    function windowssharelink($match, $state, $pos) {
512433bef32Sandi        $this->_addCall('windowssharelink',array($match, NULL), $pos);
51344881bd0Shenning.noren        return true;
5140cecf9d5Sandi    }
5150cecf9d5Sandi
5160cecf9d5Sandi    function media($match, $state, $pos) {
5170cecf9d5Sandi        $p = Doku_Handler_Parse_Media($match);
5180cecf9d5Sandi
519433bef32Sandi        $this->_addCall(
5200cecf9d5Sandi              $p['type'],
521dc673a5bSjoe.lapp              array($p['src'], $p['title'], $p['align'], $p['width'],
522dc673a5bSjoe.lapp                     $p['height'], $p['cache'], $p['linking']),
5230cecf9d5Sandi              $pos
5240cecf9d5Sandi             );
52544881bd0Shenning.noren        return true;
5260cecf9d5Sandi    }
5270cecf9d5Sandi
528b625487dSandi    function rss($match, $state, $pos) {
529b625487dSandi        $link = preg_replace(array('/^\{\{rss>/','/\}\}$/'),'',$match);
5303db95becSAndreas Gohr
5313db95becSAndreas Gohr        // get params
5323db95becSAndreas Gohr        list($link,$params) = explode(' ',$link,2);
5333db95becSAndreas Gohr
5343db95becSAndreas Gohr        $p = array();
5353db95becSAndreas Gohr        if(preg_match('/\b(\d+)\b/',$params,$match)){
5363db95becSAndreas Gohr            $p['max'] = $match[1];
5373db95becSAndreas Gohr        }else{
5383db95becSAndreas Gohr            $p['max'] = 8;
5393db95becSAndreas Gohr        }
5403db95becSAndreas Gohr        $p['reverse'] = (preg_match('/rev/',$params));
5413db95becSAndreas Gohr        $p['author']  = (preg_match('/\b(by|author)/',$params));
5423db95becSAndreas Gohr        $p['date']    = (preg_match('/\b(date)/',$params));
5433db95becSAndreas Gohr        $p['details'] = (preg_match('/\b(desc|detail)/',$params));
5443db95becSAndreas Gohr
5450a69dff7Schris        if (preg_match('/\b(\d+)([dhm])\b/',$params,$match)) {
5460a69dff7Schris          $period = array('d' => 86400, 'h' => 3600, 'm' => 60);
5470a69dff7Schris          $p['refresh'] = max(600,$match[1]*$period[$match[2]]);  // n * period in seconds, minimum 10 minutes
5480a69dff7Schris        } else {
5490a69dff7Schris          $p['refresh'] = 14400;   // default to 4 hours
5500a69dff7Schris        }
5510a69dff7Schris
5523db95becSAndreas Gohr        $this->_addCall('rss',array($link,$p),$pos);
55344881bd0Shenning.noren        return true;
554b625487dSandi    }
555b625487dSandi
5560cecf9d5Sandi    function externallink($match, $state, $pos) {
557da9f31c5SAndreas Gohr        $url   = $match;
558da9f31c5SAndreas Gohr        $title = null;
5590cecf9d5Sandi
560da9f31c5SAndreas Gohr        // add protocol on simple short URLs
561da9f31c5SAndreas Gohr        if(substr($url,0,3) == 'ftp' && (substr($url,0,6) != 'ftp://')){
562da9f31c5SAndreas Gohr            $title = $url;
563da9f31c5SAndreas Gohr            $url   = 'ftp://'.$url;
564da9f31c5SAndreas Gohr        }
565da9f31c5SAndreas Gohr        if(substr($url,0,3) == 'www' && (substr($url,0,7) != 'http://')){
566da9f31c5SAndreas Gohr            $title = $url;
567da9f31c5SAndreas Gohr            $url = 'http://'.$url;
568da9f31c5SAndreas Gohr        }
569da9f31c5SAndreas Gohr
570da9f31c5SAndreas Gohr        $this->_addCall('externallink',array($url, $title), $pos);
57144881bd0Shenning.noren        return true;
5720cecf9d5Sandi    }
5730cecf9d5Sandi
57471352defSandi    function emaillink($match, $state, $pos) {
5750cecf9d5Sandi        $email = preg_replace(array('/^</','/>$/'),'',$match);
576433bef32Sandi        $this->_addCall('emaillink',array($email, NULL), $pos);
57744881bd0Shenning.noren        return true;
5780cecf9d5Sandi    }
5790cecf9d5Sandi
5800cecf9d5Sandi    function table($match, $state, $pos) {
5810cecf9d5Sandi        switch ( $state ) {
5820cecf9d5Sandi
5830cecf9d5Sandi            case DOKU_LEXER_ENTER:
5840cecf9d5Sandi
5850cecf9d5Sandi                $ReWriter = & new Doku_Handler_Table($this->CallWriter);
5860cecf9d5Sandi                $this->CallWriter = & $ReWriter;
5870cecf9d5Sandi
588433bef32Sandi                $this->_addCall('table_start', array(), $pos);
589433bef32Sandi                //$this->_addCall('table_row', array(), $pos);
5900cecf9d5Sandi                if ( trim($match) == '^' ) {
591433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
5920cecf9d5Sandi                } else {
593433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
5940cecf9d5Sandi                }
5950cecf9d5Sandi            break;
5960cecf9d5Sandi
5970cecf9d5Sandi            case DOKU_LEXER_EXIT:
598433bef32Sandi                $this->_addCall('table_end', array(), $pos);
5990cecf9d5Sandi                $this->CallWriter->process();
6000cecf9d5Sandi                $ReWriter = & $this->CallWriter;
6010cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
6020cecf9d5Sandi            break;
6030cecf9d5Sandi
6040cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
6050cecf9d5Sandi                if ( trim($match) != '' ) {
606433bef32Sandi                    $this->_addCall('cdata',array($match), $pos);
6070cecf9d5Sandi                }
6080cecf9d5Sandi            break;
6090cecf9d5Sandi
6100cecf9d5Sandi            case DOKU_LEXER_MATCHED:
6119ab75d9eSAndreas Gohr                if ( $match == ' ' ){
6129ab75d9eSAndreas Gohr                    $this->_addCall('cdata', array($match), $pos);
613e205b721SAndreas Gohr                } else if ( preg_match('/\t+/',$match) ) {
6149ab75d9eSAndreas Gohr                    $this->_addCall('table_align', array($match), $pos);
615e205b721SAndreas Gohr                } else if ( preg_match('/ {2,}/',$match) ) {
616433bef32Sandi                    $this->_addCall('table_align', array($match), $pos);
6170cecf9d5Sandi                } else if ( $match == "\n|" ) {
618433bef32Sandi                    $this->_addCall('table_row', array(), $pos);
619433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
6200cecf9d5Sandi                } else if ( $match == "\n^" ) {
621433bef32Sandi                    $this->_addCall('table_row', array(), $pos);
622433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
6230cecf9d5Sandi                } else if ( $match == '|' ) {
624433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
6250cecf9d5Sandi                } else if ( $match == '^' ) {
626433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
6270cecf9d5Sandi                }
6280cecf9d5Sandi            break;
6290cecf9d5Sandi        }
63044881bd0Shenning.noren        return true;
6310cecf9d5Sandi    }
6320cecf9d5Sandi}
6330cecf9d5Sandi
6340cecf9d5Sandi//------------------------------------------------------------------------
6350cecf9d5Sandifunction Doku_Handler_Parse_Media($match) {
6360cecf9d5Sandi
6370cecf9d5Sandi    // Strip the opening and closing markup
6380cecf9d5Sandi    $link = preg_replace(array('/^\{\{/','/\}\}$/u'),'',$match);
6390cecf9d5Sandi
6400cecf9d5Sandi    // Split title from URL
6410cecf9d5Sandi    $link = preg_split('/\|/u',$link,2);
6420cecf9d5Sandi
6430cecf9d5Sandi
6440cecf9d5Sandi    // Check alignment
6450cecf9d5Sandi    $ralign = (bool)preg_match('/^ /',$link[0]);
6460cecf9d5Sandi    $lalign = (bool)preg_match('/ $/',$link[0]);
6470cecf9d5Sandi
6480cecf9d5Sandi    // Logic = what's that ;)...
6490cecf9d5Sandi    if ( $lalign & $ralign ) {
6500cecf9d5Sandi        $align = 'center';
6510cecf9d5Sandi    } else if ( $ralign ) {
6520cecf9d5Sandi        $align = 'right';
6530cecf9d5Sandi    } else if ( $lalign ) {
6540cecf9d5Sandi        $align = 'left';
6550cecf9d5Sandi    } else {
6560cecf9d5Sandi        $align = NULL;
6570cecf9d5Sandi    }
6580cecf9d5Sandi
6590cecf9d5Sandi    // The title...
6600cecf9d5Sandi    if ( !isset($link[1]) ) {
6610cecf9d5Sandi        $link[1] = NULL;
6620cecf9d5Sandi    }
6630cecf9d5Sandi
6644826ab45Sandi    //remove aligning spaces
6654826ab45Sandi    $link[0] = trim($link[0]);
6660cecf9d5Sandi
6674826ab45Sandi    //split into src and parameters (using the very last questionmark)
6684826ab45Sandi    $pos = strrpos($link[0], '?');
6694826ab45Sandi    if($pos !== false){
6704826ab45Sandi        $src   = substr($link[0],0,$pos);
6714826ab45Sandi        $param = substr($link[0],$pos+1);
6720cecf9d5Sandi    }else{
6734826ab45Sandi        $src   = $link[0];
6744826ab45Sandi        $param = '';
6750cecf9d5Sandi    }
6760cecf9d5Sandi
6774826ab45Sandi    //parse width and height
6784826ab45Sandi    if(preg_match('#(\d+)(x(\d+))?#i',$param,$size)){
6794826ab45Sandi        ($size[1]) ? $w = $size[1] : $w = NULL;
6804826ab45Sandi        ($size[3]) ? $h = $size[3] : $h = NULL;
681fc1c55b1Shfuecks    } else {
682fc1c55b1Shfuecks        $w = NULL;
683fc1c55b1Shfuecks        $h = NULL;
6840cecf9d5Sandi    }
6850cecf9d5Sandi
686dc673a5bSjoe.lapp    //get linking command
687d35ab615Shenning.noren    if(preg_match('/nolink/i',$param)){
688dc673a5bSjoe.lapp        $linking = 'nolink';
689d35ab615Shenning.noren    }else if(preg_match('/direct/i',$param)){
690dc673a5bSjoe.lapp        $linking = 'direct';
6918acb3108SAndreas Gohr    }else if(preg_match('/linkonly/i',$param)){
6928acb3108SAndreas Gohr        $linking = 'linkonly';
693dc673a5bSjoe.lapp    }else{
694dc673a5bSjoe.lapp        $linking = 'details';
695dc673a5bSjoe.lapp    }
696dc673a5bSjoe.lapp
6974826ab45Sandi    //get caching command
6984826ab45Sandi    if (preg_match('/(nocache|recache)/i',$param,$cachemode)){
6994826ab45Sandi        $cache = $cachemode[1];
7000cecf9d5Sandi    }else{
7014826ab45Sandi        $cache = 'cache';
7020cecf9d5Sandi    }
7030cecf9d5Sandi
7040cecf9d5Sandi    // Check whether this is a local or remote image
7054826ab45Sandi    if ( preg_match('#^(https?|ftp)#i',$src) ) {
7064826ab45Sandi        $call = 'externalmedia';
7070cecf9d5Sandi    } else {
7084826ab45Sandi        $call = 'internalmedia';
7090cecf9d5Sandi    }
7100cecf9d5Sandi
7110cecf9d5Sandi    $params = array(
7120cecf9d5Sandi        'type'=>$call,
7134826ab45Sandi        'src'=>$src,
7140cecf9d5Sandi        'title'=>$link[1],
7150cecf9d5Sandi        'align'=>$align,
7164826ab45Sandi        'width'=>$w,
7174826ab45Sandi        'height'=>$h,
7180cecf9d5Sandi        'cache'=>$cache,
719dc673a5bSjoe.lapp        'linking'=>$linking,
7200cecf9d5Sandi    );
7210cecf9d5Sandi
7220cecf9d5Sandi    return $params;
7230cecf9d5Sandi}
7240cecf9d5Sandi
7250cecf9d5Sandi//------------------------------------------------------------------------
7260cecf9d5Sandiclass Doku_Handler_CallWriter {
7270cecf9d5Sandi
7280cecf9d5Sandi    var $Handler;
7290cecf9d5Sandi
7300cecf9d5Sandi    function Doku_Handler_CallWriter(& $Handler) {
7310cecf9d5Sandi        $this->Handler = & $Handler;
7320cecf9d5Sandi    }
7330cecf9d5Sandi
7340cecf9d5Sandi    function writeCall($call) {
7350cecf9d5Sandi        $this->Handler->calls[] = $call;
7360cecf9d5Sandi    }
7370cecf9d5Sandi
7380cecf9d5Sandi    function writeCalls($calls) {
7390cecf9d5Sandi        $this->Handler->calls = array_merge($this->Handler->calls, $calls);
7400cecf9d5Sandi    }
741f4f02a0fSchris
742f4f02a0fSchris    // function is required, but since this call writer is first/highest in
743f4f02a0fSchris    // the chain it is not required to do anything
744f4f02a0fSchris    function finalise() {
745f4f02a0fSchris    }
7460cecf9d5Sandi}
7470cecf9d5Sandi
7480cecf9d5Sandi//------------------------------------------------------------------------
7495587e44cSchris/**
7505587e44cSchris * Generic call writer class to handle nesting of rendering instructions
7515587e44cSchris * within a render instruction. Also see nest() method of renderer base class
7525587e44cSchris *
7535587e44cSchris * @author    Chris Smith <chris@jalakai.co.uk>
7545587e44cSchris */
7555587e44cSchrisclass Doku_Handler_Nest {
7565587e44cSchris
7575587e44cSchris    var $CallWriter;
7585587e44cSchris    var $calls = array();
7595587e44cSchris
7605587e44cSchris    var $closingInstruction;
7615587e44cSchris
7625587e44cSchris    /**
7635587e44cSchris     * constructor
7645587e44cSchris     *
7655587e44cSchris     * @param  object     $CallWriter     the renderers current call writer
7665587e44cSchris     * @param  string     $close          closing instruction name, this is required to properly terminate the
7675587e44cSchris     *                                    syntax mode if the document ends without a closing pattern
7685587e44cSchris     */
7695587e44cSchris    function Doku_Handler_Nest(& $CallWriter, $close="nest_close") {
7705587e44cSchris        $this->CallWriter = & $CallWriter;
7715587e44cSchris
7725587e44cSchris        $this->closingInstruction = $close;
7735587e44cSchris    }
7745587e44cSchris
7755587e44cSchris    function writeCall($call) {
7765587e44cSchris        $this->calls[] = $call;
7775587e44cSchris    }
7785587e44cSchris
7795587e44cSchris    function writeCalls($calls) {
7805587e44cSchris        $this->calls = array_merge($this->calls, $calls);
7815587e44cSchris    }
7825587e44cSchris
7835587e44cSchris    function finalise() {
7845587e44cSchris        $last_call = end($this->calls);
7855587e44cSchris        $this->writeCall(array($this->closingInstruction,array(), $last_call[2]));
7865587e44cSchris
7875587e44cSchris        $this->process();
7885587e44cSchris        $this->CallWriter->finalise();
7895587e44cSchris    }
7905587e44cSchris
7915587e44cSchris    function process() {
79241624b31SChris Smith        // merge consecutive cdata
79341624b31SChris Smith        $unmerged_calls = $this->calls;
79441624b31SChris Smith        $this->calls = array();
79541624b31SChris Smith
79641624b31SChris Smith        foreach ($unmerged_calls as $call) $this->addCall($call);
79741624b31SChris Smith
7985587e44cSchris        $first_call = reset($this->calls);
7995587e44cSchris        $this->CallWriter->writeCall(array("nest", array($this->calls), $first_call[2]));
8005587e44cSchris    }
80141624b31SChris Smith
80241624b31SChris Smith    function addCall($call) {
80341624b31SChris Smith        $key = count($this->calls);
80441624b31SChris Smith        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
80541624b31SChris Smith            $this->calls[$key-1][1][0] .= $call[1][0];
80641624b31SChris Smith        } else {
80741624b31SChris Smith            $this->calls[] = $call;
80841624b31SChris Smith        }
80941624b31SChris Smith    }
8105587e44cSchris}
8115587e44cSchris
8120cecf9d5Sandiclass Doku_Handler_List {
8130cecf9d5Sandi
8140cecf9d5Sandi    var $CallWriter;
8150cecf9d5Sandi
8160cecf9d5Sandi    var $calls = array();
8170cecf9d5Sandi    var $listCalls = array();
8180cecf9d5Sandi    var $listStack = array();
8190cecf9d5Sandi
8200cecf9d5Sandi    function Doku_Handler_List(& $CallWriter) {
8210cecf9d5Sandi        $this->CallWriter = & $CallWriter;
8220cecf9d5Sandi    }
8230cecf9d5Sandi
8240cecf9d5Sandi    function writeCall($call) {
8250cecf9d5Sandi        $this->calls[] = $call;
8260cecf9d5Sandi    }
8270cecf9d5Sandi
8280cecf9d5Sandi    // Probably not needed but just in case...
8290cecf9d5Sandi    function writeCalls($calls) {
8300cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
831f4f02a0fSchris#        $this->CallWriter->writeCalls($this->calls);
832f4f02a0fSchris    }
833f4f02a0fSchris
834f4f02a0fSchris    function finalise() {
835f4f02a0fSchris        $last_call = end($this->calls);
836f4f02a0fSchris        $this->writeCall(array('list_close',array(), $last_call[2]));
837f4f02a0fSchris
838f4f02a0fSchris        $this->process();
839f4f02a0fSchris        $this->CallWriter->finalise();
8400cecf9d5Sandi    }
8410cecf9d5Sandi
8420cecf9d5Sandi    //------------------------------------------------------------------------
8430cecf9d5Sandi    function process() {
844f4f02a0fSchris
8450cecf9d5Sandi        foreach ( $this->calls as $call ) {
8460cecf9d5Sandi            switch ($call[0]) {
8470cecf9d5Sandi                case 'list_item':
8480cecf9d5Sandi                    $this->listOpen($call);
8490cecf9d5Sandi                break;
8500cecf9d5Sandi                case 'list_open':
8510cecf9d5Sandi                    $this->listStart($call);
8520cecf9d5Sandi                break;
8530cecf9d5Sandi                case 'list_close':
8540cecf9d5Sandi                    $this->listEnd($call);
8550cecf9d5Sandi                break;
8560cecf9d5Sandi                default:
8570cecf9d5Sandi                    $this->listContent($call);
8580cecf9d5Sandi                break;
8590cecf9d5Sandi            }
8600cecf9d5Sandi        }
8610cecf9d5Sandi
8620cecf9d5Sandi        $this->CallWriter->writeCalls($this->listCalls);
8630cecf9d5Sandi    }
8640cecf9d5Sandi
8650cecf9d5Sandi    //------------------------------------------------------------------------
8660cecf9d5Sandi    function listStart($call) {
8670cecf9d5Sandi        $depth = $this->interpretSyntax($call[1][0], $listType);
8680cecf9d5Sandi
8690cecf9d5Sandi        $this->initialDepth = $depth;
8700cecf9d5Sandi        $this->listStack[] = array($listType, $depth);
8710cecf9d5Sandi
8720cecf9d5Sandi        $this->listCalls[] = array('list'.$listType.'_open',array(),$call[2]);
8730cecf9d5Sandi        $this->listCalls[] = array('listitem_open',array(1),$call[2]);
8740cecf9d5Sandi        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
8750cecf9d5Sandi    }
8760cecf9d5Sandi
8770cecf9d5Sandi    //------------------------------------------------------------------------
8780cecf9d5Sandi    function listEnd($call) {
87944881bd0Shenning.noren        $closeContent = true;
8800cecf9d5Sandi
8810cecf9d5Sandi        while ( $list = array_pop($this->listStack) ) {
8820cecf9d5Sandi            if ( $closeContent ) {
8830cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
88444881bd0Shenning.noren                $closeContent = false;
8850cecf9d5Sandi            }
8860cecf9d5Sandi            $this->listCalls[] = array('listitem_close',array(),$call[2]);
8870cecf9d5Sandi            $this->listCalls[] = array('list'.$list[0].'_close', array(), $call[2]);
8880cecf9d5Sandi        }
8890cecf9d5Sandi    }
8900cecf9d5Sandi
8910cecf9d5Sandi    //------------------------------------------------------------------------
8920cecf9d5Sandi    function listOpen($call) {
8930cecf9d5Sandi        $depth = $this->interpretSyntax($call[1][0], $listType);
8940cecf9d5Sandi        $end = end($this->listStack);
8950cecf9d5Sandi
8960cecf9d5Sandi        // Not allowed to be shallower than initialDepth
8970cecf9d5Sandi        if ( $depth < $this->initialDepth ) {
8980cecf9d5Sandi            $depth = $this->initialDepth;
8990cecf9d5Sandi        }
9000cecf9d5Sandi
9010cecf9d5Sandi        //------------------------------------------------------------------------
9020cecf9d5Sandi        if ( $depth == $end[1] ) {
9030cecf9d5Sandi
9040cecf9d5Sandi            // Just another item in the list...
9050cecf9d5Sandi            if ( $listType == $end[0] ) {
9060cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9070cecf9d5Sandi                $this->listCalls[] = array('listitem_close',array(),$call[2]);
9080cecf9d5Sandi                $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
9090cecf9d5Sandi                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9100cecf9d5Sandi
9110cecf9d5Sandi            // Switched list type...
9120cecf9d5Sandi            } else {
9130cecf9d5Sandi
9140cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9150cecf9d5Sandi                $this->listCalls[] = array('listitem_close',array(),$call[2]);
9160cecf9d5Sandi                $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
9170cecf9d5Sandi                $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9180cecf9d5Sandi                $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9190cecf9d5Sandi                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9200cecf9d5Sandi
9210cecf9d5Sandi                array_pop($this->listStack);
9220cecf9d5Sandi                $this->listStack[] = array($listType, $depth);
9230cecf9d5Sandi            }
9240cecf9d5Sandi
9250cecf9d5Sandi        //------------------------------------------------------------------------
9260cecf9d5Sandi        // Getting deeper...
9270cecf9d5Sandi        } else if ( $depth > $end[1] ) {
9280cecf9d5Sandi
9290cecf9d5Sandi            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9300cecf9d5Sandi            $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9310cecf9d5Sandi            $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9320cecf9d5Sandi            $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9330cecf9d5Sandi
9340cecf9d5Sandi            $this->listStack[] = array($listType, $depth);
9350cecf9d5Sandi
9360cecf9d5Sandi        //------------------------------------------------------------------------
9370cecf9d5Sandi        // Getting shallower ( $depth < $end[1] )
9380cecf9d5Sandi        } else {
9390cecf9d5Sandi            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9400cecf9d5Sandi            $this->listCalls[] = array('listitem_close',array(),$call[2]);
9410cecf9d5Sandi            $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
9420cecf9d5Sandi
9430cecf9d5Sandi            // Throw away the end - done
9440cecf9d5Sandi            array_pop($this->listStack);
9450cecf9d5Sandi
9460cecf9d5Sandi            while (1) {
9470cecf9d5Sandi                $end = end($this->listStack);
9480cecf9d5Sandi
9490cecf9d5Sandi                if ( $end[1] <= $depth ) {
9500cecf9d5Sandi
9510cecf9d5Sandi                    // Normalize depths
9520cecf9d5Sandi                    $depth = $end[1];
9530cecf9d5Sandi
9540cecf9d5Sandi                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
9550cecf9d5Sandi
9560cecf9d5Sandi                    if ( $end[0] == $listType ) {
9570cecf9d5Sandi                        $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
9580cecf9d5Sandi                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9590cecf9d5Sandi
9600cecf9d5Sandi                    } else {
9610cecf9d5Sandi                        // Switching list type...
9620cecf9d5Sandi                        $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
9630cecf9d5Sandi                        $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9640cecf9d5Sandi                        $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9650cecf9d5Sandi                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9660cecf9d5Sandi
9670cecf9d5Sandi                        array_pop($this->listStack);
9680cecf9d5Sandi                        $this->listStack[] = array($listType, $depth);
9690cecf9d5Sandi                    }
9700cecf9d5Sandi
9710cecf9d5Sandi                    break;
9720cecf9d5Sandi
9730cecf9d5Sandi                // Haven't dropped down far enough yet.... ( $end[1] > $depth )
9740cecf9d5Sandi                } else {
9750cecf9d5Sandi
9760cecf9d5Sandi                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
9770cecf9d5Sandi                    $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
9780cecf9d5Sandi
9790cecf9d5Sandi                    array_pop($this->listStack);
9800cecf9d5Sandi
9810cecf9d5Sandi                }
9820cecf9d5Sandi
9830cecf9d5Sandi            }
9840cecf9d5Sandi
9850cecf9d5Sandi        }
9860cecf9d5Sandi    }
9870cecf9d5Sandi
9880cecf9d5Sandi    //------------------------------------------------------------------------
9890cecf9d5Sandi    function listContent($call) {
9900cecf9d5Sandi        $this->listCalls[] = $call;
9910cecf9d5Sandi    }
9920cecf9d5Sandi
9930cecf9d5Sandi    //------------------------------------------------------------------------
9940cecf9d5Sandi    function interpretSyntax($match, & $type) {
9950cecf9d5Sandi        if ( substr($match,-1) == '*' ) {
9960cecf9d5Sandi            $type = 'u';
9970cecf9d5Sandi        } else {
9980cecf9d5Sandi            $type = 'o';
9990cecf9d5Sandi        }
10000cecf9d5Sandi        return count(explode('  ',str_replace("\t",'  ',$match)));
10010cecf9d5Sandi    }
10020cecf9d5Sandi}
10030cecf9d5Sandi
10040cecf9d5Sandi//------------------------------------------------------------------------
10050cecf9d5Sandiclass Doku_Handler_Preformatted {
10060cecf9d5Sandi
10070cecf9d5Sandi    var $CallWriter;
10080cecf9d5Sandi
10090cecf9d5Sandi    var $calls = array();
10100cecf9d5Sandi    var $pos;
10110cecf9d5Sandi    var $text ='';
10120cecf9d5Sandi
10130cecf9d5Sandi
10140cecf9d5Sandi
10150cecf9d5Sandi    function Doku_Handler_Preformatted(& $CallWriter) {
10160cecf9d5Sandi        $this->CallWriter = & $CallWriter;
10170cecf9d5Sandi    }
10180cecf9d5Sandi
10190cecf9d5Sandi    function writeCall($call) {
10200cecf9d5Sandi        $this->calls[] = $call;
10210cecf9d5Sandi    }
10220cecf9d5Sandi
10230cecf9d5Sandi    // Probably not needed but just in case...
10240cecf9d5Sandi    function writeCalls($calls) {
10250cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1026f4f02a0fSchris#        $this->CallWriter->writeCalls($this->calls);
1027f4f02a0fSchris    }
1028f4f02a0fSchris
1029f4f02a0fSchris    function finalise() {
1030f4f02a0fSchris        $last_call = end($this->calls);
1031f4f02a0fSchris        $this->writeCall(array('preformatted_end',array(), $last_call[2]));
1032f4f02a0fSchris
1033f4f02a0fSchris        $this->process();
1034f4f02a0fSchris        $this->CallWriter->finalise();
10350cecf9d5Sandi    }
10360cecf9d5Sandi
10370cecf9d5Sandi    function process() {
10380cecf9d5Sandi        foreach ( $this->calls as $call ) {
10390cecf9d5Sandi            switch ($call[0]) {
10400cecf9d5Sandi                case 'preformatted_start':
10410cecf9d5Sandi                    $this->pos = $call[2];
10420cecf9d5Sandi                break;
10430cecf9d5Sandi                case 'preformatted_newline':
10440cecf9d5Sandi                    $this->text .= "\n";
10450cecf9d5Sandi                break;
10460cecf9d5Sandi                case 'preformatted_content':
10470cecf9d5Sandi                    $this->text .= $call[1][0];
10480cecf9d5Sandi                break;
10490cecf9d5Sandi                case 'preformatted_end':
105093a34bf3SChris Smith                    if (trim($this->text)) {
10510cecf9d5Sandi                      $this->CallWriter->writeCall(array('preformatted',array($this->text),$this->pos));
105293a34bf3SChris Smith                    }
10530cecf9d5Sandi                break;
10540cecf9d5Sandi            }
10550cecf9d5Sandi        }
10560cecf9d5Sandi    }
1057f4f02a0fSchris
10580cecf9d5Sandi}
10590cecf9d5Sandi
10600cecf9d5Sandi//------------------------------------------------------------------------
10610cecf9d5Sandiclass Doku_Handler_Quote {
10620cecf9d5Sandi
10630cecf9d5Sandi    var $CallWriter;
10640cecf9d5Sandi
10650cecf9d5Sandi    var $calls = array();
10660cecf9d5Sandi
10670cecf9d5Sandi    var $quoteCalls = array();
10680cecf9d5Sandi
10690cecf9d5Sandi    function Doku_Handler_Quote(& $CallWriter) {
10700cecf9d5Sandi        $this->CallWriter = & $CallWriter;
10710cecf9d5Sandi    }
10720cecf9d5Sandi
10730cecf9d5Sandi    function writeCall($call) {
10740cecf9d5Sandi        $this->calls[] = $call;
10750cecf9d5Sandi    }
10760cecf9d5Sandi
10770cecf9d5Sandi    // Probably not needed but just in case...
10780cecf9d5Sandi    function writeCalls($calls) {
10790cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1080f4f02a0fSchris    }
1081f4f02a0fSchris
1082f4f02a0fSchris    function finalise() {
1083f4f02a0fSchris        $last_call = end($this->calls);
1084f4f02a0fSchris        $this->writeCall(array('quote_end',array(), $last_call[2]));
1085f4f02a0fSchris
1086f4f02a0fSchris        $this->process();
1087f4f02a0fSchris        $this->CallWriter->finalise();
10880cecf9d5Sandi    }
10890cecf9d5Sandi
10900cecf9d5Sandi    function process() {
10910cecf9d5Sandi
10920cecf9d5Sandi        $quoteDepth = 1;
10930cecf9d5Sandi
10940cecf9d5Sandi        foreach ( $this->calls as $call ) {
10950cecf9d5Sandi            switch ($call[0]) {
10960cecf9d5Sandi
10970cecf9d5Sandi                case 'quote_start':
10980cecf9d5Sandi
10990cecf9d5Sandi                    $this->quoteCalls[] = array('quote_open',array(),$call[2]);
11000cecf9d5Sandi
11010cecf9d5Sandi                case 'quote_newline':
11020cecf9d5Sandi
11030cecf9d5Sandi                    $quoteLength = $this->getDepth($call[1][0]);
11040cecf9d5Sandi
11050cecf9d5Sandi                    if ( $quoteLength > $quoteDepth ) {
11060cecf9d5Sandi                        $quoteDiff = $quoteLength - $quoteDepth;
11070cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11080cecf9d5Sandi                            $this->quoteCalls[] = array('quote_open',array(),$call[2]);
11090cecf9d5Sandi                        }
11100cecf9d5Sandi                    } else if ( $quoteLength < $quoteDepth ) {
11110cecf9d5Sandi                        $quoteDiff = $quoteDepth - $quoteLength;
11120cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11130cecf9d5Sandi                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11140cecf9d5Sandi                        }
111526426c64Schris                    } else {
111626426c64Schris                        if ($call[0] != 'quote_start') $this->quoteCalls[] = array('linebreak',array(),$call[2]);
11170cecf9d5Sandi                    }
11180cecf9d5Sandi
11190cecf9d5Sandi                    $quoteDepth = $quoteLength;
11200cecf9d5Sandi
11210cecf9d5Sandi                break;
11220cecf9d5Sandi
11230cecf9d5Sandi                case 'quote_end':
11240cecf9d5Sandi
11250cecf9d5Sandi                    if ( $quoteDepth > 1 ) {
11260cecf9d5Sandi                        $quoteDiff = $quoteDepth - 1;
11270cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11280cecf9d5Sandi                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11290cecf9d5Sandi                        }
11300cecf9d5Sandi                    }
11310cecf9d5Sandi
11320cecf9d5Sandi                    $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11330cecf9d5Sandi
11340cecf9d5Sandi                    $this->CallWriter->writeCalls($this->quoteCalls);
11350cecf9d5Sandi                break;
11360cecf9d5Sandi
11370cecf9d5Sandi                default:
11380cecf9d5Sandi                    $this->quoteCalls[] = $call;
11390cecf9d5Sandi                break;
11400cecf9d5Sandi            }
11410cecf9d5Sandi        }
11420cecf9d5Sandi    }
11430cecf9d5Sandi
11440cecf9d5Sandi    function getDepth($marker) {
11450cecf9d5Sandi        preg_match('/>{1,}/', $marker, $matches);
11460cecf9d5Sandi        $quoteLength = strlen($matches[0]);
11470cecf9d5Sandi        return $quoteLength;
11480cecf9d5Sandi    }
11490cecf9d5Sandi}
11500cecf9d5Sandi
11510cecf9d5Sandi//------------------------------------------------------------------------
11520cecf9d5Sandiclass Doku_Handler_Table {
11530cecf9d5Sandi
11540cecf9d5Sandi    var $CallWriter;
11550cecf9d5Sandi
11560cecf9d5Sandi    var $calls = array();
11570cecf9d5Sandi    var $tableCalls = array();
11580cecf9d5Sandi    var $maxCols = 0;
11590cecf9d5Sandi    var $maxRows = 1;
11600cecf9d5Sandi    var $currentCols = 0;
116144881bd0Shenning.noren    var $firstCell = false;
11620cecf9d5Sandi    var $lastCellType = 'tablecell';
11630cecf9d5Sandi
11640cecf9d5Sandi    function Doku_Handler_Table(& $CallWriter) {
11650cecf9d5Sandi        $this->CallWriter = & $CallWriter;
11660cecf9d5Sandi    }
11670cecf9d5Sandi
11680cecf9d5Sandi    function writeCall($call) {
11690cecf9d5Sandi        $this->calls[] = $call;
11700cecf9d5Sandi    }
11710cecf9d5Sandi
11720cecf9d5Sandi    // Probably not needed but just in case...
11730cecf9d5Sandi    function writeCalls($calls) {
11740cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1175f4f02a0fSchris    }
1176f4f02a0fSchris
1177f4f02a0fSchris    function finalise() {
1178f4f02a0fSchris        $last_call = end($this->calls);
1179f4f02a0fSchris        $this->writeCall(array('table_end',array(), $last_call[2]));
1180f4f02a0fSchris
1181f4f02a0fSchris        $this->process();
1182f4f02a0fSchris        $this->CallWriter->finalise();
11830cecf9d5Sandi    }
11840cecf9d5Sandi
11850cecf9d5Sandi    //------------------------------------------------------------------------
11860cecf9d5Sandi    function process() {
11870cecf9d5Sandi        foreach ( $this->calls as $call ) {
11880cecf9d5Sandi            switch ( $call[0] ) {
11890cecf9d5Sandi                case 'table_start':
11900cecf9d5Sandi                    $this->tableStart($call);
11910cecf9d5Sandi                break;
11920cecf9d5Sandi                case 'table_row':
11930cecf9d5Sandi                    $this->tableRowClose(array('tablerow_close',$call[1],$call[2]));
11940cecf9d5Sandi                    $this->tableRowOpen(array('tablerow_open',$call[1],$call[2]));
11950cecf9d5Sandi                break;
11960cecf9d5Sandi                case 'tableheader':
11970cecf9d5Sandi                case 'tablecell':
11980cecf9d5Sandi                    $this->tableCell($call);
11990cecf9d5Sandi                break;
12000cecf9d5Sandi                case 'table_end':
12010cecf9d5Sandi                    $this->tableRowClose(array('tablerow_close',$call[1],$call[2]));
12020cecf9d5Sandi                    $this->tableEnd($call);
12030cecf9d5Sandi                break;
12040cecf9d5Sandi                default:
12050cecf9d5Sandi                    $this->tableDefault($call);
12060cecf9d5Sandi                break;
12070cecf9d5Sandi            }
12080cecf9d5Sandi        }
12090cecf9d5Sandi        $this->CallWriter->writeCalls($this->tableCalls);
12100cecf9d5Sandi    }
12110cecf9d5Sandi
12120cecf9d5Sandi    function tableStart($call) {
12130cecf9d5Sandi        $this->tableCalls[] = array('table_open',array(),$call[2]);
12140cecf9d5Sandi        $this->tableCalls[] = array('tablerow_open',array(),$call[2]);
121544881bd0Shenning.noren        $this->firstCell = true;
12160cecf9d5Sandi    }
12170cecf9d5Sandi
12180cecf9d5Sandi    function tableEnd($call) {
12190cecf9d5Sandi        $this->tableCalls[] = array('table_close',array(),$call[2]);
12200cecf9d5Sandi        $this->finalizeTable();
12210cecf9d5Sandi    }
12220cecf9d5Sandi
12230cecf9d5Sandi    function tableRowOpen($call) {
12240cecf9d5Sandi        $this->tableCalls[] = $call;
12250cecf9d5Sandi        $this->currentCols = 0;
122644881bd0Shenning.noren        $this->firstCell = true;
12270cecf9d5Sandi        $this->lastCellType = 'tablecell';
12280cecf9d5Sandi        $this->maxRows++;
12290cecf9d5Sandi    }
12300cecf9d5Sandi
12310cecf9d5Sandi    function tableRowClose($call) {
12320cecf9d5Sandi        // Strip off final cell opening and anything after it
12330cecf9d5Sandi        while ( $discard = array_pop($this->tableCalls ) ) {
12340cecf9d5Sandi
12350cecf9d5Sandi            if ( $discard[0] == 'tablecell_open' || $discard[0] == 'tableheader_open') {
12360cecf9d5Sandi
12370cecf9d5Sandi                // Its a spanning element - put it back and close it
12380cecf9d5Sandi                if ( $discard[1][0] > 1 ) {
12390cecf9d5Sandi
12400cecf9d5Sandi                    $this->tableCalls[] = $discard;
12410cecf9d5Sandi                    if ( strstr($discard[0],'cell') ) {
12420cecf9d5Sandi                        $name = 'tablecell';
12430cecf9d5Sandi                    } else {
12440cecf9d5Sandi                        $name = 'tableheader';
12450cecf9d5Sandi                    }
12460cecf9d5Sandi                    $this->tableCalls[] = array($name.'_close',array(),$call[2]);
12470cecf9d5Sandi                }
12480cecf9d5Sandi
12490cecf9d5Sandi                break;
12500cecf9d5Sandi            }
12510cecf9d5Sandi        }
12520cecf9d5Sandi        $this->tableCalls[] = $call;
12530cecf9d5Sandi
12540cecf9d5Sandi        if ( $this->currentCols > $this->maxCols ) {
12550cecf9d5Sandi            $this->maxCols = $this->currentCols;
12560cecf9d5Sandi        }
12570cecf9d5Sandi    }
12580cecf9d5Sandi
12590cecf9d5Sandi    function tableCell($call) {
12600cecf9d5Sandi        if ( !$this->firstCell ) {
12610cecf9d5Sandi
12620cecf9d5Sandi            // Increase the span
12630cecf9d5Sandi            $lastCall = end($this->tableCalls);
12640cecf9d5Sandi
12650cecf9d5Sandi            // A cell call which follows an open cell means an empty cell so span
12660cecf9d5Sandi            if ( $lastCall[0] == 'tablecell_open' || $lastCall[0] == 'tableheader_open' ) {
12670cecf9d5Sandi                 $this->tableCalls[] = array('colspan',array(),$call[2]);
12680cecf9d5Sandi
12690cecf9d5Sandi            }
12700cecf9d5Sandi
12710cecf9d5Sandi            $this->tableCalls[] = array($this->lastCellType.'_close',array(),$call[2]);
12720cecf9d5Sandi            $this->tableCalls[] = array($call[0].'_open',array(1,NULL),$call[2]);
12730cecf9d5Sandi            $this->lastCellType = $call[0];
12740cecf9d5Sandi
12750cecf9d5Sandi        } else {
12760cecf9d5Sandi
12770cecf9d5Sandi            $this->tableCalls[] = array($call[0].'_open',array(1,NULL),$call[2]);
12780cecf9d5Sandi            $this->lastCellType = $call[0];
127944881bd0Shenning.noren            $this->firstCell = false;
12800cecf9d5Sandi
12810cecf9d5Sandi        }
12820cecf9d5Sandi
12830cecf9d5Sandi        $this->currentCols++;
12840cecf9d5Sandi    }
12850cecf9d5Sandi
12860cecf9d5Sandi    function tableDefault($call) {
12870cecf9d5Sandi        $this->tableCalls[] = $call;
12880cecf9d5Sandi    }
12890cecf9d5Sandi
12900cecf9d5Sandi    function finalizeTable() {
12910cecf9d5Sandi
12920cecf9d5Sandi        // Add the max cols and rows to the table opening
12930cecf9d5Sandi        if ( $this->tableCalls[0][0] == 'table_open' ) {
12940cecf9d5Sandi            // Adjust to num cols not num col delimeters
12950cecf9d5Sandi            $this->tableCalls[0][1][] = $this->maxCols - 1;
12960cecf9d5Sandi            $this->tableCalls[0][1][] = $this->maxRows;
12970cecf9d5Sandi        } else {
12980cecf9d5Sandi            trigger_error('First element in table call list is not table_open');
12990cecf9d5Sandi        }
13000cecf9d5Sandi
13010cecf9d5Sandi        $lastRow = 0;
13020cecf9d5Sandi        $lastCell = 0;
13030cecf9d5Sandi        $toDelete = array();
13040cecf9d5Sandi
13050cecf9d5Sandi        // Look for the colspan elements and increment the colspan on the
13060cecf9d5Sandi        // previous non-empty opening cell. Once done, delete all the cells
13070cecf9d5Sandi        // that contain colspans
13080cecf9d5Sandi        foreach ( $this->tableCalls as $key => $call ) {
13090cecf9d5Sandi
13100cecf9d5Sandi            if ( $call[0] == 'tablerow_open' ) {
13110cecf9d5Sandi
13120cecf9d5Sandi                $lastRow = $key;
13130cecf9d5Sandi
13140cecf9d5Sandi            } else if ( $call[0] == 'tablecell_open' || $call[0] == 'tableheader_open' ) {
13150cecf9d5Sandi
13160cecf9d5Sandi                $lastCell = $key;
13170cecf9d5Sandi
13180cecf9d5Sandi            } else if ( $call[0] == 'table_align' ) {
13190cecf9d5Sandi
13200cecf9d5Sandi                // If the previous element was a cell open, align right
13210cecf9d5Sandi                if ( $this->tableCalls[$key-1][0] == 'tablecell_open' || $this->tableCalls[$key-1][0] == 'tableheader_open' ) {
13220cecf9d5Sandi                    $this->tableCalls[$key-1][1][1] = 'right';
13230cecf9d5Sandi
13240cecf9d5Sandi                // If the next element if the close of an element, align either center or left
13250cecf9d5Sandi                } else if ( $this->tableCalls[$key+1][0] == 'tablecell_close' || $this->tableCalls[$key+1][0] == 'tableheader_close' ) {
13260cecf9d5Sandi                    if ( $this->tableCalls[$lastCell][1][1] == 'right' ) {
13270cecf9d5Sandi                        $this->tableCalls[$lastCell][1][1] = 'center';
13280cecf9d5Sandi                    } else {
13290cecf9d5Sandi                        $this->tableCalls[$lastCell][1][1] = 'left';
13300cecf9d5Sandi                    }
13310cecf9d5Sandi
13320cecf9d5Sandi                }
13330cecf9d5Sandi
13340cecf9d5Sandi                // Now convert the whitespace back to cdata
13350cecf9d5Sandi                $this->tableCalls[$key][0] = 'cdata';
13360cecf9d5Sandi
13370cecf9d5Sandi            } else if ( $call[0] == 'colspan' ) {
13380cecf9d5Sandi
133944881bd0Shenning.noren                $this->tableCalls[$key-1][1][0] = false;
13400cecf9d5Sandi
13410cecf9d5Sandi                for($i = $key-2; $i > $lastRow; $i--) {
13420cecf9d5Sandi
13430cecf9d5Sandi                    if ( $this->tableCalls[$i][0] == 'tablecell_open' || $this->tableCalls[$i][0] == 'tableheader_open' ) {
13440cecf9d5Sandi
134544881bd0Shenning.noren                        if ( false !== $this->tableCalls[$i][1][0] ) {
13460cecf9d5Sandi                            $this->tableCalls[$i][1][0]++;
13470cecf9d5Sandi                            break;
13480cecf9d5Sandi                        }
13490cecf9d5Sandi
13500cecf9d5Sandi
13510cecf9d5Sandi                    }
13520cecf9d5Sandi                }
13530cecf9d5Sandi
13540cecf9d5Sandi                $toDelete[] = $key-1;
13550cecf9d5Sandi                $toDelete[] = $key;
13560cecf9d5Sandi                $toDelete[] = $key+1;
13570cecf9d5Sandi            }
13580cecf9d5Sandi        }
13590cecf9d5Sandi
13609ab75d9eSAndreas Gohr
13619ab75d9eSAndreas Gohr        // condense cdata
13629ab75d9eSAndreas Gohr        $cnt = count($this->tableCalls);
13639ab75d9eSAndreas Gohr        for( $key = 0; $key < $cnt; $key++){
13649ab75d9eSAndreas Gohr            if($this->tableCalls[$key][0] == 'cdata'){
13659ab75d9eSAndreas Gohr                $ckey = $key;
13669ab75d9eSAndreas Gohr                $key++;
13679ab75d9eSAndreas Gohr                while($this->tableCalls[$key][0] == 'cdata'){
13689ab75d9eSAndreas Gohr                    $this->tableCalls[$ckey][1][0] .= $this->tableCalls[$key][1][0];
13699ab75d9eSAndreas Gohr                    $toDelete[] = $key;
13709ab75d9eSAndreas Gohr                    $key++;
13719ab75d9eSAndreas Gohr                }
13729ab75d9eSAndreas Gohr                continue;
13739ab75d9eSAndreas Gohr            }
13749ab75d9eSAndreas Gohr        }
13759ab75d9eSAndreas Gohr
13760cecf9d5Sandi        foreach ( $toDelete as $delete ) {
13770cecf9d5Sandi            unset($this->tableCalls[$delete]);
13780cecf9d5Sandi        }
13790cecf9d5Sandi        $this->tableCalls = array_values($this->tableCalls);
13800cecf9d5Sandi    }
13810cecf9d5Sandi}
13820cecf9d5Sandi
13830cecf9d5Sandi//------------------------------------------------------------------------
13840cecf9d5Sandiclass Doku_Handler_Section {
13850cecf9d5Sandi
13860cecf9d5Sandi    function process($calls) {
13870cecf9d5Sandi
13880cecf9d5Sandi        $sectionCalls = array();
138944881bd0Shenning.noren        $inSection = false;
13900cecf9d5Sandi
13910cecf9d5Sandi        foreach ( $calls as $call ) {
13920cecf9d5Sandi
13930cecf9d5Sandi            if ( $call[0] == 'header' ) {
13940cecf9d5Sandi
13950cecf9d5Sandi                if ( $inSection ) {
13960cecf9d5Sandi                    $sectionCalls[] = array('section_close',array(), $call[2]);
13970cecf9d5Sandi                }
13980cecf9d5Sandi
13990cecf9d5Sandi                $sectionCalls[] = $call;
14000cecf9d5Sandi                $sectionCalls[] = array('section_open',array($call[1][1]), $call[2]);
140144881bd0Shenning.noren                $inSection = true;
14020cecf9d5Sandi
14030cecf9d5Sandi            } else {
1404e1c10e4dSchris
1405e1c10e4dSchris                if ($call[0] == 'section_open' )  {
140644881bd0Shenning.noren                    $inSection = true;
1407e1c10e4dSchris                } else if ($call[0] == 'section_open' ) {
140844881bd0Shenning.noren                    $inSection = false;
1409e1c10e4dSchris                }
14100cecf9d5Sandi                $sectionCalls[] = $call;
14110cecf9d5Sandi            }
14120cecf9d5Sandi        }
14130cecf9d5Sandi
14140cecf9d5Sandi        if ( $inSection ) {
14150cecf9d5Sandi            $sectionCalls[] = array('section_close',array(), $call[2]);
14160cecf9d5Sandi        }
14170cecf9d5Sandi
14180cecf9d5Sandi        return $sectionCalls;
14190cecf9d5Sandi    }
14200cecf9d5Sandi
14210cecf9d5Sandi}
14220cecf9d5Sandi
14232a27e99aSandi/**
14242a27e99aSandi * Handler for paragraphs
14252a27e99aSandi *
14260b7c14c2Sandi * @author Harry Fuecks <hfuecks@gmail.com>
14272a27e99aSandi */
14280cecf9d5Sandiclass Doku_Handler_Block {
14290cecf9d5Sandi
14300cecf9d5Sandi    var $calls = array();
14310cecf9d5Sandi
14320cecf9d5Sandi    var $blockStack = array();
14330cecf9d5Sandi
143444881bd0Shenning.noren    var $inParagraph = false;
143544881bd0Shenning.noren    var $atStart = true;
143658b56c06Sandi    var $skipEolKey = -1;
14370cecf9d5Sandi
1438af146da0Sandi    // Blocks these should not be inside paragraphs
14390cecf9d5Sandi    var $blockOpen = array(
14400cecf9d5Sandi            'header',
1441df9add72Schris            'listu_open','listo_open','listitem_open','listcontent_open',
14420cecf9d5Sandi            'table_open','tablerow_open','tablecell_open','tableheader_open',
14430cecf9d5Sandi            'quote_open',
14440cecf9d5Sandi            'section_open', // Needed to prevent p_open between header and section_open
144576aa94b7Schris            'code','file','hr','preformatted','rss',
144607f89c3cSAnika Henke            'htmlblock','phpblock',
14470cecf9d5Sandi        );
14480cecf9d5Sandi
14490cecf9d5Sandi    var $blockClose = array(
14500cecf9d5Sandi            'header',
1451df9add72Schris            'listu_close','listo_close','listitem_close','listcontent_close',
14520cecf9d5Sandi            'table_close','tablerow_close','tablecell_close','tableheader_close',
14530cecf9d5Sandi            'quote_close',
14540cecf9d5Sandi            'section_close', // Needed to prevent p_close after section_close
145576aa94b7Schris            'code','file','hr','preformatted','rss',
145607f89c3cSAnika Henke            'htmlblock','phpblock',
14570cecf9d5Sandi        );
14580cecf9d5Sandi
1459af146da0Sandi    // Stacks can contain paragraphs
14600cecf9d5Sandi    var $stackOpen = array(
14610cecf9d5Sandi        'footnote_open','section_open',
14620cecf9d5Sandi        );
14630cecf9d5Sandi
14640cecf9d5Sandi    var $stackClose = array(
14650cecf9d5Sandi        'footnote_close','section_close',
14660cecf9d5Sandi        );
14670cecf9d5Sandi
1468af146da0Sandi
1469af146da0Sandi    /**
1470af146da0Sandi     * Constructor. Adds loaded syntax plugins to the block and stack
1471af146da0Sandi     * arrays
1472af146da0Sandi     *
1473af146da0Sandi     * @author Andreas Gohr <andi@splitbrain.org>
1474af146da0Sandi     */
1475af146da0Sandi    function Doku_Handler_Block(){
1476af146da0Sandi        global $DOKU_PLUGINS;
1477af146da0Sandi        //check if syntax plugins were loaded
147803c4aec3Schris        if(empty($DOKU_PLUGINS['syntax'])) return;
1479af146da0Sandi        foreach($DOKU_PLUGINS['syntax'] as $n => $p){
1480af146da0Sandi            $ptype = $p->getPType();
1481af146da0Sandi            if($ptype == 'block'){
1482af146da0Sandi                $this->blockOpen[]  = 'plugin_'.$n;
1483af146da0Sandi                $this->blockClose[] = 'plugin_'.$n;
1484af146da0Sandi            }elseif($ptype == 'stack'){
1485af146da0Sandi                $this->stackOpen[]  = 'plugin_'.$n;
1486af146da0Sandi                $this->stackClose[] = 'plugin_'.$n;
1487af146da0Sandi            }
1488af146da0Sandi        }
1489af146da0Sandi    }
1490af146da0Sandi
14912a27e99aSandi    /**
14922a27e99aSandi     * Close a paragraph if needed
14932a27e99aSandi     *
14942a27e99aSandi     * This function makes sure there are no empty paragraphs on the stack
14952a27e99aSandi     *
14962a27e99aSandi     * @author Andreas Gohr <andi@splitbrain.org>
14972a27e99aSandi     */
1498506ae684Sandi    function closeParagraph($pos){
1499506ae684Sandi        // look back if there was any content - we don't want empty paragraphs
1500506ae684Sandi        $content = '';
1501506ae684Sandi        for($i=count($this->calls)-1; $i>=0; $i--){
1502506ae684Sandi            if($this->calls[$i][0] == 'p_open'){
1503506ae684Sandi                break;
1504506ae684Sandi            }elseif($this->calls[$i][0] == 'cdata'){
1505506ae684Sandi                $content .= $this->calls[$i][1][0];
1506506ae684Sandi            }else{
1507506ae684Sandi                $content = 'found markup';
1508506ae684Sandi                break;
1509506ae684Sandi            }
1510506ae684Sandi        }
1511506ae684Sandi
1512506ae684Sandi        if(trim($content)==''){
1513506ae684Sandi            //remove the whole paragraph
1514506ae684Sandi            array_splice($this->calls,$i);
1515506ae684Sandi        }else{
151629d015e3SBen Coburn            if ($this->calls[count($this->calls)-1][0] == 'section_edit') {
151729d015e3SBen Coburn                $tmp = array_pop($this->calls);
1518506ae684Sandi                $this->calls[] = array('p_close',array(), $pos);
151929d015e3SBen Coburn                $this->calls[] = $tmp;
152029d015e3SBen Coburn            } else {
152129d015e3SBen Coburn                $this->calls[] = array('p_close',array(), $pos);
152229d015e3SBen Coburn            }
1523506ae684Sandi        }
1524e1c10e4dSchris
152544881bd0Shenning.noren        $this->inParagraph = false;
1526506ae684Sandi    }
1527506ae684Sandi
15282a27e99aSandi    /**
15292a27e99aSandi     * Processes the whole instruction stack to open and close paragraphs
15302a27e99aSandi     *
15310b7c14c2Sandi     * @author Harry Fuecks <hfuecks@gmail.com>
15322a27e99aSandi     * @author Andreas Gohr <andi@splitbrain.org>
15332a27e99aSandi     * @todo   This thing is really messy and should be rewritten
15342a27e99aSandi     */
15350cecf9d5Sandi    function process($calls) {
15360cecf9d5Sandi        foreach ( $calls as $key => $call ) {
1537f0891737Sandi            $cname = $call[0];
1538e1c10e4dSchris            if($cname == 'plugin') {
1539e1c10e4dSchris                $cname='plugin_'.$call[1][0];
1540e1c10e4dSchris
1541e1c10e4dSchris                $plugin = true;
1542e1c10e4dSchris                $plugin_open = (($call[1][2] == DOKU_LEXER_ENTER) || ($call[1][2] == DOKU_LEXER_SPECIAL));
1543e1c10e4dSchris                $plugin_close = (($call[1][2] == DOKU_LEXER_EXIT) || ($call[1][2] == DOKU_LEXER_SPECIAL));
1544e1c10e4dSchris            } else {
1545e1c10e4dSchris                $plugin = false;
1546e1c10e4dSchris            }
15470cecf9d5Sandi
15480cecf9d5Sandi            // Process blocks which are stack like... (contain linefeeds)
1549e1c10e4dSchris            if ( in_array($cname,$this->stackOpen ) && (!$plugin || $plugin_open) ) {
1550e1c10e4dSchris
15510cecf9d5Sandi                $this->calls[] = $call;
15520cecf9d5Sandi
15530cecf9d5Sandi                // Hack - footnotes shouldn't immediately contain a p_open
1554f0891737Sandi                if ( $cname != 'footnote_open' ) {
15550cecf9d5Sandi                    $this->addToStack();
15560cecf9d5Sandi                } else {
155744881bd0Shenning.noren                    $this->addToStack(false);
15580cecf9d5Sandi                }
15590cecf9d5Sandi                continue;
15600cecf9d5Sandi            }
15610cecf9d5Sandi
1562e1c10e4dSchris            if ( in_array($cname,$this->stackClose ) && (!$plugin || $plugin_close)) {
15630cecf9d5Sandi
15640cecf9d5Sandi                if ( $this->inParagraph ) {
1565506ae684Sandi                    $this->closeParagraph($call[2]);
15660cecf9d5Sandi                }
15670cecf9d5Sandi                $this->calls[] = $call;
15680cecf9d5Sandi                $this->removeFromStack();
15690cecf9d5Sandi                continue;
15700cecf9d5Sandi            }
15710cecf9d5Sandi
15720cecf9d5Sandi            if ( !$this->atStart ) {
15730cecf9d5Sandi
1574f0891737Sandi                if ( $cname == 'eol' ) {
15750cecf9d5Sandi
1576e1c10e4dSchris                    // Check this isn't an eol instruction to skip...
157758b56c06Sandi                    if ( $this->skipEolKey != $key ) {
1578e1c10e4dSchris                        // Look to see if the next instruction is an EOL
157958b56c06Sandi                        if ( isset($calls[$key+1]) && $calls[$key+1][0] == 'eol' ) {
158058b56c06Sandi
158158b56c06Sandi                            if ( $this->inParagraph ) {
1582506ae684Sandi                                //$this->calls[] = array('p_close',array(), $call[2]);
1583506ae684Sandi                                $this->closeParagraph($call[2]);
158458b56c06Sandi                            }
158558b56c06Sandi
158658b56c06Sandi                            $this->calls[] = array('p_open',array(), $call[2]);
158744881bd0Shenning.noren                            $this->inParagraph = true;
158858b56c06Sandi
158958b56c06Sandi
1590e1c10e4dSchris                            // Mark the next instruction for skipping
159158b56c06Sandi                            $this->skipEolKey = $key+1;
159258b56c06Sandi
159358b56c06Sandi                        }else{
159458b56c06Sandi                            //if this is just a single eol make a space from it
159541624b31SChris Smith                            $this->addCall(array('cdata',array(DOKU_PARSER_EOL), $call[2]));
159658b56c06Sandi                        }
159758b56c06Sandi                    }
159858b56c06Sandi
15990cecf9d5Sandi
16000cecf9d5Sandi                } else {
16010cecf9d5Sandi
160244881bd0Shenning.noren                    $storeCall = true;
1603e1c10e4dSchris                    if ( $this->inParagraph && (in_array($cname, $this->blockOpen) && (!$plugin || $plugin_open))) {
1604506ae684Sandi                        $this->closeParagraph($call[2]);
16050cecf9d5Sandi                        $this->calls[] = $call;
160644881bd0Shenning.noren                        $storeCall = false;
16070cecf9d5Sandi                    }
16080cecf9d5Sandi
1609e1c10e4dSchris                    if ( in_array($cname, $this->blockClose) && (!$plugin || $plugin_close)) {
16100cecf9d5Sandi                        if ( $this->inParagraph ) {
1611506ae684Sandi                            $this->closeParagraph($call[2]);
16120cecf9d5Sandi                        }
16130cecf9d5Sandi                        if ( $storeCall ) {
16140cecf9d5Sandi                            $this->calls[] = $call;
161544881bd0Shenning.noren                            $storeCall = false;
16160cecf9d5Sandi                        }
16170cecf9d5Sandi
16180cecf9d5Sandi                        // This really sucks and suggests this whole class sucks but...
1619e1c10e4dSchris                        if ( isset($calls[$key+1])) {
1620e1c10e4dSchris                            $cname_plusone = $calls[$key+1][0];
1621e1c10e4dSchris                            if ($cname_plusone == 'plugin') {
1622e1c10e4dSchris                                $cname_plusone = 'plugin'.$calls[$key+1][1][0];
1623e1c10e4dSchris
1624e1c10e4dSchris                                // plugin test, true if plugin has a state which precludes it requiring blockOpen or blockClose
1625e1c10e4dSchris                                $plugin_plusone = true;
1626e1c10e4dSchris                                $plugin_test = ($call[$key+1][1][2] == DOKU_LEXER_MATCHED) || ($call[$key+1][1][2] == DOKU_LEXER_MATCHED);
1627e1c10e4dSchris                            } else {
1628e1c10e4dSchris                                $plugin_plusone = false;
1629e1c10e4dSchris                            }
1630e1c10e4dSchris                            if ((!in_array($cname_plusone, $this->blockOpen) && !in_array($cname_plusone, $this->blockClose)) ||
1631e1c10e4dSchris                                ($plugin_plusone && $plugin_test)
16320cecf9d5Sandi                                ) {
16330cecf9d5Sandi
16340cecf9d5Sandi                                $this->calls[] = array('p_open',array(), $call[2]);
163544881bd0Shenning.noren                                $this->inParagraph = true;
16360cecf9d5Sandi                            }
16370cecf9d5Sandi                        }
1638e1c10e4dSchris                    }
16390cecf9d5Sandi
16400cecf9d5Sandi                    if ( $storeCall ) {
164141624b31SChris Smith                        $this->addCall($call);
16420cecf9d5Sandi                    }
16430cecf9d5Sandi
16440cecf9d5Sandi                }
16450cecf9d5Sandi
16460cecf9d5Sandi
16470cecf9d5Sandi            } else {
16480cecf9d5Sandi
16490cecf9d5Sandi                // Unless there's already a block at the start, start a paragraph
1650f0891737Sandi                if ( !in_array($cname,$this->blockOpen) ) {
16510cecf9d5Sandi                    $this->calls[] = array('p_open',array(), $call[2]);
16520cecf9d5Sandi                    if ( $call[0] != 'eol' ) {
16530cecf9d5Sandi                        $this->calls[] = $call;
16540cecf9d5Sandi                    }
165544881bd0Shenning.noren                    $this->atStart = false;
165644881bd0Shenning.noren                    $this->inParagraph = true;
16570cecf9d5Sandi                } else {
165841624b31SChris Smith                    $this->addCall($call);
165944881bd0Shenning.noren                    $this->atStart = false;
16600cecf9d5Sandi                }
16610cecf9d5Sandi
16620cecf9d5Sandi            }
16630cecf9d5Sandi
16640cecf9d5Sandi        }
16650cecf9d5Sandi
16660cecf9d5Sandi        if ( $this->inParagraph ) {
1667f0891737Sandi            if ( $cname == 'p_open' ) {
16680cecf9d5Sandi                // Ditch the last call
16690cecf9d5Sandi                array_pop($this->calls);
1670f0891737Sandi            } else if ( !in_array($cname, $this->blockClose) ) {
1671506ae684Sandi                //$this->calls[] = array('p_close',array(), $call[2]);
1672506ae684Sandi                $this->closeParagraph($call[2]);
16730cecf9d5Sandi            } else {
16740cecf9d5Sandi                $last_call = array_pop($this->calls);
1675506ae684Sandi                //$this->calls[] = array('p_close',array(), $call[2]);
1676506ae684Sandi                $this->closeParagraph($call[2]);
16770cecf9d5Sandi                $this->calls[] = $last_call;
16780cecf9d5Sandi            }
16790cecf9d5Sandi        }
16800cecf9d5Sandi
16810cecf9d5Sandi        return $this->calls;
16820cecf9d5Sandi    }
16830cecf9d5Sandi
168444881bd0Shenning.noren    function addToStack($newStart = true) {
16850cecf9d5Sandi        $this->blockStack[] = array($this->atStart, $this->inParagraph);
16860cecf9d5Sandi        $this->atStart = $newStart;
168744881bd0Shenning.noren        $this->inParagraph = false;
16880cecf9d5Sandi    }
16890cecf9d5Sandi
16900cecf9d5Sandi    function removeFromStack() {
16910cecf9d5Sandi        $state = array_pop($this->blockStack);
16920cecf9d5Sandi        $this->atStart = $state[0];
16930cecf9d5Sandi        $this->inParagraph = $state[1];
16940cecf9d5Sandi    }
169541624b31SChris Smith
169641624b31SChris Smith    function addCall($call) {
169741624b31SChris Smith        $key = count($this->calls);
169841624b31SChris Smith        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
169941624b31SChris Smith            $this->calls[$key-1][1][0] .= $call[1][0];
170041624b31SChris Smith        } else {
170141624b31SChris Smith            $this->calls[] = $call;
170241624b31SChris Smith        }
170341624b31SChris Smith    }
17040cecf9d5Sandi}
17052a27e99aSandi
17064826ab45Sandi//Setup VIM: ex: et ts=4 enc=utf-8 :
1707