xref: /dokuwiki/inc/parser/handler.php (revision aa92c4ccfaf7462eb8f336c6a4e8e2b1468cfada)
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
70cecf9d5Sandi    var $Renderer = NULL;
80cecf9d5Sandi
90cecf9d5Sandi    var $CallWriter = NULL;
100cecf9d5Sandi
110cecf9d5Sandi    var $calls = array();
120cecf9d5Sandi
13e1c10e4dSchris    var $status = array(
1444881bd0Shenning.noren        'section' => false,
150cecf9d5Sandi    );
160cecf9d5Sandi
1744881bd0Shenning.noren    var $rewriteBlocks = true;
18b7c441b9SHarry Fuecks
190cecf9d5Sandi    function Doku_Handler() {
2067f9913dSAndreas Gohr        $this->CallWriter = new Doku_Handler_CallWriter($this);
210cecf9d5Sandi    }
220cecf9d5Sandi
23433bef32Sandi    function _addCall($handler, $args, $pos) {
240cecf9d5Sandi        $call = array($handler,$args, $pos);
250cecf9d5Sandi        $this->CallWriter->writeCall($call);
260cecf9d5Sandi    }
270cecf9d5Sandi
2882d61635Spierre.spring    function addPluginCall($plugin, $args, $state, $pos, $match) {
2982d61635Spierre.spring        $call = array('plugin',array($plugin, $args, $state, $match), $pos);
3004ebd214Schris        $this->CallWriter->writeCall($call);
3104ebd214Schris    }
3204ebd214Schris
33433bef32Sandi    function _finalize(){
34e1c10e4dSchris
35f4f02a0fSchris        $this->CallWriter->finalise();
36f4f02a0fSchris
37e1c10e4dSchris        if ( $this->status['section'] ) {
38e1c10e4dSchris           $last_call = end($this->calls);
39e1c10e4dSchris           array_push($this->calls,array('section_close',array(), $last_call[2]));
400cecf9d5Sandi        }
410cecf9d5Sandi
42b7c441b9SHarry Fuecks        if ( $this->rewriteBlocks ) {
4367f9913dSAndreas Gohr            $B = new Doku_Handler_Block();
440cecf9d5Sandi            $this->calls = $B->process($this->calls);
45b7c441b9SHarry Fuecks        }
46e0ad864eSchris
4724bb549bSchris        trigger_event('PARSER_HANDLER_DONE',$this);
480cecf9d5Sandi
490cecf9d5Sandi        array_unshift($this->calls,array('document_start',array(),0));
500cecf9d5Sandi        $last_call = end($this->calls);
510cecf9d5Sandi        array_push($this->calls,array('document_end',array(),$last_call[2]));
520cecf9d5Sandi    }
530cecf9d5Sandi
540cecf9d5Sandi    function fetch() {
550cecf9d5Sandi        $call = each($this->calls);
560cecf9d5Sandi        if ( $call ) {
570cecf9d5Sandi            return $call['value'];
580cecf9d5Sandi        }
5944881bd0Shenning.noren        return false;
600cecf9d5Sandi    }
61ee20e7d1Sandi
62ee20e7d1Sandi
63ee20e7d1Sandi    /**
64ee20e7d1Sandi     * Special plugin handler
65ee20e7d1Sandi     *
66ee20e7d1Sandi     * This handler is called for all modes starting with 'plugin_'.
67ee20e7d1Sandi     * An additional parameter with the plugin name is passed
68ee20e7d1Sandi     *
69ee20e7d1Sandi     * @author Andreas Gohr <andi@splitbrain.org>
70ee20e7d1Sandi     */
71ee20e7d1Sandi    function plugin($match, $state, $pos, $pluginname){
72ee20e7d1Sandi        $data = array($match);
73a46d0d65SAndreas Gohr        $plugin =& plugin_load('syntax',$pluginname);
74a46d0d65SAndreas Gohr        if($plugin != null){
75f02a7d06Schris            $data = $plugin->handle($match, $state, $pos, $this);
76ee20e7d1Sandi        }
7713ecfb18SChris Smith        if ($data !== false) {
7882d61635Spierre.spring          $this->addPluginCall($pluginname,$data,$state,$pos,$match);
7913ecfb18SChris Smith        }
8044881bd0Shenning.noren        return true;
81ee20e7d1Sandi    }
820cecf9d5Sandi
830cecf9d5Sandi    function base($match, $state, $pos) {
840cecf9d5Sandi        switch ( $state ) {
850cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
86433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
8744881bd0Shenning.noren                return true;
880cecf9d5Sandi            break;
890cecf9d5Sandi        }
900cecf9d5Sandi    }
910cecf9d5Sandi
920cecf9d5Sandi    function header($match, $state, $pos) {
93d7e8115fSAndreas Gohr        // get level and title
94a4a2d4cfSAndreas Gohr        $title = trim($match);
95a4a2d4cfSAndreas Gohr        $level = 7 - strspn($title,'=');
96d7e8115fSAndreas Gohr        if($level < 1) $level = 1;
97a4a2d4cfSAndreas Gohr        $title = trim($title,'=');
98a4a2d4cfSAndreas Gohr        $title = trim($title);
990cecf9d5Sandi
100e1c10e4dSchris        if ($this->status['section']) $this->_addCall('section_close',array(),$pos);
101e1c10e4dSchris
102433bef32Sandi        $this->_addCall('header',array($title,$level,$pos), $pos);
103e1c10e4dSchris
104e1c10e4dSchris        $this->_addCall('section_open',array($level),$pos);
10544881bd0Shenning.noren        $this->status['section'] = true;
10644881bd0Shenning.noren        return true;
1070cecf9d5Sandi    }
1080cecf9d5Sandi
1090cecf9d5Sandi    function notoc($match, $state, $pos) {
110e41c4da9SAndreas Gohr        $this->_addCall('notoc',array(),$pos);
11144881bd0Shenning.noren        return true;
1120cecf9d5Sandi    }
1130cecf9d5Sandi
1149dc2c2afSandi    function nocache($match, $state, $pos) {
1159dc2c2afSandi        $this->_addCall('nocache',array(),$pos);
11644881bd0Shenning.noren        return true;
1179dc2c2afSandi    }
1189dc2c2afSandi
1190cecf9d5Sandi    function linebreak($match, $state, $pos) {
120433bef32Sandi        $this->_addCall('linebreak',array(),$pos);
12144881bd0Shenning.noren        return true;
1220cecf9d5Sandi    }
1230cecf9d5Sandi
1240cecf9d5Sandi    function eol($match, $state, $pos) {
125433bef32Sandi        $this->_addCall('eol',array(),$pos);
12644881bd0Shenning.noren        return true;
1270cecf9d5Sandi    }
1280cecf9d5Sandi
1290cecf9d5Sandi    function hr($match, $state, $pos) {
130433bef32Sandi        $this->_addCall('hr',array(),$pos);
13144881bd0Shenning.noren        return true;
1320cecf9d5Sandi    }
1330cecf9d5Sandi
134433bef32Sandi    function _nestingTag($match, $state, $pos, $name) {
1350cecf9d5Sandi        switch ( $state ) {
1360cecf9d5Sandi            case DOKU_LEXER_ENTER:
137433bef32Sandi                $this->_addCall($name.'_open', array(), $pos);
1380cecf9d5Sandi            break;
1390cecf9d5Sandi            case DOKU_LEXER_EXIT:
140433bef32Sandi                $this->_addCall($name.'_close', array(), $pos);
1410cecf9d5Sandi            break;
1420cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
143433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
1440cecf9d5Sandi            break;
1450cecf9d5Sandi        }
1460cecf9d5Sandi    }
1470cecf9d5Sandi
1480cecf9d5Sandi    function strong($match, $state, $pos) {
149433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'strong');
15044881bd0Shenning.noren        return true;
1510cecf9d5Sandi    }
1520cecf9d5Sandi
1530cecf9d5Sandi    function emphasis($match, $state, $pos) {
154433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'emphasis');
15544881bd0Shenning.noren        return true;
1560cecf9d5Sandi    }
1570cecf9d5Sandi
1580cecf9d5Sandi    function underline($match, $state, $pos) {
159433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'underline');
16044881bd0Shenning.noren        return true;
1610cecf9d5Sandi    }
1620cecf9d5Sandi
1630cecf9d5Sandi    function monospace($match, $state, $pos) {
164433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'monospace');
16544881bd0Shenning.noren        return true;
1660cecf9d5Sandi    }
1670cecf9d5Sandi
1680cecf9d5Sandi    function subscript($match, $state, $pos) {
169433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'subscript');
17044881bd0Shenning.noren        return true;
1710cecf9d5Sandi    }
1720cecf9d5Sandi
1730cecf9d5Sandi    function superscript($match, $state, $pos) {
174433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'superscript');
17544881bd0Shenning.noren        return true;
1760cecf9d5Sandi    }
1770cecf9d5Sandi
1780cecf9d5Sandi    function deleted($match, $state, $pos) {
179433bef32Sandi        $this->_nestingTag($match, $state, $pos, 'deleted');
18044881bd0Shenning.noren        return true;
1810cecf9d5Sandi    }
1820cecf9d5Sandi
1830cecf9d5Sandi
1840cecf9d5Sandi    function footnote($match, $state, $pos) {
1855587e44cSchris//        $this->_nestingTag($match, $state, $pos, 'footnote');
186742c66f8Schris        if (!isset($this->_footnote)) $this->_footnote = false;
1872fe7363dSchris
1885587e44cSchris        switch ( $state ) {
1895587e44cSchris            case DOKU_LEXER_ENTER:
1902fe7363dSchris                // footnotes can not be nested - however due to limitations in lexer it can't be prevented
1912fe7363dSchris                // we will still enter a new footnote mode, we just do nothing
192742c66f8Schris                if ($this->_footnote) {
1932fe7363dSchris                  $this->_addCall('cdata',array($match), $pos);
1942fe7363dSchris                  break;
1952fe7363dSchris                }
1962fe7363dSchris
197742c66f8Schris                $this->_footnote = true;
1982fe7363dSchris
19967f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Nest($this->CallWriter,'footnote_close');
2005587e44cSchris                $this->CallWriter = & $ReWriter;
2014a26ad85Schris                $this->_addCall('footnote_open', array(), $pos);
2025587e44cSchris            break;
2035587e44cSchris            case DOKU_LEXER_EXIT:
2042fe7363dSchris                // check whether we have already exitted the footnote mode, can happen if the modes were nested
205742c66f8Schris                if (!$this->_footnote) {
2062fe7363dSchris                  $this->_addCall('cdata',array($match), $pos);
2072fe7363dSchris                  break;
2082fe7363dSchris                }
2092fe7363dSchris
210742c66f8Schris                $this->_footnote = false;
2112fe7363dSchris
2125587e44cSchris                $this->_addCall('footnote_close', array(), $pos);
2135587e44cSchris                $this->CallWriter->process();
2145587e44cSchris                $ReWriter = & $this->CallWriter;
2155587e44cSchris                $this->CallWriter = & $ReWriter->CallWriter;
2165587e44cSchris            break;
2175587e44cSchris            case DOKU_LEXER_UNMATCHED:
2185587e44cSchris                $this->_addCall('cdata', array($match), $pos);
2195587e44cSchris            break;
2205587e44cSchris        }
22144881bd0Shenning.noren        return true;
2220cecf9d5Sandi    }
2230cecf9d5Sandi
2240cecf9d5Sandi    function listblock($match, $state, $pos) {
2250cecf9d5Sandi        switch ( $state ) {
2260cecf9d5Sandi            case DOKU_LEXER_ENTER:
22767f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_List($this->CallWriter);
2280cecf9d5Sandi                $this->CallWriter = & $ReWriter;
229433bef32Sandi                $this->_addCall('list_open', array($match), $pos);
2300cecf9d5Sandi            break;
2310cecf9d5Sandi            case DOKU_LEXER_EXIT:
232433bef32Sandi                $this->_addCall('list_close', array(), $pos);
2330cecf9d5Sandi                $this->CallWriter->process();
2340cecf9d5Sandi                $ReWriter = & $this->CallWriter;
2350cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
2360cecf9d5Sandi            break;
2370cecf9d5Sandi            case DOKU_LEXER_MATCHED:
238433bef32Sandi                $this->_addCall('list_item', array($match), $pos);
2390cecf9d5Sandi            break;
2400cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
241433bef32Sandi                $this->_addCall('cdata', array($match), $pos);
2420cecf9d5Sandi            break;
2430cecf9d5Sandi        }
24444881bd0Shenning.noren        return true;
2450cecf9d5Sandi    }
2460cecf9d5Sandi
2470cecf9d5Sandi    function unformatted($match, $state, $pos) {
2480cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
249433bef32Sandi            $this->_addCall('unformatted',array($match), $pos);
2500cecf9d5Sandi        }
25144881bd0Shenning.noren        return true;
2520cecf9d5Sandi    }
2530cecf9d5Sandi
2540cecf9d5Sandi    function php($match, $state, $pos) {
255df9add72Schris        global $conf;
2560cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
257433bef32Sandi            $this->_addCall('php',array($match), $pos);
2580cecf9d5Sandi        }
25944881bd0Shenning.noren        return true;
2600cecf9d5Sandi    }
2610cecf9d5Sandi
26207f89c3cSAnika Henke    function phpblock($match, $state, $pos) {
26307f89c3cSAnika Henke        global $conf;
26407f89c3cSAnika Henke        if ( $state == DOKU_LEXER_UNMATCHED ) {
26507f89c3cSAnika Henke            $this->_addCall('phpblock',array($match), $pos);
26607f89c3cSAnika Henke        }
26707f89c3cSAnika Henke        return true;
26807f89c3cSAnika Henke    }
26907f89c3cSAnika Henke
2700cecf9d5Sandi    function html($match, $state, $pos) {
271df9add72Schris        global $conf;
2720cecf9d5Sandi        if ( $state == DOKU_LEXER_UNMATCHED ) {
273433bef32Sandi            $this->_addCall('html',array($match), $pos);
2740cecf9d5Sandi        }
27544881bd0Shenning.noren        return true;
2760cecf9d5Sandi    }
2770cecf9d5Sandi
27807f89c3cSAnika Henke    function htmlblock($match, $state, $pos) {
27907f89c3cSAnika Henke        global $conf;
28007f89c3cSAnika Henke        if ( $state == DOKU_LEXER_UNMATCHED ) {
28107f89c3cSAnika Henke            $this->_addCall('htmlblock',array($match), $pos);
28207f89c3cSAnika Henke        }
28307f89c3cSAnika Henke        return true;
28407f89c3cSAnika Henke    }
28507f89c3cSAnika Henke
2860cecf9d5Sandi    function preformatted($match, $state, $pos) {
2870cecf9d5Sandi        switch ( $state ) {
2880cecf9d5Sandi            case DOKU_LEXER_ENTER:
28967f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Preformatted($this->CallWriter);
2900cecf9d5Sandi                $this->CallWriter = & $ReWriter;
291433bef32Sandi                $this->_addCall('preformatted_start',array(), $pos);
2920cecf9d5Sandi            break;
2930cecf9d5Sandi            case DOKU_LEXER_EXIT:
294433bef32Sandi                $this->_addCall('preformatted_end',array(), $pos);
2950cecf9d5Sandi                $this->CallWriter->process();
2960cecf9d5Sandi                $ReWriter = & $this->CallWriter;
2970cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
2980cecf9d5Sandi            break;
2990cecf9d5Sandi            case DOKU_LEXER_MATCHED:
300433bef32Sandi                $this->_addCall('preformatted_newline',array(), $pos);
3010cecf9d5Sandi            break;
3020cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
303433bef32Sandi                $this->_addCall('preformatted_content',array($match), $pos);
3040cecf9d5Sandi            break;
3050cecf9d5Sandi        }
3060cecf9d5Sandi
30744881bd0Shenning.noren        return true;
3080cecf9d5Sandi    }
3090cecf9d5Sandi
3100cecf9d5Sandi    function quote($match, $state, $pos) {
3110cecf9d5Sandi
3120cecf9d5Sandi        switch ( $state ) {
3130cecf9d5Sandi
3140cecf9d5Sandi            case DOKU_LEXER_ENTER:
31567f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Quote($this->CallWriter);
3160cecf9d5Sandi                $this->CallWriter = & $ReWriter;
317433bef32Sandi                $this->_addCall('quote_start',array($match), $pos);
3180cecf9d5Sandi            break;
3190cecf9d5Sandi
3200cecf9d5Sandi            case DOKU_LEXER_EXIT:
321433bef32Sandi                $this->_addCall('quote_end',array(), $pos);
3220cecf9d5Sandi                $this->CallWriter->process();
3230cecf9d5Sandi                $ReWriter = & $this->CallWriter;
3240cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
3250cecf9d5Sandi            break;
3260cecf9d5Sandi
3270cecf9d5Sandi            case DOKU_LEXER_MATCHED:
328433bef32Sandi                $this->_addCall('quote_newline',array($match), $pos);
3290cecf9d5Sandi            break;
3300cecf9d5Sandi
3310cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
332433bef32Sandi                $this->_addCall('cdata',array($match), $pos);
3330cecf9d5Sandi            break;
3340cecf9d5Sandi
3350cecf9d5Sandi        }
3360cecf9d5Sandi
33744881bd0Shenning.noren        return true;
3380cecf9d5Sandi    }
3390cecf9d5Sandi
3403d491f75SAndreas Gohr    function file($match, $state, $pos) {
3413d491f75SAndreas Gohr        return $this->code($match, $state, $pos, 'file');
3423d491f75SAndreas Gohr    }
3433d491f75SAndreas Gohr
3443d491f75SAndreas Gohr    function code($match, $state, $pos, $type='code') {
3453d491f75SAndreas Gohr        if ( $state == DOKU_LEXER_UNMATCHED ) {
3464b7f9e70STom N Harris            $matches = explode('>',$match,2);
3473d491f75SAndreas Gohr
3480139312bSAdrian Lang            $param = preg_split('/\s+/', $matches[0], 2, PREG_SPLIT_NO_EMPTY);
3490139312bSAdrian Lang            while(count($param) < 2) array_push($param, null);
3500139312bSAdrian Lang
3510139312bSAdrian Lang            // We shortcut html here.
3520139312bSAdrian Lang            if ($param[0] == 'html') $param[0] = 'html4strict';
3530139312bSAdrian Lang            if ($param[0] == '-') $param[0] = null;
3540139312bSAdrian Lang            array_unshift($param, $matches[1]);
3550139312bSAdrian Lang
3560139312bSAdrian Lang            $this->_addCall($type, $param, $pos);
3570cecf9d5Sandi        }
35844881bd0Shenning.noren        return true;
3590cecf9d5Sandi    }
3600cecf9d5Sandi
3610cecf9d5Sandi    function acronym($match, $state, $pos) {
362433bef32Sandi        $this->_addCall('acronym',array($match), $pos);
36344881bd0Shenning.noren        return true;
3640cecf9d5Sandi    }
3650cecf9d5Sandi
3660cecf9d5Sandi    function smiley($match, $state, $pos) {
367433bef32Sandi        $this->_addCall('smiley',array($match), $pos);
36844881bd0Shenning.noren        return true;
3690cecf9d5Sandi    }
3700cecf9d5Sandi
3710cecf9d5Sandi    function wordblock($match, $state, $pos) {
372433bef32Sandi        $this->_addCall('wordblock',array($match), $pos);
37344881bd0Shenning.noren        return true;
3740cecf9d5Sandi    }
3750cecf9d5Sandi
3760cecf9d5Sandi    function entity($match, $state, $pos) {
377433bef32Sandi        $this->_addCall('entity',array($match), $pos);
37844881bd0Shenning.noren        return true;
3790cecf9d5Sandi    }
3800cecf9d5Sandi
3810cecf9d5Sandi    function multiplyentity($match, $state, $pos) {
3820cecf9d5Sandi        preg_match_all('/\d+/',$match,$matches);
383433bef32Sandi        $this->_addCall('multiplyentity',array($matches[0][0],$matches[0][1]), $pos);
38444881bd0Shenning.noren        return true;
3850cecf9d5Sandi    }
3860cecf9d5Sandi
3870cecf9d5Sandi    function singlequoteopening($match, $state, $pos) {
388433bef32Sandi        $this->_addCall('singlequoteopening',array(), $pos);
38944881bd0Shenning.noren        return true;
3900cecf9d5Sandi    }
3910cecf9d5Sandi
3920cecf9d5Sandi    function singlequoteclosing($match, $state, $pos) {
393433bef32Sandi        $this->_addCall('singlequoteclosing',array(), $pos);
39444881bd0Shenning.noren        return true;
3950cecf9d5Sandi    }
3960cecf9d5Sandi
39757d757d1SAndreas Gohr    function apostrophe($match, $state, $pos) {
39857d757d1SAndreas Gohr        $this->_addCall('apostrophe',array(), $pos);
39957d757d1SAndreas Gohr        return true;
40057d757d1SAndreas Gohr    }
40157d757d1SAndreas Gohr
4020cecf9d5Sandi    function doublequoteopening($match, $state, $pos) {
403433bef32Sandi        $this->_addCall('doublequoteopening',array(), $pos);
40444881bd0Shenning.noren        return true;
4050cecf9d5Sandi    }
4060cecf9d5Sandi
4070cecf9d5Sandi    function doublequoteclosing($match, $state, $pos) {
408433bef32Sandi        $this->_addCall('doublequoteclosing',array(), $pos);
40944881bd0Shenning.noren        return true;
4100cecf9d5Sandi    }
4110cecf9d5Sandi
4120cecf9d5Sandi    function camelcaselink($match, $state, $pos) {
413433bef32Sandi        $this->_addCall('camelcaselink',array($match), $pos);
41444881bd0Shenning.noren        return true;
4150cecf9d5Sandi    }
4160cecf9d5Sandi
4170cecf9d5Sandi    /*
4180cecf9d5Sandi    */
4190cecf9d5Sandi    function internallink($match, $state, $pos) {
4200cecf9d5Sandi        // Strip the opening and closing markup
4210cecf9d5Sandi        $link = preg_replace(array('/^\[\[/','/\]\]$/u'),'',$match);
4220cecf9d5Sandi
4230cecf9d5Sandi        // Split title from URL
4244b7f9e70STom N Harris        $link = explode('|',$link,2);
4250cecf9d5Sandi        if ( !isset($link[1]) ) {
4260cecf9d5Sandi            $link[1] = NULL;
4270cecf9d5Sandi        } else if ( preg_match('/^\{\{[^\}]+\}\}$/',$link[1]) ) {
4285578eb8fSandi            // If the title is an image, convert it to an array containing the image details
429b625487dSandi            $link[1] = Doku_Handler_Parse_Media($link[1]);
4300cecf9d5Sandi        }
4310b7c14c2Sandi        $link[0] = trim($link[0]);
4320cecf9d5Sandi
4330e1c636eSandi        //decide which kind of link it is
4340e1c636eSandi
435e08dda3fSAndreas Gohr        if ( preg_match('/^[a-zA-Z0-9\.]+>{1}.*$/u',$link[0]) ) {
4360e1c636eSandi        // Interwiki
4374b7f9e70STom N Harris            $interwiki = explode('>',$link[0],2);
438433bef32Sandi            $this->_addCall(
4390cecf9d5Sandi                'interwikilink',
4400cecf9d5Sandi                array($link[0],$link[1],strtolower($interwiki[0]),$interwiki[1]),
4410cecf9d5Sandi                $pos
4420cecf9d5Sandi                );
4430b7c14c2Sandi        }elseif ( preg_match('/^\\\\\\\\[\w.:?\-;,]+?\\\\/u',$link[0]) ) {
4440e1c636eSandi        // Windows Share
445433bef32Sandi            $this->_addCall(
4460cecf9d5Sandi                'windowssharelink',
4470cecf9d5Sandi                array($link[0],$link[1]),
4480cecf9d5Sandi                $pos
4490cecf9d5Sandi                );
4504468cb4cSAndreas Gohr        }elseif ( preg_match('#^([a-z0-9\-\.+]+?)://#i',$link[0]) ) {
4510e1c636eSandi        // external link (accepts all protocols)
452433bef32Sandi            $this->_addCall(
4530cecf9d5Sandi                    'externallink',
4540cecf9d5Sandi                    array($link[0],$link[1]),
4550cecf9d5Sandi                    $pos
4560cecf9d5Sandi                    );
4570a1d30bfSchris        }elseif ( preg_match('<'.PREG_PATTERN_VALID_EMAIL.'>',$link[0]) ) {
4580a1d30bfSchris        // E-Mail (pattern above is defined in inc/mail.php)
459a6755281Sandi            $this->_addCall(
460a6755281Sandi                'emaillink',
461a6755281Sandi                array($link[0],$link[1]),
462a6755281Sandi                $pos
463a6755281Sandi                );
4640b7c14c2Sandi        }elseif ( preg_match('!^#.+!',$link[0]) ){
4650b7c14c2Sandi        // local link
4660b7c14c2Sandi            $this->_addCall(
4670b7c14c2Sandi                'locallink',
4680b7c14c2Sandi                array(substr($link[0],1),$link[1]),
4690b7c14c2Sandi                $pos
4700b7c14c2Sandi                );
4710e1c636eSandi        }else{
4720e1c636eSandi        // internal link
473433bef32Sandi            $this->_addCall(
4740e1c636eSandi                'internallink',
4750e1c636eSandi                array($link[0],$link[1]),
4760e1c636eSandi                $pos
4770e1c636eSandi                );
4780cecf9d5Sandi        }
4790e1c636eSandi
48044881bd0Shenning.noren        return true;
4810cecf9d5Sandi    }
4820cecf9d5Sandi
4830cecf9d5Sandi    function filelink($match, $state, $pos) {
484433bef32Sandi        $this->_addCall('filelink',array($match, NULL), $pos);
48544881bd0Shenning.noren        return true;
4860cecf9d5Sandi    }
4870cecf9d5Sandi
4880cecf9d5Sandi    function windowssharelink($match, $state, $pos) {
489433bef32Sandi        $this->_addCall('windowssharelink',array($match, NULL), $pos);
49044881bd0Shenning.noren        return true;
4910cecf9d5Sandi    }
4920cecf9d5Sandi
4930cecf9d5Sandi    function media($match, $state, $pos) {
4940cecf9d5Sandi        $p = Doku_Handler_Parse_Media($match);
4950cecf9d5Sandi
496433bef32Sandi        $this->_addCall(
4970cecf9d5Sandi              $p['type'],
498dc673a5bSjoe.lapp              array($p['src'], $p['title'], $p['align'], $p['width'],
499dc673a5bSjoe.lapp                     $p['height'], $p['cache'], $p['linking']),
5000cecf9d5Sandi              $pos
5010cecf9d5Sandi             );
50244881bd0Shenning.noren        return true;
5030cecf9d5Sandi    }
5040cecf9d5Sandi
505b625487dSandi    function rss($match, $state, $pos) {
506b625487dSandi        $link = preg_replace(array('/^\{\{rss>/','/\}\}$/'),'',$match);
5073db95becSAndreas Gohr
5083db95becSAndreas Gohr        // get params
5093db95becSAndreas Gohr        list($link,$params) = explode(' ',$link,2);
5103db95becSAndreas Gohr
5113db95becSAndreas Gohr        $p = array();
5123db95becSAndreas Gohr        if(preg_match('/\b(\d+)\b/',$params,$match)){
5133db95becSAndreas Gohr            $p['max'] = $match[1];
5143db95becSAndreas Gohr        }else{
5153db95becSAndreas Gohr            $p['max'] = 8;
5163db95becSAndreas Gohr        }
5173db95becSAndreas Gohr        $p['reverse'] = (preg_match('/rev/',$params));
5183db95becSAndreas Gohr        $p['author']  = (preg_match('/\b(by|author)/',$params));
5193db95becSAndreas Gohr        $p['date']    = (preg_match('/\b(date)/',$params));
5203db95becSAndreas Gohr        $p['details'] = (preg_match('/\b(desc|detail)/',$params));
5213db95becSAndreas Gohr
5220a69dff7Schris        if (preg_match('/\b(\d+)([dhm])\b/',$params,$match)) {
5230a69dff7Schris          $period = array('d' => 86400, 'h' => 3600, 'm' => 60);
5240a69dff7Schris          $p['refresh'] = max(600,$match[1]*$period[$match[2]]);  // n * period in seconds, minimum 10 minutes
5250a69dff7Schris        } else {
5260a69dff7Schris          $p['refresh'] = 14400;   // default to 4 hours
5270a69dff7Schris        }
5280a69dff7Schris
5293db95becSAndreas Gohr        $this->_addCall('rss',array($link,$p),$pos);
53044881bd0Shenning.noren        return true;
531b625487dSandi    }
532b625487dSandi
5330cecf9d5Sandi    function externallink($match, $state, $pos) {
534da9f31c5SAndreas Gohr        $url   = $match;
535da9f31c5SAndreas Gohr        $title = null;
5360cecf9d5Sandi
537da9f31c5SAndreas Gohr        // add protocol on simple short URLs
538da9f31c5SAndreas Gohr        if(substr($url,0,3) == 'ftp' && (substr($url,0,6) != 'ftp://')){
539da9f31c5SAndreas Gohr            $title = $url;
540da9f31c5SAndreas Gohr            $url   = 'ftp://'.$url;
541da9f31c5SAndreas Gohr        }
542da9f31c5SAndreas Gohr        if(substr($url,0,3) == 'www' && (substr($url,0,7) != 'http://')){
543da9f31c5SAndreas Gohr            $title = $url;
544da9f31c5SAndreas Gohr            $url = 'http://'.$url;
545da9f31c5SAndreas Gohr        }
546da9f31c5SAndreas Gohr
547da9f31c5SAndreas Gohr        $this->_addCall('externallink',array($url, $title), $pos);
54844881bd0Shenning.noren        return true;
5490cecf9d5Sandi    }
5500cecf9d5Sandi
55171352defSandi    function emaillink($match, $state, $pos) {
5520cecf9d5Sandi        $email = preg_replace(array('/^</','/>$/'),'',$match);
553433bef32Sandi        $this->_addCall('emaillink',array($email, NULL), $pos);
55444881bd0Shenning.noren        return true;
5550cecf9d5Sandi    }
5560cecf9d5Sandi
5570cecf9d5Sandi    function table($match, $state, $pos) {
5580cecf9d5Sandi        switch ( $state ) {
5590cecf9d5Sandi
5600cecf9d5Sandi            case DOKU_LEXER_ENTER:
5610cecf9d5Sandi
56267f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Table($this->CallWriter);
5630cecf9d5Sandi                $this->CallWriter = & $ReWriter;
5640cecf9d5Sandi
56590df9a4dSAdrian Lang                $this->_addCall('table_start', array($pos + 1), $pos);
5660cecf9d5Sandi                if ( trim($match) == '^' ) {
567433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
5680cecf9d5Sandi                } else {
569433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
5700cecf9d5Sandi                }
5710cecf9d5Sandi            break;
5720cecf9d5Sandi
5730cecf9d5Sandi            case DOKU_LEXER_EXIT:
57490df9a4dSAdrian Lang                $this->_addCall('table_end', array($pos), $pos);
5750cecf9d5Sandi                $this->CallWriter->process();
5760cecf9d5Sandi                $ReWriter = & $this->CallWriter;
5770cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
5780cecf9d5Sandi            break;
5790cecf9d5Sandi
5800cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
5810cecf9d5Sandi                if ( trim($match) != '' ) {
582433bef32Sandi                    $this->_addCall('cdata',array($match), $pos);
5830cecf9d5Sandi                }
5840cecf9d5Sandi            break;
5850cecf9d5Sandi
5860cecf9d5Sandi            case DOKU_LEXER_MATCHED:
5879ab75d9eSAndreas Gohr                if ( $match == ' ' ){
5889ab75d9eSAndreas Gohr                    $this->_addCall('cdata', array($match), $pos);
58925b97867Shakan.sandell                } else if ( preg_match('/:::/',$match) ) {
59025b97867Shakan.sandell                    $this->_addCall('rowspan', array($match), $pos);
591e205b721SAndreas Gohr                } else if ( preg_match('/\t+/',$match) ) {
5929ab75d9eSAndreas Gohr                    $this->_addCall('table_align', array($match), $pos);
593e205b721SAndreas Gohr                } else if ( preg_match('/ {2,}/',$match) ) {
594433bef32Sandi                    $this->_addCall('table_align', array($match), $pos);
5950cecf9d5Sandi                } else if ( $match == "\n|" ) {
596433bef32Sandi                    $this->_addCall('table_row', array(), $pos);
597433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
5980cecf9d5Sandi                } else if ( $match == "\n^" ) {
599433bef32Sandi                    $this->_addCall('table_row', array(), $pos);
600433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
6010cecf9d5Sandi                } else if ( $match == '|' ) {
602433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
6030cecf9d5Sandi                } else if ( $match == '^' ) {
604433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
6050cecf9d5Sandi                }
6060cecf9d5Sandi            break;
6070cecf9d5Sandi        }
60844881bd0Shenning.noren        return true;
6090cecf9d5Sandi    }
6100cecf9d5Sandi}
6110cecf9d5Sandi
6120cecf9d5Sandi//------------------------------------------------------------------------
6130cecf9d5Sandifunction Doku_Handler_Parse_Media($match) {
6140cecf9d5Sandi
6150cecf9d5Sandi    // Strip the opening and closing markup
6160cecf9d5Sandi    $link = preg_replace(array('/^\{\{/','/\}\}$/u'),'',$match);
6170cecf9d5Sandi
6180cecf9d5Sandi    // Split title from URL
6194b7f9e70STom N Harris    $link = explode('|',$link,2);
6200cecf9d5Sandi
6210cecf9d5Sandi
6220cecf9d5Sandi    // Check alignment
6230cecf9d5Sandi    $ralign = (bool)preg_match('/^ /',$link[0]);
6240cecf9d5Sandi    $lalign = (bool)preg_match('/ $/',$link[0]);
6250cecf9d5Sandi
6260cecf9d5Sandi    // Logic = what's that ;)...
6270cecf9d5Sandi    if ( $lalign & $ralign ) {
6280cecf9d5Sandi        $align = 'center';
6290cecf9d5Sandi    } else if ( $ralign ) {
6300cecf9d5Sandi        $align = 'right';
6310cecf9d5Sandi    } else if ( $lalign ) {
6320cecf9d5Sandi        $align = 'left';
6330cecf9d5Sandi    } else {
6340cecf9d5Sandi        $align = NULL;
6350cecf9d5Sandi    }
6360cecf9d5Sandi
6370cecf9d5Sandi    // The title...
6380cecf9d5Sandi    if ( !isset($link[1]) ) {
6390cecf9d5Sandi        $link[1] = NULL;
6400cecf9d5Sandi    }
6410cecf9d5Sandi
6424826ab45Sandi    //remove aligning spaces
6434826ab45Sandi    $link[0] = trim($link[0]);
6440cecf9d5Sandi
6454826ab45Sandi    //split into src and parameters (using the very last questionmark)
6464826ab45Sandi    $pos = strrpos($link[0], '?');
6474826ab45Sandi    if($pos !== false){
6484826ab45Sandi        $src   = substr($link[0],0,$pos);
6494826ab45Sandi        $param = substr($link[0],$pos+1);
6500cecf9d5Sandi    }else{
6514826ab45Sandi        $src   = $link[0];
6524826ab45Sandi        $param = '';
6530cecf9d5Sandi    }
6540cecf9d5Sandi
6554826ab45Sandi    //parse width and height
6564826ab45Sandi    if(preg_match('#(\d+)(x(\d+))?#i',$param,$size)){
6574826ab45Sandi        ($size[1]) ? $w = $size[1] : $w = NULL;
6584826ab45Sandi        ($size[3]) ? $h = $size[3] : $h = NULL;
659fc1c55b1Shfuecks    } else {
660fc1c55b1Shfuecks        $w = NULL;
661fc1c55b1Shfuecks        $h = NULL;
6620cecf9d5Sandi    }
6630cecf9d5Sandi
664dc673a5bSjoe.lapp    //get linking command
665d35ab615Shenning.noren    if(preg_match('/nolink/i',$param)){
666dc673a5bSjoe.lapp        $linking = 'nolink';
667d35ab615Shenning.noren    }else if(preg_match('/direct/i',$param)){
668dc673a5bSjoe.lapp        $linking = 'direct';
6698acb3108SAndreas Gohr    }else if(preg_match('/linkonly/i',$param)){
6708acb3108SAndreas Gohr        $linking = 'linkonly';
671dc673a5bSjoe.lapp    }else{
672dc673a5bSjoe.lapp        $linking = 'details';
673dc673a5bSjoe.lapp    }
674dc673a5bSjoe.lapp
6754826ab45Sandi    //get caching command
6764826ab45Sandi    if (preg_match('/(nocache|recache)/i',$param,$cachemode)){
6774826ab45Sandi        $cache = $cachemode[1];
6780cecf9d5Sandi    }else{
6794826ab45Sandi        $cache = 'cache';
6800cecf9d5Sandi    }
6810cecf9d5Sandi
6820cecf9d5Sandi    // Check whether this is a local or remote image
6834826ab45Sandi    if ( preg_match('#^(https?|ftp)#i',$src) ) {
6844826ab45Sandi        $call = 'externalmedia';
6850cecf9d5Sandi    } else {
6864826ab45Sandi        $call = 'internalmedia';
6870cecf9d5Sandi    }
6880cecf9d5Sandi
6890cecf9d5Sandi    $params = array(
6900cecf9d5Sandi        'type'=>$call,
6914826ab45Sandi        'src'=>$src,
6920cecf9d5Sandi        'title'=>$link[1],
6930cecf9d5Sandi        'align'=>$align,
6944826ab45Sandi        'width'=>$w,
6954826ab45Sandi        'height'=>$h,
6960cecf9d5Sandi        'cache'=>$cache,
697dc673a5bSjoe.lapp        'linking'=>$linking,
6980cecf9d5Sandi    );
6990cecf9d5Sandi
7000cecf9d5Sandi    return $params;
7010cecf9d5Sandi}
7020cecf9d5Sandi
7030cecf9d5Sandi//------------------------------------------------------------------------
7040cecf9d5Sandiclass Doku_Handler_CallWriter {
7050cecf9d5Sandi
7060cecf9d5Sandi    var $Handler;
7070cecf9d5Sandi
7080cecf9d5Sandi    function Doku_Handler_CallWriter(& $Handler) {
7090cecf9d5Sandi        $this->Handler = & $Handler;
7100cecf9d5Sandi    }
7110cecf9d5Sandi
7120cecf9d5Sandi    function writeCall($call) {
7130cecf9d5Sandi        $this->Handler->calls[] = $call;
7140cecf9d5Sandi    }
7150cecf9d5Sandi
7160cecf9d5Sandi    function writeCalls($calls) {
7170cecf9d5Sandi        $this->Handler->calls = array_merge($this->Handler->calls, $calls);
7180cecf9d5Sandi    }
719f4f02a0fSchris
720f4f02a0fSchris    // function is required, but since this call writer is first/highest in
721f4f02a0fSchris    // the chain it is not required to do anything
722f4f02a0fSchris    function finalise() {
723f4f02a0fSchris    }
7240cecf9d5Sandi}
7250cecf9d5Sandi
7260cecf9d5Sandi//------------------------------------------------------------------------
7275587e44cSchris/**
7285587e44cSchris * Generic call writer class to handle nesting of rendering instructions
7295587e44cSchris * within a render instruction. Also see nest() method of renderer base class
7305587e44cSchris *
7315587e44cSchris * @author    Chris Smith <chris@jalakai.co.uk>
7325587e44cSchris */
7335587e44cSchrisclass Doku_Handler_Nest {
7345587e44cSchris
7355587e44cSchris    var $CallWriter;
7365587e44cSchris    var $calls = array();
7375587e44cSchris
7385587e44cSchris    var $closingInstruction;
7395587e44cSchris
7405587e44cSchris    /**
7415587e44cSchris     * constructor
7425587e44cSchris     *
7435587e44cSchris     * @param  object     $CallWriter     the renderers current call writer
7445587e44cSchris     * @param  string     $close          closing instruction name, this is required to properly terminate the
7455587e44cSchris     *                                    syntax mode if the document ends without a closing pattern
7465587e44cSchris     */
7475587e44cSchris    function Doku_Handler_Nest(& $CallWriter, $close="nest_close") {
7485587e44cSchris        $this->CallWriter = & $CallWriter;
7495587e44cSchris
7505587e44cSchris        $this->closingInstruction = $close;
7515587e44cSchris    }
7525587e44cSchris
7535587e44cSchris    function writeCall($call) {
7545587e44cSchris        $this->calls[] = $call;
7555587e44cSchris    }
7565587e44cSchris
7575587e44cSchris    function writeCalls($calls) {
7585587e44cSchris        $this->calls = array_merge($this->calls, $calls);
7595587e44cSchris    }
7605587e44cSchris
7615587e44cSchris    function finalise() {
7625587e44cSchris        $last_call = end($this->calls);
7635587e44cSchris        $this->writeCall(array($this->closingInstruction,array(), $last_call[2]));
7645587e44cSchris
7655587e44cSchris        $this->process();
7665587e44cSchris        $this->CallWriter->finalise();
7675587e44cSchris    }
7685587e44cSchris
7695587e44cSchris    function process() {
77041624b31SChris Smith        // merge consecutive cdata
77141624b31SChris Smith        $unmerged_calls = $this->calls;
77241624b31SChris Smith        $this->calls = array();
77341624b31SChris Smith
77441624b31SChris Smith        foreach ($unmerged_calls as $call) $this->addCall($call);
77541624b31SChris Smith
7765587e44cSchris        $first_call = reset($this->calls);
7775587e44cSchris        $this->CallWriter->writeCall(array("nest", array($this->calls), $first_call[2]));
7785587e44cSchris    }
77941624b31SChris Smith
78041624b31SChris Smith    function addCall($call) {
78141624b31SChris Smith        $key = count($this->calls);
78241624b31SChris Smith        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
78341624b31SChris Smith            $this->calls[$key-1][1][0] .= $call[1][0];
78473c47f3dSChris Smith        } else if ($call[0] == 'eol') {
78573c47f3dSChris Smith            // do nothing (eol shouldn't be allowed, to counter preformatted fix in #1652 & #1699)
78641624b31SChris Smith        } else {
78741624b31SChris Smith            $this->calls[] = $call;
78841624b31SChris Smith        }
78941624b31SChris Smith    }
7905587e44cSchris}
7915587e44cSchris
7920cecf9d5Sandiclass Doku_Handler_List {
7930cecf9d5Sandi
7940cecf9d5Sandi    var $CallWriter;
7950cecf9d5Sandi
7960cecf9d5Sandi    var $calls = array();
7970cecf9d5Sandi    var $listCalls = array();
7980cecf9d5Sandi    var $listStack = array();
7990cecf9d5Sandi
8000cecf9d5Sandi    function Doku_Handler_List(& $CallWriter) {
8010cecf9d5Sandi        $this->CallWriter = & $CallWriter;
8020cecf9d5Sandi    }
8030cecf9d5Sandi
8040cecf9d5Sandi    function writeCall($call) {
8050cecf9d5Sandi        $this->calls[] = $call;
8060cecf9d5Sandi    }
8070cecf9d5Sandi
8080cecf9d5Sandi    // Probably not needed but just in case...
8090cecf9d5Sandi    function writeCalls($calls) {
8100cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
811f4f02a0fSchris#        $this->CallWriter->writeCalls($this->calls);
812f4f02a0fSchris    }
813f4f02a0fSchris
814f4f02a0fSchris    function finalise() {
815f4f02a0fSchris        $last_call = end($this->calls);
816f4f02a0fSchris        $this->writeCall(array('list_close',array(), $last_call[2]));
817f4f02a0fSchris
818f4f02a0fSchris        $this->process();
819f4f02a0fSchris        $this->CallWriter->finalise();
8200cecf9d5Sandi    }
8210cecf9d5Sandi
8220cecf9d5Sandi    //------------------------------------------------------------------------
8230cecf9d5Sandi    function process() {
824f4f02a0fSchris
8250cecf9d5Sandi        foreach ( $this->calls as $call ) {
8260cecf9d5Sandi            switch ($call[0]) {
8270cecf9d5Sandi                case 'list_item':
8280cecf9d5Sandi                    $this->listOpen($call);
8290cecf9d5Sandi                break;
8300cecf9d5Sandi                case 'list_open':
8310cecf9d5Sandi                    $this->listStart($call);
8320cecf9d5Sandi                break;
8330cecf9d5Sandi                case 'list_close':
8340cecf9d5Sandi                    $this->listEnd($call);
8350cecf9d5Sandi                break;
8360cecf9d5Sandi                default:
8370cecf9d5Sandi                    $this->listContent($call);
8380cecf9d5Sandi                break;
8390cecf9d5Sandi            }
8400cecf9d5Sandi        }
8410cecf9d5Sandi
8420cecf9d5Sandi        $this->CallWriter->writeCalls($this->listCalls);
8430cecf9d5Sandi    }
8440cecf9d5Sandi
8450cecf9d5Sandi    //------------------------------------------------------------------------
8460cecf9d5Sandi    function listStart($call) {
8470cecf9d5Sandi        $depth = $this->interpretSyntax($call[1][0], $listType);
8480cecf9d5Sandi
8490cecf9d5Sandi        $this->initialDepth = $depth;
8500cecf9d5Sandi        $this->listStack[] = array($listType, $depth);
8510cecf9d5Sandi
8520cecf9d5Sandi        $this->listCalls[] = array('list'.$listType.'_open',array(),$call[2]);
8530cecf9d5Sandi        $this->listCalls[] = array('listitem_open',array(1),$call[2]);
8540cecf9d5Sandi        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
8550cecf9d5Sandi    }
8560cecf9d5Sandi
8570cecf9d5Sandi    //------------------------------------------------------------------------
8580cecf9d5Sandi    function listEnd($call) {
85944881bd0Shenning.noren        $closeContent = true;
8600cecf9d5Sandi
8610cecf9d5Sandi        while ( $list = array_pop($this->listStack) ) {
8620cecf9d5Sandi            if ( $closeContent ) {
8630cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
86444881bd0Shenning.noren                $closeContent = false;
8650cecf9d5Sandi            }
8660cecf9d5Sandi            $this->listCalls[] = array('listitem_close',array(),$call[2]);
8670cecf9d5Sandi            $this->listCalls[] = array('list'.$list[0].'_close', array(), $call[2]);
8680cecf9d5Sandi        }
8690cecf9d5Sandi    }
8700cecf9d5Sandi
8710cecf9d5Sandi    //------------------------------------------------------------------------
8720cecf9d5Sandi    function listOpen($call) {
8730cecf9d5Sandi        $depth = $this->interpretSyntax($call[1][0], $listType);
8740cecf9d5Sandi        $end = end($this->listStack);
8750cecf9d5Sandi
8760cecf9d5Sandi        // Not allowed to be shallower than initialDepth
8770cecf9d5Sandi        if ( $depth < $this->initialDepth ) {
8780cecf9d5Sandi            $depth = $this->initialDepth;
8790cecf9d5Sandi        }
8800cecf9d5Sandi
8810cecf9d5Sandi        //------------------------------------------------------------------------
8820cecf9d5Sandi        if ( $depth == $end[1] ) {
8830cecf9d5Sandi
8840cecf9d5Sandi            // Just another item in the list...
8850cecf9d5Sandi            if ( $listType == $end[0] ) {
8860cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
8870cecf9d5Sandi                $this->listCalls[] = array('listitem_close',array(),$call[2]);
8880cecf9d5Sandi                $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
8890cecf9d5Sandi                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
8900cecf9d5Sandi
8910cecf9d5Sandi            // Switched list type...
8920cecf9d5Sandi            } else {
8930cecf9d5Sandi
8940cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
8950cecf9d5Sandi                $this->listCalls[] = array('listitem_close',array(),$call[2]);
8960cecf9d5Sandi                $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
8970cecf9d5Sandi                $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
8980cecf9d5Sandi                $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
8990cecf9d5Sandi                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9000cecf9d5Sandi
9010cecf9d5Sandi                array_pop($this->listStack);
9020cecf9d5Sandi                $this->listStack[] = array($listType, $depth);
9030cecf9d5Sandi            }
9040cecf9d5Sandi
9050cecf9d5Sandi        //------------------------------------------------------------------------
9060cecf9d5Sandi        // Getting deeper...
9070cecf9d5Sandi        } else if ( $depth > $end[1] ) {
9080cecf9d5Sandi
9090cecf9d5Sandi            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9100cecf9d5Sandi            $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9110cecf9d5Sandi            $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9120cecf9d5Sandi            $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9130cecf9d5Sandi
9140cecf9d5Sandi            $this->listStack[] = array($listType, $depth);
9150cecf9d5Sandi
9160cecf9d5Sandi        //------------------------------------------------------------------------
9170cecf9d5Sandi        // Getting shallower ( $depth < $end[1] )
9180cecf9d5Sandi        } else {
9190cecf9d5Sandi            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9200cecf9d5Sandi            $this->listCalls[] = array('listitem_close',array(),$call[2]);
9210cecf9d5Sandi            $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
9220cecf9d5Sandi
9230cecf9d5Sandi            // Throw away the end - done
9240cecf9d5Sandi            array_pop($this->listStack);
9250cecf9d5Sandi
9260cecf9d5Sandi            while (1) {
9270cecf9d5Sandi                $end = end($this->listStack);
9280cecf9d5Sandi
9290cecf9d5Sandi                if ( $end[1] <= $depth ) {
9300cecf9d5Sandi
9310cecf9d5Sandi                    // Normalize depths
9320cecf9d5Sandi                    $depth = $end[1];
9330cecf9d5Sandi
9340cecf9d5Sandi                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
9350cecf9d5Sandi
9360cecf9d5Sandi                    if ( $end[0] == $listType ) {
9370cecf9d5Sandi                        $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
9380cecf9d5Sandi                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9390cecf9d5Sandi
9400cecf9d5Sandi                    } else {
9410cecf9d5Sandi                        // Switching list type...
9420cecf9d5Sandi                        $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
9430cecf9d5Sandi                        $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9440cecf9d5Sandi                        $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9450cecf9d5Sandi                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9460cecf9d5Sandi
9470cecf9d5Sandi                        array_pop($this->listStack);
9480cecf9d5Sandi                        $this->listStack[] = array($listType, $depth);
9490cecf9d5Sandi                    }
9500cecf9d5Sandi
9510cecf9d5Sandi                    break;
9520cecf9d5Sandi
9530cecf9d5Sandi                // Haven't dropped down far enough yet.... ( $end[1] > $depth )
9540cecf9d5Sandi                } else {
9550cecf9d5Sandi
9560cecf9d5Sandi                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
9570cecf9d5Sandi                    $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
9580cecf9d5Sandi
9590cecf9d5Sandi                    array_pop($this->listStack);
9600cecf9d5Sandi
9610cecf9d5Sandi                }
9620cecf9d5Sandi
9630cecf9d5Sandi            }
9640cecf9d5Sandi
9650cecf9d5Sandi        }
9660cecf9d5Sandi    }
9670cecf9d5Sandi
9680cecf9d5Sandi    //------------------------------------------------------------------------
9690cecf9d5Sandi    function listContent($call) {
9700cecf9d5Sandi        $this->listCalls[] = $call;
9710cecf9d5Sandi    }
9720cecf9d5Sandi
9730cecf9d5Sandi    //------------------------------------------------------------------------
9740cecf9d5Sandi    function interpretSyntax($match, & $type) {
9750cecf9d5Sandi        if ( substr($match,-1) == '*' ) {
9760cecf9d5Sandi            $type = 'u';
9770cecf9d5Sandi        } else {
9780cecf9d5Sandi            $type = 'o';
9790cecf9d5Sandi        }
9804b7f9e70STom N Harris        // Is the +1 needed? It used to be count(explode(...))
9814b7f9e70STom N Harris        // but I don't think the number is seen outside this handler
9824b7f9e70STom N Harris        return substr_count(str_replace("\t",'  ',$match), '  ') + 1;
9830cecf9d5Sandi    }
9840cecf9d5Sandi}
9850cecf9d5Sandi
9860cecf9d5Sandi//------------------------------------------------------------------------
9870cecf9d5Sandiclass Doku_Handler_Preformatted {
9880cecf9d5Sandi
9890cecf9d5Sandi    var $CallWriter;
9900cecf9d5Sandi
9910cecf9d5Sandi    var $calls = array();
9920cecf9d5Sandi    var $pos;
9930cecf9d5Sandi    var $text ='';
9940cecf9d5Sandi
9950cecf9d5Sandi
9960cecf9d5Sandi
9970cecf9d5Sandi    function Doku_Handler_Preformatted(& $CallWriter) {
9980cecf9d5Sandi        $this->CallWriter = & $CallWriter;
9990cecf9d5Sandi    }
10000cecf9d5Sandi
10010cecf9d5Sandi    function writeCall($call) {
10020cecf9d5Sandi        $this->calls[] = $call;
10030cecf9d5Sandi    }
10040cecf9d5Sandi
10050cecf9d5Sandi    // Probably not needed but just in case...
10060cecf9d5Sandi    function writeCalls($calls) {
10070cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1008f4f02a0fSchris#        $this->CallWriter->writeCalls($this->calls);
1009f4f02a0fSchris    }
1010f4f02a0fSchris
1011f4f02a0fSchris    function finalise() {
1012f4f02a0fSchris        $last_call = end($this->calls);
1013f4f02a0fSchris        $this->writeCall(array('preformatted_end',array(), $last_call[2]));
1014f4f02a0fSchris
1015f4f02a0fSchris        $this->process();
1016f4f02a0fSchris        $this->CallWriter->finalise();
10170cecf9d5Sandi    }
10180cecf9d5Sandi
10190cecf9d5Sandi    function process() {
10200cecf9d5Sandi        foreach ( $this->calls as $call ) {
10210cecf9d5Sandi            switch ($call[0]) {
10220cecf9d5Sandi                case 'preformatted_start':
10230cecf9d5Sandi                    $this->pos = $call[2];
10240cecf9d5Sandi                break;
10250cecf9d5Sandi                case 'preformatted_newline':
10260cecf9d5Sandi                    $this->text .= "\n";
10270cecf9d5Sandi                break;
10280cecf9d5Sandi                case 'preformatted_content':
10290cecf9d5Sandi                    $this->text .= $call[1][0];
10300cecf9d5Sandi                break;
10310cecf9d5Sandi                case 'preformatted_end':
103293a34bf3SChris Smith                    if (trim($this->text)) {
10330cecf9d5Sandi                      $this->CallWriter->writeCall(array('preformatted',array($this->text),$this->pos));
103493a34bf3SChris Smith                    }
103595c19ce7SChris Smith                    // see FS#1699 & FS#1652, add 'eol' instructions to ensure proper triggering of following p_open
103695c19ce7SChris Smith                    $this->CallWriter->writeCall(array('eol',array(),$this->pos));
103795c19ce7SChris Smith                    $this->CallWriter->writeCall(array('eol',array(),$this->pos));
10380cecf9d5Sandi                break;
10390cecf9d5Sandi            }
10400cecf9d5Sandi        }
10410cecf9d5Sandi    }
1042f4f02a0fSchris
10430cecf9d5Sandi}
10440cecf9d5Sandi
10450cecf9d5Sandi//------------------------------------------------------------------------
10460cecf9d5Sandiclass Doku_Handler_Quote {
10470cecf9d5Sandi
10480cecf9d5Sandi    var $CallWriter;
10490cecf9d5Sandi
10500cecf9d5Sandi    var $calls = array();
10510cecf9d5Sandi
10520cecf9d5Sandi    var $quoteCalls = array();
10530cecf9d5Sandi
10540cecf9d5Sandi    function Doku_Handler_Quote(& $CallWriter) {
10550cecf9d5Sandi        $this->CallWriter = & $CallWriter;
10560cecf9d5Sandi    }
10570cecf9d5Sandi
10580cecf9d5Sandi    function writeCall($call) {
10590cecf9d5Sandi        $this->calls[] = $call;
10600cecf9d5Sandi    }
10610cecf9d5Sandi
10620cecf9d5Sandi    // Probably not needed but just in case...
10630cecf9d5Sandi    function writeCalls($calls) {
10640cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1065f4f02a0fSchris    }
1066f4f02a0fSchris
1067f4f02a0fSchris    function finalise() {
1068f4f02a0fSchris        $last_call = end($this->calls);
1069f4f02a0fSchris        $this->writeCall(array('quote_end',array(), $last_call[2]));
1070f4f02a0fSchris
1071f4f02a0fSchris        $this->process();
1072f4f02a0fSchris        $this->CallWriter->finalise();
10730cecf9d5Sandi    }
10740cecf9d5Sandi
10750cecf9d5Sandi    function process() {
10760cecf9d5Sandi
10770cecf9d5Sandi        $quoteDepth = 1;
10780cecf9d5Sandi
10790cecf9d5Sandi        foreach ( $this->calls as $call ) {
10800cecf9d5Sandi            switch ($call[0]) {
10810cecf9d5Sandi
10820cecf9d5Sandi                case 'quote_start':
10830cecf9d5Sandi
10840cecf9d5Sandi                    $this->quoteCalls[] = array('quote_open',array(),$call[2]);
10850cecf9d5Sandi
10860cecf9d5Sandi                case 'quote_newline':
10870cecf9d5Sandi
10880cecf9d5Sandi                    $quoteLength = $this->getDepth($call[1][0]);
10890cecf9d5Sandi
10900cecf9d5Sandi                    if ( $quoteLength > $quoteDepth ) {
10910cecf9d5Sandi                        $quoteDiff = $quoteLength - $quoteDepth;
10920cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
10930cecf9d5Sandi                            $this->quoteCalls[] = array('quote_open',array(),$call[2]);
10940cecf9d5Sandi                        }
10950cecf9d5Sandi                    } else if ( $quoteLength < $quoteDepth ) {
10960cecf9d5Sandi                        $quoteDiff = $quoteDepth - $quoteLength;
10970cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
10980cecf9d5Sandi                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
10990cecf9d5Sandi                        }
110026426c64Schris                    } else {
110126426c64Schris                        if ($call[0] != 'quote_start') $this->quoteCalls[] = array('linebreak',array(),$call[2]);
11020cecf9d5Sandi                    }
11030cecf9d5Sandi
11040cecf9d5Sandi                    $quoteDepth = $quoteLength;
11050cecf9d5Sandi
11060cecf9d5Sandi                break;
11070cecf9d5Sandi
11080cecf9d5Sandi                case 'quote_end':
11090cecf9d5Sandi
11100cecf9d5Sandi                    if ( $quoteDepth > 1 ) {
11110cecf9d5Sandi                        $quoteDiff = $quoteDepth - 1;
11120cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11130cecf9d5Sandi                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11140cecf9d5Sandi                        }
11150cecf9d5Sandi                    }
11160cecf9d5Sandi
11170cecf9d5Sandi                    $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11180cecf9d5Sandi
11190cecf9d5Sandi                    $this->CallWriter->writeCalls($this->quoteCalls);
11200cecf9d5Sandi                break;
11210cecf9d5Sandi
11220cecf9d5Sandi                default:
11230cecf9d5Sandi                    $this->quoteCalls[] = $call;
11240cecf9d5Sandi                break;
11250cecf9d5Sandi            }
11260cecf9d5Sandi        }
11270cecf9d5Sandi    }
11280cecf9d5Sandi
11290cecf9d5Sandi    function getDepth($marker) {
11300cecf9d5Sandi        preg_match('/>{1,}/', $marker, $matches);
11310cecf9d5Sandi        $quoteLength = strlen($matches[0]);
11320cecf9d5Sandi        return $quoteLength;
11330cecf9d5Sandi    }
11340cecf9d5Sandi}
11350cecf9d5Sandi
11360cecf9d5Sandi//------------------------------------------------------------------------
11370cecf9d5Sandiclass Doku_Handler_Table {
11380cecf9d5Sandi
11390cecf9d5Sandi    var $CallWriter;
11400cecf9d5Sandi
11410cecf9d5Sandi    var $calls = array();
11420cecf9d5Sandi    var $tableCalls = array();
11430cecf9d5Sandi    var $maxCols = 0;
11440cecf9d5Sandi    var $maxRows = 1;
11450cecf9d5Sandi    var $currentCols = 0;
114644881bd0Shenning.noren    var $firstCell = false;
11470cecf9d5Sandi    var $lastCellType = 'tablecell';
11480cecf9d5Sandi
11490cecf9d5Sandi    function Doku_Handler_Table(& $CallWriter) {
11500cecf9d5Sandi        $this->CallWriter = & $CallWriter;
11510cecf9d5Sandi    }
11520cecf9d5Sandi
11530cecf9d5Sandi    function writeCall($call) {
11540cecf9d5Sandi        $this->calls[] = $call;
11550cecf9d5Sandi    }
11560cecf9d5Sandi
11570cecf9d5Sandi    // Probably not needed but just in case...
11580cecf9d5Sandi    function writeCalls($calls) {
11590cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1160f4f02a0fSchris    }
1161f4f02a0fSchris
1162f4f02a0fSchris    function finalise() {
1163f4f02a0fSchris        $last_call = end($this->calls);
1164f4f02a0fSchris        $this->writeCall(array('table_end',array(), $last_call[2]));
1165f4f02a0fSchris
1166f4f02a0fSchris        $this->process();
1167f4f02a0fSchris        $this->CallWriter->finalise();
11680cecf9d5Sandi    }
11690cecf9d5Sandi
11700cecf9d5Sandi    //------------------------------------------------------------------------
11710cecf9d5Sandi    function process() {
11720cecf9d5Sandi        foreach ( $this->calls as $call ) {
11730cecf9d5Sandi            switch ( $call[0] ) {
11740cecf9d5Sandi                case 'table_start':
11750cecf9d5Sandi                    $this->tableStart($call);
11760cecf9d5Sandi                break;
11770cecf9d5Sandi                case 'table_row':
1178*aa92c4ccSAdrian Lang                    $this->tableRowClose($call);
11790cecf9d5Sandi                    $this->tableRowOpen(array('tablerow_open',$call[1],$call[2]));
11800cecf9d5Sandi                break;
11810cecf9d5Sandi                case 'tableheader':
11820cecf9d5Sandi                case 'tablecell':
11830cecf9d5Sandi                    $this->tableCell($call);
11840cecf9d5Sandi                break;
11850cecf9d5Sandi                case 'table_end':
1186*aa92c4ccSAdrian Lang                    $this->tableRowClose($call);
11870cecf9d5Sandi                    $this->tableEnd($call);
11880cecf9d5Sandi                break;
11890cecf9d5Sandi                default:
11900cecf9d5Sandi                    $this->tableDefault($call);
11910cecf9d5Sandi                break;
11920cecf9d5Sandi            }
11930cecf9d5Sandi        }
11940cecf9d5Sandi        $this->CallWriter->writeCalls($this->tableCalls);
11950cecf9d5Sandi    }
11960cecf9d5Sandi
11970cecf9d5Sandi    function tableStart($call) {
119890df9a4dSAdrian Lang        $this->tableCalls[] = array('table_open',$call[1],$call[2]);
11990cecf9d5Sandi        $this->tableCalls[] = array('tablerow_open',array(),$call[2]);
120044881bd0Shenning.noren        $this->firstCell = true;
12010cecf9d5Sandi    }
12020cecf9d5Sandi
12030cecf9d5Sandi    function tableEnd($call) {
120407c2b1c7SAdrian Lang        $this->tableCalls[] = array('table_close',$call[1],$call[2]);
12050cecf9d5Sandi        $this->finalizeTable();
12060cecf9d5Sandi    }
12070cecf9d5Sandi
12080cecf9d5Sandi    function tableRowOpen($call) {
12090cecf9d5Sandi        $this->tableCalls[] = $call;
12100cecf9d5Sandi        $this->currentCols = 0;
121144881bd0Shenning.noren        $this->firstCell = true;
12120cecf9d5Sandi        $this->lastCellType = 'tablecell';
12130cecf9d5Sandi        $this->maxRows++;
12140cecf9d5Sandi    }
12150cecf9d5Sandi
12160cecf9d5Sandi    function tableRowClose($call) {
12170cecf9d5Sandi        // Strip off final cell opening and anything after it
12180cecf9d5Sandi        while ( $discard = array_pop($this->tableCalls ) ) {
12190cecf9d5Sandi
12200cecf9d5Sandi            if ( $discard[0] == 'tablecell_open' || $discard[0] == 'tableheader_open') {
12210cecf9d5Sandi                break;
12220cecf9d5Sandi            }
12230cecf9d5Sandi        }
1224*aa92c4ccSAdrian Lang        $this->tableCalls[] = array('tablerow_close', array(), $call[2]);
12250cecf9d5Sandi
12260cecf9d5Sandi        if ( $this->currentCols > $this->maxCols ) {
12270cecf9d5Sandi            $this->maxCols = $this->currentCols;
12280cecf9d5Sandi        }
12290cecf9d5Sandi    }
12300cecf9d5Sandi
12310cecf9d5Sandi    function tableCell($call) {
12320cecf9d5Sandi        if ( !$this->firstCell ) {
12330cecf9d5Sandi
12340cecf9d5Sandi            // Increase the span
12350cecf9d5Sandi            $lastCall = end($this->tableCalls);
12360cecf9d5Sandi
12370cecf9d5Sandi            // A cell call which follows an open cell means an empty cell so span
12380cecf9d5Sandi            if ( $lastCall[0] == 'tablecell_open' || $lastCall[0] == 'tableheader_open' ) {
12390cecf9d5Sandi                 $this->tableCalls[] = array('colspan',array(),$call[2]);
12400cecf9d5Sandi
12410cecf9d5Sandi            }
12420cecf9d5Sandi
12430cecf9d5Sandi            $this->tableCalls[] = array($this->lastCellType.'_close',array(),$call[2]);
124425b97867Shakan.sandell            $this->tableCalls[] = array($call[0].'_open',array(1,NULL,1),$call[2]);
12450cecf9d5Sandi            $this->lastCellType = $call[0];
12460cecf9d5Sandi
12470cecf9d5Sandi        } else {
12480cecf9d5Sandi
124925b97867Shakan.sandell            $this->tableCalls[] = array($call[0].'_open',array(1,NULL,1),$call[2]);
12500cecf9d5Sandi            $this->lastCellType = $call[0];
125144881bd0Shenning.noren            $this->firstCell = false;
12520cecf9d5Sandi
12530cecf9d5Sandi        }
12540cecf9d5Sandi
12550cecf9d5Sandi        $this->currentCols++;
12560cecf9d5Sandi    }
12570cecf9d5Sandi
12580cecf9d5Sandi    function tableDefault($call) {
12590cecf9d5Sandi        $this->tableCalls[] = $call;
12600cecf9d5Sandi    }
12610cecf9d5Sandi
12620cecf9d5Sandi    function finalizeTable() {
12630cecf9d5Sandi
12640cecf9d5Sandi        // Add the max cols and rows to the table opening
12650cecf9d5Sandi        if ( $this->tableCalls[0][0] == 'table_open' ) {
12660cecf9d5Sandi            // Adjust to num cols not num col delimeters
12670cecf9d5Sandi            $this->tableCalls[0][1][] = $this->maxCols - 1;
12680cecf9d5Sandi            $this->tableCalls[0][1][] = $this->maxRows;
126990df9a4dSAdrian Lang            $this->tableCalls[0][1][] = array_shift($this->tableCalls[0][1]);
12700cecf9d5Sandi        } else {
12710cecf9d5Sandi            trigger_error('First element in table call list is not table_open');
12720cecf9d5Sandi        }
12730cecf9d5Sandi
12740cecf9d5Sandi        $lastRow = 0;
12750cecf9d5Sandi        $lastCell = 0;
127625b97867Shakan.sandell        $cellKey = array();
12770cecf9d5Sandi        $toDelete = array();
12780cecf9d5Sandi
12790cecf9d5Sandi        // Look for the colspan elements and increment the colspan on the
12800cecf9d5Sandi        // previous non-empty opening cell. Once done, delete all the cells
12810cecf9d5Sandi        // that contain colspans
12820cecf9d5Sandi        foreach ( $this->tableCalls as $key => $call ) {
12830cecf9d5Sandi
1284*aa92c4ccSAdrian Lang            switch ($call[0]) {
1285*aa92c4ccSAdrian Lang            case 'tablerow_open':
12860cecf9d5Sandi
128725b97867Shakan.sandell                $lastRow++;
128825b97867Shakan.sandell                $lastCell = 0;
1289*aa92c4ccSAdrian Lang                break;
12900cecf9d5Sandi
1291*aa92c4ccSAdrian Lang            case 'tablecell_open':
1292*aa92c4ccSAdrian Lang            case 'tableheader_open':
12930cecf9d5Sandi
129425b97867Shakan.sandell                $lastCell++;
129525b97867Shakan.sandell                $cellKey[$lastRow][$lastCell] = $key;
1296*aa92c4ccSAdrian Lang                break;
12970cecf9d5Sandi
1298*aa92c4ccSAdrian Lang            case 'table_align':
12990cecf9d5Sandi
1300e03b8b2eSAdrian Lang                $prev = in_array($this->tableCalls[$key-1][0], array('tablecell_open', 'tableheader_open'));
1301e03b8b2eSAdrian Lang                $next = in_array($this->tableCalls[$key+1][0], array('tablecell_close', 'tableheader_close'));
1302e03b8b2eSAdrian Lang                // If the cell is empty, align left
1303e03b8b2eSAdrian Lang                if ($prev && $next) {
1304e03b8b2eSAdrian Lang                    $this->tableCalls[$key-1][1][1] = 'left';
1305e03b8b2eSAdrian Lang
13060cecf9d5Sandi                // If the previous element was a cell open, align right
1307e03b8b2eSAdrian Lang                } elseif ($prev) {
13080cecf9d5Sandi                    $this->tableCalls[$key-1][1][1] = 'right';
13090cecf9d5Sandi
1310e03b8b2eSAdrian Lang                // If the next element is the close of an element, align either center or left
1311e03b8b2eSAdrian Lang                } elseif ( $next) {
131225b97867Shakan.sandell                    if ( $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] == 'right' ) {
131325b97867Shakan.sandell                        $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] = 'center';
13140cecf9d5Sandi                    } else {
131525b97867Shakan.sandell                        $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] = 'left';
13160cecf9d5Sandi                    }
13170cecf9d5Sandi
13180cecf9d5Sandi                }
13190cecf9d5Sandi
13200cecf9d5Sandi                // Now convert the whitespace back to cdata
13210cecf9d5Sandi                $this->tableCalls[$key][0] = 'cdata';
1322*aa92c4ccSAdrian Lang                break;
13230cecf9d5Sandi
1324*aa92c4ccSAdrian Lang            case 'colspan':
13250cecf9d5Sandi
132644881bd0Shenning.noren                $this->tableCalls[$key-1][1][0] = false;
13270cecf9d5Sandi
132825b97867Shakan.sandell                for($i = $key-2; $i >= $cellKey[$lastRow][1]; $i--) {
13290cecf9d5Sandi
13300cecf9d5Sandi                    if ( $this->tableCalls[$i][0] == 'tablecell_open' || $this->tableCalls[$i][0] == 'tableheader_open' ) {
13310cecf9d5Sandi
133244881bd0Shenning.noren                        if ( false !== $this->tableCalls[$i][1][0] ) {
13330cecf9d5Sandi                            $this->tableCalls[$i][1][0]++;
13340cecf9d5Sandi                            break;
13350cecf9d5Sandi                        }
13360cecf9d5Sandi
13370cecf9d5Sandi
13380cecf9d5Sandi                    }
13390cecf9d5Sandi                }
13400cecf9d5Sandi
13410cecf9d5Sandi                $toDelete[] = $key-1;
13420cecf9d5Sandi                $toDelete[] = $key;
13430cecf9d5Sandi                $toDelete[] = $key+1;
1344*aa92c4ccSAdrian Lang                break;
134525b97867Shakan.sandell
1346*aa92c4ccSAdrian Lang            case 'rowspan':
134725b97867Shakan.sandell
134825b97867Shakan.sandell                if ( $this->tableCalls[$key-1][0] == 'cdata' ) {
134925b97867Shakan.sandell                    // ignore rowspan if previous call was cdata (text mixed with :::) we don't have to check next call as that wont match regex
135025b97867Shakan.sandell                    $this->tableCalls[$key][0] = 'cdata';
135125b97867Shakan.sandell
135225b97867Shakan.sandell                } else {
135325b97867Shakan.sandell
135425b97867Shakan.sandell                    $this->tableCalls[$key-1][1][2] = false;
135525b97867Shakan.sandell
135625b97867Shakan.sandell                    for($i = $lastRow-1; $i > 0; $i--) {
135725b97867Shakan.sandell
135825b97867Shakan.sandell                        if ( $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tablecell_open' || $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tableheader_open' ) {
135925b97867Shakan.sandell
136025b97867Shakan.sandell                            if ( false !== $this->tableCalls[$cellKey[$i][$lastCell]][1][2] ) {
136125b97867Shakan.sandell                                $this->tableCalls[$cellKey[$i][$lastCell]][1][2]++;
136225b97867Shakan.sandell                                break;
136325b97867Shakan.sandell                            }
136425b97867Shakan.sandell
136525b97867Shakan.sandell
136625b97867Shakan.sandell                        }
136725b97867Shakan.sandell                    }
136825b97867Shakan.sandell
136925b97867Shakan.sandell                    $toDelete[] = $key-1;
137025b97867Shakan.sandell                    $toDelete[] = $key;
137125b97867Shakan.sandell                    $toDelete[] = $key+1;
137225b97867Shakan.sandell                }
1373*aa92c4ccSAdrian Lang                break;
1374*aa92c4ccSAdrian Lang
13750cecf9d5Sandi            }
13760cecf9d5Sandi        }
13770cecf9d5Sandi
13789ab75d9eSAndreas Gohr
13799ab75d9eSAndreas Gohr        // condense cdata
13809ab75d9eSAndreas Gohr        $cnt = count($this->tableCalls);
13819ab75d9eSAndreas Gohr        for( $key = 0; $key < $cnt; $key++){
13829ab75d9eSAndreas Gohr            if($this->tableCalls[$key][0] == 'cdata'){
13839ab75d9eSAndreas Gohr                $ckey = $key;
13849ab75d9eSAndreas Gohr                $key++;
13859ab75d9eSAndreas Gohr                while($this->tableCalls[$key][0] == 'cdata'){
13869ab75d9eSAndreas Gohr                    $this->tableCalls[$ckey][1][0] .= $this->tableCalls[$key][1][0];
13879ab75d9eSAndreas Gohr                    $toDelete[] = $key;
13889ab75d9eSAndreas Gohr                    $key++;
13899ab75d9eSAndreas Gohr                }
13909ab75d9eSAndreas Gohr                continue;
13919ab75d9eSAndreas Gohr            }
13929ab75d9eSAndreas Gohr        }
13939ab75d9eSAndreas Gohr
13940cecf9d5Sandi        foreach ( $toDelete as $delete ) {
13950cecf9d5Sandi            unset($this->tableCalls[$delete]);
13960cecf9d5Sandi        }
13970cecf9d5Sandi        $this->tableCalls = array_values($this->tableCalls);
13980cecf9d5Sandi    }
13990cecf9d5Sandi}
14000cecf9d5Sandi
14010cecf9d5Sandi
14022a27e99aSandi/**
14032a27e99aSandi * Handler for paragraphs
14042a27e99aSandi *
14050b7c14c2Sandi * @author Harry Fuecks <hfuecks@gmail.com>
14062a27e99aSandi */
14070cecf9d5Sandiclass Doku_Handler_Block {
14080cecf9d5Sandi
14090cecf9d5Sandi    var $calls = array();
14100cecf9d5Sandi
14110cecf9d5Sandi    var $blockStack = array();
14120cecf9d5Sandi
141344881bd0Shenning.noren    var $inParagraph = false;
141444881bd0Shenning.noren    var $atStart = true;
141558b56c06Sandi    var $skipEolKey = -1;
14160cecf9d5Sandi
1417af146da0Sandi    // Blocks these should not be inside paragraphs
14180cecf9d5Sandi    var $blockOpen = array(
14190cecf9d5Sandi            'header',
1420df9add72Schris            'listu_open','listo_open','listitem_open','listcontent_open',
14210cecf9d5Sandi            'table_open','tablerow_open','tablecell_open','tableheader_open',
14220cecf9d5Sandi            'quote_open',
14230cecf9d5Sandi            'section_open', // Needed to prevent p_open between header and section_open
142476aa94b7Schris            'code','file','hr','preformatted','rss',
142507f89c3cSAnika Henke            'htmlblock','phpblock',
14260cecf9d5Sandi        );
14270cecf9d5Sandi
14280cecf9d5Sandi    var $blockClose = array(
14290cecf9d5Sandi            'header',
1430df9add72Schris            'listu_close','listo_close','listitem_close','listcontent_close',
14310cecf9d5Sandi            'table_close','tablerow_close','tablecell_close','tableheader_close',
14320cecf9d5Sandi            'quote_close',
14330cecf9d5Sandi            'section_close', // Needed to prevent p_close after section_close
143476aa94b7Schris            'code','file','hr','preformatted','rss',
143507f89c3cSAnika Henke            'htmlblock','phpblock',
14360cecf9d5Sandi        );
14370cecf9d5Sandi
1438af146da0Sandi    // Stacks can contain paragraphs
14390cecf9d5Sandi    var $stackOpen = array(
14400cecf9d5Sandi        'footnote_open','section_open',
14410cecf9d5Sandi        );
14420cecf9d5Sandi
14430cecf9d5Sandi    var $stackClose = array(
14440cecf9d5Sandi        'footnote_close','section_close',
14450cecf9d5Sandi        );
14460cecf9d5Sandi
1447af146da0Sandi
1448af146da0Sandi    /**
1449af146da0Sandi     * Constructor. Adds loaded syntax plugins to the block and stack
1450af146da0Sandi     * arrays
1451af146da0Sandi     *
1452af146da0Sandi     * @author Andreas Gohr <andi@splitbrain.org>
1453af146da0Sandi     */
1454af146da0Sandi    function Doku_Handler_Block(){
1455af146da0Sandi        global $DOKU_PLUGINS;
1456af146da0Sandi        //check if syntax plugins were loaded
145703c4aec3Schris        if(empty($DOKU_PLUGINS['syntax'])) return;
1458af146da0Sandi        foreach($DOKU_PLUGINS['syntax'] as $n => $p){
1459af146da0Sandi            $ptype = $p->getPType();
1460af146da0Sandi            if($ptype == 'block'){
1461af146da0Sandi                $this->blockOpen[]  = 'plugin_'.$n;
1462af146da0Sandi                $this->blockClose[] = 'plugin_'.$n;
1463af146da0Sandi            }elseif($ptype == 'stack'){
1464af146da0Sandi                $this->stackOpen[]  = 'plugin_'.$n;
1465af146da0Sandi                $this->stackClose[] = 'plugin_'.$n;
1466af146da0Sandi            }
1467af146da0Sandi        }
1468af146da0Sandi    }
1469af146da0Sandi
14702a27e99aSandi    /**
14712a27e99aSandi     * Close a paragraph if needed
14722a27e99aSandi     *
14732a27e99aSandi     * This function makes sure there are no empty paragraphs on the stack
14742a27e99aSandi     *
14752a27e99aSandi     * @author Andreas Gohr <andi@splitbrain.org>
14762a27e99aSandi     */
1477506ae684Sandi    function closeParagraph($pos){
1478506ae684Sandi        // look back if there was any content - we don't want empty paragraphs
1479506ae684Sandi        $content = '';
1480506ae684Sandi        for($i=count($this->calls)-1; $i>=0; $i--){
1481506ae684Sandi            if($this->calls[$i][0] == 'p_open'){
1482506ae684Sandi                break;
1483506ae684Sandi            }elseif($this->calls[$i][0] == 'cdata'){
1484506ae684Sandi                $content .= $this->calls[$i][1][0];
1485506ae684Sandi            }else{
1486506ae684Sandi                $content = 'found markup';
1487506ae684Sandi                break;
1488506ae684Sandi            }
1489506ae684Sandi        }
1490506ae684Sandi
1491506ae684Sandi        if(trim($content)==''){
1492506ae684Sandi            //remove the whole paragraph
1493506ae684Sandi            array_splice($this->calls,$i);
1494506ae684Sandi        }else{
1495506ae684Sandi            $this->calls[] = array('p_close',array(), $pos);
1496506ae684Sandi        }
1497e1c10e4dSchris
149844881bd0Shenning.noren        $this->inParagraph = false;
1499506ae684Sandi    }
1500506ae684Sandi
15012a27e99aSandi    /**
15022a27e99aSandi     * Processes the whole instruction stack to open and close paragraphs
15032a27e99aSandi     *
15040b7c14c2Sandi     * @author Harry Fuecks <hfuecks@gmail.com>
15052a27e99aSandi     * @author Andreas Gohr <andi@splitbrain.org>
15062a27e99aSandi     * @todo   This thing is really messy and should be rewritten
15072a27e99aSandi     */
15080cecf9d5Sandi    function process($calls) {
15090cecf9d5Sandi        foreach ( $calls as $key => $call ) {
1510f0891737Sandi            $cname = $call[0];
1511e1c10e4dSchris            if($cname == 'plugin') {
1512e1c10e4dSchris                $cname='plugin_'.$call[1][0];
1513e1c10e4dSchris
1514e1c10e4dSchris                $plugin = true;
1515e1c10e4dSchris                $plugin_open = (($call[1][2] == DOKU_LEXER_ENTER) || ($call[1][2] == DOKU_LEXER_SPECIAL));
1516e1c10e4dSchris                $plugin_close = (($call[1][2] == DOKU_LEXER_EXIT) || ($call[1][2] == DOKU_LEXER_SPECIAL));
1517e1c10e4dSchris            } else {
1518e1c10e4dSchris                $plugin = false;
1519e1c10e4dSchris            }
15200cecf9d5Sandi
15210cecf9d5Sandi            // Process blocks which are stack like... (contain linefeeds)
1522e1c10e4dSchris            if ( in_array($cname,$this->stackOpen ) && (!$plugin || $plugin_open) ) {
1523e1c10e4dSchris
15240cecf9d5Sandi                $this->calls[] = $call;
15250cecf9d5Sandi
15260cecf9d5Sandi                // Hack - footnotes shouldn't immediately contain a p_open
1527f0891737Sandi                if ( $cname != 'footnote_open' ) {
15280cecf9d5Sandi                    $this->addToStack();
15290cecf9d5Sandi                } else {
153044881bd0Shenning.noren                    $this->addToStack(false);
15310cecf9d5Sandi                }
15320cecf9d5Sandi                continue;
15330cecf9d5Sandi            }
15340cecf9d5Sandi
1535e1c10e4dSchris            if ( in_array($cname,$this->stackClose ) && (!$plugin || $plugin_close)) {
15360cecf9d5Sandi
15370cecf9d5Sandi                if ( $this->inParagraph ) {
1538506ae684Sandi                    $this->closeParagraph($call[2]);
15390cecf9d5Sandi                }
15400cecf9d5Sandi                $this->calls[] = $call;
15410cecf9d5Sandi                $this->removeFromStack();
15420cecf9d5Sandi                continue;
15430cecf9d5Sandi            }
15440cecf9d5Sandi
15450cecf9d5Sandi            if ( !$this->atStart ) {
15460cecf9d5Sandi
1547f0891737Sandi                if ( $cname == 'eol' ) {
15480cecf9d5Sandi
1549e1c10e4dSchris                    // Check this isn't an eol instruction to skip...
155058b56c06Sandi                    if ( $this->skipEolKey != $key ) {
1551e1c10e4dSchris                        // Look to see if the next instruction is an EOL
155258b56c06Sandi                        if ( isset($calls[$key+1]) && $calls[$key+1][0] == 'eol' ) {
155358b56c06Sandi
155458b56c06Sandi                            if ( $this->inParagraph ) {
1555506ae684Sandi                                //$this->calls[] = array('p_close',array(), $call[2]);
1556506ae684Sandi                                $this->closeParagraph($call[2]);
155758b56c06Sandi                            }
155858b56c06Sandi
155958b56c06Sandi                            $this->calls[] = array('p_open',array(), $call[2]);
156044881bd0Shenning.noren                            $this->inParagraph = true;
156158b56c06Sandi
156258b56c06Sandi
1563e1c10e4dSchris                            // Mark the next instruction for skipping
156458b56c06Sandi                            $this->skipEolKey = $key+1;
156558b56c06Sandi
156658b56c06Sandi                        }else{
156758b56c06Sandi                            //if this is just a single eol make a space from it
156841624b31SChris Smith                            $this->addCall(array('cdata',array(DOKU_PARSER_EOL), $call[2]));
156958b56c06Sandi                        }
157058b56c06Sandi                    }
157158b56c06Sandi
15720cecf9d5Sandi
15730cecf9d5Sandi                } else {
15740cecf9d5Sandi
157544881bd0Shenning.noren                    $storeCall = true;
1576e1c10e4dSchris                    if ( $this->inParagraph && (in_array($cname, $this->blockOpen) && (!$plugin || $plugin_open))) {
1577506ae684Sandi                        $this->closeParagraph($call[2]);
15780cecf9d5Sandi                        $this->calls[] = $call;
157944881bd0Shenning.noren                        $storeCall = false;
15800cecf9d5Sandi                    }
15810cecf9d5Sandi
1582e1c10e4dSchris                    if ( in_array($cname, $this->blockClose) && (!$plugin || $plugin_close)) {
15830cecf9d5Sandi                        if ( $this->inParagraph ) {
1584506ae684Sandi                            $this->closeParagraph($call[2]);
15850cecf9d5Sandi                        }
15860cecf9d5Sandi                        if ( $storeCall ) {
15870cecf9d5Sandi                            $this->calls[] = $call;
158844881bd0Shenning.noren                            $storeCall = false;
15890cecf9d5Sandi                        }
15900cecf9d5Sandi
15910cecf9d5Sandi                        // This really sucks and suggests this whole class sucks but...
1592e1c10e4dSchris                        if ( isset($calls[$key+1])) {
1593e1c10e4dSchris                            $cname_plusone = $calls[$key+1][0];
1594e1c10e4dSchris                            if ($cname_plusone == 'plugin') {
1595e1c10e4dSchris                                $cname_plusone = 'plugin'.$calls[$key+1][1][0];
1596e1c10e4dSchris
1597e1c10e4dSchris                                // plugin test, true if plugin has a state which precludes it requiring blockOpen or blockClose
1598e1c10e4dSchris                                $plugin_plusone = true;
1599e1c10e4dSchris                                $plugin_test = ($call[$key+1][1][2] == DOKU_LEXER_MATCHED) || ($call[$key+1][1][2] == DOKU_LEXER_MATCHED);
1600e1c10e4dSchris                            } else {
1601e1c10e4dSchris                                $plugin_plusone = false;
1602e1c10e4dSchris                            }
1603e1c10e4dSchris                            if ((!in_array($cname_plusone, $this->blockOpen) && !in_array($cname_plusone, $this->blockClose)) ||
1604e1c10e4dSchris                                ($plugin_plusone && $plugin_test)
16050cecf9d5Sandi                                ) {
16060cecf9d5Sandi
16070cecf9d5Sandi                                $this->calls[] = array('p_open',array(), $call[2]);
160844881bd0Shenning.noren                                $this->inParagraph = true;
16090cecf9d5Sandi                            }
16100cecf9d5Sandi                        }
1611e1c10e4dSchris                    }
16120cecf9d5Sandi
16130cecf9d5Sandi                    if ( $storeCall ) {
161441624b31SChris Smith                        $this->addCall($call);
16150cecf9d5Sandi                    }
16160cecf9d5Sandi
16170cecf9d5Sandi                }
16180cecf9d5Sandi
16190cecf9d5Sandi
16200cecf9d5Sandi            } else {
16210cecf9d5Sandi
16220cecf9d5Sandi                // Unless there's already a block at the start, start a paragraph
1623f0891737Sandi                if ( !in_array($cname,$this->blockOpen) ) {
16240cecf9d5Sandi                    $this->calls[] = array('p_open',array(), $call[2]);
16250cecf9d5Sandi                    if ( $call[0] != 'eol' ) {
16260cecf9d5Sandi                        $this->calls[] = $call;
16270cecf9d5Sandi                    }
162844881bd0Shenning.noren                    $this->atStart = false;
162944881bd0Shenning.noren                    $this->inParagraph = true;
16300cecf9d5Sandi                } else {
163141624b31SChris Smith                    $this->addCall($call);
163244881bd0Shenning.noren                    $this->atStart = false;
16330cecf9d5Sandi                }
16340cecf9d5Sandi
16350cecf9d5Sandi            }
16360cecf9d5Sandi
16370cecf9d5Sandi        }
16380cecf9d5Sandi
16390cecf9d5Sandi        if ( $this->inParagraph ) {
1640f0891737Sandi            if ( $cname == 'p_open' ) {
16410cecf9d5Sandi                // Ditch the last call
16420cecf9d5Sandi                array_pop($this->calls);
1643f0891737Sandi            } else if ( !in_array($cname, $this->blockClose) ) {
1644506ae684Sandi                //$this->calls[] = array('p_close',array(), $call[2]);
1645506ae684Sandi                $this->closeParagraph($call[2]);
16460cecf9d5Sandi            } else {
16470cecf9d5Sandi                $last_call = array_pop($this->calls);
1648506ae684Sandi                //$this->calls[] = array('p_close',array(), $call[2]);
1649506ae684Sandi                $this->closeParagraph($call[2]);
16500cecf9d5Sandi                $this->calls[] = $last_call;
16510cecf9d5Sandi            }
16520cecf9d5Sandi        }
16530cecf9d5Sandi
16540cecf9d5Sandi        return $this->calls;
16550cecf9d5Sandi    }
16560cecf9d5Sandi
165744881bd0Shenning.noren    function addToStack($newStart = true) {
16580cecf9d5Sandi        $this->blockStack[] = array($this->atStart, $this->inParagraph);
16590cecf9d5Sandi        $this->atStart = $newStart;
166044881bd0Shenning.noren        $this->inParagraph = false;
16610cecf9d5Sandi    }
16620cecf9d5Sandi
16630cecf9d5Sandi    function removeFromStack() {
16640cecf9d5Sandi        $state = array_pop($this->blockStack);
16650cecf9d5Sandi        $this->atStart = $state[0];
16660cecf9d5Sandi        $this->inParagraph = $state[1];
16670cecf9d5Sandi    }
166841624b31SChris Smith
166941624b31SChris Smith    function addCall($call) {
167041624b31SChris Smith        $key = count($this->calls);
167141624b31SChris Smith        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
167241624b31SChris Smith            $this->calls[$key-1][1][0] .= $call[1][0];
167341624b31SChris Smith        } else {
167441624b31SChris Smith            $this->calls[] = $call;
167541624b31SChris Smith        }
167641624b31SChris Smith    }
16770cecf9d5Sandi}
16782a27e99aSandi
16794826ab45Sandi//Setup VIM: ex: et ts=4 enc=utf-8 :
1680