xref: /dokuwiki/inc/parser/handler.php (revision f50a239b3b819527445d240746b09a05fb76d103)
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
2026e22ab8SChristopher Smith    function __construct() {
2167f9913dSAndreas Gohr        $this->CallWriter = new Doku_Handler_CallWriter($this);
220cecf9d5Sandi    }
230cecf9d5Sandi
24276820f7SScrutinizer Auto-Fixer    /**
25276820f7SScrutinizer Auto-Fixer     * @param string $handler
26*f50a239bSTakamura     * @param mixed $args
27*f50a239bSTakamura     * @param integer|string $pos
28276820f7SScrutinizer Auto-Fixer     */
29433bef32Sandi    function _addCall($handler, $args, $pos) {
300cecf9d5Sandi        $call = array($handler,$args, $pos);
310cecf9d5Sandi        $this->CallWriter->writeCall($call);
320cecf9d5Sandi    }
330cecf9d5Sandi
3482d61635Spierre.spring    function addPluginCall($plugin, $args, $state, $pos, $match) {
3582d61635Spierre.spring        $call = array('plugin',array($plugin, $args, $state, $match), $pos);
3604ebd214Schris        $this->CallWriter->writeCall($call);
3704ebd214Schris    }
3804ebd214Schris
39433bef32Sandi    function _finalize(){
40e1c10e4dSchris
41f4f02a0fSchris        $this->CallWriter->finalise();
42f4f02a0fSchris
43e1c10e4dSchris        if ( $this->status['section'] ) {
44e1c10e4dSchris            $last_call = end($this->calls);
45e1c10e4dSchris            array_push($this->calls,array('section_close',array(), $last_call[2]));
460cecf9d5Sandi        }
470cecf9d5Sandi
48b7c441b9SHarry Fuecks        if ( $this->rewriteBlocks ) {
4967f9913dSAndreas Gohr            $B = new Doku_Handler_Block();
500cecf9d5Sandi            $this->calls = $B->process($this->calls);
51b7c441b9SHarry Fuecks        }
52e0ad864eSchris
5324bb549bSchris        trigger_event('PARSER_HANDLER_DONE',$this);
540cecf9d5Sandi
550cecf9d5Sandi        array_unshift($this->calls,array('document_start',array(),0));
560cecf9d5Sandi        $last_call = end($this->calls);
570cecf9d5Sandi        array_push($this->calls,array('document_end',array(),$last_call[2]));
580cecf9d5Sandi    }
590cecf9d5Sandi
600cecf9d5Sandi    function fetch() {
610cecf9d5Sandi        $call = each($this->calls);
620cecf9d5Sandi        if ( $call ) {
630cecf9d5Sandi            return $call['value'];
640cecf9d5Sandi        }
6544881bd0Shenning.noren        return false;
660cecf9d5Sandi    }
67ee20e7d1Sandi
68ee20e7d1Sandi
69ee20e7d1Sandi    /**
70ee20e7d1Sandi     * Special plugin handler
71ee20e7d1Sandi     *
72ee20e7d1Sandi     * This handler is called for all modes starting with 'plugin_'.
73ee20e7d1Sandi     * An additional parameter with the plugin name is passed
74ee20e7d1Sandi     *
75ee20e7d1Sandi     * @author Andreas Gohr <andi@splitbrain.org>
76*f50a239bSTakamura     *
77*f50a239bSTakamura     * @param string|integer $match
78*f50a239bSTakamura     * @param string|integer $state
79*f50a239bSTakamura     * @param integer $pos
80*f50a239bSTakamura     * @param $pluginname
81*f50a239bSTakamura     *
82*f50a239bSTakamura     * @return bool
83ee20e7d1Sandi     */
84ee20e7d1Sandi    function plugin($match, $state, $pos, $pluginname){
85ee20e7d1Sandi        $data = array($match);
8651a883edSGerrit Uitslag        /** @var DokuWiki_Syntax_Plugin $plugin */
8745d5ad75SGerrit Uitslag        $plugin = plugin_load('syntax',$pluginname);
88a46d0d65SAndreas Gohr        if($plugin != null){
89f02a7d06Schris            $data = $plugin->handle($match, $state, $pos, $this);
90ee20e7d1Sandi        }
9113ecfb18SChris Smith        if ($data !== false) {
9282d61635Spierre.spring            $this->addPluginCall($pluginname,$data,$state,$pos,$match);
9313ecfb18SChris Smith        }
9444881bd0Shenning.noren        return true;
95ee20e7d1Sandi    }
960cecf9d5Sandi
970cecf9d5Sandi    function base($match, $state, $pos) {
980cecf9d5Sandi        switch ( $state ) {
990cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
100433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
10144881bd0Shenning.noren                return true;
1020cecf9d5Sandi            break;
1030cecf9d5Sandi        }
1040cecf9d5Sandi    }
1050cecf9d5Sandi
1060cecf9d5Sandi    function header($match, $state, $pos) {
107d7e8115fSAndreas Gohr        // get level and title
108a4a2d4cfSAndreas Gohr        $title = trim($match);
109a4a2d4cfSAndreas Gohr        $level = 7 - strspn($title,'=');
110d7e8115fSAndreas Gohr        if($level < 1) $level = 1;
111a4a2d4cfSAndreas Gohr        $title = trim($title,'=');
112a4a2d4cfSAndreas Gohr        $title = trim($title);
1130cecf9d5Sandi
114e1c10e4dSchris        if ($this->status['section']) $this->_addCall('section_close',array(),$pos);
115e1c10e4dSchris
116433bef32Sandi        $this->_addCall('header',array($title,$level,$pos), $pos);
117e1c10e4dSchris
118e1c10e4dSchris        $this->_addCall('section_open',array($level),$pos);
11944881bd0Shenning.noren        $this->status['section'] = true;
12044881bd0Shenning.noren        return true;
1210cecf9d5Sandi    }
1220cecf9d5Sandi
1230cecf9d5Sandi    function notoc($match, $state, $pos) {
124e41c4da9SAndreas Gohr        $this->_addCall('notoc',array(),$pos);
12544881bd0Shenning.noren        return true;
1260cecf9d5Sandi    }
1270cecf9d5Sandi
1289dc2c2afSandi    function nocache($match, $state, $pos) {
1299dc2c2afSandi        $this->_addCall('nocache',array(),$pos);
13044881bd0Shenning.noren        return true;
1319dc2c2afSandi    }
1329dc2c2afSandi
1330cecf9d5Sandi    function linebreak($match, $state, $pos) {
134433bef32Sandi        $this->_addCall('linebreak',array(),$pos);
13544881bd0Shenning.noren        return true;
1360cecf9d5Sandi    }
1370cecf9d5Sandi
1380cecf9d5Sandi    function eol($match, $state, $pos) {
139433bef32Sandi        $this->_addCall('eol',array(),$pos);
14044881bd0Shenning.noren        return true;
1410cecf9d5Sandi    }
1420cecf9d5Sandi
1430cecf9d5Sandi    function hr($match, $state, $pos) {
144433bef32Sandi        $this->_addCall('hr',array(),$pos);
14544881bd0Shenning.noren        return true;
1460cecf9d5Sandi    }
1470cecf9d5Sandi
148276820f7SScrutinizer Auto-Fixer    /**
149*f50a239bSTakamura     * @param string|integer $match
150*f50a239bSTakamura     * @param string|integer $state
151*f50a239bSTakamura     * @param integer $pos
152276820f7SScrutinizer Auto-Fixer     * @param string $name
153276820f7SScrutinizer Auto-Fixer     */
154433bef32Sandi    function _nestingTag($match, $state, $pos, $name) {
1550cecf9d5Sandi        switch ( $state ) {
1560cecf9d5Sandi            case DOKU_LEXER_ENTER:
157433bef32Sandi                $this->_addCall($name.'_open', array(), $pos);
1580cecf9d5Sandi            break;
1590cecf9d5Sandi            case DOKU_LEXER_EXIT:
160433bef32Sandi                $this->_addCall($name.'_close', array(), $pos);
1610cecf9d5Sandi            break;
1620cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
163433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
1640cecf9d5Sandi            break;
1650cecf9d5Sandi        }
1660cecf9d5Sandi    }
1670cecf9d5Sandi
1680cecf9d5Sandi    function strong($match, $state, $pos) {
169433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'strong');
17044881bd0Shenning.noren        return true;
1710cecf9d5Sandi    }
1720cecf9d5Sandi
1730cecf9d5Sandi    function emphasis($match, $state, $pos) {
174433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'emphasis');
17544881bd0Shenning.noren        return true;
1760cecf9d5Sandi    }
1770cecf9d5Sandi
1780cecf9d5Sandi    function underline($match, $state, $pos) {
179433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'underline');
18044881bd0Shenning.noren        return true;
1810cecf9d5Sandi    }
1820cecf9d5Sandi
1830cecf9d5Sandi    function monospace($match, $state, $pos) {
184433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'monospace');
18544881bd0Shenning.noren        return true;
1860cecf9d5Sandi    }
1870cecf9d5Sandi
1880cecf9d5Sandi    function subscript($match, $state, $pos) {
189433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'subscript');
19044881bd0Shenning.noren        return true;
1910cecf9d5Sandi    }
1920cecf9d5Sandi
1930cecf9d5Sandi    function superscript($match, $state, $pos) {
194433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'superscript');
19544881bd0Shenning.noren        return true;
1960cecf9d5Sandi    }
1970cecf9d5Sandi
1980cecf9d5Sandi    function deleted($match, $state, $pos) {
199433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'deleted');
20044881bd0Shenning.noren        return true;
2010cecf9d5Sandi    }
2020cecf9d5Sandi
2030cecf9d5Sandi
2040cecf9d5Sandi    function footnote($match, $state, $pos) {
2055587e44cSchris//        $this->_nestingTag($match, $state, $pos, 'footnote');
206742c66f8Schris        if (!isset($this->_footnote)) $this->_footnote = false;
2072fe7363dSchris
2085587e44cSchris        switch ( $state ) {
2095587e44cSchris            case DOKU_LEXER_ENTER:
2102fe7363dSchris                // footnotes can not be nested - however due to limitations in lexer it can't be prevented
2112fe7363dSchris                // we will still enter a new footnote mode, we just do nothing
212742c66f8Schris                if ($this->_footnote) {
2132fe7363dSchris                    $this->_addCall('cdata',array($match), $pos);
2142fe7363dSchris                    break;
2152fe7363dSchris                }
2162fe7363dSchris
217742c66f8Schris                $this->_footnote = true;
2182fe7363dSchris
21967f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Nest($this->CallWriter,'footnote_close');
2205587e44cSchris                $this->CallWriter = & $ReWriter;
2214a26ad85Schris                $this->_addCall('footnote_open', array(), $pos);
2225587e44cSchris            break;
2235587e44cSchris            case DOKU_LEXER_EXIT:
2242fe7363dSchris                // check whether we have already exitted the footnote mode, can happen if the modes were nested
225742c66f8Schris                if (!$this->_footnote) {
2262fe7363dSchris                    $this->_addCall('cdata',array($match), $pos);
2272fe7363dSchris                    break;
2282fe7363dSchris                }
2292fe7363dSchris
230742c66f8Schris                $this->_footnote = false;
2312fe7363dSchris
2325587e44cSchris                $this->_addCall('footnote_close', array(), $pos);
2335587e44cSchris                $this->CallWriter->process();
2345587e44cSchris                $ReWriter = & $this->CallWriter;
2355587e44cSchris                $this->CallWriter = & $ReWriter->CallWriter;
2365587e44cSchris            break;
2375587e44cSchris            case DOKU_LEXER_UNMATCHED:
2385587e44cSchris                $this->_addCall('cdata', array($match), $pos);
2395587e44cSchris            break;
2405587e44cSchris        }
24144881bd0Shenning.noren        return true;
2420cecf9d5Sandi    }
2430cecf9d5Sandi
2440cecf9d5Sandi    function listblock($match, $state, $pos) {
2450cecf9d5Sandi        switch ( $state ) {
2460cecf9d5Sandi            case DOKU_LEXER_ENTER:
24767f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_List($this->CallWriter);
2480cecf9d5Sandi                $this->CallWriter = & $ReWriter;
249433bef32Sandi                $this->_addCall('list_open', array($match), $pos);
2500cecf9d5Sandi            break;
2510cecf9d5Sandi            case DOKU_LEXER_EXIT:
252433bef32Sandi                $this->_addCall('list_close', array(), $pos);
2530cecf9d5Sandi                $this->CallWriter->process();
2540cecf9d5Sandi                $ReWriter = & $this->CallWriter;
2550cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
2560cecf9d5Sandi            break;
2570cecf9d5Sandi            case DOKU_LEXER_MATCHED:
258433bef32Sandi                $this->_addCall('list_item', array($match), $pos);
2590cecf9d5Sandi            break;
2600cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
261433bef32Sandi                $this->_addCall('cdata', array($match), $pos);
2620cecf9d5Sandi            break;
2630cecf9d5Sandi        }
26444881bd0Shenning.noren        return true;
2650cecf9d5Sandi    }
2660cecf9d5Sandi
2670cecf9d5Sandi    function unformatted($match, $state, $pos) {
2680cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
269433bef32Sandi            $this->_addCall('unformatted',array($match), $pos);
2700cecf9d5Sandi        }
27144881bd0Shenning.noren        return true;
2720cecf9d5Sandi    }
2730cecf9d5Sandi
2740cecf9d5Sandi    function php($match, $state, $pos) {
275df9add72Schris        global $conf;
2760cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
277433bef32Sandi            $this->_addCall('php',array($match), $pos);
2780cecf9d5Sandi        }
27944881bd0Shenning.noren        return true;
2800cecf9d5Sandi    }
2810cecf9d5Sandi
28207f89c3cSAnika Henke    function phpblock($match, $state, $pos) {
28307f89c3cSAnika Henke        global $conf;
28407f89c3cSAnika Henke        if ( $state == DOKU_LEXER_UNMATCHED ) {
28507f89c3cSAnika Henke            $this->_addCall('phpblock',array($match), $pos);
28607f89c3cSAnika Henke        }
28707f89c3cSAnika Henke        return true;
28807f89c3cSAnika Henke    }
28907f89c3cSAnika Henke
2900cecf9d5Sandi    function html($match, $state, $pos) {
291df9add72Schris        global $conf;
2920cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
293433bef32Sandi            $this->_addCall('html',array($match), $pos);
2940cecf9d5Sandi        }
29544881bd0Shenning.noren        return true;
2960cecf9d5Sandi    }
2970cecf9d5Sandi
29807f89c3cSAnika Henke    function htmlblock($match, $state, $pos) {
29907f89c3cSAnika Henke        global $conf;
30007f89c3cSAnika Henke        if ( $state == DOKU_LEXER_UNMATCHED ) {
30107f89c3cSAnika Henke            $this->_addCall('htmlblock',array($match), $pos);
30207f89c3cSAnika Henke        }
30307f89c3cSAnika Henke        return true;
30407f89c3cSAnika Henke    }
30507f89c3cSAnika Henke
3060cecf9d5Sandi    function preformatted($match, $state, $pos) {
3070cecf9d5Sandi        switch ( $state ) {
3080cecf9d5Sandi            case DOKU_LEXER_ENTER:
30967f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Preformatted($this->CallWriter);
31026e22ab8SChristopher Smith                $this->CallWriter = $ReWriter;
311433bef32Sandi                $this->_addCall('preformatted_start',array(), $pos);
3120cecf9d5Sandi            break;
3130cecf9d5Sandi            case DOKU_LEXER_EXIT:
314433bef32Sandi                $this->_addCall('preformatted_end',array(), $pos);
3150cecf9d5Sandi                $this->CallWriter->process();
3160cecf9d5Sandi                $ReWriter = & $this->CallWriter;
3170cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
3180cecf9d5Sandi            break;
3190cecf9d5Sandi            case DOKU_LEXER_MATCHED:
320433bef32Sandi                $this->_addCall('preformatted_newline',array(), $pos);
3210cecf9d5Sandi            break;
3220cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
323433bef32Sandi                $this->_addCall('preformatted_content',array($match), $pos);
3240cecf9d5Sandi            break;
3250cecf9d5Sandi        }
3260cecf9d5Sandi
32744881bd0Shenning.noren        return true;
3280cecf9d5Sandi    }
3290cecf9d5Sandi
3300cecf9d5Sandi    function quote($match, $state, $pos) {
3310cecf9d5Sandi
3320cecf9d5Sandi        switch ( $state ) {
3330cecf9d5Sandi
3340cecf9d5Sandi            case DOKU_LEXER_ENTER:
33567f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Quote($this->CallWriter);
3360cecf9d5Sandi                $this->CallWriter = & $ReWriter;
337433bef32Sandi                $this->_addCall('quote_start',array($match), $pos);
3380cecf9d5Sandi            break;
3390cecf9d5Sandi
3400cecf9d5Sandi            case DOKU_LEXER_EXIT:
341433bef32Sandi                $this->_addCall('quote_end',array(), $pos);
3420cecf9d5Sandi                $this->CallWriter->process();
3430cecf9d5Sandi                $ReWriter = & $this->CallWriter;
3440cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
3450cecf9d5Sandi            break;
3460cecf9d5Sandi
3470cecf9d5Sandi            case DOKU_LEXER_MATCHED:
348433bef32Sandi                $this->_addCall('quote_newline',array($match), $pos);
3490cecf9d5Sandi            break;
3500cecf9d5Sandi
3510cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
352433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
3530cecf9d5Sandi            break;
3540cecf9d5Sandi
3550cecf9d5Sandi        }
3560cecf9d5Sandi
35744881bd0Shenning.noren        return true;
3580cecf9d5Sandi    }
3590cecf9d5Sandi
3603d491f75SAndreas Gohr    function file($match, $state, $pos) {
3613d491f75SAndreas Gohr        return $this->code($match, $state, $pos, 'file');
3623d491f75SAndreas Gohr    }
3633d491f75SAndreas Gohr
3643d491f75SAndreas Gohr    function code($match, $state, $pos, $type='code') {
3653d491f75SAndreas Gohr        if ( $state == DOKU_LEXER_UNMATCHED ) {
3664b7f9e70STom N Harris            $matches = explode('>',$match,2);
3673d491f75SAndreas Gohr
3680139312bSAdrian Lang            $param = preg_split('/\s+/', $matches[0], 2, PREG_SPLIT_NO_EMPTY);
3690139312bSAdrian Lang            while(count($param) < 2) array_push($param, null);
3700139312bSAdrian Lang
3710139312bSAdrian Lang            // We shortcut html here.
3720139312bSAdrian Lang            if ($param[0] == 'html') $param[0] = 'html4strict';
3730139312bSAdrian Lang            if ($param[0] == '-') $param[0] = null;
3740139312bSAdrian Lang            array_unshift($param, $matches[1]);
3750139312bSAdrian Lang
3760139312bSAdrian Lang            $this->_addCall($type, $param, $pos);
3770cecf9d5Sandi        }
37844881bd0Shenning.noren        return true;
3790cecf9d5Sandi    }
3800cecf9d5Sandi
3810cecf9d5Sandi    function acronym($match, $state, $pos) {
382433bef32Sandi        $this->_addCall('acronym',array($match), $pos);
38344881bd0Shenning.noren        return true;
3840cecf9d5Sandi    }
3850cecf9d5Sandi
3860cecf9d5Sandi    function smiley($match, $state, $pos) {
387433bef32Sandi        $this->_addCall('smiley',array($match), $pos);
38844881bd0Shenning.noren        return true;
3890cecf9d5Sandi    }
3900cecf9d5Sandi
3910cecf9d5Sandi    function wordblock($match, $state, $pos) {
392433bef32Sandi        $this->_addCall('wordblock',array($match), $pos);
39344881bd0Shenning.noren        return true;
3940cecf9d5Sandi    }
3950cecf9d5Sandi
3960cecf9d5Sandi    function entity($match, $state, $pos) {
397433bef32Sandi        $this->_addCall('entity',array($match), $pos);
39844881bd0Shenning.noren        return true;
3990cecf9d5Sandi    }
4000cecf9d5Sandi
4010cecf9d5Sandi    function multiplyentity($match, $state, $pos) {
4020cecf9d5Sandi        preg_match_all('/\d+/',$match,$matches);
403433bef32Sandi        $this->_addCall('multiplyentity',array($matches[0][0],$matches[0][1]), $pos);
40444881bd0Shenning.noren        return true;
4050cecf9d5Sandi    }
4060cecf9d5Sandi
4070cecf9d5Sandi    function singlequoteopening($match, $state, $pos) {
408433bef32Sandi        $this->_addCall('singlequoteopening',array(), $pos);
40944881bd0Shenning.noren        return true;
4100cecf9d5Sandi    }
4110cecf9d5Sandi
4120cecf9d5Sandi    function singlequoteclosing($match, $state, $pos) {
413433bef32Sandi        $this->_addCall('singlequoteclosing',array(), $pos);
41444881bd0Shenning.noren        return true;
4150cecf9d5Sandi    }
4160cecf9d5Sandi
41757d757d1SAndreas Gohr    function apostrophe($match, $state, $pos) {
41857d757d1SAndreas Gohr        $this->_addCall('apostrophe',array(), $pos);
41957d757d1SAndreas Gohr        return true;
42057d757d1SAndreas Gohr    }
42157d757d1SAndreas Gohr
4220cecf9d5Sandi    function doublequoteopening($match, $state, $pos) {
423433bef32Sandi        $this->_addCall('doublequoteopening',array(), $pos);
424e950d12fSChristopher Smith        $this->status['doublequote']++;
42544881bd0Shenning.noren        return true;
4260cecf9d5Sandi    }
4270cecf9d5Sandi
4280cecf9d5Sandi    function doublequoteclosing($match, $state, $pos) {
429e950d12fSChristopher Smith        if ($this->status['doublequote'] <= 0) {
430e950d12fSChristopher Smith            $this->doublequoteopening($match, $state, $pos);
431e950d12fSChristopher Smith        } else {
432433bef32Sandi            $this->_addCall('doublequoteclosing',array(), $pos);
433e950d12fSChristopher Smith            $this->status['doublequote'] = max(0, --$this->status['doublequote']);
434e950d12fSChristopher Smith        }
43544881bd0Shenning.noren        return true;
4360cecf9d5Sandi    }
4370cecf9d5Sandi
4380cecf9d5Sandi    function camelcaselink($match, $state, $pos) {
439433bef32Sandi        $this->_addCall('camelcaselink',array($match), $pos);
44044881bd0Shenning.noren        return true;
4410cecf9d5Sandi    }
4420cecf9d5Sandi
4430cecf9d5Sandi    /*
4440cecf9d5Sandi    */
4450cecf9d5Sandi    function internallink($match, $state, $pos) {
4460cecf9d5Sandi        // Strip the opening and closing markup
4470cecf9d5Sandi        $link = preg_replace(array('/^\[\[/','/\]\]$/u'),'',$match);
4480cecf9d5Sandi
4490cecf9d5Sandi        // Split title from URL
4504b7f9e70STom N Harris        $link = explode('|',$link,2);
4510cecf9d5Sandi        if ( !isset($link[1]) ) {
4520ea51e63SMatt Perry            $link[1] = null;
4530cecf9d5Sandi        } else if ( preg_match('/^\{\{[^\}]+\}\}$/',$link[1]) ) {
4545578eb8fSandi            // If the title is an image, convert it to an array containing the image details
455b625487dSandi            $link[1] = Doku_Handler_Parse_Media($link[1]);
4560cecf9d5Sandi        }
4570b7c14c2Sandi        $link[0] = trim($link[0]);
4580cecf9d5Sandi
4590e1c636eSandi        //decide which kind of link it is
4600e1c636eSandi
4616efc45a2SDmitry Katsubo        if ( link_isinterwiki($link[0]) ) {
4620e1c636eSandi            // Interwiki
4634b7f9e70STom N Harris            $interwiki = explode('>',$link[0],2);
464433bef32Sandi            $this->_addCall(
4650cecf9d5Sandi                'interwikilink',
4660cecf9d5Sandi                array($link[0],$link[1],strtolower($interwiki[0]),$interwiki[1]),
4670cecf9d5Sandi                $pos
4680cecf9d5Sandi                );
46915f1b77cSAndreas Gohr        }elseif ( preg_match('/^\\\\\\\\[^\\\\]+?\\\\/u',$link[0]) ) {
4700e1c636eSandi            // Windows Share
471433bef32Sandi            $this->_addCall(
4720cecf9d5Sandi                'windowssharelink',
4730cecf9d5Sandi                array($link[0],$link[1]),
4740cecf9d5Sandi                $pos
4750cecf9d5Sandi                );
4764468cb4cSAndreas Gohr        }elseif ( preg_match('#^([a-z0-9\-\.+]+?)://#i',$link[0]) ) {
4770e1c636eSandi            // external link (accepts all protocols)
478433bef32Sandi            $this->_addCall(
4790cecf9d5Sandi                    'externallink',
4800cecf9d5Sandi                    array($link[0],$link[1]),
4810cecf9d5Sandi                    $pos
4820cecf9d5Sandi                    );
4830a1d30bfSchris        }elseif ( preg_match('<'.PREG_PATTERN_VALID_EMAIL.'>',$link[0]) ) {
4840a1d30bfSchris            // E-Mail (pattern above is defined in inc/mail.php)
485a6755281Sandi            $this->_addCall(
486a6755281Sandi                'emaillink',
487a6755281Sandi                array($link[0],$link[1]),
488a6755281Sandi                $pos
489a6755281Sandi                );
4900b7c14c2Sandi        }elseif ( preg_match('!^#.+!',$link[0]) ){
4910b7c14c2Sandi            // local link
4920b7c14c2Sandi            $this->_addCall(
4930b7c14c2Sandi                'locallink',
4940b7c14c2Sandi                array(substr($link[0],1),$link[1]),
4950b7c14c2Sandi                $pos
4960b7c14c2Sandi                );
4970e1c636eSandi        }else{
4980e1c636eSandi            // internal link
499433bef32Sandi            $this->_addCall(
5000e1c636eSandi                'internallink',
5010e1c636eSandi                array($link[0],$link[1]),
5020e1c636eSandi                $pos
5030e1c636eSandi                );
5040cecf9d5Sandi        }
5050e1c636eSandi
50644881bd0Shenning.noren        return true;
5070cecf9d5Sandi    }
5080cecf9d5Sandi
5090cecf9d5Sandi    function filelink($match, $state, $pos) {
5100ea51e63SMatt Perry        $this->_addCall('filelink',array($match, null), $pos);
51144881bd0Shenning.noren        return true;
5120cecf9d5Sandi    }
5130cecf9d5Sandi
5140cecf9d5Sandi    function windowssharelink($match, $state, $pos) {
5150ea51e63SMatt Perry        $this->_addCall('windowssharelink',array($match, null), $pos);
51644881bd0Shenning.noren        return true;
5170cecf9d5Sandi    }
5180cecf9d5Sandi
5190cecf9d5Sandi    function media($match, $state, $pos) {
5200cecf9d5Sandi        $p = Doku_Handler_Parse_Media($match);
5210cecf9d5Sandi
522433bef32Sandi        $this->_addCall(
5230cecf9d5Sandi              $p['type'],
524dc673a5bSjoe.lapp              array($p['src'], $p['title'], $p['align'], $p['width'],
525dc673a5bSjoe.lapp                     $p['height'], $p['cache'], $p['linking']),
5260cecf9d5Sandi              $pos
5270cecf9d5Sandi             );
52844881bd0Shenning.noren        return true;
5290cecf9d5Sandi    }
5300cecf9d5Sandi
531b625487dSandi    function rss($match, $state, $pos) {
532b625487dSandi        $link = preg_replace(array('/^\{\{rss>/','/\}\}$/'),'',$match);
5333db95becSAndreas Gohr
5343db95becSAndreas Gohr        // get params
5353db95becSAndreas Gohr        list($link,$params) = explode(' ',$link,2);
5363db95becSAndreas Gohr
5373db95becSAndreas Gohr        $p = array();
5383db95becSAndreas Gohr        if(preg_match('/\b(\d+)\b/',$params,$match)){
5393db95becSAndreas Gohr            $p['max'] = $match[1];
5403db95becSAndreas Gohr        }else{
5413db95becSAndreas Gohr            $p['max'] = 8;
5423db95becSAndreas Gohr        }
5433db95becSAndreas Gohr        $p['reverse'] = (preg_match('/rev/',$params));
5443db95becSAndreas Gohr        $p['author']  = (preg_match('/\b(by|author)/',$params));
5453db95becSAndreas Gohr        $p['date']    = (preg_match('/\b(date)/',$params));
5463db95becSAndreas Gohr        $p['details'] = (preg_match('/\b(desc|detail)/',$params));
54738c6f603SRobin H. Johnson        $p['nosort']  = (preg_match('/\b(nosort)\b/',$params));
5483db95becSAndreas Gohr
5490a69dff7Schris        if (preg_match('/\b(\d+)([dhm])\b/',$params,$match)) {
5500a69dff7Schris            $period = array('d' => 86400, 'h' => 3600, 'm' => 60);
5510a69dff7Schris            $p['refresh'] = max(600,$match[1]*$period[$match[2]]);  // n * period in seconds, minimum 10 minutes
5520a69dff7Schris        } else {
5530a69dff7Schris            $p['refresh'] = 14400;   // default to 4 hours
5540a69dff7Schris        }
5550a69dff7Schris
5563db95becSAndreas Gohr        $this->_addCall('rss',array($link,$p),$pos);
55744881bd0Shenning.noren        return true;
558b625487dSandi    }
559b625487dSandi
5600cecf9d5Sandi    function externallink($match, $state, $pos) {
561da9f31c5SAndreas Gohr        $url   = $match;
562da9f31c5SAndreas Gohr        $title = null;
5630cecf9d5Sandi
564da9f31c5SAndreas Gohr        // add protocol on simple short URLs
565da9f31c5SAndreas Gohr        if(substr($url,0,3) == 'ftp' && (substr($url,0,6) != 'ftp://')){
566da9f31c5SAndreas Gohr            $title = $url;
567da9f31c5SAndreas Gohr            $url   = 'ftp://'.$url;
568da9f31c5SAndreas Gohr        }
569da9f31c5SAndreas Gohr        if(substr($url,0,3) == 'www' && (substr($url,0,7) != 'http://')){
570da9f31c5SAndreas Gohr            $title = $url;
571da9f31c5SAndreas Gohr            $url = 'http://'.$url;
572da9f31c5SAndreas Gohr        }
573da9f31c5SAndreas Gohr
574da9f31c5SAndreas Gohr        $this->_addCall('externallink',array($url, $title), $pos);
57544881bd0Shenning.noren        return true;
5760cecf9d5Sandi    }
5770cecf9d5Sandi
57871352defSandi    function emaillink($match, $state, $pos) {
5790cecf9d5Sandi        $email = preg_replace(array('/^</','/>$/'),'',$match);
5800ea51e63SMatt Perry        $this->_addCall('emaillink',array($email, null), $pos);
58144881bd0Shenning.noren        return true;
5820cecf9d5Sandi    }
5830cecf9d5Sandi
5840cecf9d5Sandi    function table($match, $state, $pos) {
5850cecf9d5Sandi        switch ( $state ) {
5860cecf9d5Sandi
5870cecf9d5Sandi            case DOKU_LEXER_ENTER:
5880cecf9d5Sandi
58967f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Table($this->CallWriter);
5900cecf9d5Sandi                $this->CallWriter = & $ReWriter;
5910cecf9d5Sandi
59290df9a4dSAdrian Lang                $this->_addCall('table_start', array($pos + 1), $pos);
5930cecf9d5Sandi                if ( trim($match) == '^' ) {
594433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
5950cecf9d5Sandi                } else {
596433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
5970cecf9d5Sandi                }
5980cecf9d5Sandi            break;
5990cecf9d5Sandi
6000cecf9d5Sandi            case DOKU_LEXER_EXIT:
60190df9a4dSAdrian Lang                $this->_addCall('table_end', array($pos), $pos);
6020cecf9d5Sandi                $this->CallWriter->process();
6030cecf9d5Sandi                $ReWriter = & $this->CallWriter;
6040cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
6050cecf9d5Sandi            break;
6060cecf9d5Sandi
6070cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
6080cecf9d5Sandi                if ( trim($match) != '' ) {
609433bef32Sandi                    $this->_addCall('cdata',array($match), $pos);
6100cecf9d5Sandi                }
6110cecf9d5Sandi            break;
6120cecf9d5Sandi
6130cecf9d5Sandi            case DOKU_LEXER_MATCHED:
6149ab75d9eSAndreas Gohr                if ( $match == ' ' ){
6159ab75d9eSAndreas Gohr                    $this->_addCall('cdata', array($match), $pos);
61625b97867Shakan.sandell                } else if ( preg_match('/:::/',$match) ) {
61725b97867Shakan.sandell                    $this->_addCall('rowspan', array($match), $pos);
618e205b721SAndreas Gohr                } else if ( preg_match('/\t+/',$match) ) {
6199ab75d9eSAndreas Gohr                    $this->_addCall('table_align', array($match), $pos);
620e205b721SAndreas Gohr                } else if ( preg_match('/ {2,}/',$match) ) {
621433bef32Sandi                    $this->_addCall('table_align', array($match), $pos);
6220cecf9d5Sandi                } else if ( $match == "\n|" ) {
623433bef32Sandi                    $this->_addCall('table_row', array(), $pos);
624433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
6250cecf9d5Sandi                } else if ( $match == "\n^" ) {
626433bef32Sandi                    $this->_addCall('table_row', array(), $pos);
627433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
6280cecf9d5Sandi                } else if ( $match == '|' ) {
629433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
6300cecf9d5Sandi                } else if ( $match == '^' ) {
631433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
6320cecf9d5Sandi                }
6330cecf9d5Sandi            break;
6340cecf9d5Sandi        }
63544881bd0Shenning.noren        return true;
6360cecf9d5Sandi    }
6370cecf9d5Sandi}
6380cecf9d5Sandi
6390cecf9d5Sandi//------------------------------------------------------------------------
6400cecf9d5Sandifunction Doku_Handler_Parse_Media($match) {
6410cecf9d5Sandi
6420cecf9d5Sandi    // Strip the opening and closing markup
6430cecf9d5Sandi    $link = preg_replace(array('/^\{\{/','/\}\}$/u'),'',$match);
6440cecf9d5Sandi
6450cecf9d5Sandi    // Split title from URL
6464b7f9e70STom N Harris    $link = explode('|',$link,2);
6470cecf9d5Sandi
6480cecf9d5Sandi    // Check alignment
6490cecf9d5Sandi    $ralign = (bool)preg_match('/^ /',$link[0]);
6500cecf9d5Sandi    $lalign = (bool)preg_match('/ $/',$link[0]);
6510cecf9d5Sandi
6520cecf9d5Sandi    // Logic = what's that ;)...
6530cecf9d5Sandi    if ( $lalign & $ralign ) {
6540cecf9d5Sandi        $align = 'center';
6550cecf9d5Sandi    } else if ( $ralign ) {
6560cecf9d5Sandi        $align = 'right';
6570cecf9d5Sandi    } else if ( $lalign ) {
6580cecf9d5Sandi        $align = 'left';
6590cecf9d5Sandi    } else {
6600ea51e63SMatt Perry        $align = null;
6610cecf9d5Sandi    }
6620cecf9d5Sandi
6630cecf9d5Sandi    // The title...
6640cecf9d5Sandi    if ( !isset($link[1]) ) {
6650ea51e63SMatt Perry        $link[1] = null;
6660cecf9d5Sandi    }
6670cecf9d5Sandi
6684826ab45Sandi    //remove aligning spaces
6694826ab45Sandi    $link[0] = trim($link[0]);
6700cecf9d5Sandi
6714826ab45Sandi    //split into src and parameters (using the very last questionmark)
6724826ab45Sandi    $pos = strrpos($link[0], '?');
6734826ab45Sandi    if($pos !== false){
6744826ab45Sandi        $src   = substr($link[0],0,$pos);
6754826ab45Sandi        $param = substr($link[0],$pos+1);
6760cecf9d5Sandi    }else{
6774826ab45Sandi        $src   = $link[0];
6784826ab45Sandi        $param = '';
6790cecf9d5Sandi    }
6800cecf9d5Sandi
6814826ab45Sandi    //parse width and height
6824826ab45Sandi    if(preg_match('#(\d+)(x(\d+))?#i',$param,$size)){
683443e135dSChristopher Smith        !empty($size[1]) ? $w = $size[1] : $w = null;
684443e135dSChristopher Smith        !empty($size[3]) ? $h = $size[3] : $h = null;
685fc1c55b1Shfuecks    } else {
6860ea51e63SMatt Perry        $w = null;
6870ea51e63SMatt Perry        $h = null;
6880cecf9d5Sandi    }
6890cecf9d5Sandi
690dc673a5bSjoe.lapp    //get linking command
691d35ab615Shenning.noren    if(preg_match('/nolink/i',$param)){
692dc673a5bSjoe.lapp        $linking = 'nolink';
693d35ab615Shenning.noren    }else if(preg_match('/direct/i',$param)){
694dc673a5bSjoe.lapp        $linking = 'direct';
6958acb3108SAndreas Gohr    }else if(preg_match('/linkonly/i',$param)){
6968acb3108SAndreas Gohr        $linking = 'linkonly';
697dc673a5bSjoe.lapp    }else{
698dc673a5bSjoe.lapp        $linking = 'details';
699dc673a5bSjoe.lapp    }
700dc673a5bSjoe.lapp
7014826ab45Sandi    //get caching command
7024826ab45Sandi    if (preg_match('/(nocache|recache)/i',$param,$cachemode)){
7034826ab45Sandi        $cache = $cachemode[1];
7040cecf9d5Sandi    }else{
7054826ab45Sandi        $cache = 'cache';
7060cecf9d5Sandi    }
7070cecf9d5Sandi
7086efc45a2SDmitry Katsubo    // Check whether this is a local or remote image or interwiki
7096efc45a2SDmitry Katsubo    if (media_isexternal($src) || link_isinterwiki($src)){
7104826ab45Sandi        $call = 'externalmedia';
7110cecf9d5Sandi    } else {
7124826ab45Sandi        $call = 'internalmedia';
7130cecf9d5Sandi    }
7140cecf9d5Sandi
7150cecf9d5Sandi    $params = array(
7160cecf9d5Sandi        'type'=>$call,
7174826ab45Sandi        'src'=>$src,
7180cecf9d5Sandi        'title'=>$link[1],
7190cecf9d5Sandi        'align'=>$align,
7204826ab45Sandi        'width'=>$w,
7214826ab45Sandi        'height'=>$h,
7220cecf9d5Sandi        'cache'=>$cache,
723dc673a5bSjoe.lapp        'linking'=>$linking,
7240cecf9d5Sandi    );
7250cecf9d5Sandi
7260cecf9d5Sandi    return $params;
7270cecf9d5Sandi}
7280cecf9d5Sandi
7290cecf9d5Sandi//------------------------------------------------------------------------
73026e22ab8SChristopher Smithinterface Doku_Handler_CallWriter_Interface {
73126e22ab8SChristopher Smith    public function writeCall($call);
73226e22ab8SChristopher Smith    public function writeCalls($calls);
73326e22ab8SChristopher Smith    public function finalise();
73426e22ab8SChristopher Smith}
73526e22ab8SChristopher Smith
73626e22ab8SChristopher Smithclass Doku_Handler_CallWriter implements Doku_Handler_CallWriter_Interface {
7370cecf9d5Sandi
7380cecf9d5Sandi    var $Handler;
7390cecf9d5Sandi
74042ea7f44SGerrit Uitslag    /**
74142ea7f44SGerrit Uitslag     * @param Doku_Handler $Handler
74242ea7f44SGerrit Uitslag     */
74326e22ab8SChristopher Smith    function __construct(Doku_Handler $Handler) {
74426e22ab8SChristopher Smith        $this->Handler = $Handler;
7450cecf9d5Sandi    }
7460cecf9d5Sandi
7470cecf9d5Sandi    function writeCall($call) {
7480cecf9d5Sandi        $this->Handler->calls[] = $call;
7490cecf9d5Sandi    }
7500cecf9d5Sandi
7510cecf9d5Sandi    function writeCalls($calls) {
7520cecf9d5Sandi        $this->Handler->calls = array_merge($this->Handler->calls, $calls);
7530cecf9d5Sandi    }
754f4f02a0fSchris
755f4f02a0fSchris    // function is required, but since this call writer is first/highest in
756f4f02a0fSchris    // the chain it is not required to do anything
757f4f02a0fSchris    function finalise() {
7583893df8eSChristopher Smith        unset($this->Handler);
759f4f02a0fSchris    }
7600cecf9d5Sandi}
7610cecf9d5Sandi
7620cecf9d5Sandi//------------------------------------------------------------------------
7635587e44cSchris/**
7645587e44cSchris * Generic call writer class to handle nesting of rendering instructions
7655587e44cSchris * within a render instruction. Also see nest() method of renderer base class
7665587e44cSchris *
7675587e44cSchris * @author    Chris Smith <chris@jalakai.co.uk>
7685587e44cSchris */
76926e22ab8SChristopher Smithclass Doku_Handler_Nest implements Doku_Handler_CallWriter_Interface {
7705587e44cSchris
7715587e44cSchris    var $CallWriter;
7725587e44cSchris    var $calls = array();
7735587e44cSchris
7745587e44cSchris    var $closingInstruction;
7755587e44cSchris
7765587e44cSchris    /**
7775587e44cSchris     * constructor
7785587e44cSchris     *
779*f50a239bSTakamura     * @param Doku_Handler_CallWriter|Doku_Handler_CallWriter_Interface $CallWriter the renderers current call writer
7805587e44cSchris     * @param  string $close closing instruction name, this is required to properly terminate the
7815587e44cSchris     *                                    syntax mode if the document ends without a closing pattern
7825587e44cSchris     */
78326e22ab8SChristopher Smith    function __construct(Doku_Handler_CallWriter_Interface $CallWriter, $close="nest_close") {
78426e22ab8SChristopher Smith        $this->CallWriter = $CallWriter;
7855587e44cSchris
7865587e44cSchris        $this->closingInstruction = $close;
7875587e44cSchris    }
7885587e44cSchris
7895587e44cSchris    function writeCall($call) {
7905587e44cSchris        $this->calls[] = $call;
7915587e44cSchris    }
7925587e44cSchris
7935587e44cSchris    function writeCalls($calls) {
7945587e44cSchris        $this->calls = array_merge($this->calls, $calls);
7955587e44cSchris    }
7965587e44cSchris
7975587e44cSchris    function finalise() {
7985587e44cSchris        $last_call = end($this->calls);
7995587e44cSchris        $this->writeCall(array($this->closingInstruction,array(), $last_call[2]));
8005587e44cSchris
8015587e44cSchris        $this->process();
8025587e44cSchris        $this->CallWriter->finalise();
8033893df8eSChristopher Smith        unset($this->CallWriter);
8045587e44cSchris    }
8055587e44cSchris
8065587e44cSchris    function process() {
80741624b31SChris Smith        // merge consecutive cdata
80841624b31SChris Smith        $unmerged_calls = $this->calls;
80941624b31SChris Smith        $this->calls = array();
81041624b31SChris Smith
81141624b31SChris Smith        foreach ($unmerged_calls as $call) $this->addCall($call);
81241624b31SChris Smith
8135587e44cSchris        $first_call = reset($this->calls);
8145587e44cSchris        $this->CallWriter->writeCall(array("nest", array($this->calls), $first_call[2]));
8155587e44cSchris    }
81641624b31SChris Smith
81741624b31SChris Smith    function addCall($call) {
81841624b31SChris Smith        $key = count($this->calls);
81941624b31SChris Smith        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
82041624b31SChris Smith            $this->calls[$key-1][1][0] .= $call[1][0];
82173c47f3dSChris Smith        } else if ($call[0] == 'eol') {
82273c47f3dSChris Smith            // do nothing (eol shouldn't be allowed, to counter preformatted fix in #1652 & #1699)
82341624b31SChris Smith        } else {
82441624b31SChris Smith            $this->calls[] = $call;
82541624b31SChris Smith        }
82641624b31SChris Smith    }
8275587e44cSchris}
8285587e44cSchris
82926e22ab8SChristopher Smithclass Doku_Handler_List implements Doku_Handler_CallWriter_Interface {
8300cecf9d5Sandi
8310cecf9d5Sandi    var $CallWriter;
8320cecf9d5Sandi
8330cecf9d5Sandi    var $calls = array();
8340cecf9d5Sandi    var $listCalls = array();
8350cecf9d5Sandi    var $listStack = array();
8360cecf9d5Sandi
83710bcc8aaSChristopher Smith    const NODE = 1;
83810bcc8aaSChristopher Smith
83926e22ab8SChristopher Smith    function __construct(Doku_Handler_CallWriter_Interface $CallWriter) {
84026e22ab8SChristopher Smith        $this->CallWriter = $CallWriter;
8410cecf9d5Sandi    }
8420cecf9d5Sandi
8430cecf9d5Sandi    function writeCall($call) {
8440cecf9d5Sandi        $this->calls[] = $call;
8450cecf9d5Sandi    }
8460cecf9d5Sandi
8470cecf9d5Sandi    // Probably not needed but just in case...
8480cecf9d5Sandi    function writeCalls($calls) {
8490cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
850f4f02a0fSchris#        $this->CallWriter->writeCalls($this->calls);
851f4f02a0fSchris    }
852f4f02a0fSchris
853f4f02a0fSchris    function finalise() {
854f4f02a0fSchris        $last_call = end($this->calls);
855f4f02a0fSchris        $this->writeCall(array('list_close',array(), $last_call[2]));
856f4f02a0fSchris
857f4f02a0fSchris        $this->process();
858f4f02a0fSchris        $this->CallWriter->finalise();
8593893df8eSChristopher Smith        unset($this->CallWriter);
8600cecf9d5Sandi    }
8610cecf9d5Sandi
8620cecf9d5Sandi    //------------------------------------------------------------------------
8630cecf9d5Sandi    function process() {
864f4f02a0fSchris
8650cecf9d5Sandi        foreach ( $this->calls as $call ) {
8660cecf9d5Sandi            switch ($call[0]) {
8670cecf9d5Sandi                case 'list_item':
8680cecf9d5Sandi                    $this->listOpen($call);
8690cecf9d5Sandi                break;
8700cecf9d5Sandi                case 'list_open':
8710cecf9d5Sandi                    $this->listStart($call);
8720cecf9d5Sandi                break;
8730cecf9d5Sandi                case 'list_close':
8740cecf9d5Sandi                    $this->listEnd($call);
8750cecf9d5Sandi                break;
8760cecf9d5Sandi                default:
8770cecf9d5Sandi                    $this->listContent($call);
8780cecf9d5Sandi                break;
8790cecf9d5Sandi            }
8800cecf9d5Sandi        }
8810cecf9d5Sandi
8820cecf9d5Sandi        $this->CallWriter->writeCalls($this->listCalls);
8830cecf9d5Sandi    }
8840cecf9d5Sandi
8850cecf9d5Sandi    //------------------------------------------------------------------------
8860cecf9d5Sandi    function listStart($call) {
8870cecf9d5Sandi        $depth = $this->interpretSyntax($call[1][0], $listType);
8880cecf9d5Sandi
8890cecf9d5Sandi        $this->initialDepth = $depth;
89010bcc8aaSChristopher Smith        //                   array(list type, current depth, index of current listitem_open)
89110bcc8aaSChristopher Smith        $this->listStack[] = array($listType, $depth, 1);
8920cecf9d5Sandi
8930cecf9d5Sandi        $this->listCalls[] = array('list'.$listType.'_open',array(),$call[2]);
8940cecf9d5Sandi        $this->listCalls[] = array('listitem_open',array(1),$call[2]);
8950cecf9d5Sandi        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
8960cecf9d5Sandi    }
8970cecf9d5Sandi
8980cecf9d5Sandi    //------------------------------------------------------------------------
8990cecf9d5Sandi    function listEnd($call) {
90044881bd0Shenning.noren        $closeContent = true;
9010cecf9d5Sandi
9020cecf9d5Sandi        while ( $list = array_pop($this->listStack) ) {
9030cecf9d5Sandi            if ( $closeContent ) {
9040cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
90544881bd0Shenning.noren                $closeContent = false;
9060cecf9d5Sandi            }
9070cecf9d5Sandi            $this->listCalls[] = array('listitem_close',array(),$call[2]);
9080cecf9d5Sandi            $this->listCalls[] = array('list'.$list[0].'_close', array(), $call[2]);
9090cecf9d5Sandi        }
9100cecf9d5Sandi    }
9110cecf9d5Sandi
9120cecf9d5Sandi    //------------------------------------------------------------------------
9130cecf9d5Sandi    function listOpen($call) {
9140cecf9d5Sandi        $depth = $this->interpretSyntax($call[1][0], $listType);
9150cecf9d5Sandi        $end = end($this->listStack);
91610bcc8aaSChristopher Smith        $key = key($this->listStack);
9170cecf9d5Sandi
9180cecf9d5Sandi        // Not allowed to be shallower than initialDepth
9190cecf9d5Sandi        if ( $depth < $this->initialDepth ) {
9200cecf9d5Sandi            $depth = $this->initialDepth;
9210cecf9d5Sandi        }
9220cecf9d5Sandi
9230cecf9d5Sandi        //------------------------------------------------------------------------
9240cecf9d5Sandi        if ( $depth == $end[1] ) {
9250cecf9d5Sandi
9260cecf9d5Sandi            // Just another item in the list...
9270cecf9d5Sandi            if ( $listType == $end[0] ) {
9280cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9290cecf9d5Sandi                $this->listCalls[] = array('listitem_close',array(),$call[2]);
9300cecf9d5Sandi                $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
9310cecf9d5Sandi                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9320cecf9d5Sandi
93310bcc8aaSChristopher Smith                // new list item, update list stack's index into current listitem_open
93410bcc8aaSChristopher Smith                $this->listStack[$key][2] = count($this->listCalls) - 2;
93510bcc8aaSChristopher Smith
9360cecf9d5Sandi            // Switched list type...
9370cecf9d5Sandi            } else {
9380cecf9d5Sandi
9390cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9400cecf9d5Sandi                $this->listCalls[] = array('listitem_close',array(),$call[2]);
9410cecf9d5Sandi                $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
9420cecf9d5Sandi                $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9430cecf9d5Sandi                $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9440cecf9d5Sandi                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9450cecf9d5Sandi
9460cecf9d5Sandi                array_pop($this->listStack);
94710bcc8aaSChristopher Smith                $this->listStack[] = array($listType, $depth, count($this->listCalls) - 2);
9480cecf9d5Sandi            }
9490cecf9d5Sandi
9500cecf9d5Sandi        //------------------------------------------------------------------------
9510cecf9d5Sandi        // Getting deeper...
9520cecf9d5Sandi        } else if ( $depth > $end[1] ) {
9530cecf9d5Sandi
9540cecf9d5Sandi            $this->listCalls[] = array('listcontent_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
95910bcc8aaSChristopher Smith            // set the node/leaf state of this item's parent listitem_open to NODE
96010bcc8aaSChristopher Smith            $this->listCalls[$this->listStack[$key][2]][1][1] = self::NODE;
96110bcc8aaSChristopher Smith
96210bcc8aaSChristopher Smith            $this->listStack[] = array($listType, $depth, count($this->listCalls) - 2);
9630cecf9d5Sandi
9640cecf9d5Sandi        //------------------------------------------------------------------------
9650cecf9d5Sandi        // Getting shallower ( $depth < $end[1] )
9660cecf9d5Sandi        } else {
9670cecf9d5Sandi            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9680cecf9d5Sandi            $this->listCalls[] = array('listitem_close',array(),$call[2]);
9690cecf9d5Sandi            $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
9700cecf9d5Sandi
9710cecf9d5Sandi            // Throw away the end - done
9720cecf9d5Sandi            array_pop($this->listStack);
9730cecf9d5Sandi
9740cecf9d5Sandi            while (1) {
9750cecf9d5Sandi                $end = end($this->listStack);
97610bcc8aaSChristopher Smith                $key = key($this->listStack);
9770cecf9d5Sandi
9780cecf9d5Sandi                if ( $end[1] <= $depth ) {
9790cecf9d5Sandi
9800cecf9d5Sandi                    // Normalize depths
9810cecf9d5Sandi                    $depth = $end[1];
9820cecf9d5Sandi
9830cecf9d5Sandi                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
9840cecf9d5Sandi
9850cecf9d5Sandi                    if ( $end[0] == $listType ) {
9860cecf9d5Sandi                        $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
9870cecf9d5Sandi                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9880cecf9d5Sandi
98910bcc8aaSChristopher Smith                        // new list item, update list stack's index into current listitem_open
99010bcc8aaSChristopher Smith                        $this->listStack[$key][2] = count($this->listCalls) - 2;
99110bcc8aaSChristopher Smith
9920cecf9d5Sandi                    } else {
9930cecf9d5Sandi                        // Switching list type...
9940cecf9d5Sandi                        $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
9950cecf9d5Sandi                        $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9960cecf9d5Sandi                        $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9970cecf9d5Sandi                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9980cecf9d5Sandi
9990cecf9d5Sandi                        array_pop($this->listStack);
100010bcc8aaSChristopher Smith                        $this->listStack[] = array($listType, $depth, count($this->listCalls) - 2);
10010cecf9d5Sandi                    }
10020cecf9d5Sandi
10030cecf9d5Sandi                    break;
10040cecf9d5Sandi
10050cecf9d5Sandi                // Haven't dropped down far enough yet.... ( $end[1] > $depth )
10060cecf9d5Sandi                } else {
10070cecf9d5Sandi
10080cecf9d5Sandi                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
10090cecf9d5Sandi                    $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
10100cecf9d5Sandi
10110cecf9d5Sandi                    array_pop($this->listStack);
10120cecf9d5Sandi
10130cecf9d5Sandi                }
10140cecf9d5Sandi
10150cecf9d5Sandi            }
10160cecf9d5Sandi
10170cecf9d5Sandi        }
10180cecf9d5Sandi    }
10190cecf9d5Sandi
10200cecf9d5Sandi    //------------------------------------------------------------------------
10210cecf9d5Sandi    function listContent($call) {
10220cecf9d5Sandi        $this->listCalls[] = $call;
10230cecf9d5Sandi    }
10240cecf9d5Sandi
10250cecf9d5Sandi    //------------------------------------------------------------------------
10260cecf9d5Sandi    function interpretSyntax($match, & $type) {
10270cecf9d5Sandi        if ( substr($match,-1) == '*' ) {
10280cecf9d5Sandi            $type = 'u';
10290cecf9d5Sandi        } else {
10300cecf9d5Sandi            $type = 'o';
10310cecf9d5Sandi        }
10324b7f9e70STom N Harris        // Is the +1 needed? It used to be count(explode(...))
10334b7f9e70STom N Harris        // but I don't think the number is seen outside this handler
10344b7f9e70STom N Harris        return substr_count(str_replace("\t",'  ',$match), '  ') + 1;
10350cecf9d5Sandi    }
10360cecf9d5Sandi}
10370cecf9d5Sandi
10380cecf9d5Sandi//------------------------------------------------------------------------
103926e22ab8SChristopher Smithclass Doku_Handler_Preformatted implements Doku_Handler_CallWriter_Interface {
10400cecf9d5Sandi
10410cecf9d5Sandi    var $CallWriter;
10420cecf9d5Sandi
10430cecf9d5Sandi    var $calls = array();
10440cecf9d5Sandi    var $pos;
10450cecf9d5Sandi    var $text ='';
10460cecf9d5Sandi
10470cecf9d5Sandi
10480cecf9d5Sandi
104926e22ab8SChristopher Smith    function __construct(Doku_Handler_CallWriter_Interface $CallWriter) {
105026e22ab8SChristopher Smith        $this->CallWriter = $CallWriter;
10510cecf9d5Sandi    }
10520cecf9d5Sandi
10530cecf9d5Sandi    function writeCall($call) {
10540cecf9d5Sandi        $this->calls[] = $call;
10550cecf9d5Sandi    }
10560cecf9d5Sandi
10570cecf9d5Sandi    // Probably not needed but just in case...
10580cecf9d5Sandi    function writeCalls($calls) {
10590cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1060f4f02a0fSchris#        $this->CallWriter->writeCalls($this->calls);
1061f4f02a0fSchris    }
1062f4f02a0fSchris
1063f4f02a0fSchris    function finalise() {
1064f4f02a0fSchris        $last_call = end($this->calls);
1065f4f02a0fSchris        $this->writeCall(array('preformatted_end',array(), $last_call[2]));
1066f4f02a0fSchris
1067f4f02a0fSchris        $this->process();
1068f4f02a0fSchris        $this->CallWriter->finalise();
10693893df8eSChristopher Smith        unset($this->CallWriter);
10700cecf9d5Sandi    }
10710cecf9d5Sandi
10720cecf9d5Sandi    function process() {
10730cecf9d5Sandi        foreach ( $this->calls as $call ) {
10740cecf9d5Sandi            switch ($call[0]) {
10750cecf9d5Sandi                case 'preformatted_start':
10760cecf9d5Sandi                    $this->pos = $call[2];
10770cecf9d5Sandi                break;
10780cecf9d5Sandi                case 'preformatted_newline':
10790cecf9d5Sandi                    $this->text .= "\n";
10800cecf9d5Sandi                break;
10810cecf9d5Sandi                case 'preformatted_content':
10820cecf9d5Sandi                    $this->text .= $call[1][0];
10830cecf9d5Sandi                break;
10840cecf9d5Sandi                case 'preformatted_end':
108593a34bf3SChris Smith                    if (trim($this->text)) {
10860cecf9d5Sandi                        $this->CallWriter->writeCall(array('preformatted',array($this->text),$this->pos));
108793a34bf3SChris Smith                    }
108895c19ce7SChris Smith                    // see FS#1699 & FS#1652, add 'eol' instructions to ensure proper triggering of following p_open
108995c19ce7SChris Smith                    $this->CallWriter->writeCall(array('eol',array(),$this->pos));
109095c19ce7SChris Smith                    $this->CallWriter->writeCall(array('eol',array(),$this->pos));
10910cecf9d5Sandi                break;
10920cecf9d5Sandi            }
10930cecf9d5Sandi        }
10940cecf9d5Sandi    }
1095f4f02a0fSchris
10960cecf9d5Sandi}
10970cecf9d5Sandi
10980cecf9d5Sandi//------------------------------------------------------------------------
109926e22ab8SChristopher Smithclass Doku_Handler_Quote implements Doku_Handler_CallWriter_Interface {
11000cecf9d5Sandi
11010cecf9d5Sandi    var $CallWriter;
11020cecf9d5Sandi
11030cecf9d5Sandi    var $calls = array();
11040cecf9d5Sandi
11050cecf9d5Sandi    var $quoteCalls = array();
11060cecf9d5Sandi
110726e22ab8SChristopher Smith    function __construct(Doku_Handler_CallWriter_Interface $CallWriter) {
110826e22ab8SChristopher Smith        $this->CallWriter = $CallWriter;
11090cecf9d5Sandi    }
11100cecf9d5Sandi
11110cecf9d5Sandi    function writeCall($call) {
11120cecf9d5Sandi        $this->calls[] = $call;
11130cecf9d5Sandi    }
11140cecf9d5Sandi
11150cecf9d5Sandi    // Probably not needed but just in case...
11160cecf9d5Sandi    function writeCalls($calls) {
11170cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1118f4f02a0fSchris    }
1119f4f02a0fSchris
1120f4f02a0fSchris    function finalise() {
1121f4f02a0fSchris        $last_call = end($this->calls);
1122f4f02a0fSchris        $this->writeCall(array('quote_end',array(), $last_call[2]));
1123f4f02a0fSchris
1124f4f02a0fSchris        $this->process();
1125f4f02a0fSchris        $this->CallWriter->finalise();
11263893df8eSChristopher Smith        unset($this->CallWriter);
11270cecf9d5Sandi    }
11280cecf9d5Sandi
11290cecf9d5Sandi    function process() {
11300cecf9d5Sandi
11310cecf9d5Sandi        $quoteDepth = 1;
11320cecf9d5Sandi
11330cecf9d5Sandi        foreach ( $this->calls as $call ) {
11340cecf9d5Sandi            switch ($call[0]) {
11350cecf9d5Sandi
11360cecf9d5Sandi                case 'quote_start':
11370cecf9d5Sandi
11380cecf9d5Sandi                    $this->quoteCalls[] = array('quote_open',array(),$call[2]);
11390cecf9d5Sandi
11400cecf9d5Sandi                case 'quote_newline':
11410cecf9d5Sandi
11420cecf9d5Sandi                    $quoteLength = $this->getDepth($call[1][0]);
11430cecf9d5Sandi
11440cecf9d5Sandi                    if ( $quoteLength > $quoteDepth ) {
11450cecf9d5Sandi                        $quoteDiff = $quoteLength - $quoteDepth;
11460cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11470cecf9d5Sandi                            $this->quoteCalls[] = array('quote_open',array(),$call[2]);
11480cecf9d5Sandi                        }
11490cecf9d5Sandi                    } else if ( $quoteLength < $quoteDepth ) {
11500cecf9d5Sandi                        $quoteDiff = $quoteDepth - $quoteLength;
11510cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11520cecf9d5Sandi                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11530cecf9d5Sandi                        }
115426426c64Schris                    } else {
115526426c64Schris                        if ($call[0] != 'quote_start') $this->quoteCalls[] = array('linebreak',array(),$call[2]);
11560cecf9d5Sandi                    }
11570cecf9d5Sandi
11580cecf9d5Sandi                    $quoteDepth = $quoteLength;
11590cecf9d5Sandi
11600cecf9d5Sandi                break;
11610cecf9d5Sandi
11620cecf9d5Sandi                case 'quote_end':
11630cecf9d5Sandi
11640cecf9d5Sandi                    if ( $quoteDepth > 1 ) {
11650cecf9d5Sandi                        $quoteDiff = $quoteDepth - 1;
11660cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11670cecf9d5Sandi                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11680cecf9d5Sandi                        }
11690cecf9d5Sandi                    }
11700cecf9d5Sandi
11710cecf9d5Sandi                    $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11720cecf9d5Sandi
11730cecf9d5Sandi                    $this->CallWriter->writeCalls($this->quoteCalls);
11740cecf9d5Sandi                break;
11750cecf9d5Sandi
11760cecf9d5Sandi                default:
11770cecf9d5Sandi                    $this->quoteCalls[] = $call;
11780cecf9d5Sandi                break;
11790cecf9d5Sandi            }
11800cecf9d5Sandi        }
11810cecf9d5Sandi    }
11820cecf9d5Sandi
11830cecf9d5Sandi    function getDepth($marker) {
11840cecf9d5Sandi        preg_match('/>{1,}/', $marker, $matches);
11850cecf9d5Sandi        $quoteLength = strlen($matches[0]);
11860cecf9d5Sandi        return $quoteLength;
11870cecf9d5Sandi    }
11880cecf9d5Sandi}
11890cecf9d5Sandi
11900cecf9d5Sandi//------------------------------------------------------------------------
119126e22ab8SChristopher Smithclass Doku_Handler_Table implements Doku_Handler_CallWriter_Interface {
11920cecf9d5Sandi
11930cecf9d5Sandi    var $CallWriter;
11940cecf9d5Sandi
11950cecf9d5Sandi    var $calls = array();
11960cecf9d5Sandi    var $tableCalls = array();
11970cecf9d5Sandi    var $maxCols = 0;
11980cecf9d5Sandi    var $maxRows = 1;
11990cecf9d5Sandi    var $currentCols = 0;
120044881bd0Shenning.noren    var $firstCell = false;
12010cecf9d5Sandi    var $lastCellType = 'tablecell';
12029060b8b0SChristopher Smith    var $inTableHead = true;
1203cef031c1SChristopher Smith    var $currentRow = array('tableheader' => 0, 'tablecell' => 0);
12049060b8b0SChristopher Smith    var $countTableHeadRows = 0;
12050cecf9d5Sandi
120626e22ab8SChristopher Smith    function __construct(Doku_Handler_CallWriter_Interface $CallWriter) {
120726e22ab8SChristopher Smith        $this->CallWriter = $CallWriter;
12080cecf9d5Sandi    }
12090cecf9d5Sandi
12100cecf9d5Sandi    function writeCall($call) {
12110cecf9d5Sandi        $this->calls[] = $call;
12120cecf9d5Sandi    }
12130cecf9d5Sandi
12140cecf9d5Sandi    // Probably not needed but just in case...
12150cecf9d5Sandi    function writeCalls($calls) {
12160cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1217f4f02a0fSchris    }
1218f4f02a0fSchris
1219f4f02a0fSchris    function finalise() {
1220f4f02a0fSchris        $last_call = end($this->calls);
1221f4f02a0fSchris        $this->writeCall(array('table_end',array(), $last_call[2]));
1222f4f02a0fSchris
1223f4f02a0fSchris        $this->process();
1224f4f02a0fSchris        $this->CallWriter->finalise();
12253893df8eSChristopher Smith        unset($this->CallWriter);
12260cecf9d5Sandi    }
12270cecf9d5Sandi
12280cecf9d5Sandi    //------------------------------------------------------------------------
12290cecf9d5Sandi    function process() {
12300cecf9d5Sandi        foreach ( $this->calls as $call ) {
12310cecf9d5Sandi            switch ( $call[0] ) {
12320cecf9d5Sandi                case 'table_start':
12330cecf9d5Sandi                    $this->tableStart($call);
12340cecf9d5Sandi                break;
12350cecf9d5Sandi                case 'table_row':
1236aa92c4ccSAdrian Lang                    $this->tableRowClose($call);
12370cecf9d5Sandi                    $this->tableRowOpen(array('tablerow_open',$call[1],$call[2]));
12380cecf9d5Sandi                break;
12390cecf9d5Sandi                case 'tableheader':
12400cecf9d5Sandi                case 'tablecell':
12410cecf9d5Sandi                    $this->tableCell($call);
12420cecf9d5Sandi                break;
12430cecf9d5Sandi                case 'table_end':
1244aa92c4ccSAdrian Lang                    $this->tableRowClose($call);
12450cecf9d5Sandi                    $this->tableEnd($call);
12460cecf9d5Sandi                break;
12470cecf9d5Sandi                default:
12480cecf9d5Sandi                    $this->tableDefault($call);
12490cecf9d5Sandi                break;
12500cecf9d5Sandi            }
12510cecf9d5Sandi        }
12520cecf9d5Sandi        $this->CallWriter->writeCalls($this->tableCalls);
12530cecf9d5Sandi    }
12540cecf9d5Sandi
12550cecf9d5Sandi    function tableStart($call) {
125690df9a4dSAdrian Lang        $this->tableCalls[] = array('table_open',$call[1],$call[2]);
12570cecf9d5Sandi        $this->tableCalls[] = array('tablerow_open',array(),$call[2]);
125844881bd0Shenning.noren        $this->firstCell = true;
12590cecf9d5Sandi    }
12600cecf9d5Sandi
12610cecf9d5Sandi    function tableEnd($call) {
126207c2b1c7SAdrian Lang        $this->tableCalls[] = array('table_close',$call[1],$call[2]);
12630cecf9d5Sandi        $this->finalizeTable();
12640cecf9d5Sandi    }
12650cecf9d5Sandi
12660cecf9d5Sandi    function tableRowOpen($call) {
12670cecf9d5Sandi        $this->tableCalls[] = $call;
12680cecf9d5Sandi        $this->currentCols = 0;
126944881bd0Shenning.noren        $this->firstCell = true;
12700cecf9d5Sandi        $this->lastCellType = 'tablecell';
12710cecf9d5Sandi        $this->maxRows++;
1272cef031c1SChristopher Smith        if ($this->inTableHead) {
1273cef031c1SChristopher Smith            $this->currentRow = array('tablecell' => 0, 'tableheader' => 0);
1274cef031c1SChristopher Smith        }
12750cecf9d5Sandi    }
12760cecf9d5Sandi
12770cecf9d5Sandi    function tableRowClose($call) {
1278cef031c1SChristopher Smith        if ($this->inTableHead && ($this->inTableHead = $this->isTableHeadRow())) {
12799060b8b0SChristopher Smith            $this->countTableHeadRows++;
12809060b8b0SChristopher Smith        }
12810cecf9d5Sandi        // Strip off final cell opening and anything after it
12820cecf9d5Sandi        while ( $discard = array_pop($this->tableCalls ) ) {
12830cecf9d5Sandi
12840cecf9d5Sandi            if ( $discard[0] == 'tablecell_open' || $discard[0] == 'tableheader_open') {
12850cecf9d5Sandi                break;
12860cecf9d5Sandi            }
1287cef031c1SChristopher Smith            if (!empty($this->currentRow[$discard[0]])) {
1288cef031c1SChristopher Smith                $this->currentRow[$discard[0]]--;
1289cef031c1SChristopher Smith            }
12900cecf9d5Sandi        }
1291aa92c4ccSAdrian Lang        $this->tableCalls[] = array('tablerow_close', array(), $call[2]);
12920cecf9d5Sandi
12930cecf9d5Sandi        if ( $this->currentCols > $this->maxCols ) {
12940cecf9d5Sandi            $this->maxCols = $this->currentCols;
12950cecf9d5Sandi        }
12960cecf9d5Sandi    }
12970cecf9d5Sandi
1298cef031c1SChristopher Smith    function isTableHeadRow() {
1299cef031c1SChristopher Smith        $td = $this->currentRow['tablecell'];
1300cef031c1SChristopher Smith        $th = $this->currentRow['tableheader'];
1301cef031c1SChristopher Smith
1302cef031c1SChristopher Smith        if (!$th || $td > 2) return false;
1303cef031c1SChristopher Smith        if (2*$td > $th) return false;
1304cef031c1SChristopher Smith
1305cef031c1SChristopher Smith        return true;
1306cef031c1SChristopher Smith    }
1307cef031c1SChristopher Smith
13080cecf9d5Sandi    function tableCell($call) {
1309cef031c1SChristopher Smith        if ($this->inTableHead) {
1310cef031c1SChristopher Smith            $this->currentRow[$call[0]]++;
13119060b8b0SChristopher Smith        }
13120cecf9d5Sandi        if ( !$this->firstCell ) {
13130cecf9d5Sandi
13140cecf9d5Sandi            // Increase the span
13150cecf9d5Sandi            $lastCall = end($this->tableCalls);
13160cecf9d5Sandi
13170cecf9d5Sandi            // A cell call which follows an open cell means an empty cell so span
13180cecf9d5Sandi            if ( $lastCall[0] == 'tablecell_open' || $lastCall[0] == 'tableheader_open' ) {
13190cecf9d5Sandi                 $this->tableCalls[] = array('colspan',array(),$call[2]);
13200cecf9d5Sandi
13210cecf9d5Sandi            }
13220cecf9d5Sandi
13230cecf9d5Sandi            $this->tableCalls[] = array($this->lastCellType.'_close',array(),$call[2]);
13240ea51e63SMatt Perry            $this->tableCalls[] = array($call[0].'_open',array(1,null,1),$call[2]);
13250cecf9d5Sandi            $this->lastCellType = $call[0];
13260cecf9d5Sandi
13270cecf9d5Sandi        } else {
13280cecf9d5Sandi
13290ea51e63SMatt Perry            $this->tableCalls[] = array($call[0].'_open',array(1,null,1),$call[2]);
13300cecf9d5Sandi            $this->lastCellType = $call[0];
133144881bd0Shenning.noren            $this->firstCell = false;
13320cecf9d5Sandi
13330cecf9d5Sandi        }
13340cecf9d5Sandi
13350cecf9d5Sandi        $this->currentCols++;
13360cecf9d5Sandi    }
13370cecf9d5Sandi
13380cecf9d5Sandi    function tableDefault($call) {
13390cecf9d5Sandi        $this->tableCalls[] = $call;
13400cecf9d5Sandi    }
13410cecf9d5Sandi
13420cecf9d5Sandi    function finalizeTable() {
13430cecf9d5Sandi
13440cecf9d5Sandi        // Add the max cols and rows to the table opening
13450cecf9d5Sandi        if ( $this->tableCalls[0][0] == 'table_open' ) {
13460cecf9d5Sandi            // Adjust to num cols not num col delimeters
13470cecf9d5Sandi            $this->tableCalls[0][1][] = $this->maxCols - 1;
13480cecf9d5Sandi            $this->tableCalls[0][1][] = $this->maxRows;
134990df9a4dSAdrian Lang            $this->tableCalls[0][1][] = array_shift($this->tableCalls[0][1]);
13500cecf9d5Sandi        } else {
13510cecf9d5Sandi            trigger_error('First element in table call list is not table_open');
13520cecf9d5Sandi        }
13530cecf9d5Sandi
13540cecf9d5Sandi        $lastRow = 0;
13550cecf9d5Sandi        $lastCell = 0;
135625b97867Shakan.sandell        $cellKey = array();
13570cecf9d5Sandi        $toDelete = array();
13580cecf9d5Sandi
1359cef031c1SChristopher Smith        // if still in tableheader, then there can be no table header
1360cef031c1SChristopher Smith        // as all rows can't be within <THEAD>
1361cef031c1SChristopher Smith        if ($this->inTableHead) {
1362cef031c1SChristopher Smith            $this->inTableHead = false;
1363cef031c1SChristopher Smith            $this->countTableHeadRows = 0;
1364cef031c1SChristopher Smith        }
1365cef031c1SChristopher Smith
13660cecf9d5Sandi        // Look for the colspan elements and increment the colspan on the
13670cecf9d5Sandi        // previous non-empty opening cell. Once done, delete all the cells
13680cecf9d5Sandi        // that contain colspans
13696606d6fcSAdrian Lang        for ($key = 0 ; $key < count($this->tableCalls) ; ++$key) {
13706606d6fcSAdrian Lang            $call = $this->tableCalls[$key];
13710cecf9d5Sandi
1372aa92c4ccSAdrian Lang            switch ($call[0]) {
13739060b8b0SChristopher Smith                case 'table_open' :
13749060b8b0SChristopher Smith                    if($this->countTableHeadRows) {
13759060b8b0SChristopher Smith                        array_splice($this->tableCalls, $key+1, 0, array(
13769060b8b0SChristopher Smith                              array('tablethead_open', array(), $call[2]))
13779060b8b0SChristopher Smith                        );
13789060b8b0SChristopher Smith                    }
13799060b8b0SChristopher Smith                    break;
13809060b8b0SChristopher Smith
1381aa92c4ccSAdrian Lang                case 'tablerow_open':
13820cecf9d5Sandi
138325b97867Shakan.sandell                    $lastRow++;
138425b97867Shakan.sandell                    $lastCell = 0;
1385aa92c4ccSAdrian Lang                    break;
13860cecf9d5Sandi
1387aa92c4ccSAdrian Lang                case 'tablecell_open':
1388aa92c4ccSAdrian Lang                case 'tableheader_open':
13890cecf9d5Sandi
139025b97867Shakan.sandell                    $lastCell++;
139125b97867Shakan.sandell                    $cellKey[$lastRow][$lastCell] = $key;
1392aa92c4ccSAdrian Lang                    break;
13930cecf9d5Sandi
1394aa92c4ccSAdrian Lang                case 'table_align':
13950cecf9d5Sandi
1396e03b8b2eSAdrian Lang                    $prev = in_array($this->tableCalls[$key-1][0], array('tablecell_open', 'tableheader_open'));
1397e03b8b2eSAdrian Lang                    $next = in_array($this->tableCalls[$key+1][0], array('tablecell_close', 'tableheader_close'));
1398e03b8b2eSAdrian Lang                    // If the cell is empty, align left
1399e03b8b2eSAdrian Lang                    if ($prev && $next) {
1400e03b8b2eSAdrian Lang                        $this->tableCalls[$key-1][1][1] = 'left';
1401e03b8b2eSAdrian Lang
14020cecf9d5Sandi                    // If the previous element was a cell open, align right
1403e03b8b2eSAdrian Lang                    } elseif ($prev) {
14040cecf9d5Sandi                        $this->tableCalls[$key-1][1][1] = 'right';
14050cecf9d5Sandi
1406e03b8b2eSAdrian Lang                    // If the next element is the close of an element, align either center or left
1407e03b8b2eSAdrian Lang                    } elseif ( $next) {
140825b97867Shakan.sandell                        if ( $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] == 'right' ) {
140925b97867Shakan.sandell                            $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] = 'center';
14100cecf9d5Sandi                        } else {
141125b97867Shakan.sandell                            $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] = 'left';
14120cecf9d5Sandi                        }
14130cecf9d5Sandi
14140cecf9d5Sandi                    }
14150cecf9d5Sandi
14160cecf9d5Sandi                    // Now convert the whitespace back to cdata
14170cecf9d5Sandi                    $this->tableCalls[$key][0] = 'cdata';
1418aa92c4ccSAdrian Lang                    break;
14190cecf9d5Sandi
1420aa92c4ccSAdrian Lang                case 'colspan':
14210cecf9d5Sandi
142244881bd0Shenning.noren                    $this->tableCalls[$key-1][1][0] = false;
14230cecf9d5Sandi
142425b97867Shakan.sandell                    for($i = $key-2; $i >= $cellKey[$lastRow][1]; $i--) {
14250cecf9d5Sandi
14260cecf9d5Sandi                        if ( $this->tableCalls[$i][0] == 'tablecell_open' || $this->tableCalls[$i][0] == 'tableheader_open' ) {
14270cecf9d5Sandi
142844881bd0Shenning.noren                            if ( false !== $this->tableCalls[$i][1][0] ) {
14290cecf9d5Sandi                                $this->tableCalls[$i][1][0]++;
14300cecf9d5Sandi                                break;
14310cecf9d5Sandi                            }
14320cecf9d5Sandi
14330cecf9d5Sandi                        }
14340cecf9d5Sandi                    }
14350cecf9d5Sandi
14360cecf9d5Sandi                    $toDelete[] = $key-1;
14370cecf9d5Sandi                    $toDelete[] = $key;
14380cecf9d5Sandi                    $toDelete[] = $key+1;
1439aa92c4ccSAdrian Lang                    break;
144025b97867Shakan.sandell
1441aa92c4ccSAdrian Lang                case 'rowspan':
144225b97867Shakan.sandell
144325b97867Shakan.sandell                    if ( $this->tableCalls[$key-1][0] == 'cdata' ) {
144425b97867Shakan.sandell                        // ignore rowspan if previous call was cdata (text mixed with :::) we don't have to check next call as that wont match regex
144525b97867Shakan.sandell                        $this->tableCalls[$key][0] = 'cdata';
144625b97867Shakan.sandell
144725b97867Shakan.sandell                    } else {
144825b97867Shakan.sandell
14496606d6fcSAdrian Lang                        $spanning_cell = null;
14509060b8b0SChristopher Smith
14519060b8b0SChristopher Smith                        // can't cross thead/tbody boundary
14529060b8b0SChristopher Smith                        if (!$this->countTableHeadRows || ($lastRow-1 != $this->countTableHeadRows)) {
145325b97867Shakan.sandell                            for($i = $lastRow-1; $i > 0; $i--) {
145425b97867Shakan.sandell
145525b97867Shakan.sandell                                if ( $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tablecell_open' || $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tableheader_open' ) {
145625b97867Shakan.sandell
14576606d6fcSAdrian Lang                                    if ($this->tableCalls[$cellKey[$i][$lastCell]][1][2] >= $lastRow - $i) {
14586606d6fcSAdrian Lang                                        $spanning_cell = $i;
145925b97867Shakan.sandell                                        break;
146025b97867Shakan.sandell                                    }
146125b97867Shakan.sandell
146225b97867Shakan.sandell                                }
146325b97867Shakan.sandell                            }
14649060b8b0SChristopher Smith                        }
14656606d6fcSAdrian Lang                        if (is_null($spanning_cell)) {
14666606d6fcSAdrian Lang                            // No spanning cell found, so convert this cell to
14676606d6fcSAdrian Lang                            // an empty one to avoid broken tables
14686a7e97d5SGerrit Uitslag                            $this->tableCalls[$key][0] = 'cdata';
14696a7e97d5SGerrit Uitslag                            $this->tableCalls[$key][1][0] = '';
14706606d6fcSAdrian Lang                            continue;
14716606d6fcSAdrian Lang                        }
14726606d6fcSAdrian Lang                        $this->tableCalls[$cellKey[$spanning_cell][$lastCell]][1][2]++;
14736606d6fcSAdrian Lang
14746606d6fcSAdrian Lang                        $this->tableCalls[$key-1][1][2] = false;
147525b97867Shakan.sandell
147625b97867Shakan.sandell                        $toDelete[] = $key-1;
147725b97867Shakan.sandell                        $toDelete[] = $key;
147825b97867Shakan.sandell                        $toDelete[] = $key+1;
147925b97867Shakan.sandell                    }
1480aa92c4ccSAdrian Lang                    break;
1481aa92c4ccSAdrian Lang
14826606d6fcSAdrian Lang                case 'tablerow_close':
14836606d6fcSAdrian Lang
14846606d6fcSAdrian Lang                    // Fix broken tables by adding missing cells
1485dbd52c81SAndreas Gohr                    $moreCalls = array();
14866606d6fcSAdrian Lang                    while (++$lastCell < $this->maxCols) {
1487dbd52c81SAndreas Gohr                        $moreCalls[] = array('tablecell_open', array(1, null, 1), $call[2]);
1488dbd52c81SAndreas Gohr                        $moreCalls[] = array('cdata', array(''), $call[2]);
1489dbd52c81SAndreas Gohr                        $moreCalls[] = array('tablecell_close', array(), $call[2]);
1490dbd52c81SAndreas Gohr                    }
1491dbd52c81SAndreas Gohr                    $moreCallsLength = count($moreCalls);
1492dbd52c81SAndreas Gohr                    if($moreCallsLength) {
1493dbd52c81SAndreas Gohr                        array_splice($this->tableCalls, $key, 0, $moreCalls);
1494dbd52c81SAndreas Gohr                        $key += $moreCallsLength;
14956606d6fcSAdrian Lang                    }
14966606d6fcSAdrian Lang
14979060b8b0SChristopher Smith                    if($this->countTableHeadRows == $lastRow) {
1498f05a1cc5SGerrit Uitslag                        array_splice($this->tableCalls, $key+1, 0, array(
1499f05a1cc5SGerrit Uitslag                              array('tablethead_close', array(), $call[2])));
1500f05a1cc5SGerrit Uitslag                    }
15016606d6fcSAdrian Lang                    break;
15026606d6fcSAdrian Lang
15030cecf9d5Sandi            }
15040cecf9d5Sandi        }
15050cecf9d5Sandi
15069ab75d9eSAndreas Gohr        // condense cdata
15079ab75d9eSAndreas Gohr        $cnt = count($this->tableCalls);
15089ab75d9eSAndreas Gohr        for( $key = 0; $key < $cnt; $key++){
15099ab75d9eSAndreas Gohr            if($this->tableCalls[$key][0] == 'cdata'){
15109ab75d9eSAndreas Gohr                $ckey = $key;
15119ab75d9eSAndreas Gohr                $key++;
15129ab75d9eSAndreas Gohr                while($this->tableCalls[$key][0] == 'cdata'){
15139ab75d9eSAndreas Gohr                    $this->tableCalls[$ckey][1][0] .= $this->tableCalls[$key][1][0];
15149ab75d9eSAndreas Gohr                    $toDelete[] = $key;
15159ab75d9eSAndreas Gohr                    $key++;
15169ab75d9eSAndreas Gohr                }
15179ab75d9eSAndreas Gohr                continue;
15189ab75d9eSAndreas Gohr            }
15199ab75d9eSAndreas Gohr        }
15209ab75d9eSAndreas Gohr
15210cecf9d5Sandi        foreach ( $toDelete as $delete ) {
15220cecf9d5Sandi            unset($this->tableCalls[$delete]);
15230cecf9d5Sandi        }
15240cecf9d5Sandi        $this->tableCalls = array_values($this->tableCalls);
15250cecf9d5Sandi    }
15260cecf9d5Sandi}
15270cecf9d5Sandi
15280cecf9d5Sandi
15292a27e99aSandi/**
15302a27e99aSandi * Handler for paragraphs
15312a27e99aSandi *
15320b7c14c2Sandi * @author Harry Fuecks <hfuecks@gmail.com>
15332a27e99aSandi */
15340cecf9d5Sandiclass Doku_Handler_Block {
15350cecf9d5Sandi    var $calls = array();
15369569a107SDanny Lin    var $skipEol = false;
153753bfcb59SChristopher Smith    var $inParagraph = false;
15380cecf9d5Sandi
1539af146da0Sandi    // Blocks these should not be inside paragraphs
15400cecf9d5Sandi    var $blockOpen = array(
15410cecf9d5Sandi            'header',
1542df9add72Schris            'listu_open','listo_open','listitem_open','listcontent_open',
1543f05a1cc5SGerrit Uitslag            'table_open','tablerow_open','tablecell_open','tableheader_open','tablethead_open',
15440cecf9d5Sandi            'quote_open',
154576aa94b7Schris            'code','file','hr','preformatted','rss',
154607f89c3cSAnika Henke            'htmlblock','phpblock',
15479569a107SDanny Lin            'footnote_open',
15480cecf9d5Sandi        );
15490cecf9d5Sandi
15500cecf9d5Sandi    var $blockClose = array(
15510cecf9d5Sandi            'header',
1552df9add72Schris            'listu_close','listo_close','listitem_close','listcontent_close',
1553f05a1cc5SGerrit Uitslag            'table_close','tablerow_close','tablecell_close','tableheader_close','tablethead_close',
15540cecf9d5Sandi            'quote_close',
155576aa94b7Schris            'code','file','hr','preformatted','rss',
155607f89c3cSAnika Henke            'htmlblock','phpblock',
15579569a107SDanny Lin            'footnote_close',
15580cecf9d5Sandi        );
15590cecf9d5Sandi
1560af146da0Sandi    // Stacks can contain paragraphs
15610cecf9d5Sandi    var $stackOpen = array(
15629569a107SDanny Lin        'section_open',
15630cecf9d5Sandi        );
15640cecf9d5Sandi
15650cecf9d5Sandi    var $stackClose = array(
15669569a107SDanny Lin        'section_close',
15670cecf9d5Sandi        );
15680cecf9d5Sandi
1569af146da0Sandi
1570af146da0Sandi    /**
1571af146da0Sandi     * Constructor. Adds loaded syntax plugins to the block and stack
1572af146da0Sandi     * arrays
1573af146da0Sandi     *
1574af146da0Sandi     * @author Andreas Gohr <andi@splitbrain.org>
1575af146da0Sandi     */
157626e22ab8SChristopher Smith    function __construct(){
1577af146da0Sandi        global $DOKU_PLUGINS;
1578af146da0Sandi        //check if syntax plugins were loaded
157903c4aec3Schris        if(empty($DOKU_PLUGINS['syntax'])) return;
1580af146da0Sandi        foreach($DOKU_PLUGINS['syntax'] as $n => $p){
1581af146da0Sandi            $ptype = $p->getPType();
1582af146da0Sandi            if($ptype == 'block'){
1583af146da0Sandi                $this->blockOpen[]  = 'plugin_'.$n;
1584af146da0Sandi                $this->blockClose[] = 'plugin_'.$n;
1585af146da0Sandi            }elseif($ptype == 'stack'){
1586af146da0Sandi                $this->stackOpen[]  = 'plugin_'.$n;
1587af146da0Sandi                $this->stackClose[] = 'plugin_'.$n;
1588af146da0Sandi            }
1589af146da0Sandi        }
1590af146da0Sandi    }
1591af146da0Sandi
1592b5a0b131SDanny Lin    function openParagraph($pos){
15939569a107SDanny Lin        if ($this->inParagraph) return;
1594b5a0b131SDanny Lin        $this->calls[] = array('p_open',array(), $pos);
1595b5a0b131SDanny Lin        $this->inParagraph = true;
15969569a107SDanny Lin        $this->skipEol = true;
1597b5a0b131SDanny Lin    }
1598b5a0b131SDanny Lin
15992a27e99aSandi    /**
16002a27e99aSandi     * Close a paragraph if needed
16012a27e99aSandi     *
16022a27e99aSandi     * This function makes sure there are no empty paragraphs on the stack
16032a27e99aSandi     *
16042a27e99aSandi     * @author Andreas Gohr <andi@splitbrain.org>
1605*f50a239bSTakamura     *
1606*f50a239bSTakamura     * @param string|integer $pos
16072a27e99aSandi     */
1608506ae684Sandi    function closeParagraph($pos){
16099569a107SDanny Lin        if (!$this->inParagraph) return;
1610506ae684Sandi        // look back if there was any content - we don't want empty paragraphs
1611506ae684Sandi        $content = '';
1612faba9a35SAndreas Gohr        $ccount = count($this->calls);
1613faba9a35SAndreas Gohr        for($i=$ccount-1; $i>=0; $i--){
1614506ae684Sandi            if($this->calls[$i][0] == 'p_open'){
1615506ae684Sandi                break;
1616506ae684Sandi            }elseif($this->calls[$i][0] == 'cdata'){
1617506ae684Sandi                $content .= $this->calls[$i][1][0];
1618506ae684Sandi            }else{
1619506ae684Sandi                $content = 'found markup';
1620506ae684Sandi                break;
1621506ae684Sandi            }
1622506ae684Sandi        }
1623506ae684Sandi
1624506ae684Sandi        if(trim($content)==''){
1625506ae684Sandi            //remove the whole paragraph
1626a86cc527SAndreas Gohr            //array_splice($this->calls,$i); // <- this is much slower than the loop below
1627d8f7a7f3SAndreas Gohr            for($x=$ccount; $x>$i; $x--) array_pop($this->calls);
1628506ae684Sandi        }else{
16299569a107SDanny Lin            // remove ending linebreaks in the paragraph
16309569a107SDanny Lin            $i=count($this->calls)-1;
16319569a107SDanny Lin            if ($this->calls[$i][0] == 'cdata') $this->calls[$i][1][0] = rtrim($this->calls[$i][1][0],DOKU_PARSER_EOL);
1632506ae684Sandi            $this->calls[] = array('p_close',array(), $pos);
1633506ae684Sandi        }
1634e1c10e4dSchris
163544881bd0Shenning.noren        $this->inParagraph = false;
16369569a107SDanny Lin        $this->skipEol = true;
16370cecf9d5Sandi    }
163841624b31SChris Smith
163941624b31SChris Smith    function addCall($call) {
164041624b31SChris Smith        $key = count($this->calls);
164141624b31SChris Smith        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
164241624b31SChris Smith            $this->calls[$key-1][1][0] .= $call[1][0];
164341624b31SChris Smith        } else {
164441624b31SChris Smith            $this->calls[] = $call;
164541624b31SChris Smith        }
164641624b31SChris Smith    }
16479569a107SDanny Lin
16489569a107SDanny Lin    // simple version of addCall, without checking cdata
16499569a107SDanny Lin    function storeCall($call) {
16509569a107SDanny Lin        $this->calls[] = $call;
16519569a107SDanny Lin    }
16529569a107SDanny Lin
16539569a107SDanny Lin    /**
16549569a107SDanny Lin     * Processes the whole instruction stack to open and close paragraphs
16559569a107SDanny Lin     *
16569569a107SDanny Lin     * @author Harry Fuecks <hfuecks@gmail.com>
16579569a107SDanny Lin     * @author Andreas Gohr <andi@splitbrain.org>
1658*f50a239bSTakamura     *
1659*f50a239bSTakamura     * @param array $calls
1660*f50a239bSTakamura     *
1661*f50a239bSTakamura     * @return array
16629569a107SDanny Lin     */
16639569a107SDanny Lin    function process($calls) {
16649569a107SDanny Lin        // open first paragraph
16659569a107SDanny Lin        $this->openParagraph(0);
16669569a107SDanny Lin        foreach ( $calls as $key => $call ) {
16679569a107SDanny Lin            $cname = $call[0];
16689569a107SDanny Lin            if ($cname == 'plugin') {
16699569a107SDanny Lin                $cname='plugin_'.$call[1][0];
16709569a107SDanny Lin                $plugin = true;
16719569a107SDanny Lin                $plugin_open = (($call[1][2] == DOKU_LEXER_ENTER) || ($call[1][2] == DOKU_LEXER_SPECIAL));
16729569a107SDanny Lin                $plugin_close = (($call[1][2] == DOKU_LEXER_EXIT) || ($call[1][2] == DOKU_LEXER_SPECIAL));
16739569a107SDanny Lin            } else {
16749569a107SDanny Lin                $plugin = false;
16759569a107SDanny Lin            }
16769569a107SDanny Lin            /* stack */
16779569a107SDanny Lin            if ( in_array($cname,$this->stackClose ) && (!$plugin || $plugin_close)) {
16789569a107SDanny Lin                $this->closeParagraph($call[2]);
16799569a107SDanny Lin                $this->storeCall($call);
16809569a107SDanny Lin                $this->openParagraph($call[2]);
16819569a107SDanny Lin                continue;
16829569a107SDanny Lin            }
16839569a107SDanny Lin            if ( in_array($cname,$this->stackOpen ) && (!$plugin || $plugin_open) ) {
16849569a107SDanny Lin                $this->closeParagraph($call[2]);
16859569a107SDanny Lin                $this->storeCall($call);
16869569a107SDanny Lin                $this->openParagraph($call[2]);
16879569a107SDanny Lin                continue;
16889569a107SDanny Lin            }
16899569a107SDanny Lin            /* block */
16909569a107SDanny Lin            // If it's a substition it opens and closes at the same call.
16919569a107SDanny Lin            // To make sure next paragraph is correctly started, let close go first.
16929569a107SDanny Lin            if ( in_array($cname, $this->blockClose) && (!$plugin || $plugin_close)) {
16939569a107SDanny Lin                $this->closeParagraph($call[2]);
16949569a107SDanny Lin                $this->storeCall($call);
16959569a107SDanny Lin                $this->openParagraph($call[2]);
16969569a107SDanny Lin                continue;
16979569a107SDanny Lin            }
16989569a107SDanny Lin            if ( in_array($cname, $this->blockOpen) && (!$plugin || $plugin_open)) {
16999569a107SDanny Lin                $this->closeParagraph($call[2]);
17009569a107SDanny Lin                $this->storeCall($call);
17019569a107SDanny Lin                continue;
17029569a107SDanny Lin            }
17039569a107SDanny Lin            /* eol */
17049569a107SDanny Lin            if ( $cname == 'eol' ) {
17059569a107SDanny Lin                // Check this isn't an eol instruction to skip...
17069569a107SDanny Lin                if ( !$this->skipEol ) {
17079569a107SDanny Lin                    // Next is EOL => double eol => mark as paragraph
17089569a107SDanny Lin                    if ( isset($calls[$key+1]) && $calls[$key+1][0] == 'eol' ) {
17099569a107SDanny Lin                        $this->closeParagraph($call[2]);
17109569a107SDanny Lin                        $this->openParagraph($call[2]);
17119569a107SDanny Lin                    } else {
17129569a107SDanny Lin                        //if this is just a single eol make a space from it
17139569a107SDanny Lin                        $this->addCall(array('cdata',array(DOKU_PARSER_EOL), $call[2]));
17149569a107SDanny Lin                    }
17159569a107SDanny Lin                }
17169569a107SDanny Lin                continue;
17179569a107SDanny Lin            }
17189569a107SDanny Lin            /* normal */
17199569a107SDanny Lin            $this->addCall($call);
17209569a107SDanny Lin            $this->skipEol = false;
17219569a107SDanny Lin        }
17229569a107SDanny Lin        // close last paragraph
17239569a107SDanny Lin        $call = end($this->calls);
17249569a107SDanny Lin        $this->closeParagraph($call[2]);
17259569a107SDanny Lin        return $this->calls;
17269569a107SDanny Lin    }
17270cecf9d5Sandi}
17282a27e99aSandi
1729e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
1730