xref: /dokuwiki/inc/parser/handler.php (revision 42ea7f447f39fbc2f79eaaec31f8c10ede59c5d0)
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);
7445d5ad75SGerrit Uitslag        $plugin = plugin_load('syntax',$pluginname);
75a46d0d65SAndreas Gohr        if($plugin != null){
76f02a7d06Schris            $data = $plugin->handle($match, $state, $pos, $this);
77ee20e7d1Sandi        }
7813ecfb18SChris Smith        if ($data !== false) {
7982d61635Spierre.spring            $this->addPluginCall($pluginname,$data,$state,$pos,$match);
8013ecfb18SChris Smith        }
8144881bd0Shenning.noren        return true;
82ee20e7d1Sandi    }
830cecf9d5Sandi
840cecf9d5Sandi    function base($match, $state, $pos) {
850cecf9d5Sandi        switch ( $state ) {
860cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
87433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
8844881bd0Shenning.noren                return true;
890cecf9d5Sandi            break;
900cecf9d5Sandi        }
910cecf9d5Sandi    }
920cecf9d5Sandi
930cecf9d5Sandi    function header($match, $state, $pos) {
94d7e8115fSAndreas Gohr        // get level and title
95a4a2d4cfSAndreas Gohr        $title = trim($match);
96a4a2d4cfSAndreas Gohr        $level = 7 - strspn($title,'=');
97d7e8115fSAndreas Gohr        if($level < 1) $level = 1;
98a4a2d4cfSAndreas Gohr        $title = trim($title,'=');
99a4a2d4cfSAndreas Gohr        $title = trim($title);
1000cecf9d5Sandi
101e1c10e4dSchris        if ($this->status['section']) $this->_addCall('section_close',array(),$pos);
102e1c10e4dSchris
103433bef32Sandi        $this->_addCall('header',array($title,$level,$pos), $pos);
104e1c10e4dSchris
105e1c10e4dSchris        $this->_addCall('section_open',array($level),$pos);
10644881bd0Shenning.noren        $this->status['section'] = true;
10744881bd0Shenning.noren        return true;
1080cecf9d5Sandi    }
1090cecf9d5Sandi
1100cecf9d5Sandi    function notoc($match, $state, $pos) {
111e41c4da9SAndreas Gohr        $this->_addCall('notoc',array(),$pos);
11244881bd0Shenning.noren        return true;
1130cecf9d5Sandi    }
1140cecf9d5Sandi
1159dc2c2afSandi    function nocache($match, $state, $pos) {
1169dc2c2afSandi        $this->_addCall('nocache',array(),$pos);
11744881bd0Shenning.noren        return true;
1189dc2c2afSandi    }
1199dc2c2afSandi
1200cecf9d5Sandi    function linebreak($match, $state, $pos) {
121433bef32Sandi        $this->_addCall('linebreak',array(),$pos);
12244881bd0Shenning.noren        return true;
1230cecf9d5Sandi    }
1240cecf9d5Sandi
1250cecf9d5Sandi    function eol($match, $state, $pos) {
126433bef32Sandi        $this->_addCall('eol',array(),$pos);
12744881bd0Shenning.noren        return true;
1280cecf9d5Sandi    }
1290cecf9d5Sandi
1300cecf9d5Sandi    function hr($match, $state, $pos) {
131433bef32Sandi        $this->_addCall('hr',array(),$pos);
13244881bd0Shenning.noren        return true;
1330cecf9d5Sandi    }
1340cecf9d5Sandi
135433bef32Sandi    function _nestingTag($match, $state, $pos, $name) {
1360cecf9d5Sandi        switch ( $state ) {
1370cecf9d5Sandi            case DOKU_LEXER_ENTER:
138433bef32Sandi                $this->_addCall($name.'_open', array(), $pos);
1390cecf9d5Sandi            break;
1400cecf9d5Sandi            case DOKU_LEXER_EXIT:
141433bef32Sandi                $this->_addCall($name.'_close', array(), $pos);
1420cecf9d5Sandi            break;
1430cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
144433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
1450cecf9d5Sandi            break;
1460cecf9d5Sandi        }
1470cecf9d5Sandi    }
1480cecf9d5Sandi
1490cecf9d5Sandi    function strong($match, $state, $pos) {
150433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'strong');
15144881bd0Shenning.noren        return true;
1520cecf9d5Sandi    }
1530cecf9d5Sandi
1540cecf9d5Sandi    function emphasis($match, $state, $pos) {
155433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'emphasis');
15644881bd0Shenning.noren        return true;
1570cecf9d5Sandi    }
1580cecf9d5Sandi
1590cecf9d5Sandi    function underline($match, $state, $pos) {
160433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'underline');
16144881bd0Shenning.noren        return true;
1620cecf9d5Sandi    }
1630cecf9d5Sandi
1640cecf9d5Sandi    function monospace($match, $state, $pos) {
165433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'monospace');
16644881bd0Shenning.noren        return true;
1670cecf9d5Sandi    }
1680cecf9d5Sandi
1690cecf9d5Sandi    function subscript($match, $state, $pos) {
170433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'subscript');
17144881bd0Shenning.noren        return true;
1720cecf9d5Sandi    }
1730cecf9d5Sandi
1740cecf9d5Sandi    function superscript($match, $state, $pos) {
175433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'superscript');
17644881bd0Shenning.noren        return true;
1770cecf9d5Sandi    }
1780cecf9d5Sandi
1790cecf9d5Sandi    function deleted($match, $state, $pos) {
180433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'deleted');
18144881bd0Shenning.noren        return true;
1820cecf9d5Sandi    }
1830cecf9d5Sandi
1840cecf9d5Sandi
1850cecf9d5Sandi    function footnote($match, $state, $pos) {
1865587e44cSchris//        $this->_nestingTag($match, $state, $pos, 'footnote');
187742c66f8Schris        if (!isset($this->_footnote)) $this->_footnote = false;
1882fe7363dSchris
1895587e44cSchris        switch ( $state ) {
1905587e44cSchris            case DOKU_LEXER_ENTER:
1912fe7363dSchris                // footnotes can not be nested - however due to limitations in lexer it can't be prevented
1922fe7363dSchris                // we will still enter a new footnote mode, we just do nothing
193742c66f8Schris                if ($this->_footnote) {
1942fe7363dSchris                    $this->_addCall('cdata',array($match), $pos);
1952fe7363dSchris                    break;
1962fe7363dSchris                }
1972fe7363dSchris
198742c66f8Schris                $this->_footnote = true;
1992fe7363dSchris
20067f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Nest($this->CallWriter,'footnote_close');
2015587e44cSchris                $this->CallWriter = & $ReWriter;
2024a26ad85Schris                $this->_addCall('footnote_open', array(), $pos);
2035587e44cSchris            break;
2045587e44cSchris            case DOKU_LEXER_EXIT:
2052fe7363dSchris                // check whether we have already exitted the footnote mode, can happen if the modes were nested
206742c66f8Schris                if (!$this->_footnote) {
2072fe7363dSchris                    $this->_addCall('cdata',array($match), $pos);
2082fe7363dSchris                    break;
2092fe7363dSchris                }
2102fe7363dSchris
211742c66f8Schris                $this->_footnote = false;
2122fe7363dSchris
2135587e44cSchris                $this->_addCall('footnote_close', array(), $pos);
2145587e44cSchris                $this->CallWriter->process();
2155587e44cSchris                $ReWriter = & $this->CallWriter;
2165587e44cSchris                $this->CallWriter = & $ReWriter->CallWriter;
2175587e44cSchris            break;
2185587e44cSchris            case DOKU_LEXER_UNMATCHED:
2195587e44cSchris                $this->_addCall('cdata', array($match), $pos);
2205587e44cSchris            break;
2215587e44cSchris        }
22244881bd0Shenning.noren        return true;
2230cecf9d5Sandi    }
2240cecf9d5Sandi
2250cecf9d5Sandi    function listblock($match, $state, $pos) {
2260cecf9d5Sandi        switch ( $state ) {
2270cecf9d5Sandi            case DOKU_LEXER_ENTER:
22867f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_List($this->CallWriter);
2290cecf9d5Sandi                $this->CallWriter = & $ReWriter;
230433bef32Sandi                $this->_addCall('list_open', array($match), $pos);
2310cecf9d5Sandi            break;
2320cecf9d5Sandi            case DOKU_LEXER_EXIT:
233433bef32Sandi                $this->_addCall('list_close', array(), $pos);
2340cecf9d5Sandi                $this->CallWriter->process();
2350cecf9d5Sandi                $ReWriter = & $this->CallWriter;
2360cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
2370cecf9d5Sandi            break;
2380cecf9d5Sandi            case DOKU_LEXER_MATCHED:
239433bef32Sandi                $this->_addCall('list_item', array($match), $pos);
2400cecf9d5Sandi            break;
2410cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
242433bef32Sandi                $this->_addCall('cdata', array($match), $pos);
2430cecf9d5Sandi            break;
2440cecf9d5Sandi        }
24544881bd0Shenning.noren        return true;
2460cecf9d5Sandi    }
2470cecf9d5Sandi
2480cecf9d5Sandi    function unformatted($match, $state, $pos) {
2490cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
250433bef32Sandi            $this->_addCall('unformatted',array($match), $pos);
2510cecf9d5Sandi        }
25244881bd0Shenning.noren        return true;
2530cecf9d5Sandi    }
2540cecf9d5Sandi
2550cecf9d5Sandi    function php($match, $state, $pos) {
256df9add72Schris        global $conf;
2570cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
258433bef32Sandi            $this->_addCall('php',array($match), $pos);
2590cecf9d5Sandi        }
26044881bd0Shenning.noren        return true;
2610cecf9d5Sandi    }
2620cecf9d5Sandi
26307f89c3cSAnika Henke    function phpblock($match, $state, $pos) {
26407f89c3cSAnika Henke        global $conf;
26507f89c3cSAnika Henke        if ( $state == DOKU_LEXER_UNMATCHED ) {
26607f89c3cSAnika Henke            $this->_addCall('phpblock',array($match), $pos);
26707f89c3cSAnika Henke        }
26807f89c3cSAnika Henke        return true;
26907f89c3cSAnika Henke    }
27007f89c3cSAnika Henke
2710cecf9d5Sandi    function html($match, $state, $pos) {
272df9add72Schris        global $conf;
2730cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
274433bef32Sandi            $this->_addCall('html',array($match), $pos);
2750cecf9d5Sandi        }
27644881bd0Shenning.noren        return true;
2770cecf9d5Sandi    }
2780cecf9d5Sandi
27907f89c3cSAnika Henke    function htmlblock($match, $state, $pos) {
28007f89c3cSAnika Henke        global $conf;
28107f89c3cSAnika Henke        if ( $state == DOKU_LEXER_UNMATCHED ) {
28207f89c3cSAnika Henke            $this->_addCall('htmlblock',array($match), $pos);
28307f89c3cSAnika Henke        }
28407f89c3cSAnika Henke        return true;
28507f89c3cSAnika Henke    }
28607f89c3cSAnika Henke
2870cecf9d5Sandi    function preformatted($match, $state, $pos) {
2880cecf9d5Sandi        switch ( $state ) {
2890cecf9d5Sandi            case DOKU_LEXER_ENTER:
29067f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Preformatted($this->CallWriter);
2910cecf9d5Sandi                $this->CallWriter = & $ReWriter;
292433bef32Sandi                $this->_addCall('preformatted_start',array(), $pos);
2930cecf9d5Sandi            break;
2940cecf9d5Sandi            case DOKU_LEXER_EXIT:
295433bef32Sandi                $this->_addCall('preformatted_end',array(), $pos);
2960cecf9d5Sandi                $this->CallWriter->process();
2970cecf9d5Sandi                $ReWriter = & $this->CallWriter;
2980cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
2990cecf9d5Sandi            break;
3000cecf9d5Sandi            case DOKU_LEXER_MATCHED:
301433bef32Sandi                $this->_addCall('preformatted_newline',array(), $pos);
3020cecf9d5Sandi            break;
3030cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
304433bef32Sandi                $this->_addCall('preformatted_content',array($match), $pos);
3050cecf9d5Sandi            break;
3060cecf9d5Sandi        }
3070cecf9d5Sandi
30844881bd0Shenning.noren        return true;
3090cecf9d5Sandi    }
3100cecf9d5Sandi
3110cecf9d5Sandi    function quote($match, $state, $pos) {
3120cecf9d5Sandi
3130cecf9d5Sandi        switch ( $state ) {
3140cecf9d5Sandi
3150cecf9d5Sandi            case DOKU_LEXER_ENTER:
31667f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Quote($this->CallWriter);
3170cecf9d5Sandi                $this->CallWriter = & $ReWriter;
318433bef32Sandi                $this->_addCall('quote_start',array($match), $pos);
3190cecf9d5Sandi            break;
3200cecf9d5Sandi
3210cecf9d5Sandi            case DOKU_LEXER_EXIT:
322433bef32Sandi                $this->_addCall('quote_end',array(), $pos);
3230cecf9d5Sandi                $this->CallWriter->process();
3240cecf9d5Sandi                $ReWriter = & $this->CallWriter;
3250cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
3260cecf9d5Sandi            break;
3270cecf9d5Sandi
3280cecf9d5Sandi            case DOKU_LEXER_MATCHED:
329433bef32Sandi                $this->_addCall('quote_newline',array($match), $pos);
3300cecf9d5Sandi            break;
3310cecf9d5Sandi
3320cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
333433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
3340cecf9d5Sandi            break;
3350cecf9d5Sandi
3360cecf9d5Sandi        }
3370cecf9d5Sandi
33844881bd0Shenning.noren        return true;
3390cecf9d5Sandi    }
3400cecf9d5Sandi
3413d491f75SAndreas Gohr    function file($match, $state, $pos) {
3423d491f75SAndreas Gohr        return $this->code($match, $state, $pos, 'file');
3433d491f75SAndreas Gohr    }
3443d491f75SAndreas Gohr
3453d491f75SAndreas Gohr    function code($match, $state, $pos, $type='code') {
3463d491f75SAndreas Gohr        if ( $state == DOKU_LEXER_UNMATCHED ) {
3474b7f9e70STom N Harris            $matches = explode('>',$match,2);
3483d491f75SAndreas Gohr
3490139312bSAdrian Lang            $param = preg_split('/\s+/', $matches[0], 2, PREG_SPLIT_NO_EMPTY);
3500139312bSAdrian Lang            while(count($param) < 2) array_push($param, null);
3510139312bSAdrian Lang
3520139312bSAdrian Lang            // We shortcut html here.
3530139312bSAdrian Lang            if ($param[0] == 'html') $param[0] = 'html4strict';
3540139312bSAdrian Lang            if ($param[0] == '-') $param[0] = null;
3550139312bSAdrian Lang            array_unshift($param, $matches[1]);
3560139312bSAdrian Lang
3570139312bSAdrian Lang            $this->_addCall($type, $param, $pos);
3580cecf9d5Sandi        }
35944881bd0Shenning.noren        return true;
3600cecf9d5Sandi    }
3610cecf9d5Sandi
3620cecf9d5Sandi    function acronym($match, $state, $pos) {
363433bef32Sandi        $this->_addCall('acronym',array($match), $pos);
36444881bd0Shenning.noren        return true;
3650cecf9d5Sandi    }
3660cecf9d5Sandi
3670cecf9d5Sandi    function smiley($match, $state, $pos) {
368433bef32Sandi        $this->_addCall('smiley',array($match), $pos);
36944881bd0Shenning.noren        return true;
3700cecf9d5Sandi    }
3710cecf9d5Sandi
3720cecf9d5Sandi    function wordblock($match, $state, $pos) {
373433bef32Sandi        $this->_addCall('wordblock',array($match), $pos);
37444881bd0Shenning.noren        return true;
3750cecf9d5Sandi    }
3760cecf9d5Sandi
3770cecf9d5Sandi    function entity($match, $state, $pos) {
378433bef32Sandi        $this->_addCall('entity',array($match), $pos);
37944881bd0Shenning.noren        return true;
3800cecf9d5Sandi    }
3810cecf9d5Sandi
3820cecf9d5Sandi    function multiplyentity($match, $state, $pos) {
3830cecf9d5Sandi        preg_match_all('/\d+/',$match,$matches);
384433bef32Sandi        $this->_addCall('multiplyentity',array($matches[0][0],$matches[0][1]), $pos);
38544881bd0Shenning.noren        return true;
3860cecf9d5Sandi    }
3870cecf9d5Sandi
3880cecf9d5Sandi    function singlequoteopening($match, $state, $pos) {
389433bef32Sandi        $this->_addCall('singlequoteopening',array(), $pos);
39044881bd0Shenning.noren        return true;
3910cecf9d5Sandi    }
3920cecf9d5Sandi
3930cecf9d5Sandi    function singlequoteclosing($match, $state, $pos) {
394433bef32Sandi        $this->_addCall('singlequoteclosing',array(), $pos);
39544881bd0Shenning.noren        return true;
3960cecf9d5Sandi    }
3970cecf9d5Sandi
39857d757d1SAndreas Gohr    function apostrophe($match, $state, $pos) {
39957d757d1SAndreas Gohr        $this->_addCall('apostrophe',array(), $pos);
40057d757d1SAndreas Gohr        return true;
40157d757d1SAndreas Gohr    }
40257d757d1SAndreas Gohr
4030cecf9d5Sandi    function doublequoteopening($match, $state, $pos) {
404433bef32Sandi        $this->_addCall('doublequoteopening',array(), $pos);
405e950d12fSChristopher Smith        $this->status['doublequote']++;
40644881bd0Shenning.noren        return true;
4070cecf9d5Sandi    }
4080cecf9d5Sandi
4090cecf9d5Sandi    function doublequoteclosing($match, $state, $pos) {
410e950d12fSChristopher Smith        if ($this->status['doublequote'] <= 0) {
411e950d12fSChristopher Smith            $this->doublequoteopening($match, $state, $pos);
412e950d12fSChristopher Smith        } else {
413433bef32Sandi            $this->_addCall('doublequoteclosing',array(), $pos);
414e950d12fSChristopher Smith            $this->status['doublequote'] = max(0, --$this->status['doublequote']);
415e950d12fSChristopher Smith        }
41644881bd0Shenning.noren        return true;
4170cecf9d5Sandi    }
4180cecf9d5Sandi
4190cecf9d5Sandi    function camelcaselink($match, $state, $pos) {
420433bef32Sandi        $this->_addCall('camelcaselink',array($match), $pos);
42144881bd0Shenning.noren        return true;
4220cecf9d5Sandi    }
4230cecf9d5Sandi
4240cecf9d5Sandi    /*
4250cecf9d5Sandi    */
4260cecf9d5Sandi    function internallink($match, $state, $pos) {
4270cecf9d5Sandi        // Strip the opening and closing markup
4280cecf9d5Sandi        $link = preg_replace(array('/^\[\[/','/\]\]$/u'),'',$match);
4290cecf9d5Sandi
4300cecf9d5Sandi        // Split title from URL
4314b7f9e70STom N Harris        $link = explode('|',$link,2);
4320cecf9d5Sandi        if ( !isset($link[1]) ) {
4330ea51e63SMatt Perry            $link[1] = null;
4340cecf9d5Sandi        } else if ( preg_match('/^\{\{[^\}]+\}\}$/',$link[1]) ) {
4355578eb8fSandi            // If the title is an image, convert it to an array containing the image details
436b625487dSandi            $link[1] = Doku_Handler_Parse_Media($link[1]);
4370cecf9d5Sandi        }
4380b7c14c2Sandi        $link[0] = trim($link[0]);
4390cecf9d5Sandi
4400e1c636eSandi        //decide which kind of link it is
4410e1c636eSandi
442e08dda3fSAndreas Gohr        if ( preg_match('/^[a-zA-Z0-9\.]+>{1}.*$/u',$link[0]) ) {
4430e1c636eSandi            // Interwiki
4444b7f9e70STom N Harris            $interwiki = explode('>',$link[0],2);
445433bef32Sandi            $this->_addCall(
4460cecf9d5Sandi                'interwikilink',
4470cecf9d5Sandi                array($link[0],$link[1],strtolower($interwiki[0]),$interwiki[1]),
4480cecf9d5Sandi                $pos
4490cecf9d5Sandi                );
45015f1b77cSAndreas Gohr        }elseif ( preg_match('/^\\\\\\\\[^\\\\]+?\\\\/u',$link[0]) ) {
4510e1c636eSandi            // Windows Share
452433bef32Sandi            $this->_addCall(
4530cecf9d5Sandi                'windowssharelink',
4540cecf9d5Sandi                array($link[0],$link[1]),
4550cecf9d5Sandi                $pos
4560cecf9d5Sandi                );
4574468cb4cSAndreas Gohr        }elseif ( preg_match('#^([a-z0-9\-\.+]+?)://#i',$link[0]) ) {
4580e1c636eSandi            // external link (accepts all protocols)
459433bef32Sandi            $this->_addCall(
4600cecf9d5Sandi                    'externallink',
4610cecf9d5Sandi                    array($link[0],$link[1]),
4620cecf9d5Sandi                    $pos
4630cecf9d5Sandi                    );
4640a1d30bfSchris        }elseif ( preg_match('<'.PREG_PATTERN_VALID_EMAIL.'>',$link[0]) ) {
4650a1d30bfSchris            // E-Mail (pattern above is defined in inc/mail.php)
466a6755281Sandi            $this->_addCall(
467a6755281Sandi                'emaillink',
468a6755281Sandi                array($link[0],$link[1]),
469a6755281Sandi                $pos
470a6755281Sandi                );
4710b7c14c2Sandi        }elseif ( preg_match('!^#.+!',$link[0]) ){
4720b7c14c2Sandi            // local link
4730b7c14c2Sandi            $this->_addCall(
4740b7c14c2Sandi                'locallink',
4750b7c14c2Sandi                array(substr($link[0],1),$link[1]),
4760b7c14c2Sandi                $pos
4770b7c14c2Sandi                );
4780e1c636eSandi        }else{
4790e1c636eSandi            // internal link
480433bef32Sandi            $this->_addCall(
4810e1c636eSandi                'internallink',
4820e1c636eSandi                array($link[0],$link[1]),
4830e1c636eSandi                $pos
4840e1c636eSandi                );
4850cecf9d5Sandi        }
4860e1c636eSandi
48744881bd0Shenning.noren        return true;
4880cecf9d5Sandi    }
4890cecf9d5Sandi
4900cecf9d5Sandi    function filelink($match, $state, $pos) {
4910ea51e63SMatt Perry        $this->_addCall('filelink',array($match, null), $pos);
49244881bd0Shenning.noren        return true;
4930cecf9d5Sandi    }
4940cecf9d5Sandi
4950cecf9d5Sandi    function windowssharelink($match, $state, $pos) {
4960ea51e63SMatt Perry        $this->_addCall('windowssharelink',array($match, null), $pos);
49744881bd0Shenning.noren        return true;
4980cecf9d5Sandi    }
4990cecf9d5Sandi
5000cecf9d5Sandi    function media($match, $state, $pos) {
5010cecf9d5Sandi        $p = Doku_Handler_Parse_Media($match);
5020cecf9d5Sandi
503433bef32Sandi        $this->_addCall(
5040cecf9d5Sandi              $p['type'],
505dc673a5bSjoe.lapp              array($p['src'], $p['title'], $p['align'], $p['width'],
506dc673a5bSjoe.lapp                     $p['height'], $p['cache'], $p['linking']),
5070cecf9d5Sandi              $pos
5080cecf9d5Sandi             );
50944881bd0Shenning.noren        return true;
5100cecf9d5Sandi    }
5110cecf9d5Sandi
512b625487dSandi    function rss($match, $state, $pos) {
513b625487dSandi        $link = preg_replace(array('/^\{\{rss>/','/\}\}$/'),'',$match);
5143db95becSAndreas Gohr
5153db95becSAndreas Gohr        // get params
5163db95becSAndreas Gohr        list($link,$params) = explode(' ',$link,2);
5173db95becSAndreas Gohr
5183db95becSAndreas Gohr        $p = array();
5193db95becSAndreas Gohr        if(preg_match('/\b(\d+)\b/',$params,$match)){
5203db95becSAndreas Gohr            $p['max'] = $match[1];
5213db95becSAndreas Gohr        }else{
5223db95becSAndreas Gohr            $p['max'] = 8;
5233db95becSAndreas Gohr        }
5243db95becSAndreas Gohr        $p['reverse'] = (preg_match('/rev/',$params));
5253db95becSAndreas Gohr        $p['author']  = (preg_match('/\b(by|author)/',$params));
5263db95becSAndreas Gohr        $p['date']    = (preg_match('/\b(date)/',$params));
5273db95becSAndreas Gohr        $p['details'] = (preg_match('/\b(desc|detail)/',$params));
5283db95becSAndreas Gohr
5290a69dff7Schris        if (preg_match('/\b(\d+)([dhm])\b/',$params,$match)) {
5300a69dff7Schris            $period = array('d' => 86400, 'h' => 3600, 'm' => 60);
5310a69dff7Schris            $p['refresh'] = max(600,$match[1]*$period[$match[2]]);  // n * period in seconds, minimum 10 minutes
5320a69dff7Schris        } else {
5330a69dff7Schris            $p['refresh'] = 14400;   // default to 4 hours
5340a69dff7Schris        }
5350a69dff7Schris
5363db95becSAndreas Gohr        $this->_addCall('rss',array($link,$p),$pos);
53744881bd0Shenning.noren        return true;
538b625487dSandi    }
539b625487dSandi
5400cecf9d5Sandi    function externallink($match, $state, $pos) {
541da9f31c5SAndreas Gohr        $url   = $match;
542da9f31c5SAndreas Gohr        $title = null;
5430cecf9d5Sandi
544da9f31c5SAndreas Gohr        // add protocol on simple short URLs
545da9f31c5SAndreas Gohr        if(substr($url,0,3) == 'ftp' && (substr($url,0,6) != 'ftp://')){
546da9f31c5SAndreas Gohr            $title = $url;
547da9f31c5SAndreas Gohr            $url   = 'ftp://'.$url;
548da9f31c5SAndreas Gohr        }
549da9f31c5SAndreas Gohr        if(substr($url,0,3) == 'www' && (substr($url,0,7) != 'http://')){
550da9f31c5SAndreas Gohr            $title = $url;
551da9f31c5SAndreas Gohr            $url = 'http://'.$url;
552da9f31c5SAndreas Gohr        }
553da9f31c5SAndreas Gohr
554da9f31c5SAndreas Gohr        $this->_addCall('externallink',array($url, $title), $pos);
55544881bd0Shenning.noren        return true;
5560cecf9d5Sandi    }
5570cecf9d5Sandi
55871352defSandi    function emaillink($match, $state, $pos) {
5590cecf9d5Sandi        $email = preg_replace(array('/^</','/>$/'),'',$match);
5600ea51e63SMatt Perry        $this->_addCall('emaillink',array($email, null), $pos);
56144881bd0Shenning.noren        return true;
5620cecf9d5Sandi    }
5630cecf9d5Sandi
5640cecf9d5Sandi    function table($match, $state, $pos) {
5650cecf9d5Sandi        switch ( $state ) {
5660cecf9d5Sandi
5670cecf9d5Sandi            case DOKU_LEXER_ENTER:
5680cecf9d5Sandi
56967f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Table($this->CallWriter);
5700cecf9d5Sandi                $this->CallWriter = & $ReWriter;
5710cecf9d5Sandi
57290df9a4dSAdrian Lang                $this->_addCall('table_start', array($pos + 1), $pos);
5730cecf9d5Sandi                if ( trim($match) == '^' ) {
574433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
5750cecf9d5Sandi                } else {
576433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
5770cecf9d5Sandi                }
5780cecf9d5Sandi            break;
5790cecf9d5Sandi
5800cecf9d5Sandi            case DOKU_LEXER_EXIT:
58190df9a4dSAdrian Lang                $this->_addCall('table_end', array($pos), $pos);
5820cecf9d5Sandi                $this->CallWriter->process();
5830cecf9d5Sandi                $ReWriter = & $this->CallWriter;
5840cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
5850cecf9d5Sandi            break;
5860cecf9d5Sandi
5870cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
5880cecf9d5Sandi                if ( trim($match) != '' ) {
589433bef32Sandi                    $this->_addCall('cdata',array($match), $pos);
5900cecf9d5Sandi                }
5910cecf9d5Sandi            break;
5920cecf9d5Sandi
5930cecf9d5Sandi            case DOKU_LEXER_MATCHED:
5949ab75d9eSAndreas Gohr                if ( $match == ' ' ){
5959ab75d9eSAndreas Gohr                    $this->_addCall('cdata', array($match), $pos);
59625b97867Shakan.sandell                } else if ( preg_match('/:::/',$match) ) {
59725b97867Shakan.sandell                    $this->_addCall('rowspan', array($match), $pos);
598e205b721SAndreas Gohr                } else if ( preg_match('/\t+/',$match) ) {
5999ab75d9eSAndreas Gohr                    $this->_addCall('table_align', array($match), $pos);
600e205b721SAndreas Gohr                } else if ( preg_match('/ {2,}/',$match) ) {
601433bef32Sandi                    $this->_addCall('table_align', array($match), $pos);
6020cecf9d5Sandi                } else if ( $match == "\n|" ) {
603433bef32Sandi                    $this->_addCall('table_row', array(), $pos);
604433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
6050cecf9d5Sandi                } else if ( $match == "\n^" ) {
606433bef32Sandi                    $this->_addCall('table_row', array(), $pos);
607433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
6080cecf9d5Sandi                } else if ( $match == '|' ) {
609433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
6100cecf9d5Sandi                } else if ( $match == '^' ) {
611433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
6120cecf9d5Sandi                }
6130cecf9d5Sandi            break;
6140cecf9d5Sandi        }
61544881bd0Shenning.noren        return true;
6160cecf9d5Sandi    }
6170cecf9d5Sandi}
6180cecf9d5Sandi
6190cecf9d5Sandi//------------------------------------------------------------------------
6200cecf9d5Sandifunction Doku_Handler_Parse_Media($match) {
6210cecf9d5Sandi
6220cecf9d5Sandi    // Strip the opening and closing markup
6230cecf9d5Sandi    $link = preg_replace(array('/^\{\{/','/\}\}$/u'),'',$match);
6240cecf9d5Sandi
6250cecf9d5Sandi    // Split title from URL
6264b7f9e70STom N Harris    $link = explode('|',$link,2);
6270cecf9d5Sandi
6280cecf9d5Sandi    // Check alignment
6290cecf9d5Sandi    $ralign = (bool)preg_match('/^ /',$link[0]);
6300cecf9d5Sandi    $lalign = (bool)preg_match('/ $/',$link[0]);
6310cecf9d5Sandi
6320cecf9d5Sandi    // Logic = what's that ;)...
6330cecf9d5Sandi    if ( $lalign & $ralign ) {
6340cecf9d5Sandi        $align = 'center';
6350cecf9d5Sandi    } else if ( $ralign ) {
6360cecf9d5Sandi        $align = 'right';
6370cecf9d5Sandi    } else if ( $lalign ) {
6380cecf9d5Sandi        $align = 'left';
6390cecf9d5Sandi    } else {
6400ea51e63SMatt Perry        $align = null;
6410cecf9d5Sandi    }
6420cecf9d5Sandi
6430cecf9d5Sandi    // The title...
6440cecf9d5Sandi    if ( !isset($link[1]) ) {
6450ea51e63SMatt Perry        $link[1] = null;
6460cecf9d5Sandi    }
6470cecf9d5Sandi
6484826ab45Sandi    //remove aligning spaces
6494826ab45Sandi    $link[0] = trim($link[0]);
6500cecf9d5Sandi
6514826ab45Sandi    //split into src and parameters (using the very last questionmark)
6524826ab45Sandi    $pos = strrpos($link[0], '?');
6534826ab45Sandi    if($pos !== false){
6544826ab45Sandi        $src   = substr($link[0],0,$pos);
6554826ab45Sandi        $param = substr($link[0],$pos+1);
6560cecf9d5Sandi    }else{
6574826ab45Sandi        $src   = $link[0];
6584826ab45Sandi        $param = '';
6590cecf9d5Sandi    }
6600cecf9d5Sandi
6614826ab45Sandi    //parse width and height
6624826ab45Sandi    if(preg_match('#(\d+)(x(\d+))?#i',$param,$size)){
663443e135dSChristopher Smith        !empty($size[1]) ? $w = $size[1] : $w = null;
664443e135dSChristopher Smith        !empty($size[3]) ? $h = $size[3] : $h = null;
665fc1c55b1Shfuecks    } else {
6660ea51e63SMatt Perry        $w = null;
6670ea51e63SMatt Perry        $h = null;
6680cecf9d5Sandi    }
6690cecf9d5Sandi
670dc673a5bSjoe.lapp    //get linking command
671d35ab615Shenning.noren    if(preg_match('/nolink/i',$param)){
672dc673a5bSjoe.lapp        $linking = 'nolink';
673d35ab615Shenning.noren    }else if(preg_match('/direct/i',$param)){
674dc673a5bSjoe.lapp        $linking = 'direct';
6758acb3108SAndreas Gohr    }else if(preg_match('/linkonly/i',$param)){
6768acb3108SAndreas Gohr        $linking = 'linkonly';
677dc673a5bSjoe.lapp    }else{
678dc673a5bSjoe.lapp        $linking = 'details';
679dc673a5bSjoe.lapp    }
680dc673a5bSjoe.lapp
6814826ab45Sandi    //get caching command
6824826ab45Sandi    if (preg_match('/(nocache|recache)/i',$param,$cachemode)){
6834826ab45Sandi        $cache = $cachemode[1];
6840cecf9d5Sandi    }else{
6854826ab45Sandi        $cache = 'cache';
6860cecf9d5Sandi    }
6870cecf9d5Sandi
6880cecf9d5Sandi    // Check whether this is a local or remote image
6893e7e6067SKlap-in    if ( media_isexternal($src) ) {
6904826ab45Sandi        $call = 'externalmedia';
6910cecf9d5Sandi    } else {
6924826ab45Sandi        $call = 'internalmedia';
6930cecf9d5Sandi    }
6940cecf9d5Sandi
6950cecf9d5Sandi    $params = array(
6960cecf9d5Sandi        'type'=>$call,
6974826ab45Sandi        'src'=>$src,
6980cecf9d5Sandi        'title'=>$link[1],
6990cecf9d5Sandi        'align'=>$align,
7004826ab45Sandi        'width'=>$w,
7014826ab45Sandi        'height'=>$h,
7020cecf9d5Sandi        'cache'=>$cache,
703dc673a5bSjoe.lapp        'linking'=>$linking,
7040cecf9d5Sandi    );
7050cecf9d5Sandi
7060cecf9d5Sandi    return $params;
7070cecf9d5Sandi}
7080cecf9d5Sandi
7090cecf9d5Sandi//------------------------------------------------------------------------
7100cecf9d5Sandiclass Doku_Handler_CallWriter {
7110cecf9d5Sandi
7120cecf9d5Sandi    var $Handler;
7130cecf9d5Sandi
714*42ea7f44SGerrit Uitslag    /**
715*42ea7f44SGerrit Uitslag     * @param Doku_Handler $Handler
716*42ea7f44SGerrit Uitslag     */
7170cecf9d5Sandi    function Doku_Handler_CallWriter(& $Handler) {
7180cecf9d5Sandi        $this->Handler = & $Handler;
7190cecf9d5Sandi    }
7200cecf9d5Sandi
7210cecf9d5Sandi    function writeCall($call) {
7220cecf9d5Sandi        $this->Handler->calls[] = $call;
7230cecf9d5Sandi    }
7240cecf9d5Sandi
7250cecf9d5Sandi    function writeCalls($calls) {
7260cecf9d5Sandi        $this->Handler->calls = array_merge($this->Handler->calls, $calls);
7270cecf9d5Sandi    }
728f4f02a0fSchris
729f4f02a0fSchris    // function is required, but since this call writer is first/highest in
730f4f02a0fSchris    // the chain it is not required to do anything
731f4f02a0fSchris    function finalise() {
7323893df8eSChristopher Smith        unset($this->Handler);
733f4f02a0fSchris    }
7340cecf9d5Sandi}
7350cecf9d5Sandi
7360cecf9d5Sandi//------------------------------------------------------------------------
7375587e44cSchris/**
7385587e44cSchris * Generic call writer class to handle nesting of rendering instructions
7395587e44cSchris * within a render instruction. Also see nest() method of renderer base class
7405587e44cSchris *
7415587e44cSchris * @author    Chris Smith <chris@jalakai.co.uk>
7425587e44cSchris */
7435587e44cSchrisclass Doku_Handler_Nest {
7445587e44cSchris
7455587e44cSchris    var $CallWriter;
7465587e44cSchris    var $calls = array();
7475587e44cSchris
7485587e44cSchris    var $closingInstruction;
7495587e44cSchris
7505587e44cSchris    /**
7515587e44cSchris     * constructor
7525587e44cSchris     *
753*42ea7f44SGerrit Uitslag     * @param  Doku_Handler_CallWriter $CallWriter     the renderers current call writer
7545587e44cSchris     * @param  string     $close          closing instruction name, this is required to properly terminate the
7555587e44cSchris     *                                    syntax mode if the document ends without a closing pattern
7565587e44cSchris     */
7575587e44cSchris    function Doku_Handler_Nest(& $CallWriter, $close="nest_close") {
7585587e44cSchris        $this->CallWriter = & $CallWriter;
7595587e44cSchris
7605587e44cSchris        $this->closingInstruction = $close;
7615587e44cSchris    }
7625587e44cSchris
7635587e44cSchris    function writeCall($call) {
7645587e44cSchris        $this->calls[] = $call;
7655587e44cSchris    }
7665587e44cSchris
7675587e44cSchris    function writeCalls($calls) {
7685587e44cSchris        $this->calls = array_merge($this->calls, $calls);
7695587e44cSchris    }
7705587e44cSchris
7715587e44cSchris    function finalise() {
7725587e44cSchris        $last_call = end($this->calls);
7735587e44cSchris        $this->writeCall(array($this->closingInstruction,array(), $last_call[2]));
7745587e44cSchris
7755587e44cSchris        $this->process();
7765587e44cSchris        $this->CallWriter->finalise();
7773893df8eSChristopher Smith        unset($this->CallWriter);
7785587e44cSchris    }
7795587e44cSchris
7805587e44cSchris    function process() {
78141624b31SChris Smith        // merge consecutive cdata
78241624b31SChris Smith        $unmerged_calls = $this->calls;
78341624b31SChris Smith        $this->calls = array();
78441624b31SChris Smith
78541624b31SChris Smith        foreach ($unmerged_calls as $call) $this->addCall($call);
78641624b31SChris Smith
7875587e44cSchris        $first_call = reset($this->calls);
7885587e44cSchris        $this->CallWriter->writeCall(array("nest", array($this->calls), $first_call[2]));
7895587e44cSchris    }
79041624b31SChris Smith
79141624b31SChris Smith    function addCall($call) {
79241624b31SChris Smith        $key = count($this->calls);
79341624b31SChris Smith        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
79441624b31SChris Smith            $this->calls[$key-1][1][0] .= $call[1][0];
79573c47f3dSChris Smith        } else if ($call[0] == 'eol') {
79673c47f3dSChris Smith            // do nothing (eol shouldn't be allowed, to counter preformatted fix in #1652 & #1699)
79741624b31SChris Smith        } else {
79841624b31SChris Smith            $this->calls[] = $call;
79941624b31SChris Smith        }
80041624b31SChris Smith    }
8015587e44cSchris}
8025587e44cSchris
8030cecf9d5Sandiclass Doku_Handler_List {
8040cecf9d5Sandi
8050cecf9d5Sandi    var $CallWriter;
8060cecf9d5Sandi
8070cecf9d5Sandi    var $calls = array();
8080cecf9d5Sandi    var $listCalls = array();
8090cecf9d5Sandi    var $listStack = array();
8100cecf9d5Sandi
8110cecf9d5Sandi    function Doku_Handler_List(& $CallWriter) {
8120cecf9d5Sandi        $this->CallWriter = & $CallWriter;
8130cecf9d5Sandi    }
8140cecf9d5Sandi
8150cecf9d5Sandi    function writeCall($call) {
8160cecf9d5Sandi        $this->calls[] = $call;
8170cecf9d5Sandi    }
8180cecf9d5Sandi
8190cecf9d5Sandi    // Probably not needed but just in case...
8200cecf9d5Sandi    function writeCalls($calls) {
8210cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
822f4f02a0fSchris#        $this->CallWriter->writeCalls($this->calls);
823f4f02a0fSchris    }
824f4f02a0fSchris
825f4f02a0fSchris    function finalise() {
826f4f02a0fSchris        $last_call = end($this->calls);
827f4f02a0fSchris        $this->writeCall(array('list_close',array(), $last_call[2]));
828f4f02a0fSchris
829f4f02a0fSchris        $this->process();
830f4f02a0fSchris        $this->CallWriter->finalise();
8313893df8eSChristopher Smith        unset($this->CallWriter);
8320cecf9d5Sandi    }
8330cecf9d5Sandi
8340cecf9d5Sandi    //------------------------------------------------------------------------
8350cecf9d5Sandi    function process() {
836f4f02a0fSchris
8370cecf9d5Sandi        foreach ( $this->calls as $call ) {
8380cecf9d5Sandi            switch ($call[0]) {
8390cecf9d5Sandi                case 'list_item':
8400cecf9d5Sandi                    $this->listOpen($call);
8410cecf9d5Sandi                break;
8420cecf9d5Sandi                case 'list_open':
8430cecf9d5Sandi                    $this->listStart($call);
8440cecf9d5Sandi                break;
8450cecf9d5Sandi                case 'list_close':
8460cecf9d5Sandi                    $this->listEnd($call);
8470cecf9d5Sandi                break;
8480cecf9d5Sandi                default:
8490cecf9d5Sandi                    $this->listContent($call);
8500cecf9d5Sandi                break;
8510cecf9d5Sandi            }
8520cecf9d5Sandi        }
8530cecf9d5Sandi
8540cecf9d5Sandi        $this->CallWriter->writeCalls($this->listCalls);
8550cecf9d5Sandi    }
8560cecf9d5Sandi
8570cecf9d5Sandi    //------------------------------------------------------------------------
8580cecf9d5Sandi    function listStart($call) {
8590cecf9d5Sandi        $depth = $this->interpretSyntax($call[1][0], $listType);
8600cecf9d5Sandi
8610cecf9d5Sandi        $this->initialDepth = $depth;
8620cecf9d5Sandi        $this->listStack[] = array($listType, $depth);
8630cecf9d5Sandi
8640cecf9d5Sandi        $this->listCalls[] = array('list'.$listType.'_open',array(),$call[2]);
8650cecf9d5Sandi        $this->listCalls[] = array('listitem_open',array(1),$call[2]);
8660cecf9d5Sandi        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
8670cecf9d5Sandi    }
8680cecf9d5Sandi
8690cecf9d5Sandi    //------------------------------------------------------------------------
8700cecf9d5Sandi    function listEnd($call) {
87144881bd0Shenning.noren        $closeContent = true;
8720cecf9d5Sandi
8730cecf9d5Sandi        while ( $list = array_pop($this->listStack) ) {
8740cecf9d5Sandi            if ( $closeContent ) {
8750cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
87644881bd0Shenning.noren                $closeContent = false;
8770cecf9d5Sandi            }
8780cecf9d5Sandi            $this->listCalls[] = array('listitem_close',array(),$call[2]);
8790cecf9d5Sandi            $this->listCalls[] = array('list'.$list[0].'_close', array(), $call[2]);
8800cecf9d5Sandi        }
8810cecf9d5Sandi    }
8820cecf9d5Sandi
8830cecf9d5Sandi    //------------------------------------------------------------------------
8840cecf9d5Sandi    function listOpen($call) {
8850cecf9d5Sandi        $depth = $this->interpretSyntax($call[1][0], $listType);
8860cecf9d5Sandi        $end = end($this->listStack);
8870cecf9d5Sandi
8880cecf9d5Sandi        // Not allowed to be shallower than initialDepth
8890cecf9d5Sandi        if ( $depth < $this->initialDepth ) {
8900cecf9d5Sandi            $depth = $this->initialDepth;
8910cecf9d5Sandi        }
8920cecf9d5Sandi
8930cecf9d5Sandi        //------------------------------------------------------------------------
8940cecf9d5Sandi        if ( $depth == $end[1] ) {
8950cecf9d5Sandi
8960cecf9d5Sandi            // Just another item in the list...
8970cecf9d5Sandi            if ( $listType == $end[0] ) {
8980cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
8990cecf9d5Sandi                $this->listCalls[] = array('listitem_close',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            // Switched list type...
9040cecf9d5Sandi            } else {
9050cecf9d5Sandi
9060cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9070cecf9d5Sandi                $this->listCalls[] = array('listitem_close',array(),$call[2]);
9080cecf9d5Sandi                $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
9090cecf9d5Sandi                $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9100cecf9d5Sandi                $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9110cecf9d5Sandi                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9120cecf9d5Sandi
9130cecf9d5Sandi                array_pop($this->listStack);
9140cecf9d5Sandi                $this->listStack[] = array($listType, $depth);
9150cecf9d5Sandi            }
9160cecf9d5Sandi
9170cecf9d5Sandi        //------------------------------------------------------------------------
9180cecf9d5Sandi        // Getting deeper...
9190cecf9d5Sandi        } else if ( $depth > $end[1] ) {
9200cecf9d5Sandi
9210cecf9d5Sandi            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9220cecf9d5Sandi            $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9230cecf9d5Sandi            $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9240cecf9d5Sandi            $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9250cecf9d5Sandi
9260cecf9d5Sandi            $this->listStack[] = array($listType, $depth);
9270cecf9d5Sandi
9280cecf9d5Sandi        //------------------------------------------------------------------------
9290cecf9d5Sandi        // Getting shallower ( $depth < $end[1] )
9300cecf9d5Sandi        } else {
9310cecf9d5Sandi            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9320cecf9d5Sandi            $this->listCalls[] = array('listitem_close',array(),$call[2]);
9330cecf9d5Sandi            $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
9340cecf9d5Sandi
9350cecf9d5Sandi            // Throw away the end - done
9360cecf9d5Sandi            array_pop($this->listStack);
9370cecf9d5Sandi
9380cecf9d5Sandi            while (1) {
9390cecf9d5Sandi                $end = end($this->listStack);
9400cecf9d5Sandi
9410cecf9d5Sandi                if ( $end[1] <= $depth ) {
9420cecf9d5Sandi
9430cecf9d5Sandi                    // Normalize depths
9440cecf9d5Sandi                    $depth = $end[1];
9450cecf9d5Sandi
9460cecf9d5Sandi                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
9470cecf9d5Sandi
9480cecf9d5Sandi                    if ( $end[0] == $listType ) {
9490cecf9d5Sandi                        $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
9500cecf9d5Sandi                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9510cecf9d5Sandi
9520cecf9d5Sandi                    } else {
9530cecf9d5Sandi                        // Switching list type...
9540cecf9d5Sandi                        $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
9550cecf9d5Sandi                        $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9560cecf9d5Sandi                        $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9570cecf9d5Sandi                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9580cecf9d5Sandi
9590cecf9d5Sandi                        array_pop($this->listStack);
9600cecf9d5Sandi                        $this->listStack[] = array($listType, $depth);
9610cecf9d5Sandi                    }
9620cecf9d5Sandi
9630cecf9d5Sandi                    break;
9640cecf9d5Sandi
9650cecf9d5Sandi                // Haven't dropped down far enough yet.... ( $end[1] > $depth )
9660cecf9d5Sandi                } else {
9670cecf9d5Sandi
9680cecf9d5Sandi                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
9690cecf9d5Sandi                    $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
9700cecf9d5Sandi
9710cecf9d5Sandi                    array_pop($this->listStack);
9720cecf9d5Sandi
9730cecf9d5Sandi                }
9740cecf9d5Sandi
9750cecf9d5Sandi            }
9760cecf9d5Sandi
9770cecf9d5Sandi        }
9780cecf9d5Sandi    }
9790cecf9d5Sandi
9800cecf9d5Sandi    //------------------------------------------------------------------------
9810cecf9d5Sandi    function listContent($call) {
9820cecf9d5Sandi        $this->listCalls[] = $call;
9830cecf9d5Sandi    }
9840cecf9d5Sandi
9850cecf9d5Sandi    //------------------------------------------------------------------------
9860cecf9d5Sandi    function interpretSyntax($match, & $type) {
9870cecf9d5Sandi        if ( substr($match,-1) == '*' ) {
9880cecf9d5Sandi            $type = 'u';
9890cecf9d5Sandi        } else {
9900cecf9d5Sandi            $type = 'o';
9910cecf9d5Sandi        }
9924b7f9e70STom N Harris        // Is the +1 needed? It used to be count(explode(...))
9934b7f9e70STom N Harris        // but I don't think the number is seen outside this handler
9944b7f9e70STom N Harris        return substr_count(str_replace("\t",'  ',$match), '  ') + 1;
9950cecf9d5Sandi    }
9960cecf9d5Sandi}
9970cecf9d5Sandi
9980cecf9d5Sandi//------------------------------------------------------------------------
9990cecf9d5Sandiclass Doku_Handler_Preformatted {
10000cecf9d5Sandi
10010cecf9d5Sandi    var $CallWriter;
10020cecf9d5Sandi
10030cecf9d5Sandi    var $calls = array();
10040cecf9d5Sandi    var $pos;
10050cecf9d5Sandi    var $text ='';
10060cecf9d5Sandi
10070cecf9d5Sandi
10080cecf9d5Sandi
10090cecf9d5Sandi    function Doku_Handler_Preformatted(& $CallWriter) {
10100cecf9d5Sandi        $this->CallWriter = & $CallWriter;
10110cecf9d5Sandi    }
10120cecf9d5Sandi
10130cecf9d5Sandi    function writeCall($call) {
10140cecf9d5Sandi        $this->calls[] = $call;
10150cecf9d5Sandi    }
10160cecf9d5Sandi
10170cecf9d5Sandi    // Probably not needed but just in case...
10180cecf9d5Sandi    function writeCalls($calls) {
10190cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1020f4f02a0fSchris#        $this->CallWriter->writeCalls($this->calls);
1021f4f02a0fSchris    }
1022f4f02a0fSchris
1023f4f02a0fSchris    function finalise() {
1024f4f02a0fSchris        $last_call = end($this->calls);
1025f4f02a0fSchris        $this->writeCall(array('preformatted_end',array(), $last_call[2]));
1026f4f02a0fSchris
1027f4f02a0fSchris        $this->process();
1028f4f02a0fSchris        $this->CallWriter->finalise();
10293893df8eSChristopher Smith        unset($this->CallWriter);
10300cecf9d5Sandi    }
10310cecf9d5Sandi
10320cecf9d5Sandi    function process() {
10330cecf9d5Sandi        foreach ( $this->calls as $call ) {
10340cecf9d5Sandi            switch ($call[0]) {
10350cecf9d5Sandi                case 'preformatted_start':
10360cecf9d5Sandi                    $this->pos = $call[2];
10370cecf9d5Sandi                break;
10380cecf9d5Sandi                case 'preformatted_newline':
10390cecf9d5Sandi                    $this->text .= "\n";
10400cecf9d5Sandi                break;
10410cecf9d5Sandi                case 'preformatted_content':
10420cecf9d5Sandi                    $this->text .= $call[1][0];
10430cecf9d5Sandi                break;
10440cecf9d5Sandi                case 'preformatted_end':
104593a34bf3SChris Smith                    if (trim($this->text)) {
10460cecf9d5Sandi                        $this->CallWriter->writeCall(array('preformatted',array($this->text),$this->pos));
104793a34bf3SChris Smith                    }
104895c19ce7SChris Smith                    // see FS#1699 & FS#1652, add 'eol' instructions to ensure proper triggering of following p_open
104995c19ce7SChris Smith                    $this->CallWriter->writeCall(array('eol',array(),$this->pos));
105095c19ce7SChris Smith                    $this->CallWriter->writeCall(array('eol',array(),$this->pos));
10510cecf9d5Sandi                break;
10520cecf9d5Sandi            }
10530cecf9d5Sandi        }
10540cecf9d5Sandi    }
1055f4f02a0fSchris
10560cecf9d5Sandi}
10570cecf9d5Sandi
10580cecf9d5Sandi//------------------------------------------------------------------------
10590cecf9d5Sandiclass Doku_Handler_Quote {
10600cecf9d5Sandi
10610cecf9d5Sandi    var $CallWriter;
10620cecf9d5Sandi
10630cecf9d5Sandi    var $calls = array();
10640cecf9d5Sandi
10650cecf9d5Sandi    var $quoteCalls = array();
10660cecf9d5Sandi
10670cecf9d5Sandi    function Doku_Handler_Quote(& $CallWriter) {
10680cecf9d5Sandi        $this->CallWriter = & $CallWriter;
10690cecf9d5Sandi    }
10700cecf9d5Sandi
10710cecf9d5Sandi    function writeCall($call) {
10720cecf9d5Sandi        $this->calls[] = $call;
10730cecf9d5Sandi    }
10740cecf9d5Sandi
10750cecf9d5Sandi    // Probably not needed but just in case...
10760cecf9d5Sandi    function writeCalls($calls) {
10770cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1078f4f02a0fSchris    }
1079f4f02a0fSchris
1080f4f02a0fSchris    function finalise() {
1081f4f02a0fSchris        $last_call = end($this->calls);
1082f4f02a0fSchris        $this->writeCall(array('quote_end',array(), $last_call[2]));
1083f4f02a0fSchris
1084f4f02a0fSchris        $this->process();
1085f4f02a0fSchris        $this->CallWriter->finalise();
10863893df8eSChristopher Smith        unset($this->CallWriter);
10870cecf9d5Sandi    }
10880cecf9d5Sandi
10890cecf9d5Sandi    function process() {
10900cecf9d5Sandi
10910cecf9d5Sandi        $quoteDepth = 1;
10920cecf9d5Sandi
10930cecf9d5Sandi        foreach ( $this->calls as $call ) {
10940cecf9d5Sandi            switch ($call[0]) {
10950cecf9d5Sandi
10960cecf9d5Sandi                case 'quote_start':
10970cecf9d5Sandi
10980cecf9d5Sandi                    $this->quoteCalls[] = array('quote_open',array(),$call[2]);
10990cecf9d5Sandi
11000cecf9d5Sandi                case 'quote_newline':
11010cecf9d5Sandi
11020cecf9d5Sandi                    $quoteLength = $this->getDepth($call[1][0]);
11030cecf9d5Sandi
11040cecf9d5Sandi                    if ( $quoteLength > $quoteDepth ) {
11050cecf9d5Sandi                        $quoteDiff = $quoteLength - $quoteDepth;
11060cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11070cecf9d5Sandi                            $this->quoteCalls[] = array('quote_open',array(),$call[2]);
11080cecf9d5Sandi                        }
11090cecf9d5Sandi                    } else if ( $quoteLength < $quoteDepth ) {
11100cecf9d5Sandi                        $quoteDiff = $quoteDepth - $quoteLength;
11110cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11120cecf9d5Sandi                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11130cecf9d5Sandi                        }
111426426c64Schris                    } else {
111526426c64Schris                        if ($call[0] != 'quote_start') $this->quoteCalls[] = array('linebreak',array(),$call[2]);
11160cecf9d5Sandi                    }
11170cecf9d5Sandi
11180cecf9d5Sandi                    $quoteDepth = $quoteLength;
11190cecf9d5Sandi
11200cecf9d5Sandi                break;
11210cecf9d5Sandi
11220cecf9d5Sandi                case 'quote_end':
11230cecf9d5Sandi
11240cecf9d5Sandi                    if ( $quoteDepth > 1 ) {
11250cecf9d5Sandi                        $quoteDiff = $quoteDepth - 1;
11260cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11270cecf9d5Sandi                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11280cecf9d5Sandi                        }
11290cecf9d5Sandi                    }
11300cecf9d5Sandi
11310cecf9d5Sandi                    $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11320cecf9d5Sandi
11330cecf9d5Sandi                    $this->CallWriter->writeCalls($this->quoteCalls);
11340cecf9d5Sandi                break;
11350cecf9d5Sandi
11360cecf9d5Sandi                default:
11370cecf9d5Sandi                    $this->quoteCalls[] = $call;
11380cecf9d5Sandi                break;
11390cecf9d5Sandi            }
11400cecf9d5Sandi        }
11410cecf9d5Sandi    }
11420cecf9d5Sandi
11430cecf9d5Sandi    function getDepth($marker) {
11440cecf9d5Sandi        preg_match('/>{1,}/', $marker, $matches);
11450cecf9d5Sandi        $quoteLength = strlen($matches[0]);
11460cecf9d5Sandi        return $quoteLength;
11470cecf9d5Sandi    }
11480cecf9d5Sandi}
11490cecf9d5Sandi
11500cecf9d5Sandi//------------------------------------------------------------------------
11510cecf9d5Sandiclass Doku_Handler_Table {
11520cecf9d5Sandi
11530cecf9d5Sandi    var $CallWriter;
11540cecf9d5Sandi
11550cecf9d5Sandi    var $calls = array();
11560cecf9d5Sandi    var $tableCalls = array();
11570cecf9d5Sandi    var $maxCols = 0;
11580cecf9d5Sandi    var $maxRows = 1;
11590cecf9d5Sandi    var $currentCols = 0;
116044881bd0Shenning.noren    var $firstCell = false;
11610cecf9d5Sandi    var $lastCellType = 'tablecell';
11629060b8b0SChristopher Smith    var $inTableHead = true;
1163cef031c1SChristopher Smith    var $currentRow = array('tableheader' => 0, 'tablecell' => 0);
11649060b8b0SChristopher Smith    var $countTableHeadRows = 0;
11650cecf9d5Sandi
11660cecf9d5Sandi    function Doku_Handler_Table(& $CallWriter) {
11670cecf9d5Sandi        $this->CallWriter = & $CallWriter;
11680cecf9d5Sandi    }
11690cecf9d5Sandi
11700cecf9d5Sandi    function writeCall($call) {
11710cecf9d5Sandi        $this->calls[] = $call;
11720cecf9d5Sandi    }
11730cecf9d5Sandi
11740cecf9d5Sandi    // Probably not needed but just in case...
11750cecf9d5Sandi    function writeCalls($calls) {
11760cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1177f4f02a0fSchris    }
1178f4f02a0fSchris
1179f4f02a0fSchris    function finalise() {
1180f4f02a0fSchris        $last_call = end($this->calls);
1181f4f02a0fSchris        $this->writeCall(array('table_end',array(), $last_call[2]));
1182f4f02a0fSchris
1183f4f02a0fSchris        $this->process();
1184f4f02a0fSchris        $this->CallWriter->finalise();
11853893df8eSChristopher Smith        unset($this->CallWriter);
11860cecf9d5Sandi    }
11870cecf9d5Sandi
11880cecf9d5Sandi    //------------------------------------------------------------------------
11890cecf9d5Sandi    function process() {
11900cecf9d5Sandi        foreach ( $this->calls as $call ) {
11910cecf9d5Sandi            switch ( $call[0] ) {
11920cecf9d5Sandi                case 'table_start':
11930cecf9d5Sandi                    $this->tableStart($call);
11940cecf9d5Sandi                break;
11950cecf9d5Sandi                case 'table_row':
1196aa92c4ccSAdrian Lang                    $this->tableRowClose($call);
11970cecf9d5Sandi                    $this->tableRowOpen(array('tablerow_open',$call[1],$call[2]));
11980cecf9d5Sandi                break;
11990cecf9d5Sandi                case 'tableheader':
12000cecf9d5Sandi                case 'tablecell':
12010cecf9d5Sandi                    $this->tableCell($call);
12020cecf9d5Sandi                break;
12030cecf9d5Sandi                case 'table_end':
1204aa92c4ccSAdrian Lang                    $this->tableRowClose($call);
12050cecf9d5Sandi                    $this->tableEnd($call);
12060cecf9d5Sandi                break;
12070cecf9d5Sandi                default:
12080cecf9d5Sandi                    $this->tableDefault($call);
12090cecf9d5Sandi                break;
12100cecf9d5Sandi            }
12110cecf9d5Sandi        }
12120cecf9d5Sandi        $this->CallWriter->writeCalls($this->tableCalls);
12130cecf9d5Sandi    }
12140cecf9d5Sandi
12150cecf9d5Sandi    function tableStart($call) {
121690df9a4dSAdrian Lang        $this->tableCalls[] = array('table_open',$call[1],$call[2]);
12170cecf9d5Sandi        $this->tableCalls[] = array('tablerow_open',array(),$call[2]);
121844881bd0Shenning.noren        $this->firstCell = true;
12190cecf9d5Sandi    }
12200cecf9d5Sandi
12210cecf9d5Sandi    function tableEnd($call) {
122207c2b1c7SAdrian Lang        $this->tableCalls[] = array('table_close',$call[1],$call[2]);
12230cecf9d5Sandi        $this->finalizeTable();
12240cecf9d5Sandi    }
12250cecf9d5Sandi
12260cecf9d5Sandi    function tableRowOpen($call) {
12270cecf9d5Sandi        $this->tableCalls[] = $call;
12280cecf9d5Sandi        $this->currentCols = 0;
122944881bd0Shenning.noren        $this->firstCell = true;
12300cecf9d5Sandi        $this->lastCellType = 'tablecell';
12310cecf9d5Sandi        $this->maxRows++;
1232cef031c1SChristopher Smith        if ($this->inTableHead) {
1233cef031c1SChristopher Smith            $this->currentRow = array('tablecell' => 0, 'tableheader' => 0);
1234cef031c1SChristopher Smith        }
12350cecf9d5Sandi    }
12360cecf9d5Sandi
12370cecf9d5Sandi    function tableRowClose($call) {
1238cef031c1SChristopher Smith        if ($this->inTableHead && ($this->inTableHead = $this->isTableHeadRow())) {
12399060b8b0SChristopher Smith            $this->countTableHeadRows++;
12409060b8b0SChristopher Smith        }
12410cecf9d5Sandi        // Strip off final cell opening and anything after it
12420cecf9d5Sandi        while ( $discard = array_pop($this->tableCalls ) ) {
12430cecf9d5Sandi
12440cecf9d5Sandi            if ( $discard[0] == 'tablecell_open' || $discard[0] == 'tableheader_open') {
12450cecf9d5Sandi                break;
12460cecf9d5Sandi            }
1247cef031c1SChristopher Smith            if (!empty($this->currentRow[$discard[0]])) {
1248cef031c1SChristopher Smith                $this->currentRow[$discard[0]]--;
1249cef031c1SChristopher Smith            }
12500cecf9d5Sandi        }
1251aa92c4ccSAdrian Lang        $this->tableCalls[] = array('tablerow_close', array(), $call[2]);
12520cecf9d5Sandi
12530cecf9d5Sandi        if ( $this->currentCols > $this->maxCols ) {
12540cecf9d5Sandi            $this->maxCols = $this->currentCols;
12550cecf9d5Sandi        }
12560cecf9d5Sandi    }
12570cecf9d5Sandi
1258cef031c1SChristopher Smith    function isTableHeadRow() {
1259cef031c1SChristopher Smith        $td = $this->currentRow['tablecell'];
1260cef031c1SChristopher Smith        $th = $this->currentRow['tableheader'];
1261cef031c1SChristopher Smith
1262cef031c1SChristopher Smith        if (!$th || $td > 2) return false;
1263cef031c1SChristopher Smith        if (2*$td > $th) return false;
1264cef031c1SChristopher Smith
1265cef031c1SChristopher Smith        return true;
1266cef031c1SChristopher Smith    }
1267cef031c1SChristopher Smith
12680cecf9d5Sandi    function tableCell($call) {
1269cef031c1SChristopher Smith        if ($this->inTableHead) {
1270cef031c1SChristopher Smith            $this->currentRow[$call[0]]++;
12719060b8b0SChristopher Smith        }
12720cecf9d5Sandi        if ( !$this->firstCell ) {
12730cecf9d5Sandi
12740cecf9d5Sandi            // Increase the span
12750cecf9d5Sandi            $lastCall = end($this->tableCalls);
12760cecf9d5Sandi
12770cecf9d5Sandi            // A cell call which follows an open cell means an empty cell so span
12780cecf9d5Sandi            if ( $lastCall[0] == 'tablecell_open' || $lastCall[0] == 'tableheader_open' ) {
12790cecf9d5Sandi                 $this->tableCalls[] = array('colspan',array(),$call[2]);
12800cecf9d5Sandi
12810cecf9d5Sandi            }
12820cecf9d5Sandi
12830cecf9d5Sandi            $this->tableCalls[] = array($this->lastCellType.'_close',array(),$call[2]);
12840ea51e63SMatt Perry            $this->tableCalls[] = array($call[0].'_open',array(1,null,1),$call[2]);
12850cecf9d5Sandi            $this->lastCellType = $call[0];
12860cecf9d5Sandi
12870cecf9d5Sandi        } else {
12880cecf9d5Sandi
12890ea51e63SMatt Perry            $this->tableCalls[] = array($call[0].'_open',array(1,null,1),$call[2]);
12900cecf9d5Sandi            $this->lastCellType = $call[0];
129144881bd0Shenning.noren            $this->firstCell = false;
12920cecf9d5Sandi
12930cecf9d5Sandi        }
12940cecf9d5Sandi
12950cecf9d5Sandi        $this->currentCols++;
12960cecf9d5Sandi    }
12970cecf9d5Sandi
12980cecf9d5Sandi    function tableDefault($call) {
12990cecf9d5Sandi        $this->tableCalls[] = $call;
13000cecf9d5Sandi    }
13010cecf9d5Sandi
13020cecf9d5Sandi    function finalizeTable() {
13030cecf9d5Sandi
13040cecf9d5Sandi        // Add the max cols and rows to the table opening
13050cecf9d5Sandi        if ( $this->tableCalls[0][0] == 'table_open' ) {
13060cecf9d5Sandi            // Adjust to num cols not num col delimeters
13070cecf9d5Sandi            $this->tableCalls[0][1][] = $this->maxCols - 1;
13080cecf9d5Sandi            $this->tableCalls[0][1][] = $this->maxRows;
130990df9a4dSAdrian Lang            $this->tableCalls[0][1][] = array_shift($this->tableCalls[0][1]);
13100cecf9d5Sandi        } else {
13110cecf9d5Sandi            trigger_error('First element in table call list is not table_open');
13120cecf9d5Sandi        }
13130cecf9d5Sandi
13140cecf9d5Sandi        $lastRow = 0;
13150cecf9d5Sandi        $lastCell = 0;
131625b97867Shakan.sandell        $cellKey = array();
13170cecf9d5Sandi        $toDelete = array();
13180cecf9d5Sandi
1319cef031c1SChristopher Smith        // if still in tableheader, then there can be no table header
1320cef031c1SChristopher Smith        // as all rows can't be within <THEAD>
1321cef031c1SChristopher Smith        if ($this->inTableHead) {
1322cef031c1SChristopher Smith            $this->inTableHead = false;
1323cef031c1SChristopher Smith            $this->countTableHeadRows = 0;
1324cef031c1SChristopher Smith        }
1325cef031c1SChristopher Smith
13260cecf9d5Sandi        // Look for the colspan elements and increment the colspan on the
13270cecf9d5Sandi        // previous non-empty opening cell. Once done, delete all the cells
13280cecf9d5Sandi        // that contain colspans
13296606d6fcSAdrian Lang        for ($key = 0 ; $key < count($this->tableCalls) ; ++$key) {
13306606d6fcSAdrian Lang            $call = $this->tableCalls[$key];
13310cecf9d5Sandi
1332aa92c4ccSAdrian Lang            switch ($call[0]) {
13339060b8b0SChristopher Smith                case 'table_open' :
13349060b8b0SChristopher Smith                    if($this->countTableHeadRows) {
13359060b8b0SChristopher Smith                        array_splice($this->tableCalls, $key+1, 0, array(
13369060b8b0SChristopher Smith                              array('tablethead_open', array(), $call[2]))
13379060b8b0SChristopher Smith                        );
13389060b8b0SChristopher Smith                    }
13399060b8b0SChristopher Smith                    break;
13409060b8b0SChristopher Smith
1341aa92c4ccSAdrian Lang                case 'tablerow_open':
13420cecf9d5Sandi
134325b97867Shakan.sandell                    $lastRow++;
134425b97867Shakan.sandell                    $lastCell = 0;
1345aa92c4ccSAdrian Lang                    break;
13460cecf9d5Sandi
1347aa92c4ccSAdrian Lang                case 'tablecell_open':
1348aa92c4ccSAdrian Lang                case 'tableheader_open':
13490cecf9d5Sandi
135025b97867Shakan.sandell                    $lastCell++;
135125b97867Shakan.sandell                    $cellKey[$lastRow][$lastCell] = $key;
1352aa92c4ccSAdrian Lang                    break;
13530cecf9d5Sandi
1354aa92c4ccSAdrian Lang                case 'table_align':
13550cecf9d5Sandi
1356e03b8b2eSAdrian Lang                    $prev = in_array($this->tableCalls[$key-1][0], array('tablecell_open', 'tableheader_open'));
1357e03b8b2eSAdrian Lang                    $next = in_array($this->tableCalls[$key+1][0], array('tablecell_close', 'tableheader_close'));
1358e03b8b2eSAdrian Lang                    // If the cell is empty, align left
1359e03b8b2eSAdrian Lang                    if ($prev && $next) {
1360e03b8b2eSAdrian Lang                        $this->tableCalls[$key-1][1][1] = 'left';
1361e03b8b2eSAdrian Lang
13620cecf9d5Sandi                    // If the previous element was a cell open, align right
1363e03b8b2eSAdrian Lang                    } elseif ($prev) {
13640cecf9d5Sandi                        $this->tableCalls[$key-1][1][1] = 'right';
13650cecf9d5Sandi
1366e03b8b2eSAdrian Lang                    // If the next element is the close of an element, align either center or left
1367e03b8b2eSAdrian Lang                    } elseif ( $next) {
136825b97867Shakan.sandell                        if ( $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] == 'right' ) {
136925b97867Shakan.sandell                            $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] = 'center';
13700cecf9d5Sandi                        } else {
137125b97867Shakan.sandell                            $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] = 'left';
13720cecf9d5Sandi                        }
13730cecf9d5Sandi
13740cecf9d5Sandi                    }
13750cecf9d5Sandi
13760cecf9d5Sandi                    // Now convert the whitespace back to cdata
13770cecf9d5Sandi                    $this->tableCalls[$key][0] = 'cdata';
1378aa92c4ccSAdrian Lang                    break;
13790cecf9d5Sandi
1380aa92c4ccSAdrian Lang                case 'colspan':
13810cecf9d5Sandi
138244881bd0Shenning.noren                    $this->tableCalls[$key-1][1][0] = false;
13830cecf9d5Sandi
138425b97867Shakan.sandell                    for($i = $key-2; $i >= $cellKey[$lastRow][1]; $i--) {
13850cecf9d5Sandi
13860cecf9d5Sandi                        if ( $this->tableCalls[$i][0] == 'tablecell_open' || $this->tableCalls[$i][0] == 'tableheader_open' ) {
13870cecf9d5Sandi
138844881bd0Shenning.noren                            if ( false !== $this->tableCalls[$i][1][0] ) {
13890cecf9d5Sandi                                $this->tableCalls[$i][1][0]++;
13900cecf9d5Sandi                                break;
13910cecf9d5Sandi                            }
13920cecf9d5Sandi
13930cecf9d5Sandi                        }
13940cecf9d5Sandi                    }
13950cecf9d5Sandi
13960cecf9d5Sandi                    $toDelete[] = $key-1;
13970cecf9d5Sandi                    $toDelete[] = $key;
13980cecf9d5Sandi                    $toDelete[] = $key+1;
1399aa92c4ccSAdrian Lang                    break;
140025b97867Shakan.sandell
1401aa92c4ccSAdrian Lang                case 'rowspan':
140225b97867Shakan.sandell
140325b97867Shakan.sandell                    if ( $this->tableCalls[$key-1][0] == 'cdata' ) {
140425b97867Shakan.sandell                        // ignore rowspan if previous call was cdata (text mixed with :::) we don't have to check next call as that wont match regex
140525b97867Shakan.sandell                        $this->tableCalls[$key][0] = 'cdata';
140625b97867Shakan.sandell
140725b97867Shakan.sandell                    } else {
140825b97867Shakan.sandell
14096606d6fcSAdrian Lang                        $spanning_cell = null;
14109060b8b0SChristopher Smith
14119060b8b0SChristopher Smith                        // can't cross thead/tbody boundary
14129060b8b0SChristopher Smith                        if (!$this->countTableHeadRows || ($lastRow-1 != $this->countTableHeadRows)) {
141325b97867Shakan.sandell                            for($i = $lastRow-1; $i > 0; $i--) {
141425b97867Shakan.sandell
141525b97867Shakan.sandell                                if ( $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tablecell_open' || $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tableheader_open' ) {
141625b97867Shakan.sandell
14176606d6fcSAdrian Lang                                    if ($this->tableCalls[$cellKey[$i][$lastCell]][1][2] >= $lastRow - $i) {
14186606d6fcSAdrian Lang                                        $spanning_cell = $i;
141925b97867Shakan.sandell                                        break;
142025b97867Shakan.sandell                                    }
142125b97867Shakan.sandell
142225b97867Shakan.sandell                                }
142325b97867Shakan.sandell                            }
14249060b8b0SChristopher Smith                        }
14256606d6fcSAdrian Lang                        if (is_null($spanning_cell)) {
14266606d6fcSAdrian Lang                            // No spanning cell found, so convert this cell to
14276606d6fcSAdrian Lang                            // an empty one to avoid broken tables
14286a7e97d5SGerrit Uitslag                            $this->tableCalls[$key][0] = 'cdata';
14296a7e97d5SGerrit Uitslag                            $this->tableCalls[$key][1][0] = '';
14306606d6fcSAdrian Lang                            continue;
14316606d6fcSAdrian Lang                        }
14326606d6fcSAdrian Lang                        $this->tableCalls[$cellKey[$spanning_cell][$lastCell]][1][2]++;
14336606d6fcSAdrian Lang
14346606d6fcSAdrian Lang                        $this->tableCalls[$key-1][1][2] = false;
143525b97867Shakan.sandell
143625b97867Shakan.sandell                        $toDelete[] = $key-1;
143725b97867Shakan.sandell                        $toDelete[] = $key;
143825b97867Shakan.sandell                        $toDelete[] = $key+1;
143925b97867Shakan.sandell                    }
1440aa92c4ccSAdrian Lang                    break;
1441aa92c4ccSAdrian Lang
14426606d6fcSAdrian Lang                case 'tablerow_close':
14436606d6fcSAdrian Lang
14446606d6fcSAdrian Lang                    // Fix broken tables by adding missing cells
14456606d6fcSAdrian Lang                    while (++$lastCell < $this->maxCols) {
14466606d6fcSAdrian Lang                        array_splice($this->tableCalls, $key, 0, array(
14476606d6fcSAdrian Lang                               array('tablecell_open', array(1, null, 1), $call[2]),
14486606d6fcSAdrian Lang                               array('cdata', array(''), $call[2]),
14496606d6fcSAdrian Lang                               array('tablecell_close', array(), $call[2])));
14506606d6fcSAdrian Lang                        $key += 3;
14516606d6fcSAdrian Lang                    }
14526606d6fcSAdrian Lang
14539060b8b0SChristopher Smith                    if($this->countTableHeadRows == $lastRow) {
1454f05a1cc5SGerrit Uitslag                        array_splice($this->tableCalls, $key+1, 0, array(
1455f05a1cc5SGerrit Uitslag                              array('tablethead_close', array(), $call[2])));
1456f05a1cc5SGerrit Uitslag                    }
14576606d6fcSAdrian Lang                    break;
14586606d6fcSAdrian Lang
14590cecf9d5Sandi            }
14600cecf9d5Sandi        }
14610cecf9d5Sandi
14629ab75d9eSAndreas Gohr        // condense cdata
14639ab75d9eSAndreas Gohr        $cnt = count($this->tableCalls);
14649ab75d9eSAndreas Gohr        for( $key = 0; $key < $cnt; $key++){
14659ab75d9eSAndreas Gohr            if($this->tableCalls[$key][0] == 'cdata'){
14669ab75d9eSAndreas Gohr                $ckey = $key;
14679ab75d9eSAndreas Gohr                $key++;
14689ab75d9eSAndreas Gohr                while($this->tableCalls[$key][0] == 'cdata'){
14699ab75d9eSAndreas Gohr                    $this->tableCalls[$ckey][1][0] .= $this->tableCalls[$key][1][0];
14709ab75d9eSAndreas Gohr                    $toDelete[] = $key;
14719ab75d9eSAndreas Gohr                    $key++;
14729ab75d9eSAndreas Gohr                }
14739ab75d9eSAndreas Gohr                continue;
14749ab75d9eSAndreas Gohr            }
14759ab75d9eSAndreas Gohr        }
14769ab75d9eSAndreas Gohr
14770cecf9d5Sandi        foreach ( $toDelete as $delete ) {
14780cecf9d5Sandi            unset($this->tableCalls[$delete]);
14790cecf9d5Sandi        }
14800cecf9d5Sandi        $this->tableCalls = array_values($this->tableCalls);
14810cecf9d5Sandi    }
14820cecf9d5Sandi}
14830cecf9d5Sandi
14840cecf9d5Sandi
14852a27e99aSandi/**
14862a27e99aSandi * Handler for paragraphs
14872a27e99aSandi *
14880b7c14c2Sandi * @author Harry Fuecks <hfuecks@gmail.com>
14892a27e99aSandi */
14900cecf9d5Sandiclass Doku_Handler_Block {
14910cecf9d5Sandi    var $calls = array();
14929569a107SDanny Lin    var $skipEol = false;
149353bfcb59SChristopher Smith    var $inParagraph = false;
14940cecf9d5Sandi
1495af146da0Sandi    // Blocks these should not be inside paragraphs
14960cecf9d5Sandi    var $blockOpen = array(
14970cecf9d5Sandi            'header',
1498df9add72Schris            'listu_open','listo_open','listitem_open','listcontent_open',
1499f05a1cc5SGerrit Uitslag            'table_open','tablerow_open','tablecell_open','tableheader_open','tablethead_open',
15000cecf9d5Sandi            'quote_open',
150176aa94b7Schris            'code','file','hr','preformatted','rss',
150207f89c3cSAnika Henke            'htmlblock','phpblock',
15039569a107SDanny Lin            'footnote_open',
15040cecf9d5Sandi        );
15050cecf9d5Sandi
15060cecf9d5Sandi    var $blockClose = array(
15070cecf9d5Sandi            'header',
1508df9add72Schris            'listu_close','listo_close','listitem_close','listcontent_close',
1509f05a1cc5SGerrit Uitslag            'table_close','tablerow_close','tablecell_close','tableheader_close','tablethead_close',
15100cecf9d5Sandi            'quote_close',
151176aa94b7Schris            'code','file','hr','preformatted','rss',
151207f89c3cSAnika Henke            'htmlblock','phpblock',
15139569a107SDanny Lin            'footnote_close',
15140cecf9d5Sandi        );
15150cecf9d5Sandi
1516af146da0Sandi    // Stacks can contain paragraphs
15170cecf9d5Sandi    var $stackOpen = array(
15189569a107SDanny Lin        'section_open',
15190cecf9d5Sandi        );
15200cecf9d5Sandi
15210cecf9d5Sandi    var $stackClose = array(
15229569a107SDanny Lin        'section_close',
15230cecf9d5Sandi        );
15240cecf9d5Sandi
1525af146da0Sandi
1526af146da0Sandi    /**
1527af146da0Sandi     * Constructor. Adds loaded syntax plugins to the block and stack
1528af146da0Sandi     * arrays
1529af146da0Sandi     *
1530af146da0Sandi     * @author Andreas Gohr <andi@splitbrain.org>
1531af146da0Sandi     */
1532af146da0Sandi    function Doku_Handler_Block(){
1533af146da0Sandi        global $DOKU_PLUGINS;
1534af146da0Sandi        //check if syntax plugins were loaded
153503c4aec3Schris        if(empty($DOKU_PLUGINS['syntax'])) return;
1536af146da0Sandi        foreach($DOKU_PLUGINS['syntax'] as $n => $p){
1537af146da0Sandi            $ptype = $p->getPType();
1538af146da0Sandi            if($ptype == 'block'){
1539af146da0Sandi                $this->blockOpen[]  = 'plugin_'.$n;
1540af146da0Sandi                $this->blockClose[] = 'plugin_'.$n;
1541af146da0Sandi            }elseif($ptype == 'stack'){
1542af146da0Sandi                $this->stackOpen[]  = 'plugin_'.$n;
1543af146da0Sandi                $this->stackClose[] = 'plugin_'.$n;
1544af146da0Sandi            }
1545af146da0Sandi        }
1546af146da0Sandi    }
1547af146da0Sandi
1548b5a0b131SDanny Lin    function openParagraph($pos){
15499569a107SDanny Lin        if ($this->inParagraph) return;
1550b5a0b131SDanny Lin        $this->calls[] = array('p_open',array(), $pos);
1551b5a0b131SDanny Lin        $this->inParagraph = true;
15529569a107SDanny Lin        $this->skipEol = true;
1553b5a0b131SDanny Lin    }
1554b5a0b131SDanny Lin
15552a27e99aSandi    /**
15562a27e99aSandi     * Close a paragraph if needed
15572a27e99aSandi     *
15582a27e99aSandi     * This function makes sure there are no empty paragraphs on the stack
15592a27e99aSandi     *
15602a27e99aSandi     * @author Andreas Gohr <andi@splitbrain.org>
15612a27e99aSandi     */
1562506ae684Sandi    function closeParagraph($pos){
15639569a107SDanny Lin        if (!$this->inParagraph) return;
1564506ae684Sandi        // look back if there was any content - we don't want empty paragraphs
1565506ae684Sandi        $content = '';
1566faba9a35SAndreas Gohr        $ccount = count($this->calls);
1567faba9a35SAndreas Gohr        for($i=$ccount-1; $i>=0; $i--){
1568506ae684Sandi            if($this->calls[$i][0] == 'p_open'){
1569506ae684Sandi                break;
1570506ae684Sandi            }elseif($this->calls[$i][0] == 'cdata'){
1571506ae684Sandi                $content .= $this->calls[$i][1][0];
1572506ae684Sandi            }else{
1573506ae684Sandi                $content = 'found markup';
1574506ae684Sandi                break;
1575506ae684Sandi            }
1576506ae684Sandi        }
1577506ae684Sandi
1578506ae684Sandi        if(trim($content)==''){
1579506ae684Sandi            //remove the whole paragraph
1580a86cc527SAndreas Gohr            //array_splice($this->calls,$i); // <- this is much slower than the loop below
1581d8f7a7f3SAndreas Gohr            for($x=$ccount; $x>$i; $x--) array_pop($this->calls);
1582506ae684Sandi        }else{
15839569a107SDanny Lin            // remove ending linebreaks in the paragraph
15849569a107SDanny Lin            $i=count($this->calls)-1;
15859569a107SDanny Lin            if ($this->calls[$i][0] == 'cdata') $this->calls[$i][1][0] = rtrim($this->calls[$i][1][0],DOKU_PARSER_EOL);
1586506ae684Sandi            $this->calls[] = array('p_close',array(), $pos);
1587506ae684Sandi        }
1588e1c10e4dSchris
158944881bd0Shenning.noren        $this->inParagraph = false;
15909569a107SDanny Lin        $this->skipEol = true;
15910cecf9d5Sandi    }
159241624b31SChris Smith
159341624b31SChris Smith    function addCall($call) {
159441624b31SChris Smith        $key = count($this->calls);
159541624b31SChris Smith        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
159641624b31SChris Smith            $this->calls[$key-1][1][0] .= $call[1][0];
159741624b31SChris Smith        } else {
159841624b31SChris Smith            $this->calls[] = $call;
159941624b31SChris Smith        }
160041624b31SChris Smith    }
16019569a107SDanny Lin
16029569a107SDanny Lin    // simple version of addCall, without checking cdata
16039569a107SDanny Lin    function storeCall($call) {
16049569a107SDanny Lin        $this->calls[] = $call;
16059569a107SDanny Lin    }
16069569a107SDanny Lin
16079569a107SDanny Lin    /**
16089569a107SDanny Lin     * Processes the whole instruction stack to open and close paragraphs
16099569a107SDanny Lin     *
16109569a107SDanny Lin     * @author Harry Fuecks <hfuecks@gmail.com>
16119569a107SDanny Lin     * @author Andreas Gohr <andi@splitbrain.org>
16129569a107SDanny Lin     */
16139569a107SDanny Lin    function process($calls) {
16149569a107SDanny Lin        // open first paragraph
16159569a107SDanny Lin        $this->openParagraph(0);
16169569a107SDanny Lin        foreach ( $calls as $key => $call ) {
16179569a107SDanny Lin            $cname = $call[0];
16189569a107SDanny Lin            if ($cname == 'plugin') {
16199569a107SDanny Lin                $cname='plugin_'.$call[1][0];
16209569a107SDanny Lin                $plugin = true;
16219569a107SDanny Lin                $plugin_open = (($call[1][2] == DOKU_LEXER_ENTER) || ($call[1][2] == DOKU_LEXER_SPECIAL));
16229569a107SDanny Lin                $plugin_close = (($call[1][2] == DOKU_LEXER_EXIT) || ($call[1][2] == DOKU_LEXER_SPECIAL));
16239569a107SDanny Lin            } else {
16249569a107SDanny Lin                $plugin = false;
16259569a107SDanny Lin            }
16269569a107SDanny Lin            /* stack */
16279569a107SDanny Lin            if ( in_array($cname,$this->stackClose ) && (!$plugin || $plugin_close)) {
16289569a107SDanny Lin                $this->closeParagraph($call[2]);
16299569a107SDanny Lin                $this->storeCall($call);
16309569a107SDanny Lin                $this->openParagraph($call[2]);
16319569a107SDanny Lin                continue;
16329569a107SDanny Lin            }
16339569a107SDanny Lin            if ( in_array($cname,$this->stackOpen ) && (!$plugin || $plugin_open) ) {
16349569a107SDanny Lin                $this->closeParagraph($call[2]);
16359569a107SDanny Lin                $this->storeCall($call);
16369569a107SDanny Lin                $this->openParagraph($call[2]);
16379569a107SDanny Lin                continue;
16389569a107SDanny Lin            }
16399569a107SDanny Lin            /* block */
16409569a107SDanny Lin            // If it's a substition it opens and closes at the same call.
16419569a107SDanny Lin            // To make sure next paragraph is correctly started, let close go first.
16429569a107SDanny Lin            if ( in_array($cname, $this->blockClose) && (!$plugin || $plugin_close)) {
16439569a107SDanny Lin                $this->closeParagraph($call[2]);
16449569a107SDanny Lin                $this->storeCall($call);
16459569a107SDanny Lin                $this->openParagraph($call[2]);
16469569a107SDanny Lin                continue;
16479569a107SDanny Lin            }
16489569a107SDanny Lin            if ( in_array($cname, $this->blockOpen) && (!$plugin || $plugin_open)) {
16499569a107SDanny Lin                $this->closeParagraph($call[2]);
16509569a107SDanny Lin                $this->storeCall($call);
16519569a107SDanny Lin                continue;
16529569a107SDanny Lin            }
16539569a107SDanny Lin            /* eol */
16549569a107SDanny Lin            if ( $cname == 'eol' ) {
16559569a107SDanny Lin                // Check this isn't an eol instruction to skip...
16569569a107SDanny Lin                if ( !$this->skipEol ) {
16579569a107SDanny Lin                    // Next is EOL => double eol => mark as paragraph
16589569a107SDanny Lin                    if ( isset($calls[$key+1]) && $calls[$key+1][0] == 'eol' ) {
16599569a107SDanny Lin                        $this->closeParagraph($call[2]);
16609569a107SDanny Lin                        $this->openParagraph($call[2]);
16619569a107SDanny Lin                    } else {
16629569a107SDanny Lin                        //if this is just a single eol make a space from it
16639569a107SDanny Lin                        $this->addCall(array('cdata',array(DOKU_PARSER_EOL), $call[2]));
16649569a107SDanny Lin                    }
16659569a107SDanny Lin                }
16669569a107SDanny Lin                continue;
16679569a107SDanny Lin            }
16689569a107SDanny Lin            /* normal */
16699569a107SDanny Lin            $this->addCall($call);
16709569a107SDanny Lin            $this->skipEol = false;
16719569a107SDanny Lin        }
16729569a107SDanny Lin        // close last paragraph
16739569a107SDanny Lin        $call = end($this->calls);
16749569a107SDanny Lin        $this->closeParagraph($call[2]);
16759569a107SDanny Lin        return $this->calls;
16769569a107SDanny Lin    }
16770cecf9d5Sandi}
16782a27e99aSandi
1679e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
1680