xref: /dokuwiki/inc/parser/handler.php (revision 9060b8b02222ada7d08f4f6b77b4dd1e125fe5a2)
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,
150cecf9d5Sandi    );
160cecf9d5Sandi
1744881bd0Shenning.noren    var $rewriteBlocks = true;
18b7c441b9SHarry Fuecks
190cecf9d5Sandi    function Doku_Handler() {
2067f9913dSAndreas Gohr        $this->CallWriter = new Doku_Handler_CallWriter($this);
210cecf9d5Sandi    }
220cecf9d5Sandi
23433bef32Sandi    function _addCall($handler, $args, $pos) {
240cecf9d5Sandi        $call = array($handler,$args, $pos);
250cecf9d5Sandi        $this->CallWriter->writeCall($call);
260cecf9d5Sandi    }
270cecf9d5Sandi
2882d61635Spierre.spring    function addPluginCall($plugin, $args, $state, $pos, $match) {
2982d61635Spierre.spring        $call = array('plugin',array($plugin, $args, $state, $match), $pos);
3004ebd214Schris        $this->CallWriter->writeCall($call);
3104ebd214Schris    }
3204ebd214Schris
33433bef32Sandi    function _finalize(){
34e1c10e4dSchris
35f4f02a0fSchris        $this->CallWriter->finalise();
36f4f02a0fSchris
37e1c10e4dSchris        if ( $this->status['section'] ) {
38e1c10e4dSchris            $last_call = end($this->calls);
39e1c10e4dSchris            array_push($this->calls,array('section_close',array(), $last_call[2]));
400cecf9d5Sandi        }
410cecf9d5Sandi
42b7c441b9SHarry Fuecks        if ( $this->rewriteBlocks ) {
4367f9913dSAndreas Gohr            $B = new Doku_Handler_Block();
440cecf9d5Sandi            $this->calls = $B->process($this->calls);
45b7c441b9SHarry Fuecks        }
46e0ad864eSchris
4724bb549bSchris        trigger_event('PARSER_HANDLER_DONE',$this);
480cecf9d5Sandi
490cecf9d5Sandi        array_unshift($this->calls,array('document_start',array(),0));
500cecf9d5Sandi        $last_call = end($this->calls);
510cecf9d5Sandi        array_push($this->calls,array('document_end',array(),$last_call[2]));
520cecf9d5Sandi    }
530cecf9d5Sandi
540cecf9d5Sandi    function fetch() {
550cecf9d5Sandi        $call = each($this->calls);
560cecf9d5Sandi        if ( $call ) {
570cecf9d5Sandi            return $call['value'];
580cecf9d5Sandi        }
5944881bd0Shenning.noren        return false;
600cecf9d5Sandi    }
61ee20e7d1Sandi
62ee20e7d1Sandi
63ee20e7d1Sandi    /**
64ee20e7d1Sandi     * Special plugin handler
65ee20e7d1Sandi     *
66ee20e7d1Sandi     * This handler is called for all modes starting with 'plugin_'.
67ee20e7d1Sandi     * An additional parameter with the plugin name is passed
68ee20e7d1Sandi     *
69ee20e7d1Sandi     * @author Andreas Gohr <andi@splitbrain.org>
70ee20e7d1Sandi     */
71ee20e7d1Sandi    function plugin($match, $state, $pos, $pluginname){
72ee20e7d1Sandi        $data = array($match);
73a46d0d65SAndreas Gohr        $plugin =& plugin_load('syntax',$pluginname);
74a46d0d65SAndreas Gohr        if($plugin != null){
75f02a7d06Schris            $data = $plugin->handle($match, $state, $pos, $this);
76ee20e7d1Sandi        }
7713ecfb18SChris Smith        if ($data !== false) {
7882d61635Spierre.spring            $this->addPluginCall($pluginname,$data,$state,$pos,$match);
7913ecfb18SChris Smith        }
8044881bd0Shenning.noren        return true;
81ee20e7d1Sandi    }
820cecf9d5Sandi
830cecf9d5Sandi    function base($match, $state, $pos) {
840cecf9d5Sandi        switch ( $state ) {
850cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
86433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
8744881bd0Shenning.noren                return true;
880cecf9d5Sandi            break;
890cecf9d5Sandi        }
900cecf9d5Sandi    }
910cecf9d5Sandi
920cecf9d5Sandi    function header($match, $state, $pos) {
93d7e8115fSAndreas Gohr        // get level and title
94a4a2d4cfSAndreas Gohr        $title = trim($match);
95a4a2d4cfSAndreas Gohr        $level = 7 - strspn($title,'=');
96d7e8115fSAndreas Gohr        if($level < 1) $level = 1;
97a4a2d4cfSAndreas Gohr        $title = trim($title,'=');
98a4a2d4cfSAndreas Gohr        $title = trim($title);
990cecf9d5Sandi
100e1c10e4dSchris        if ($this->status['section']) $this->_addCall('section_close',array(),$pos);
101e1c10e4dSchris
102433bef32Sandi        $this->_addCall('header',array($title,$level,$pos), $pos);
103e1c10e4dSchris
104e1c10e4dSchris        $this->_addCall('section_open',array($level),$pos);
10544881bd0Shenning.noren        $this->status['section'] = true;
10644881bd0Shenning.noren        return true;
1070cecf9d5Sandi    }
1080cecf9d5Sandi
1090cecf9d5Sandi    function notoc($match, $state, $pos) {
110e41c4da9SAndreas Gohr        $this->_addCall('notoc',array(),$pos);
11144881bd0Shenning.noren        return true;
1120cecf9d5Sandi    }
1130cecf9d5Sandi
1149dc2c2afSandi    function nocache($match, $state, $pos) {
1159dc2c2afSandi        $this->_addCall('nocache',array(),$pos);
11644881bd0Shenning.noren        return true;
1179dc2c2afSandi    }
1189dc2c2afSandi
1190cecf9d5Sandi    function linebreak($match, $state, $pos) {
120433bef32Sandi        $this->_addCall('linebreak',array(),$pos);
12144881bd0Shenning.noren        return true;
1220cecf9d5Sandi    }
1230cecf9d5Sandi
1240cecf9d5Sandi    function eol($match, $state, $pos) {
125433bef32Sandi        $this->_addCall('eol',array(),$pos);
12644881bd0Shenning.noren        return true;
1270cecf9d5Sandi    }
1280cecf9d5Sandi
1290cecf9d5Sandi    function hr($match, $state, $pos) {
130433bef32Sandi        $this->_addCall('hr',array(),$pos);
13144881bd0Shenning.noren        return true;
1320cecf9d5Sandi    }
1330cecf9d5Sandi
134433bef32Sandi    function _nestingTag($match, $state, $pos, $name) {
1350cecf9d5Sandi        switch ( $state ) {
1360cecf9d5Sandi            case DOKU_LEXER_ENTER:
137433bef32Sandi                $this->_addCall($name.'_open', array(), $pos);
1380cecf9d5Sandi            break;
1390cecf9d5Sandi            case DOKU_LEXER_EXIT:
140433bef32Sandi                $this->_addCall($name.'_close', array(), $pos);
1410cecf9d5Sandi            break;
1420cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
143433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
1440cecf9d5Sandi            break;
1450cecf9d5Sandi        }
1460cecf9d5Sandi    }
1470cecf9d5Sandi
1480cecf9d5Sandi    function strong($match, $state, $pos) {
149433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'strong');
15044881bd0Shenning.noren        return true;
1510cecf9d5Sandi    }
1520cecf9d5Sandi
1530cecf9d5Sandi    function emphasis($match, $state, $pos) {
154433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'emphasis');
15544881bd0Shenning.noren        return true;
1560cecf9d5Sandi    }
1570cecf9d5Sandi
1580cecf9d5Sandi    function underline($match, $state, $pos) {
159433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'underline');
16044881bd0Shenning.noren        return true;
1610cecf9d5Sandi    }
1620cecf9d5Sandi
1630cecf9d5Sandi    function monospace($match, $state, $pos) {
164433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'monospace');
16544881bd0Shenning.noren        return true;
1660cecf9d5Sandi    }
1670cecf9d5Sandi
1680cecf9d5Sandi    function subscript($match, $state, $pos) {
169433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'subscript');
17044881bd0Shenning.noren        return true;
1710cecf9d5Sandi    }
1720cecf9d5Sandi
1730cecf9d5Sandi    function superscript($match, $state, $pos) {
174433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'superscript');
17544881bd0Shenning.noren        return true;
1760cecf9d5Sandi    }
1770cecf9d5Sandi
1780cecf9d5Sandi    function deleted($match, $state, $pos) {
179433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'deleted');
18044881bd0Shenning.noren        return true;
1810cecf9d5Sandi    }
1820cecf9d5Sandi
1830cecf9d5Sandi
1840cecf9d5Sandi    function footnote($match, $state, $pos) {
1855587e44cSchris//        $this->_nestingTag($match, $state, $pos, 'footnote');
186742c66f8Schris        if (!isset($this->_footnote)) $this->_footnote = false;
1872fe7363dSchris
1885587e44cSchris        switch ( $state ) {
1895587e44cSchris            case DOKU_LEXER_ENTER:
1902fe7363dSchris                // footnotes can not be nested - however due to limitations in lexer it can't be prevented
1912fe7363dSchris                // we will still enter a new footnote mode, we just do nothing
192742c66f8Schris                if ($this->_footnote) {
1932fe7363dSchris                    $this->_addCall('cdata',array($match), $pos);
1942fe7363dSchris                    break;
1952fe7363dSchris                }
1962fe7363dSchris
197742c66f8Schris                $this->_footnote = true;
1982fe7363dSchris
19967f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Nest($this->CallWriter,'footnote_close');
2005587e44cSchris                $this->CallWriter = & $ReWriter;
2014a26ad85Schris                $this->_addCall('footnote_open', array(), $pos);
2025587e44cSchris            break;
2035587e44cSchris            case DOKU_LEXER_EXIT:
2042fe7363dSchris                // check whether we have already exitted the footnote mode, can happen if the modes were nested
205742c66f8Schris                if (!$this->_footnote) {
2062fe7363dSchris                    $this->_addCall('cdata',array($match), $pos);
2072fe7363dSchris                    break;
2082fe7363dSchris                }
2092fe7363dSchris
210742c66f8Schris                $this->_footnote = false;
2112fe7363dSchris
2125587e44cSchris                $this->_addCall('footnote_close', array(), $pos);
2135587e44cSchris                $this->CallWriter->process();
2145587e44cSchris                $ReWriter = & $this->CallWriter;
2155587e44cSchris                $this->CallWriter = & $ReWriter->CallWriter;
2165587e44cSchris            break;
2175587e44cSchris            case DOKU_LEXER_UNMATCHED:
2185587e44cSchris                $this->_addCall('cdata', array($match), $pos);
2195587e44cSchris            break;
2205587e44cSchris        }
22144881bd0Shenning.noren        return true;
2220cecf9d5Sandi    }
2230cecf9d5Sandi
2240cecf9d5Sandi    function listblock($match, $state, $pos) {
2250cecf9d5Sandi        switch ( $state ) {
2260cecf9d5Sandi            case DOKU_LEXER_ENTER:
22767f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_List($this->CallWriter);
2280cecf9d5Sandi                $this->CallWriter = & $ReWriter;
229433bef32Sandi                $this->_addCall('list_open', array($match), $pos);
2300cecf9d5Sandi            break;
2310cecf9d5Sandi            case DOKU_LEXER_EXIT:
232433bef32Sandi                $this->_addCall('list_close', array(), $pos);
2330cecf9d5Sandi                $this->CallWriter->process();
2340cecf9d5Sandi                $ReWriter = & $this->CallWriter;
2350cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
2360cecf9d5Sandi            break;
2370cecf9d5Sandi            case DOKU_LEXER_MATCHED:
238433bef32Sandi                $this->_addCall('list_item', array($match), $pos);
2390cecf9d5Sandi            break;
2400cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
241433bef32Sandi                $this->_addCall('cdata', array($match), $pos);
2420cecf9d5Sandi            break;
2430cecf9d5Sandi        }
24444881bd0Shenning.noren        return true;
2450cecf9d5Sandi    }
2460cecf9d5Sandi
2470cecf9d5Sandi    function unformatted($match, $state, $pos) {
2480cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
249433bef32Sandi            $this->_addCall('unformatted',array($match), $pos);
2500cecf9d5Sandi        }
25144881bd0Shenning.noren        return true;
2520cecf9d5Sandi    }
2530cecf9d5Sandi
2540cecf9d5Sandi    function php($match, $state, $pos) {
255df9add72Schris        global $conf;
2560cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
257433bef32Sandi            $this->_addCall('php',array($match), $pos);
2580cecf9d5Sandi        }
25944881bd0Shenning.noren        return true;
2600cecf9d5Sandi    }
2610cecf9d5Sandi
26207f89c3cSAnika Henke    function phpblock($match, $state, $pos) {
26307f89c3cSAnika Henke        global $conf;
26407f89c3cSAnika Henke        if ( $state == DOKU_LEXER_UNMATCHED ) {
26507f89c3cSAnika Henke            $this->_addCall('phpblock',array($match), $pos);
26607f89c3cSAnika Henke        }
26707f89c3cSAnika Henke        return true;
26807f89c3cSAnika Henke    }
26907f89c3cSAnika Henke
2700cecf9d5Sandi    function html($match, $state, $pos) {
271df9add72Schris        global $conf;
2720cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
273433bef32Sandi            $this->_addCall('html',array($match), $pos);
2740cecf9d5Sandi        }
27544881bd0Shenning.noren        return true;
2760cecf9d5Sandi    }
2770cecf9d5Sandi
27807f89c3cSAnika Henke    function htmlblock($match, $state, $pos) {
27907f89c3cSAnika Henke        global $conf;
28007f89c3cSAnika Henke        if ( $state == DOKU_LEXER_UNMATCHED ) {
28107f89c3cSAnika Henke            $this->_addCall('htmlblock',array($match), $pos);
28207f89c3cSAnika Henke        }
28307f89c3cSAnika Henke        return true;
28407f89c3cSAnika Henke    }
28507f89c3cSAnika Henke
2860cecf9d5Sandi    function preformatted($match, $state, $pos) {
2870cecf9d5Sandi        switch ( $state ) {
2880cecf9d5Sandi            case DOKU_LEXER_ENTER:
28967f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Preformatted($this->CallWriter);
2900cecf9d5Sandi                $this->CallWriter = & $ReWriter;
291433bef32Sandi                $this->_addCall('preformatted_start',array(), $pos);
2920cecf9d5Sandi            break;
2930cecf9d5Sandi            case DOKU_LEXER_EXIT:
294433bef32Sandi                $this->_addCall('preformatted_end',array(), $pos);
2950cecf9d5Sandi                $this->CallWriter->process();
2960cecf9d5Sandi                $ReWriter = & $this->CallWriter;
2970cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
2980cecf9d5Sandi            break;
2990cecf9d5Sandi            case DOKU_LEXER_MATCHED:
300433bef32Sandi                $this->_addCall('preformatted_newline',array(), $pos);
3010cecf9d5Sandi            break;
3020cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
303433bef32Sandi                $this->_addCall('preformatted_content',array($match), $pos);
3040cecf9d5Sandi            break;
3050cecf9d5Sandi        }
3060cecf9d5Sandi
30744881bd0Shenning.noren        return true;
3080cecf9d5Sandi    }
3090cecf9d5Sandi
3100cecf9d5Sandi    function quote($match, $state, $pos) {
3110cecf9d5Sandi
3120cecf9d5Sandi        switch ( $state ) {
3130cecf9d5Sandi
3140cecf9d5Sandi            case DOKU_LEXER_ENTER:
31567f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Quote($this->CallWriter);
3160cecf9d5Sandi                $this->CallWriter = & $ReWriter;
317433bef32Sandi                $this->_addCall('quote_start',array($match), $pos);
3180cecf9d5Sandi            break;
3190cecf9d5Sandi
3200cecf9d5Sandi            case DOKU_LEXER_EXIT:
321433bef32Sandi                $this->_addCall('quote_end',array(), $pos);
3220cecf9d5Sandi                $this->CallWriter->process();
3230cecf9d5Sandi                $ReWriter = & $this->CallWriter;
3240cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
3250cecf9d5Sandi            break;
3260cecf9d5Sandi
3270cecf9d5Sandi            case DOKU_LEXER_MATCHED:
328433bef32Sandi                $this->_addCall('quote_newline',array($match), $pos);
3290cecf9d5Sandi            break;
3300cecf9d5Sandi
3310cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
332433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
3330cecf9d5Sandi            break;
3340cecf9d5Sandi
3350cecf9d5Sandi        }
3360cecf9d5Sandi
33744881bd0Shenning.noren        return true;
3380cecf9d5Sandi    }
3390cecf9d5Sandi
3403d491f75SAndreas Gohr    function file($match, $state, $pos) {
3413d491f75SAndreas Gohr        return $this->code($match, $state, $pos, 'file');
3423d491f75SAndreas Gohr    }
3433d491f75SAndreas Gohr
3443d491f75SAndreas Gohr    function code($match, $state, $pos, $type='code') {
3453d491f75SAndreas Gohr        if ( $state == DOKU_LEXER_UNMATCHED ) {
3464b7f9e70STom N Harris            $matches = explode('>',$match,2);
3473d491f75SAndreas Gohr
3480139312bSAdrian Lang            $param = preg_split('/\s+/', $matches[0], 2, PREG_SPLIT_NO_EMPTY);
3490139312bSAdrian Lang            while(count($param) < 2) array_push($param, null);
3500139312bSAdrian Lang
3510139312bSAdrian Lang            // We shortcut html here.
3520139312bSAdrian Lang            if ($param[0] == 'html') $param[0] = 'html4strict';
3530139312bSAdrian Lang            if ($param[0] == '-') $param[0] = null;
3540139312bSAdrian Lang            array_unshift($param, $matches[1]);
3550139312bSAdrian Lang
3560139312bSAdrian Lang            $this->_addCall($type, $param, $pos);
3570cecf9d5Sandi        }
35844881bd0Shenning.noren        return true;
3590cecf9d5Sandi    }
3600cecf9d5Sandi
3610cecf9d5Sandi    function acronym($match, $state, $pos) {
362433bef32Sandi        $this->_addCall('acronym',array($match), $pos);
36344881bd0Shenning.noren        return true;
3640cecf9d5Sandi    }
3650cecf9d5Sandi
3660cecf9d5Sandi    function smiley($match, $state, $pos) {
367433bef32Sandi        $this->_addCall('smiley',array($match), $pos);
36844881bd0Shenning.noren        return true;
3690cecf9d5Sandi    }
3700cecf9d5Sandi
3710cecf9d5Sandi    function wordblock($match, $state, $pos) {
372433bef32Sandi        $this->_addCall('wordblock',array($match), $pos);
37344881bd0Shenning.noren        return true;
3740cecf9d5Sandi    }
3750cecf9d5Sandi
3760cecf9d5Sandi    function entity($match, $state, $pos) {
377433bef32Sandi        $this->_addCall('entity',array($match), $pos);
37844881bd0Shenning.noren        return true;
3790cecf9d5Sandi    }
3800cecf9d5Sandi
3810cecf9d5Sandi    function multiplyentity($match, $state, $pos) {
3820cecf9d5Sandi        preg_match_all('/\d+/',$match,$matches);
383433bef32Sandi        $this->_addCall('multiplyentity',array($matches[0][0],$matches[0][1]), $pos);
38444881bd0Shenning.noren        return true;
3850cecf9d5Sandi    }
3860cecf9d5Sandi
3870cecf9d5Sandi    function singlequoteopening($match, $state, $pos) {
388433bef32Sandi        $this->_addCall('singlequoteopening',array(), $pos);
38944881bd0Shenning.noren        return true;
3900cecf9d5Sandi    }
3910cecf9d5Sandi
3920cecf9d5Sandi    function singlequoteclosing($match, $state, $pos) {
393433bef32Sandi        $this->_addCall('singlequoteclosing',array(), $pos);
39444881bd0Shenning.noren        return true;
3950cecf9d5Sandi    }
3960cecf9d5Sandi
39757d757d1SAndreas Gohr    function apostrophe($match, $state, $pos) {
39857d757d1SAndreas Gohr        $this->_addCall('apostrophe',array(), $pos);
39957d757d1SAndreas Gohr        return true;
40057d757d1SAndreas Gohr    }
40157d757d1SAndreas Gohr
4020cecf9d5Sandi    function doublequoteopening($match, $state, $pos) {
403433bef32Sandi        $this->_addCall('doublequoteopening',array(), $pos);
40444881bd0Shenning.noren        return true;
4050cecf9d5Sandi    }
4060cecf9d5Sandi
4070cecf9d5Sandi    function doublequoteclosing($match, $state, $pos) {
408433bef32Sandi        $this->_addCall('doublequoteclosing',array(), $pos);
40944881bd0Shenning.noren        return true;
4100cecf9d5Sandi    }
4110cecf9d5Sandi
4120cecf9d5Sandi    function camelcaselink($match, $state, $pos) {
413433bef32Sandi        $this->_addCall('camelcaselink',array($match), $pos);
41444881bd0Shenning.noren        return true;
4150cecf9d5Sandi    }
4160cecf9d5Sandi
4170cecf9d5Sandi    /*
4180cecf9d5Sandi    */
4190cecf9d5Sandi    function internallink($match, $state, $pos) {
4200cecf9d5Sandi        // Strip the opening and closing markup
4210cecf9d5Sandi        $link = preg_replace(array('/^\[\[/','/\]\]$/u'),'',$match);
4220cecf9d5Sandi
4230cecf9d5Sandi        // Split title from URL
4244b7f9e70STom N Harris        $link = explode('|',$link,2);
4250cecf9d5Sandi        if ( !isset($link[1]) ) {
4260ea51e63SMatt Perry            $link[1] = null;
4270cecf9d5Sandi        } else if ( preg_match('/^\{\{[^\}]+\}\}$/',$link[1]) ) {
4285578eb8fSandi            // If the title is an image, convert it to an array containing the image details
429b625487dSandi            $link[1] = Doku_Handler_Parse_Media($link[1]);
4300cecf9d5Sandi        }
4310b7c14c2Sandi        $link[0] = trim($link[0]);
4320cecf9d5Sandi
4330e1c636eSandi        //decide which kind of link it is
4340e1c636eSandi
435e08dda3fSAndreas Gohr        if ( preg_match('/^[a-zA-Z0-9\.]+>{1}.*$/u',$link[0]) ) {
4360e1c636eSandi            // Interwiki
4374b7f9e70STom N Harris            $interwiki = explode('>',$link[0],2);
438433bef32Sandi            $this->_addCall(
4390cecf9d5Sandi                'interwikilink',
4400cecf9d5Sandi                array($link[0],$link[1],strtolower($interwiki[0]),$interwiki[1]),
4410cecf9d5Sandi                $pos
4420cecf9d5Sandi                );
44315f1b77cSAndreas Gohr        }elseif ( preg_match('/^\\\\\\\\[^\\\\]+?\\\\/u',$link[0]) ) {
4440e1c636eSandi            // Windows Share
445433bef32Sandi            $this->_addCall(
4460cecf9d5Sandi                'windowssharelink',
4470cecf9d5Sandi                array($link[0],$link[1]),
4480cecf9d5Sandi                $pos
4490cecf9d5Sandi                );
4504468cb4cSAndreas Gohr        }elseif ( preg_match('#^([a-z0-9\-\.+]+?)://#i',$link[0]) ) {
4510e1c636eSandi            // external link (accepts all protocols)
452433bef32Sandi            $this->_addCall(
4530cecf9d5Sandi                    'externallink',
4540cecf9d5Sandi                    array($link[0],$link[1]),
4550cecf9d5Sandi                    $pos
4560cecf9d5Sandi                    );
4570a1d30bfSchris        }elseif ( preg_match('<'.PREG_PATTERN_VALID_EMAIL.'>',$link[0]) ) {
4580a1d30bfSchris            // E-Mail (pattern above is defined in inc/mail.php)
459a6755281Sandi            $this->_addCall(
460a6755281Sandi                'emaillink',
461a6755281Sandi                array($link[0],$link[1]),
462a6755281Sandi                $pos
463a6755281Sandi                );
4640b7c14c2Sandi        }elseif ( preg_match('!^#.+!',$link[0]) ){
4650b7c14c2Sandi            // local link
4660b7c14c2Sandi            $this->_addCall(
4670b7c14c2Sandi                'locallink',
4680b7c14c2Sandi                array(substr($link[0],1),$link[1]),
4690b7c14c2Sandi                $pos
4700b7c14c2Sandi                );
4710e1c636eSandi        }else{
4720e1c636eSandi            // internal link
473433bef32Sandi            $this->_addCall(
4740e1c636eSandi                'internallink',
4750e1c636eSandi                array($link[0],$link[1]),
4760e1c636eSandi                $pos
4770e1c636eSandi                );
4780cecf9d5Sandi        }
4790e1c636eSandi
48044881bd0Shenning.noren        return true;
4810cecf9d5Sandi    }
4820cecf9d5Sandi
4830cecf9d5Sandi    function filelink($match, $state, $pos) {
4840ea51e63SMatt Perry        $this->_addCall('filelink',array($match, null), $pos);
48544881bd0Shenning.noren        return true;
4860cecf9d5Sandi    }
4870cecf9d5Sandi
4880cecf9d5Sandi    function windowssharelink($match, $state, $pos) {
4890ea51e63SMatt Perry        $this->_addCall('windowssharelink',array($match, null), $pos);
49044881bd0Shenning.noren        return true;
4910cecf9d5Sandi    }
4920cecf9d5Sandi
4930cecf9d5Sandi    function media($match, $state, $pos) {
4940cecf9d5Sandi        $p = Doku_Handler_Parse_Media($match);
4950cecf9d5Sandi
496433bef32Sandi        $this->_addCall(
4970cecf9d5Sandi              $p['type'],
498dc673a5bSjoe.lapp              array($p['src'], $p['title'], $p['align'], $p['width'],
499dc673a5bSjoe.lapp                     $p['height'], $p['cache'], $p['linking']),
5000cecf9d5Sandi              $pos
5010cecf9d5Sandi             );
50244881bd0Shenning.noren        return true;
5030cecf9d5Sandi    }
5040cecf9d5Sandi
505b625487dSandi    function rss($match, $state, $pos) {
506b625487dSandi        $link = preg_replace(array('/^\{\{rss>/','/\}\}$/'),'',$match);
5073db95becSAndreas Gohr
5083db95becSAndreas Gohr        // get params
5093db95becSAndreas Gohr        list($link,$params) = explode(' ',$link,2);
5103db95becSAndreas Gohr
5113db95becSAndreas Gohr        $p = array();
5123db95becSAndreas Gohr        if(preg_match('/\b(\d+)\b/',$params,$match)){
5133db95becSAndreas Gohr            $p['max'] = $match[1];
5143db95becSAndreas Gohr        }else{
5153db95becSAndreas Gohr            $p['max'] = 8;
5163db95becSAndreas Gohr        }
5173db95becSAndreas Gohr        $p['reverse'] = (preg_match('/rev/',$params));
5183db95becSAndreas Gohr        $p['author']  = (preg_match('/\b(by|author)/',$params));
5193db95becSAndreas Gohr        $p['date']    = (preg_match('/\b(date)/',$params));
5203db95becSAndreas Gohr        $p['details'] = (preg_match('/\b(desc|detail)/',$params));
5213db95becSAndreas Gohr
5220a69dff7Schris        if (preg_match('/\b(\d+)([dhm])\b/',$params,$match)) {
5230a69dff7Schris            $period = array('d' => 86400, 'h' => 3600, 'm' => 60);
5240a69dff7Schris            $p['refresh'] = max(600,$match[1]*$period[$match[2]]);  // n * period in seconds, minimum 10 minutes
5250a69dff7Schris        } else {
5260a69dff7Schris            $p['refresh'] = 14400;   // default to 4 hours
5270a69dff7Schris        }
5280a69dff7Schris
5293db95becSAndreas Gohr        $this->_addCall('rss',array($link,$p),$pos);
53044881bd0Shenning.noren        return true;
531b625487dSandi    }
532b625487dSandi
5330cecf9d5Sandi    function externallink($match, $state, $pos) {
534da9f31c5SAndreas Gohr        $url   = $match;
535da9f31c5SAndreas Gohr        $title = null;
5360cecf9d5Sandi
537da9f31c5SAndreas Gohr        // add protocol on simple short URLs
538da9f31c5SAndreas Gohr        if(substr($url,0,3) == 'ftp' && (substr($url,0,6) != 'ftp://')){
539da9f31c5SAndreas Gohr            $title = $url;
540da9f31c5SAndreas Gohr            $url   = 'ftp://'.$url;
541da9f31c5SAndreas Gohr        }
542da9f31c5SAndreas Gohr        if(substr($url,0,3) == 'www' && (substr($url,0,7) != 'http://')){
543da9f31c5SAndreas Gohr            $title = $url;
544da9f31c5SAndreas Gohr            $url = 'http://'.$url;
545da9f31c5SAndreas Gohr        }
546da9f31c5SAndreas Gohr
547da9f31c5SAndreas Gohr        $this->_addCall('externallink',array($url, $title), $pos);
54844881bd0Shenning.noren        return true;
5490cecf9d5Sandi    }
5500cecf9d5Sandi
55171352defSandi    function emaillink($match, $state, $pos) {
5520cecf9d5Sandi        $email = preg_replace(array('/^</','/>$/'),'',$match);
5530ea51e63SMatt Perry        $this->_addCall('emaillink',array($email, null), $pos);
55444881bd0Shenning.noren        return true;
5550cecf9d5Sandi    }
5560cecf9d5Sandi
5570cecf9d5Sandi    function table($match, $state, $pos) {
5580cecf9d5Sandi        switch ( $state ) {
5590cecf9d5Sandi
5600cecf9d5Sandi            case DOKU_LEXER_ENTER:
5610cecf9d5Sandi
56267f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Table($this->CallWriter);
5630cecf9d5Sandi                $this->CallWriter = & $ReWriter;
5640cecf9d5Sandi
56590df9a4dSAdrian Lang                $this->_addCall('table_start', array($pos + 1), $pos);
5660cecf9d5Sandi                if ( trim($match) == '^' ) {
567433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
5680cecf9d5Sandi                } else {
569433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
5700cecf9d5Sandi                }
5710cecf9d5Sandi            break;
5720cecf9d5Sandi
5730cecf9d5Sandi            case DOKU_LEXER_EXIT:
57490df9a4dSAdrian Lang                $this->_addCall('table_end', array($pos), $pos);
5750cecf9d5Sandi                $this->CallWriter->process();
5760cecf9d5Sandi                $ReWriter = & $this->CallWriter;
5770cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
5780cecf9d5Sandi            break;
5790cecf9d5Sandi
5800cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
5810cecf9d5Sandi                if ( trim($match) != '' ) {
582433bef32Sandi                    $this->_addCall('cdata',array($match), $pos);
5830cecf9d5Sandi                }
5840cecf9d5Sandi            break;
5850cecf9d5Sandi
5860cecf9d5Sandi            case DOKU_LEXER_MATCHED:
5879ab75d9eSAndreas Gohr                if ( $match == ' ' ){
5889ab75d9eSAndreas Gohr                    $this->_addCall('cdata', array($match), $pos);
58925b97867Shakan.sandell                } else if ( preg_match('/:::/',$match) ) {
59025b97867Shakan.sandell                    $this->_addCall('rowspan', array($match), $pos);
591e205b721SAndreas Gohr                } else if ( preg_match('/\t+/',$match) ) {
5929ab75d9eSAndreas Gohr                    $this->_addCall('table_align', array($match), $pos);
593e205b721SAndreas Gohr                } else if ( preg_match('/ {2,}/',$match) ) {
594433bef32Sandi                    $this->_addCall('table_align', array($match), $pos);
5950cecf9d5Sandi                } else if ( $match == "\n|" ) {
596433bef32Sandi                    $this->_addCall('table_row', array(), $pos);
597433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
5980cecf9d5Sandi                } else if ( $match == "\n^" ) {
599433bef32Sandi                    $this->_addCall('table_row', array(), $pos);
600433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
6010cecf9d5Sandi                } else if ( $match == '|' ) {
602433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
6030cecf9d5Sandi                } else if ( $match == '^' ) {
604433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
6050cecf9d5Sandi                }
6060cecf9d5Sandi            break;
6070cecf9d5Sandi        }
60844881bd0Shenning.noren        return true;
6090cecf9d5Sandi    }
6100cecf9d5Sandi}
6110cecf9d5Sandi
6120cecf9d5Sandi//------------------------------------------------------------------------
6130cecf9d5Sandifunction Doku_Handler_Parse_Media($match) {
6140cecf9d5Sandi
6150cecf9d5Sandi    // Strip the opening and closing markup
6160cecf9d5Sandi    $link = preg_replace(array('/^\{\{/','/\}\}$/u'),'',$match);
6170cecf9d5Sandi
6180cecf9d5Sandi    // Split title from URL
6194b7f9e70STom N Harris    $link = explode('|',$link,2);
6200cecf9d5Sandi
6210cecf9d5Sandi    // Check alignment
6220cecf9d5Sandi    $ralign = (bool)preg_match('/^ /',$link[0]);
6230cecf9d5Sandi    $lalign = (bool)preg_match('/ $/',$link[0]);
6240cecf9d5Sandi
6250cecf9d5Sandi    // Logic = what's that ;)...
6260cecf9d5Sandi    if ( $lalign & $ralign ) {
6270cecf9d5Sandi        $align = 'center';
6280cecf9d5Sandi    } else if ( $ralign ) {
6290cecf9d5Sandi        $align = 'right';
6300cecf9d5Sandi    } else if ( $lalign ) {
6310cecf9d5Sandi        $align = 'left';
6320cecf9d5Sandi    } else {
6330ea51e63SMatt Perry        $align = null;
6340cecf9d5Sandi    }
6350cecf9d5Sandi
6360cecf9d5Sandi    // The title...
6370cecf9d5Sandi    if ( !isset($link[1]) ) {
6380ea51e63SMatt Perry        $link[1] = null;
6390cecf9d5Sandi    }
6400cecf9d5Sandi
6414826ab45Sandi    //remove aligning spaces
6424826ab45Sandi    $link[0] = trim($link[0]);
6430cecf9d5Sandi
6444826ab45Sandi    //split into src and parameters (using the very last questionmark)
6454826ab45Sandi    $pos = strrpos($link[0], '?');
6464826ab45Sandi    if($pos !== false){
6474826ab45Sandi        $src   = substr($link[0],0,$pos);
6484826ab45Sandi        $param = substr($link[0],$pos+1);
6490cecf9d5Sandi    }else{
6504826ab45Sandi        $src   = $link[0];
6514826ab45Sandi        $param = '';
6520cecf9d5Sandi    }
6530cecf9d5Sandi
6544826ab45Sandi    //parse width and height
6554826ab45Sandi    if(preg_match('#(\d+)(x(\d+))?#i',$param,$size)){
6560ea51e63SMatt Perry        ($size[1]) ? $w = $size[1] : $w = null;
6570ea51e63SMatt Perry        ($size[3]) ? $h = $size[3] : $h = null;
658fc1c55b1Shfuecks    } else {
6590ea51e63SMatt Perry        $w = null;
6600ea51e63SMatt Perry        $h = null;
6610cecf9d5Sandi    }
6620cecf9d5Sandi
663dc673a5bSjoe.lapp    //get linking command
664d35ab615Shenning.noren    if(preg_match('/nolink/i',$param)){
665dc673a5bSjoe.lapp        $linking = 'nolink';
666d35ab615Shenning.noren    }else if(preg_match('/direct/i',$param)){
667dc673a5bSjoe.lapp        $linking = 'direct';
6688acb3108SAndreas Gohr    }else if(preg_match('/linkonly/i',$param)){
6698acb3108SAndreas Gohr        $linking = 'linkonly';
670dc673a5bSjoe.lapp    }else{
671dc673a5bSjoe.lapp        $linking = 'details';
672dc673a5bSjoe.lapp    }
673dc673a5bSjoe.lapp
6744826ab45Sandi    //get caching command
6754826ab45Sandi    if (preg_match('/(nocache|recache)/i',$param,$cachemode)){
6764826ab45Sandi        $cache = $cachemode[1];
6770cecf9d5Sandi    }else{
6784826ab45Sandi        $cache = 'cache';
6790cecf9d5Sandi    }
6800cecf9d5Sandi
6810cecf9d5Sandi    // Check whether this is a local or remote image
6823e7e6067SKlap-in    if ( media_isexternal($src) ) {
6834826ab45Sandi        $call = 'externalmedia';
6840cecf9d5Sandi    } else {
6854826ab45Sandi        $call = 'internalmedia';
6860cecf9d5Sandi    }
6870cecf9d5Sandi
6880cecf9d5Sandi    $params = array(
6890cecf9d5Sandi        'type'=>$call,
6904826ab45Sandi        'src'=>$src,
6910cecf9d5Sandi        'title'=>$link[1],
6920cecf9d5Sandi        'align'=>$align,
6934826ab45Sandi        'width'=>$w,
6944826ab45Sandi        'height'=>$h,
6950cecf9d5Sandi        'cache'=>$cache,
696dc673a5bSjoe.lapp        'linking'=>$linking,
6970cecf9d5Sandi    );
6980cecf9d5Sandi
6990cecf9d5Sandi    return $params;
7000cecf9d5Sandi}
7010cecf9d5Sandi
7020cecf9d5Sandi//------------------------------------------------------------------------
7030cecf9d5Sandiclass Doku_Handler_CallWriter {
7040cecf9d5Sandi
7050cecf9d5Sandi    var $Handler;
7060cecf9d5Sandi
7070cecf9d5Sandi    function Doku_Handler_CallWriter(& $Handler) {
7080cecf9d5Sandi        $this->Handler = & $Handler;
7090cecf9d5Sandi    }
7100cecf9d5Sandi
7110cecf9d5Sandi    function writeCall($call) {
7120cecf9d5Sandi        $this->Handler->calls[] = $call;
7130cecf9d5Sandi    }
7140cecf9d5Sandi
7150cecf9d5Sandi    function writeCalls($calls) {
7160cecf9d5Sandi        $this->Handler->calls = array_merge($this->Handler->calls, $calls);
7170cecf9d5Sandi    }
718f4f02a0fSchris
719f4f02a0fSchris    // function is required, but since this call writer is first/highest in
720f4f02a0fSchris    // the chain it is not required to do anything
721f4f02a0fSchris    function finalise() {
7223893df8eSChristopher Smith        unset($this->Handler);
723f4f02a0fSchris    }
7240cecf9d5Sandi}
7250cecf9d5Sandi
7260cecf9d5Sandi//------------------------------------------------------------------------
7275587e44cSchris/**
7285587e44cSchris * Generic call writer class to handle nesting of rendering instructions
7295587e44cSchris * within a render instruction. Also see nest() method of renderer base class
7305587e44cSchris *
7315587e44cSchris * @author    Chris Smith <chris@jalakai.co.uk>
7325587e44cSchris */
7335587e44cSchrisclass Doku_Handler_Nest {
7345587e44cSchris
7355587e44cSchris    var $CallWriter;
7365587e44cSchris    var $calls = array();
7375587e44cSchris
7385587e44cSchris    var $closingInstruction;
7395587e44cSchris
7405587e44cSchris    /**
7415587e44cSchris     * constructor
7425587e44cSchris     *
7435587e44cSchris     * @param  object     $CallWriter     the renderers current call writer
7445587e44cSchris     * @param  string     $close          closing instruction name, this is required to properly terminate the
7455587e44cSchris     *                                    syntax mode if the document ends without a closing pattern
7465587e44cSchris     */
7475587e44cSchris    function Doku_Handler_Nest(& $CallWriter, $close="nest_close") {
7485587e44cSchris        $this->CallWriter = & $CallWriter;
7495587e44cSchris
7505587e44cSchris        $this->closingInstruction = $close;
7515587e44cSchris    }
7525587e44cSchris
7535587e44cSchris    function writeCall($call) {
7545587e44cSchris        $this->calls[] = $call;
7555587e44cSchris    }
7565587e44cSchris
7575587e44cSchris    function writeCalls($calls) {
7585587e44cSchris        $this->calls = array_merge($this->calls, $calls);
7595587e44cSchris    }
7605587e44cSchris
7615587e44cSchris    function finalise() {
7625587e44cSchris        $last_call = end($this->calls);
7635587e44cSchris        $this->writeCall(array($this->closingInstruction,array(), $last_call[2]));
7645587e44cSchris
7655587e44cSchris        $this->process();
7665587e44cSchris        $this->CallWriter->finalise();
7673893df8eSChristopher Smith        unset($this->CallWriter);
7685587e44cSchris    }
7695587e44cSchris
7705587e44cSchris    function process() {
77141624b31SChris Smith        // merge consecutive cdata
77241624b31SChris Smith        $unmerged_calls = $this->calls;
77341624b31SChris Smith        $this->calls = array();
77441624b31SChris Smith
77541624b31SChris Smith        foreach ($unmerged_calls as $call) $this->addCall($call);
77641624b31SChris Smith
7775587e44cSchris        $first_call = reset($this->calls);
7785587e44cSchris        $this->CallWriter->writeCall(array("nest", array($this->calls), $first_call[2]));
7795587e44cSchris    }
78041624b31SChris Smith
78141624b31SChris Smith    function addCall($call) {
78241624b31SChris Smith        $key = count($this->calls);
78341624b31SChris Smith        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
78441624b31SChris Smith            $this->calls[$key-1][1][0] .= $call[1][0];
78573c47f3dSChris Smith        } else if ($call[0] == 'eol') {
78673c47f3dSChris Smith            // do nothing (eol shouldn't be allowed, to counter preformatted fix in #1652 & #1699)
78741624b31SChris Smith        } else {
78841624b31SChris Smith            $this->calls[] = $call;
78941624b31SChris Smith        }
79041624b31SChris Smith    }
7915587e44cSchris}
7925587e44cSchris
7930cecf9d5Sandiclass Doku_Handler_List {
7940cecf9d5Sandi
7950cecf9d5Sandi    var $CallWriter;
7960cecf9d5Sandi
7970cecf9d5Sandi    var $calls = array();
7980cecf9d5Sandi    var $listCalls = array();
7990cecf9d5Sandi    var $listStack = array();
8000cecf9d5Sandi
8010cecf9d5Sandi    function Doku_Handler_List(& $CallWriter) {
8020cecf9d5Sandi        $this->CallWriter = & $CallWriter;
8030cecf9d5Sandi    }
8040cecf9d5Sandi
8050cecf9d5Sandi    function writeCall($call) {
8060cecf9d5Sandi        $this->calls[] = $call;
8070cecf9d5Sandi    }
8080cecf9d5Sandi
8090cecf9d5Sandi    // Probably not needed but just in case...
8100cecf9d5Sandi    function writeCalls($calls) {
8110cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
812f4f02a0fSchris#        $this->CallWriter->writeCalls($this->calls);
813f4f02a0fSchris    }
814f4f02a0fSchris
815f4f02a0fSchris    function finalise() {
816f4f02a0fSchris        $last_call = end($this->calls);
817f4f02a0fSchris        $this->writeCall(array('list_close',array(), $last_call[2]));
818f4f02a0fSchris
819f4f02a0fSchris        $this->process();
820f4f02a0fSchris        $this->CallWriter->finalise();
8213893df8eSChristopher Smith        unset($this->CallWriter);
8220cecf9d5Sandi    }
8230cecf9d5Sandi
8240cecf9d5Sandi    //------------------------------------------------------------------------
8250cecf9d5Sandi    function process() {
826f4f02a0fSchris
8270cecf9d5Sandi        foreach ( $this->calls as $call ) {
8280cecf9d5Sandi            switch ($call[0]) {
8290cecf9d5Sandi                case 'list_item':
8300cecf9d5Sandi                    $this->listOpen($call);
8310cecf9d5Sandi                break;
8320cecf9d5Sandi                case 'list_open':
8330cecf9d5Sandi                    $this->listStart($call);
8340cecf9d5Sandi                break;
8350cecf9d5Sandi                case 'list_close':
8360cecf9d5Sandi                    $this->listEnd($call);
8370cecf9d5Sandi                break;
8380cecf9d5Sandi                default:
8390cecf9d5Sandi                    $this->listContent($call);
8400cecf9d5Sandi                break;
8410cecf9d5Sandi            }
8420cecf9d5Sandi        }
8430cecf9d5Sandi
8440cecf9d5Sandi        $this->CallWriter->writeCalls($this->listCalls);
8450cecf9d5Sandi    }
8460cecf9d5Sandi
8470cecf9d5Sandi    //------------------------------------------------------------------------
8480cecf9d5Sandi    function listStart($call) {
8490cecf9d5Sandi        $depth = $this->interpretSyntax($call[1][0], $listType);
8500cecf9d5Sandi
8510cecf9d5Sandi        $this->initialDepth = $depth;
8520cecf9d5Sandi        $this->listStack[] = array($listType, $depth);
8530cecf9d5Sandi
8540cecf9d5Sandi        $this->listCalls[] = array('list'.$listType.'_open',array(),$call[2]);
8550cecf9d5Sandi        $this->listCalls[] = array('listitem_open',array(1),$call[2]);
8560cecf9d5Sandi        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
8570cecf9d5Sandi    }
8580cecf9d5Sandi
8590cecf9d5Sandi    //------------------------------------------------------------------------
8600cecf9d5Sandi    function listEnd($call) {
86144881bd0Shenning.noren        $closeContent = true;
8620cecf9d5Sandi
8630cecf9d5Sandi        while ( $list = array_pop($this->listStack) ) {
8640cecf9d5Sandi            if ( $closeContent ) {
8650cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
86644881bd0Shenning.noren                $closeContent = false;
8670cecf9d5Sandi            }
8680cecf9d5Sandi            $this->listCalls[] = array('listitem_close',array(),$call[2]);
8690cecf9d5Sandi            $this->listCalls[] = array('list'.$list[0].'_close', array(), $call[2]);
8700cecf9d5Sandi        }
8710cecf9d5Sandi    }
8720cecf9d5Sandi
8730cecf9d5Sandi    //------------------------------------------------------------------------
8740cecf9d5Sandi    function listOpen($call) {
8750cecf9d5Sandi        $depth = $this->interpretSyntax($call[1][0], $listType);
8760cecf9d5Sandi        $end = end($this->listStack);
8770cecf9d5Sandi
8780cecf9d5Sandi        // Not allowed to be shallower than initialDepth
8790cecf9d5Sandi        if ( $depth < $this->initialDepth ) {
8800cecf9d5Sandi            $depth = $this->initialDepth;
8810cecf9d5Sandi        }
8820cecf9d5Sandi
8830cecf9d5Sandi        //------------------------------------------------------------------------
8840cecf9d5Sandi        if ( $depth == $end[1] ) {
8850cecf9d5Sandi
8860cecf9d5Sandi            // Just another item in the list...
8870cecf9d5Sandi            if ( $listType == $end[0] ) {
8880cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
8890cecf9d5Sandi                $this->listCalls[] = array('listitem_close',array(),$call[2]);
8900cecf9d5Sandi                $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
8910cecf9d5Sandi                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
8920cecf9d5Sandi
8930cecf9d5Sandi            // Switched list type...
8940cecf9d5Sandi            } else {
8950cecf9d5Sandi
8960cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
8970cecf9d5Sandi                $this->listCalls[] = array('listitem_close',array(),$call[2]);
8980cecf9d5Sandi                $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
8990cecf9d5Sandi                $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9000cecf9d5Sandi                $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9010cecf9d5Sandi                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9020cecf9d5Sandi
9030cecf9d5Sandi                array_pop($this->listStack);
9040cecf9d5Sandi                $this->listStack[] = array($listType, $depth);
9050cecf9d5Sandi            }
9060cecf9d5Sandi
9070cecf9d5Sandi        //------------------------------------------------------------------------
9080cecf9d5Sandi        // Getting deeper...
9090cecf9d5Sandi        } else if ( $depth > $end[1] ) {
9100cecf9d5Sandi
9110cecf9d5Sandi            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9120cecf9d5Sandi            $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9130cecf9d5Sandi            $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9140cecf9d5Sandi            $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9150cecf9d5Sandi
9160cecf9d5Sandi            $this->listStack[] = array($listType, $depth);
9170cecf9d5Sandi
9180cecf9d5Sandi        //------------------------------------------------------------------------
9190cecf9d5Sandi        // Getting shallower ( $depth < $end[1] )
9200cecf9d5Sandi        } else {
9210cecf9d5Sandi            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9220cecf9d5Sandi            $this->listCalls[] = array('listitem_close',array(),$call[2]);
9230cecf9d5Sandi            $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
9240cecf9d5Sandi
9250cecf9d5Sandi            // Throw away the end - done
9260cecf9d5Sandi            array_pop($this->listStack);
9270cecf9d5Sandi
9280cecf9d5Sandi            while (1) {
9290cecf9d5Sandi                $end = end($this->listStack);
9300cecf9d5Sandi
9310cecf9d5Sandi                if ( $end[1] <= $depth ) {
9320cecf9d5Sandi
9330cecf9d5Sandi                    // Normalize depths
9340cecf9d5Sandi                    $depth = $end[1];
9350cecf9d5Sandi
9360cecf9d5Sandi                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
9370cecf9d5Sandi
9380cecf9d5Sandi                    if ( $end[0] == $listType ) {
9390cecf9d5Sandi                        $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
9400cecf9d5Sandi                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9410cecf9d5Sandi
9420cecf9d5Sandi                    } else {
9430cecf9d5Sandi                        // Switching list type...
9440cecf9d5Sandi                        $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
9450cecf9d5Sandi                        $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9460cecf9d5Sandi                        $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9470cecf9d5Sandi                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9480cecf9d5Sandi
9490cecf9d5Sandi                        array_pop($this->listStack);
9500cecf9d5Sandi                        $this->listStack[] = array($listType, $depth);
9510cecf9d5Sandi                    }
9520cecf9d5Sandi
9530cecf9d5Sandi                    break;
9540cecf9d5Sandi
9550cecf9d5Sandi                // Haven't dropped down far enough yet.... ( $end[1] > $depth )
9560cecf9d5Sandi                } else {
9570cecf9d5Sandi
9580cecf9d5Sandi                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
9590cecf9d5Sandi                    $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
9600cecf9d5Sandi
9610cecf9d5Sandi                    array_pop($this->listStack);
9620cecf9d5Sandi
9630cecf9d5Sandi                }
9640cecf9d5Sandi
9650cecf9d5Sandi            }
9660cecf9d5Sandi
9670cecf9d5Sandi        }
9680cecf9d5Sandi    }
9690cecf9d5Sandi
9700cecf9d5Sandi    //------------------------------------------------------------------------
9710cecf9d5Sandi    function listContent($call) {
9720cecf9d5Sandi        $this->listCalls[] = $call;
9730cecf9d5Sandi    }
9740cecf9d5Sandi
9750cecf9d5Sandi    //------------------------------------------------------------------------
9760cecf9d5Sandi    function interpretSyntax($match, & $type) {
9770cecf9d5Sandi        if ( substr($match,-1) == '*' ) {
9780cecf9d5Sandi            $type = 'u';
9790cecf9d5Sandi        } else {
9800cecf9d5Sandi            $type = 'o';
9810cecf9d5Sandi        }
9824b7f9e70STom N Harris        // Is the +1 needed? It used to be count(explode(...))
9834b7f9e70STom N Harris        // but I don't think the number is seen outside this handler
9844b7f9e70STom N Harris        return substr_count(str_replace("\t",'  ',$match), '  ') + 1;
9850cecf9d5Sandi    }
9860cecf9d5Sandi}
9870cecf9d5Sandi
9880cecf9d5Sandi//------------------------------------------------------------------------
9890cecf9d5Sandiclass Doku_Handler_Preformatted {
9900cecf9d5Sandi
9910cecf9d5Sandi    var $CallWriter;
9920cecf9d5Sandi
9930cecf9d5Sandi    var $calls = array();
9940cecf9d5Sandi    var $pos;
9950cecf9d5Sandi    var $text ='';
9960cecf9d5Sandi
9970cecf9d5Sandi
9980cecf9d5Sandi
9990cecf9d5Sandi    function Doku_Handler_Preformatted(& $CallWriter) {
10000cecf9d5Sandi        $this->CallWriter = & $CallWriter;
10010cecf9d5Sandi    }
10020cecf9d5Sandi
10030cecf9d5Sandi    function writeCall($call) {
10040cecf9d5Sandi        $this->calls[] = $call;
10050cecf9d5Sandi    }
10060cecf9d5Sandi
10070cecf9d5Sandi    // Probably not needed but just in case...
10080cecf9d5Sandi    function writeCalls($calls) {
10090cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1010f4f02a0fSchris#        $this->CallWriter->writeCalls($this->calls);
1011f4f02a0fSchris    }
1012f4f02a0fSchris
1013f4f02a0fSchris    function finalise() {
1014f4f02a0fSchris        $last_call = end($this->calls);
1015f4f02a0fSchris        $this->writeCall(array('preformatted_end',array(), $last_call[2]));
1016f4f02a0fSchris
1017f4f02a0fSchris        $this->process();
1018f4f02a0fSchris        $this->CallWriter->finalise();
10193893df8eSChristopher Smith        unset($this->CallWriter);
10200cecf9d5Sandi    }
10210cecf9d5Sandi
10220cecf9d5Sandi    function process() {
10230cecf9d5Sandi        foreach ( $this->calls as $call ) {
10240cecf9d5Sandi            switch ($call[0]) {
10250cecf9d5Sandi                case 'preformatted_start':
10260cecf9d5Sandi                    $this->pos = $call[2];
10270cecf9d5Sandi                break;
10280cecf9d5Sandi                case 'preformatted_newline':
10290cecf9d5Sandi                    $this->text .= "\n";
10300cecf9d5Sandi                break;
10310cecf9d5Sandi                case 'preformatted_content':
10320cecf9d5Sandi                    $this->text .= $call[1][0];
10330cecf9d5Sandi                break;
10340cecf9d5Sandi                case 'preformatted_end':
103593a34bf3SChris Smith                    if (trim($this->text)) {
10360cecf9d5Sandi                        $this->CallWriter->writeCall(array('preformatted',array($this->text),$this->pos));
103793a34bf3SChris Smith                    }
103895c19ce7SChris Smith                    // see FS#1699 & FS#1652, add 'eol' instructions to ensure proper triggering of following p_open
103995c19ce7SChris Smith                    $this->CallWriter->writeCall(array('eol',array(),$this->pos));
104095c19ce7SChris Smith                    $this->CallWriter->writeCall(array('eol',array(),$this->pos));
10410cecf9d5Sandi                break;
10420cecf9d5Sandi            }
10430cecf9d5Sandi        }
10440cecf9d5Sandi    }
1045f4f02a0fSchris
10460cecf9d5Sandi}
10470cecf9d5Sandi
10480cecf9d5Sandi//------------------------------------------------------------------------
10490cecf9d5Sandiclass Doku_Handler_Quote {
10500cecf9d5Sandi
10510cecf9d5Sandi    var $CallWriter;
10520cecf9d5Sandi
10530cecf9d5Sandi    var $calls = array();
10540cecf9d5Sandi
10550cecf9d5Sandi    var $quoteCalls = array();
10560cecf9d5Sandi
10570cecf9d5Sandi    function Doku_Handler_Quote(& $CallWriter) {
10580cecf9d5Sandi        $this->CallWriter = & $CallWriter;
10590cecf9d5Sandi    }
10600cecf9d5Sandi
10610cecf9d5Sandi    function writeCall($call) {
10620cecf9d5Sandi        $this->calls[] = $call;
10630cecf9d5Sandi    }
10640cecf9d5Sandi
10650cecf9d5Sandi    // Probably not needed but just in case...
10660cecf9d5Sandi    function writeCalls($calls) {
10670cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1068f4f02a0fSchris    }
1069f4f02a0fSchris
1070f4f02a0fSchris    function finalise() {
1071f4f02a0fSchris        $last_call = end($this->calls);
1072f4f02a0fSchris        $this->writeCall(array('quote_end',array(), $last_call[2]));
1073f4f02a0fSchris
1074f4f02a0fSchris        $this->process();
1075f4f02a0fSchris        $this->CallWriter->finalise();
10763893df8eSChristopher Smith        unset($this->CallWriter);
10770cecf9d5Sandi    }
10780cecf9d5Sandi
10790cecf9d5Sandi    function process() {
10800cecf9d5Sandi
10810cecf9d5Sandi        $quoteDepth = 1;
10820cecf9d5Sandi
10830cecf9d5Sandi        foreach ( $this->calls as $call ) {
10840cecf9d5Sandi            switch ($call[0]) {
10850cecf9d5Sandi
10860cecf9d5Sandi                case 'quote_start':
10870cecf9d5Sandi
10880cecf9d5Sandi                    $this->quoteCalls[] = array('quote_open',array(),$call[2]);
10890cecf9d5Sandi
10900cecf9d5Sandi                case 'quote_newline':
10910cecf9d5Sandi
10920cecf9d5Sandi                    $quoteLength = $this->getDepth($call[1][0]);
10930cecf9d5Sandi
10940cecf9d5Sandi                    if ( $quoteLength > $quoteDepth ) {
10950cecf9d5Sandi                        $quoteDiff = $quoteLength - $quoteDepth;
10960cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
10970cecf9d5Sandi                            $this->quoteCalls[] = array('quote_open',array(),$call[2]);
10980cecf9d5Sandi                        }
10990cecf9d5Sandi                    } else if ( $quoteLength < $quoteDepth ) {
11000cecf9d5Sandi                        $quoteDiff = $quoteDepth - $quoteLength;
11010cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11020cecf9d5Sandi                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11030cecf9d5Sandi                        }
110426426c64Schris                    } else {
110526426c64Schris                        if ($call[0] != 'quote_start') $this->quoteCalls[] = array('linebreak',array(),$call[2]);
11060cecf9d5Sandi                    }
11070cecf9d5Sandi
11080cecf9d5Sandi                    $quoteDepth = $quoteLength;
11090cecf9d5Sandi
11100cecf9d5Sandi                break;
11110cecf9d5Sandi
11120cecf9d5Sandi                case 'quote_end':
11130cecf9d5Sandi
11140cecf9d5Sandi                    if ( $quoteDepth > 1 ) {
11150cecf9d5Sandi                        $quoteDiff = $quoteDepth - 1;
11160cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11170cecf9d5Sandi                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11180cecf9d5Sandi                        }
11190cecf9d5Sandi                    }
11200cecf9d5Sandi
11210cecf9d5Sandi                    $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11220cecf9d5Sandi
11230cecf9d5Sandi                    $this->CallWriter->writeCalls($this->quoteCalls);
11240cecf9d5Sandi                break;
11250cecf9d5Sandi
11260cecf9d5Sandi                default:
11270cecf9d5Sandi                    $this->quoteCalls[] = $call;
11280cecf9d5Sandi                break;
11290cecf9d5Sandi            }
11300cecf9d5Sandi        }
11310cecf9d5Sandi    }
11320cecf9d5Sandi
11330cecf9d5Sandi    function getDepth($marker) {
11340cecf9d5Sandi        preg_match('/>{1,}/', $marker, $matches);
11350cecf9d5Sandi        $quoteLength = strlen($matches[0]);
11360cecf9d5Sandi        return $quoteLength;
11370cecf9d5Sandi    }
11380cecf9d5Sandi}
11390cecf9d5Sandi
11400cecf9d5Sandi//------------------------------------------------------------------------
11410cecf9d5Sandiclass Doku_Handler_Table {
11420cecf9d5Sandi
11430cecf9d5Sandi    var $CallWriter;
11440cecf9d5Sandi
11450cecf9d5Sandi    var $calls = array();
11460cecf9d5Sandi    var $tableCalls = array();
11470cecf9d5Sandi    var $maxCols = 0;
11480cecf9d5Sandi    var $maxRows = 1;
11490cecf9d5Sandi    var $currentCols = 0;
115044881bd0Shenning.noren    var $firstCell = false;
11510cecf9d5Sandi    var $lastCellType = 'tablecell';
1152*9060b8b0SChristopher Smith    var $inTableHead = true;
1153*9060b8b0SChristopher Smith    var $countTableHeadRows = 0;
11540cecf9d5Sandi
11550cecf9d5Sandi    function Doku_Handler_Table(& $CallWriter) {
11560cecf9d5Sandi        $this->CallWriter = & $CallWriter;
11570cecf9d5Sandi    }
11580cecf9d5Sandi
11590cecf9d5Sandi    function writeCall($call) {
11600cecf9d5Sandi        $this->calls[] = $call;
11610cecf9d5Sandi    }
11620cecf9d5Sandi
11630cecf9d5Sandi    // Probably not needed but just in case...
11640cecf9d5Sandi    function writeCalls($calls) {
11650cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1166f4f02a0fSchris    }
1167f4f02a0fSchris
1168f4f02a0fSchris    function finalise() {
1169f4f02a0fSchris        $last_call = end($this->calls);
1170f4f02a0fSchris        $this->writeCall(array('table_end',array(), $last_call[2]));
1171f4f02a0fSchris
1172f4f02a0fSchris        $this->process();
1173f4f02a0fSchris        $this->CallWriter->finalise();
11743893df8eSChristopher Smith        unset($this->CallWriter);
11750cecf9d5Sandi    }
11760cecf9d5Sandi
11770cecf9d5Sandi    //------------------------------------------------------------------------
11780cecf9d5Sandi    function process() {
11790cecf9d5Sandi        foreach ( $this->calls as $call ) {
11800cecf9d5Sandi            switch ( $call[0] ) {
11810cecf9d5Sandi                case 'table_start':
11820cecf9d5Sandi                    $this->tableStart($call);
11830cecf9d5Sandi                break;
11840cecf9d5Sandi                case 'table_row':
1185aa92c4ccSAdrian Lang                    $this->tableRowClose($call);
11860cecf9d5Sandi                    $this->tableRowOpen(array('tablerow_open',$call[1],$call[2]));
11870cecf9d5Sandi                break;
11880cecf9d5Sandi                case 'tableheader':
11890cecf9d5Sandi                case 'tablecell':
11900cecf9d5Sandi                    $this->tableCell($call);
11910cecf9d5Sandi                break;
11920cecf9d5Sandi                case 'table_end':
1193aa92c4ccSAdrian Lang                    $this->tableRowClose($call);
11940cecf9d5Sandi                    $this->tableEnd($call);
11950cecf9d5Sandi                break;
11960cecf9d5Sandi                default:
11970cecf9d5Sandi                    $this->tableDefault($call);
11980cecf9d5Sandi                break;
11990cecf9d5Sandi            }
12000cecf9d5Sandi        }
12010cecf9d5Sandi        $this->CallWriter->writeCalls($this->tableCalls);
12020cecf9d5Sandi    }
12030cecf9d5Sandi
12040cecf9d5Sandi    function tableStart($call) {
120590df9a4dSAdrian Lang        $this->tableCalls[] = array('table_open',$call[1],$call[2]);
12060cecf9d5Sandi        $this->tableCalls[] = array('tablerow_open',array(),$call[2]);
120744881bd0Shenning.noren        $this->firstCell = true;
12080cecf9d5Sandi    }
12090cecf9d5Sandi
12100cecf9d5Sandi    function tableEnd($call) {
121107c2b1c7SAdrian Lang        $this->tableCalls[] = array('table_close',$call[1],$call[2]);
12120cecf9d5Sandi        $this->finalizeTable();
12130cecf9d5Sandi    }
12140cecf9d5Sandi
12150cecf9d5Sandi    function tableRowOpen($call) {
12160cecf9d5Sandi        $this->tableCalls[] = $call;
12170cecf9d5Sandi        $this->currentCols = 0;
121844881bd0Shenning.noren        $this->firstCell = true;
12190cecf9d5Sandi        $this->lastCellType = 'tablecell';
12200cecf9d5Sandi        $this->maxRows++;
12210cecf9d5Sandi    }
12220cecf9d5Sandi
12230cecf9d5Sandi    function tableRowClose($call) {
1224*9060b8b0SChristopher Smith        if ($this->inTableHead) {
1225*9060b8b0SChristopher Smith            $this->countTableHeadRows++;
1226*9060b8b0SChristopher Smith        }
12270cecf9d5Sandi        // Strip off final cell opening and anything after it
12280cecf9d5Sandi        while ( $discard = array_pop($this->tableCalls ) ) {
12290cecf9d5Sandi
12300cecf9d5Sandi            if ( $discard[0] == 'tablecell_open' || $discard[0] == 'tableheader_open') {
12310cecf9d5Sandi                break;
12320cecf9d5Sandi            }
12330cecf9d5Sandi        }
1234aa92c4ccSAdrian Lang        $this->tableCalls[] = array('tablerow_close', array(), $call[2]);
12350cecf9d5Sandi
12360cecf9d5Sandi        if ( $this->currentCols > $this->maxCols ) {
12370cecf9d5Sandi            $this->maxCols = $this->currentCols;
12380cecf9d5Sandi        }
12390cecf9d5Sandi    }
12400cecf9d5Sandi
12410cecf9d5Sandi    function tableCell($call) {
1242*9060b8b0SChristopher Smith        if ($call[0] != 'tableheader') {
1243*9060b8b0SChristopher Smith            $this->inTableHead = false;
1244*9060b8b0SChristopher Smith        }
12450cecf9d5Sandi        if ( !$this->firstCell ) {
12460cecf9d5Sandi
12470cecf9d5Sandi            // Increase the span
12480cecf9d5Sandi            $lastCall = end($this->tableCalls);
12490cecf9d5Sandi
12500cecf9d5Sandi            // A cell call which follows an open cell means an empty cell so span
12510cecf9d5Sandi            if ( $lastCall[0] == 'tablecell_open' || $lastCall[0] == 'tableheader_open' ) {
12520cecf9d5Sandi                 $this->tableCalls[] = array('colspan',array(),$call[2]);
12530cecf9d5Sandi
12540cecf9d5Sandi            }
12550cecf9d5Sandi
12560cecf9d5Sandi            $this->tableCalls[] = array($this->lastCellType.'_close',array(),$call[2]);
12570ea51e63SMatt Perry            $this->tableCalls[] = array($call[0].'_open',array(1,null,1),$call[2]);
12580cecf9d5Sandi            $this->lastCellType = $call[0];
12590cecf9d5Sandi
12600cecf9d5Sandi        } else {
12610cecf9d5Sandi
12620ea51e63SMatt Perry            $this->tableCalls[] = array($call[0].'_open',array(1,null,1),$call[2]);
12630cecf9d5Sandi            $this->lastCellType = $call[0];
126444881bd0Shenning.noren            $this->firstCell = false;
12650cecf9d5Sandi
12660cecf9d5Sandi        }
12670cecf9d5Sandi
12680cecf9d5Sandi        $this->currentCols++;
12690cecf9d5Sandi    }
12700cecf9d5Sandi
12710cecf9d5Sandi    function tableDefault($call) {
12720cecf9d5Sandi        $this->tableCalls[] = $call;
12730cecf9d5Sandi    }
12740cecf9d5Sandi
12750cecf9d5Sandi    function finalizeTable() {
12760cecf9d5Sandi
12770cecf9d5Sandi        // Add the max cols and rows to the table opening
12780cecf9d5Sandi        if ( $this->tableCalls[0][0] == 'table_open' ) {
12790cecf9d5Sandi            // Adjust to num cols not num col delimeters
12800cecf9d5Sandi            $this->tableCalls[0][1][] = $this->maxCols - 1;
12810cecf9d5Sandi            $this->tableCalls[0][1][] = $this->maxRows;
128290df9a4dSAdrian Lang            $this->tableCalls[0][1][] = array_shift($this->tableCalls[0][1]);
12830cecf9d5Sandi        } else {
12840cecf9d5Sandi            trigger_error('First element in table call list is not table_open');
12850cecf9d5Sandi        }
12860cecf9d5Sandi
12870cecf9d5Sandi        $lastRow = 0;
12880cecf9d5Sandi        $lastCell = 0;
128925b97867Shakan.sandell        $cellKey = array();
12900cecf9d5Sandi        $toDelete = array();
12910cecf9d5Sandi
12920cecf9d5Sandi        // Look for the colspan elements and increment the colspan on the
12930cecf9d5Sandi        // previous non-empty opening cell. Once done, delete all the cells
12940cecf9d5Sandi        // that contain colspans
12956606d6fcSAdrian Lang        for ($key = 0 ; $key < count($this->tableCalls) ; ++$key) {
12966606d6fcSAdrian Lang            $call = $this->tableCalls[$key];
12970cecf9d5Sandi
1298aa92c4ccSAdrian Lang            switch ($call[0]) {
1299*9060b8b0SChristopher Smith                case 'table_open' :
1300*9060b8b0SChristopher Smith                    if($this->countTableHeadRows) {
1301*9060b8b0SChristopher Smith                        array_splice($this->tableCalls, $key+1, 0, array(
1302*9060b8b0SChristopher Smith                              array('tablethead_open', array(), $call[2]))
1303*9060b8b0SChristopher Smith                        );
1304*9060b8b0SChristopher Smith                    }
1305*9060b8b0SChristopher Smith                    break;
1306*9060b8b0SChristopher Smith
1307aa92c4ccSAdrian Lang                case 'tablerow_open':
13080cecf9d5Sandi
130925b97867Shakan.sandell                    $lastRow++;
131025b97867Shakan.sandell                    $lastCell = 0;
1311aa92c4ccSAdrian Lang                    break;
13120cecf9d5Sandi
1313aa92c4ccSAdrian Lang                case 'tablecell_open':
1314aa92c4ccSAdrian Lang                case 'tableheader_open':
13150cecf9d5Sandi
131625b97867Shakan.sandell                    $lastCell++;
131725b97867Shakan.sandell                    $cellKey[$lastRow][$lastCell] = $key;
1318aa92c4ccSAdrian Lang                    break;
13190cecf9d5Sandi
1320aa92c4ccSAdrian Lang                case 'table_align':
13210cecf9d5Sandi
1322e03b8b2eSAdrian Lang                    $prev = in_array($this->tableCalls[$key-1][0], array('tablecell_open', 'tableheader_open'));
1323e03b8b2eSAdrian Lang                    $next = in_array($this->tableCalls[$key+1][0], array('tablecell_close', 'tableheader_close'));
1324e03b8b2eSAdrian Lang                    // If the cell is empty, align left
1325e03b8b2eSAdrian Lang                    if ($prev && $next) {
1326e03b8b2eSAdrian Lang                        $this->tableCalls[$key-1][1][1] = 'left';
1327e03b8b2eSAdrian Lang
13280cecf9d5Sandi                    // If the previous element was a cell open, align right
1329e03b8b2eSAdrian Lang                    } elseif ($prev) {
13300cecf9d5Sandi                        $this->tableCalls[$key-1][1][1] = 'right';
13310cecf9d5Sandi
1332e03b8b2eSAdrian Lang                    // If the next element is the close of an element, align either center or left
1333e03b8b2eSAdrian Lang                    } elseif ( $next) {
133425b97867Shakan.sandell                        if ( $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] == 'right' ) {
133525b97867Shakan.sandell                            $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] = 'center';
13360cecf9d5Sandi                        } else {
133725b97867Shakan.sandell                            $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] = 'left';
13380cecf9d5Sandi                        }
13390cecf9d5Sandi
13400cecf9d5Sandi                    }
13410cecf9d5Sandi
13420cecf9d5Sandi                    // Now convert the whitespace back to cdata
13430cecf9d5Sandi                    $this->tableCalls[$key][0] = 'cdata';
1344aa92c4ccSAdrian Lang                    break;
13450cecf9d5Sandi
1346aa92c4ccSAdrian Lang                case 'colspan':
13470cecf9d5Sandi
134844881bd0Shenning.noren                    $this->tableCalls[$key-1][1][0] = false;
13490cecf9d5Sandi
135025b97867Shakan.sandell                    for($i = $key-2; $i >= $cellKey[$lastRow][1]; $i--) {
13510cecf9d5Sandi
13520cecf9d5Sandi                        if ( $this->tableCalls[$i][0] == 'tablecell_open' || $this->tableCalls[$i][0] == 'tableheader_open' ) {
13530cecf9d5Sandi
135444881bd0Shenning.noren                            if ( false !== $this->tableCalls[$i][1][0] ) {
13550cecf9d5Sandi                                $this->tableCalls[$i][1][0]++;
13560cecf9d5Sandi                                break;
13570cecf9d5Sandi                            }
13580cecf9d5Sandi
13590cecf9d5Sandi                        }
13600cecf9d5Sandi                    }
13610cecf9d5Sandi
13620cecf9d5Sandi                    $toDelete[] = $key-1;
13630cecf9d5Sandi                    $toDelete[] = $key;
13640cecf9d5Sandi                    $toDelete[] = $key+1;
1365aa92c4ccSAdrian Lang                    break;
136625b97867Shakan.sandell
1367aa92c4ccSAdrian Lang                case 'rowspan':
136825b97867Shakan.sandell
136925b97867Shakan.sandell                    if ( $this->tableCalls[$key-1][0] == 'cdata' ) {
137025b97867Shakan.sandell                        // ignore rowspan if previous call was cdata (text mixed with :::) we don't have to check next call as that wont match regex
137125b97867Shakan.sandell                        $this->tableCalls[$key][0] = 'cdata';
137225b97867Shakan.sandell
137325b97867Shakan.sandell                    } else {
137425b97867Shakan.sandell
13756606d6fcSAdrian Lang                        $spanning_cell = null;
1376*9060b8b0SChristopher Smith
1377*9060b8b0SChristopher Smith                        // can't cross thead/tbody boundary
1378*9060b8b0SChristopher Smith                        if (!$this->countTableHeadRows || ($lastRow-1 != $this->countTableHeadRows)) {
137925b97867Shakan.sandell                            for($i = $lastRow-1; $i > 0; $i--) {
138025b97867Shakan.sandell
138125b97867Shakan.sandell                                if ( $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tablecell_open' || $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tableheader_open' ) {
138225b97867Shakan.sandell
13836606d6fcSAdrian Lang                                    if ($this->tableCalls[$cellKey[$i][$lastCell]][1][2] >= $lastRow - $i) {
13846606d6fcSAdrian Lang                                        $spanning_cell = $i;
138525b97867Shakan.sandell                                        break;
138625b97867Shakan.sandell                                    }
138725b97867Shakan.sandell
138825b97867Shakan.sandell                                }
138925b97867Shakan.sandell                            }
1390*9060b8b0SChristopher Smith                        }
13916606d6fcSAdrian Lang                        if (is_null($spanning_cell)) {
13926606d6fcSAdrian Lang                            // No spanning cell found, so convert this cell to
13936606d6fcSAdrian Lang                            // an empty one to avoid broken tables
13946a7e97d5SGerrit Uitslag                            $this->tableCalls[$key][0] = 'cdata';
13956a7e97d5SGerrit Uitslag                            $this->tableCalls[$key][1][0] = '';
13966606d6fcSAdrian Lang                            continue;
13976606d6fcSAdrian Lang                        }
13986606d6fcSAdrian Lang                        $this->tableCalls[$cellKey[$spanning_cell][$lastCell]][1][2]++;
13996606d6fcSAdrian Lang
14006606d6fcSAdrian Lang                        $this->tableCalls[$key-1][1][2] = false;
140125b97867Shakan.sandell
140225b97867Shakan.sandell                        $toDelete[] = $key-1;
140325b97867Shakan.sandell                        $toDelete[] = $key;
140425b97867Shakan.sandell                        $toDelete[] = $key+1;
140525b97867Shakan.sandell                    }
1406aa92c4ccSAdrian Lang                    break;
1407aa92c4ccSAdrian Lang
14086606d6fcSAdrian Lang                case 'tablerow_close':
14096606d6fcSAdrian Lang
14106606d6fcSAdrian Lang                    // Fix broken tables by adding missing cells
14116606d6fcSAdrian Lang                    while (++$lastCell < $this->maxCols) {
14126606d6fcSAdrian Lang                        array_splice($this->tableCalls, $key, 0, array(
14136606d6fcSAdrian Lang                               array('tablecell_open', array(1, null, 1), $call[2]),
14146606d6fcSAdrian Lang                               array('cdata', array(''), $call[2]),
14156606d6fcSAdrian Lang                               array('tablecell_close', array(), $call[2])));
14166606d6fcSAdrian Lang                        $key += 3;
14176606d6fcSAdrian Lang                    }
14186606d6fcSAdrian Lang
1419*9060b8b0SChristopher Smith                    if($this->countTableHeadRows == $lastRow) {
1420f05a1cc5SGerrit Uitslag                        array_splice($this->tableCalls, $key+1, 0, array(
1421f05a1cc5SGerrit Uitslag                              array('tablethead_close', array(), $call[2])));
1422f05a1cc5SGerrit Uitslag                    }
14236606d6fcSAdrian Lang                    break;
14246606d6fcSAdrian Lang
14250cecf9d5Sandi            }
14260cecf9d5Sandi        }
14270cecf9d5Sandi
14289ab75d9eSAndreas Gohr        // condense cdata
14299ab75d9eSAndreas Gohr        $cnt = count($this->tableCalls);
14309ab75d9eSAndreas Gohr        for( $key = 0; $key < $cnt; $key++){
14319ab75d9eSAndreas Gohr            if($this->tableCalls[$key][0] == 'cdata'){
14329ab75d9eSAndreas Gohr                $ckey = $key;
14339ab75d9eSAndreas Gohr                $key++;
14349ab75d9eSAndreas Gohr                while($this->tableCalls[$key][0] == 'cdata'){
14359ab75d9eSAndreas Gohr                    $this->tableCalls[$ckey][1][0] .= $this->tableCalls[$key][1][0];
14369ab75d9eSAndreas Gohr                    $toDelete[] = $key;
14379ab75d9eSAndreas Gohr                    $key++;
14389ab75d9eSAndreas Gohr                }
14399ab75d9eSAndreas Gohr                continue;
14409ab75d9eSAndreas Gohr            }
14419ab75d9eSAndreas Gohr        }
14429ab75d9eSAndreas Gohr
14430cecf9d5Sandi        foreach ( $toDelete as $delete ) {
14440cecf9d5Sandi            unset($this->tableCalls[$delete]);
14450cecf9d5Sandi        }
14460cecf9d5Sandi        $this->tableCalls = array_values($this->tableCalls);
14470cecf9d5Sandi    }
14480cecf9d5Sandi}
14490cecf9d5Sandi
14500cecf9d5Sandi
14512a27e99aSandi/**
14522a27e99aSandi * Handler for paragraphs
14532a27e99aSandi *
14540b7c14c2Sandi * @author Harry Fuecks <hfuecks@gmail.com>
14552a27e99aSandi */
14560cecf9d5Sandiclass Doku_Handler_Block {
14570cecf9d5Sandi    var $calls = array();
14589569a107SDanny Lin    var $skipEol = false;
14590cecf9d5Sandi
1460af146da0Sandi    // Blocks these should not be inside paragraphs
14610cecf9d5Sandi    var $blockOpen = array(
14620cecf9d5Sandi            'header',
1463df9add72Schris            'listu_open','listo_open','listitem_open','listcontent_open',
1464f05a1cc5SGerrit Uitslag            'table_open','tablerow_open','tablecell_open','tableheader_open','tablethead_open',
14650cecf9d5Sandi            'quote_open',
146676aa94b7Schris            'code','file','hr','preformatted','rss',
146707f89c3cSAnika Henke            'htmlblock','phpblock',
14689569a107SDanny Lin            'footnote_open',
14690cecf9d5Sandi        );
14700cecf9d5Sandi
14710cecf9d5Sandi    var $blockClose = array(
14720cecf9d5Sandi            'header',
1473df9add72Schris            'listu_close','listo_close','listitem_close','listcontent_close',
1474f05a1cc5SGerrit Uitslag            'table_close','tablerow_close','tablecell_close','tableheader_close','tablethead_close',
14750cecf9d5Sandi            'quote_close',
147676aa94b7Schris            'code','file','hr','preformatted','rss',
147707f89c3cSAnika Henke            'htmlblock','phpblock',
14789569a107SDanny Lin            'footnote_close',
14790cecf9d5Sandi        );
14800cecf9d5Sandi
1481af146da0Sandi    // Stacks can contain paragraphs
14820cecf9d5Sandi    var $stackOpen = array(
14839569a107SDanny Lin        'section_open',
14840cecf9d5Sandi        );
14850cecf9d5Sandi
14860cecf9d5Sandi    var $stackClose = array(
14879569a107SDanny Lin        'section_close',
14880cecf9d5Sandi        );
14890cecf9d5Sandi
1490af146da0Sandi
1491af146da0Sandi    /**
1492af146da0Sandi     * Constructor. Adds loaded syntax plugins to the block and stack
1493af146da0Sandi     * arrays
1494af146da0Sandi     *
1495af146da0Sandi     * @author Andreas Gohr <andi@splitbrain.org>
1496af146da0Sandi     */
1497af146da0Sandi    function Doku_Handler_Block(){
1498af146da0Sandi        global $DOKU_PLUGINS;
1499af146da0Sandi        //check if syntax plugins were loaded
150003c4aec3Schris        if(empty($DOKU_PLUGINS['syntax'])) return;
1501af146da0Sandi        foreach($DOKU_PLUGINS['syntax'] as $n => $p){
1502af146da0Sandi            $ptype = $p->getPType();
1503af146da0Sandi            if($ptype == 'block'){
1504af146da0Sandi                $this->blockOpen[]  = 'plugin_'.$n;
1505af146da0Sandi                $this->blockClose[] = 'plugin_'.$n;
1506af146da0Sandi            }elseif($ptype == 'stack'){
1507af146da0Sandi                $this->stackOpen[]  = 'plugin_'.$n;
1508af146da0Sandi                $this->stackClose[] = 'plugin_'.$n;
1509af146da0Sandi            }
1510af146da0Sandi        }
1511af146da0Sandi    }
1512af146da0Sandi
1513b5a0b131SDanny Lin    function openParagraph($pos){
15149569a107SDanny Lin        if ($this->inParagraph) return;
1515b5a0b131SDanny Lin        $this->calls[] = array('p_open',array(), $pos);
1516b5a0b131SDanny Lin        $this->inParagraph = true;
15179569a107SDanny Lin        $this->skipEol = true;
1518b5a0b131SDanny Lin    }
1519b5a0b131SDanny Lin
15202a27e99aSandi    /**
15212a27e99aSandi     * Close a paragraph if needed
15222a27e99aSandi     *
15232a27e99aSandi     * This function makes sure there are no empty paragraphs on the stack
15242a27e99aSandi     *
15252a27e99aSandi     * @author Andreas Gohr <andi@splitbrain.org>
15262a27e99aSandi     */
1527506ae684Sandi    function closeParagraph($pos){
15289569a107SDanny Lin        if (!$this->inParagraph) return;
1529506ae684Sandi        // look back if there was any content - we don't want empty paragraphs
1530506ae684Sandi        $content = '';
1531faba9a35SAndreas Gohr        $ccount = count($this->calls);
1532faba9a35SAndreas Gohr        for($i=$ccount-1; $i>=0; $i--){
1533506ae684Sandi            if($this->calls[$i][0] == 'p_open'){
1534506ae684Sandi                break;
1535506ae684Sandi            }elseif($this->calls[$i][0] == 'cdata'){
1536506ae684Sandi                $content .= $this->calls[$i][1][0];
1537506ae684Sandi            }else{
1538506ae684Sandi                $content = 'found markup';
1539506ae684Sandi                break;
1540506ae684Sandi            }
1541506ae684Sandi        }
1542506ae684Sandi
1543506ae684Sandi        if(trim($content)==''){
1544506ae684Sandi            //remove the whole paragraph
1545a86cc527SAndreas Gohr            //array_splice($this->calls,$i); // <- this is much slower than the loop below
1546d8f7a7f3SAndreas Gohr            for($x=$ccount; $x>$i; $x--) array_pop($this->calls);
1547506ae684Sandi        }else{
15489569a107SDanny Lin            // remove ending linebreaks in the paragraph
15499569a107SDanny Lin            $i=count($this->calls)-1;
15509569a107SDanny Lin            if ($this->calls[$i][0] == 'cdata') $this->calls[$i][1][0] = rtrim($this->calls[$i][1][0],DOKU_PARSER_EOL);
1551506ae684Sandi            $this->calls[] = array('p_close',array(), $pos);
1552506ae684Sandi        }
1553e1c10e4dSchris
155444881bd0Shenning.noren        $this->inParagraph = false;
15559569a107SDanny Lin        $this->skipEol = true;
15560cecf9d5Sandi    }
155741624b31SChris Smith
155841624b31SChris Smith    function addCall($call) {
155941624b31SChris Smith        $key = count($this->calls);
156041624b31SChris Smith        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
156141624b31SChris Smith            $this->calls[$key-1][1][0] .= $call[1][0];
156241624b31SChris Smith        } else {
156341624b31SChris Smith            $this->calls[] = $call;
156441624b31SChris Smith        }
156541624b31SChris Smith    }
15669569a107SDanny Lin
15679569a107SDanny Lin    // simple version of addCall, without checking cdata
15689569a107SDanny Lin    function storeCall($call) {
15699569a107SDanny Lin        $this->calls[] = $call;
15709569a107SDanny Lin    }
15719569a107SDanny Lin
15729569a107SDanny Lin    /**
15739569a107SDanny Lin     * Processes the whole instruction stack to open and close paragraphs
15749569a107SDanny Lin     *
15759569a107SDanny Lin     * @author Harry Fuecks <hfuecks@gmail.com>
15769569a107SDanny Lin     * @author Andreas Gohr <andi@splitbrain.org>
15779569a107SDanny Lin     */
15789569a107SDanny Lin    function process($calls) {
15799569a107SDanny Lin        // open first paragraph
15809569a107SDanny Lin        $this->openParagraph(0);
15819569a107SDanny Lin        foreach ( $calls as $key => $call ) {
15829569a107SDanny Lin            $cname = $call[0];
15839569a107SDanny Lin            if ($cname == 'plugin') {
15849569a107SDanny Lin                $cname='plugin_'.$call[1][0];
15859569a107SDanny Lin                $plugin = true;
15869569a107SDanny Lin                $plugin_open = (($call[1][2] == DOKU_LEXER_ENTER) || ($call[1][2] == DOKU_LEXER_SPECIAL));
15879569a107SDanny Lin                $plugin_close = (($call[1][2] == DOKU_LEXER_EXIT) || ($call[1][2] == DOKU_LEXER_SPECIAL));
15889569a107SDanny Lin            } else {
15899569a107SDanny Lin                $plugin = false;
15909569a107SDanny Lin            }
15919569a107SDanny Lin            /* stack */
15929569a107SDanny Lin            if ( in_array($cname,$this->stackClose ) && (!$plugin || $plugin_close)) {
15939569a107SDanny Lin                $this->closeParagraph($call[2]);
15949569a107SDanny Lin                $this->storeCall($call);
15959569a107SDanny Lin                $this->openParagraph($call[2]);
15969569a107SDanny Lin                continue;
15979569a107SDanny Lin            }
15989569a107SDanny Lin            if ( in_array($cname,$this->stackOpen ) && (!$plugin || $plugin_open) ) {
15999569a107SDanny Lin                $this->closeParagraph($call[2]);
16009569a107SDanny Lin                $this->storeCall($call);
16019569a107SDanny Lin                $this->openParagraph($call[2]);
16029569a107SDanny Lin                continue;
16039569a107SDanny Lin            }
16049569a107SDanny Lin            /* block */
16059569a107SDanny Lin            // If it's a substition it opens and closes at the same call.
16069569a107SDanny Lin            // To make sure next paragraph is correctly started, let close go first.
16079569a107SDanny Lin            if ( in_array($cname, $this->blockClose) && (!$plugin || $plugin_close)) {
16089569a107SDanny Lin                $this->closeParagraph($call[2]);
16099569a107SDanny Lin                $this->storeCall($call);
16109569a107SDanny Lin                $this->openParagraph($call[2]);
16119569a107SDanny Lin                continue;
16129569a107SDanny Lin            }
16139569a107SDanny Lin            if ( in_array($cname, $this->blockOpen) && (!$plugin || $plugin_open)) {
16149569a107SDanny Lin                $this->closeParagraph($call[2]);
16159569a107SDanny Lin                $this->storeCall($call);
16169569a107SDanny Lin                continue;
16179569a107SDanny Lin            }
16189569a107SDanny Lin            /* eol */
16199569a107SDanny Lin            if ( $cname == 'eol' ) {
16209569a107SDanny Lin                // Check this isn't an eol instruction to skip...
16219569a107SDanny Lin                if ( !$this->skipEol ) {
16229569a107SDanny Lin                    // Next is EOL => double eol => mark as paragraph
16239569a107SDanny Lin                    if ( isset($calls[$key+1]) && $calls[$key+1][0] == 'eol' ) {
16249569a107SDanny Lin                        $this->closeParagraph($call[2]);
16259569a107SDanny Lin                        $this->openParagraph($call[2]);
16269569a107SDanny Lin                    } else {
16279569a107SDanny Lin                        //if this is just a single eol make a space from it
16289569a107SDanny Lin                        $this->addCall(array('cdata',array(DOKU_PARSER_EOL), $call[2]));
16299569a107SDanny Lin                    }
16309569a107SDanny Lin                }
16319569a107SDanny Lin                continue;
16329569a107SDanny Lin            }
16339569a107SDanny Lin            /* normal */
16349569a107SDanny Lin            $this->addCall($call);
16359569a107SDanny Lin            $this->skipEol = false;
16369569a107SDanny Lin        }
16379569a107SDanny Lin        // close last paragraph
16389569a107SDanny Lin        $call = end($this->calls);
16399569a107SDanny Lin        $this->closeParagraph($call[2]);
16409569a107SDanny Lin        return $this->calls;
16419569a107SDanny Lin    }
16420cecf9d5Sandi}
16432a27e99aSandi
1644e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
1645