xref: /dokuwiki/inc/parser/handler.php (revision 54f741e8823b91c5872313ab46f59c7a1ef0be2c)
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
26f50a239bSTakamura     * @param mixed $args
27f50a239bSTakamura     * @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
609e491c01SAndreas Gohr    /**
619e491c01SAndreas Gohr     * fetch the current call and advance the pointer to the next one
629e491c01SAndreas Gohr     *
639e491c01SAndreas Gohr     * @return bool|mixed
649e491c01SAndreas Gohr     */
650cecf9d5Sandi    function fetch() {
669e491c01SAndreas Gohr        $call = current($this->calls);
679e491c01SAndreas Gohr        if($call !== false) {
689e491c01SAndreas Gohr            next($this->calls); //advance the pointer
699e491c01SAndreas Gohr            return $call;
700cecf9d5Sandi        }
7144881bd0Shenning.noren        return false;
720cecf9d5Sandi    }
73ee20e7d1Sandi
74ee20e7d1Sandi
75ee20e7d1Sandi    /**
76ee20e7d1Sandi     * Special plugin handler
77ee20e7d1Sandi     *
78ee20e7d1Sandi     * This handler is called for all modes starting with 'plugin_'.
79ee20e7d1Sandi     * An additional parameter with the plugin name is passed
80ee20e7d1Sandi     *
81ee20e7d1Sandi     * @author Andreas Gohr <andi@splitbrain.org>
82f50a239bSTakamura     *
83f50a239bSTakamura     * @param string|integer $match
84f50a239bSTakamura     * @param string|integer $state
85f50a239bSTakamura     * @param integer $pos
86f50a239bSTakamura     * @param $pluginname
87f50a239bSTakamura     *
88f50a239bSTakamura     * @return bool
89ee20e7d1Sandi     */
90ee20e7d1Sandi    function plugin($match, $state, $pos, $pluginname){
91ee20e7d1Sandi        $data = array($match);
9251a883edSGerrit Uitslag        /** @var DokuWiki_Syntax_Plugin $plugin */
9345d5ad75SGerrit Uitslag        $plugin = plugin_load('syntax',$pluginname);
94a46d0d65SAndreas Gohr        if($plugin != null){
95f02a7d06Schris            $data = $plugin->handle($match, $state, $pos, $this);
96ee20e7d1Sandi        }
9713ecfb18SChris Smith        if ($data !== false) {
9882d61635Spierre.spring            $this->addPluginCall($pluginname,$data,$state,$pos,$match);
9913ecfb18SChris Smith        }
10044881bd0Shenning.noren        return true;
101ee20e7d1Sandi    }
1020cecf9d5Sandi
1030cecf9d5Sandi    function base($match, $state, $pos) {
1040cecf9d5Sandi        switch ( $state ) {
1050cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
106433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
10744881bd0Shenning.noren                return true;
1080cecf9d5Sandi            break;
1090cecf9d5Sandi        }
1100cecf9d5Sandi    }
1110cecf9d5Sandi
1120cecf9d5Sandi    function header($match, $state, $pos) {
113d7e8115fSAndreas Gohr        // get level and title
114a4a2d4cfSAndreas Gohr        $title = trim($match);
115a4a2d4cfSAndreas Gohr        $level = 7 - strspn($title,'=');
116d7e8115fSAndreas Gohr        if($level < 1) $level = 1;
117a4a2d4cfSAndreas Gohr        $title = trim($title,'=');
118a4a2d4cfSAndreas Gohr        $title = trim($title);
1190cecf9d5Sandi
120e1c10e4dSchris        if ($this->status['section']) $this->_addCall('section_close',array(),$pos);
121e1c10e4dSchris
122433bef32Sandi        $this->_addCall('header',array($title,$level,$pos), $pos);
123e1c10e4dSchris
124e1c10e4dSchris        $this->_addCall('section_open',array($level),$pos);
12544881bd0Shenning.noren        $this->status['section'] = true;
12644881bd0Shenning.noren        return true;
1270cecf9d5Sandi    }
1280cecf9d5Sandi
1290cecf9d5Sandi    function notoc($match, $state, $pos) {
130e41c4da9SAndreas Gohr        $this->_addCall('notoc',array(),$pos);
13144881bd0Shenning.noren        return true;
1320cecf9d5Sandi    }
1330cecf9d5Sandi
1349dc2c2afSandi    function nocache($match, $state, $pos) {
1359dc2c2afSandi        $this->_addCall('nocache',array(),$pos);
13644881bd0Shenning.noren        return true;
1379dc2c2afSandi    }
1389dc2c2afSandi
1390cecf9d5Sandi    function linebreak($match, $state, $pos) {
140433bef32Sandi        $this->_addCall('linebreak',array(),$pos);
14144881bd0Shenning.noren        return true;
1420cecf9d5Sandi    }
1430cecf9d5Sandi
1440cecf9d5Sandi    function eol($match, $state, $pos) {
145433bef32Sandi        $this->_addCall('eol',array(),$pos);
14644881bd0Shenning.noren        return true;
1470cecf9d5Sandi    }
1480cecf9d5Sandi
1490cecf9d5Sandi    function hr($match, $state, $pos) {
150433bef32Sandi        $this->_addCall('hr',array(),$pos);
15144881bd0Shenning.noren        return true;
1520cecf9d5Sandi    }
1530cecf9d5Sandi
154276820f7SScrutinizer Auto-Fixer    /**
155f50a239bSTakamura     * @param string|integer $match
156f50a239bSTakamura     * @param string|integer $state
157f50a239bSTakamura     * @param integer $pos
158276820f7SScrutinizer Auto-Fixer     * @param string $name
159276820f7SScrutinizer Auto-Fixer     */
160433bef32Sandi    function _nestingTag($match, $state, $pos, $name) {
1610cecf9d5Sandi        switch ( $state ) {
1620cecf9d5Sandi            case DOKU_LEXER_ENTER:
163433bef32Sandi                $this->_addCall($name.'_open', array(), $pos);
1640cecf9d5Sandi            break;
1650cecf9d5Sandi            case DOKU_LEXER_EXIT:
166433bef32Sandi                $this->_addCall($name.'_close', array(), $pos);
1670cecf9d5Sandi            break;
1680cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
169433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
1700cecf9d5Sandi            break;
1710cecf9d5Sandi        }
1720cecf9d5Sandi    }
1730cecf9d5Sandi
1740cecf9d5Sandi    function strong($match, $state, $pos) {
175433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'strong');
17644881bd0Shenning.noren        return true;
1770cecf9d5Sandi    }
1780cecf9d5Sandi
1790cecf9d5Sandi    function emphasis($match, $state, $pos) {
180433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'emphasis');
18144881bd0Shenning.noren        return true;
1820cecf9d5Sandi    }
1830cecf9d5Sandi
1840cecf9d5Sandi    function underline($match, $state, $pos) {
185433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'underline');
18644881bd0Shenning.noren        return true;
1870cecf9d5Sandi    }
1880cecf9d5Sandi
1890cecf9d5Sandi    function monospace($match, $state, $pos) {
190433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'monospace');
19144881bd0Shenning.noren        return true;
1920cecf9d5Sandi    }
1930cecf9d5Sandi
1940cecf9d5Sandi    function subscript($match, $state, $pos) {
195433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'subscript');
19644881bd0Shenning.noren        return true;
1970cecf9d5Sandi    }
1980cecf9d5Sandi
1990cecf9d5Sandi    function superscript($match, $state, $pos) {
200433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'superscript');
20144881bd0Shenning.noren        return true;
2020cecf9d5Sandi    }
2030cecf9d5Sandi
2040cecf9d5Sandi    function deleted($match, $state, $pos) {
205433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'deleted');
20644881bd0Shenning.noren        return true;
2070cecf9d5Sandi    }
2080cecf9d5Sandi
2090cecf9d5Sandi
2100cecf9d5Sandi    function footnote($match, $state, $pos) {
2115587e44cSchris//        $this->_nestingTag($match, $state, $pos, 'footnote');
212742c66f8Schris        if (!isset($this->_footnote)) $this->_footnote = false;
2132fe7363dSchris
2145587e44cSchris        switch ( $state ) {
2155587e44cSchris            case DOKU_LEXER_ENTER:
2162fe7363dSchris                // footnotes can not be nested - however due to limitations in lexer it can't be prevented
2172fe7363dSchris                // we will still enter a new footnote mode, we just do nothing
218742c66f8Schris                if ($this->_footnote) {
2192fe7363dSchris                    $this->_addCall('cdata',array($match), $pos);
2202fe7363dSchris                    break;
2212fe7363dSchris                }
2222fe7363dSchris
223742c66f8Schris                $this->_footnote = true;
2242fe7363dSchris
22567f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Nest($this->CallWriter,'footnote_close');
2265587e44cSchris                $this->CallWriter = & $ReWriter;
2274a26ad85Schris                $this->_addCall('footnote_open', array(), $pos);
2285587e44cSchris            break;
2295587e44cSchris            case DOKU_LEXER_EXIT:
2302fe7363dSchris                // check whether we have already exitted the footnote mode, can happen if the modes were nested
231742c66f8Schris                if (!$this->_footnote) {
2322fe7363dSchris                    $this->_addCall('cdata',array($match), $pos);
2332fe7363dSchris                    break;
2342fe7363dSchris                }
2352fe7363dSchris
236742c66f8Schris                $this->_footnote = false;
2372fe7363dSchris
2385587e44cSchris                $this->_addCall('footnote_close', array(), $pos);
2395587e44cSchris                $this->CallWriter->process();
2405587e44cSchris                $ReWriter = & $this->CallWriter;
2415587e44cSchris                $this->CallWriter = & $ReWriter->CallWriter;
2425587e44cSchris            break;
2435587e44cSchris            case DOKU_LEXER_UNMATCHED:
2445587e44cSchris                $this->_addCall('cdata', array($match), $pos);
2455587e44cSchris            break;
2465587e44cSchris        }
24744881bd0Shenning.noren        return true;
2480cecf9d5Sandi    }
2490cecf9d5Sandi
2500cecf9d5Sandi    function listblock($match, $state, $pos) {
2510cecf9d5Sandi        switch ( $state ) {
2520cecf9d5Sandi            case DOKU_LEXER_ENTER:
25367f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_List($this->CallWriter);
2540cecf9d5Sandi                $this->CallWriter = & $ReWriter;
255433bef32Sandi                $this->_addCall('list_open', array($match), $pos);
2560cecf9d5Sandi            break;
2570cecf9d5Sandi            case DOKU_LEXER_EXIT:
258433bef32Sandi                $this->_addCall('list_close', array(), $pos);
2590cecf9d5Sandi                $this->CallWriter->process();
2600cecf9d5Sandi                $ReWriter = & $this->CallWriter;
2610cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
2620cecf9d5Sandi            break;
2630cecf9d5Sandi            case DOKU_LEXER_MATCHED:
264433bef32Sandi                $this->_addCall('list_item', array($match), $pos);
2650cecf9d5Sandi            break;
2660cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
267433bef32Sandi                $this->_addCall('cdata', array($match), $pos);
2680cecf9d5Sandi            break;
2690cecf9d5Sandi        }
27044881bd0Shenning.noren        return true;
2710cecf9d5Sandi    }
2720cecf9d5Sandi
2730cecf9d5Sandi    function unformatted($match, $state, $pos) {
2740cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
275433bef32Sandi            $this->_addCall('unformatted',array($match), $pos);
2760cecf9d5Sandi        }
27744881bd0Shenning.noren        return true;
2780cecf9d5Sandi    }
2790cecf9d5Sandi
2800cecf9d5Sandi    function php($match, $state, $pos) {
281df9add72Schris        global $conf;
2820cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
283433bef32Sandi            $this->_addCall('php',array($match), $pos);
2840cecf9d5Sandi        }
28544881bd0Shenning.noren        return true;
2860cecf9d5Sandi    }
2870cecf9d5Sandi
28807f89c3cSAnika Henke    function phpblock($match, $state, $pos) {
28907f89c3cSAnika Henke        global $conf;
29007f89c3cSAnika Henke        if ( $state == DOKU_LEXER_UNMATCHED ) {
29107f89c3cSAnika Henke            $this->_addCall('phpblock',array($match), $pos);
29207f89c3cSAnika Henke        }
29307f89c3cSAnika Henke        return true;
29407f89c3cSAnika Henke    }
29507f89c3cSAnika Henke
2960cecf9d5Sandi    function html($match, $state, $pos) {
297df9add72Schris        global $conf;
2980cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
299433bef32Sandi            $this->_addCall('html',array($match), $pos);
3000cecf9d5Sandi        }
30144881bd0Shenning.noren        return true;
3020cecf9d5Sandi    }
3030cecf9d5Sandi
30407f89c3cSAnika Henke    function htmlblock($match, $state, $pos) {
30507f89c3cSAnika Henke        global $conf;
30607f89c3cSAnika Henke        if ( $state == DOKU_LEXER_UNMATCHED ) {
30707f89c3cSAnika Henke            $this->_addCall('htmlblock',array($match), $pos);
30807f89c3cSAnika Henke        }
30907f89c3cSAnika Henke        return true;
31007f89c3cSAnika Henke    }
31107f89c3cSAnika Henke
3120cecf9d5Sandi    function preformatted($match, $state, $pos) {
3130cecf9d5Sandi        switch ( $state ) {
3140cecf9d5Sandi            case DOKU_LEXER_ENTER:
31567f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Preformatted($this->CallWriter);
31626e22ab8SChristopher Smith                $this->CallWriter = $ReWriter;
317433bef32Sandi                $this->_addCall('preformatted_start',array(), $pos);
3180cecf9d5Sandi            break;
3190cecf9d5Sandi            case DOKU_LEXER_EXIT:
320433bef32Sandi                $this->_addCall('preformatted_end',array(), $pos);
3210cecf9d5Sandi                $this->CallWriter->process();
3220cecf9d5Sandi                $ReWriter = & $this->CallWriter;
3230cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
3240cecf9d5Sandi            break;
3250cecf9d5Sandi            case DOKU_LEXER_MATCHED:
326433bef32Sandi                $this->_addCall('preformatted_newline',array(), $pos);
3270cecf9d5Sandi            break;
3280cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
329433bef32Sandi                $this->_addCall('preformatted_content',array($match), $pos);
3300cecf9d5Sandi            break;
3310cecf9d5Sandi        }
3320cecf9d5Sandi
33344881bd0Shenning.noren        return true;
3340cecf9d5Sandi    }
3350cecf9d5Sandi
3360cecf9d5Sandi    function quote($match, $state, $pos) {
3370cecf9d5Sandi
3380cecf9d5Sandi        switch ( $state ) {
3390cecf9d5Sandi
3400cecf9d5Sandi            case DOKU_LEXER_ENTER:
34167f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Quote($this->CallWriter);
3420cecf9d5Sandi                $this->CallWriter = & $ReWriter;
343433bef32Sandi                $this->_addCall('quote_start',array($match), $pos);
3440cecf9d5Sandi            break;
3450cecf9d5Sandi
3460cecf9d5Sandi            case DOKU_LEXER_EXIT:
347433bef32Sandi                $this->_addCall('quote_end',array(), $pos);
3480cecf9d5Sandi                $this->CallWriter->process();
3490cecf9d5Sandi                $ReWriter = & $this->CallWriter;
3500cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
3510cecf9d5Sandi            break;
3520cecf9d5Sandi
3530cecf9d5Sandi            case DOKU_LEXER_MATCHED:
354433bef32Sandi                $this->_addCall('quote_newline',array($match), $pos);
3550cecf9d5Sandi            break;
3560cecf9d5Sandi
3570cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
358433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
3590cecf9d5Sandi            break;
3600cecf9d5Sandi
3610cecf9d5Sandi        }
3620cecf9d5Sandi
36344881bd0Shenning.noren        return true;
3640cecf9d5Sandi    }
3650cecf9d5Sandi
366e2d88156SLarsDW223    /**
367e2d88156SLarsDW223     * Internal function for parsing highlight options.
368e2d88156SLarsDW223     * $options is parsed for key value pairs separated by commas.
369e2d88156SLarsDW223     * A value might also be missing in which case the value will simple
370e2d88156SLarsDW223     * be set to true. Commas in strings are ignored, e.g. option="4,56"
371e2d88156SLarsDW223     * will work as expected and will only create one entry.
372e2d88156SLarsDW223     *
373*54f741e8SAndreas Gohr     * @param string $options space separated list of key-value pairs,
374e2d88156SLarsDW223     *                        e.g. option1=123, option2="456"
375e2d88156SLarsDW223     * @return array|null     Array of key-value pairs $array['key'] = 'value';
376e2d88156SLarsDW223     *                        or null if no entries found
377e2d88156SLarsDW223     */
378e2d88156SLarsDW223    protected function parse_highlight_options ($options) {
379e2d88156SLarsDW223        $result = array();
380*54f741e8SAndreas Gohr        preg_match_all('/(\w+(?:="[^"]*"))|(\w+(?:=[^\s]*))|(\w+[^=\s\]])(?:\s*)/', $options, $matches, PREG_SET_ORDER);
381e2d88156SLarsDW223        foreach ($matches as $match) {
382e2d88156SLarsDW223            $equal_sign = strpos($match [0], '=');
383e2d88156SLarsDW223            if ($equal_sign === false) {
384*54f741e8SAndreas Gohr                $key = trim($match[0]);
385e2d88156SLarsDW223                $result [$key] = 1;
386e2d88156SLarsDW223            } else {
387e2d88156SLarsDW223                $key = substr($match[0], 0, $equal_sign);
388e2d88156SLarsDW223                $value = substr($match[0], $equal_sign+1);
389e2d88156SLarsDW223                $value = trim($value, '"');
390e2d88156SLarsDW223                if (strlen($value) > 0) {
391e2d88156SLarsDW223                    $result [$key] = $value;
392e2d88156SLarsDW223                } else {
393e2d88156SLarsDW223                    $result [$key] = 1;
394e2d88156SLarsDW223                }
395e2d88156SLarsDW223            }
396e2d88156SLarsDW223        }
397e2d88156SLarsDW223
398e2d88156SLarsDW223        // Check for supported options
399e2d88156SLarsDW223        $result = array_intersect_key(
400e2d88156SLarsDW223            $result,
401e2d88156SLarsDW223            array_flip(array(
402e2d88156SLarsDW223                'enable_line_numbers',
403e2d88156SLarsDW223                'start_line_numbers_at',
404e2d88156SLarsDW223                'highlight_lines_extra',
405e2d88156SLarsDW223                'enable_keyword_links')
406e2d88156SLarsDW223            )
407e2d88156SLarsDW223        );
408e2d88156SLarsDW223
409e2d88156SLarsDW223        // Sanitize values
410e2d88156SLarsDW223        if(isset($result['enable_line_numbers'])) {
411*54f741e8SAndreas Gohr            if($result['enable_line_numbers'] === 'false') {
412*54f741e8SAndreas Gohr                $result['enable_line_numbers'] = false;
413*54f741e8SAndreas Gohr            }
414e2d88156SLarsDW223            $result['enable_line_numbers'] = (bool) $result['enable_line_numbers'];
415e2d88156SLarsDW223        }
416e2d88156SLarsDW223        if(isset($result['highlight_lines_extra'])) {
417e2d88156SLarsDW223            $result['highlight_lines_extra'] = array_map('intval', explode(',', $result['highlight_lines_extra']));
418e2d88156SLarsDW223            $result['highlight_lines_extra'] = array_filter($result['highlight_lines_extra']);
419e2d88156SLarsDW223            $result['highlight_lines_extra'] = array_unique($result['highlight_lines_extra']);
420e2d88156SLarsDW223        }
421e2d88156SLarsDW223        if(isset($result['start_line_numbers_at'])) {
422e2d88156SLarsDW223            $result['start_line_numbers_at'] = (int) $result['start_line_numbers_at'];
423e2d88156SLarsDW223        }
424e2d88156SLarsDW223        if(isset($result['enable_keyword_links'])) {
425*54f741e8SAndreas Gohr            if($result['enable_keyword_links'] === 'false') {
426*54f741e8SAndreas Gohr                $result['enable_keyword_links'] = false;
427*54f741e8SAndreas Gohr            }
428*54f741e8SAndreas Gohr            $result['enable_keyword_links'] = (bool) $result['enable_keyword_links'];
429e2d88156SLarsDW223        }
430e2d88156SLarsDW223        if (count($result) == 0) {
431e2d88156SLarsDW223            return null;
432e2d88156SLarsDW223        }
433e2d88156SLarsDW223
434e2d88156SLarsDW223        return $result;
435e2d88156SLarsDW223    }
436e2d88156SLarsDW223
4373d491f75SAndreas Gohr    function file($match, $state, $pos) {
4383d491f75SAndreas Gohr        return $this->code($match, $state, $pos, 'file');
4393d491f75SAndreas Gohr    }
4403d491f75SAndreas Gohr
4413d491f75SAndreas Gohr    function code($match, $state, $pos, $type='code') {
4423d491f75SAndreas Gohr        if ( $state == DOKU_LEXER_UNMATCHED ) {
4434b7f9e70STom N Harris            $matches = explode('>',$match,2);
444e2d88156SLarsDW223            // Cut out variable options enclosed in []
445e2d88156SLarsDW223            preg_match('/\[.*\]/', $matches[0], $options);
446e2d88156SLarsDW223            if (!empty($options[0])) {
447e2d88156SLarsDW223                $matches[0] = str_replace($options[0], '', $matches[0]);
448e2d88156SLarsDW223            }
4490139312bSAdrian Lang            $param = preg_split('/\s+/', $matches[0], 2, PREG_SPLIT_NO_EMPTY);
4500139312bSAdrian Lang            while(count($param) < 2) array_push($param, null);
4510139312bSAdrian Lang            // We shortcut html here.
4520139312bSAdrian Lang            if ($param[0] == 'html') $param[0] = 'html4strict';
4530139312bSAdrian Lang            if ($param[0] == '-') $param[0] = null;
4540139312bSAdrian Lang            array_unshift($param, $matches[1]);
455e2d88156SLarsDW223            if (!empty($options[0])) {
456e2d88156SLarsDW223                $param [] = $this->parse_highlight_options ($options[0]);
457e2d88156SLarsDW223            }
4580139312bSAdrian Lang            $this->_addCall($type, $param, $pos);
4590cecf9d5Sandi        }
46044881bd0Shenning.noren        return true;
4610cecf9d5Sandi    }
4620cecf9d5Sandi
4630cecf9d5Sandi    function acronym($match, $state, $pos) {
464433bef32Sandi        $this->_addCall('acronym',array($match), $pos);
46544881bd0Shenning.noren        return true;
4660cecf9d5Sandi    }
4670cecf9d5Sandi
4680cecf9d5Sandi    function smiley($match, $state, $pos) {
469433bef32Sandi        $this->_addCall('smiley',array($match), $pos);
47044881bd0Shenning.noren        return true;
4710cecf9d5Sandi    }
4720cecf9d5Sandi
4730cecf9d5Sandi    function wordblock($match, $state, $pos) {
474433bef32Sandi        $this->_addCall('wordblock',array($match), $pos);
47544881bd0Shenning.noren        return true;
4760cecf9d5Sandi    }
4770cecf9d5Sandi
4780cecf9d5Sandi    function entity($match, $state, $pos) {
479433bef32Sandi        $this->_addCall('entity',array($match), $pos);
48044881bd0Shenning.noren        return true;
4810cecf9d5Sandi    }
4820cecf9d5Sandi
4830cecf9d5Sandi    function multiplyentity($match, $state, $pos) {
4840cecf9d5Sandi        preg_match_all('/\d+/',$match,$matches);
485433bef32Sandi        $this->_addCall('multiplyentity',array($matches[0][0],$matches[0][1]), $pos);
48644881bd0Shenning.noren        return true;
4870cecf9d5Sandi    }
4880cecf9d5Sandi
4890cecf9d5Sandi    function singlequoteopening($match, $state, $pos) {
490433bef32Sandi        $this->_addCall('singlequoteopening',array(), $pos);
49144881bd0Shenning.noren        return true;
4920cecf9d5Sandi    }
4930cecf9d5Sandi
4940cecf9d5Sandi    function singlequoteclosing($match, $state, $pos) {
495433bef32Sandi        $this->_addCall('singlequoteclosing',array(), $pos);
49644881bd0Shenning.noren        return true;
4970cecf9d5Sandi    }
4980cecf9d5Sandi
49957d757d1SAndreas Gohr    function apostrophe($match, $state, $pos) {
50057d757d1SAndreas Gohr        $this->_addCall('apostrophe',array(), $pos);
50157d757d1SAndreas Gohr        return true;
50257d757d1SAndreas Gohr    }
50357d757d1SAndreas Gohr
5040cecf9d5Sandi    function doublequoteopening($match, $state, $pos) {
505433bef32Sandi        $this->_addCall('doublequoteopening',array(), $pos);
506e950d12fSChristopher Smith        $this->status['doublequote']++;
50744881bd0Shenning.noren        return true;
5080cecf9d5Sandi    }
5090cecf9d5Sandi
5100cecf9d5Sandi    function doublequoteclosing($match, $state, $pos) {
511e950d12fSChristopher Smith        if ($this->status['doublequote'] <= 0) {
512e950d12fSChristopher Smith            $this->doublequoteopening($match, $state, $pos);
513e950d12fSChristopher Smith        } else {
514433bef32Sandi            $this->_addCall('doublequoteclosing',array(), $pos);
515e950d12fSChristopher Smith            $this->status['doublequote'] = max(0, --$this->status['doublequote']);
516e950d12fSChristopher Smith        }
51744881bd0Shenning.noren        return true;
5180cecf9d5Sandi    }
5190cecf9d5Sandi
5200cecf9d5Sandi    function camelcaselink($match, $state, $pos) {
521433bef32Sandi        $this->_addCall('camelcaselink',array($match), $pos);
52244881bd0Shenning.noren        return true;
5230cecf9d5Sandi    }
5240cecf9d5Sandi
5250cecf9d5Sandi    /*
5260cecf9d5Sandi    */
5270cecf9d5Sandi    function internallink($match, $state, $pos) {
5280cecf9d5Sandi        // Strip the opening and closing markup
5290cecf9d5Sandi        $link = preg_replace(array('/^\[\[/','/\]\]$/u'),'',$match);
5300cecf9d5Sandi
5310cecf9d5Sandi        // Split title from URL
5324b7f9e70STom N Harris        $link = explode('|',$link,2);
5330cecf9d5Sandi        if ( !isset($link[1]) ) {
5340ea51e63SMatt Perry            $link[1] = null;
5350cecf9d5Sandi        } else if ( preg_match('/^\{\{[^\}]+\}\}$/',$link[1]) ) {
5365578eb8fSandi            // If the title is an image, convert it to an array containing the image details
537b625487dSandi            $link[1] = Doku_Handler_Parse_Media($link[1]);
5380cecf9d5Sandi        }
5390b7c14c2Sandi        $link[0] = trim($link[0]);
5400cecf9d5Sandi
5410e1c636eSandi        //decide which kind of link it is
5420e1c636eSandi
5436efc45a2SDmitry Katsubo        if ( link_isinterwiki($link[0]) ) {
5440e1c636eSandi            // Interwiki
5454b7f9e70STom N Harris            $interwiki = explode('>',$link[0],2);
546433bef32Sandi            $this->_addCall(
5470cecf9d5Sandi                'interwikilink',
5480cecf9d5Sandi                array($link[0],$link[1],strtolower($interwiki[0]),$interwiki[1]),
5490cecf9d5Sandi                $pos
5500cecf9d5Sandi                );
55115f1b77cSAndreas Gohr        }elseif ( preg_match('/^\\\\\\\\[^\\\\]+?\\\\/u',$link[0]) ) {
5520e1c636eSandi            // Windows Share
553433bef32Sandi            $this->_addCall(
5540cecf9d5Sandi                'windowssharelink',
5550cecf9d5Sandi                array($link[0],$link[1]),
5560cecf9d5Sandi                $pos
5570cecf9d5Sandi                );
5584468cb4cSAndreas Gohr        }elseif ( preg_match('#^([a-z0-9\-\.+]+?)://#i',$link[0]) ) {
5590e1c636eSandi            // external link (accepts all protocols)
560433bef32Sandi            $this->_addCall(
5610cecf9d5Sandi                    'externallink',
5620cecf9d5Sandi                    array($link[0],$link[1]),
5630cecf9d5Sandi                    $pos
5640cecf9d5Sandi                    );
5650a1d30bfSchris        }elseif ( preg_match('<'.PREG_PATTERN_VALID_EMAIL.'>',$link[0]) ) {
5660a1d30bfSchris            // E-Mail (pattern above is defined in inc/mail.php)
567a6755281Sandi            $this->_addCall(
568a6755281Sandi                'emaillink',
569a6755281Sandi                array($link[0],$link[1]),
570a6755281Sandi                $pos
571a6755281Sandi                );
5720b7c14c2Sandi        }elseif ( preg_match('!^#.+!',$link[0]) ){
5730b7c14c2Sandi            // local link
5740b7c14c2Sandi            $this->_addCall(
5750b7c14c2Sandi                'locallink',
5760b7c14c2Sandi                array(substr($link[0],1),$link[1]),
5770b7c14c2Sandi                $pos
5780b7c14c2Sandi                );
5790e1c636eSandi        }else{
5800e1c636eSandi            // internal link
581433bef32Sandi            $this->_addCall(
5820e1c636eSandi                'internallink',
5830e1c636eSandi                array($link[0],$link[1]),
5840e1c636eSandi                $pos
5850e1c636eSandi                );
5860cecf9d5Sandi        }
5870e1c636eSandi
58844881bd0Shenning.noren        return true;
5890cecf9d5Sandi    }
5900cecf9d5Sandi
5910cecf9d5Sandi    function filelink($match, $state, $pos) {
5920ea51e63SMatt Perry        $this->_addCall('filelink',array($match, null), $pos);
59344881bd0Shenning.noren        return true;
5940cecf9d5Sandi    }
5950cecf9d5Sandi
5960cecf9d5Sandi    function windowssharelink($match, $state, $pos) {
5970ea51e63SMatt Perry        $this->_addCall('windowssharelink',array($match, null), $pos);
59844881bd0Shenning.noren        return true;
5990cecf9d5Sandi    }
6000cecf9d5Sandi
6010cecf9d5Sandi    function media($match, $state, $pos) {
6020cecf9d5Sandi        $p = Doku_Handler_Parse_Media($match);
6030cecf9d5Sandi
604433bef32Sandi        $this->_addCall(
6050cecf9d5Sandi              $p['type'],
606dc673a5bSjoe.lapp              array($p['src'], $p['title'], $p['align'], $p['width'],
607dc673a5bSjoe.lapp                     $p['height'], $p['cache'], $p['linking']),
6080cecf9d5Sandi              $pos
6090cecf9d5Sandi             );
61044881bd0Shenning.noren        return true;
6110cecf9d5Sandi    }
6120cecf9d5Sandi
613b625487dSandi    function rss($match, $state, $pos) {
614b625487dSandi        $link = preg_replace(array('/^\{\{rss>/','/\}\}$/'),'',$match);
6153db95becSAndreas Gohr
6163db95becSAndreas Gohr        // get params
6173db95becSAndreas Gohr        list($link,$params) = explode(' ',$link,2);
6183db95becSAndreas Gohr
6193db95becSAndreas Gohr        $p = array();
6203db95becSAndreas Gohr        if(preg_match('/\b(\d+)\b/',$params,$match)){
6213db95becSAndreas Gohr            $p['max'] = $match[1];
6223db95becSAndreas Gohr        }else{
6233db95becSAndreas Gohr            $p['max'] = 8;
6243db95becSAndreas Gohr        }
6253db95becSAndreas Gohr        $p['reverse'] = (preg_match('/rev/',$params));
6263db95becSAndreas Gohr        $p['author']  = (preg_match('/\b(by|author)/',$params));
6273db95becSAndreas Gohr        $p['date']    = (preg_match('/\b(date)/',$params));
6283db95becSAndreas Gohr        $p['details'] = (preg_match('/\b(desc|detail)/',$params));
62938c6f603SRobin H. Johnson        $p['nosort']  = (preg_match('/\b(nosort)\b/',$params));
6303db95becSAndreas Gohr
6310a69dff7Schris        if (preg_match('/\b(\d+)([dhm])\b/',$params,$match)) {
6320a69dff7Schris            $period = array('d' => 86400, 'h' => 3600, 'm' => 60);
6330a69dff7Schris            $p['refresh'] = max(600,$match[1]*$period[$match[2]]);  // n * period in seconds, minimum 10 minutes
6340a69dff7Schris        } else {
6350a69dff7Schris            $p['refresh'] = 14400;   // default to 4 hours
6360a69dff7Schris        }
6370a69dff7Schris
6383db95becSAndreas Gohr        $this->_addCall('rss',array($link,$p),$pos);
63944881bd0Shenning.noren        return true;
640b625487dSandi    }
641b625487dSandi
6420cecf9d5Sandi    function externallink($match, $state, $pos) {
643da9f31c5SAndreas Gohr        $url   = $match;
644da9f31c5SAndreas Gohr        $title = null;
6450cecf9d5Sandi
646da9f31c5SAndreas Gohr        // add protocol on simple short URLs
647da9f31c5SAndreas Gohr        if(substr($url,0,3) == 'ftp' && (substr($url,0,6) != 'ftp://')){
648da9f31c5SAndreas Gohr            $title = $url;
649da9f31c5SAndreas Gohr            $url   = 'ftp://'.$url;
650da9f31c5SAndreas Gohr        }
651da9f31c5SAndreas Gohr        if(substr($url,0,3) == 'www' && (substr($url,0,7) != 'http://')){
652da9f31c5SAndreas Gohr            $title = $url;
653da9f31c5SAndreas Gohr            $url = 'http://'.$url;
654da9f31c5SAndreas Gohr        }
655da9f31c5SAndreas Gohr
656da9f31c5SAndreas Gohr        $this->_addCall('externallink',array($url, $title), $pos);
65744881bd0Shenning.noren        return true;
6580cecf9d5Sandi    }
6590cecf9d5Sandi
66071352defSandi    function emaillink($match, $state, $pos) {
6610cecf9d5Sandi        $email = preg_replace(array('/^</','/>$/'),'',$match);
6620ea51e63SMatt Perry        $this->_addCall('emaillink',array($email, null), $pos);
66344881bd0Shenning.noren        return true;
6640cecf9d5Sandi    }
6650cecf9d5Sandi
6660cecf9d5Sandi    function table($match, $state, $pos) {
6670cecf9d5Sandi        switch ( $state ) {
6680cecf9d5Sandi
6690cecf9d5Sandi            case DOKU_LEXER_ENTER:
6700cecf9d5Sandi
67167f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Table($this->CallWriter);
6720cecf9d5Sandi                $this->CallWriter = & $ReWriter;
6730cecf9d5Sandi
67490df9a4dSAdrian Lang                $this->_addCall('table_start', array($pos + 1), $pos);
6750cecf9d5Sandi                if ( trim($match) == '^' ) {
676433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
6770cecf9d5Sandi                } else {
678433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
6790cecf9d5Sandi                }
6800cecf9d5Sandi            break;
6810cecf9d5Sandi
6820cecf9d5Sandi            case DOKU_LEXER_EXIT:
68390df9a4dSAdrian Lang                $this->_addCall('table_end', array($pos), $pos);
6840cecf9d5Sandi                $this->CallWriter->process();
6850cecf9d5Sandi                $ReWriter = & $this->CallWriter;
6860cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
6870cecf9d5Sandi            break;
6880cecf9d5Sandi
6890cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
6900cecf9d5Sandi                if ( trim($match) != '' ) {
691433bef32Sandi                    $this->_addCall('cdata',array($match), $pos);
6920cecf9d5Sandi                }
6930cecf9d5Sandi            break;
6940cecf9d5Sandi
6950cecf9d5Sandi            case DOKU_LEXER_MATCHED:
6969ab75d9eSAndreas Gohr                if ( $match == ' ' ){
6979ab75d9eSAndreas Gohr                    $this->_addCall('cdata', array($match), $pos);
69825b97867Shakan.sandell                } else if ( preg_match('/:::/',$match) ) {
69925b97867Shakan.sandell                    $this->_addCall('rowspan', array($match), $pos);
700e205b721SAndreas Gohr                } else if ( preg_match('/\t+/',$match) ) {
7019ab75d9eSAndreas Gohr                    $this->_addCall('table_align', array($match), $pos);
702e205b721SAndreas Gohr                } else if ( preg_match('/ {2,}/',$match) ) {
703433bef32Sandi                    $this->_addCall('table_align', array($match), $pos);
7040cecf9d5Sandi                } else if ( $match == "\n|" ) {
705433bef32Sandi                    $this->_addCall('table_row', array(), $pos);
706433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
7070cecf9d5Sandi                } else if ( $match == "\n^" ) {
708433bef32Sandi                    $this->_addCall('table_row', array(), $pos);
709433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
7100cecf9d5Sandi                } else if ( $match == '|' ) {
711433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
7120cecf9d5Sandi                } else if ( $match == '^' ) {
713433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
7140cecf9d5Sandi                }
7150cecf9d5Sandi            break;
7160cecf9d5Sandi        }
71744881bd0Shenning.noren        return true;
7180cecf9d5Sandi    }
7190cecf9d5Sandi}
7200cecf9d5Sandi
7210cecf9d5Sandi//------------------------------------------------------------------------
7220cecf9d5Sandifunction Doku_Handler_Parse_Media($match) {
7230cecf9d5Sandi
7240cecf9d5Sandi    // Strip the opening and closing markup
7250cecf9d5Sandi    $link = preg_replace(array('/^\{\{/','/\}\}$/u'),'',$match);
7260cecf9d5Sandi
7270cecf9d5Sandi    // Split title from URL
7284b7f9e70STom N Harris    $link = explode('|',$link,2);
7290cecf9d5Sandi
7300cecf9d5Sandi    // Check alignment
7310cecf9d5Sandi    $ralign = (bool)preg_match('/^ /',$link[0]);
7320cecf9d5Sandi    $lalign = (bool)preg_match('/ $/',$link[0]);
7330cecf9d5Sandi
7340cecf9d5Sandi    // Logic = what's that ;)...
7350cecf9d5Sandi    if ( $lalign & $ralign ) {
7360cecf9d5Sandi        $align = 'center';
7370cecf9d5Sandi    } else if ( $ralign ) {
7380cecf9d5Sandi        $align = 'right';
7390cecf9d5Sandi    } else if ( $lalign ) {
7400cecf9d5Sandi        $align = 'left';
7410cecf9d5Sandi    } else {
7420ea51e63SMatt Perry        $align = null;
7430cecf9d5Sandi    }
7440cecf9d5Sandi
7450cecf9d5Sandi    // The title...
7460cecf9d5Sandi    if ( !isset($link[1]) ) {
7470ea51e63SMatt Perry        $link[1] = null;
7480cecf9d5Sandi    }
7490cecf9d5Sandi
7504826ab45Sandi    //remove aligning spaces
7514826ab45Sandi    $link[0] = trim($link[0]);
7520cecf9d5Sandi
7534826ab45Sandi    //split into src and parameters (using the very last questionmark)
7544826ab45Sandi    $pos = strrpos($link[0], '?');
7554826ab45Sandi    if($pos !== false){
7564826ab45Sandi        $src   = substr($link[0],0,$pos);
7574826ab45Sandi        $param = substr($link[0],$pos+1);
7580cecf9d5Sandi    }else{
7594826ab45Sandi        $src   = $link[0];
7604826ab45Sandi        $param = '';
7610cecf9d5Sandi    }
7620cecf9d5Sandi
7634826ab45Sandi    //parse width and height
7644826ab45Sandi    if(preg_match('#(\d+)(x(\d+))?#i',$param,$size)){
765443e135dSChristopher Smith        !empty($size[1]) ? $w = $size[1] : $w = null;
766443e135dSChristopher Smith        !empty($size[3]) ? $h = $size[3] : $h = null;
767fc1c55b1Shfuecks    } else {
7680ea51e63SMatt Perry        $w = null;
7690ea51e63SMatt Perry        $h = null;
7700cecf9d5Sandi    }
7710cecf9d5Sandi
772dc673a5bSjoe.lapp    //get linking command
773d35ab615Shenning.noren    if(preg_match('/nolink/i',$param)){
774dc673a5bSjoe.lapp        $linking = 'nolink';
775d35ab615Shenning.noren    }else if(preg_match('/direct/i',$param)){
776dc673a5bSjoe.lapp        $linking = 'direct';
7778acb3108SAndreas Gohr    }else if(preg_match('/linkonly/i',$param)){
7788acb3108SAndreas Gohr        $linking = 'linkonly';
779dc673a5bSjoe.lapp    }else{
780dc673a5bSjoe.lapp        $linking = 'details';
781dc673a5bSjoe.lapp    }
782dc673a5bSjoe.lapp
7834826ab45Sandi    //get caching command
7844826ab45Sandi    if (preg_match('/(nocache|recache)/i',$param,$cachemode)){
7854826ab45Sandi        $cache = $cachemode[1];
7860cecf9d5Sandi    }else{
7874826ab45Sandi        $cache = 'cache';
7880cecf9d5Sandi    }
7890cecf9d5Sandi
7906efc45a2SDmitry Katsubo    // Check whether this is a local or remote image or interwiki
7916efc45a2SDmitry Katsubo    if (media_isexternal($src) || link_isinterwiki($src)){
7924826ab45Sandi        $call = 'externalmedia';
7930cecf9d5Sandi    } else {
7944826ab45Sandi        $call = 'internalmedia';
7950cecf9d5Sandi    }
7960cecf9d5Sandi
7970cecf9d5Sandi    $params = array(
7980cecf9d5Sandi        'type'=>$call,
7994826ab45Sandi        'src'=>$src,
8000cecf9d5Sandi        'title'=>$link[1],
8010cecf9d5Sandi        'align'=>$align,
8024826ab45Sandi        'width'=>$w,
8034826ab45Sandi        'height'=>$h,
8040cecf9d5Sandi        'cache'=>$cache,
805dc673a5bSjoe.lapp        'linking'=>$linking,
8060cecf9d5Sandi    );
8070cecf9d5Sandi
8080cecf9d5Sandi    return $params;
8090cecf9d5Sandi}
8100cecf9d5Sandi
8110cecf9d5Sandi//------------------------------------------------------------------------
81226e22ab8SChristopher Smithinterface Doku_Handler_CallWriter_Interface {
81326e22ab8SChristopher Smith    public function writeCall($call);
81426e22ab8SChristopher Smith    public function writeCalls($calls);
81526e22ab8SChristopher Smith    public function finalise();
81626e22ab8SChristopher Smith}
81726e22ab8SChristopher Smith
81826e22ab8SChristopher Smithclass Doku_Handler_CallWriter implements Doku_Handler_CallWriter_Interface {
8190cecf9d5Sandi
8200cecf9d5Sandi    var $Handler;
8210cecf9d5Sandi
82242ea7f44SGerrit Uitslag    /**
82342ea7f44SGerrit Uitslag     * @param Doku_Handler $Handler
82442ea7f44SGerrit Uitslag     */
82526e22ab8SChristopher Smith    function __construct(Doku_Handler $Handler) {
82626e22ab8SChristopher Smith        $this->Handler = $Handler;
8270cecf9d5Sandi    }
8280cecf9d5Sandi
8290cecf9d5Sandi    function writeCall($call) {
8300cecf9d5Sandi        $this->Handler->calls[] = $call;
8310cecf9d5Sandi    }
8320cecf9d5Sandi
8330cecf9d5Sandi    function writeCalls($calls) {
8340cecf9d5Sandi        $this->Handler->calls = array_merge($this->Handler->calls, $calls);
8350cecf9d5Sandi    }
836f4f02a0fSchris
837f4f02a0fSchris    // function is required, but since this call writer is first/highest in
838f4f02a0fSchris    // the chain it is not required to do anything
839f4f02a0fSchris    function finalise() {
8403893df8eSChristopher Smith        unset($this->Handler);
841f4f02a0fSchris    }
8420cecf9d5Sandi}
8430cecf9d5Sandi
8440cecf9d5Sandi//------------------------------------------------------------------------
8455587e44cSchris/**
8465587e44cSchris * Generic call writer class to handle nesting of rendering instructions
8475587e44cSchris * within a render instruction. Also see nest() method of renderer base class
8485587e44cSchris *
8495587e44cSchris * @author    Chris Smith <chris@jalakai.co.uk>
8505587e44cSchris */
85126e22ab8SChristopher Smithclass Doku_Handler_Nest implements Doku_Handler_CallWriter_Interface {
8525587e44cSchris
8535587e44cSchris    var $CallWriter;
8545587e44cSchris    var $calls = array();
8555587e44cSchris
8565587e44cSchris    var $closingInstruction;
8575587e44cSchris
8585587e44cSchris    /**
8595587e44cSchris     * constructor
8605587e44cSchris     *
861074b2b3fSTakamura     * @param  Doku_Handler_CallWriter $CallWriter     the renderers current call writer
8625587e44cSchris     * @param  string     $close          closing instruction name, this is required to properly terminate the
8635587e44cSchris     *                                    syntax mode if the document ends without a closing pattern
8645587e44cSchris     */
86526e22ab8SChristopher Smith    function __construct(Doku_Handler_CallWriter_Interface $CallWriter, $close="nest_close") {
86626e22ab8SChristopher Smith        $this->CallWriter = $CallWriter;
8675587e44cSchris
8685587e44cSchris        $this->closingInstruction = $close;
8695587e44cSchris    }
8705587e44cSchris
8715587e44cSchris    function writeCall($call) {
8725587e44cSchris        $this->calls[] = $call;
8735587e44cSchris    }
8745587e44cSchris
8755587e44cSchris    function writeCalls($calls) {
8765587e44cSchris        $this->calls = array_merge($this->calls, $calls);
8775587e44cSchris    }
8785587e44cSchris
8795587e44cSchris    function finalise() {
8805587e44cSchris        $last_call = end($this->calls);
8815587e44cSchris        $this->writeCall(array($this->closingInstruction,array(), $last_call[2]));
8825587e44cSchris
8835587e44cSchris        $this->process();
8845587e44cSchris        $this->CallWriter->finalise();
8853893df8eSChristopher Smith        unset($this->CallWriter);
8865587e44cSchris    }
8875587e44cSchris
8885587e44cSchris    function process() {
88941624b31SChris Smith        // merge consecutive cdata
89041624b31SChris Smith        $unmerged_calls = $this->calls;
89141624b31SChris Smith        $this->calls = array();
89241624b31SChris Smith
89341624b31SChris Smith        foreach ($unmerged_calls as $call) $this->addCall($call);
89441624b31SChris Smith
8955587e44cSchris        $first_call = reset($this->calls);
8965587e44cSchris        $this->CallWriter->writeCall(array("nest", array($this->calls), $first_call[2]));
8975587e44cSchris    }
89841624b31SChris Smith
89941624b31SChris Smith    function addCall($call) {
90041624b31SChris Smith        $key = count($this->calls);
90141624b31SChris Smith        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
90241624b31SChris Smith            $this->calls[$key-1][1][0] .= $call[1][0];
90373c47f3dSChris Smith        } else if ($call[0] == 'eol') {
90473c47f3dSChris Smith            // do nothing (eol shouldn't be allowed, to counter preformatted fix in #1652 & #1699)
90541624b31SChris Smith        } else {
90641624b31SChris Smith            $this->calls[] = $call;
90741624b31SChris Smith        }
90841624b31SChris Smith    }
9095587e44cSchris}
9105587e44cSchris
91126e22ab8SChristopher Smithclass Doku_Handler_List implements Doku_Handler_CallWriter_Interface {
9120cecf9d5Sandi
9130cecf9d5Sandi    var $CallWriter;
9140cecf9d5Sandi
9150cecf9d5Sandi    var $calls = array();
9160cecf9d5Sandi    var $listCalls = array();
9170cecf9d5Sandi    var $listStack = array();
9180cecf9d5Sandi
91910bcc8aaSChristopher Smith    const NODE = 1;
92010bcc8aaSChristopher Smith
92126e22ab8SChristopher Smith    function __construct(Doku_Handler_CallWriter_Interface $CallWriter) {
92226e22ab8SChristopher Smith        $this->CallWriter = $CallWriter;
9230cecf9d5Sandi    }
9240cecf9d5Sandi
9250cecf9d5Sandi    function writeCall($call) {
9260cecf9d5Sandi        $this->calls[] = $call;
9270cecf9d5Sandi    }
9280cecf9d5Sandi
9290cecf9d5Sandi    // Probably not needed but just in case...
9300cecf9d5Sandi    function writeCalls($calls) {
9310cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
932f4f02a0fSchris#        $this->CallWriter->writeCalls($this->calls);
933f4f02a0fSchris    }
934f4f02a0fSchris
935f4f02a0fSchris    function finalise() {
936f4f02a0fSchris        $last_call = end($this->calls);
937f4f02a0fSchris        $this->writeCall(array('list_close',array(), $last_call[2]));
938f4f02a0fSchris
939f4f02a0fSchris        $this->process();
940f4f02a0fSchris        $this->CallWriter->finalise();
9413893df8eSChristopher Smith        unset($this->CallWriter);
9420cecf9d5Sandi    }
9430cecf9d5Sandi
9440cecf9d5Sandi    //------------------------------------------------------------------------
9450cecf9d5Sandi    function process() {
946f4f02a0fSchris
9470cecf9d5Sandi        foreach ( $this->calls as $call ) {
9480cecf9d5Sandi            switch ($call[0]) {
9490cecf9d5Sandi                case 'list_item':
9500cecf9d5Sandi                    $this->listOpen($call);
9510cecf9d5Sandi                break;
9520cecf9d5Sandi                case 'list_open':
9530cecf9d5Sandi                    $this->listStart($call);
9540cecf9d5Sandi                break;
9550cecf9d5Sandi                case 'list_close':
9560cecf9d5Sandi                    $this->listEnd($call);
9570cecf9d5Sandi                break;
9580cecf9d5Sandi                default:
9590cecf9d5Sandi                    $this->listContent($call);
9600cecf9d5Sandi                break;
9610cecf9d5Sandi            }
9620cecf9d5Sandi        }
9630cecf9d5Sandi
9640cecf9d5Sandi        $this->CallWriter->writeCalls($this->listCalls);
9650cecf9d5Sandi    }
9660cecf9d5Sandi
9670cecf9d5Sandi    //------------------------------------------------------------------------
9680cecf9d5Sandi    function listStart($call) {
9690cecf9d5Sandi        $depth = $this->interpretSyntax($call[1][0], $listType);
9700cecf9d5Sandi
9710cecf9d5Sandi        $this->initialDepth = $depth;
97210bcc8aaSChristopher Smith        //                   array(list type, current depth, index of current listitem_open)
97310bcc8aaSChristopher Smith        $this->listStack[] = array($listType, $depth, 1);
9740cecf9d5Sandi
9750cecf9d5Sandi        $this->listCalls[] = array('list'.$listType.'_open',array(),$call[2]);
9760cecf9d5Sandi        $this->listCalls[] = array('listitem_open',array(1),$call[2]);
9770cecf9d5Sandi        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9780cecf9d5Sandi    }
9790cecf9d5Sandi
9800cecf9d5Sandi    //------------------------------------------------------------------------
9810cecf9d5Sandi    function listEnd($call) {
98244881bd0Shenning.noren        $closeContent = true;
9830cecf9d5Sandi
9840cecf9d5Sandi        while ( $list = array_pop($this->listStack) ) {
9850cecf9d5Sandi            if ( $closeContent ) {
9860cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
98744881bd0Shenning.noren                $closeContent = false;
9880cecf9d5Sandi            }
9890cecf9d5Sandi            $this->listCalls[] = array('listitem_close',array(),$call[2]);
9900cecf9d5Sandi            $this->listCalls[] = array('list'.$list[0].'_close', array(), $call[2]);
9910cecf9d5Sandi        }
9920cecf9d5Sandi    }
9930cecf9d5Sandi
9940cecf9d5Sandi    //------------------------------------------------------------------------
9950cecf9d5Sandi    function listOpen($call) {
9960cecf9d5Sandi        $depth = $this->interpretSyntax($call[1][0], $listType);
9970cecf9d5Sandi        $end = end($this->listStack);
99810bcc8aaSChristopher Smith        $key = key($this->listStack);
9990cecf9d5Sandi
10000cecf9d5Sandi        // Not allowed to be shallower than initialDepth
10010cecf9d5Sandi        if ( $depth < $this->initialDepth ) {
10020cecf9d5Sandi            $depth = $this->initialDepth;
10030cecf9d5Sandi        }
10040cecf9d5Sandi
10050cecf9d5Sandi        //------------------------------------------------------------------------
10060cecf9d5Sandi        if ( $depth == $end[1] ) {
10070cecf9d5Sandi
10080cecf9d5Sandi            // Just another item in the list...
10090cecf9d5Sandi            if ( $listType == $end[0] ) {
10100cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
10110cecf9d5Sandi                $this->listCalls[] = array('listitem_close',array(),$call[2]);
10120cecf9d5Sandi                $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
10130cecf9d5Sandi                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
10140cecf9d5Sandi
101510bcc8aaSChristopher Smith                // new list item, update list stack's index into current listitem_open
101610bcc8aaSChristopher Smith                $this->listStack[$key][2] = count($this->listCalls) - 2;
101710bcc8aaSChristopher Smith
10180cecf9d5Sandi            // Switched list type...
10190cecf9d5Sandi            } else {
10200cecf9d5Sandi
10210cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
10220cecf9d5Sandi                $this->listCalls[] = array('listitem_close',array(),$call[2]);
10230cecf9d5Sandi                $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
10240cecf9d5Sandi                $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
10250cecf9d5Sandi                $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
10260cecf9d5Sandi                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
10270cecf9d5Sandi
10280cecf9d5Sandi                array_pop($this->listStack);
102910bcc8aaSChristopher Smith                $this->listStack[] = array($listType, $depth, count($this->listCalls) - 2);
10300cecf9d5Sandi            }
10310cecf9d5Sandi
10320cecf9d5Sandi        //------------------------------------------------------------------------
10330cecf9d5Sandi        // Getting deeper...
10340cecf9d5Sandi        } else if ( $depth > $end[1] ) {
10350cecf9d5Sandi
10360cecf9d5Sandi            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
10370cecf9d5Sandi            $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
10380cecf9d5Sandi            $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
10390cecf9d5Sandi            $this->listCalls[] = array('listcontent_open',array(),$call[2]);
10400cecf9d5Sandi
104110bcc8aaSChristopher Smith            // set the node/leaf state of this item's parent listitem_open to NODE
104210bcc8aaSChristopher Smith            $this->listCalls[$this->listStack[$key][2]][1][1] = self::NODE;
104310bcc8aaSChristopher Smith
104410bcc8aaSChristopher Smith            $this->listStack[] = array($listType, $depth, count($this->listCalls) - 2);
10450cecf9d5Sandi
10460cecf9d5Sandi        //------------------------------------------------------------------------
10470cecf9d5Sandi        // Getting shallower ( $depth < $end[1] )
10480cecf9d5Sandi        } else {
10490cecf9d5Sandi            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
10500cecf9d5Sandi            $this->listCalls[] = array('listitem_close',array(),$call[2]);
10510cecf9d5Sandi            $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
10520cecf9d5Sandi
10530cecf9d5Sandi            // Throw away the end - done
10540cecf9d5Sandi            array_pop($this->listStack);
10550cecf9d5Sandi
10560cecf9d5Sandi            while (1) {
10570cecf9d5Sandi                $end = end($this->listStack);
105810bcc8aaSChristopher Smith                $key = key($this->listStack);
10590cecf9d5Sandi
10600cecf9d5Sandi                if ( $end[1] <= $depth ) {
10610cecf9d5Sandi
10620cecf9d5Sandi                    // Normalize depths
10630cecf9d5Sandi                    $depth = $end[1];
10640cecf9d5Sandi
10650cecf9d5Sandi                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
10660cecf9d5Sandi
10670cecf9d5Sandi                    if ( $end[0] == $listType ) {
10680cecf9d5Sandi                        $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
10690cecf9d5Sandi                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
10700cecf9d5Sandi
107110bcc8aaSChristopher Smith                        // new list item, update list stack's index into current listitem_open
107210bcc8aaSChristopher Smith                        $this->listStack[$key][2] = count($this->listCalls) - 2;
107310bcc8aaSChristopher Smith
10740cecf9d5Sandi                    } else {
10750cecf9d5Sandi                        // Switching list type...
10760cecf9d5Sandi                        $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
10770cecf9d5Sandi                        $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
10780cecf9d5Sandi                        $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
10790cecf9d5Sandi                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
10800cecf9d5Sandi
10810cecf9d5Sandi                        array_pop($this->listStack);
108210bcc8aaSChristopher Smith                        $this->listStack[] = array($listType, $depth, count($this->listCalls) - 2);
10830cecf9d5Sandi                    }
10840cecf9d5Sandi
10850cecf9d5Sandi                    break;
10860cecf9d5Sandi
10870cecf9d5Sandi                // Haven't dropped down far enough yet.... ( $end[1] > $depth )
10880cecf9d5Sandi                } else {
10890cecf9d5Sandi
10900cecf9d5Sandi                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
10910cecf9d5Sandi                    $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
10920cecf9d5Sandi
10930cecf9d5Sandi                    array_pop($this->listStack);
10940cecf9d5Sandi
10950cecf9d5Sandi                }
10960cecf9d5Sandi
10970cecf9d5Sandi            }
10980cecf9d5Sandi
10990cecf9d5Sandi        }
11000cecf9d5Sandi    }
11010cecf9d5Sandi
11020cecf9d5Sandi    //------------------------------------------------------------------------
11030cecf9d5Sandi    function listContent($call) {
11040cecf9d5Sandi        $this->listCalls[] = $call;
11050cecf9d5Sandi    }
11060cecf9d5Sandi
11070cecf9d5Sandi    //------------------------------------------------------------------------
11080cecf9d5Sandi    function interpretSyntax($match, & $type) {
11090cecf9d5Sandi        if ( substr($match,-1) == '*' ) {
11100cecf9d5Sandi            $type = 'u';
11110cecf9d5Sandi        } else {
11120cecf9d5Sandi            $type = 'o';
11130cecf9d5Sandi        }
11144b7f9e70STom N Harris        // Is the +1 needed? It used to be count(explode(...))
11154b7f9e70STom N Harris        // but I don't think the number is seen outside this handler
11164b7f9e70STom N Harris        return substr_count(str_replace("\t",'  ',$match), '  ') + 1;
11170cecf9d5Sandi    }
11180cecf9d5Sandi}
11190cecf9d5Sandi
11200cecf9d5Sandi//------------------------------------------------------------------------
112126e22ab8SChristopher Smithclass Doku_Handler_Preformatted implements Doku_Handler_CallWriter_Interface {
11220cecf9d5Sandi
11230cecf9d5Sandi    var $CallWriter;
11240cecf9d5Sandi
11250cecf9d5Sandi    var $calls = array();
11260cecf9d5Sandi    var $pos;
11270cecf9d5Sandi    var $text ='';
11280cecf9d5Sandi
11290cecf9d5Sandi
11300cecf9d5Sandi
113126e22ab8SChristopher Smith    function __construct(Doku_Handler_CallWriter_Interface $CallWriter) {
113226e22ab8SChristopher Smith        $this->CallWriter = $CallWriter;
11330cecf9d5Sandi    }
11340cecf9d5Sandi
11350cecf9d5Sandi    function writeCall($call) {
11360cecf9d5Sandi        $this->calls[] = $call;
11370cecf9d5Sandi    }
11380cecf9d5Sandi
11390cecf9d5Sandi    // Probably not needed but just in case...
11400cecf9d5Sandi    function writeCalls($calls) {
11410cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1142f4f02a0fSchris#        $this->CallWriter->writeCalls($this->calls);
1143f4f02a0fSchris    }
1144f4f02a0fSchris
1145f4f02a0fSchris    function finalise() {
1146f4f02a0fSchris        $last_call = end($this->calls);
1147f4f02a0fSchris        $this->writeCall(array('preformatted_end',array(), $last_call[2]));
1148f4f02a0fSchris
1149f4f02a0fSchris        $this->process();
1150f4f02a0fSchris        $this->CallWriter->finalise();
11513893df8eSChristopher Smith        unset($this->CallWriter);
11520cecf9d5Sandi    }
11530cecf9d5Sandi
11540cecf9d5Sandi    function process() {
11550cecf9d5Sandi        foreach ( $this->calls as $call ) {
11560cecf9d5Sandi            switch ($call[0]) {
11570cecf9d5Sandi                case 'preformatted_start':
11580cecf9d5Sandi                    $this->pos = $call[2];
11590cecf9d5Sandi                break;
11600cecf9d5Sandi                case 'preformatted_newline':
11610cecf9d5Sandi                    $this->text .= "\n";
11620cecf9d5Sandi                break;
11630cecf9d5Sandi                case 'preformatted_content':
11640cecf9d5Sandi                    $this->text .= $call[1][0];
11650cecf9d5Sandi                break;
11660cecf9d5Sandi                case 'preformatted_end':
116793a34bf3SChris Smith                    if (trim($this->text)) {
11680cecf9d5Sandi                        $this->CallWriter->writeCall(array('preformatted',array($this->text),$this->pos));
116993a34bf3SChris Smith                    }
117095c19ce7SChris Smith                    // see FS#1699 & FS#1652, add 'eol' instructions to ensure proper triggering of following p_open
117195c19ce7SChris Smith                    $this->CallWriter->writeCall(array('eol',array(),$this->pos));
117295c19ce7SChris Smith                    $this->CallWriter->writeCall(array('eol',array(),$this->pos));
11730cecf9d5Sandi                break;
11740cecf9d5Sandi            }
11750cecf9d5Sandi        }
11760cecf9d5Sandi    }
1177f4f02a0fSchris
11780cecf9d5Sandi}
11790cecf9d5Sandi
11800cecf9d5Sandi//------------------------------------------------------------------------
118126e22ab8SChristopher Smithclass Doku_Handler_Quote implements Doku_Handler_CallWriter_Interface {
11820cecf9d5Sandi
11830cecf9d5Sandi    var $CallWriter;
11840cecf9d5Sandi
11850cecf9d5Sandi    var $calls = array();
11860cecf9d5Sandi
11870cecf9d5Sandi    var $quoteCalls = array();
11880cecf9d5Sandi
118926e22ab8SChristopher Smith    function __construct(Doku_Handler_CallWriter_Interface $CallWriter) {
119026e22ab8SChristopher Smith        $this->CallWriter = $CallWriter;
11910cecf9d5Sandi    }
11920cecf9d5Sandi
11930cecf9d5Sandi    function writeCall($call) {
11940cecf9d5Sandi        $this->calls[] = $call;
11950cecf9d5Sandi    }
11960cecf9d5Sandi
11970cecf9d5Sandi    // Probably not needed but just in case...
11980cecf9d5Sandi    function writeCalls($calls) {
11990cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1200f4f02a0fSchris    }
1201f4f02a0fSchris
1202f4f02a0fSchris    function finalise() {
1203f4f02a0fSchris        $last_call = end($this->calls);
1204f4f02a0fSchris        $this->writeCall(array('quote_end',array(), $last_call[2]));
1205f4f02a0fSchris
1206f4f02a0fSchris        $this->process();
1207f4f02a0fSchris        $this->CallWriter->finalise();
12083893df8eSChristopher Smith        unset($this->CallWriter);
12090cecf9d5Sandi    }
12100cecf9d5Sandi
12110cecf9d5Sandi    function process() {
12120cecf9d5Sandi
12130cecf9d5Sandi        $quoteDepth = 1;
12140cecf9d5Sandi
12150cecf9d5Sandi        foreach ( $this->calls as $call ) {
12160cecf9d5Sandi            switch ($call[0]) {
12170cecf9d5Sandi
12180cecf9d5Sandi                case 'quote_start':
12190cecf9d5Sandi
12200cecf9d5Sandi                    $this->quoteCalls[] = array('quote_open',array(),$call[2]);
12210cecf9d5Sandi
12220cecf9d5Sandi                case 'quote_newline':
12230cecf9d5Sandi
12240cecf9d5Sandi                    $quoteLength = $this->getDepth($call[1][0]);
12250cecf9d5Sandi
12260cecf9d5Sandi                    if ( $quoteLength > $quoteDepth ) {
12270cecf9d5Sandi                        $quoteDiff = $quoteLength - $quoteDepth;
12280cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
12290cecf9d5Sandi                            $this->quoteCalls[] = array('quote_open',array(),$call[2]);
12300cecf9d5Sandi                        }
12310cecf9d5Sandi                    } else if ( $quoteLength < $quoteDepth ) {
12320cecf9d5Sandi                        $quoteDiff = $quoteDepth - $quoteLength;
12330cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
12340cecf9d5Sandi                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
12350cecf9d5Sandi                        }
123626426c64Schris                    } else {
123726426c64Schris                        if ($call[0] != 'quote_start') $this->quoteCalls[] = array('linebreak',array(),$call[2]);
12380cecf9d5Sandi                    }
12390cecf9d5Sandi
12400cecf9d5Sandi                    $quoteDepth = $quoteLength;
12410cecf9d5Sandi
12420cecf9d5Sandi                break;
12430cecf9d5Sandi
12440cecf9d5Sandi                case 'quote_end':
12450cecf9d5Sandi
12460cecf9d5Sandi                    if ( $quoteDepth > 1 ) {
12470cecf9d5Sandi                        $quoteDiff = $quoteDepth - 1;
12480cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
12490cecf9d5Sandi                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
12500cecf9d5Sandi                        }
12510cecf9d5Sandi                    }
12520cecf9d5Sandi
12530cecf9d5Sandi                    $this->quoteCalls[] = array('quote_close',array(),$call[2]);
12540cecf9d5Sandi
12550cecf9d5Sandi                    $this->CallWriter->writeCalls($this->quoteCalls);
12560cecf9d5Sandi                break;
12570cecf9d5Sandi
12580cecf9d5Sandi                default:
12590cecf9d5Sandi                    $this->quoteCalls[] = $call;
12600cecf9d5Sandi                break;
12610cecf9d5Sandi            }
12620cecf9d5Sandi        }
12630cecf9d5Sandi    }
12640cecf9d5Sandi
12650cecf9d5Sandi    function getDepth($marker) {
12660cecf9d5Sandi        preg_match('/>{1,}/', $marker, $matches);
12670cecf9d5Sandi        $quoteLength = strlen($matches[0]);
12680cecf9d5Sandi        return $quoteLength;
12690cecf9d5Sandi    }
12700cecf9d5Sandi}
12710cecf9d5Sandi
12720cecf9d5Sandi//------------------------------------------------------------------------
127326e22ab8SChristopher Smithclass Doku_Handler_Table implements Doku_Handler_CallWriter_Interface {
12740cecf9d5Sandi
12750cecf9d5Sandi    var $CallWriter;
12760cecf9d5Sandi
12770cecf9d5Sandi    var $calls = array();
12780cecf9d5Sandi    var $tableCalls = array();
12790cecf9d5Sandi    var $maxCols = 0;
12800cecf9d5Sandi    var $maxRows = 1;
12810cecf9d5Sandi    var $currentCols = 0;
128244881bd0Shenning.noren    var $firstCell = false;
12830cecf9d5Sandi    var $lastCellType = 'tablecell';
12849060b8b0SChristopher Smith    var $inTableHead = true;
1285cef031c1SChristopher Smith    var $currentRow = array('tableheader' => 0, 'tablecell' => 0);
12869060b8b0SChristopher Smith    var $countTableHeadRows = 0;
12870cecf9d5Sandi
128826e22ab8SChristopher Smith    function __construct(Doku_Handler_CallWriter_Interface $CallWriter) {
128926e22ab8SChristopher Smith        $this->CallWriter = $CallWriter;
12900cecf9d5Sandi    }
12910cecf9d5Sandi
12920cecf9d5Sandi    function writeCall($call) {
12930cecf9d5Sandi        $this->calls[] = $call;
12940cecf9d5Sandi    }
12950cecf9d5Sandi
12960cecf9d5Sandi    // Probably not needed but just in case...
12970cecf9d5Sandi    function writeCalls($calls) {
12980cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1299f4f02a0fSchris    }
1300f4f02a0fSchris
1301f4f02a0fSchris    function finalise() {
1302f4f02a0fSchris        $last_call = end($this->calls);
1303f4f02a0fSchris        $this->writeCall(array('table_end',array(), $last_call[2]));
1304f4f02a0fSchris
1305f4f02a0fSchris        $this->process();
1306f4f02a0fSchris        $this->CallWriter->finalise();
13073893df8eSChristopher Smith        unset($this->CallWriter);
13080cecf9d5Sandi    }
13090cecf9d5Sandi
13100cecf9d5Sandi    //------------------------------------------------------------------------
13110cecf9d5Sandi    function process() {
13120cecf9d5Sandi        foreach ( $this->calls as $call ) {
13130cecf9d5Sandi            switch ( $call[0] ) {
13140cecf9d5Sandi                case 'table_start':
13150cecf9d5Sandi                    $this->tableStart($call);
13160cecf9d5Sandi                break;
13170cecf9d5Sandi                case 'table_row':
1318aa92c4ccSAdrian Lang                    $this->tableRowClose($call);
13190cecf9d5Sandi                    $this->tableRowOpen(array('tablerow_open',$call[1],$call[2]));
13200cecf9d5Sandi                break;
13210cecf9d5Sandi                case 'tableheader':
13220cecf9d5Sandi                case 'tablecell':
13230cecf9d5Sandi                    $this->tableCell($call);
13240cecf9d5Sandi                break;
13250cecf9d5Sandi                case 'table_end':
1326aa92c4ccSAdrian Lang                    $this->tableRowClose($call);
13270cecf9d5Sandi                    $this->tableEnd($call);
13280cecf9d5Sandi                break;
13290cecf9d5Sandi                default:
13300cecf9d5Sandi                    $this->tableDefault($call);
13310cecf9d5Sandi                break;
13320cecf9d5Sandi            }
13330cecf9d5Sandi        }
13340cecf9d5Sandi        $this->CallWriter->writeCalls($this->tableCalls);
13350cecf9d5Sandi    }
13360cecf9d5Sandi
13370cecf9d5Sandi    function tableStart($call) {
133890df9a4dSAdrian Lang        $this->tableCalls[] = array('table_open',$call[1],$call[2]);
13390cecf9d5Sandi        $this->tableCalls[] = array('tablerow_open',array(),$call[2]);
134044881bd0Shenning.noren        $this->firstCell = true;
13410cecf9d5Sandi    }
13420cecf9d5Sandi
13430cecf9d5Sandi    function tableEnd($call) {
134407c2b1c7SAdrian Lang        $this->tableCalls[] = array('table_close',$call[1],$call[2]);
13450cecf9d5Sandi        $this->finalizeTable();
13460cecf9d5Sandi    }
13470cecf9d5Sandi
13480cecf9d5Sandi    function tableRowOpen($call) {
13490cecf9d5Sandi        $this->tableCalls[] = $call;
13500cecf9d5Sandi        $this->currentCols = 0;
135144881bd0Shenning.noren        $this->firstCell = true;
13520cecf9d5Sandi        $this->lastCellType = 'tablecell';
13530cecf9d5Sandi        $this->maxRows++;
1354cef031c1SChristopher Smith        if ($this->inTableHead) {
1355cef031c1SChristopher Smith            $this->currentRow = array('tablecell' => 0, 'tableheader' => 0);
1356cef031c1SChristopher Smith        }
13570cecf9d5Sandi    }
13580cecf9d5Sandi
13590cecf9d5Sandi    function tableRowClose($call) {
1360cef031c1SChristopher Smith        if ($this->inTableHead && ($this->inTableHead = $this->isTableHeadRow())) {
13619060b8b0SChristopher Smith            $this->countTableHeadRows++;
13629060b8b0SChristopher Smith        }
13630cecf9d5Sandi        // Strip off final cell opening and anything after it
13640cecf9d5Sandi        while ( $discard = array_pop($this->tableCalls ) ) {
13650cecf9d5Sandi
13660cecf9d5Sandi            if ( $discard[0] == 'tablecell_open' || $discard[0] == 'tableheader_open') {
13670cecf9d5Sandi                break;
13680cecf9d5Sandi            }
1369cef031c1SChristopher Smith            if (!empty($this->currentRow[$discard[0]])) {
1370cef031c1SChristopher Smith                $this->currentRow[$discard[0]]--;
1371cef031c1SChristopher Smith            }
13720cecf9d5Sandi        }
1373aa92c4ccSAdrian Lang        $this->tableCalls[] = array('tablerow_close', array(), $call[2]);
13740cecf9d5Sandi
13750cecf9d5Sandi        if ( $this->currentCols > $this->maxCols ) {
13760cecf9d5Sandi            $this->maxCols = $this->currentCols;
13770cecf9d5Sandi        }
13780cecf9d5Sandi    }
13790cecf9d5Sandi
1380cef031c1SChristopher Smith    function isTableHeadRow() {
1381cef031c1SChristopher Smith        $td = $this->currentRow['tablecell'];
1382cef031c1SChristopher Smith        $th = $this->currentRow['tableheader'];
1383cef031c1SChristopher Smith
1384cef031c1SChristopher Smith        if (!$th || $td > 2) return false;
1385cef031c1SChristopher Smith        if (2*$td > $th) return false;
1386cef031c1SChristopher Smith
1387cef031c1SChristopher Smith        return true;
1388cef031c1SChristopher Smith    }
1389cef031c1SChristopher Smith
13900cecf9d5Sandi    function tableCell($call) {
1391cef031c1SChristopher Smith        if ($this->inTableHead) {
1392cef031c1SChristopher Smith            $this->currentRow[$call[0]]++;
13939060b8b0SChristopher Smith        }
13940cecf9d5Sandi        if ( !$this->firstCell ) {
13950cecf9d5Sandi
13960cecf9d5Sandi            // Increase the span
13970cecf9d5Sandi            $lastCall = end($this->tableCalls);
13980cecf9d5Sandi
13990cecf9d5Sandi            // A cell call which follows an open cell means an empty cell so span
14000cecf9d5Sandi            if ( $lastCall[0] == 'tablecell_open' || $lastCall[0] == 'tableheader_open' ) {
14010cecf9d5Sandi                 $this->tableCalls[] = array('colspan',array(),$call[2]);
14020cecf9d5Sandi
14030cecf9d5Sandi            }
14040cecf9d5Sandi
14050cecf9d5Sandi            $this->tableCalls[] = array($this->lastCellType.'_close',array(),$call[2]);
14060ea51e63SMatt Perry            $this->tableCalls[] = array($call[0].'_open',array(1,null,1),$call[2]);
14070cecf9d5Sandi            $this->lastCellType = $call[0];
14080cecf9d5Sandi
14090cecf9d5Sandi        } else {
14100cecf9d5Sandi
14110ea51e63SMatt Perry            $this->tableCalls[] = array($call[0].'_open',array(1,null,1),$call[2]);
14120cecf9d5Sandi            $this->lastCellType = $call[0];
141344881bd0Shenning.noren            $this->firstCell = false;
14140cecf9d5Sandi
14150cecf9d5Sandi        }
14160cecf9d5Sandi
14170cecf9d5Sandi        $this->currentCols++;
14180cecf9d5Sandi    }
14190cecf9d5Sandi
14200cecf9d5Sandi    function tableDefault($call) {
14210cecf9d5Sandi        $this->tableCalls[] = $call;
14220cecf9d5Sandi    }
14230cecf9d5Sandi
14240cecf9d5Sandi    function finalizeTable() {
14250cecf9d5Sandi
14260cecf9d5Sandi        // Add the max cols and rows to the table opening
14270cecf9d5Sandi        if ( $this->tableCalls[0][0] == 'table_open' ) {
14280cecf9d5Sandi            // Adjust to num cols not num col delimeters
14290cecf9d5Sandi            $this->tableCalls[0][1][] = $this->maxCols - 1;
14300cecf9d5Sandi            $this->tableCalls[0][1][] = $this->maxRows;
143190df9a4dSAdrian Lang            $this->tableCalls[0][1][] = array_shift($this->tableCalls[0][1]);
14320cecf9d5Sandi        } else {
14330cecf9d5Sandi            trigger_error('First element in table call list is not table_open');
14340cecf9d5Sandi        }
14350cecf9d5Sandi
14360cecf9d5Sandi        $lastRow = 0;
14370cecf9d5Sandi        $lastCell = 0;
143825b97867Shakan.sandell        $cellKey = array();
14390cecf9d5Sandi        $toDelete = array();
14400cecf9d5Sandi
1441cef031c1SChristopher Smith        // if still in tableheader, then there can be no table header
1442cef031c1SChristopher Smith        // as all rows can't be within <THEAD>
1443cef031c1SChristopher Smith        if ($this->inTableHead) {
1444cef031c1SChristopher Smith            $this->inTableHead = false;
1445cef031c1SChristopher Smith            $this->countTableHeadRows = 0;
1446cef031c1SChristopher Smith        }
1447cef031c1SChristopher Smith
14480cecf9d5Sandi        // Look for the colspan elements and increment the colspan on the
14490cecf9d5Sandi        // previous non-empty opening cell. Once done, delete all the cells
14500cecf9d5Sandi        // that contain colspans
14516606d6fcSAdrian Lang        for ($key = 0 ; $key < count($this->tableCalls) ; ++$key) {
14526606d6fcSAdrian Lang            $call = $this->tableCalls[$key];
14530cecf9d5Sandi
1454aa92c4ccSAdrian Lang            switch ($call[0]) {
14559060b8b0SChristopher Smith                case 'table_open' :
14569060b8b0SChristopher Smith                    if($this->countTableHeadRows) {
14579060b8b0SChristopher Smith                        array_splice($this->tableCalls, $key+1, 0, array(
14589060b8b0SChristopher Smith                              array('tablethead_open', array(), $call[2]))
14599060b8b0SChristopher Smith                        );
14609060b8b0SChristopher Smith                    }
14619060b8b0SChristopher Smith                    break;
14629060b8b0SChristopher Smith
1463aa92c4ccSAdrian Lang                case 'tablerow_open':
14640cecf9d5Sandi
146525b97867Shakan.sandell                    $lastRow++;
146625b97867Shakan.sandell                    $lastCell = 0;
1467aa92c4ccSAdrian Lang                    break;
14680cecf9d5Sandi
1469aa92c4ccSAdrian Lang                case 'tablecell_open':
1470aa92c4ccSAdrian Lang                case 'tableheader_open':
14710cecf9d5Sandi
147225b97867Shakan.sandell                    $lastCell++;
147325b97867Shakan.sandell                    $cellKey[$lastRow][$lastCell] = $key;
1474aa92c4ccSAdrian Lang                    break;
14750cecf9d5Sandi
1476aa92c4ccSAdrian Lang                case 'table_align':
14770cecf9d5Sandi
1478e03b8b2eSAdrian Lang                    $prev = in_array($this->tableCalls[$key-1][0], array('tablecell_open', 'tableheader_open'));
1479e03b8b2eSAdrian Lang                    $next = in_array($this->tableCalls[$key+1][0], array('tablecell_close', 'tableheader_close'));
1480e03b8b2eSAdrian Lang                    // If the cell is empty, align left
1481e03b8b2eSAdrian Lang                    if ($prev && $next) {
1482e03b8b2eSAdrian Lang                        $this->tableCalls[$key-1][1][1] = 'left';
1483e03b8b2eSAdrian Lang
14840cecf9d5Sandi                    // If the previous element was a cell open, align right
1485e03b8b2eSAdrian Lang                    } elseif ($prev) {
14860cecf9d5Sandi                        $this->tableCalls[$key-1][1][1] = 'right';
14870cecf9d5Sandi
1488e03b8b2eSAdrian Lang                    // If the next element is the close of an element, align either center or left
1489e03b8b2eSAdrian Lang                    } elseif ( $next) {
149025b97867Shakan.sandell                        if ( $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] == 'right' ) {
149125b97867Shakan.sandell                            $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] = 'center';
14920cecf9d5Sandi                        } else {
149325b97867Shakan.sandell                            $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] = 'left';
14940cecf9d5Sandi                        }
14950cecf9d5Sandi
14960cecf9d5Sandi                    }
14970cecf9d5Sandi
14980cecf9d5Sandi                    // Now convert the whitespace back to cdata
14990cecf9d5Sandi                    $this->tableCalls[$key][0] = 'cdata';
1500aa92c4ccSAdrian Lang                    break;
15010cecf9d5Sandi
1502aa92c4ccSAdrian Lang                case 'colspan':
15030cecf9d5Sandi
150444881bd0Shenning.noren                    $this->tableCalls[$key-1][1][0] = false;
15050cecf9d5Sandi
150625b97867Shakan.sandell                    for($i = $key-2; $i >= $cellKey[$lastRow][1]; $i--) {
15070cecf9d5Sandi
15080cecf9d5Sandi                        if ( $this->tableCalls[$i][0] == 'tablecell_open' || $this->tableCalls[$i][0] == 'tableheader_open' ) {
15090cecf9d5Sandi
151044881bd0Shenning.noren                            if ( false !== $this->tableCalls[$i][1][0] ) {
15110cecf9d5Sandi                                $this->tableCalls[$i][1][0]++;
15120cecf9d5Sandi                                break;
15130cecf9d5Sandi                            }
15140cecf9d5Sandi
15150cecf9d5Sandi                        }
15160cecf9d5Sandi                    }
15170cecf9d5Sandi
15180cecf9d5Sandi                    $toDelete[] = $key-1;
15190cecf9d5Sandi                    $toDelete[] = $key;
15200cecf9d5Sandi                    $toDelete[] = $key+1;
1521aa92c4ccSAdrian Lang                    break;
152225b97867Shakan.sandell
1523aa92c4ccSAdrian Lang                case 'rowspan':
152425b97867Shakan.sandell
152525b97867Shakan.sandell                    if ( $this->tableCalls[$key-1][0] == 'cdata' ) {
152625b97867Shakan.sandell                        // ignore rowspan if previous call was cdata (text mixed with :::) we don't have to check next call as that wont match regex
152725b97867Shakan.sandell                        $this->tableCalls[$key][0] = 'cdata';
152825b97867Shakan.sandell
152925b97867Shakan.sandell                    } else {
153025b97867Shakan.sandell
15316606d6fcSAdrian Lang                        $spanning_cell = null;
15329060b8b0SChristopher Smith
15339060b8b0SChristopher Smith                        // can't cross thead/tbody boundary
15349060b8b0SChristopher Smith                        if (!$this->countTableHeadRows || ($lastRow-1 != $this->countTableHeadRows)) {
153525b97867Shakan.sandell                            for($i = $lastRow-1; $i > 0; $i--) {
153625b97867Shakan.sandell
153725b97867Shakan.sandell                                if ( $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tablecell_open' || $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tableheader_open' ) {
153825b97867Shakan.sandell
15396606d6fcSAdrian Lang                                    if ($this->tableCalls[$cellKey[$i][$lastCell]][1][2] >= $lastRow - $i) {
15406606d6fcSAdrian Lang                                        $spanning_cell = $i;
154125b97867Shakan.sandell                                        break;
154225b97867Shakan.sandell                                    }
154325b97867Shakan.sandell
154425b97867Shakan.sandell                                }
154525b97867Shakan.sandell                            }
15469060b8b0SChristopher Smith                        }
15476606d6fcSAdrian Lang                        if (is_null($spanning_cell)) {
15486606d6fcSAdrian Lang                            // No spanning cell found, so convert this cell to
15496606d6fcSAdrian Lang                            // an empty one to avoid broken tables
15506a7e97d5SGerrit Uitslag                            $this->tableCalls[$key][0] = 'cdata';
15516a7e97d5SGerrit Uitslag                            $this->tableCalls[$key][1][0] = '';
15526606d6fcSAdrian Lang                            continue;
15536606d6fcSAdrian Lang                        }
15546606d6fcSAdrian Lang                        $this->tableCalls[$cellKey[$spanning_cell][$lastCell]][1][2]++;
15556606d6fcSAdrian Lang
15566606d6fcSAdrian Lang                        $this->tableCalls[$key-1][1][2] = false;
155725b97867Shakan.sandell
155825b97867Shakan.sandell                        $toDelete[] = $key-1;
155925b97867Shakan.sandell                        $toDelete[] = $key;
156025b97867Shakan.sandell                        $toDelete[] = $key+1;
156125b97867Shakan.sandell                    }
1562aa92c4ccSAdrian Lang                    break;
1563aa92c4ccSAdrian Lang
15646606d6fcSAdrian Lang                case 'tablerow_close':
15656606d6fcSAdrian Lang
15666606d6fcSAdrian Lang                    // Fix broken tables by adding missing cells
1567dbd52c81SAndreas Gohr                    $moreCalls = array();
15686606d6fcSAdrian Lang                    while (++$lastCell < $this->maxCols) {
1569dbd52c81SAndreas Gohr                        $moreCalls[] = array('tablecell_open', array(1, null, 1), $call[2]);
1570dbd52c81SAndreas Gohr                        $moreCalls[] = array('cdata', array(''), $call[2]);
1571dbd52c81SAndreas Gohr                        $moreCalls[] = array('tablecell_close', array(), $call[2]);
1572dbd52c81SAndreas Gohr                    }
1573dbd52c81SAndreas Gohr                    $moreCallsLength = count($moreCalls);
1574dbd52c81SAndreas Gohr                    if($moreCallsLength) {
1575dbd52c81SAndreas Gohr                        array_splice($this->tableCalls, $key, 0, $moreCalls);
1576dbd52c81SAndreas Gohr                        $key += $moreCallsLength;
15776606d6fcSAdrian Lang                    }
15786606d6fcSAdrian Lang
15799060b8b0SChristopher Smith                    if($this->countTableHeadRows == $lastRow) {
1580f05a1cc5SGerrit Uitslag                        array_splice($this->tableCalls, $key+1, 0, array(
1581f05a1cc5SGerrit Uitslag                              array('tablethead_close', array(), $call[2])));
1582f05a1cc5SGerrit Uitslag                    }
15836606d6fcSAdrian Lang                    break;
15846606d6fcSAdrian Lang
15850cecf9d5Sandi            }
15860cecf9d5Sandi        }
15870cecf9d5Sandi
15889ab75d9eSAndreas Gohr        // condense cdata
15899ab75d9eSAndreas Gohr        $cnt = count($this->tableCalls);
15909ab75d9eSAndreas Gohr        for( $key = 0; $key < $cnt; $key++){
15919ab75d9eSAndreas Gohr            if($this->tableCalls[$key][0] == 'cdata'){
15929ab75d9eSAndreas Gohr                $ckey = $key;
15939ab75d9eSAndreas Gohr                $key++;
15949ab75d9eSAndreas Gohr                while($this->tableCalls[$key][0] == 'cdata'){
15959ab75d9eSAndreas Gohr                    $this->tableCalls[$ckey][1][0] .= $this->tableCalls[$key][1][0];
15969ab75d9eSAndreas Gohr                    $toDelete[] = $key;
15979ab75d9eSAndreas Gohr                    $key++;
15989ab75d9eSAndreas Gohr                }
15999ab75d9eSAndreas Gohr                continue;
16009ab75d9eSAndreas Gohr            }
16019ab75d9eSAndreas Gohr        }
16029ab75d9eSAndreas Gohr
16030cecf9d5Sandi        foreach ( $toDelete as $delete ) {
16040cecf9d5Sandi            unset($this->tableCalls[$delete]);
16050cecf9d5Sandi        }
16060cecf9d5Sandi        $this->tableCalls = array_values($this->tableCalls);
16070cecf9d5Sandi    }
16080cecf9d5Sandi}
16090cecf9d5Sandi
16100cecf9d5Sandi
16112a27e99aSandi/**
16122a27e99aSandi * Handler for paragraphs
16132a27e99aSandi *
16140b7c14c2Sandi * @author Harry Fuecks <hfuecks@gmail.com>
16152a27e99aSandi */
16160cecf9d5Sandiclass Doku_Handler_Block {
16170cecf9d5Sandi    var $calls = array();
16189569a107SDanny Lin    var $skipEol = false;
161953bfcb59SChristopher Smith    var $inParagraph = false;
16200cecf9d5Sandi
1621af146da0Sandi    // Blocks these should not be inside paragraphs
16220cecf9d5Sandi    var $blockOpen = array(
16230cecf9d5Sandi            'header',
1624df9add72Schris            'listu_open','listo_open','listitem_open','listcontent_open',
1625f05a1cc5SGerrit Uitslag            'table_open','tablerow_open','tablecell_open','tableheader_open','tablethead_open',
16260cecf9d5Sandi            'quote_open',
162776aa94b7Schris            'code','file','hr','preformatted','rss',
162807f89c3cSAnika Henke            'htmlblock','phpblock',
16299569a107SDanny Lin            'footnote_open',
16300cecf9d5Sandi        );
16310cecf9d5Sandi
16320cecf9d5Sandi    var $blockClose = array(
16330cecf9d5Sandi            'header',
1634df9add72Schris            'listu_close','listo_close','listitem_close','listcontent_close',
1635f05a1cc5SGerrit Uitslag            'table_close','tablerow_close','tablecell_close','tableheader_close','tablethead_close',
16360cecf9d5Sandi            'quote_close',
163776aa94b7Schris            'code','file','hr','preformatted','rss',
163807f89c3cSAnika Henke            'htmlblock','phpblock',
16399569a107SDanny Lin            'footnote_close',
16400cecf9d5Sandi        );
16410cecf9d5Sandi
1642af146da0Sandi    // Stacks can contain paragraphs
16430cecf9d5Sandi    var $stackOpen = array(
16449569a107SDanny Lin        'section_open',
16450cecf9d5Sandi        );
16460cecf9d5Sandi
16470cecf9d5Sandi    var $stackClose = array(
16489569a107SDanny Lin        'section_close',
16490cecf9d5Sandi        );
16500cecf9d5Sandi
1651af146da0Sandi
1652af146da0Sandi    /**
1653af146da0Sandi     * Constructor. Adds loaded syntax plugins to the block and stack
1654af146da0Sandi     * arrays
1655af146da0Sandi     *
1656af146da0Sandi     * @author Andreas Gohr <andi@splitbrain.org>
1657af146da0Sandi     */
165826e22ab8SChristopher Smith    function __construct(){
1659af146da0Sandi        global $DOKU_PLUGINS;
1660af146da0Sandi        //check if syntax plugins were loaded
166103c4aec3Schris        if(empty($DOKU_PLUGINS['syntax'])) return;
1662af146da0Sandi        foreach($DOKU_PLUGINS['syntax'] as $n => $p){
1663af146da0Sandi            $ptype = $p->getPType();
1664af146da0Sandi            if($ptype == 'block'){
1665af146da0Sandi                $this->blockOpen[]  = 'plugin_'.$n;
1666af146da0Sandi                $this->blockClose[] = 'plugin_'.$n;
1667af146da0Sandi            }elseif($ptype == 'stack'){
1668af146da0Sandi                $this->stackOpen[]  = 'plugin_'.$n;
1669af146da0Sandi                $this->stackClose[] = 'plugin_'.$n;
1670af146da0Sandi            }
1671af146da0Sandi        }
1672af146da0Sandi    }
1673af146da0Sandi
1674b5a0b131SDanny Lin    function openParagraph($pos){
16759569a107SDanny Lin        if ($this->inParagraph) return;
1676b5a0b131SDanny Lin        $this->calls[] = array('p_open',array(), $pos);
1677b5a0b131SDanny Lin        $this->inParagraph = true;
16789569a107SDanny Lin        $this->skipEol = true;
1679b5a0b131SDanny Lin    }
1680b5a0b131SDanny Lin
16812a27e99aSandi    /**
16822a27e99aSandi     * Close a paragraph if needed
16832a27e99aSandi     *
16842a27e99aSandi     * This function makes sure there are no empty paragraphs on the stack
16852a27e99aSandi     *
16862a27e99aSandi     * @author Andreas Gohr <andi@splitbrain.org>
1687f50a239bSTakamura     *
1688f50a239bSTakamura     * @param string|integer $pos
16892a27e99aSandi     */
1690506ae684Sandi    function closeParagraph($pos){
16919569a107SDanny Lin        if (!$this->inParagraph) return;
1692506ae684Sandi        // look back if there was any content - we don't want empty paragraphs
1693506ae684Sandi        $content = '';
1694faba9a35SAndreas Gohr        $ccount = count($this->calls);
1695faba9a35SAndreas Gohr        for($i=$ccount-1; $i>=0; $i--){
1696506ae684Sandi            if($this->calls[$i][0] == 'p_open'){
1697506ae684Sandi                break;
1698506ae684Sandi            }elseif($this->calls[$i][0] == 'cdata'){
1699506ae684Sandi                $content .= $this->calls[$i][1][0];
1700506ae684Sandi            }else{
1701506ae684Sandi                $content = 'found markup';
1702506ae684Sandi                break;
1703506ae684Sandi            }
1704506ae684Sandi        }
1705506ae684Sandi
1706506ae684Sandi        if(trim($content)==''){
1707506ae684Sandi            //remove the whole paragraph
1708a86cc527SAndreas Gohr            //array_splice($this->calls,$i); // <- this is much slower than the loop below
1709d8f7a7f3SAndreas Gohr            for($x=$ccount; $x>$i; $x--) array_pop($this->calls);
1710506ae684Sandi        }else{
17119569a107SDanny Lin            // remove ending linebreaks in the paragraph
17129569a107SDanny Lin            $i=count($this->calls)-1;
17139569a107SDanny Lin            if ($this->calls[$i][0] == 'cdata') $this->calls[$i][1][0] = rtrim($this->calls[$i][1][0],DOKU_PARSER_EOL);
1714506ae684Sandi            $this->calls[] = array('p_close',array(), $pos);
1715506ae684Sandi        }
1716e1c10e4dSchris
171744881bd0Shenning.noren        $this->inParagraph = false;
17189569a107SDanny Lin        $this->skipEol = true;
17190cecf9d5Sandi    }
172041624b31SChris Smith
172141624b31SChris Smith    function addCall($call) {
172241624b31SChris Smith        $key = count($this->calls);
172341624b31SChris Smith        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
172441624b31SChris Smith            $this->calls[$key-1][1][0] .= $call[1][0];
172541624b31SChris Smith        } else {
172641624b31SChris Smith            $this->calls[] = $call;
172741624b31SChris Smith        }
172841624b31SChris Smith    }
17299569a107SDanny Lin
17309569a107SDanny Lin    // simple version of addCall, without checking cdata
17319569a107SDanny Lin    function storeCall($call) {
17329569a107SDanny Lin        $this->calls[] = $call;
17339569a107SDanny Lin    }
17349569a107SDanny Lin
17359569a107SDanny Lin    /**
17369569a107SDanny Lin     * Processes the whole instruction stack to open and close paragraphs
17379569a107SDanny Lin     *
17389569a107SDanny Lin     * @author Harry Fuecks <hfuecks@gmail.com>
17399569a107SDanny Lin     * @author Andreas Gohr <andi@splitbrain.org>
1740f50a239bSTakamura     *
1741f50a239bSTakamura     * @param array $calls
1742f50a239bSTakamura     *
1743f50a239bSTakamura     * @return array
17449569a107SDanny Lin     */
17459569a107SDanny Lin    function process($calls) {
17469569a107SDanny Lin        // open first paragraph
17479569a107SDanny Lin        $this->openParagraph(0);
17489569a107SDanny Lin        foreach ( $calls as $key => $call ) {
17499569a107SDanny Lin            $cname = $call[0];
17509569a107SDanny Lin            if ($cname == 'plugin') {
17519569a107SDanny Lin                $cname='plugin_'.$call[1][0];
17529569a107SDanny Lin                $plugin = true;
17539569a107SDanny Lin                $plugin_open = (($call[1][2] == DOKU_LEXER_ENTER) || ($call[1][2] == DOKU_LEXER_SPECIAL));
17549569a107SDanny Lin                $plugin_close = (($call[1][2] == DOKU_LEXER_EXIT) || ($call[1][2] == DOKU_LEXER_SPECIAL));
17559569a107SDanny Lin            } else {
17569569a107SDanny Lin                $plugin = false;
17579569a107SDanny Lin            }
17589569a107SDanny Lin            /* stack */
17599569a107SDanny Lin            if ( in_array($cname,$this->stackClose ) && (!$plugin || $plugin_close)) {
17609569a107SDanny Lin                $this->closeParagraph($call[2]);
17619569a107SDanny Lin                $this->storeCall($call);
17629569a107SDanny Lin                $this->openParagraph($call[2]);
17639569a107SDanny Lin                continue;
17649569a107SDanny Lin            }
17659569a107SDanny Lin            if ( in_array($cname,$this->stackOpen ) && (!$plugin || $plugin_open) ) {
17669569a107SDanny Lin                $this->closeParagraph($call[2]);
17679569a107SDanny Lin                $this->storeCall($call);
17689569a107SDanny Lin                $this->openParagraph($call[2]);
17699569a107SDanny Lin                continue;
17709569a107SDanny Lin            }
17719569a107SDanny Lin            /* block */
17729569a107SDanny Lin            // If it's a substition it opens and closes at the same call.
17739569a107SDanny Lin            // To make sure next paragraph is correctly started, let close go first.
17749569a107SDanny Lin            if ( in_array($cname, $this->blockClose) && (!$plugin || $plugin_close)) {
17759569a107SDanny Lin                $this->closeParagraph($call[2]);
17769569a107SDanny Lin                $this->storeCall($call);
17779569a107SDanny Lin                $this->openParagraph($call[2]);
17789569a107SDanny Lin                continue;
17799569a107SDanny Lin            }
17809569a107SDanny Lin            if ( in_array($cname, $this->blockOpen) && (!$plugin || $plugin_open)) {
17819569a107SDanny Lin                $this->closeParagraph($call[2]);
17829569a107SDanny Lin                $this->storeCall($call);
17839569a107SDanny Lin                continue;
17849569a107SDanny Lin            }
17859569a107SDanny Lin            /* eol */
17869569a107SDanny Lin            if ( $cname == 'eol' ) {
17879569a107SDanny Lin                // Check this isn't an eol instruction to skip...
17889569a107SDanny Lin                if ( !$this->skipEol ) {
17899569a107SDanny Lin                    // Next is EOL => double eol => mark as paragraph
17909569a107SDanny Lin                    if ( isset($calls[$key+1]) && $calls[$key+1][0] == 'eol' ) {
17919569a107SDanny Lin                        $this->closeParagraph($call[2]);
17929569a107SDanny Lin                        $this->openParagraph($call[2]);
17939569a107SDanny Lin                    } else {
17949569a107SDanny Lin                        //if this is just a single eol make a space from it
17959569a107SDanny Lin                        $this->addCall(array('cdata',array(DOKU_PARSER_EOL), $call[2]));
17969569a107SDanny Lin                    }
17979569a107SDanny Lin                }
17989569a107SDanny Lin                continue;
17999569a107SDanny Lin            }
18009569a107SDanny Lin            /* normal */
18019569a107SDanny Lin            $this->addCall($call);
18029569a107SDanny Lin            $this->skipEol = false;
18039569a107SDanny Lin        }
18049569a107SDanny Lin        // close last paragraph
18059569a107SDanny Lin        $call = end($this->calls);
18069569a107SDanny Lin        $this->closeParagraph($call[2]);
18079569a107SDanny Lin        return $this->calls;
18089569a107SDanny Lin    }
18090cecf9d5Sandi}
18102a27e99aSandi
1811e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
1812