xref: /dokuwiki/inc/parser/handler.php (revision dbd52c81e07da4440f3afc2e248c5b036f6bbec7)
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
26276820f7SScrutinizer Auto-Fixer     */
27433bef32Sandi    function _addCall($handler, $args, $pos) {
280cecf9d5Sandi        $call = array($handler,$args, $pos);
290cecf9d5Sandi        $this->CallWriter->writeCall($call);
300cecf9d5Sandi    }
310cecf9d5Sandi
3282d61635Spierre.spring    function addPluginCall($plugin, $args, $state, $pos, $match) {
3382d61635Spierre.spring        $call = array('plugin',array($plugin, $args, $state, $match), $pos);
3404ebd214Schris        $this->CallWriter->writeCall($call);
3504ebd214Schris    }
3604ebd214Schris
37433bef32Sandi    function _finalize(){
38e1c10e4dSchris
39f4f02a0fSchris        $this->CallWriter->finalise();
40f4f02a0fSchris
41e1c10e4dSchris        if ( $this->status['section'] ) {
42e1c10e4dSchris            $last_call = end($this->calls);
43e1c10e4dSchris            array_push($this->calls,array('section_close',array(), $last_call[2]));
440cecf9d5Sandi        }
450cecf9d5Sandi
46b7c441b9SHarry Fuecks        if ( $this->rewriteBlocks ) {
4767f9913dSAndreas Gohr            $B = new Doku_Handler_Block();
480cecf9d5Sandi            $this->calls = $B->process($this->calls);
49b7c441b9SHarry Fuecks        }
50e0ad864eSchris
5124bb549bSchris        trigger_event('PARSER_HANDLER_DONE',$this);
520cecf9d5Sandi
530cecf9d5Sandi        array_unshift($this->calls,array('document_start',array(),0));
540cecf9d5Sandi        $last_call = end($this->calls);
550cecf9d5Sandi        array_push($this->calls,array('document_end',array(),$last_call[2]));
560cecf9d5Sandi    }
570cecf9d5Sandi
580cecf9d5Sandi    function fetch() {
590cecf9d5Sandi        $call = each($this->calls);
600cecf9d5Sandi        if ( $call ) {
610cecf9d5Sandi            return $call['value'];
620cecf9d5Sandi        }
6344881bd0Shenning.noren        return false;
640cecf9d5Sandi    }
65ee20e7d1Sandi
66ee20e7d1Sandi
67ee20e7d1Sandi    /**
68ee20e7d1Sandi     * Special plugin handler
69ee20e7d1Sandi     *
70ee20e7d1Sandi     * This handler is called for all modes starting with 'plugin_'.
71ee20e7d1Sandi     * An additional parameter with the plugin name is passed
72ee20e7d1Sandi     *
73ee20e7d1Sandi     * @author Andreas Gohr <andi@splitbrain.org>
74ee20e7d1Sandi     */
75ee20e7d1Sandi    function plugin($match, $state, $pos, $pluginname){
76ee20e7d1Sandi        $data = array($match);
7751a883edSGerrit Uitslag        /** @var DokuWiki_Syntax_Plugin $plugin */
7845d5ad75SGerrit Uitslag        $plugin = plugin_load('syntax',$pluginname);
79a46d0d65SAndreas Gohr        if($plugin != null){
80f02a7d06Schris            $data = $plugin->handle($match, $state, $pos, $this);
81ee20e7d1Sandi        }
8213ecfb18SChris Smith        if ($data !== false) {
8382d61635Spierre.spring            $this->addPluginCall($pluginname,$data,$state,$pos,$match);
8413ecfb18SChris Smith        }
8544881bd0Shenning.noren        return true;
86ee20e7d1Sandi    }
870cecf9d5Sandi
880cecf9d5Sandi    function base($match, $state, $pos) {
890cecf9d5Sandi        switch ( $state ) {
900cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
91433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
9244881bd0Shenning.noren                return true;
930cecf9d5Sandi            break;
940cecf9d5Sandi        }
950cecf9d5Sandi    }
960cecf9d5Sandi
970cecf9d5Sandi    function header($match, $state, $pos) {
98d7e8115fSAndreas Gohr        // get level and title
99a4a2d4cfSAndreas Gohr        $title = trim($match);
100a4a2d4cfSAndreas Gohr        $level = 7 - strspn($title,'=');
101d7e8115fSAndreas Gohr        if($level < 1) $level = 1;
102a4a2d4cfSAndreas Gohr        $title = trim($title,'=');
103a4a2d4cfSAndreas Gohr        $title = trim($title);
1040cecf9d5Sandi
105e1c10e4dSchris        if ($this->status['section']) $this->_addCall('section_close',array(),$pos);
106e1c10e4dSchris
107433bef32Sandi        $this->_addCall('header',array($title,$level,$pos), $pos);
108e1c10e4dSchris
109e1c10e4dSchris        $this->_addCall('section_open',array($level),$pos);
11044881bd0Shenning.noren        $this->status['section'] = true;
11144881bd0Shenning.noren        return true;
1120cecf9d5Sandi    }
1130cecf9d5Sandi
1140cecf9d5Sandi    function notoc($match, $state, $pos) {
115e41c4da9SAndreas Gohr        $this->_addCall('notoc',array(),$pos);
11644881bd0Shenning.noren        return true;
1170cecf9d5Sandi    }
1180cecf9d5Sandi
1199dc2c2afSandi    function nocache($match, $state, $pos) {
1209dc2c2afSandi        $this->_addCall('nocache',array(),$pos);
12144881bd0Shenning.noren        return true;
1229dc2c2afSandi    }
1239dc2c2afSandi
1240cecf9d5Sandi    function linebreak($match, $state, $pos) {
125433bef32Sandi        $this->_addCall('linebreak',array(),$pos);
12644881bd0Shenning.noren        return true;
1270cecf9d5Sandi    }
1280cecf9d5Sandi
1290cecf9d5Sandi    function eol($match, $state, $pos) {
130433bef32Sandi        $this->_addCall('eol',array(),$pos);
13144881bd0Shenning.noren        return true;
1320cecf9d5Sandi    }
1330cecf9d5Sandi
1340cecf9d5Sandi    function hr($match, $state, $pos) {
135433bef32Sandi        $this->_addCall('hr',array(),$pos);
13644881bd0Shenning.noren        return true;
1370cecf9d5Sandi    }
1380cecf9d5Sandi
139276820f7SScrutinizer Auto-Fixer    /**
140276820f7SScrutinizer Auto-Fixer     * @param string $name
141276820f7SScrutinizer Auto-Fixer     */
142433bef32Sandi    function _nestingTag($match, $state, $pos, $name) {
1430cecf9d5Sandi        switch ( $state ) {
1440cecf9d5Sandi            case DOKU_LEXER_ENTER:
145433bef32Sandi                $this->_addCall($name.'_open', array(), $pos);
1460cecf9d5Sandi            break;
1470cecf9d5Sandi            case DOKU_LEXER_EXIT:
148433bef32Sandi                $this->_addCall($name.'_close', array(), $pos);
1490cecf9d5Sandi            break;
1500cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
151433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
1520cecf9d5Sandi            break;
1530cecf9d5Sandi        }
1540cecf9d5Sandi    }
1550cecf9d5Sandi
1560cecf9d5Sandi    function strong($match, $state, $pos) {
157433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'strong');
15844881bd0Shenning.noren        return true;
1590cecf9d5Sandi    }
1600cecf9d5Sandi
1610cecf9d5Sandi    function emphasis($match, $state, $pos) {
162433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'emphasis');
16344881bd0Shenning.noren        return true;
1640cecf9d5Sandi    }
1650cecf9d5Sandi
1660cecf9d5Sandi    function underline($match, $state, $pos) {
167433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'underline');
16844881bd0Shenning.noren        return true;
1690cecf9d5Sandi    }
1700cecf9d5Sandi
1710cecf9d5Sandi    function monospace($match, $state, $pos) {
172433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'monospace');
17344881bd0Shenning.noren        return true;
1740cecf9d5Sandi    }
1750cecf9d5Sandi
1760cecf9d5Sandi    function subscript($match, $state, $pos) {
177433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'subscript');
17844881bd0Shenning.noren        return true;
1790cecf9d5Sandi    }
1800cecf9d5Sandi
1810cecf9d5Sandi    function superscript($match, $state, $pos) {
182433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'superscript');
18344881bd0Shenning.noren        return true;
1840cecf9d5Sandi    }
1850cecf9d5Sandi
1860cecf9d5Sandi    function deleted($match, $state, $pos) {
187433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'deleted');
18844881bd0Shenning.noren        return true;
1890cecf9d5Sandi    }
1900cecf9d5Sandi
1910cecf9d5Sandi
1920cecf9d5Sandi    function footnote($match, $state, $pos) {
1935587e44cSchris//        $this->_nestingTag($match, $state, $pos, 'footnote');
194742c66f8Schris        if (!isset($this->_footnote)) $this->_footnote = false;
1952fe7363dSchris
1965587e44cSchris        switch ( $state ) {
1975587e44cSchris            case DOKU_LEXER_ENTER:
1982fe7363dSchris                // footnotes can not be nested - however due to limitations in lexer it can't be prevented
1992fe7363dSchris                // we will still enter a new footnote mode, we just do nothing
200742c66f8Schris                if ($this->_footnote) {
2012fe7363dSchris                    $this->_addCall('cdata',array($match), $pos);
2022fe7363dSchris                    break;
2032fe7363dSchris                }
2042fe7363dSchris
205742c66f8Schris                $this->_footnote = true;
2062fe7363dSchris
20767f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Nest($this->CallWriter,'footnote_close');
2085587e44cSchris                $this->CallWriter = & $ReWriter;
2094a26ad85Schris                $this->_addCall('footnote_open', array(), $pos);
2105587e44cSchris            break;
2115587e44cSchris            case DOKU_LEXER_EXIT:
2122fe7363dSchris                // check whether we have already exitted the footnote mode, can happen if the modes were nested
213742c66f8Schris                if (!$this->_footnote) {
2142fe7363dSchris                    $this->_addCall('cdata',array($match), $pos);
2152fe7363dSchris                    break;
2162fe7363dSchris                }
2172fe7363dSchris
218742c66f8Schris                $this->_footnote = false;
2192fe7363dSchris
2205587e44cSchris                $this->_addCall('footnote_close', array(), $pos);
2215587e44cSchris                $this->CallWriter->process();
2225587e44cSchris                $ReWriter = & $this->CallWriter;
2235587e44cSchris                $this->CallWriter = & $ReWriter->CallWriter;
2245587e44cSchris            break;
2255587e44cSchris            case DOKU_LEXER_UNMATCHED:
2265587e44cSchris                $this->_addCall('cdata', array($match), $pos);
2275587e44cSchris            break;
2285587e44cSchris        }
22944881bd0Shenning.noren        return true;
2300cecf9d5Sandi    }
2310cecf9d5Sandi
2320cecf9d5Sandi    function listblock($match, $state, $pos) {
2330cecf9d5Sandi        switch ( $state ) {
2340cecf9d5Sandi            case DOKU_LEXER_ENTER:
23567f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_List($this->CallWriter);
2360cecf9d5Sandi                $this->CallWriter = & $ReWriter;
237433bef32Sandi                $this->_addCall('list_open', array($match), $pos);
2380cecf9d5Sandi            break;
2390cecf9d5Sandi            case DOKU_LEXER_EXIT:
240433bef32Sandi                $this->_addCall('list_close', array(), $pos);
2410cecf9d5Sandi                $this->CallWriter->process();
2420cecf9d5Sandi                $ReWriter = & $this->CallWriter;
2430cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
2440cecf9d5Sandi            break;
2450cecf9d5Sandi            case DOKU_LEXER_MATCHED:
246433bef32Sandi                $this->_addCall('list_item', array($match), $pos);
2470cecf9d5Sandi            break;
2480cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
249433bef32Sandi                $this->_addCall('cdata', array($match), $pos);
2500cecf9d5Sandi            break;
2510cecf9d5Sandi        }
25244881bd0Shenning.noren        return true;
2530cecf9d5Sandi    }
2540cecf9d5Sandi
2550cecf9d5Sandi    function unformatted($match, $state, $pos) {
2560cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
257433bef32Sandi            $this->_addCall('unformatted',array($match), $pos);
2580cecf9d5Sandi        }
25944881bd0Shenning.noren        return true;
2600cecf9d5Sandi    }
2610cecf9d5Sandi
2620cecf9d5Sandi    function php($match, $state, $pos) {
263df9add72Schris        global $conf;
2640cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
265433bef32Sandi            $this->_addCall('php',array($match), $pos);
2660cecf9d5Sandi        }
26744881bd0Shenning.noren        return true;
2680cecf9d5Sandi    }
2690cecf9d5Sandi
27007f89c3cSAnika Henke    function phpblock($match, $state, $pos) {
27107f89c3cSAnika Henke        global $conf;
27207f89c3cSAnika Henke        if ( $state == DOKU_LEXER_UNMATCHED ) {
27307f89c3cSAnika Henke            $this->_addCall('phpblock',array($match), $pos);
27407f89c3cSAnika Henke        }
27507f89c3cSAnika Henke        return true;
27607f89c3cSAnika Henke    }
27707f89c3cSAnika Henke
2780cecf9d5Sandi    function html($match, $state, $pos) {
279df9add72Schris        global $conf;
2800cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
281433bef32Sandi            $this->_addCall('html',array($match), $pos);
2820cecf9d5Sandi        }
28344881bd0Shenning.noren        return true;
2840cecf9d5Sandi    }
2850cecf9d5Sandi
28607f89c3cSAnika Henke    function htmlblock($match, $state, $pos) {
28707f89c3cSAnika Henke        global $conf;
28807f89c3cSAnika Henke        if ( $state == DOKU_LEXER_UNMATCHED ) {
28907f89c3cSAnika Henke            $this->_addCall('htmlblock',array($match), $pos);
29007f89c3cSAnika Henke        }
29107f89c3cSAnika Henke        return true;
29207f89c3cSAnika Henke    }
29307f89c3cSAnika Henke
2940cecf9d5Sandi    function preformatted($match, $state, $pos) {
2950cecf9d5Sandi        switch ( $state ) {
2960cecf9d5Sandi            case DOKU_LEXER_ENTER:
29767f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Preformatted($this->CallWriter);
29826e22ab8SChristopher Smith                $this->CallWriter = $ReWriter;
299433bef32Sandi                $this->_addCall('preformatted_start',array(), $pos);
3000cecf9d5Sandi            break;
3010cecf9d5Sandi            case DOKU_LEXER_EXIT:
302433bef32Sandi                $this->_addCall('preformatted_end',array(), $pos);
3030cecf9d5Sandi                $this->CallWriter->process();
3040cecf9d5Sandi                $ReWriter = & $this->CallWriter;
3050cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
3060cecf9d5Sandi            break;
3070cecf9d5Sandi            case DOKU_LEXER_MATCHED:
308433bef32Sandi                $this->_addCall('preformatted_newline',array(), $pos);
3090cecf9d5Sandi            break;
3100cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
311433bef32Sandi                $this->_addCall('preformatted_content',array($match), $pos);
3120cecf9d5Sandi            break;
3130cecf9d5Sandi        }
3140cecf9d5Sandi
31544881bd0Shenning.noren        return true;
3160cecf9d5Sandi    }
3170cecf9d5Sandi
3180cecf9d5Sandi    function quote($match, $state, $pos) {
3190cecf9d5Sandi
3200cecf9d5Sandi        switch ( $state ) {
3210cecf9d5Sandi
3220cecf9d5Sandi            case DOKU_LEXER_ENTER:
32367f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Quote($this->CallWriter);
3240cecf9d5Sandi                $this->CallWriter = & $ReWriter;
325433bef32Sandi                $this->_addCall('quote_start',array($match), $pos);
3260cecf9d5Sandi            break;
3270cecf9d5Sandi
3280cecf9d5Sandi            case DOKU_LEXER_EXIT:
329433bef32Sandi                $this->_addCall('quote_end',array(), $pos);
3300cecf9d5Sandi                $this->CallWriter->process();
3310cecf9d5Sandi                $ReWriter = & $this->CallWriter;
3320cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
3330cecf9d5Sandi            break;
3340cecf9d5Sandi
3350cecf9d5Sandi            case DOKU_LEXER_MATCHED:
336433bef32Sandi                $this->_addCall('quote_newline',array($match), $pos);
3370cecf9d5Sandi            break;
3380cecf9d5Sandi
3390cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
340433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
3410cecf9d5Sandi            break;
3420cecf9d5Sandi
3430cecf9d5Sandi        }
3440cecf9d5Sandi
34544881bd0Shenning.noren        return true;
3460cecf9d5Sandi    }
3470cecf9d5Sandi
3483d491f75SAndreas Gohr    function file($match, $state, $pos) {
3493d491f75SAndreas Gohr        return $this->code($match, $state, $pos, 'file');
3503d491f75SAndreas Gohr    }
3513d491f75SAndreas Gohr
3523d491f75SAndreas Gohr    function code($match, $state, $pos, $type='code') {
3533d491f75SAndreas Gohr        if ( $state == DOKU_LEXER_UNMATCHED ) {
3544b7f9e70STom N Harris            $matches = explode('>',$match,2);
3553d491f75SAndreas Gohr
3560139312bSAdrian Lang            $param = preg_split('/\s+/', $matches[0], 2, PREG_SPLIT_NO_EMPTY);
3570139312bSAdrian Lang            while(count($param) < 2) array_push($param, null);
3580139312bSAdrian Lang
3590139312bSAdrian Lang            // We shortcut html here.
3600139312bSAdrian Lang            if ($param[0] == 'html') $param[0] = 'html4strict';
3610139312bSAdrian Lang            if ($param[0] == '-') $param[0] = null;
3620139312bSAdrian Lang            array_unshift($param, $matches[1]);
3630139312bSAdrian Lang
3640139312bSAdrian Lang            $this->_addCall($type, $param, $pos);
3650cecf9d5Sandi        }
36644881bd0Shenning.noren        return true;
3670cecf9d5Sandi    }
3680cecf9d5Sandi
3690cecf9d5Sandi    function acronym($match, $state, $pos) {
370433bef32Sandi        $this->_addCall('acronym',array($match), $pos);
37144881bd0Shenning.noren        return true;
3720cecf9d5Sandi    }
3730cecf9d5Sandi
3740cecf9d5Sandi    function smiley($match, $state, $pos) {
375433bef32Sandi        $this->_addCall('smiley',array($match), $pos);
37644881bd0Shenning.noren        return true;
3770cecf9d5Sandi    }
3780cecf9d5Sandi
3790cecf9d5Sandi    function wordblock($match, $state, $pos) {
380433bef32Sandi        $this->_addCall('wordblock',array($match), $pos);
38144881bd0Shenning.noren        return true;
3820cecf9d5Sandi    }
3830cecf9d5Sandi
3840cecf9d5Sandi    function entity($match, $state, $pos) {
385433bef32Sandi        $this->_addCall('entity',array($match), $pos);
38644881bd0Shenning.noren        return true;
3870cecf9d5Sandi    }
3880cecf9d5Sandi
3890cecf9d5Sandi    function multiplyentity($match, $state, $pos) {
3900cecf9d5Sandi        preg_match_all('/\d+/',$match,$matches);
391433bef32Sandi        $this->_addCall('multiplyentity',array($matches[0][0],$matches[0][1]), $pos);
39244881bd0Shenning.noren        return true;
3930cecf9d5Sandi    }
3940cecf9d5Sandi
3950cecf9d5Sandi    function singlequoteopening($match, $state, $pos) {
396433bef32Sandi        $this->_addCall('singlequoteopening',array(), $pos);
39744881bd0Shenning.noren        return true;
3980cecf9d5Sandi    }
3990cecf9d5Sandi
4000cecf9d5Sandi    function singlequoteclosing($match, $state, $pos) {
401433bef32Sandi        $this->_addCall('singlequoteclosing',array(), $pos);
40244881bd0Shenning.noren        return true;
4030cecf9d5Sandi    }
4040cecf9d5Sandi
40557d757d1SAndreas Gohr    function apostrophe($match, $state, $pos) {
40657d757d1SAndreas Gohr        $this->_addCall('apostrophe',array(), $pos);
40757d757d1SAndreas Gohr        return true;
40857d757d1SAndreas Gohr    }
40957d757d1SAndreas Gohr
4100cecf9d5Sandi    function doublequoteopening($match, $state, $pos) {
411433bef32Sandi        $this->_addCall('doublequoteopening',array(), $pos);
412e950d12fSChristopher Smith        $this->status['doublequote']++;
41344881bd0Shenning.noren        return true;
4140cecf9d5Sandi    }
4150cecf9d5Sandi
4160cecf9d5Sandi    function doublequoteclosing($match, $state, $pos) {
417e950d12fSChristopher Smith        if ($this->status['doublequote'] <= 0) {
418e950d12fSChristopher Smith            $this->doublequoteopening($match, $state, $pos);
419e950d12fSChristopher Smith        } else {
420433bef32Sandi            $this->_addCall('doublequoteclosing',array(), $pos);
421e950d12fSChristopher Smith            $this->status['doublequote'] = max(0, --$this->status['doublequote']);
422e950d12fSChristopher Smith        }
42344881bd0Shenning.noren        return true;
4240cecf9d5Sandi    }
4250cecf9d5Sandi
4260cecf9d5Sandi    function camelcaselink($match, $state, $pos) {
427433bef32Sandi        $this->_addCall('camelcaselink',array($match), $pos);
42844881bd0Shenning.noren        return true;
4290cecf9d5Sandi    }
4300cecf9d5Sandi
4310cecf9d5Sandi    /*
4320cecf9d5Sandi    */
4330cecf9d5Sandi    function internallink($match, $state, $pos) {
4340cecf9d5Sandi        // Strip the opening and closing markup
4350cecf9d5Sandi        $link = preg_replace(array('/^\[\[/','/\]\]$/u'),'',$match);
4360cecf9d5Sandi
4370cecf9d5Sandi        // Split title from URL
4384b7f9e70STom N Harris        $link = explode('|',$link,2);
4390cecf9d5Sandi        if ( !isset($link[1]) ) {
4400ea51e63SMatt Perry            $link[1] = null;
4410cecf9d5Sandi        } else if ( preg_match('/^\{\{[^\}]+\}\}$/',$link[1]) ) {
4425578eb8fSandi            // If the title is an image, convert it to an array containing the image details
443b625487dSandi            $link[1] = Doku_Handler_Parse_Media($link[1]);
4440cecf9d5Sandi        }
4450b7c14c2Sandi        $link[0] = trim($link[0]);
4460cecf9d5Sandi
4470e1c636eSandi        //decide which kind of link it is
4480e1c636eSandi
449e08dda3fSAndreas Gohr        if ( preg_match('/^[a-zA-Z0-9\.]+>{1}.*$/u',$link[0]) ) {
4500e1c636eSandi            // Interwiki
4514b7f9e70STom N Harris            $interwiki = explode('>',$link[0],2);
452433bef32Sandi            $this->_addCall(
4530cecf9d5Sandi                'interwikilink',
4540cecf9d5Sandi                array($link[0],$link[1],strtolower($interwiki[0]),$interwiki[1]),
4550cecf9d5Sandi                $pos
4560cecf9d5Sandi                );
45715f1b77cSAndreas Gohr        }elseif ( preg_match('/^\\\\\\\\[^\\\\]+?\\\\/u',$link[0]) ) {
4580e1c636eSandi            // Windows Share
459433bef32Sandi            $this->_addCall(
4600cecf9d5Sandi                'windowssharelink',
4610cecf9d5Sandi                array($link[0],$link[1]),
4620cecf9d5Sandi                $pos
4630cecf9d5Sandi                );
4644468cb4cSAndreas Gohr        }elseif ( preg_match('#^([a-z0-9\-\.+]+?)://#i',$link[0]) ) {
4650e1c636eSandi            // external link (accepts all protocols)
466433bef32Sandi            $this->_addCall(
4670cecf9d5Sandi                    'externallink',
4680cecf9d5Sandi                    array($link[0],$link[1]),
4690cecf9d5Sandi                    $pos
4700cecf9d5Sandi                    );
4710a1d30bfSchris        }elseif ( preg_match('<'.PREG_PATTERN_VALID_EMAIL.'>',$link[0]) ) {
4720a1d30bfSchris            // E-Mail (pattern above is defined in inc/mail.php)
473a6755281Sandi            $this->_addCall(
474a6755281Sandi                'emaillink',
475a6755281Sandi                array($link[0],$link[1]),
476a6755281Sandi                $pos
477a6755281Sandi                );
4780b7c14c2Sandi        }elseif ( preg_match('!^#.+!',$link[0]) ){
4790b7c14c2Sandi            // local link
4800b7c14c2Sandi            $this->_addCall(
4810b7c14c2Sandi                'locallink',
4820b7c14c2Sandi                array(substr($link[0],1),$link[1]),
4830b7c14c2Sandi                $pos
4840b7c14c2Sandi                );
4850e1c636eSandi        }else{
4860e1c636eSandi            // internal link
487433bef32Sandi            $this->_addCall(
4880e1c636eSandi                'internallink',
4890e1c636eSandi                array($link[0],$link[1]),
4900e1c636eSandi                $pos
4910e1c636eSandi                );
4920cecf9d5Sandi        }
4930e1c636eSandi
49444881bd0Shenning.noren        return true;
4950cecf9d5Sandi    }
4960cecf9d5Sandi
4970cecf9d5Sandi    function filelink($match, $state, $pos) {
4980ea51e63SMatt Perry        $this->_addCall('filelink',array($match, null), $pos);
49944881bd0Shenning.noren        return true;
5000cecf9d5Sandi    }
5010cecf9d5Sandi
5020cecf9d5Sandi    function windowssharelink($match, $state, $pos) {
5030ea51e63SMatt Perry        $this->_addCall('windowssharelink',array($match, null), $pos);
50444881bd0Shenning.noren        return true;
5050cecf9d5Sandi    }
5060cecf9d5Sandi
5070cecf9d5Sandi    function media($match, $state, $pos) {
5080cecf9d5Sandi        $p = Doku_Handler_Parse_Media($match);
5090cecf9d5Sandi
510433bef32Sandi        $this->_addCall(
5110cecf9d5Sandi              $p['type'],
512dc673a5bSjoe.lapp              array($p['src'], $p['title'], $p['align'], $p['width'],
513dc673a5bSjoe.lapp                     $p['height'], $p['cache'], $p['linking']),
5140cecf9d5Sandi              $pos
5150cecf9d5Sandi             );
51644881bd0Shenning.noren        return true;
5170cecf9d5Sandi    }
5180cecf9d5Sandi
519b625487dSandi    function rss($match, $state, $pos) {
520b625487dSandi        $link = preg_replace(array('/^\{\{rss>/','/\}\}$/'),'',$match);
5213db95becSAndreas Gohr
5223db95becSAndreas Gohr        // get params
5233db95becSAndreas Gohr        list($link,$params) = explode(' ',$link,2);
5243db95becSAndreas Gohr
5253db95becSAndreas Gohr        $p = array();
5263db95becSAndreas Gohr        if(preg_match('/\b(\d+)\b/',$params,$match)){
5273db95becSAndreas Gohr            $p['max'] = $match[1];
5283db95becSAndreas Gohr        }else{
5293db95becSAndreas Gohr            $p['max'] = 8;
5303db95becSAndreas Gohr        }
5313db95becSAndreas Gohr        $p['reverse'] = (preg_match('/rev/',$params));
5323db95becSAndreas Gohr        $p['author']  = (preg_match('/\b(by|author)/',$params));
5333db95becSAndreas Gohr        $p['date']    = (preg_match('/\b(date)/',$params));
5343db95becSAndreas Gohr        $p['details'] = (preg_match('/\b(desc|detail)/',$params));
53538c6f603SRobin H. Johnson        $p['nosort']  = (preg_match('/\b(nosort)\b/',$params));
5363db95becSAndreas Gohr
5370a69dff7Schris        if (preg_match('/\b(\d+)([dhm])\b/',$params,$match)) {
5380a69dff7Schris            $period = array('d' => 86400, 'h' => 3600, 'm' => 60);
5390a69dff7Schris            $p['refresh'] = max(600,$match[1]*$period[$match[2]]);  // n * period in seconds, minimum 10 minutes
5400a69dff7Schris        } else {
5410a69dff7Schris            $p['refresh'] = 14400;   // default to 4 hours
5420a69dff7Schris        }
5430a69dff7Schris
5443db95becSAndreas Gohr        $this->_addCall('rss',array($link,$p),$pos);
54544881bd0Shenning.noren        return true;
546b625487dSandi    }
547b625487dSandi
5480cecf9d5Sandi    function externallink($match, $state, $pos) {
549da9f31c5SAndreas Gohr        $url   = $match;
550da9f31c5SAndreas Gohr        $title = null;
5510cecf9d5Sandi
552da9f31c5SAndreas Gohr        // add protocol on simple short URLs
553da9f31c5SAndreas Gohr        if(substr($url,0,3) == 'ftp' && (substr($url,0,6) != 'ftp://')){
554da9f31c5SAndreas Gohr            $title = $url;
555da9f31c5SAndreas Gohr            $url   = 'ftp://'.$url;
556da9f31c5SAndreas Gohr        }
557da9f31c5SAndreas Gohr        if(substr($url,0,3) == 'www' && (substr($url,0,7) != 'http://')){
558da9f31c5SAndreas Gohr            $title = $url;
559da9f31c5SAndreas Gohr            $url = 'http://'.$url;
560da9f31c5SAndreas Gohr        }
561da9f31c5SAndreas Gohr
562da9f31c5SAndreas Gohr        $this->_addCall('externallink',array($url, $title), $pos);
56344881bd0Shenning.noren        return true;
5640cecf9d5Sandi    }
5650cecf9d5Sandi
56671352defSandi    function emaillink($match, $state, $pos) {
5670cecf9d5Sandi        $email = preg_replace(array('/^</','/>$/'),'',$match);
5680ea51e63SMatt Perry        $this->_addCall('emaillink',array($email, null), $pos);
56944881bd0Shenning.noren        return true;
5700cecf9d5Sandi    }
5710cecf9d5Sandi
5720cecf9d5Sandi    function table($match, $state, $pos) {
5730cecf9d5Sandi        switch ( $state ) {
5740cecf9d5Sandi
5750cecf9d5Sandi            case DOKU_LEXER_ENTER:
5760cecf9d5Sandi
57767f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Table($this->CallWriter);
5780cecf9d5Sandi                $this->CallWriter = & $ReWriter;
5790cecf9d5Sandi
58090df9a4dSAdrian Lang                $this->_addCall('table_start', array($pos + 1), $pos);
5810cecf9d5Sandi                if ( trim($match) == '^' ) {
582433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
5830cecf9d5Sandi                } else {
584433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
5850cecf9d5Sandi                }
5860cecf9d5Sandi            break;
5870cecf9d5Sandi
5880cecf9d5Sandi            case DOKU_LEXER_EXIT:
58990df9a4dSAdrian Lang                $this->_addCall('table_end', array($pos), $pos);
5900cecf9d5Sandi                $this->CallWriter->process();
5910cecf9d5Sandi                $ReWriter = & $this->CallWriter;
5920cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
5930cecf9d5Sandi            break;
5940cecf9d5Sandi
5950cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
5960cecf9d5Sandi                if ( trim($match) != '' ) {
597433bef32Sandi                    $this->_addCall('cdata',array($match), $pos);
5980cecf9d5Sandi                }
5990cecf9d5Sandi            break;
6000cecf9d5Sandi
6010cecf9d5Sandi            case DOKU_LEXER_MATCHED:
6029ab75d9eSAndreas Gohr                if ( $match == ' ' ){
6039ab75d9eSAndreas Gohr                    $this->_addCall('cdata', array($match), $pos);
60425b97867Shakan.sandell                } else if ( preg_match('/:::/',$match) ) {
60525b97867Shakan.sandell                    $this->_addCall('rowspan', array($match), $pos);
606e205b721SAndreas Gohr                } else if ( preg_match('/\t+/',$match) ) {
6079ab75d9eSAndreas Gohr                    $this->_addCall('table_align', array($match), $pos);
608e205b721SAndreas Gohr                } else if ( preg_match('/ {2,}/',$match) ) {
609433bef32Sandi                    $this->_addCall('table_align', array($match), $pos);
6100cecf9d5Sandi                } else if ( $match == "\n|" ) {
611433bef32Sandi                    $this->_addCall('table_row', array(), $pos);
612433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
6130cecf9d5Sandi                } else if ( $match == "\n^" ) {
614433bef32Sandi                    $this->_addCall('table_row', array(), $pos);
615433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
6160cecf9d5Sandi                } else if ( $match == '|' ) {
617433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
6180cecf9d5Sandi                } else if ( $match == '^' ) {
619433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
6200cecf9d5Sandi                }
6210cecf9d5Sandi            break;
6220cecf9d5Sandi        }
62344881bd0Shenning.noren        return true;
6240cecf9d5Sandi    }
6250cecf9d5Sandi}
6260cecf9d5Sandi
6270cecf9d5Sandi//------------------------------------------------------------------------
6280cecf9d5Sandifunction Doku_Handler_Parse_Media($match) {
6290cecf9d5Sandi
6300cecf9d5Sandi    // Strip the opening and closing markup
6310cecf9d5Sandi    $link = preg_replace(array('/^\{\{/','/\}\}$/u'),'',$match);
6320cecf9d5Sandi
6330cecf9d5Sandi    // Split title from URL
6344b7f9e70STom N Harris    $link = explode('|',$link,2);
6350cecf9d5Sandi
6360cecf9d5Sandi    // Check alignment
6370cecf9d5Sandi    $ralign = (bool)preg_match('/^ /',$link[0]);
6380cecf9d5Sandi    $lalign = (bool)preg_match('/ $/',$link[0]);
6390cecf9d5Sandi
6400cecf9d5Sandi    // Logic = what's that ;)...
6410cecf9d5Sandi    if ( $lalign & $ralign ) {
6420cecf9d5Sandi        $align = 'center';
6430cecf9d5Sandi    } else if ( $ralign ) {
6440cecf9d5Sandi        $align = 'right';
6450cecf9d5Sandi    } else if ( $lalign ) {
6460cecf9d5Sandi        $align = 'left';
6470cecf9d5Sandi    } else {
6480ea51e63SMatt Perry        $align = null;
6490cecf9d5Sandi    }
6500cecf9d5Sandi
6510cecf9d5Sandi    // The title...
6520cecf9d5Sandi    if ( !isset($link[1]) ) {
6530ea51e63SMatt Perry        $link[1] = null;
6540cecf9d5Sandi    }
6550cecf9d5Sandi
6564826ab45Sandi    //remove aligning spaces
6574826ab45Sandi    $link[0] = trim($link[0]);
6580cecf9d5Sandi
6594826ab45Sandi    //split into src and parameters (using the very last questionmark)
6604826ab45Sandi    $pos = strrpos($link[0], '?');
6614826ab45Sandi    if($pos !== false){
6624826ab45Sandi        $src   = substr($link[0],0,$pos);
6634826ab45Sandi        $param = substr($link[0],$pos+1);
6640cecf9d5Sandi    }else{
6654826ab45Sandi        $src   = $link[0];
6664826ab45Sandi        $param = '';
6670cecf9d5Sandi    }
6680cecf9d5Sandi
6694826ab45Sandi    //parse width and height
6704826ab45Sandi    if(preg_match('#(\d+)(x(\d+))?#i',$param,$size)){
671443e135dSChristopher Smith        !empty($size[1]) ? $w = $size[1] : $w = null;
672443e135dSChristopher Smith        !empty($size[3]) ? $h = $size[3] : $h = null;
673fc1c55b1Shfuecks    } else {
6740ea51e63SMatt Perry        $w = null;
6750ea51e63SMatt Perry        $h = null;
6760cecf9d5Sandi    }
6770cecf9d5Sandi
678dc673a5bSjoe.lapp    //get linking command
679d35ab615Shenning.noren    if(preg_match('/nolink/i',$param)){
680dc673a5bSjoe.lapp        $linking = 'nolink';
681d35ab615Shenning.noren    }else if(preg_match('/direct/i',$param)){
682dc673a5bSjoe.lapp        $linking = 'direct';
6838acb3108SAndreas Gohr    }else if(preg_match('/linkonly/i',$param)){
6848acb3108SAndreas Gohr        $linking = 'linkonly';
685dc673a5bSjoe.lapp    }else{
686dc673a5bSjoe.lapp        $linking = 'details';
687dc673a5bSjoe.lapp    }
688dc673a5bSjoe.lapp
6894826ab45Sandi    //get caching command
6904826ab45Sandi    if (preg_match('/(nocache|recache)/i',$param,$cachemode)){
6914826ab45Sandi        $cache = $cachemode[1];
6920cecf9d5Sandi    }else{
6934826ab45Sandi        $cache = 'cache';
6940cecf9d5Sandi    }
6950cecf9d5Sandi
6960cecf9d5Sandi    // Check whether this is a local or remote image
6973e7e6067SKlap-in    if ( media_isexternal($src) ) {
6984826ab45Sandi        $call = 'externalmedia';
6990cecf9d5Sandi    } else {
7004826ab45Sandi        $call = 'internalmedia';
7010cecf9d5Sandi    }
7020cecf9d5Sandi
7030cecf9d5Sandi    $params = array(
7040cecf9d5Sandi        'type'=>$call,
7054826ab45Sandi        'src'=>$src,
7060cecf9d5Sandi        'title'=>$link[1],
7070cecf9d5Sandi        'align'=>$align,
7084826ab45Sandi        'width'=>$w,
7094826ab45Sandi        'height'=>$h,
7100cecf9d5Sandi        'cache'=>$cache,
711dc673a5bSjoe.lapp        'linking'=>$linking,
7120cecf9d5Sandi    );
7130cecf9d5Sandi
7140cecf9d5Sandi    return $params;
7150cecf9d5Sandi}
7160cecf9d5Sandi
7170cecf9d5Sandi//------------------------------------------------------------------------
71826e22ab8SChristopher Smithinterface Doku_Handler_CallWriter_Interface {
71926e22ab8SChristopher Smith    public function writeCall($call);
72026e22ab8SChristopher Smith    public function writeCalls($calls);
72126e22ab8SChristopher Smith    public function finalise();
72226e22ab8SChristopher Smith}
72326e22ab8SChristopher Smith
72426e22ab8SChristopher Smithclass Doku_Handler_CallWriter implements Doku_Handler_CallWriter_Interface {
7250cecf9d5Sandi
7260cecf9d5Sandi    var $Handler;
7270cecf9d5Sandi
72842ea7f44SGerrit Uitslag    /**
72942ea7f44SGerrit Uitslag     * @param Doku_Handler $Handler
73042ea7f44SGerrit Uitslag     */
73126e22ab8SChristopher Smith    function __construct(Doku_Handler $Handler) {
73226e22ab8SChristopher Smith        $this->Handler = $Handler;
7330cecf9d5Sandi    }
7340cecf9d5Sandi
7350cecf9d5Sandi    function writeCall($call) {
7360cecf9d5Sandi        $this->Handler->calls[] = $call;
7370cecf9d5Sandi    }
7380cecf9d5Sandi
7390cecf9d5Sandi    function writeCalls($calls) {
7400cecf9d5Sandi        $this->Handler->calls = array_merge($this->Handler->calls, $calls);
7410cecf9d5Sandi    }
742f4f02a0fSchris
743f4f02a0fSchris    // function is required, but since this call writer is first/highest in
744f4f02a0fSchris    // the chain it is not required to do anything
745f4f02a0fSchris    function finalise() {
7463893df8eSChristopher Smith        unset($this->Handler);
747f4f02a0fSchris    }
7480cecf9d5Sandi}
7490cecf9d5Sandi
7500cecf9d5Sandi//------------------------------------------------------------------------
7515587e44cSchris/**
7525587e44cSchris * Generic call writer class to handle nesting of rendering instructions
7535587e44cSchris * within a render instruction. Also see nest() method of renderer base class
7545587e44cSchris *
7555587e44cSchris * @author    Chris Smith <chris@jalakai.co.uk>
7565587e44cSchris */
75726e22ab8SChristopher Smithclass Doku_Handler_Nest implements Doku_Handler_CallWriter_Interface {
7585587e44cSchris
7595587e44cSchris    var $CallWriter;
7605587e44cSchris    var $calls = array();
7615587e44cSchris
7625587e44cSchris    var $closingInstruction;
7635587e44cSchris
7645587e44cSchris    /**
7655587e44cSchris     * constructor
7665587e44cSchris     *
76742ea7f44SGerrit Uitslag     * @param  Doku_Handler_CallWriter $CallWriter     the renderers current call writer
7685587e44cSchris     * @param  string     $close          closing instruction name, this is required to properly terminate the
7695587e44cSchris     *                                    syntax mode if the document ends without a closing pattern
7705587e44cSchris     */
77126e22ab8SChristopher Smith    function __construct(Doku_Handler_CallWriter_Interface $CallWriter, $close="nest_close") {
77226e22ab8SChristopher Smith        $this->CallWriter = $CallWriter;
7735587e44cSchris
7745587e44cSchris        $this->closingInstruction = $close;
7755587e44cSchris    }
7765587e44cSchris
7775587e44cSchris    function writeCall($call) {
7785587e44cSchris        $this->calls[] = $call;
7795587e44cSchris    }
7805587e44cSchris
7815587e44cSchris    function writeCalls($calls) {
7825587e44cSchris        $this->calls = array_merge($this->calls, $calls);
7835587e44cSchris    }
7845587e44cSchris
7855587e44cSchris    function finalise() {
7865587e44cSchris        $last_call = end($this->calls);
7875587e44cSchris        $this->writeCall(array($this->closingInstruction,array(), $last_call[2]));
7885587e44cSchris
7895587e44cSchris        $this->process();
7905587e44cSchris        $this->CallWriter->finalise();
7913893df8eSChristopher Smith        unset($this->CallWriter);
7925587e44cSchris    }
7935587e44cSchris
7945587e44cSchris    function process() {
79541624b31SChris Smith        // merge consecutive cdata
79641624b31SChris Smith        $unmerged_calls = $this->calls;
79741624b31SChris Smith        $this->calls = array();
79841624b31SChris Smith
79941624b31SChris Smith        foreach ($unmerged_calls as $call) $this->addCall($call);
80041624b31SChris Smith
8015587e44cSchris        $first_call = reset($this->calls);
8025587e44cSchris        $this->CallWriter->writeCall(array("nest", array($this->calls), $first_call[2]));
8035587e44cSchris    }
80441624b31SChris Smith
80541624b31SChris Smith    function addCall($call) {
80641624b31SChris Smith        $key = count($this->calls);
80741624b31SChris Smith        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
80841624b31SChris Smith            $this->calls[$key-1][1][0] .= $call[1][0];
80973c47f3dSChris Smith        } else if ($call[0] == 'eol') {
81073c47f3dSChris Smith            // do nothing (eol shouldn't be allowed, to counter preformatted fix in #1652 & #1699)
81141624b31SChris Smith        } else {
81241624b31SChris Smith            $this->calls[] = $call;
81341624b31SChris Smith        }
81441624b31SChris Smith    }
8155587e44cSchris}
8165587e44cSchris
81726e22ab8SChristopher Smithclass Doku_Handler_List implements Doku_Handler_CallWriter_Interface {
8180cecf9d5Sandi
8190cecf9d5Sandi    var $CallWriter;
8200cecf9d5Sandi
8210cecf9d5Sandi    var $calls = array();
8220cecf9d5Sandi    var $listCalls = array();
8230cecf9d5Sandi    var $listStack = array();
8240cecf9d5Sandi
82510bcc8aaSChristopher Smith    const NODE = 1;
82610bcc8aaSChristopher Smith
82726e22ab8SChristopher Smith    function __construct(Doku_Handler_CallWriter_Interface $CallWriter) {
82826e22ab8SChristopher Smith        $this->CallWriter = $CallWriter;
8290cecf9d5Sandi    }
8300cecf9d5Sandi
8310cecf9d5Sandi    function writeCall($call) {
8320cecf9d5Sandi        $this->calls[] = $call;
8330cecf9d5Sandi    }
8340cecf9d5Sandi
8350cecf9d5Sandi    // Probably not needed but just in case...
8360cecf9d5Sandi    function writeCalls($calls) {
8370cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
838f4f02a0fSchris#        $this->CallWriter->writeCalls($this->calls);
839f4f02a0fSchris    }
840f4f02a0fSchris
841f4f02a0fSchris    function finalise() {
842f4f02a0fSchris        $last_call = end($this->calls);
843f4f02a0fSchris        $this->writeCall(array('list_close',array(), $last_call[2]));
844f4f02a0fSchris
845f4f02a0fSchris        $this->process();
846f4f02a0fSchris        $this->CallWriter->finalise();
8473893df8eSChristopher Smith        unset($this->CallWriter);
8480cecf9d5Sandi    }
8490cecf9d5Sandi
8500cecf9d5Sandi    //------------------------------------------------------------------------
8510cecf9d5Sandi    function process() {
852f4f02a0fSchris
8530cecf9d5Sandi        foreach ( $this->calls as $call ) {
8540cecf9d5Sandi            switch ($call[0]) {
8550cecf9d5Sandi                case 'list_item':
8560cecf9d5Sandi                    $this->listOpen($call);
8570cecf9d5Sandi                break;
8580cecf9d5Sandi                case 'list_open':
8590cecf9d5Sandi                    $this->listStart($call);
8600cecf9d5Sandi                break;
8610cecf9d5Sandi                case 'list_close':
8620cecf9d5Sandi                    $this->listEnd($call);
8630cecf9d5Sandi                break;
8640cecf9d5Sandi                default:
8650cecf9d5Sandi                    $this->listContent($call);
8660cecf9d5Sandi                break;
8670cecf9d5Sandi            }
8680cecf9d5Sandi        }
8690cecf9d5Sandi
8700cecf9d5Sandi        $this->CallWriter->writeCalls($this->listCalls);
8710cecf9d5Sandi    }
8720cecf9d5Sandi
8730cecf9d5Sandi    //------------------------------------------------------------------------
8740cecf9d5Sandi    function listStart($call) {
8750cecf9d5Sandi        $depth = $this->interpretSyntax($call[1][0], $listType);
8760cecf9d5Sandi
8770cecf9d5Sandi        $this->initialDepth = $depth;
87810bcc8aaSChristopher Smith        //                   array(list type, current depth, index of current listitem_open)
87910bcc8aaSChristopher Smith        $this->listStack[] = array($listType, $depth, 1);
8800cecf9d5Sandi
8810cecf9d5Sandi        $this->listCalls[] = array('list'.$listType.'_open',array(),$call[2]);
8820cecf9d5Sandi        $this->listCalls[] = array('listitem_open',array(1),$call[2]);
8830cecf9d5Sandi        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
8840cecf9d5Sandi    }
8850cecf9d5Sandi
8860cecf9d5Sandi    //------------------------------------------------------------------------
8870cecf9d5Sandi    function listEnd($call) {
88844881bd0Shenning.noren        $closeContent = true;
8890cecf9d5Sandi
8900cecf9d5Sandi        while ( $list = array_pop($this->listStack) ) {
8910cecf9d5Sandi            if ( $closeContent ) {
8920cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
89344881bd0Shenning.noren                $closeContent = false;
8940cecf9d5Sandi            }
8950cecf9d5Sandi            $this->listCalls[] = array('listitem_close',array(),$call[2]);
8960cecf9d5Sandi            $this->listCalls[] = array('list'.$list[0].'_close', array(), $call[2]);
8970cecf9d5Sandi        }
8980cecf9d5Sandi    }
8990cecf9d5Sandi
9000cecf9d5Sandi    //------------------------------------------------------------------------
9010cecf9d5Sandi    function listOpen($call) {
9020cecf9d5Sandi        $depth = $this->interpretSyntax($call[1][0], $listType);
9030cecf9d5Sandi        $end = end($this->listStack);
90410bcc8aaSChristopher Smith        $key = key($this->listStack);
9050cecf9d5Sandi
9060cecf9d5Sandi        // Not allowed to be shallower than initialDepth
9070cecf9d5Sandi        if ( $depth < $this->initialDepth ) {
9080cecf9d5Sandi            $depth = $this->initialDepth;
9090cecf9d5Sandi        }
9100cecf9d5Sandi
9110cecf9d5Sandi        //------------------------------------------------------------------------
9120cecf9d5Sandi        if ( $depth == $end[1] ) {
9130cecf9d5Sandi
9140cecf9d5Sandi            // Just another item in the list...
9150cecf9d5Sandi            if ( $listType == $end[0] ) {
9160cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9170cecf9d5Sandi                $this->listCalls[] = array('listitem_close',array(),$call[2]);
9180cecf9d5Sandi                $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
9190cecf9d5Sandi                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9200cecf9d5Sandi
92110bcc8aaSChristopher Smith                // new list item, update list stack's index into current listitem_open
92210bcc8aaSChristopher Smith                $this->listStack[$key][2] = count($this->listCalls) - 2;
92310bcc8aaSChristopher Smith
9240cecf9d5Sandi            // Switched list type...
9250cecf9d5Sandi            } else {
9260cecf9d5Sandi
9270cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9280cecf9d5Sandi                $this->listCalls[] = array('listitem_close',array(),$call[2]);
9290cecf9d5Sandi                $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
9300cecf9d5Sandi                $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9310cecf9d5Sandi                $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9320cecf9d5Sandi                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9330cecf9d5Sandi
9340cecf9d5Sandi                array_pop($this->listStack);
93510bcc8aaSChristopher Smith                $this->listStack[] = array($listType, $depth, count($this->listCalls) - 2);
9360cecf9d5Sandi            }
9370cecf9d5Sandi
9380cecf9d5Sandi        //------------------------------------------------------------------------
9390cecf9d5Sandi        // Getting deeper...
9400cecf9d5Sandi        } else if ( $depth > $end[1] ) {
9410cecf9d5Sandi
9420cecf9d5Sandi            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9430cecf9d5Sandi            $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9440cecf9d5Sandi            $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9450cecf9d5Sandi            $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9460cecf9d5Sandi
94710bcc8aaSChristopher Smith            // set the node/leaf state of this item's parent listitem_open to NODE
94810bcc8aaSChristopher Smith            $this->listCalls[$this->listStack[$key][2]][1][1] = self::NODE;
94910bcc8aaSChristopher Smith
95010bcc8aaSChristopher Smith            $this->listStack[] = array($listType, $depth, count($this->listCalls) - 2);
9510cecf9d5Sandi
9520cecf9d5Sandi        //------------------------------------------------------------------------
9530cecf9d5Sandi        // Getting shallower ( $depth < $end[1] )
9540cecf9d5Sandi        } else {
9550cecf9d5Sandi            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9560cecf9d5Sandi            $this->listCalls[] = array('listitem_close',array(),$call[2]);
9570cecf9d5Sandi            $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
9580cecf9d5Sandi
9590cecf9d5Sandi            // Throw away the end - done
9600cecf9d5Sandi            array_pop($this->listStack);
9610cecf9d5Sandi
9620cecf9d5Sandi            while (1) {
9630cecf9d5Sandi                $end = end($this->listStack);
96410bcc8aaSChristopher Smith                $key = key($this->listStack);
9650cecf9d5Sandi
9660cecf9d5Sandi                if ( $end[1] <= $depth ) {
9670cecf9d5Sandi
9680cecf9d5Sandi                    // Normalize depths
9690cecf9d5Sandi                    $depth = $end[1];
9700cecf9d5Sandi
9710cecf9d5Sandi                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
9720cecf9d5Sandi
9730cecf9d5Sandi                    if ( $end[0] == $listType ) {
9740cecf9d5Sandi                        $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
9750cecf9d5Sandi                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9760cecf9d5Sandi
97710bcc8aaSChristopher Smith                        // new list item, update list stack's index into current listitem_open
97810bcc8aaSChristopher Smith                        $this->listStack[$key][2] = count($this->listCalls) - 2;
97910bcc8aaSChristopher Smith
9800cecf9d5Sandi                    } else {
9810cecf9d5Sandi                        // Switching list type...
9820cecf9d5Sandi                        $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
9830cecf9d5Sandi                        $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9840cecf9d5Sandi                        $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9850cecf9d5Sandi                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9860cecf9d5Sandi
9870cecf9d5Sandi                        array_pop($this->listStack);
98810bcc8aaSChristopher Smith                        $this->listStack[] = array($listType, $depth, count($this->listCalls) - 2);
9890cecf9d5Sandi                    }
9900cecf9d5Sandi
9910cecf9d5Sandi                    break;
9920cecf9d5Sandi
9930cecf9d5Sandi                // Haven't dropped down far enough yet.... ( $end[1] > $depth )
9940cecf9d5Sandi                } else {
9950cecf9d5Sandi
9960cecf9d5Sandi                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
9970cecf9d5Sandi                    $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
9980cecf9d5Sandi
9990cecf9d5Sandi                    array_pop($this->listStack);
10000cecf9d5Sandi
10010cecf9d5Sandi                }
10020cecf9d5Sandi
10030cecf9d5Sandi            }
10040cecf9d5Sandi
10050cecf9d5Sandi        }
10060cecf9d5Sandi    }
10070cecf9d5Sandi
10080cecf9d5Sandi    //------------------------------------------------------------------------
10090cecf9d5Sandi    function listContent($call) {
10100cecf9d5Sandi        $this->listCalls[] = $call;
10110cecf9d5Sandi    }
10120cecf9d5Sandi
10130cecf9d5Sandi    //------------------------------------------------------------------------
10140cecf9d5Sandi    function interpretSyntax($match, & $type) {
10150cecf9d5Sandi        if ( substr($match,-1) == '*' ) {
10160cecf9d5Sandi            $type = 'u';
10170cecf9d5Sandi        } else {
10180cecf9d5Sandi            $type = 'o';
10190cecf9d5Sandi        }
10204b7f9e70STom N Harris        // Is the +1 needed? It used to be count(explode(...))
10214b7f9e70STom N Harris        // but I don't think the number is seen outside this handler
10224b7f9e70STom N Harris        return substr_count(str_replace("\t",'  ',$match), '  ') + 1;
10230cecf9d5Sandi    }
10240cecf9d5Sandi}
10250cecf9d5Sandi
10260cecf9d5Sandi//------------------------------------------------------------------------
102726e22ab8SChristopher Smithclass Doku_Handler_Preformatted implements Doku_Handler_CallWriter_Interface {
10280cecf9d5Sandi
10290cecf9d5Sandi    var $CallWriter;
10300cecf9d5Sandi
10310cecf9d5Sandi    var $calls = array();
10320cecf9d5Sandi    var $pos;
10330cecf9d5Sandi    var $text ='';
10340cecf9d5Sandi
10350cecf9d5Sandi
10360cecf9d5Sandi
103726e22ab8SChristopher Smith    function __construct(Doku_Handler_CallWriter_Interface $CallWriter) {
103826e22ab8SChristopher Smith        $this->CallWriter = $CallWriter;
10390cecf9d5Sandi    }
10400cecf9d5Sandi
10410cecf9d5Sandi    function writeCall($call) {
10420cecf9d5Sandi        $this->calls[] = $call;
10430cecf9d5Sandi    }
10440cecf9d5Sandi
10450cecf9d5Sandi    // Probably not needed but just in case...
10460cecf9d5Sandi    function writeCalls($calls) {
10470cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1048f4f02a0fSchris#        $this->CallWriter->writeCalls($this->calls);
1049f4f02a0fSchris    }
1050f4f02a0fSchris
1051f4f02a0fSchris    function finalise() {
1052f4f02a0fSchris        $last_call = end($this->calls);
1053f4f02a0fSchris        $this->writeCall(array('preformatted_end',array(), $last_call[2]));
1054f4f02a0fSchris
1055f4f02a0fSchris        $this->process();
1056f4f02a0fSchris        $this->CallWriter->finalise();
10573893df8eSChristopher Smith        unset($this->CallWriter);
10580cecf9d5Sandi    }
10590cecf9d5Sandi
10600cecf9d5Sandi    function process() {
10610cecf9d5Sandi        foreach ( $this->calls as $call ) {
10620cecf9d5Sandi            switch ($call[0]) {
10630cecf9d5Sandi                case 'preformatted_start':
10640cecf9d5Sandi                    $this->pos = $call[2];
10650cecf9d5Sandi                break;
10660cecf9d5Sandi                case 'preformatted_newline':
10670cecf9d5Sandi                    $this->text .= "\n";
10680cecf9d5Sandi                break;
10690cecf9d5Sandi                case 'preformatted_content':
10700cecf9d5Sandi                    $this->text .= $call[1][0];
10710cecf9d5Sandi                break;
10720cecf9d5Sandi                case 'preformatted_end':
107393a34bf3SChris Smith                    if (trim($this->text)) {
10740cecf9d5Sandi                        $this->CallWriter->writeCall(array('preformatted',array($this->text),$this->pos));
107593a34bf3SChris Smith                    }
107695c19ce7SChris Smith                    // see FS#1699 & FS#1652, add 'eol' instructions to ensure proper triggering of following p_open
107795c19ce7SChris Smith                    $this->CallWriter->writeCall(array('eol',array(),$this->pos));
107895c19ce7SChris Smith                    $this->CallWriter->writeCall(array('eol',array(),$this->pos));
10790cecf9d5Sandi                break;
10800cecf9d5Sandi            }
10810cecf9d5Sandi        }
10820cecf9d5Sandi    }
1083f4f02a0fSchris
10840cecf9d5Sandi}
10850cecf9d5Sandi
10860cecf9d5Sandi//------------------------------------------------------------------------
108726e22ab8SChristopher Smithclass Doku_Handler_Quote implements Doku_Handler_CallWriter_Interface {
10880cecf9d5Sandi
10890cecf9d5Sandi    var $CallWriter;
10900cecf9d5Sandi
10910cecf9d5Sandi    var $calls = array();
10920cecf9d5Sandi
10930cecf9d5Sandi    var $quoteCalls = array();
10940cecf9d5Sandi
109526e22ab8SChristopher Smith    function __construct(Doku_Handler_CallWriter_Interface $CallWriter) {
109626e22ab8SChristopher Smith        $this->CallWriter = $CallWriter;
10970cecf9d5Sandi    }
10980cecf9d5Sandi
10990cecf9d5Sandi    function writeCall($call) {
11000cecf9d5Sandi        $this->calls[] = $call;
11010cecf9d5Sandi    }
11020cecf9d5Sandi
11030cecf9d5Sandi    // Probably not needed but just in case...
11040cecf9d5Sandi    function writeCalls($calls) {
11050cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1106f4f02a0fSchris    }
1107f4f02a0fSchris
1108f4f02a0fSchris    function finalise() {
1109f4f02a0fSchris        $last_call = end($this->calls);
1110f4f02a0fSchris        $this->writeCall(array('quote_end',array(), $last_call[2]));
1111f4f02a0fSchris
1112f4f02a0fSchris        $this->process();
1113f4f02a0fSchris        $this->CallWriter->finalise();
11143893df8eSChristopher Smith        unset($this->CallWriter);
11150cecf9d5Sandi    }
11160cecf9d5Sandi
11170cecf9d5Sandi    function process() {
11180cecf9d5Sandi
11190cecf9d5Sandi        $quoteDepth = 1;
11200cecf9d5Sandi
11210cecf9d5Sandi        foreach ( $this->calls as $call ) {
11220cecf9d5Sandi            switch ($call[0]) {
11230cecf9d5Sandi
11240cecf9d5Sandi                case 'quote_start':
11250cecf9d5Sandi
11260cecf9d5Sandi                    $this->quoteCalls[] = array('quote_open',array(),$call[2]);
11270cecf9d5Sandi
11280cecf9d5Sandi                case 'quote_newline':
11290cecf9d5Sandi
11300cecf9d5Sandi                    $quoteLength = $this->getDepth($call[1][0]);
11310cecf9d5Sandi
11320cecf9d5Sandi                    if ( $quoteLength > $quoteDepth ) {
11330cecf9d5Sandi                        $quoteDiff = $quoteLength - $quoteDepth;
11340cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11350cecf9d5Sandi                            $this->quoteCalls[] = array('quote_open',array(),$call[2]);
11360cecf9d5Sandi                        }
11370cecf9d5Sandi                    } else if ( $quoteLength < $quoteDepth ) {
11380cecf9d5Sandi                        $quoteDiff = $quoteDepth - $quoteLength;
11390cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11400cecf9d5Sandi                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11410cecf9d5Sandi                        }
114226426c64Schris                    } else {
114326426c64Schris                        if ($call[0] != 'quote_start') $this->quoteCalls[] = array('linebreak',array(),$call[2]);
11440cecf9d5Sandi                    }
11450cecf9d5Sandi
11460cecf9d5Sandi                    $quoteDepth = $quoteLength;
11470cecf9d5Sandi
11480cecf9d5Sandi                break;
11490cecf9d5Sandi
11500cecf9d5Sandi                case 'quote_end':
11510cecf9d5Sandi
11520cecf9d5Sandi                    if ( $quoteDepth > 1 ) {
11530cecf9d5Sandi                        $quoteDiff = $quoteDepth - 1;
11540cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11550cecf9d5Sandi                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11560cecf9d5Sandi                        }
11570cecf9d5Sandi                    }
11580cecf9d5Sandi
11590cecf9d5Sandi                    $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11600cecf9d5Sandi
11610cecf9d5Sandi                    $this->CallWriter->writeCalls($this->quoteCalls);
11620cecf9d5Sandi                break;
11630cecf9d5Sandi
11640cecf9d5Sandi                default:
11650cecf9d5Sandi                    $this->quoteCalls[] = $call;
11660cecf9d5Sandi                break;
11670cecf9d5Sandi            }
11680cecf9d5Sandi        }
11690cecf9d5Sandi    }
11700cecf9d5Sandi
11710cecf9d5Sandi    function getDepth($marker) {
11720cecf9d5Sandi        preg_match('/>{1,}/', $marker, $matches);
11730cecf9d5Sandi        $quoteLength = strlen($matches[0]);
11740cecf9d5Sandi        return $quoteLength;
11750cecf9d5Sandi    }
11760cecf9d5Sandi}
11770cecf9d5Sandi
11780cecf9d5Sandi//------------------------------------------------------------------------
117926e22ab8SChristopher Smithclass Doku_Handler_Table implements Doku_Handler_CallWriter_Interface {
11800cecf9d5Sandi
11810cecf9d5Sandi    var $CallWriter;
11820cecf9d5Sandi
11830cecf9d5Sandi    var $calls = array();
11840cecf9d5Sandi    var $tableCalls = array();
11850cecf9d5Sandi    var $maxCols = 0;
11860cecf9d5Sandi    var $maxRows = 1;
11870cecf9d5Sandi    var $currentCols = 0;
118844881bd0Shenning.noren    var $firstCell = false;
11890cecf9d5Sandi    var $lastCellType = 'tablecell';
11909060b8b0SChristopher Smith    var $inTableHead = true;
1191cef031c1SChristopher Smith    var $currentRow = array('tableheader' => 0, 'tablecell' => 0);
11929060b8b0SChristopher Smith    var $countTableHeadRows = 0;
11930cecf9d5Sandi
119426e22ab8SChristopher Smith    function __construct(Doku_Handler_CallWriter_Interface $CallWriter) {
119526e22ab8SChristopher Smith        $this->CallWriter = $CallWriter;
11960cecf9d5Sandi    }
11970cecf9d5Sandi
11980cecf9d5Sandi    function writeCall($call) {
11990cecf9d5Sandi        $this->calls[] = $call;
12000cecf9d5Sandi    }
12010cecf9d5Sandi
12020cecf9d5Sandi    // Probably not needed but just in case...
12030cecf9d5Sandi    function writeCalls($calls) {
12040cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1205f4f02a0fSchris    }
1206f4f02a0fSchris
1207f4f02a0fSchris    function finalise() {
1208f4f02a0fSchris        $last_call = end($this->calls);
1209f4f02a0fSchris        $this->writeCall(array('table_end',array(), $last_call[2]));
1210f4f02a0fSchris
1211f4f02a0fSchris        $this->process();
1212f4f02a0fSchris        $this->CallWriter->finalise();
12133893df8eSChristopher Smith        unset($this->CallWriter);
12140cecf9d5Sandi    }
12150cecf9d5Sandi
12160cecf9d5Sandi    //------------------------------------------------------------------------
12170cecf9d5Sandi    function process() {
12180cecf9d5Sandi        foreach ( $this->calls as $call ) {
12190cecf9d5Sandi            switch ( $call[0] ) {
12200cecf9d5Sandi                case 'table_start':
12210cecf9d5Sandi                    $this->tableStart($call);
12220cecf9d5Sandi                break;
12230cecf9d5Sandi                case 'table_row':
1224aa92c4ccSAdrian Lang                    $this->tableRowClose($call);
12250cecf9d5Sandi                    $this->tableRowOpen(array('tablerow_open',$call[1],$call[2]));
12260cecf9d5Sandi                break;
12270cecf9d5Sandi                case 'tableheader':
12280cecf9d5Sandi                case 'tablecell':
12290cecf9d5Sandi                    $this->tableCell($call);
12300cecf9d5Sandi                break;
12310cecf9d5Sandi                case 'table_end':
1232aa92c4ccSAdrian Lang                    $this->tableRowClose($call);
12330cecf9d5Sandi                    $this->tableEnd($call);
12340cecf9d5Sandi                break;
12350cecf9d5Sandi                default:
12360cecf9d5Sandi                    $this->tableDefault($call);
12370cecf9d5Sandi                break;
12380cecf9d5Sandi            }
12390cecf9d5Sandi        }
12400cecf9d5Sandi        $this->CallWriter->writeCalls($this->tableCalls);
12410cecf9d5Sandi    }
12420cecf9d5Sandi
12430cecf9d5Sandi    function tableStart($call) {
124490df9a4dSAdrian Lang        $this->tableCalls[] = array('table_open',$call[1],$call[2]);
12450cecf9d5Sandi        $this->tableCalls[] = array('tablerow_open',array(),$call[2]);
124644881bd0Shenning.noren        $this->firstCell = true;
12470cecf9d5Sandi    }
12480cecf9d5Sandi
12490cecf9d5Sandi    function tableEnd($call) {
125007c2b1c7SAdrian Lang        $this->tableCalls[] = array('table_close',$call[1],$call[2]);
12510cecf9d5Sandi        $this->finalizeTable();
12520cecf9d5Sandi    }
12530cecf9d5Sandi
12540cecf9d5Sandi    function tableRowOpen($call) {
12550cecf9d5Sandi        $this->tableCalls[] = $call;
12560cecf9d5Sandi        $this->currentCols = 0;
125744881bd0Shenning.noren        $this->firstCell = true;
12580cecf9d5Sandi        $this->lastCellType = 'tablecell';
12590cecf9d5Sandi        $this->maxRows++;
1260cef031c1SChristopher Smith        if ($this->inTableHead) {
1261cef031c1SChristopher Smith            $this->currentRow = array('tablecell' => 0, 'tableheader' => 0);
1262cef031c1SChristopher Smith        }
12630cecf9d5Sandi    }
12640cecf9d5Sandi
12650cecf9d5Sandi    function tableRowClose($call) {
1266cef031c1SChristopher Smith        if ($this->inTableHead && ($this->inTableHead = $this->isTableHeadRow())) {
12679060b8b0SChristopher Smith            $this->countTableHeadRows++;
12689060b8b0SChristopher Smith        }
12690cecf9d5Sandi        // Strip off final cell opening and anything after it
12700cecf9d5Sandi        while ( $discard = array_pop($this->tableCalls ) ) {
12710cecf9d5Sandi
12720cecf9d5Sandi            if ( $discard[0] == 'tablecell_open' || $discard[0] == 'tableheader_open') {
12730cecf9d5Sandi                break;
12740cecf9d5Sandi            }
1275cef031c1SChristopher Smith            if (!empty($this->currentRow[$discard[0]])) {
1276cef031c1SChristopher Smith                $this->currentRow[$discard[0]]--;
1277cef031c1SChristopher Smith            }
12780cecf9d5Sandi        }
1279aa92c4ccSAdrian Lang        $this->tableCalls[] = array('tablerow_close', array(), $call[2]);
12800cecf9d5Sandi
12810cecf9d5Sandi        if ( $this->currentCols > $this->maxCols ) {
12820cecf9d5Sandi            $this->maxCols = $this->currentCols;
12830cecf9d5Sandi        }
12840cecf9d5Sandi    }
12850cecf9d5Sandi
1286cef031c1SChristopher Smith    function isTableHeadRow() {
1287cef031c1SChristopher Smith        $td = $this->currentRow['tablecell'];
1288cef031c1SChristopher Smith        $th = $this->currentRow['tableheader'];
1289cef031c1SChristopher Smith
1290cef031c1SChristopher Smith        if (!$th || $td > 2) return false;
1291cef031c1SChristopher Smith        if (2*$td > $th) return false;
1292cef031c1SChristopher Smith
1293cef031c1SChristopher Smith        return true;
1294cef031c1SChristopher Smith    }
1295cef031c1SChristopher Smith
12960cecf9d5Sandi    function tableCell($call) {
1297cef031c1SChristopher Smith        if ($this->inTableHead) {
1298cef031c1SChristopher Smith            $this->currentRow[$call[0]]++;
12999060b8b0SChristopher Smith        }
13000cecf9d5Sandi        if ( !$this->firstCell ) {
13010cecf9d5Sandi
13020cecf9d5Sandi            // Increase the span
13030cecf9d5Sandi            $lastCall = end($this->tableCalls);
13040cecf9d5Sandi
13050cecf9d5Sandi            // A cell call which follows an open cell means an empty cell so span
13060cecf9d5Sandi            if ( $lastCall[0] == 'tablecell_open' || $lastCall[0] == 'tableheader_open' ) {
13070cecf9d5Sandi                 $this->tableCalls[] = array('colspan',array(),$call[2]);
13080cecf9d5Sandi
13090cecf9d5Sandi            }
13100cecf9d5Sandi
13110cecf9d5Sandi            $this->tableCalls[] = array($this->lastCellType.'_close',array(),$call[2]);
13120ea51e63SMatt Perry            $this->tableCalls[] = array($call[0].'_open',array(1,null,1),$call[2]);
13130cecf9d5Sandi            $this->lastCellType = $call[0];
13140cecf9d5Sandi
13150cecf9d5Sandi        } else {
13160cecf9d5Sandi
13170ea51e63SMatt Perry            $this->tableCalls[] = array($call[0].'_open',array(1,null,1),$call[2]);
13180cecf9d5Sandi            $this->lastCellType = $call[0];
131944881bd0Shenning.noren            $this->firstCell = false;
13200cecf9d5Sandi
13210cecf9d5Sandi        }
13220cecf9d5Sandi
13230cecf9d5Sandi        $this->currentCols++;
13240cecf9d5Sandi    }
13250cecf9d5Sandi
13260cecf9d5Sandi    function tableDefault($call) {
13270cecf9d5Sandi        $this->tableCalls[] = $call;
13280cecf9d5Sandi    }
13290cecf9d5Sandi
13300cecf9d5Sandi    function finalizeTable() {
13310cecf9d5Sandi
13320cecf9d5Sandi        // Add the max cols and rows to the table opening
13330cecf9d5Sandi        if ( $this->tableCalls[0][0] == 'table_open' ) {
13340cecf9d5Sandi            // Adjust to num cols not num col delimeters
13350cecf9d5Sandi            $this->tableCalls[0][1][] = $this->maxCols - 1;
13360cecf9d5Sandi            $this->tableCalls[0][1][] = $this->maxRows;
133790df9a4dSAdrian Lang            $this->tableCalls[0][1][] = array_shift($this->tableCalls[0][1]);
13380cecf9d5Sandi        } else {
13390cecf9d5Sandi            trigger_error('First element in table call list is not table_open');
13400cecf9d5Sandi        }
13410cecf9d5Sandi
13420cecf9d5Sandi        $lastRow = 0;
13430cecf9d5Sandi        $lastCell = 0;
134425b97867Shakan.sandell        $cellKey = array();
13450cecf9d5Sandi        $toDelete = array();
13460cecf9d5Sandi
1347cef031c1SChristopher Smith        // if still in tableheader, then there can be no table header
1348cef031c1SChristopher Smith        // as all rows can't be within <THEAD>
1349cef031c1SChristopher Smith        if ($this->inTableHead) {
1350cef031c1SChristopher Smith            $this->inTableHead = false;
1351cef031c1SChristopher Smith            $this->countTableHeadRows = 0;
1352cef031c1SChristopher Smith        }
1353cef031c1SChristopher Smith
13540cecf9d5Sandi        // Look for the colspan elements and increment the colspan on the
13550cecf9d5Sandi        // previous non-empty opening cell. Once done, delete all the cells
13560cecf9d5Sandi        // that contain colspans
13576606d6fcSAdrian Lang        for ($key = 0 ; $key < count($this->tableCalls) ; ++$key) {
13586606d6fcSAdrian Lang            $call = $this->tableCalls[$key];
13590cecf9d5Sandi
1360aa92c4ccSAdrian Lang            switch ($call[0]) {
13619060b8b0SChristopher Smith                case 'table_open' :
13629060b8b0SChristopher Smith                    if($this->countTableHeadRows) {
13639060b8b0SChristopher Smith                        array_splice($this->tableCalls, $key+1, 0, array(
13649060b8b0SChristopher Smith                              array('tablethead_open', array(), $call[2]))
13659060b8b0SChristopher Smith                        );
13669060b8b0SChristopher Smith                    }
13679060b8b0SChristopher Smith                    break;
13689060b8b0SChristopher Smith
1369aa92c4ccSAdrian Lang                case 'tablerow_open':
13700cecf9d5Sandi
137125b97867Shakan.sandell                    $lastRow++;
137225b97867Shakan.sandell                    $lastCell = 0;
1373aa92c4ccSAdrian Lang                    break;
13740cecf9d5Sandi
1375aa92c4ccSAdrian Lang                case 'tablecell_open':
1376aa92c4ccSAdrian Lang                case 'tableheader_open':
13770cecf9d5Sandi
137825b97867Shakan.sandell                    $lastCell++;
137925b97867Shakan.sandell                    $cellKey[$lastRow][$lastCell] = $key;
1380aa92c4ccSAdrian Lang                    break;
13810cecf9d5Sandi
1382aa92c4ccSAdrian Lang                case 'table_align':
13830cecf9d5Sandi
1384e03b8b2eSAdrian Lang                    $prev = in_array($this->tableCalls[$key-1][0], array('tablecell_open', 'tableheader_open'));
1385e03b8b2eSAdrian Lang                    $next = in_array($this->tableCalls[$key+1][0], array('tablecell_close', 'tableheader_close'));
1386e03b8b2eSAdrian Lang                    // If the cell is empty, align left
1387e03b8b2eSAdrian Lang                    if ($prev && $next) {
1388e03b8b2eSAdrian Lang                        $this->tableCalls[$key-1][1][1] = 'left';
1389e03b8b2eSAdrian Lang
13900cecf9d5Sandi                    // If the previous element was a cell open, align right
1391e03b8b2eSAdrian Lang                    } elseif ($prev) {
13920cecf9d5Sandi                        $this->tableCalls[$key-1][1][1] = 'right';
13930cecf9d5Sandi
1394e03b8b2eSAdrian Lang                    // If the next element is the close of an element, align either center or left
1395e03b8b2eSAdrian Lang                    } elseif ( $next) {
139625b97867Shakan.sandell                        if ( $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] == 'right' ) {
139725b97867Shakan.sandell                            $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] = 'center';
13980cecf9d5Sandi                        } else {
139925b97867Shakan.sandell                            $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] = 'left';
14000cecf9d5Sandi                        }
14010cecf9d5Sandi
14020cecf9d5Sandi                    }
14030cecf9d5Sandi
14040cecf9d5Sandi                    // Now convert the whitespace back to cdata
14050cecf9d5Sandi                    $this->tableCalls[$key][0] = 'cdata';
1406aa92c4ccSAdrian Lang                    break;
14070cecf9d5Sandi
1408aa92c4ccSAdrian Lang                case 'colspan':
14090cecf9d5Sandi
141044881bd0Shenning.noren                    $this->tableCalls[$key-1][1][0] = false;
14110cecf9d5Sandi
141225b97867Shakan.sandell                    for($i = $key-2; $i >= $cellKey[$lastRow][1]; $i--) {
14130cecf9d5Sandi
14140cecf9d5Sandi                        if ( $this->tableCalls[$i][0] == 'tablecell_open' || $this->tableCalls[$i][0] == 'tableheader_open' ) {
14150cecf9d5Sandi
141644881bd0Shenning.noren                            if ( false !== $this->tableCalls[$i][1][0] ) {
14170cecf9d5Sandi                                $this->tableCalls[$i][1][0]++;
14180cecf9d5Sandi                                break;
14190cecf9d5Sandi                            }
14200cecf9d5Sandi
14210cecf9d5Sandi                        }
14220cecf9d5Sandi                    }
14230cecf9d5Sandi
14240cecf9d5Sandi                    $toDelete[] = $key-1;
14250cecf9d5Sandi                    $toDelete[] = $key;
14260cecf9d5Sandi                    $toDelete[] = $key+1;
1427aa92c4ccSAdrian Lang                    break;
142825b97867Shakan.sandell
1429aa92c4ccSAdrian Lang                case 'rowspan':
143025b97867Shakan.sandell
143125b97867Shakan.sandell                    if ( $this->tableCalls[$key-1][0] == 'cdata' ) {
143225b97867Shakan.sandell                        // ignore rowspan if previous call was cdata (text mixed with :::) we don't have to check next call as that wont match regex
143325b97867Shakan.sandell                        $this->tableCalls[$key][0] = 'cdata';
143425b97867Shakan.sandell
143525b97867Shakan.sandell                    } else {
143625b97867Shakan.sandell
14376606d6fcSAdrian Lang                        $spanning_cell = null;
14389060b8b0SChristopher Smith
14399060b8b0SChristopher Smith                        // can't cross thead/tbody boundary
14409060b8b0SChristopher Smith                        if (!$this->countTableHeadRows || ($lastRow-1 != $this->countTableHeadRows)) {
144125b97867Shakan.sandell                            for($i = $lastRow-1; $i > 0; $i--) {
144225b97867Shakan.sandell
144325b97867Shakan.sandell                                if ( $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tablecell_open' || $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tableheader_open' ) {
144425b97867Shakan.sandell
14456606d6fcSAdrian Lang                                    if ($this->tableCalls[$cellKey[$i][$lastCell]][1][2] >= $lastRow - $i) {
14466606d6fcSAdrian Lang                                        $spanning_cell = $i;
144725b97867Shakan.sandell                                        break;
144825b97867Shakan.sandell                                    }
144925b97867Shakan.sandell
145025b97867Shakan.sandell                                }
145125b97867Shakan.sandell                            }
14529060b8b0SChristopher Smith                        }
14536606d6fcSAdrian Lang                        if (is_null($spanning_cell)) {
14546606d6fcSAdrian Lang                            // No spanning cell found, so convert this cell to
14556606d6fcSAdrian Lang                            // an empty one to avoid broken tables
14566a7e97d5SGerrit Uitslag                            $this->tableCalls[$key][0] = 'cdata';
14576a7e97d5SGerrit Uitslag                            $this->tableCalls[$key][1][0] = '';
14586606d6fcSAdrian Lang                            continue;
14596606d6fcSAdrian Lang                        }
14606606d6fcSAdrian Lang                        $this->tableCalls[$cellKey[$spanning_cell][$lastCell]][1][2]++;
14616606d6fcSAdrian Lang
14626606d6fcSAdrian Lang                        $this->tableCalls[$key-1][1][2] = false;
146325b97867Shakan.sandell
146425b97867Shakan.sandell                        $toDelete[] = $key-1;
146525b97867Shakan.sandell                        $toDelete[] = $key;
146625b97867Shakan.sandell                        $toDelete[] = $key+1;
146725b97867Shakan.sandell                    }
1468aa92c4ccSAdrian Lang                    break;
1469aa92c4ccSAdrian Lang
14706606d6fcSAdrian Lang                case 'tablerow_close':
14716606d6fcSAdrian Lang
14726606d6fcSAdrian Lang                    // Fix broken tables by adding missing cells
1473*dbd52c81SAndreas Gohr                    $moreCalls = array();
14746606d6fcSAdrian Lang                    while (++$lastCell < $this->maxCols) {
1475*dbd52c81SAndreas Gohr                        $moreCalls[] = array('tablecell_open', array(1, null, 1), $call[2]);
1476*dbd52c81SAndreas Gohr                        $moreCalls[] = array('cdata', array(''), $call[2]);
1477*dbd52c81SAndreas Gohr                        $moreCalls[] = array('tablecell_close', array(), $call[2]);
1478*dbd52c81SAndreas Gohr                    }
1479*dbd52c81SAndreas Gohr                    $moreCallsLength = count($moreCalls);
1480*dbd52c81SAndreas Gohr                    if($moreCallsLength) {
1481*dbd52c81SAndreas Gohr                        array_splice($this->tableCalls, $key, 0, $moreCalls);
1482*dbd52c81SAndreas Gohr                        $key += $moreCallsLength;
14836606d6fcSAdrian Lang                    }
14846606d6fcSAdrian Lang
14859060b8b0SChristopher Smith                    if($this->countTableHeadRows == $lastRow) {
1486f05a1cc5SGerrit Uitslag                        array_splice($this->tableCalls, $key+1, 0, array(
1487f05a1cc5SGerrit Uitslag                              array('tablethead_close', array(), $call[2])));
1488f05a1cc5SGerrit Uitslag                    }
14896606d6fcSAdrian Lang                    break;
14906606d6fcSAdrian Lang
14910cecf9d5Sandi            }
14920cecf9d5Sandi        }
14930cecf9d5Sandi
14949ab75d9eSAndreas Gohr        // condense cdata
14959ab75d9eSAndreas Gohr        $cnt = count($this->tableCalls);
14969ab75d9eSAndreas Gohr        for( $key = 0; $key < $cnt; $key++){
14979ab75d9eSAndreas Gohr            if($this->tableCalls[$key][0] == 'cdata'){
14989ab75d9eSAndreas Gohr                $ckey = $key;
14999ab75d9eSAndreas Gohr                $key++;
15009ab75d9eSAndreas Gohr                while($this->tableCalls[$key][0] == 'cdata'){
15019ab75d9eSAndreas Gohr                    $this->tableCalls[$ckey][1][0] .= $this->tableCalls[$key][1][0];
15029ab75d9eSAndreas Gohr                    $toDelete[] = $key;
15039ab75d9eSAndreas Gohr                    $key++;
15049ab75d9eSAndreas Gohr                }
15059ab75d9eSAndreas Gohr                continue;
15069ab75d9eSAndreas Gohr            }
15079ab75d9eSAndreas Gohr        }
15089ab75d9eSAndreas Gohr
15090cecf9d5Sandi        foreach ( $toDelete as $delete ) {
15100cecf9d5Sandi            unset($this->tableCalls[$delete]);
15110cecf9d5Sandi        }
15120cecf9d5Sandi        $this->tableCalls = array_values($this->tableCalls);
15130cecf9d5Sandi    }
15140cecf9d5Sandi}
15150cecf9d5Sandi
15160cecf9d5Sandi
15172a27e99aSandi/**
15182a27e99aSandi * Handler for paragraphs
15192a27e99aSandi *
15200b7c14c2Sandi * @author Harry Fuecks <hfuecks@gmail.com>
15212a27e99aSandi */
15220cecf9d5Sandiclass Doku_Handler_Block {
15230cecf9d5Sandi    var $calls = array();
15249569a107SDanny Lin    var $skipEol = false;
152553bfcb59SChristopher Smith    var $inParagraph = false;
15260cecf9d5Sandi
1527af146da0Sandi    // Blocks these should not be inside paragraphs
15280cecf9d5Sandi    var $blockOpen = array(
15290cecf9d5Sandi            'header',
1530df9add72Schris            'listu_open','listo_open','listitem_open','listcontent_open',
1531f05a1cc5SGerrit Uitslag            'table_open','tablerow_open','tablecell_open','tableheader_open','tablethead_open',
15320cecf9d5Sandi            'quote_open',
153376aa94b7Schris            'code','file','hr','preformatted','rss',
153407f89c3cSAnika Henke            'htmlblock','phpblock',
15359569a107SDanny Lin            'footnote_open',
15360cecf9d5Sandi        );
15370cecf9d5Sandi
15380cecf9d5Sandi    var $blockClose = array(
15390cecf9d5Sandi            'header',
1540df9add72Schris            'listu_close','listo_close','listitem_close','listcontent_close',
1541f05a1cc5SGerrit Uitslag            'table_close','tablerow_close','tablecell_close','tableheader_close','tablethead_close',
15420cecf9d5Sandi            'quote_close',
154376aa94b7Schris            'code','file','hr','preformatted','rss',
154407f89c3cSAnika Henke            'htmlblock','phpblock',
15459569a107SDanny Lin            'footnote_close',
15460cecf9d5Sandi        );
15470cecf9d5Sandi
1548af146da0Sandi    // Stacks can contain paragraphs
15490cecf9d5Sandi    var $stackOpen = array(
15509569a107SDanny Lin        'section_open',
15510cecf9d5Sandi        );
15520cecf9d5Sandi
15530cecf9d5Sandi    var $stackClose = array(
15549569a107SDanny Lin        'section_close',
15550cecf9d5Sandi        );
15560cecf9d5Sandi
1557af146da0Sandi
1558af146da0Sandi    /**
1559af146da0Sandi     * Constructor. Adds loaded syntax plugins to the block and stack
1560af146da0Sandi     * arrays
1561af146da0Sandi     *
1562af146da0Sandi     * @author Andreas Gohr <andi@splitbrain.org>
1563af146da0Sandi     */
156426e22ab8SChristopher Smith    function __construct(){
1565af146da0Sandi        global $DOKU_PLUGINS;
1566af146da0Sandi        //check if syntax plugins were loaded
156703c4aec3Schris        if(empty($DOKU_PLUGINS['syntax'])) return;
1568af146da0Sandi        foreach($DOKU_PLUGINS['syntax'] as $n => $p){
1569af146da0Sandi            $ptype = $p->getPType();
1570af146da0Sandi            if($ptype == 'block'){
1571af146da0Sandi                $this->blockOpen[]  = 'plugin_'.$n;
1572af146da0Sandi                $this->blockClose[] = 'plugin_'.$n;
1573af146da0Sandi            }elseif($ptype == 'stack'){
1574af146da0Sandi                $this->stackOpen[]  = 'plugin_'.$n;
1575af146da0Sandi                $this->stackClose[] = 'plugin_'.$n;
1576af146da0Sandi            }
1577af146da0Sandi        }
1578af146da0Sandi    }
1579af146da0Sandi
1580b5a0b131SDanny Lin    function openParagraph($pos){
15819569a107SDanny Lin        if ($this->inParagraph) return;
1582b5a0b131SDanny Lin        $this->calls[] = array('p_open',array(), $pos);
1583b5a0b131SDanny Lin        $this->inParagraph = true;
15849569a107SDanny Lin        $this->skipEol = true;
1585b5a0b131SDanny Lin    }
1586b5a0b131SDanny Lin
15872a27e99aSandi    /**
15882a27e99aSandi     * Close a paragraph if needed
15892a27e99aSandi     *
15902a27e99aSandi     * This function makes sure there are no empty paragraphs on the stack
15912a27e99aSandi     *
15922a27e99aSandi     * @author Andreas Gohr <andi@splitbrain.org>
15932a27e99aSandi     */
1594506ae684Sandi    function closeParagraph($pos){
15959569a107SDanny Lin        if (!$this->inParagraph) return;
1596506ae684Sandi        // look back if there was any content - we don't want empty paragraphs
1597506ae684Sandi        $content = '';
1598faba9a35SAndreas Gohr        $ccount = count($this->calls);
1599faba9a35SAndreas Gohr        for($i=$ccount-1; $i>=0; $i--){
1600506ae684Sandi            if($this->calls[$i][0] == 'p_open'){
1601506ae684Sandi                break;
1602506ae684Sandi            }elseif($this->calls[$i][0] == 'cdata'){
1603506ae684Sandi                $content .= $this->calls[$i][1][0];
1604506ae684Sandi            }else{
1605506ae684Sandi                $content = 'found markup';
1606506ae684Sandi                break;
1607506ae684Sandi            }
1608506ae684Sandi        }
1609506ae684Sandi
1610506ae684Sandi        if(trim($content)==''){
1611506ae684Sandi            //remove the whole paragraph
1612a86cc527SAndreas Gohr            //array_splice($this->calls,$i); // <- this is much slower than the loop below
1613d8f7a7f3SAndreas Gohr            for($x=$ccount; $x>$i; $x--) array_pop($this->calls);
1614506ae684Sandi        }else{
16159569a107SDanny Lin            // remove ending linebreaks in the paragraph
16169569a107SDanny Lin            $i=count($this->calls)-1;
16179569a107SDanny Lin            if ($this->calls[$i][0] == 'cdata') $this->calls[$i][1][0] = rtrim($this->calls[$i][1][0],DOKU_PARSER_EOL);
1618506ae684Sandi            $this->calls[] = array('p_close',array(), $pos);
1619506ae684Sandi        }
1620e1c10e4dSchris
162144881bd0Shenning.noren        $this->inParagraph = false;
16229569a107SDanny Lin        $this->skipEol = true;
16230cecf9d5Sandi    }
162441624b31SChris Smith
162541624b31SChris Smith    function addCall($call) {
162641624b31SChris Smith        $key = count($this->calls);
162741624b31SChris Smith        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
162841624b31SChris Smith            $this->calls[$key-1][1][0] .= $call[1][0];
162941624b31SChris Smith        } else {
163041624b31SChris Smith            $this->calls[] = $call;
163141624b31SChris Smith        }
163241624b31SChris Smith    }
16339569a107SDanny Lin
16349569a107SDanny Lin    // simple version of addCall, without checking cdata
16359569a107SDanny Lin    function storeCall($call) {
16369569a107SDanny Lin        $this->calls[] = $call;
16379569a107SDanny Lin    }
16389569a107SDanny Lin
16399569a107SDanny Lin    /**
16409569a107SDanny Lin     * Processes the whole instruction stack to open and close paragraphs
16419569a107SDanny Lin     *
16429569a107SDanny Lin     * @author Harry Fuecks <hfuecks@gmail.com>
16439569a107SDanny Lin     * @author Andreas Gohr <andi@splitbrain.org>
16449569a107SDanny Lin     */
16459569a107SDanny Lin    function process($calls) {
16469569a107SDanny Lin        // open first paragraph
16479569a107SDanny Lin        $this->openParagraph(0);
16489569a107SDanny Lin        foreach ( $calls as $key => $call ) {
16499569a107SDanny Lin            $cname = $call[0];
16509569a107SDanny Lin            if ($cname == 'plugin') {
16519569a107SDanny Lin                $cname='plugin_'.$call[1][0];
16529569a107SDanny Lin                $plugin = true;
16539569a107SDanny Lin                $plugin_open = (($call[1][2] == DOKU_LEXER_ENTER) || ($call[1][2] == DOKU_LEXER_SPECIAL));
16549569a107SDanny Lin                $plugin_close = (($call[1][2] == DOKU_LEXER_EXIT) || ($call[1][2] == DOKU_LEXER_SPECIAL));
16559569a107SDanny Lin            } else {
16569569a107SDanny Lin                $plugin = false;
16579569a107SDanny Lin            }
16589569a107SDanny Lin            /* stack */
16599569a107SDanny Lin            if ( in_array($cname,$this->stackClose ) && (!$plugin || $plugin_close)) {
16609569a107SDanny Lin                $this->closeParagraph($call[2]);
16619569a107SDanny Lin                $this->storeCall($call);
16629569a107SDanny Lin                $this->openParagraph($call[2]);
16639569a107SDanny Lin                continue;
16649569a107SDanny Lin            }
16659569a107SDanny Lin            if ( in_array($cname,$this->stackOpen ) && (!$plugin || $plugin_open) ) {
16669569a107SDanny Lin                $this->closeParagraph($call[2]);
16679569a107SDanny Lin                $this->storeCall($call);
16689569a107SDanny Lin                $this->openParagraph($call[2]);
16699569a107SDanny Lin                continue;
16709569a107SDanny Lin            }
16719569a107SDanny Lin            /* block */
16729569a107SDanny Lin            // If it's a substition it opens and closes at the same call.
16739569a107SDanny Lin            // To make sure next paragraph is correctly started, let close go first.
16749569a107SDanny Lin            if ( in_array($cname, $this->blockClose) && (!$plugin || $plugin_close)) {
16759569a107SDanny Lin                $this->closeParagraph($call[2]);
16769569a107SDanny Lin                $this->storeCall($call);
16779569a107SDanny Lin                $this->openParagraph($call[2]);
16789569a107SDanny Lin                continue;
16799569a107SDanny Lin            }
16809569a107SDanny Lin            if ( in_array($cname, $this->blockOpen) && (!$plugin || $plugin_open)) {
16819569a107SDanny Lin                $this->closeParagraph($call[2]);
16829569a107SDanny Lin                $this->storeCall($call);
16839569a107SDanny Lin                continue;
16849569a107SDanny Lin            }
16859569a107SDanny Lin            /* eol */
16869569a107SDanny Lin            if ( $cname == 'eol' ) {
16879569a107SDanny Lin                // Check this isn't an eol instruction to skip...
16889569a107SDanny Lin                if ( !$this->skipEol ) {
16899569a107SDanny Lin                    // Next is EOL => double eol => mark as paragraph
16909569a107SDanny Lin                    if ( isset($calls[$key+1]) && $calls[$key+1][0] == 'eol' ) {
16919569a107SDanny Lin                        $this->closeParagraph($call[2]);
16929569a107SDanny Lin                        $this->openParagraph($call[2]);
16939569a107SDanny Lin                    } else {
16949569a107SDanny Lin                        //if this is just a single eol make a space from it
16959569a107SDanny Lin                        $this->addCall(array('cdata',array(DOKU_PARSER_EOL), $call[2]));
16969569a107SDanny Lin                    }
16979569a107SDanny Lin                }
16989569a107SDanny Lin                continue;
16999569a107SDanny Lin            }
17009569a107SDanny Lin            /* normal */
17019569a107SDanny Lin            $this->addCall($call);
17029569a107SDanny Lin            $this->skipEol = false;
17039569a107SDanny Lin        }
17049569a107SDanny Lin        // close last paragraph
17059569a107SDanny Lin        $call = end($this->calls);
17069569a107SDanny Lin        $this->closeParagraph($call[2]);
17079569a107SDanny Lin        return $this->calls;
17089569a107SDanny Lin    }
17090cecf9d5Sandi}
17102a27e99aSandi
1711e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
1712