xref: /dokuwiki/inc/parser/handler.php (revision 51a883ed288fd1a0a93fc2723019a334d104b179)
10cecf9d5Sandi<?php
2fa8adffeSAndreas 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
70ea51e63SMatt Perry    var $Renderer = null;
80cecf9d5Sandi
90ea51e63SMatt Perry    var $CallWriter = null;
100cecf9d5Sandi
110cecf9d5Sandi    var $calls = array();
120cecf9d5Sandi
13e1c10e4dSchris    var $status = array(
1444881bd0Shenning.noren        'section' => false,
15e950d12fSChristopher Smith        'doublequote' => 0,
160cecf9d5Sandi    );
170cecf9d5Sandi
1844881bd0Shenning.noren    var $rewriteBlocks = true;
19b7c441b9SHarry Fuecks
200cecf9d5Sandi    function Doku_Handler() {
2167f9913dSAndreas Gohr        $this->CallWriter = new Doku_Handler_CallWriter($this);
220cecf9d5Sandi    }
230cecf9d5Sandi
24433bef32Sandi    function _addCall($handler, $args, $pos) {
250cecf9d5Sandi        $call = array($handler,$args, $pos);
260cecf9d5Sandi        $this->CallWriter->writeCall($call);
270cecf9d5Sandi    }
280cecf9d5Sandi
2982d61635Spierre.spring    function addPluginCall($plugin, $args, $state, $pos, $match) {
3082d61635Spierre.spring        $call = array('plugin',array($plugin, $args, $state, $match), $pos);
3104ebd214Schris        $this->CallWriter->writeCall($call);
3204ebd214Schris    }
3304ebd214Schris
34433bef32Sandi    function _finalize(){
35e1c10e4dSchris
36f4f02a0fSchris        $this->CallWriter->finalise();
37f4f02a0fSchris
38e1c10e4dSchris        if ( $this->status['section'] ) {
39e1c10e4dSchris            $last_call = end($this->calls);
40e1c10e4dSchris            array_push($this->calls,array('section_close',array(), $last_call[2]));
410cecf9d5Sandi        }
420cecf9d5Sandi
43b7c441b9SHarry Fuecks        if ( $this->rewriteBlocks ) {
4467f9913dSAndreas Gohr            $B = new Doku_Handler_Block();
450cecf9d5Sandi            $this->calls = $B->process($this->calls);
46b7c441b9SHarry Fuecks        }
47e0ad864eSchris
4824bb549bSchris        trigger_event('PARSER_HANDLER_DONE',$this);
490cecf9d5Sandi
500cecf9d5Sandi        array_unshift($this->calls,array('document_start',array(),0));
510cecf9d5Sandi        $last_call = end($this->calls);
520cecf9d5Sandi        array_push($this->calls,array('document_end',array(),$last_call[2]));
530cecf9d5Sandi    }
540cecf9d5Sandi
550cecf9d5Sandi    function fetch() {
560cecf9d5Sandi        $call = each($this->calls);
570cecf9d5Sandi        if ( $call ) {
580cecf9d5Sandi            return $call['value'];
590cecf9d5Sandi        }
6044881bd0Shenning.noren        return false;
610cecf9d5Sandi    }
62ee20e7d1Sandi
63ee20e7d1Sandi
64ee20e7d1Sandi    /**
65ee20e7d1Sandi     * Special plugin handler
66ee20e7d1Sandi     *
67ee20e7d1Sandi     * This handler is called for all modes starting with 'plugin_'.
68ee20e7d1Sandi     * An additional parameter with the plugin name is passed
69ee20e7d1Sandi     *
70ee20e7d1Sandi     * @author Andreas Gohr <andi@splitbrain.org>
71ee20e7d1Sandi     */
72ee20e7d1Sandi    function plugin($match, $state, $pos, $pluginname){
73ee20e7d1Sandi        $data = array($match);
74*51a883edSGerrit Uitslag        /** @var DokuWiki_Syntax_Plugin $plugin */
7545d5ad75SGerrit Uitslag        $plugin = plugin_load('syntax',$pluginname);
76a46d0d65SAndreas Gohr        if($plugin != null){
77f02a7d06Schris            $data = $plugin->handle($match, $state, $pos, $this);
78ee20e7d1Sandi        }
7913ecfb18SChris Smith        if ($data !== false) {
8082d61635Spierre.spring            $this->addPluginCall($pluginname,$data,$state,$pos,$match);
8113ecfb18SChris Smith        }
8244881bd0Shenning.noren        return true;
83ee20e7d1Sandi    }
840cecf9d5Sandi
850cecf9d5Sandi    function base($match, $state, $pos) {
860cecf9d5Sandi        switch ( $state ) {
870cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
88433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
8944881bd0Shenning.noren                return true;
900cecf9d5Sandi            break;
910cecf9d5Sandi        }
920cecf9d5Sandi    }
930cecf9d5Sandi
940cecf9d5Sandi    function header($match, $state, $pos) {
95d7e8115fSAndreas Gohr        // get level and title
96a4a2d4cfSAndreas Gohr        $title = trim($match);
97a4a2d4cfSAndreas Gohr        $level = 7 - strspn($title,'=');
98d7e8115fSAndreas Gohr        if($level < 1) $level = 1;
99a4a2d4cfSAndreas Gohr        $title = trim($title,'=');
100a4a2d4cfSAndreas Gohr        $title = trim($title);
1010cecf9d5Sandi
102e1c10e4dSchris        if ($this->status['section']) $this->_addCall('section_close',array(),$pos);
103e1c10e4dSchris
104433bef32Sandi        $this->_addCall('header',array($title,$level,$pos), $pos);
105e1c10e4dSchris
106e1c10e4dSchris        $this->_addCall('section_open',array($level),$pos);
10744881bd0Shenning.noren        $this->status['section'] = true;
10844881bd0Shenning.noren        return true;
1090cecf9d5Sandi    }
1100cecf9d5Sandi
1110cecf9d5Sandi    function notoc($match, $state, $pos) {
112e41c4da9SAndreas Gohr        $this->_addCall('notoc',array(),$pos);
11344881bd0Shenning.noren        return true;
1140cecf9d5Sandi    }
1150cecf9d5Sandi
1169dc2c2afSandi    function nocache($match, $state, $pos) {
1179dc2c2afSandi        $this->_addCall('nocache',array(),$pos);
11844881bd0Shenning.noren        return true;
1199dc2c2afSandi    }
1209dc2c2afSandi
1210cecf9d5Sandi    function linebreak($match, $state, $pos) {
122433bef32Sandi        $this->_addCall('linebreak',array(),$pos);
12344881bd0Shenning.noren        return true;
1240cecf9d5Sandi    }
1250cecf9d5Sandi
1260cecf9d5Sandi    function eol($match, $state, $pos) {
127433bef32Sandi        $this->_addCall('eol',array(),$pos);
12844881bd0Shenning.noren        return true;
1290cecf9d5Sandi    }
1300cecf9d5Sandi
1310cecf9d5Sandi    function hr($match, $state, $pos) {
132433bef32Sandi        $this->_addCall('hr',array(),$pos);
13344881bd0Shenning.noren        return true;
1340cecf9d5Sandi    }
1350cecf9d5Sandi
136433bef32Sandi    function _nestingTag($match, $state, $pos, $name) {
1370cecf9d5Sandi        switch ( $state ) {
1380cecf9d5Sandi            case DOKU_LEXER_ENTER:
139433bef32Sandi                $this->_addCall($name.'_open', array(), $pos);
1400cecf9d5Sandi            break;
1410cecf9d5Sandi            case DOKU_LEXER_EXIT:
142433bef32Sandi                $this->_addCall($name.'_close', array(), $pos);
1430cecf9d5Sandi            break;
1440cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
145433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
1460cecf9d5Sandi            break;
1470cecf9d5Sandi        }
1480cecf9d5Sandi    }
1490cecf9d5Sandi
1500cecf9d5Sandi    function strong($match, $state, $pos) {
151433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'strong');
15244881bd0Shenning.noren        return true;
1530cecf9d5Sandi    }
1540cecf9d5Sandi
1550cecf9d5Sandi    function emphasis($match, $state, $pos) {
156433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'emphasis');
15744881bd0Shenning.noren        return true;
1580cecf9d5Sandi    }
1590cecf9d5Sandi
1600cecf9d5Sandi    function underline($match, $state, $pos) {
161433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'underline');
16244881bd0Shenning.noren        return true;
1630cecf9d5Sandi    }
1640cecf9d5Sandi
1650cecf9d5Sandi    function monospace($match, $state, $pos) {
166433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'monospace');
16744881bd0Shenning.noren        return true;
1680cecf9d5Sandi    }
1690cecf9d5Sandi
1700cecf9d5Sandi    function subscript($match, $state, $pos) {
171433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'subscript');
17244881bd0Shenning.noren        return true;
1730cecf9d5Sandi    }
1740cecf9d5Sandi
1750cecf9d5Sandi    function superscript($match, $state, $pos) {
176433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'superscript');
17744881bd0Shenning.noren        return true;
1780cecf9d5Sandi    }
1790cecf9d5Sandi
1800cecf9d5Sandi    function deleted($match, $state, $pos) {
181433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'deleted');
18244881bd0Shenning.noren        return true;
1830cecf9d5Sandi    }
1840cecf9d5Sandi
1850cecf9d5Sandi
1860cecf9d5Sandi    function footnote($match, $state, $pos) {
1875587e44cSchris//        $this->_nestingTag($match, $state, $pos, 'footnote');
188742c66f8Schris        if (!isset($this->_footnote)) $this->_footnote = false;
1892fe7363dSchris
1905587e44cSchris        switch ( $state ) {
1915587e44cSchris            case DOKU_LEXER_ENTER:
1922fe7363dSchris                // footnotes can not be nested - however due to limitations in lexer it can't be prevented
1932fe7363dSchris                // we will still enter a new footnote mode, we just do nothing
194742c66f8Schris                if ($this->_footnote) {
1952fe7363dSchris                    $this->_addCall('cdata',array($match), $pos);
1962fe7363dSchris                    break;
1972fe7363dSchris                }
1982fe7363dSchris
199742c66f8Schris                $this->_footnote = true;
2002fe7363dSchris
20167f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Nest($this->CallWriter,'footnote_close');
2025587e44cSchris                $this->CallWriter = & $ReWriter;
2034a26ad85Schris                $this->_addCall('footnote_open', array(), $pos);
2045587e44cSchris            break;
2055587e44cSchris            case DOKU_LEXER_EXIT:
2062fe7363dSchris                // check whether we have already exitted the footnote mode, can happen if the modes were nested
207742c66f8Schris                if (!$this->_footnote) {
2082fe7363dSchris                    $this->_addCall('cdata',array($match), $pos);
2092fe7363dSchris                    break;
2102fe7363dSchris                }
2112fe7363dSchris
212742c66f8Schris                $this->_footnote = false;
2132fe7363dSchris
2145587e44cSchris                $this->_addCall('footnote_close', array(), $pos);
2155587e44cSchris                $this->CallWriter->process();
2165587e44cSchris                $ReWriter = & $this->CallWriter;
2175587e44cSchris                $this->CallWriter = & $ReWriter->CallWriter;
2185587e44cSchris            break;
2195587e44cSchris            case DOKU_LEXER_UNMATCHED:
2205587e44cSchris                $this->_addCall('cdata', array($match), $pos);
2215587e44cSchris            break;
2225587e44cSchris        }
22344881bd0Shenning.noren        return true;
2240cecf9d5Sandi    }
2250cecf9d5Sandi
2260cecf9d5Sandi    function listblock($match, $state, $pos) {
2270cecf9d5Sandi        switch ( $state ) {
2280cecf9d5Sandi            case DOKU_LEXER_ENTER:
22967f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_List($this->CallWriter);
2300cecf9d5Sandi                $this->CallWriter = & $ReWriter;
231433bef32Sandi                $this->_addCall('list_open', array($match), $pos);
2320cecf9d5Sandi            break;
2330cecf9d5Sandi            case DOKU_LEXER_EXIT:
234433bef32Sandi                $this->_addCall('list_close', array(), $pos);
2350cecf9d5Sandi                $this->CallWriter->process();
2360cecf9d5Sandi                $ReWriter = & $this->CallWriter;
2370cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
2380cecf9d5Sandi            break;
2390cecf9d5Sandi            case DOKU_LEXER_MATCHED:
240433bef32Sandi                $this->_addCall('list_item', array($match), $pos);
2410cecf9d5Sandi            break;
2420cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
243433bef32Sandi                $this->_addCall('cdata', array($match), $pos);
2440cecf9d5Sandi            break;
2450cecf9d5Sandi        }
24644881bd0Shenning.noren        return true;
2470cecf9d5Sandi    }
2480cecf9d5Sandi
2490cecf9d5Sandi    function unformatted($match, $state, $pos) {
2500cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
251433bef32Sandi            $this->_addCall('unformatted',array($match), $pos);
2520cecf9d5Sandi        }
25344881bd0Shenning.noren        return true;
2540cecf9d5Sandi    }
2550cecf9d5Sandi
2560cecf9d5Sandi    function php($match, $state, $pos) {
257df9add72Schris        global $conf;
2580cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
259433bef32Sandi            $this->_addCall('php',array($match), $pos);
2600cecf9d5Sandi        }
26144881bd0Shenning.noren        return true;
2620cecf9d5Sandi    }
2630cecf9d5Sandi
26407f89c3cSAnika Henke    function phpblock($match, $state, $pos) {
26507f89c3cSAnika Henke        global $conf;
26607f89c3cSAnika Henke        if ( $state == DOKU_LEXER_UNMATCHED ) {
26707f89c3cSAnika Henke            $this->_addCall('phpblock',array($match), $pos);
26807f89c3cSAnika Henke        }
26907f89c3cSAnika Henke        return true;
27007f89c3cSAnika Henke    }
27107f89c3cSAnika Henke
2720cecf9d5Sandi    function html($match, $state, $pos) {
273df9add72Schris        global $conf;
2740cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
275433bef32Sandi            $this->_addCall('html',array($match), $pos);
2760cecf9d5Sandi        }
27744881bd0Shenning.noren        return true;
2780cecf9d5Sandi    }
2790cecf9d5Sandi
28007f89c3cSAnika Henke    function htmlblock($match, $state, $pos) {
28107f89c3cSAnika Henke        global $conf;
28207f89c3cSAnika Henke        if ( $state == DOKU_LEXER_UNMATCHED ) {
28307f89c3cSAnika Henke            $this->_addCall('htmlblock',array($match), $pos);
28407f89c3cSAnika Henke        }
28507f89c3cSAnika Henke        return true;
28607f89c3cSAnika Henke    }
28707f89c3cSAnika Henke
2880cecf9d5Sandi    function preformatted($match, $state, $pos) {
2890cecf9d5Sandi        switch ( $state ) {
2900cecf9d5Sandi            case DOKU_LEXER_ENTER:
29167f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Preformatted($this->CallWriter);
2920cecf9d5Sandi                $this->CallWriter = & $ReWriter;
293433bef32Sandi                $this->_addCall('preformatted_start',array(), $pos);
2940cecf9d5Sandi            break;
2950cecf9d5Sandi            case DOKU_LEXER_EXIT:
296433bef32Sandi                $this->_addCall('preformatted_end',array(), $pos);
2970cecf9d5Sandi                $this->CallWriter->process();
2980cecf9d5Sandi                $ReWriter = & $this->CallWriter;
2990cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
3000cecf9d5Sandi            break;
3010cecf9d5Sandi            case DOKU_LEXER_MATCHED:
302433bef32Sandi                $this->_addCall('preformatted_newline',array(), $pos);
3030cecf9d5Sandi            break;
3040cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
305433bef32Sandi                $this->_addCall('preformatted_content',array($match), $pos);
3060cecf9d5Sandi            break;
3070cecf9d5Sandi        }
3080cecf9d5Sandi
30944881bd0Shenning.noren        return true;
3100cecf9d5Sandi    }
3110cecf9d5Sandi
3120cecf9d5Sandi    function quote($match, $state, $pos) {
3130cecf9d5Sandi
3140cecf9d5Sandi        switch ( $state ) {
3150cecf9d5Sandi
3160cecf9d5Sandi            case DOKU_LEXER_ENTER:
31767f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Quote($this->CallWriter);
3180cecf9d5Sandi                $this->CallWriter = & $ReWriter;
319433bef32Sandi                $this->_addCall('quote_start',array($match), $pos);
3200cecf9d5Sandi            break;
3210cecf9d5Sandi
3220cecf9d5Sandi            case DOKU_LEXER_EXIT:
323433bef32Sandi                $this->_addCall('quote_end',array(), $pos);
3240cecf9d5Sandi                $this->CallWriter->process();
3250cecf9d5Sandi                $ReWriter = & $this->CallWriter;
3260cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
3270cecf9d5Sandi            break;
3280cecf9d5Sandi
3290cecf9d5Sandi            case DOKU_LEXER_MATCHED:
330433bef32Sandi                $this->_addCall('quote_newline',array($match), $pos);
3310cecf9d5Sandi            break;
3320cecf9d5Sandi
3330cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
334433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
3350cecf9d5Sandi            break;
3360cecf9d5Sandi
3370cecf9d5Sandi        }
3380cecf9d5Sandi
33944881bd0Shenning.noren        return true;
3400cecf9d5Sandi    }
3410cecf9d5Sandi
3423d491f75SAndreas Gohr    function file($match, $state, $pos) {
3433d491f75SAndreas Gohr        return $this->code($match, $state, $pos, 'file');
3443d491f75SAndreas Gohr    }
3453d491f75SAndreas Gohr
3463d491f75SAndreas Gohr    function code($match, $state, $pos, $type='code') {
3473d491f75SAndreas Gohr        if ( $state == DOKU_LEXER_UNMATCHED ) {
3484b7f9e70STom N Harris            $matches = explode('>',$match,2);
3493d491f75SAndreas Gohr
3500139312bSAdrian Lang            $param = preg_split('/\s+/', $matches[0], 2, PREG_SPLIT_NO_EMPTY);
3510139312bSAdrian Lang            while(count($param) < 2) array_push($param, null);
3520139312bSAdrian Lang
3530139312bSAdrian Lang            // We shortcut html here.
3540139312bSAdrian Lang            if ($param[0] == 'html') $param[0] = 'html4strict';
3550139312bSAdrian Lang            if ($param[0] == '-') $param[0] = null;
3560139312bSAdrian Lang            array_unshift($param, $matches[1]);
3570139312bSAdrian Lang
3580139312bSAdrian Lang            $this->_addCall($type, $param, $pos);
3590cecf9d5Sandi        }
36044881bd0Shenning.noren        return true;
3610cecf9d5Sandi    }
3620cecf9d5Sandi
3630cecf9d5Sandi    function acronym($match, $state, $pos) {
364433bef32Sandi        $this->_addCall('acronym',array($match), $pos);
36544881bd0Shenning.noren        return true;
3660cecf9d5Sandi    }
3670cecf9d5Sandi
3680cecf9d5Sandi    function smiley($match, $state, $pos) {
369433bef32Sandi        $this->_addCall('smiley',array($match), $pos);
37044881bd0Shenning.noren        return true;
3710cecf9d5Sandi    }
3720cecf9d5Sandi
3730cecf9d5Sandi    function wordblock($match, $state, $pos) {
374433bef32Sandi        $this->_addCall('wordblock',array($match), $pos);
37544881bd0Shenning.noren        return true;
3760cecf9d5Sandi    }
3770cecf9d5Sandi
3780cecf9d5Sandi    function entity($match, $state, $pos) {
379433bef32Sandi        $this->_addCall('entity',array($match), $pos);
38044881bd0Shenning.noren        return true;
3810cecf9d5Sandi    }
3820cecf9d5Sandi
3830cecf9d5Sandi    function multiplyentity($match, $state, $pos) {
3840cecf9d5Sandi        preg_match_all('/\d+/',$match,$matches);
385433bef32Sandi        $this->_addCall('multiplyentity',array($matches[0][0],$matches[0][1]), $pos);
38644881bd0Shenning.noren        return true;
3870cecf9d5Sandi    }
3880cecf9d5Sandi
3890cecf9d5Sandi    function singlequoteopening($match, $state, $pos) {
390433bef32Sandi        $this->_addCall('singlequoteopening',array(), $pos);
39144881bd0Shenning.noren        return true;
3920cecf9d5Sandi    }
3930cecf9d5Sandi
3940cecf9d5Sandi    function singlequoteclosing($match, $state, $pos) {
395433bef32Sandi        $this->_addCall('singlequoteclosing',array(), $pos);
39644881bd0Shenning.noren        return true;
3970cecf9d5Sandi    }
3980cecf9d5Sandi
39957d757d1SAndreas Gohr    function apostrophe($match, $state, $pos) {
40057d757d1SAndreas Gohr        $this->_addCall('apostrophe',array(), $pos);
40157d757d1SAndreas Gohr        return true;
40257d757d1SAndreas Gohr    }
40357d757d1SAndreas Gohr
4040cecf9d5Sandi    function doublequoteopening($match, $state, $pos) {
405433bef32Sandi        $this->_addCall('doublequoteopening',array(), $pos);
406e950d12fSChristopher Smith        $this->status['doublequote']++;
40744881bd0Shenning.noren        return true;
4080cecf9d5Sandi    }
4090cecf9d5Sandi
4100cecf9d5Sandi    function doublequoteclosing($match, $state, $pos) {
411e950d12fSChristopher Smith        if ($this->status['doublequote'] <= 0) {
412e950d12fSChristopher Smith            $this->doublequoteopening($match, $state, $pos);
413e950d12fSChristopher Smith        } else {
414433bef32Sandi            $this->_addCall('doublequoteclosing',array(), $pos);
415e950d12fSChristopher Smith            $this->status['doublequote'] = max(0, --$this->status['doublequote']);
416e950d12fSChristopher Smith        }
41744881bd0Shenning.noren        return true;
4180cecf9d5Sandi    }
4190cecf9d5Sandi
4200cecf9d5Sandi    function camelcaselink($match, $state, $pos) {
421433bef32Sandi        $this->_addCall('camelcaselink',array($match), $pos);
42244881bd0Shenning.noren        return true;
4230cecf9d5Sandi    }
4240cecf9d5Sandi
4250cecf9d5Sandi    /*
4260cecf9d5Sandi    */
4270cecf9d5Sandi    function internallink($match, $state, $pos) {
4280cecf9d5Sandi        // Strip the opening and closing markup
4290cecf9d5Sandi        $link = preg_replace(array('/^\[\[/','/\]\]$/u'),'',$match);
4300cecf9d5Sandi
4310cecf9d5Sandi        // Split title from URL
4324b7f9e70STom N Harris        $link = explode('|',$link,2);
4330cecf9d5Sandi        if ( !isset($link[1]) ) {
4340ea51e63SMatt Perry            $link[1] = null;
4350cecf9d5Sandi        } else if ( preg_match('/^\{\{[^\}]+\}\}$/',$link[1]) ) {
4365578eb8fSandi            // If the title is an image, convert it to an array containing the image details
437b625487dSandi            $link[1] = Doku_Handler_Parse_Media($link[1]);
4380cecf9d5Sandi        }
4390b7c14c2Sandi        $link[0] = trim($link[0]);
4400cecf9d5Sandi
4410e1c636eSandi        //decide which kind of link it is
4420e1c636eSandi
443e08dda3fSAndreas Gohr        if ( preg_match('/^[a-zA-Z0-9\.]+>{1}.*$/u',$link[0]) ) {
4440e1c636eSandi            // Interwiki
4454b7f9e70STom N Harris            $interwiki = explode('>',$link[0],2);
446433bef32Sandi            $this->_addCall(
4470cecf9d5Sandi                'interwikilink',
4480cecf9d5Sandi                array($link[0],$link[1],strtolower($interwiki[0]),$interwiki[1]),
4490cecf9d5Sandi                $pos
4500cecf9d5Sandi                );
45115f1b77cSAndreas Gohr        }elseif ( preg_match('/^\\\\\\\\[^\\\\]+?\\\\/u',$link[0]) ) {
4520e1c636eSandi            // Windows Share
453433bef32Sandi            $this->_addCall(
4540cecf9d5Sandi                'windowssharelink',
4550cecf9d5Sandi                array($link[0],$link[1]),
4560cecf9d5Sandi                $pos
4570cecf9d5Sandi                );
4584468cb4cSAndreas Gohr        }elseif ( preg_match('#^([a-z0-9\-\.+]+?)://#i',$link[0]) ) {
4590e1c636eSandi            // external link (accepts all protocols)
460433bef32Sandi            $this->_addCall(
4610cecf9d5Sandi                    'externallink',
4620cecf9d5Sandi                    array($link[0],$link[1]),
4630cecf9d5Sandi                    $pos
4640cecf9d5Sandi                    );
4650a1d30bfSchris        }elseif ( preg_match('<'.PREG_PATTERN_VALID_EMAIL.'>',$link[0]) ) {
4660a1d30bfSchris            // E-Mail (pattern above is defined in inc/mail.php)
467a6755281Sandi            $this->_addCall(
468a6755281Sandi                'emaillink',
469a6755281Sandi                array($link[0],$link[1]),
470a6755281Sandi                $pos
471a6755281Sandi                );
4720b7c14c2Sandi        }elseif ( preg_match('!^#.+!',$link[0]) ){
4730b7c14c2Sandi            // local link
4740b7c14c2Sandi            $this->_addCall(
4750b7c14c2Sandi                'locallink',
4760b7c14c2Sandi                array(substr($link[0],1),$link[1]),
4770b7c14c2Sandi                $pos
4780b7c14c2Sandi                );
4790e1c636eSandi        }else{
4800e1c636eSandi            // internal link
481433bef32Sandi            $this->_addCall(
4820e1c636eSandi                'internallink',
4830e1c636eSandi                array($link[0],$link[1]),
4840e1c636eSandi                $pos
4850e1c636eSandi                );
4860cecf9d5Sandi        }
4870e1c636eSandi
48844881bd0Shenning.noren        return true;
4890cecf9d5Sandi    }
4900cecf9d5Sandi
4910cecf9d5Sandi    function filelink($match, $state, $pos) {
4920ea51e63SMatt Perry        $this->_addCall('filelink',array($match, null), $pos);
49344881bd0Shenning.noren        return true;
4940cecf9d5Sandi    }
4950cecf9d5Sandi
4960cecf9d5Sandi    function windowssharelink($match, $state, $pos) {
4970ea51e63SMatt Perry        $this->_addCall('windowssharelink',array($match, null), $pos);
49844881bd0Shenning.noren        return true;
4990cecf9d5Sandi    }
5000cecf9d5Sandi
5010cecf9d5Sandi    function media($match, $state, $pos) {
5020cecf9d5Sandi        $p = Doku_Handler_Parse_Media($match);
5030cecf9d5Sandi
504433bef32Sandi        $this->_addCall(
5050cecf9d5Sandi              $p['type'],
506dc673a5bSjoe.lapp              array($p['src'], $p['title'], $p['align'], $p['width'],
507dc673a5bSjoe.lapp                     $p['height'], $p['cache'], $p['linking']),
5080cecf9d5Sandi              $pos
5090cecf9d5Sandi             );
51044881bd0Shenning.noren        return true;
5110cecf9d5Sandi    }
5120cecf9d5Sandi
513b625487dSandi    function rss($match, $state, $pos) {
514b625487dSandi        $link = preg_replace(array('/^\{\{rss>/','/\}\}$/'),'',$match);
5153db95becSAndreas Gohr
5163db95becSAndreas Gohr        // get params
5173db95becSAndreas Gohr        list($link,$params) = explode(' ',$link,2);
5183db95becSAndreas Gohr
5193db95becSAndreas Gohr        $p = array();
5203db95becSAndreas Gohr        if(preg_match('/\b(\d+)\b/',$params,$match)){
5213db95becSAndreas Gohr            $p['max'] = $match[1];
5223db95becSAndreas Gohr        }else{
5233db95becSAndreas Gohr            $p['max'] = 8;
5243db95becSAndreas Gohr        }
5253db95becSAndreas Gohr        $p['reverse'] = (preg_match('/rev/',$params));
5263db95becSAndreas Gohr        $p['author']  = (preg_match('/\b(by|author)/',$params));
5273db95becSAndreas Gohr        $p['date']    = (preg_match('/\b(date)/',$params));
5283db95becSAndreas Gohr        $p['details'] = (preg_match('/\b(desc|detail)/',$params));
5293db95becSAndreas Gohr
5300a69dff7Schris        if (preg_match('/\b(\d+)([dhm])\b/',$params,$match)) {
5310a69dff7Schris            $period = array('d' => 86400, 'h' => 3600, 'm' => 60);
5320a69dff7Schris            $p['refresh'] = max(600,$match[1]*$period[$match[2]]);  // n * period in seconds, minimum 10 minutes
5330a69dff7Schris        } else {
5340a69dff7Schris            $p['refresh'] = 14400;   // default to 4 hours
5350a69dff7Schris        }
5360a69dff7Schris
5373db95becSAndreas Gohr        $this->_addCall('rss',array($link,$p),$pos);
53844881bd0Shenning.noren        return true;
539b625487dSandi    }
540b625487dSandi
5410cecf9d5Sandi    function externallink($match, $state, $pos) {
542da9f31c5SAndreas Gohr        $url   = $match;
543da9f31c5SAndreas Gohr        $title = null;
5440cecf9d5Sandi
545da9f31c5SAndreas Gohr        // add protocol on simple short URLs
546da9f31c5SAndreas Gohr        if(substr($url,0,3) == 'ftp' && (substr($url,0,6) != 'ftp://')){
547da9f31c5SAndreas Gohr            $title = $url;
548da9f31c5SAndreas Gohr            $url   = 'ftp://'.$url;
549da9f31c5SAndreas Gohr        }
550da9f31c5SAndreas Gohr        if(substr($url,0,3) == 'www' && (substr($url,0,7) != 'http://')){
551da9f31c5SAndreas Gohr            $title = $url;
552da9f31c5SAndreas Gohr            $url = 'http://'.$url;
553da9f31c5SAndreas Gohr        }
554da9f31c5SAndreas Gohr
555da9f31c5SAndreas Gohr        $this->_addCall('externallink',array($url, $title), $pos);
55644881bd0Shenning.noren        return true;
5570cecf9d5Sandi    }
5580cecf9d5Sandi
55971352defSandi    function emaillink($match, $state, $pos) {
5600cecf9d5Sandi        $email = preg_replace(array('/^</','/>$/'),'',$match);
5610ea51e63SMatt Perry        $this->_addCall('emaillink',array($email, null), $pos);
56244881bd0Shenning.noren        return true;
5630cecf9d5Sandi    }
5640cecf9d5Sandi
5650cecf9d5Sandi    function table($match, $state, $pos) {
5660cecf9d5Sandi        switch ( $state ) {
5670cecf9d5Sandi
5680cecf9d5Sandi            case DOKU_LEXER_ENTER:
5690cecf9d5Sandi
57067f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Table($this->CallWriter);
5710cecf9d5Sandi                $this->CallWriter = & $ReWriter;
5720cecf9d5Sandi
57390df9a4dSAdrian Lang                $this->_addCall('table_start', array($pos + 1), $pos);
5740cecf9d5Sandi                if ( trim($match) == '^' ) {
575433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
5760cecf9d5Sandi                } else {
577433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
5780cecf9d5Sandi                }
5790cecf9d5Sandi            break;
5800cecf9d5Sandi
5810cecf9d5Sandi            case DOKU_LEXER_EXIT:
58290df9a4dSAdrian Lang                $this->_addCall('table_end', array($pos), $pos);
5830cecf9d5Sandi                $this->CallWriter->process();
5840cecf9d5Sandi                $ReWriter = & $this->CallWriter;
5850cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
5860cecf9d5Sandi            break;
5870cecf9d5Sandi
5880cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
5890cecf9d5Sandi                if ( trim($match) != '' ) {
590433bef32Sandi                    $this->_addCall('cdata',array($match), $pos);
5910cecf9d5Sandi                }
5920cecf9d5Sandi            break;
5930cecf9d5Sandi
5940cecf9d5Sandi            case DOKU_LEXER_MATCHED:
5959ab75d9eSAndreas Gohr                if ( $match == ' ' ){
5969ab75d9eSAndreas Gohr                    $this->_addCall('cdata', array($match), $pos);
59725b97867Shakan.sandell                } else if ( preg_match('/:::/',$match) ) {
59825b97867Shakan.sandell                    $this->_addCall('rowspan', array($match), $pos);
599e205b721SAndreas Gohr                } else if ( preg_match('/\t+/',$match) ) {
6009ab75d9eSAndreas Gohr                    $this->_addCall('table_align', array($match), $pos);
601e205b721SAndreas Gohr                } else if ( preg_match('/ {2,}/',$match) ) {
602433bef32Sandi                    $this->_addCall('table_align', array($match), $pos);
6030cecf9d5Sandi                } else if ( $match == "\n|" ) {
604433bef32Sandi                    $this->_addCall('table_row', array(), $pos);
605433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
6060cecf9d5Sandi                } else if ( $match == "\n^" ) {
607433bef32Sandi                    $this->_addCall('table_row', array(), $pos);
608433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
6090cecf9d5Sandi                } else if ( $match == '|' ) {
610433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
6110cecf9d5Sandi                } else if ( $match == '^' ) {
612433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
6130cecf9d5Sandi                }
6140cecf9d5Sandi            break;
6150cecf9d5Sandi        }
61644881bd0Shenning.noren        return true;
6170cecf9d5Sandi    }
6180cecf9d5Sandi}
6190cecf9d5Sandi
6200cecf9d5Sandi//------------------------------------------------------------------------
6210cecf9d5Sandifunction Doku_Handler_Parse_Media($match) {
6220cecf9d5Sandi
6230cecf9d5Sandi    // Strip the opening and closing markup
6240cecf9d5Sandi    $link = preg_replace(array('/^\{\{/','/\}\}$/u'),'',$match);
6250cecf9d5Sandi
6260cecf9d5Sandi    // Split title from URL
6274b7f9e70STom N Harris    $link = explode('|',$link,2);
6280cecf9d5Sandi
6290cecf9d5Sandi    // Check alignment
6300cecf9d5Sandi    $ralign = (bool)preg_match('/^ /',$link[0]);
6310cecf9d5Sandi    $lalign = (bool)preg_match('/ $/',$link[0]);
6320cecf9d5Sandi
6330cecf9d5Sandi    // Logic = what's that ;)...
6340cecf9d5Sandi    if ( $lalign & $ralign ) {
6350cecf9d5Sandi        $align = 'center';
6360cecf9d5Sandi    } else if ( $ralign ) {
6370cecf9d5Sandi        $align = 'right';
6380cecf9d5Sandi    } else if ( $lalign ) {
6390cecf9d5Sandi        $align = 'left';
6400cecf9d5Sandi    } else {
6410ea51e63SMatt Perry        $align = null;
6420cecf9d5Sandi    }
6430cecf9d5Sandi
6440cecf9d5Sandi    // The title...
6450cecf9d5Sandi    if ( !isset($link[1]) ) {
6460ea51e63SMatt Perry        $link[1] = null;
6470cecf9d5Sandi    }
6480cecf9d5Sandi
6494826ab45Sandi    //remove aligning spaces
6504826ab45Sandi    $link[0] = trim($link[0]);
6510cecf9d5Sandi
6524826ab45Sandi    //split into src and parameters (using the very last questionmark)
6534826ab45Sandi    $pos = strrpos($link[0], '?');
6544826ab45Sandi    if($pos !== false){
6554826ab45Sandi        $src   = substr($link[0],0,$pos);
6564826ab45Sandi        $param = substr($link[0],$pos+1);
6570cecf9d5Sandi    }else{
6584826ab45Sandi        $src   = $link[0];
6594826ab45Sandi        $param = '';
6600cecf9d5Sandi    }
6610cecf9d5Sandi
6624826ab45Sandi    //parse width and height
6634826ab45Sandi    if(preg_match('#(\d+)(x(\d+))?#i',$param,$size)){
664443e135dSChristopher Smith        !empty($size[1]) ? $w = $size[1] : $w = null;
665443e135dSChristopher Smith        !empty($size[3]) ? $h = $size[3] : $h = null;
666fc1c55b1Shfuecks    } else {
6670ea51e63SMatt Perry        $w = null;
6680ea51e63SMatt Perry        $h = null;
6690cecf9d5Sandi    }
6700cecf9d5Sandi
671dc673a5bSjoe.lapp    //get linking command
672d35ab615Shenning.noren    if(preg_match('/nolink/i',$param)){
673dc673a5bSjoe.lapp        $linking = 'nolink';
674d35ab615Shenning.noren    }else if(preg_match('/direct/i',$param)){
675dc673a5bSjoe.lapp        $linking = 'direct';
6768acb3108SAndreas Gohr    }else if(preg_match('/linkonly/i',$param)){
6778acb3108SAndreas Gohr        $linking = 'linkonly';
678dc673a5bSjoe.lapp    }else{
679dc673a5bSjoe.lapp        $linking = 'details';
680dc673a5bSjoe.lapp    }
681dc673a5bSjoe.lapp
6824826ab45Sandi    //get caching command
6834826ab45Sandi    if (preg_match('/(nocache|recache)/i',$param,$cachemode)){
6844826ab45Sandi        $cache = $cachemode[1];
6850cecf9d5Sandi    }else{
6864826ab45Sandi        $cache = 'cache';
6870cecf9d5Sandi    }
6880cecf9d5Sandi
6890cecf9d5Sandi    // Check whether this is a local or remote image
6903e7e6067SKlap-in    if ( media_isexternal($src) ) {
6914826ab45Sandi        $call = 'externalmedia';
6920cecf9d5Sandi    } else {
6934826ab45Sandi        $call = 'internalmedia';
6940cecf9d5Sandi    }
6950cecf9d5Sandi
6960cecf9d5Sandi    $params = array(
6970cecf9d5Sandi        'type'=>$call,
6984826ab45Sandi        'src'=>$src,
6990cecf9d5Sandi        'title'=>$link[1],
7000cecf9d5Sandi        'align'=>$align,
7014826ab45Sandi        'width'=>$w,
7024826ab45Sandi        'height'=>$h,
7030cecf9d5Sandi        'cache'=>$cache,
704dc673a5bSjoe.lapp        'linking'=>$linking,
7050cecf9d5Sandi    );
7060cecf9d5Sandi
7070cecf9d5Sandi    return $params;
7080cecf9d5Sandi}
7090cecf9d5Sandi
7100cecf9d5Sandi//------------------------------------------------------------------------
7110cecf9d5Sandiclass Doku_Handler_CallWriter {
7120cecf9d5Sandi
7130cecf9d5Sandi    var $Handler;
7140cecf9d5Sandi
7150cecf9d5Sandi    function Doku_Handler_CallWriter(& $Handler) {
7160cecf9d5Sandi        $this->Handler = & $Handler;
7170cecf9d5Sandi    }
7180cecf9d5Sandi
7190cecf9d5Sandi    function writeCall($call) {
7200cecf9d5Sandi        $this->Handler->calls[] = $call;
7210cecf9d5Sandi    }
7220cecf9d5Sandi
7230cecf9d5Sandi    function writeCalls($calls) {
7240cecf9d5Sandi        $this->Handler->calls = array_merge($this->Handler->calls, $calls);
7250cecf9d5Sandi    }
726f4f02a0fSchris
727f4f02a0fSchris    // function is required, but since this call writer is first/highest in
728f4f02a0fSchris    // the chain it is not required to do anything
729f4f02a0fSchris    function finalise() {
7303893df8eSChristopher Smith        unset($this->Handler);
731f4f02a0fSchris    }
7320cecf9d5Sandi}
7330cecf9d5Sandi
7340cecf9d5Sandi//------------------------------------------------------------------------
7355587e44cSchris/**
7365587e44cSchris * Generic call writer class to handle nesting of rendering instructions
7375587e44cSchris * within a render instruction. Also see nest() method of renderer base class
7385587e44cSchris *
7395587e44cSchris * @author    Chris Smith <chris@jalakai.co.uk>
7405587e44cSchris */
7415587e44cSchrisclass Doku_Handler_Nest {
7425587e44cSchris
7435587e44cSchris    var $CallWriter;
7445587e44cSchris    var $calls = array();
7455587e44cSchris
7465587e44cSchris    var $closingInstruction;
7475587e44cSchris
7485587e44cSchris    /**
7495587e44cSchris     * constructor
7505587e44cSchris     *
7515587e44cSchris     * @param  object     $CallWriter     the renderers current call writer
7525587e44cSchris     * @param  string     $close          closing instruction name, this is required to properly terminate the
7535587e44cSchris     *                                    syntax mode if the document ends without a closing pattern
7545587e44cSchris     */
7555587e44cSchris    function Doku_Handler_Nest(& $CallWriter, $close="nest_close") {
7565587e44cSchris        $this->CallWriter = & $CallWriter;
7575587e44cSchris
7585587e44cSchris        $this->closingInstruction = $close;
7595587e44cSchris    }
7605587e44cSchris
7615587e44cSchris    function writeCall($call) {
7625587e44cSchris        $this->calls[] = $call;
7635587e44cSchris    }
7645587e44cSchris
7655587e44cSchris    function writeCalls($calls) {
7665587e44cSchris        $this->calls = array_merge($this->calls, $calls);
7675587e44cSchris    }
7685587e44cSchris
7695587e44cSchris    function finalise() {
7705587e44cSchris        $last_call = end($this->calls);
7715587e44cSchris        $this->writeCall(array($this->closingInstruction,array(), $last_call[2]));
7725587e44cSchris
7735587e44cSchris        $this->process();
7745587e44cSchris        $this->CallWriter->finalise();
7753893df8eSChristopher Smith        unset($this->CallWriter);
7765587e44cSchris    }
7775587e44cSchris
7785587e44cSchris    function process() {
77941624b31SChris Smith        // merge consecutive cdata
78041624b31SChris Smith        $unmerged_calls = $this->calls;
78141624b31SChris Smith        $this->calls = array();
78241624b31SChris Smith
78341624b31SChris Smith        foreach ($unmerged_calls as $call) $this->addCall($call);
78441624b31SChris Smith
7855587e44cSchris        $first_call = reset($this->calls);
7865587e44cSchris        $this->CallWriter->writeCall(array("nest", array($this->calls), $first_call[2]));
7875587e44cSchris    }
78841624b31SChris Smith
78941624b31SChris Smith    function addCall($call) {
79041624b31SChris Smith        $key = count($this->calls);
79141624b31SChris Smith        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
79241624b31SChris Smith            $this->calls[$key-1][1][0] .= $call[1][0];
79373c47f3dSChris Smith        } else if ($call[0] == 'eol') {
79473c47f3dSChris Smith            // do nothing (eol shouldn't be allowed, to counter preformatted fix in #1652 & #1699)
79541624b31SChris Smith        } else {
79641624b31SChris Smith            $this->calls[] = $call;
79741624b31SChris Smith        }
79841624b31SChris Smith    }
7995587e44cSchris}
8005587e44cSchris
8010cecf9d5Sandiclass Doku_Handler_List {
8020cecf9d5Sandi
8030cecf9d5Sandi    var $CallWriter;
8040cecf9d5Sandi
8050cecf9d5Sandi    var $calls = array();
8060cecf9d5Sandi    var $listCalls = array();
8070cecf9d5Sandi    var $listStack = array();
8080cecf9d5Sandi
8090cecf9d5Sandi    function Doku_Handler_List(& $CallWriter) {
8100cecf9d5Sandi        $this->CallWriter = & $CallWriter;
8110cecf9d5Sandi    }
8120cecf9d5Sandi
8130cecf9d5Sandi    function writeCall($call) {
8140cecf9d5Sandi        $this->calls[] = $call;
8150cecf9d5Sandi    }
8160cecf9d5Sandi
8170cecf9d5Sandi    // Probably not needed but just in case...
8180cecf9d5Sandi    function writeCalls($calls) {
8190cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
820f4f02a0fSchris#        $this->CallWriter->writeCalls($this->calls);
821f4f02a0fSchris    }
822f4f02a0fSchris
823f4f02a0fSchris    function finalise() {
824f4f02a0fSchris        $last_call = end($this->calls);
825f4f02a0fSchris        $this->writeCall(array('list_close',array(), $last_call[2]));
826f4f02a0fSchris
827f4f02a0fSchris        $this->process();
828f4f02a0fSchris        $this->CallWriter->finalise();
8293893df8eSChristopher Smith        unset($this->CallWriter);
8300cecf9d5Sandi    }
8310cecf9d5Sandi
8320cecf9d5Sandi    //------------------------------------------------------------------------
8330cecf9d5Sandi    function process() {
834f4f02a0fSchris
8350cecf9d5Sandi        foreach ( $this->calls as $call ) {
8360cecf9d5Sandi            switch ($call[0]) {
8370cecf9d5Sandi                case 'list_item':
8380cecf9d5Sandi                    $this->listOpen($call);
8390cecf9d5Sandi                break;
8400cecf9d5Sandi                case 'list_open':
8410cecf9d5Sandi                    $this->listStart($call);
8420cecf9d5Sandi                break;
8430cecf9d5Sandi                case 'list_close':
8440cecf9d5Sandi                    $this->listEnd($call);
8450cecf9d5Sandi                break;
8460cecf9d5Sandi                default:
8470cecf9d5Sandi                    $this->listContent($call);
8480cecf9d5Sandi                break;
8490cecf9d5Sandi            }
8500cecf9d5Sandi        }
8510cecf9d5Sandi
8520cecf9d5Sandi        $this->CallWriter->writeCalls($this->listCalls);
8530cecf9d5Sandi    }
8540cecf9d5Sandi
8550cecf9d5Sandi    //------------------------------------------------------------------------
8560cecf9d5Sandi    function listStart($call) {
8570cecf9d5Sandi        $depth = $this->interpretSyntax($call[1][0], $listType);
8580cecf9d5Sandi
8590cecf9d5Sandi        $this->initialDepth = $depth;
8600cecf9d5Sandi        $this->listStack[] = array($listType, $depth);
8610cecf9d5Sandi
8620cecf9d5Sandi        $this->listCalls[] = array('list'.$listType.'_open',array(),$call[2]);
8630cecf9d5Sandi        $this->listCalls[] = array('listitem_open',array(1),$call[2]);
8640cecf9d5Sandi        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
8650cecf9d5Sandi    }
8660cecf9d5Sandi
8670cecf9d5Sandi    //------------------------------------------------------------------------
8680cecf9d5Sandi    function listEnd($call) {
86944881bd0Shenning.noren        $closeContent = true;
8700cecf9d5Sandi
8710cecf9d5Sandi        while ( $list = array_pop($this->listStack) ) {
8720cecf9d5Sandi            if ( $closeContent ) {
8730cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
87444881bd0Shenning.noren                $closeContent = false;
8750cecf9d5Sandi            }
8760cecf9d5Sandi            $this->listCalls[] = array('listitem_close',array(),$call[2]);
8770cecf9d5Sandi            $this->listCalls[] = array('list'.$list[0].'_close', array(), $call[2]);
8780cecf9d5Sandi        }
8790cecf9d5Sandi    }
8800cecf9d5Sandi
8810cecf9d5Sandi    //------------------------------------------------------------------------
8820cecf9d5Sandi    function listOpen($call) {
8830cecf9d5Sandi        $depth = $this->interpretSyntax($call[1][0], $listType);
8840cecf9d5Sandi        $end = end($this->listStack);
8850cecf9d5Sandi
8860cecf9d5Sandi        // Not allowed to be shallower than initialDepth
8870cecf9d5Sandi        if ( $depth < $this->initialDepth ) {
8880cecf9d5Sandi            $depth = $this->initialDepth;
8890cecf9d5Sandi        }
8900cecf9d5Sandi
8910cecf9d5Sandi        //------------------------------------------------------------------------
8920cecf9d5Sandi        if ( $depth == $end[1] ) {
8930cecf9d5Sandi
8940cecf9d5Sandi            // Just another item in the list...
8950cecf9d5Sandi            if ( $listType == $end[0] ) {
8960cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
8970cecf9d5Sandi                $this->listCalls[] = array('listitem_close',array(),$call[2]);
8980cecf9d5Sandi                $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
8990cecf9d5Sandi                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9000cecf9d5Sandi
9010cecf9d5Sandi            // Switched list type...
9020cecf9d5Sandi            } else {
9030cecf9d5Sandi
9040cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9050cecf9d5Sandi                $this->listCalls[] = array('listitem_close',array(),$call[2]);
9060cecf9d5Sandi                $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
9070cecf9d5Sandi                $this->listCalls[] = array('list'.$listType.'_open', 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                array_pop($this->listStack);
9120cecf9d5Sandi                $this->listStack[] = array($listType, $depth);
9130cecf9d5Sandi            }
9140cecf9d5Sandi
9150cecf9d5Sandi        //------------------------------------------------------------------------
9160cecf9d5Sandi        // Getting deeper...
9170cecf9d5Sandi        } else if ( $depth > $end[1] ) {
9180cecf9d5Sandi
9190cecf9d5Sandi            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9200cecf9d5Sandi            $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9210cecf9d5Sandi            $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9220cecf9d5Sandi            $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9230cecf9d5Sandi
9240cecf9d5Sandi            $this->listStack[] = array($listType, $depth);
9250cecf9d5Sandi
9260cecf9d5Sandi        //------------------------------------------------------------------------
9270cecf9d5Sandi        // Getting shallower ( $depth < $end[1] )
9280cecf9d5Sandi        } else {
9290cecf9d5Sandi            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9300cecf9d5Sandi            $this->listCalls[] = array('listitem_close',array(),$call[2]);
9310cecf9d5Sandi            $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
9320cecf9d5Sandi
9330cecf9d5Sandi            // Throw away the end - done
9340cecf9d5Sandi            array_pop($this->listStack);
9350cecf9d5Sandi
9360cecf9d5Sandi            while (1) {
9370cecf9d5Sandi                $end = end($this->listStack);
9380cecf9d5Sandi
9390cecf9d5Sandi                if ( $end[1] <= $depth ) {
9400cecf9d5Sandi
9410cecf9d5Sandi                    // Normalize depths
9420cecf9d5Sandi                    $depth = $end[1];
9430cecf9d5Sandi
9440cecf9d5Sandi                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
9450cecf9d5Sandi
9460cecf9d5Sandi                    if ( $end[0] == $listType ) {
9470cecf9d5Sandi                        $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
9480cecf9d5Sandi                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9490cecf9d5Sandi
9500cecf9d5Sandi                    } else {
9510cecf9d5Sandi                        // Switching list type...
9520cecf9d5Sandi                        $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
9530cecf9d5Sandi                        $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9540cecf9d5Sandi                        $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9550cecf9d5Sandi                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9560cecf9d5Sandi
9570cecf9d5Sandi                        array_pop($this->listStack);
9580cecf9d5Sandi                        $this->listStack[] = array($listType, $depth);
9590cecf9d5Sandi                    }
9600cecf9d5Sandi
9610cecf9d5Sandi                    break;
9620cecf9d5Sandi
9630cecf9d5Sandi                // Haven't dropped down far enough yet.... ( $end[1] > $depth )
9640cecf9d5Sandi                } else {
9650cecf9d5Sandi
9660cecf9d5Sandi                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
9670cecf9d5Sandi                    $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
9680cecf9d5Sandi
9690cecf9d5Sandi                    array_pop($this->listStack);
9700cecf9d5Sandi
9710cecf9d5Sandi                }
9720cecf9d5Sandi
9730cecf9d5Sandi            }
9740cecf9d5Sandi
9750cecf9d5Sandi        }
9760cecf9d5Sandi    }
9770cecf9d5Sandi
9780cecf9d5Sandi    //------------------------------------------------------------------------
9790cecf9d5Sandi    function listContent($call) {
9800cecf9d5Sandi        $this->listCalls[] = $call;
9810cecf9d5Sandi    }
9820cecf9d5Sandi
9830cecf9d5Sandi    //------------------------------------------------------------------------
9840cecf9d5Sandi    function interpretSyntax($match, & $type) {
9850cecf9d5Sandi        if ( substr($match,-1) == '*' ) {
9860cecf9d5Sandi            $type = 'u';
9870cecf9d5Sandi        } else {
9880cecf9d5Sandi            $type = 'o';
9890cecf9d5Sandi        }
9904b7f9e70STom N Harris        // Is the +1 needed? It used to be count(explode(...))
9914b7f9e70STom N Harris        // but I don't think the number is seen outside this handler
9924b7f9e70STom N Harris        return substr_count(str_replace("\t",'  ',$match), '  ') + 1;
9930cecf9d5Sandi    }
9940cecf9d5Sandi}
9950cecf9d5Sandi
9960cecf9d5Sandi//------------------------------------------------------------------------
9970cecf9d5Sandiclass Doku_Handler_Preformatted {
9980cecf9d5Sandi
9990cecf9d5Sandi    var $CallWriter;
10000cecf9d5Sandi
10010cecf9d5Sandi    var $calls = array();
10020cecf9d5Sandi    var $pos;
10030cecf9d5Sandi    var $text ='';
10040cecf9d5Sandi
10050cecf9d5Sandi
10060cecf9d5Sandi
10070cecf9d5Sandi    function Doku_Handler_Preformatted(& $CallWriter) {
10080cecf9d5Sandi        $this->CallWriter = & $CallWriter;
10090cecf9d5Sandi    }
10100cecf9d5Sandi
10110cecf9d5Sandi    function writeCall($call) {
10120cecf9d5Sandi        $this->calls[] = $call;
10130cecf9d5Sandi    }
10140cecf9d5Sandi
10150cecf9d5Sandi    // Probably not needed but just in case...
10160cecf9d5Sandi    function writeCalls($calls) {
10170cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1018f4f02a0fSchris#        $this->CallWriter->writeCalls($this->calls);
1019f4f02a0fSchris    }
1020f4f02a0fSchris
1021f4f02a0fSchris    function finalise() {
1022f4f02a0fSchris        $last_call = end($this->calls);
1023f4f02a0fSchris        $this->writeCall(array('preformatted_end',array(), $last_call[2]));
1024f4f02a0fSchris
1025f4f02a0fSchris        $this->process();
1026f4f02a0fSchris        $this->CallWriter->finalise();
10273893df8eSChristopher Smith        unset($this->CallWriter);
10280cecf9d5Sandi    }
10290cecf9d5Sandi
10300cecf9d5Sandi    function process() {
10310cecf9d5Sandi        foreach ( $this->calls as $call ) {
10320cecf9d5Sandi            switch ($call[0]) {
10330cecf9d5Sandi                case 'preformatted_start':
10340cecf9d5Sandi                    $this->pos = $call[2];
10350cecf9d5Sandi                break;
10360cecf9d5Sandi                case 'preformatted_newline':
10370cecf9d5Sandi                    $this->text .= "\n";
10380cecf9d5Sandi                break;
10390cecf9d5Sandi                case 'preformatted_content':
10400cecf9d5Sandi                    $this->text .= $call[1][0];
10410cecf9d5Sandi                break;
10420cecf9d5Sandi                case 'preformatted_end':
104393a34bf3SChris Smith                    if (trim($this->text)) {
10440cecf9d5Sandi                        $this->CallWriter->writeCall(array('preformatted',array($this->text),$this->pos));
104593a34bf3SChris Smith                    }
104695c19ce7SChris Smith                    // see FS#1699 & FS#1652, add 'eol' instructions to ensure proper triggering of following p_open
104795c19ce7SChris Smith                    $this->CallWriter->writeCall(array('eol',array(),$this->pos));
104895c19ce7SChris Smith                    $this->CallWriter->writeCall(array('eol',array(),$this->pos));
10490cecf9d5Sandi                break;
10500cecf9d5Sandi            }
10510cecf9d5Sandi        }
10520cecf9d5Sandi    }
1053f4f02a0fSchris
10540cecf9d5Sandi}
10550cecf9d5Sandi
10560cecf9d5Sandi//------------------------------------------------------------------------
10570cecf9d5Sandiclass Doku_Handler_Quote {
10580cecf9d5Sandi
10590cecf9d5Sandi    var $CallWriter;
10600cecf9d5Sandi
10610cecf9d5Sandi    var $calls = array();
10620cecf9d5Sandi
10630cecf9d5Sandi    var $quoteCalls = array();
10640cecf9d5Sandi
10650cecf9d5Sandi    function Doku_Handler_Quote(& $CallWriter) {
10660cecf9d5Sandi        $this->CallWriter = & $CallWriter;
10670cecf9d5Sandi    }
10680cecf9d5Sandi
10690cecf9d5Sandi    function writeCall($call) {
10700cecf9d5Sandi        $this->calls[] = $call;
10710cecf9d5Sandi    }
10720cecf9d5Sandi
10730cecf9d5Sandi    // Probably not needed but just in case...
10740cecf9d5Sandi    function writeCalls($calls) {
10750cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1076f4f02a0fSchris    }
1077f4f02a0fSchris
1078f4f02a0fSchris    function finalise() {
1079f4f02a0fSchris        $last_call = end($this->calls);
1080f4f02a0fSchris        $this->writeCall(array('quote_end',array(), $last_call[2]));
1081f4f02a0fSchris
1082f4f02a0fSchris        $this->process();
1083f4f02a0fSchris        $this->CallWriter->finalise();
10843893df8eSChristopher Smith        unset($this->CallWriter);
10850cecf9d5Sandi    }
10860cecf9d5Sandi
10870cecf9d5Sandi    function process() {
10880cecf9d5Sandi
10890cecf9d5Sandi        $quoteDepth = 1;
10900cecf9d5Sandi
10910cecf9d5Sandi        foreach ( $this->calls as $call ) {
10920cecf9d5Sandi            switch ($call[0]) {
10930cecf9d5Sandi
10940cecf9d5Sandi                case 'quote_start':
10950cecf9d5Sandi
10960cecf9d5Sandi                    $this->quoteCalls[] = array('quote_open',array(),$call[2]);
10970cecf9d5Sandi
10980cecf9d5Sandi                case 'quote_newline':
10990cecf9d5Sandi
11000cecf9d5Sandi                    $quoteLength = $this->getDepth($call[1][0]);
11010cecf9d5Sandi
11020cecf9d5Sandi                    if ( $quoteLength > $quoteDepth ) {
11030cecf9d5Sandi                        $quoteDiff = $quoteLength - $quoteDepth;
11040cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11050cecf9d5Sandi                            $this->quoteCalls[] = array('quote_open',array(),$call[2]);
11060cecf9d5Sandi                        }
11070cecf9d5Sandi                    } else if ( $quoteLength < $quoteDepth ) {
11080cecf9d5Sandi                        $quoteDiff = $quoteDepth - $quoteLength;
11090cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11100cecf9d5Sandi                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11110cecf9d5Sandi                        }
111226426c64Schris                    } else {
111326426c64Schris                        if ($call[0] != 'quote_start') $this->quoteCalls[] = array('linebreak',array(),$call[2]);
11140cecf9d5Sandi                    }
11150cecf9d5Sandi
11160cecf9d5Sandi                    $quoteDepth = $quoteLength;
11170cecf9d5Sandi
11180cecf9d5Sandi                break;
11190cecf9d5Sandi
11200cecf9d5Sandi                case 'quote_end':
11210cecf9d5Sandi
11220cecf9d5Sandi                    if ( $quoteDepth > 1 ) {
11230cecf9d5Sandi                        $quoteDiff = $quoteDepth - 1;
11240cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11250cecf9d5Sandi                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11260cecf9d5Sandi                        }
11270cecf9d5Sandi                    }
11280cecf9d5Sandi
11290cecf9d5Sandi                    $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11300cecf9d5Sandi
11310cecf9d5Sandi                    $this->CallWriter->writeCalls($this->quoteCalls);
11320cecf9d5Sandi                break;
11330cecf9d5Sandi
11340cecf9d5Sandi                default:
11350cecf9d5Sandi                    $this->quoteCalls[] = $call;
11360cecf9d5Sandi                break;
11370cecf9d5Sandi            }
11380cecf9d5Sandi        }
11390cecf9d5Sandi    }
11400cecf9d5Sandi
11410cecf9d5Sandi    function getDepth($marker) {
11420cecf9d5Sandi        preg_match('/>{1,}/', $marker, $matches);
11430cecf9d5Sandi        $quoteLength = strlen($matches[0]);
11440cecf9d5Sandi        return $quoteLength;
11450cecf9d5Sandi    }
11460cecf9d5Sandi}
11470cecf9d5Sandi
11480cecf9d5Sandi//------------------------------------------------------------------------
11490cecf9d5Sandiclass Doku_Handler_Table {
11500cecf9d5Sandi
11510cecf9d5Sandi    var $CallWriter;
11520cecf9d5Sandi
11530cecf9d5Sandi    var $calls = array();
11540cecf9d5Sandi    var $tableCalls = array();
11550cecf9d5Sandi    var $maxCols = 0;
11560cecf9d5Sandi    var $maxRows = 1;
11570cecf9d5Sandi    var $currentCols = 0;
115844881bd0Shenning.noren    var $firstCell = false;
11590cecf9d5Sandi    var $lastCellType = 'tablecell';
11609060b8b0SChristopher Smith    var $inTableHead = true;
1161cef031c1SChristopher Smith    var $currentRow = array('tableheader' => 0, 'tablecell' => 0);
11629060b8b0SChristopher Smith    var $countTableHeadRows = 0;
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();
11833893df8eSChristopher Smith        unset($this->CallWriter);
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':
1194aa92c4ccSAdrian Lang                    $this->tableRowClose($call);
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':
1202aa92c4ccSAdrian Lang                    $this->tableRowClose($call);
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) {
121490df9a4dSAdrian Lang        $this->tableCalls[] = array('table_open',$call[1],$call[2]);
12150cecf9d5Sandi        $this->tableCalls[] = array('tablerow_open',array(),$call[2]);
121644881bd0Shenning.noren        $this->firstCell = true;
12170cecf9d5Sandi    }
12180cecf9d5Sandi
12190cecf9d5Sandi    function tableEnd($call) {
122007c2b1c7SAdrian Lang        $this->tableCalls[] = array('table_close',$call[1],$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++;
1230cef031c1SChristopher Smith        if ($this->inTableHead) {
1231cef031c1SChristopher Smith            $this->currentRow = array('tablecell' => 0, 'tableheader' => 0);
1232cef031c1SChristopher Smith        }
12330cecf9d5Sandi    }
12340cecf9d5Sandi
12350cecf9d5Sandi    function tableRowClose($call) {
1236cef031c1SChristopher Smith        if ($this->inTableHead && ($this->inTableHead = $this->isTableHeadRow())) {
12379060b8b0SChristopher Smith            $this->countTableHeadRows++;
12389060b8b0SChristopher Smith        }
12390cecf9d5Sandi        // Strip off final cell opening and anything after it
12400cecf9d5Sandi        while ( $discard = array_pop($this->tableCalls ) ) {
12410cecf9d5Sandi
12420cecf9d5Sandi            if ( $discard[0] == 'tablecell_open' || $discard[0] == 'tableheader_open') {
12430cecf9d5Sandi                break;
12440cecf9d5Sandi            }
1245cef031c1SChristopher Smith            if (!empty($this->currentRow[$discard[0]])) {
1246cef031c1SChristopher Smith                $this->currentRow[$discard[0]]--;
1247cef031c1SChristopher Smith            }
12480cecf9d5Sandi        }
1249aa92c4ccSAdrian Lang        $this->tableCalls[] = array('tablerow_close', array(), $call[2]);
12500cecf9d5Sandi
12510cecf9d5Sandi        if ( $this->currentCols > $this->maxCols ) {
12520cecf9d5Sandi            $this->maxCols = $this->currentCols;
12530cecf9d5Sandi        }
12540cecf9d5Sandi    }
12550cecf9d5Sandi
1256cef031c1SChristopher Smith    function isTableHeadRow() {
1257cef031c1SChristopher Smith        $td = $this->currentRow['tablecell'];
1258cef031c1SChristopher Smith        $th = $this->currentRow['tableheader'];
1259cef031c1SChristopher Smith
1260cef031c1SChristopher Smith        if (!$th || $td > 2) return false;
1261cef031c1SChristopher Smith        if (2*$td > $th) return false;
1262cef031c1SChristopher Smith
1263cef031c1SChristopher Smith        return true;
1264cef031c1SChristopher Smith    }
1265cef031c1SChristopher Smith
12660cecf9d5Sandi    function tableCell($call) {
1267cef031c1SChristopher Smith        if ($this->inTableHead) {
1268cef031c1SChristopher Smith            $this->currentRow[$call[0]]++;
12699060b8b0SChristopher Smith        }
12700cecf9d5Sandi        if ( !$this->firstCell ) {
12710cecf9d5Sandi
12720cecf9d5Sandi            // Increase the span
12730cecf9d5Sandi            $lastCall = end($this->tableCalls);
12740cecf9d5Sandi
12750cecf9d5Sandi            // A cell call which follows an open cell means an empty cell so span
12760cecf9d5Sandi            if ( $lastCall[0] == 'tablecell_open' || $lastCall[0] == 'tableheader_open' ) {
12770cecf9d5Sandi                 $this->tableCalls[] = array('colspan',array(),$call[2]);
12780cecf9d5Sandi
12790cecf9d5Sandi            }
12800cecf9d5Sandi
12810cecf9d5Sandi            $this->tableCalls[] = array($this->lastCellType.'_close',array(),$call[2]);
12820ea51e63SMatt Perry            $this->tableCalls[] = array($call[0].'_open',array(1,null,1),$call[2]);
12830cecf9d5Sandi            $this->lastCellType = $call[0];
12840cecf9d5Sandi
12850cecf9d5Sandi        } else {
12860cecf9d5Sandi
12870ea51e63SMatt Perry            $this->tableCalls[] = array($call[0].'_open',array(1,null,1),$call[2]);
12880cecf9d5Sandi            $this->lastCellType = $call[0];
128944881bd0Shenning.noren            $this->firstCell = false;
12900cecf9d5Sandi
12910cecf9d5Sandi        }
12920cecf9d5Sandi
12930cecf9d5Sandi        $this->currentCols++;
12940cecf9d5Sandi    }
12950cecf9d5Sandi
12960cecf9d5Sandi    function tableDefault($call) {
12970cecf9d5Sandi        $this->tableCalls[] = $call;
12980cecf9d5Sandi    }
12990cecf9d5Sandi
13000cecf9d5Sandi    function finalizeTable() {
13010cecf9d5Sandi
13020cecf9d5Sandi        // Add the max cols and rows to the table opening
13030cecf9d5Sandi        if ( $this->tableCalls[0][0] == 'table_open' ) {
13040cecf9d5Sandi            // Adjust to num cols not num col delimeters
13050cecf9d5Sandi            $this->tableCalls[0][1][] = $this->maxCols - 1;
13060cecf9d5Sandi            $this->tableCalls[0][1][] = $this->maxRows;
130790df9a4dSAdrian Lang            $this->tableCalls[0][1][] = array_shift($this->tableCalls[0][1]);
13080cecf9d5Sandi        } else {
13090cecf9d5Sandi            trigger_error('First element in table call list is not table_open');
13100cecf9d5Sandi        }
13110cecf9d5Sandi
13120cecf9d5Sandi        $lastRow = 0;
13130cecf9d5Sandi        $lastCell = 0;
131425b97867Shakan.sandell        $cellKey = array();
13150cecf9d5Sandi        $toDelete = array();
13160cecf9d5Sandi
1317cef031c1SChristopher Smith        // if still in tableheader, then there can be no table header
1318cef031c1SChristopher Smith        // as all rows can't be within <THEAD>
1319cef031c1SChristopher Smith        if ($this->inTableHead) {
1320cef031c1SChristopher Smith            $this->inTableHead = false;
1321cef031c1SChristopher Smith            $this->countTableHeadRows = 0;
1322cef031c1SChristopher Smith        }
1323cef031c1SChristopher Smith
13240cecf9d5Sandi        // Look for the colspan elements and increment the colspan on the
13250cecf9d5Sandi        // previous non-empty opening cell. Once done, delete all the cells
13260cecf9d5Sandi        // that contain colspans
13276606d6fcSAdrian Lang        for ($key = 0 ; $key < count($this->tableCalls) ; ++$key) {
13286606d6fcSAdrian Lang            $call = $this->tableCalls[$key];
13290cecf9d5Sandi
1330aa92c4ccSAdrian Lang            switch ($call[0]) {
13319060b8b0SChristopher Smith                case 'table_open' :
13329060b8b0SChristopher Smith                    if($this->countTableHeadRows) {
13339060b8b0SChristopher Smith                        array_splice($this->tableCalls, $key+1, 0, array(
13349060b8b0SChristopher Smith                              array('tablethead_open', array(), $call[2]))
13359060b8b0SChristopher Smith                        );
13369060b8b0SChristopher Smith                    }
13379060b8b0SChristopher Smith                    break;
13389060b8b0SChristopher Smith
1339aa92c4ccSAdrian Lang                case 'tablerow_open':
13400cecf9d5Sandi
134125b97867Shakan.sandell                    $lastRow++;
134225b97867Shakan.sandell                    $lastCell = 0;
1343aa92c4ccSAdrian Lang                    break;
13440cecf9d5Sandi
1345aa92c4ccSAdrian Lang                case 'tablecell_open':
1346aa92c4ccSAdrian Lang                case 'tableheader_open':
13470cecf9d5Sandi
134825b97867Shakan.sandell                    $lastCell++;
134925b97867Shakan.sandell                    $cellKey[$lastRow][$lastCell] = $key;
1350aa92c4ccSAdrian Lang                    break;
13510cecf9d5Sandi
1352aa92c4ccSAdrian Lang                case 'table_align':
13530cecf9d5Sandi
1354e03b8b2eSAdrian Lang                    $prev = in_array($this->tableCalls[$key-1][0], array('tablecell_open', 'tableheader_open'));
1355e03b8b2eSAdrian Lang                    $next = in_array($this->tableCalls[$key+1][0], array('tablecell_close', 'tableheader_close'));
1356e03b8b2eSAdrian Lang                    // If the cell is empty, align left
1357e03b8b2eSAdrian Lang                    if ($prev && $next) {
1358e03b8b2eSAdrian Lang                        $this->tableCalls[$key-1][1][1] = 'left';
1359e03b8b2eSAdrian Lang
13600cecf9d5Sandi                    // If the previous element was a cell open, align right
1361e03b8b2eSAdrian Lang                    } elseif ($prev) {
13620cecf9d5Sandi                        $this->tableCalls[$key-1][1][1] = 'right';
13630cecf9d5Sandi
1364e03b8b2eSAdrian Lang                    // If the next element is the close of an element, align either center or left
1365e03b8b2eSAdrian Lang                    } elseif ( $next) {
136625b97867Shakan.sandell                        if ( $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] == 'right' ) {
136725b97867Shakan.sandell                            $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] = 'center';
13680cecf9d5Sandi                        } else {
136925b97867Shakan.sandell                            $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] = 'left';
13700cecf9d5Sandi                        }
13710cecf9d5Sandi
13720cecf9d5Sandi                    }
13730cecf9d5Sandi
13740cecf9d5Sandi                    // Now convert the whitespace back to cdata
13750cecf9d5Sandi                    $this->tableCalls[$key][0] = 'cdata';
1376aa92c4ccSAdrian Lang                    break;
13770cecf9d5Sandi
1378aa92c4ccSAdrian Lang                case 'colspan':
13790cecf9d5Sandi
138044881bd0Shenning.noren                    $this->tableCalls[$key-1][1][0] = false;
13810cecf9d5Sandi
138225b97867Shakan.sandell                    for($i = $key-2; $i >= $cellKey[$lastRow][1]; $i--) {
13830cecf9d5Sandi
13840cecf9d5Sandi                        if ( $this->tableCalls[$i][0] == 'tablecell_open' || $this->tableCalls[$i][0] == 'tableheader_open' ) {
13850cecf9d5Sandi
138644881bd0Shenning.noren                            if ( false !== $this->tableCalls[$i][1][0] ) {
13870cecf9d5Sandi                                $this->tableCalls[$i][1][0]++;
13880cecf9d5Sandi                                break;
13890cecf9d5Sandi                            }
13900cecf9d5Sandi
13910cecf9d5Sandi                        }
13920cecf9d5Sandi                    }
13930cecf9d5Sandi
13940cecf9d5Sandi                    $toDelete[] = $key-1;
13950cecf9d5Sandi                    $toDelete[] = $key;
13960cecf9d5Sandi                    $toDelete[] = $key+1;
1397aa92c4ccSAdrian Lang                    break;
139825b97867Shakan.sandell
1399aa92c4ccSAdrian Lang                case 'rowspan':
140025b97867Shakan.sandell
140125b97867Shakan.sandell                    if ( $this->tableCalls[$key-1][0] == 'cdata' ) {
140225b97867Shakan.sandell                        // ignore rowspan if previous call was cdata (text mixed with :::) we don't have to check next call as that wont match regex
140325b97867Shakan.sandell                        $this->tableCalls[$key][0] = 'cdata';
140425b97867Shakan.sandell
140525b97867Shakan.sandell                    } else {
140625b97867Shakan.sandell
14076606d6fcSAdrian Lang                        $spanning_cell = null;
14089060b8b0SChristopher Smith
14099060b8b0SChristopher Smith                        // can't cross thead/tbody boundary
14109060b8b0SChristopher Smith                        if (!$this->countTableHeadRows || ($lastRow-1 != $this->countTableHeadRows)) {
141125b97867Shakan.sandell                            for($i = $lastRow-1; $i > 0; $i--) {
141225b97867Shakan.sandell
141325b97867Shakan.sandell                                if ( $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tablecell_open' || $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tableheader_open' ) {
141425b97867Shakan.sandell
14156606d6fcSAdrian Lang                                    if ($this->tableCalls[$cellKey[$i][$lastCell]][1][2] >= $lastRow - $i) {
14166606d6fcSAdrian Lang                                        $spanning_cell = $i;
141725b97867Shakan.sandell                                        break;
141825b97867Shakan.sandell                                    }
141925b97867Shakan.sandell
142025b97867Shakan.sandell                                }
142125b97867Shakan.sandell                            }
14229060b8b0SChristopher Smith                        }
14236606d6fcSAdrian Lang                        if (is_null($spanning_cell)) {
14246606d6fcSAdrian Lang                            // No spanning cell found, so convert this cell to
14256606d6fcSAdrian Lang                            // an empty one to avoid broken tables
14266a7e97d5SGerrit Uitslag                            $this->tableCalls[$key][0] = 'cdata';
14276a7e97d5SGerrit Uitslag                            $this->tableCalls[$key][1][0] = '';
14286606d6fcSAdrian Lang                            continue;
14296606d6fcSAdrian Lang                        }
14306606d6fcSAdrian Lang                        $this->tableCalls[$cellKey[$spanning_cell][$lastCell]][1][2]++;
14316606d6fcSAdrian Lang
14326606d6fcSAdrian Lang                        $this->tableCalls[$key-1][1][2] = false;
143325b97867Shakan.sandell
143425b97867Shakan.sandell                        $toDelete[] = $key-1;
143525b97867Shakan.sandell                        $toDelete[] = $key;
143625b97867Shakan.sandell                        $toDelete[] = $key+1;
143725b97867Shakan.sandell                    }
1438aa92c4ccSAdrian Lang                    break;
1439aa92c4ccSAdrian Lang
14406606d6fcSAdrian Lang                case 'tablerow_close':
14416606d6fcSAdrian Lang
14426606d6fcSAdrian Lang                    // Fix broken tables by adding missing cells
14436606d6fcSAdrian Lang                    while (++$lastCell < $this->maxCols) {
14446606d6fcSAdrian Lang                        array_splice($this->tableCalls, $key, 0, array(
14456606d6fcSAdrian Lang                               array('tablecell_open', array(1, null, 1), $call[2]),
14466606d6fcSAdrian Lang                               array('cdata', array(''), $call[2]),
14476606d6fcSAdrian Lang                               array('tablecell_close', array(), $call[2])));
14486606d6fcSAdrian Lang                        $key += 3;
14496606d6fcSAdrian Lang                    }
14506606d6fcSAdrian Lang
14519060b8b0SChristopher Smith                    if($this->countTableHeadRows == $lastRow) {
1452f05a1cc5SGerrit Uitslag                        array_splice($this->tableCalls, $key+1, 0, array(
1453f05a1cc5SGerrit Uitslag                              array('tablethead_close', array(), $call[2])));
1454f05a1cc5SGerrit Uitslag                    }
14556606d6fcSAdrian Lang                    break;
14566606d6fcSAdrian Lang
14570cecf9d5Sandi            }
14580cecf9d5Sandi        }
14590cecf9d5Sandi
14609ab75d9eSAndreas Gohr        // condense cdata
14619ab75d9eSAndreas Gohr        $cnt = count($this->tableCalls);
14629ab75d9eSAndreas Gohr        for( $key = 0; $key < $cnt; $key++){
14639ab75d9eSAndreas Gohr            if($this->tableCalls[$key][0] == 'cdata'){
14649ab75d9eSAndreas Gohr                $ckey = $key;
14659ab75d9eSAndreas Gohr                $key++;
14669ab75d9eSAndreas Gohr                while($this->tableCalls[$key][0] == 'cdata'){
14679ab75d9eSAndreas Gohr                    $this->tableCalls[$ckey][1][0] .= $this->tableCalls[$key][1][0];
14689ab75d9eSAndreas Gohr                    $toDelete[] = $key;
14699ab75d9eSAndreas Gohr                    $key++;
14709ab75d9eSAndreas Gohr                }
14719ab75d9eSAndreas Gohr                continue;
14729ab75d9eSAndreas Gohr            }
14739ab75d9eSAndreas Gohr        }
14749ab75d9eSAndreas Gohr
14750cecf9d5Sandi        foreach ( $toDelete as $delete ) {
14760cecf9d5Sandi            unset($this->tableCalls[$delete]);
14770cecf9d5Sandi        }
14780cecf9d5Sandi        $this->tableCalls = array_values($this->tableCalls);
14790cecf9d5Sandi    }
14800cecf9d5Sandi}
14810cecf9d5Sandi
14820cecf9d5Sandi
14832a27e99aSandi/**
14842a27e99aSandi * Handler for paragraphs
14852a27e99aSandi *
14860b7c14c2Sandi * @author Harry Fuecks <hfuecks@gmail.com>
14872a27e99aSandi */
14880cecf9d5Sandiclass Doku_Handler_Block {
14890cecf9d5Sandi    var $calls = array();
14909569a107SDanny Lin    var $skipEol = false;
149153bfcb59SChristopher Smith    var $inParagraph = false;
14920cecf9d5Sandi
1493af146da0Sandi    // Blocks these should not be inside paragraphs
14940cecf9d5Sandi    var $blockOpen = array(
14950cecf9d5Sandi            'header',
1496df9add72Schris            'listu_open','listo_open','listitem_open','listcontent_open',
1497f05a1cc5SGerrit Uitslag            'table_open','tablerow_open','tablecell_open','tableheader_open','tablethead_open',
14980cecf9d5Sandi            'quote_open',
149976aa94b7Schris            'code','file','hr','preformatted','rss',
150007f89c3cSAnika Henke            'htmlblock','phpblock',
15019569a107SDanny Lin            'footnote_open',
15020cecf9d5Sandi        );
15030cecf9d5Sandi
15040cecf9d5Sandi    var $blockClose = array(
15050cecf9d5Sandi            'header',
1506df9add72Schris            'listu_close','listo_close','listitem_close','listcontent_close',
1507f05a1cc5SGerrit Uitslag            'table_close','tablerow_close','tablecell_close','tableheader_close','tablethead_close',
15080cecf9d5Sandi            'quote_close',
150976aa94b7Schris            'code','file','hr','preformatted','rss',
151007f89c3cSAnika Henke            'htmlblock','phpblock',
15119569a107SDanny Lin            'footnote_close',
15120cecf9d5Sandi        );
15130cecf9d5Sandi
1514af146da0Sandi    // Stacks can contain paragraphs
15150cecf9d5Sandi    var $stackOpen = array(
15169569a107SDanny Lin        'section_open',
15170cecf9d5Sandi        );
15180cecf9d5Sandi
15190cecf9d5Sandi    var $stackClose = array(
15209569a107SDanny Lin        'section_close',
15210cecf9d5Sandi        );
15220cecf9d5Sandi
1523af146da0Sandi
1524af146da0Sandi    /**
1525af146da0Sandi     * Constructor. Adds loaded syntax plugins to the block and stack
1526af146da0Sandi     * arrays
1527af146da0Sandi     *
1528af146da0Sandi     * @author Andreas Gohr <andi@splitbrain.org>
1529af146da0Sandi     */
1530af146da0Sandi    function Doku_Handler_Block(){
1531af146da0Sandi        global $DOKU_PLUGINS;
1532af146da0Sandi        //check if syntax plugins were loaded
153303c4aec3Schris        if(empty($DOKU_PLUGINS['syntax'])) return;
1534af146da0Sandi        foreach($DOKU_PLUGINS['syntax'] as $n => $p){
1535af146da0Sandi            $ptype = $p->getPType();
1536af146da0Sandi            if($ptype == 'block'){
1537af146da0Sandi                $this->blockOpen[]  = 'plugin_'.$n;
1538af146da0Sandi                $this->blockClose[] = 'plugin_'.$n;
1539af146da0Sandi            }elseif($ptype == 'stack'){
1540af146da0Sandi                $this->stackOpen[]  = 'plugin_'.$n;
1541af146da0Sandi                $this->stackClose[] = 'plugin_'.$n;
1542af146da0Sandi            }
1543af146da0Sandi        }
1544af146da0Sandi    }
1545af146da0Sandi
1546b5a0b131SDanny Lin    function openParagraph($pos){
15479569a107SDanny Lin        if ($this->inParagraph) return;
1548b5a0b131SDanny Lin        $this->calls[] = array('p_open',array(), $pos);
1549b5a0b131SDanny Lin        $this->inParagraph = true;
15509569a107SDanny Lin        $this->skipEol = true;
1551b5a0b131SDanny Lin    }
1552b5a0b131SDanny Lin
15532a27e99aSandi    /**
15542a27e99aSandi     * Close a paragraph if needed
15552a27e99aSandi     *
15562a27e99aSandi     * This function makes sure there are no empty paragraphs on the stack
15572a27e99aSandi     *
15582a27e99aSandi     * @author Andreas Gohr <andi@splitbrain.org>
15592a27e99aSandi     */
1560506ae684Sandi    function closeParagraph($pos){
15619569a107SDanny Lin        if (!$this->inParagraph) return;
1562506ae684Sandi        // look back if there was any content - we don't want empty paragraphs
1563506ae684Sandi        $content = '';
1564faba9a35SAndreas Gohr        $ccount = count($this->calls);
1565faba9a35SAndreas Gohr        for($i=$ccount-1; $i>=0; $i--){
1566506ae684Sandi            if($this->calls[$i][0] == 'p_open'){
1567506ae684Sandi                break;
1568506ae684Sandi            }elseif($this->calls[$i][0] == 'cdata'){
1569506ae684Sandi                $content .= $this->calls[$i][1][0];
1570506ae684Sandi            }else{
1571506ae684Sandi                $content = 'found markup';
1572506ae684Sandi                break;
1573506ae684Sandi            }
1574506ae684Sandi        }
1575506ae684Sandi
1576506ae684Sandi        if(trim($content)==''){
1577506ae684Sandi            //remove the whole paragraph
1578a86cc527SAndreas Gohr            //array_splice($this->calls,$i); // <- this is much slower than the loop below
1579d8f7a7f3SAndreas Gohr            for($x=$ccount; $x>$i; $x--) array_pop($this->calls);
1580506ae684Sandi        }else{
15819569a107SDanny Lin            // remove ending linebreaks in the paragraph
15829569a107SDanny Lin            $i=count($this->calls)-1;
15839569a107SDanny Lin            if ($this->calls[$i][0] == 'cdata') $this->calls[$i][1][0] = rtrim($this->calls[$i][1][0],DOKU_PARSER_EOL);
1584506ae684Sandi            $this->calls[] = array('p_close',array(), $pos);
1585506ae684Sandi        }
1586e1c10e4dSchris
158744881bd0Shenning.noren        $this->inParagraph = false;
15889569a107SDanny Lin        $this->skipEol = true;
15890cecf9d5Sandi    }
159041624b31SChris Smith
159141624b31SChris Smith    function addCall($call) {
159241624b31SChris Smith        $key = count($this->calls);
159341624b31SChris Smith        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
159441624b31SChris Smith            $this->calls[$key-1][1][0] .= $call[1][0];
159541624b31SChris Smith        } else {
159641624b31SChris Smith            $this->calls[] = $call;
159741624b31SChris Smith        }
159841624b31SChris Smith    }
15999569a107SDanny Lin
16009569a107SDanny Lin    // simple version of addCall, without checking cdata
16019569a107SDanny Lin    function storeCall($call) {
16029569a107SDanny Lin        $this->calls[] = $call;
16039569a107SDanny Lin    }
16049569a107SDanny Lin
16059569a107SDanny Lin    /**
16069569a107SDanny Lin     * Processes the whole instruction stack to open and close paragraphs
16079569a107SDanny Lin     *
16089569a107SDanny Lin     * @author Harry Fuecks <hfuecks@gmail.com>
16099569a107SDanny Lin     * @author Andreas Gohr <andi@splitbrain.org>
16109569a107SDanny Lin     */
16119569a107SDanny Lin    function process($calls) {
16129569a107SDanny Lin        // open first paragraph
16139569a107SDanny Lin        $this->openParagraph(0);
16149569a107SDanny Lin        foreach ( $calls as $key => $call ) {
16159569a107SDanny Lin            $cname = $call[0];
16169569a107SDanny Lin            if ($cname == 'plugin') {
16179569a107SDanny Lin                $cname='plugin_'.$call[1][0];
16189569a107SDanny Lin                $plugin = true;
16199569a107SDanny Lin                $plugin_open = (($call[1][2] == DOKU_LEXER_ENTER) || ($call[1][2] == DOKU_LEXER_SPECIAL));
16209569a107SDanny Lin                $plugin_close = (($call[1][2] == DOKU_LEXER_EXIT) || ($call[1][2] == DOKU_LEXER_SPECIAL));
16219569a107SDanny Lin            } else {
16229569a107SDanny Lin                $plugin = false;
16239569a107SDanny Lin            }
16249569a107SDanny Lin            /* stack */
16259569a107SDanny Lin            if ( in_array($cname,$this->stackClose ) && (!$plugin || $plugin_close)) {
16269569a107SDanny Lin                $this->closeParagraph($call[2]);
16279569a107SDanny Lin                $this->storeCall($call);
16289569a107SDanny Lin                $this->openParagraph($call[2]);
16299569a107SDanny Lin                continue;
16309569a107SDanny Lin            }
16319569a107SDanny Lin            if ( in_array($cname,$this->stackOpen ) && (!$plugin || $plugin_open) ) {
16329569a107SDanny Lin                $this->closeParagraph($call[2]);
16339569a107SDanny Lin                $this->storeCall($call);
16349569a107SDanny Lin                $this->openParagraph($call[2]);
16359569a107SDanny Lin                continue;
16369569a107SDanny Lin            }
16379569a107SDanny Lin            /* block */
16389569a107SDanny Lin            // If it's a substition it opens and closes at the same call.
16399569a107SDanny Lin            // To make sure next paragraph is correctly started, let close go first.
16409569a107SDanny Lin            if ( in_array($cname, $this->blockClose) && (!$plugin || $plugin_close)) {
16419569a107SDanny Lin                $this->closeParagraph($call[2]);
16429569a107SDanny Lin                $this->storeCall($call);
16439569a107SDanny Lin                $this->openParagraph($call[2]);
16449569a107SDanny Lin                continue;
16459569a107SDanny Lin            }
16469569a107SDanny Lin            if ( in_array($cname, $this->blockOpen) && (!$plugin || $plugin_open)) {
16479569a107SDanny Lin                $this->closeParagraph($call[2]);
16489569a107SDanny Lin                $this->storeCall($call);
16499569a107SDanny Lin                continue;
16509569a107SDanny Lin            }
16519569a107SDanny Lin            /* eol */
16529569a107SDanny Lin            if ( $cname == 'eol' ) {
16539569a107SDanny Lin                // Check this isn't an eol instruction to skip...
16549569a107SDanny Lin                if ( !$this->skipEol ) {
16559569a107SDanny Lin                    // Next is EOL => double eol => mark as paragraph
16569569a107SDanny Lin                    if ( isset($calls[$key+1]) && $calls[$key+1][0] == 'eol' ) {
16579569a107SDanny Lin                        $this->closeParagraph($call[2]);
16589569a107SDanny Lin                        $this->openParagraph($call[2]);
16599569a107SDanny Lin                    } else {
16609569a107SDanny Lin                        //if this is just a single eol make a space from it
16619569a107SDanny Lin                        $this->addCall(array('cdata',array(DOKU_PARSER_EOL), $call[2]));
16629569a107SDanny Lin                    }
16639569a107SDanny Lin                }
16649569a107SDanny Lin                continue;
16659569a107SDanny Lin            }
16669569a107SDanny Lin            /* normal */
16679569a107SDanny Lin            $this->addCall($call);
16689569a107SDanny Lin            $this->skipEol = false;
16699569a107SDanny Lin        }
16709569a107SDanny Lin        // close last paragraph
16719569a107SDanny Lin        $call = end($this->calls);
16729569a107SDanny Lin        $this->closeParagraph($call[2]);
16739569a107SDanny Lin        return $this->calls;
16749569a107SDanny Lin    }
16750cecf9d5Sandi}
16762a27e99aSandi
1677e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
1678