xref: /dokuwiki/inc/parser/handler.php (revision 90df9a4d69a2e467433b419b94fe799d11590539)
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);
3470cecf9d5Sandi            $matches[0] = trim($matches[0]);
3483d491f75SAndreas Gohr
3493d491f75SAndreas Gohr            list($language,$filename) = explode(' ',$matches[0],2);
3503d491f75SAndreas Gohr            $language = trim($language);
3513d491f75SAndreas Gohr            $filename = trim($filename);
3523d491f75SAndreas Gohr            if ( $language == '' )  $language = null;
3533d491f75SAndreas Gohr            if ( $language == '-' ) $language = null;
3543d491f75SAndreas Gohr            if ( $filename == '' )  $filename = null;
3553d491f75SAndreas Gohr            # We shortcut html here.
3563d491f75SAndreas Gohr            if($language == 'html') $language = 'html4strict';
357433bef32Sandi            $this->_addCall(
3583d491f75SAndreas Gohr                    $type,
3593d491f75SAndreas Gohr                    array($matches[1],$language,$filename),
3600cecf9d5Sandi                    $pos
3610cecf9d5Sandi                );
3620cecf9d5Sandi        }
36344881bd0Shenning.noren        return true;
3640cecf9d5Sandi    }
3650cecf9d5Sandi
3660cecf9d5Sandi    function acronym($match, $state, $pos) {
367433bef32Sandi        $this->_addCall('acronym',array($match), $pos);
36844881bd0Shenning.noren        return true;
3690cecf9d5Sandi    }
3700cecf9d5Sandi
3710cecf9d5Sandi    function smiley($match, $state, $pos) {
372433bef32Sandi        $this->_addCall('smiley',array($match), $pos);
37344881bd0Shenning.noren        return true;
3740cecf9d5Sandi    }
3750cecf9d5Sandi
3760cecf9d5Sandi    function wordblock($match, $state, $pos) {
377433bef32Sandi        $this->_addCall('wordblock',array($match), $pos);
37844881bd0Shenning.noren        return true;
3790cecf9d5Sandi    }
3800cecf9d5Sandi
3810cecf9d5Sandi    function entity($match, $state, $pos) {
382433bef32Sandi        $this->_addCall('entity',array($match), $pos);
38344881bd0Shenning.noren        return true;
3840cecf9d5Sandi    }
3850cecf9d5Sandi
3860cecf9d5Sandi    function multiplyentity($match, $state, $pos) {
3870cecf9d5Sandi        preg_match_all('/\d+/',$match,$matches);
388433bef32Sandi        $this->_addCall('multiplyentity',array($matches[0][0],$matches[0][1]), $pos);
38944881bd0Shenning.noren        return true;
3900cecf9d5Sandi    }
3910cecf9d5Sandi
3920cecf9d5Sandi    function singlequoteopening($match, $state, $pos) {
393433bef32Sandi        $this->_addCall('singlequoteopening',array(), $pos);
39444881bd0Shenning.noren        return true;
3950cecf9d5Sandi    }
3960cecf9d5Sandi
3970cecf9d5Sandi    function singlequoteclosing($match, $state, $pos) {
398433bef32Sandi        $this->_addCall('singlequoteclosing',array(), $pos);
39944881bd0Shenning.noren        return true;
4000cecf9d5Sandi    }
4010cecf9d5Sandi
40257d757d1SAndreas Gohr    function apostrophe($match, $state, $pos) {
40357d757d1SAndreas Gohr        $this->_addCall('apostrophe',array(), $pos);
40457d757d1SAndreas Gohr        return true;
40557d757d1SAndreas Gohr    }
40657d757d1SAndreas Gohr
4070cecf9d5Sandi    function doublequoteopening($match, $state, $pos) {
408433bef32Sandi        $this->_addCall('doublequoteopening',array(), $pos);
40944881bd0Shenning.noren        return true;
4100cecf9d5Sandi    }
4110cecf9d5Sandi
4120cecf9d5Sandi    function doublequoteclosing($match, $state, $pos) {
413433bef32Sandi        $this->_addCall('doublequoteclosing',array(), $pos);
41444881bd0Shenning.noren        return true;
4150cecf9d5Sandi    }
4160cecf9d5Sandi
4170cecf9d5Sandi    function camelcaselink($match, $state, $pos) {
418433bef32Sandi        $this->_addCall('camelcaselink',array($match), $pos);
41944881bd0Shenning.noren        return true;
4200cecf9d5Sandi    }
4210cecf9d5Sandi
4220cecf9d5Sandi    /*
4230cecf9d5Sandi    */
4240cecf9d5Sandi    function internallink($match, $state, $pos) {
4250cecf9d5Sandi        // Strip the opening and closing markup
4260cecf9d5Sandi        $link = preg_replace(array('/^\[\[/','/\]\]$/u'),'',$match);
4270cecf9d5Sandi
4280cecf9d5Sandi        // Split title from URL
4294b7f9e70STom N Harris        $link = explode('|',$link,2);
4300cecf9d5Sandi        if ( !isset($link[1]) ) {
4310cecf9d5Sandi            $link[1] = NULL;
4320cecf9d5Sandi        } else if ( preg_match('/^\{\{[^\}]+\}\}$/',$link[1]) ) {
4335578eb8fSandi            // If the title is an image, convert it to an array containing the image details
434b625487dSandi            $link[1] = Doku_Handler_Parse_Media($link[1]);
4350cecf9d5Sandi        }
4360b7c14c2Sandi        $link[0] = trim($link[0]);
4370cecf9d5Sandi
4380e1c636eSandi        //decide which kind of link it is
4390e1c636eSandi
440e08dda3fSAndreas Gohr        if ( preg_match('/^[a-zA-Z0-9\.]+>{1}.*$/u',$link[0]) ) {
4410e1c636eSandi        // Interwiki
4424b7f9e70STom N Harris            $interwiki = explode('>',$link[0],2);
443433bef32Sandi            $this->_addCall(
4440cecf9d5Sandi                'interwikilink',
4450cecf9d5Sandi                array($link[0],$link[1],strtolower($interwiki[0]),$interwiki[1]),
4460cecf9d5Sandi                $pos
4470cecf9d5Sandi                );
4480b7c14c2Sandi        }elseif ( preg_match('/^\\\\\\\\[\w.:?\-;,]+?\\\\/u',$link[0]) ) {
4490e1c636eSandi        // Windows Share
450433bef32Sandi            $this->_addCall(
4510cecf9d5Sandi                'windowssharelink',
4520cecf9d5Sandi                array($link[0],$link[1]),
4530cecf9d5Sandi                $pos
4540cecf9d5Sandi                );
4554468cb4cSAndreas Gohr        }elseif ( preg_match('#^([a-z0-9\-\.+]+?)://#i',$link[0]) ) {
4560e1c636eSandi        // external link (accepts all protocols)
457433bef32Sandi            $this->_addCall(
4580cecf9d5Sandi                    'externallink',
4590cecf9d5Sandi                    array($link[0],$link[1]),
4600cecf9d5Sandi                    $pos
4610cecf9d5Sandi                    );
4620a1d30bfSchris        }elseif ( preg_match('<'.PREG_PATTERN_VALID_EMAIL.'>',$link[0]) ) {
4630a1d30bfSchris        // E-Mail (pattern above is defined in inc/mail.php)
464a6755281Sandi            $this->_addCall(
465a6755281Sandi                'emaillink',
466a6755281Sandi                array($link[0],$link[1]),
467a6755281Sandi                $pos
468a6755281Sandi                );
4690b7c14c2Sandi        }elseif ( preg_match('!^#.+!',$link[0]) ){
4700b7c14c2Sandi        // local link
4710b7c14c2Sandi            $this->_addCall(
4720b7c14c2Sandi                'locallink',
4730b7c14c2Sandi                array(substr($link[0],1),$link[1]),
4740b7c14c2Sandi                $pos
4750b7c14c2Sandi                );
4760e1c636eSandi        }else{
4770e1c636eSandi        // internal link
478433bef32Sandi            $this->_addCall(
4790e1c636eSandi                'internallink',
4800e1c636eSandi                array($link[0],$link[1]),
4810e1c636eSandi                $pos
4820e1c636eSandi                );
4830cecf9d5Sandi        }
4840e1c636eSandi
48544881bd0Shenning.noren        return true;
4860cecf9d5Sandi    }
4870cecf9d5Sandi
4880cecf9d5Sandi    function filelink($match, $state, $pos) {
489433bef32Sandi        $this->_addCall('filelink',array($match, NULL), $pos);
49044881bd0Shenning.noren        return true;
4910cecf9d5Sandi    }
4920cecf9d5Sandi
4930cecf9d5Sandi    function windowssharelink($match, $state, $pos) {
494433bef32Sandi        $this->_addCall('windowssharelink',array($match, NULL), $pos);
49544881bd0Shenning.noren        return true;
4960cecf9d5Sandi    }
4970cecf9d5Sandi
4980cecf9d5Sandi    function media($match, $state, $pos) {
4990cecf9d5Sandi        $p = Doku_Handler_Parse_Media($match);
5000cecf9d5Sandi
501433bef32Sandi        $this->_addCall(
5020cecf9d5Sandi              $p['type'],
503dc673a5bSjoe.lapp              array($p['src'], $p['title'], $p['align'], $p['width'],
504dc673a5bSjoe.lapp                     $p['height'], $p['cache'], $p['linking']),
5050cecf9d5Sandi              $pos
5060cecf9d5Sandi             );
50744881bd0Shenning.noren        return true;
5080cecf9d5Sandi    }
5090cecf9d5Sandi
510b625487dSandi    function rss($match, $state, $pos) {
511b625487dSandi        $link = preg_replace(array('/^\{\{rss>/','/\}\}$/'),'',$match);
5123db95becSAndreas Gohr
5133db95becSAndreas Gohr        // get params
5143db95becSAndreas Gohr        list($link,$params) = explode(' ',$link,2);
5153db95becSAndreas Gohr
5163db95becSAndreas Gohr        $p = array();
5173db95becSAndreas Gohr        if(preg_match('/\b(\d+)\b/',$params,$match)){
5183db95becSAndreas Gohr            $p['max'] = $match[1];
5193db95becSAndreas Gohr        }else{
5203db95becSAndreas Gohr            $p['max'] = 8;
5213db95becSAndreas Gohr        }
5223db95becSAndreas Gohr        $p['reverse'] = (preg_match('/rev/',$params));
5233db95becSAndreas Gohr        $p['author']  = (preg_match('/\b(by|author)/',$params));
5243db95becSAndreas Gohr        $p['date']    = (preg_match('/\b(date)/',$params));
5253db95becSAndreas Gohr        $p['details'] = (preg_match('/\b(desc|detail)/',$params));
5263db95becSAndreas Gohr
5270a69dff7Schris        if (preg_match('/\b(\d+)([dhm])\b/',$params,$match)) {
5280a69dff7Schris          $period = array('d' => 86400, 'h' => 3600, 'm' => 60);
5290a69dff7Schris          $p['refresh'] = max(600,$match[1]*$period[$match[2]]);  // n * period in seconds, minimum 10 minutes
5300a69dff7Schris        } else {
5310a69dff7Schris          $p['refresh'] = 14400;   // default to 4 hours
5320a69dff7Schris        }
5330a69dff7Schris
5343db95becSAndreas Gohr        $this->_addCall('rss',array($link,$p),$pos);
53544881bd0Shenning.noren        return true;
536b625487dSandi    }
537b625487dSandi
5380cecf9d5Sandi    function externallink($match, $state, $pos) {
539da9f31c5SAndreas Gohr        $url   = $match;
540da9f31c5SAndreas Gohr        $title = null;
5410cecf9d5Sandi
542da9f31c5SAndreas Gohr        // add protocol on simple short URLs
543da9f31c5SAndreas Gohr        if(substr($url,0,3) == 'ftp' && (substr($url,0,6) != 'ftp://')){
544da9f31c5SAndreas Gohr            $title = $url;
545da9f31c5SAndreas Gohr            $url   = 'ftp://'.$url;
546da9f31c5SAndreas Gohr        }
547da9f31c5SAndreas Gohr        if(substr($url,0,3) == 'www' && (substr($url,0,7) != 'http://')){
548da9f31c5SAndreas Gohr            $title = $url;
549da9f31c5SAndreas Gohr            $url = 'http://'.$url;
550da9f31c5SAndreas Gohr        }
551da9f31c5SAndreas Gohr
552da9f31c5SAndreas Gohr        $this->_addCall('externallink',array($url, $title), $pos);
55344881bd0Shenning.noren        return true;
5540cecf9d5Sandi    }
5550cecf9d5Sandi
55671352defSandi    function emaillink($match, $state, $pos) {
5570cecf9d5Sandi        $email = preg_replace(array('/^</','/>$/'),'',$match);
558433bef32Sandi        $this->_addCall('emaillink',array($email, NULL), $pos);
55944881bd0Shenning.noren        return true;
5600cecf9d5Sandi    }
5610cecf9d5Sandi
5620cecf9d5Sandi    function table($match, $state, $pos) {
5630cecf9d5Sandi        switch ( $state ) {
5640cecf9d5Sandi
5650cecf9d5Sandi            case DOKU_LEXER_ENTER:
5660cecf9d5Sandi
56767f9913dSAndreas Gohr                $ReWriter = new Doku_Handler_Table($this->CallWriter);
5680cecf9d5Sandi                $this->CallWriter = & $ReWriter;
5690cecf9d5Sandi
570*90df9a4dSAdrian Lang                $this->_addCall('table_start', array($pos + 1), $pos);
5710cecf9d5Sandi                if ( trim($match) == '^' ) {
572433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
5730cecf9d5Sandi                } else {
574433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
5750cecf9d5Sandi                }
5760cecf9d5Sandi            break;
5770cecf9d5Sandi
5780cecf9d5Sandi            case DOKU_LEXER_EXIT:
579*90df9a4dSAdrian Lang                $this->_addCall('table_end', array($pos), $pos);
5800cecf9d5Sandi                $this->CallWriter->process();
5810cecf9d5Sandi                $ReWriter = & $this->CallWriter;
5820cecf9d5Sandi                $this->CallWriter = & $ReWriter->CallWriter;
5830cecf9d5Sandi            break;
5840cecf9d5Sandi
5850cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
5860cecf9d5Sandi                if ( trim($match) != '' ) {
587433bef32Sandi                    $this->_addCall('cdata',array($match), $pos);
5880cecf9d5Sandi                }
5890cecf9d5Sandi            break;
5900cecf9d5Sandi
5910cecf9d5Sandi            case DOKU_LEXER_MATCHED:
5929ab75d9eSAndreas Gohr                if ( $match == ' ' ){
5939ab75d9eSAndreas Gohr                    $this->_addCall('cdata', array($match), $pos);
59425b97867Shakan.sandell                } else if ( preg_match('/:::/',$match) ) {
59525b97867Shakan.sandell                    $this->_addCall('rowspan', array($match), $pos);
596e205b721SAndreas Gohr                } else if ( preg_match('/\t+/',$match) ) {
5979ab75d9eSAndreas Gohr                    $this->_addCall('table_align', array($match), $pos);
598e205b721SAndreas Gohr                } else if ( preg_match('/ {2,}/',$match) ) {
599433bef32Sandi                    $this->_addCall('table_align', array($match), $pos);
6000cecf9d5Sandi                } else if ( $match == "\n|" ) {
601433bef32Sandi                    $this->_addCall('table_row', array(), $pos);
602433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
6030cecf9d5Sandi                } else if ( $match == "\n^" ) {
604433bef32Sandi                    $this->_addCall('table_row', array(), $pos);
605433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
6060cecf9d5Sandi                } else if ( $match == '|' ) {
607433bef32Sandi                    $this->_addCall('tablecell', array(), $pos);
6080cecf9d5Sandi                } else if ( $match == '^' ) {
609433bef32Sandi                    $this->_addCall('tableheader', array(), $pos);
6100cecf9d5Sandi                }
6110cecf9d5Sandi            break;
6120cecf9d5Sandi        }
61344881bd0Shenning.noren        return true;
6140cecf9d5Sandi    }
6150cecf9d5Sandi}
6160cecf9d5Sandi
6170cecf9d5Sandi//------------------------------------------------------------------------
6180cecf9d5Sandifunction Doku_Handler_Parse_Media($match) {
6190cecf9d5Sandi
6200cecf9d5Sandi    // Strip the opening and closing markup
6210cecf9d5Sandi    $link = preg_replace(array('/^\{\{/','/\}\}$/u'),'',$match);
6220cecf9d5Sandi
6230cecf9d5Sandi    // Split title from URL
6244b7f9e70STom N Harris    $link = explode('|',$link,2);
6250cecf9d5Sandi
6260cecf9d5Sandi
6270cecf9d5Sandi    // Check alignment
6280cecf9d5Sandi    $ralign = (bool)preg_match('/^ /',$link[0]);
6290cecf9d5Sandi    $lalign = (bool)preg_match('/ $/',$link[0]);
6300cecf9d5Sandi
6310cecf9d5Sandi    // Logic = what's that ;)...
6320cecf9d5Sandi    if ( $lalign & $ralign ) {
6330cecf9d5Sandi        $align = 'center';
6340cecf9d5Sandi    } else if ( $ralign ) {
6350cecf9d5Sandi        $align = 'right';
6360cecf9d5Sandi    } else if ( $lalign ) {
6370cecf9d5Sandi        $align = 'left';
6380cecf9d5Sandi    } else {
6390cecf9d5Sandi        $align = NULL;
6400cecf9d5Sandi    }
6410cecf9d5Sandi
6420cecf9d5Sandi    // The title...
6430cecf9d5Sandi    if ( !isset($link[1]) ) {
6440cecf9d5Sandi        $link[1] = NULL;
6450cecf9d5Sandi    }
6460cecf9d5Sandi
6474826ab45Sandi    //remove aligning spaces
6484826ab45Sandi    $link[0] = trim($link[0]);
6490cecf9d5Sandi
6504826ab45Sandi    //split into src and parameters (using the very last questionmark)
6514826ab45Sandi    $pos = strrpos($link[0], '?');
6524826ab45Sandi    if($pos !== false){
6534826ab45Sandi        $src   = substr($link[0],0,$pos);
6544826ab45Sandi        $param = substr($link[0],$pos+1);
6550cecf9d5Sandi    }else{
6564826ab45Sandi        $src   = $link[0];
6574826ab45Sandi        $param = '';
6580cecf9d5Sandi    }
6590cecf9d5Sandi
6604826ab45Sandi    //parse width and height
6614826ab45Sandi    if(preg_match('#(\d+)(x(\d+))?#i',$param,$size)){
6624826ab45Sandi        ($size[1]) ? $w = $size[1] : $w = NULL;
6634826ab45Sandi        ($size[3]) ? $h = $size[3] : $h = NULL;
664fc1c55b1Shfuecks    } else {
665fc1c55b1Shfuecks        $w = NULL;
666fc1c55b1Shfuecks        $h = NULL;
6670cecf9d5Sandi    }
6680cecf9d5Sandi
669dc673a5bSjoe.lapp    //get linking command
670d35ab615Shenning.noren    if(preg_match('/nolink/i',$param)){
671dc673a5bSjoe.lapp        $linking = 'nolink';
672d35ab615Shenning.noren    }else if(preg_match('/direct/i',$param)){
673dc673a5bSjoe.lapp        $linking = 'direct';
6748acb3108SAndreas Gohr    }else if(preg_match('/linkonly/i',$param)){
6758acb3108SAndreas Gohr        $linking = 'linkonly';
676dc673a5bSjoe.lapp    }else{
677dc673a5bSjoe.lapp        $linking = 'details';
678dc673a5bSjoe.lapp    }
679dc673a5bSjoe.lapp
6804826ab45Sandi    //get caching command
6814826ab45Sandi    if (preg_match('/(nocache|recache)/i',$param,$cachemode)){
6824826ab45Sandi        $cache = $cachemode[1];
6830cecf9d5Sandi    }else{
6844826ab45Sandi        $cache = 'cache';
6850cecf9d5Sandi    }
6860cecf9d5Sandi
6870cecf9d5Sandi    // Check whether this is a local or remote image
6884826ab45Sandi    if ( preg_match('#^(https?|ftp)#i',$src) ) {
6894826ab45Sandi        $call = 'externalmedia';
6900cecf9d5Sandi    } else {
6914826ab45Sandi        $call = 'internalmedia';
6920cecf9d5Sandi    }
6930cecf9d5Sandi
6940cecf9d5Sandi    $params = array(
6950cecf9d5Sandi        'type'=>$call,
6964826ab45Sandi        'src'=>$src,
6970cecf9d5Sandi        'title'=>$link[1],
6980cecf9d5Sandi        'align'=>$align,
6994826ab45Sandi        'width'=>$w,
7004826ab45Sandi        'height'=>$h,
7010cecf9d5Sandi        'cache'=>$cache,
702dc673a5bSjoe.lapp        'linking'=>$linking,
7030cecf9d5Sandi    );
7040cecf9d5Sandi
7050cecf9d5Sandi    return $params;
7060cecf9d5Sandi}
7070cecf9d5Sandi
7080cecf9d5Sandi//------------------------------------------------------------------------
7090cecf9d5Sandiclass Doku_Handler_CallWriter {
7100cecf9d5Sandi
7110cecf9d5Sandi    var $Handler;
7120cecf9d5Sandi
7130cecf9d5Sandi    function Doku_Handler_CallWriter(& $Handler) {
7140cecf9d5Sandi        $this->Handler = & $Handler;
7150cecf9d5Sandi    }
7160cecf9d5Sandi
7170cecf9d5Sandi    function writeCall($call) {
7180cecf9d5Sandi        $this->Handler->calls[] = $call;
7190cecf9d5Sandi    }
7200cecf9d5Sandi
7210cecf9d5Sandi    function writeCalls($calls) {
7220cecf9d5Sandi        $this->Handler->calls = array_merge($this->Handler->calls, $calls);
7230cecf9d5Sandi    }
724f4f02a0fSchris
725f4f02a0fSchris    // function is required, but since this call writer is first/highest in
726f4f02a0fSchris    // the chain it is not required to do anything
727f4f02a0fSchris    function finalise() {
728f4f02a0fSchris    }
7290cecf9d5Sandi}
7300cecf9d5Sandi
7310cecf9d5Sandi//------------------------------------------------------------------------
7325587e44cSchris/**
7335587e44cSchris * Generic call writer class to handle nesting of rendering instructions
7345587e44cSchris * within a render instruction. Also see nest() method of renderer base class
7355587e44cSchris *
7365587e44cSchris * @author    Chris Smith <chris@jalakai.co.uk>
7375587e44cSchris */
7385587e44cSchrisclass Doku_Handler_Nest {
7395587e44cSchris
7405587e44cSchris    var $CallWriter;
7415587e44cSchris    var $calls = array();
7425587e44cSchris
7435587e44cSchris    var $closingInstruction;
7445587e44cSchris
7455587e44cSchris    /**
7465587e44cSchris     * constructor
7475587e44cSchris     *
7485587e44cSchris     * @param  object     $CallWriter     the renderers current call writer
7495587e44cSchris     * @param  string     $close          closing instruction name, this is required to properly terminate the
7505587e44cSchris     *                                    syntax mode if the document ends without a closing pattern
7515587e44cSchris     */
7525587e44cSchris    function Doku_Handler_Nest(& $CallWriter, $close="nest_close") {
7535587e44cSchris        $this->CallWriter = & $CallWriter;
7545587e44cSchris
7555587e44cSchris        $this->closingInstruction = $close;
7565587e44cSchris    }
7575587e44cSchris
7585587e44cSchris    function writeCall($call) {
7595587e44cSchris        $this->calls[] = $call;
7605587e44cSchris    }
7615587e44cSchris
7625587e44cSchris    function writeCalls($calls) {
7635587e44cSchris        $this->calls = array_merge($this->calls, $calls);
7645587e44cSchris    }
7655587e44cSchris
7665587e44cSchris    function finalise() {
7675587e44cSchris        $last_call = end($this->calls);
7685587e44cSchris        $this->writeCall(array($this->closingInstruction,array(), $last_call[2]));
7695587e44cSchris
7705587e44cSchris        $this->process();
7715587e44cSchris        $this->CallWriter->finalise();
7725587e44cSchris    }
7735587e44cSchris
7745587e44cSchris    function process() {
77541624b31SChris Smith        // merge consecutive cdata
77641624b31SChris Smith        $unmerged_calls = $this->calls;
77741624b31SChris Smith        $this->calls = array();
77841624b31SChris Smith
77941624b31SChris Smith        foreach ($unmerged_calls as $call) $this->addCall($call);
78041624b31SChris Smith
7815587e44cSchris        $first_call = reset($this->calls);
7825587e44cSchris        $this->CallWriter->writeCall(array("nest", array($this->calls), $first_call[2]));
7835587e44cSchris    }
78441624b31SChris Smith
78541624b31SChris Smith    function addCall($call) {
78641624b31SChris Smith        $key = count($this->calls);
78741624b31SChris Smith        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
78841624b31SChris Smith            $this->calls[$key-1][1][0] .= $call[1][0];
78973c47f3dSChris Smith        } else if ($call[0] == 'eol') {
79073c47f3dSChris Smith            // do nothing (eol shouldn't be allowed, to counter preformatted fix in #1652 & #1699)
79141624b31SChris Smith        } else {
79241624b31SChris Smith            $this->calls[] = $call;
79341624b31SChris Smith        }
79441624b31SChris Smith    }
7955587e44cSchris}
7965587e44cSchris
7970cecf9d5Sandiclass Doku_Handler_List {
7980cecf9d5Sandi
7990cecf9d5Sandi    var $CallWriter;
8000cecf9d5Sandi
8010cecf9d5Sandi    var $calls = array();
8020cecf9d5Sandi    var $listCalls = array();
8030cecf9d5Sandi    var $listStack = array();
8040cecf9d5Sandi
8050cecf9d5Sandi    function Doku_Handler_List(& $CallWriter) {
8060cecf9d5Sandi        $this->CallWriter = & $CallWriter;
8070cecf9d5Sandi    }
8080cecf9d5Sandi
8090cecf9d5Sandi    function writeCall($call) {
8100cecf9d5Sandi        $this->calls[] = $call;
8110cecf9d5Sandi    }
8120cecf9d5Sandi
8130cecf9d5Sandi    // Probably not needed but just in case...
8140cecf9d5Sandi    function writeCalls($calls) {
8150cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
816f4f02a0fSchris#        $this->CallWriter->writeCalls($this->calls);
817f4f02a0fSchris    }
818f4f02a0fSchris
819f4f02a0fSchris    function finalise() {
820f4f02a0fSchris        $last_call = end($this->calls);
821f4f02a0fSchris        $this->writeCall(array('list_close',array(), $last_call[2]));
822f4f02a0fSchris
823f4f02a0fSchris        $this->process();
824f4f02a0fSchris        $this->CallWriter->finalise();
8250cecf9d5Sandi    }
8260cecf9d5Sandi
8270cecf9d5Sandi    //------------------------------------------------------------------------
8280cecf9d5Sandi    function process() {
829f4f02a0fSchris
8300cecf9d5Sandi        foreach ( $this->calls as $call ) {
8310cecf9d5Sandi            switch ($call[0]) {
8320cecf9d5Sandi                case 'list_item':
8330cecf9d5Sandi                    $this->listOpen($call);
8340cecf9d5Sandi                break;
8350cecf9d5Sandi                case 'list_open':
8360cecf9d5Sandi                    $this->listStart($call);
8370cecf9d5Sandi                break;
8380cecf9d5Sandi                case 'list_close':
8390cecf9d5Sandi                    $this->listEnd($call);
8400cecf9d5Sandi                break;
8410cecf9d5Sandi                default:
8420cecf9d5Sandi                    $this->listContent($call);
8430cecf9d5Sandi                break;
8440cecf9d5Sandi            }
8450cecf9d5Sandi        }
8460cecf9d5Sandi
8470cecf9d5Sandi        $this->CallWriter->writeCalls($this->listCalls);
8480cecf9d5Sandi    }
8490cecf9d5Sandi
8500cecf9d5Sandi    //------------------------------------------------------------------------
8510cecf9d5Sandi    function listStart($call) {
8520cecf9d5Sandi        $depth = $this->interpretSyntax($call[1][0], $listType);
8530cecf9d5Sandi
8540cecf9d5Sandi        $this->initialDepth = $depth;
8550cecf9d5Sandi        $this->listStack[] = array($listType, $depth);
8560cecf9d5Sandi
8570cecf9d5Sandi        $this->listCalls[] = array('list'.$listType.'_open',array(),$call[2]);
8580cecf9d5Sandi        $this->listCalls[] = array('listitem_open',array(1),$call[2]);
8590cecf9d5Sandi        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
8600cecf9d5Sandi    }
8610cecf9d5Sandi
8620cecf9d5Sandi    //------------------------------------------------------------------------
8630cecf9d5Sandi    function listEnd($call) {
86444881bd0Shenning.noren        $closeContent = true;
8650cecf9d5Sandi
8660cecf9d5Sandi        while ( $list = array_pop($this->listStack) ) {
8670cecf9d5Sandi            if ( $closeContent ) {
8680cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
86944881bd0Shenning.noren                $closeContent = false;
8700cecf9d5Sandi            }
8710cecf9d5Sandi            $this->listCalls[] = array('listitem_close',array(),$call[2]);
8720cecf9d5Sandi            $this->listCalls[] = array('list'.$list[0].'_close', array(), $call[2]);
8730cecf9d5Sandi        }
8740cecf9d5Sandi    }
8750cecf9d5Sandi
8760cecf9d5Sandi    //------------------------------------------------------------------------
8770cecf9d5Sandi    function listOpen($call) {
8780cecf9d5Sandi        $depth = $this->interpretSyntax($call[1][0], $listType);
8790cecf9d5Sandi        $end = end($this->listStack);
8800cecf9d5Sandi
8810cecf9d5Sandi        // Not allowed to be shallower than initialDepth
8820cecf9d5Sandi        if ( $depth < $this->initialDepth ) {
8830cecf9d5Sandi            $depth = $this->initialDepth;
8840cecf9d5Sandi        }
8850cecf9d5Sandi
8860cecf9d5Sandi        //------------------------------------------------------------------------
8870cecf9d5Sandi        if ( $depth == $end[1] ) {
8880cecf9d5Sandi
8890cecf9d5Sandi            // Just another item in the list...
8900cecf9d5Sandi            if ( $listType == $end[0] ) {
8910cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
8920cecf9d5Sandi                $this->listCalls[] = array('listitem_close',array(),$call[2]);
8930cecf9d5Sandi                $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
8940cecf9d5Sandi                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
8950cecf9d5Sandi
8960cecf9d5Sandi            // Switched list type...
8970cecf9d5Sandi            } else {
8980cecf9d5Sandi
8990cecf9d5Sandi                $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9000cecf9d5Sandi                $this->listCalls[] = array('listitem_close',array(),$call[2]);
9010cecf9d5Sandi                $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
9020cecf9d5Sandi                $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9030cecf9d5Sandi                $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9040cecf9d5Sandi                $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9050cecf9d5Sandi
9060cecf9d5Sandi                array_pop($this->listStack);
9070cecf9d5Sandi                $this->listStack[] = array($listType, $depth);
9080cecf9d5Sandi            }
9090cecf9d5Sandi
9100cecf9d5Sandi        //------------------------------------------------------------------------
9110cecf9d5Sandi        // Getting deeper...
9120cecf9d5Sandi        } else if ( $depth > $end[1] ) {
9130cecf9d5Sandi
9140cecf9d5Sandi            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9150cecf9d5Sandi            $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9160cecf9d5Sandi            $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9170cecf9d5Sandi            $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9180cecf9d5Sandi
9190cecf9d5Sandi            $this->listStack[] = array($listType, $depth);
9200cecf9d5Sandi
9210cecf9d5Sandi        //------------------------------------------------------------------------
9220cecf9d5Sandi        // Getting shallower ( $depth < $end[1] )
9230cecf9d5Sandi        } else {
9240cecf9d5Sandi            $this->listCalls[] = array('listcontent_close',array(),$call[2]);
9250cecf9d5Sandi            $this->listCalls[] = array('listitem_close',array(),$call[2]);
9260cecf9d5Sandi            $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
9270cecf9d5Sandi
9280cecf9d5Sandi            // Throw away the end - done
9290cecf9d5Sandi            array_pop($this->listStack);
9300cecf9d5Sandi
9310cecf9d5Sandi            while (1) {
9320cecf9d5Sandi                $end = end($this->listStack);
9330cecf9d5Sandi
9340cecf9d5Sandi                if ( $end[1] <= $depth ) {
9350cecf9d5Sandi
9360cecf9d5Sandi                    // Normalize depths
9370cecf9d5Sandi                    $depth = $end[1];
9380cecf9d5Sandi
9390cecf9d5Sandi                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
9400cecf9d5Sandi
9410cecf9d5Sandi                    if ( $end[0] == $listType ) {
9420cecf9d5Sandi                        $this->listCalls[] = array('listitem_open',array($depth-1),$call[2]);
9430cecf9d5Sandi                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9440cecf9d5Sandi
9450cecf9d5Sandi                    } else {
9460cecf9d5Sandi                        // Switching list type...
9470cecf9d5Sandi                        $this->listCalls[] = array('list'.$end[0].'_close', array(), $call[2]);
9480cecf9d5Sandi                        $this->listCalls[] = array('list'.$listType.'_open', array(), $call[2]);
9490cecf9d5Sandi                        $this->listCalls[] = array('listitem_open', array($depth-1), $call[2]);
9500cecf9d5Sandi                        $this->listCalls[] = array('listcontent_open',array(),$call[2]);
9510cecf9d5Sandi
9520cecf9d5Sandi                        array_pop($this->listStack);
9530cecf9d5Sandi                        $this->listStack[] = array($listType, $depth);
9540cecf9d5Sandi                    }
9550cecf9d5Sandi
9560cecf9d5Sandi                    break;
9570cecf9d5Sandi
9580cecf9d5Sandi                // Haven't dropped down far enough yet.... ( $end[1] > $depth )
9590cecf9d5Sandi                } else {
9600cecf9d5Sandi
9610cecf9d5Sandi                    $this->listCalls[] = array('listitem_close',array(),$call[2]);
9620cecf9d5Sandi                    $this->listCalls[] = array('list'.$end[0].'_close',array(),$call[2]);
9630cecf9d5Sandi
9640cecf9d5Sandi                    array_pop($this->listStack);
9650cecf9d5Sandi
9660cecf9d5Sandi                }
9670cecf9d5Sandi
9680cecf9d5Sandi            }
9690cecf9d5Sandi
9700cecf9d5Sandi        }
9710cecf9d5Sandi    }
9720cecf9d5Sandi
9730cecf9d5Sandi    //------------------------------------------------------------------------
9740cecf9d5Sandi    function listContent($call) {
9750cecf9d5Sandi        $this->listCalls[] = $call;
9760cecf9d5Sandi    }
9770cecf9d5Sandi
9780cecf9d5Sandi    //------------------------------------------------------------------------
9790cecf9d5Sandi    function interpretSyntax($match, & $type) {
9800cecf9d5Sandi        if ( substr($match,-1) == '*' ) {
9810cecf9d5Sandi            $type = 'u';
9820cecf9d5Sandi        } else {
9830cecf9d5Sandi            $type = 'o';
9840cecf9d5Sandi        }
9854b7f9e70STom N Harris        // Is the +1 needed? It used to be count(explode(...))
9864b7f9e70STom N Harris        // but I don't think the number is seen outside this handler
9874b7f9e70STom N Harris        return substr_count(str_replace("\t",'  ',$match), '  ') + 1;
9880cecf9d5Sandi    }
9890cecf9d5Sandi}
9900cecf9d5Sandi
9910cecf9d5Sandi//------------------------------------------------------------------------
9920cecf9d5Sandiclass Doku_Handler_Preformatted {
9930cecf9d5Sandi
9940cecf9d5Sandi    var $CallWriter;
9950cecf9d5Sandi
9960cecf9d5Sandi    var $calls = array();
9970cecf9d5Sandi    var $pos;
9980cecf9d5Sandi    var $text ='';
9990cecf9d5Sandi
10000cecf9d5Sandi
10010cecf9d5Sandi
10020cecf9d5Sandi    function Doku_Handler_Preformatted(& $CallWriter) {
10030cecf9d5Sandi        $this->CallWriter = & $CallWriter;
10040cecf9d5Sandi    }
10050cecf9d5Sandi
10060cecf9d5Sandi    function writeCall($call) {
10070cecf9d5Sandi        $this->calls[] = $call;
10080cecf9d5Sandi    }
10090cecf9d5Sandi
10100cecf9d5Sandi    // Probably not needed but just in case...
10110cecf9d5Sandi    function writeCalls($calls) {
10120cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1013f4f02a0fSchris#        $this->CallWriter->writeCalls($this->calls);
1014f4f02a0fSchris    }
1015f4f02a0fSchris
1016f4f02a0fSchris    function finalise() {
1017f4f02a0fSchris        $last_call = end($this->calls);
1018f4f02a0fSchris        $this->writeCall(array('preformatted_end',array(), $last_call[2]));
1019f4f02a0fSchris
1020f4f02a0fSchris        $this->process();
1021f4f02a0fSchris        $this->CallWriter->finalise();
10220cecf9d5Sandi    }
10230cecf9d5Sandi
10240cecf9d5Sandi    function process() {
10250cecf9d5Sandi        foreach ( $this->calls as $call ) {
10260cecf9d5Sandi            switch ($call[0]) {
10270cecf9d5Sandi                case 'preformatted_start':
10280cecf9d5Sandi                    $this->pos = $call[2];
10290cecf9d5Sandi                break;
10300cecf9d5Sandi                case 'preformatted_newline':
10310cecf9d5Sandi                    $this->text .= "\n";
10320cecf9d5Sandi                break;
10330cecf9d5Sandi                case 'preformatted_content':
10340cecf9d5Sandi                    $this->text .= $call[1][0];
10350cecf9d5Sandi                break;
10360cecf9d5Sandi                case 'preformatted_end':
103793a34bf3SChris Smith                    if (trim($this->text)) {
10380cecf9d5Sandi                      $this->CallWriter->writeCall(array('preformatted',array($this->text),$this->pos));
103993a34bf3SChris Smith                    }
104095c19ce7SChris Smith                    // see FS#1699 & FS#1652, add 'eol' instructions to ensure proper triggering of following p_open
104195c19ce7SChris Smith                    $this->CallWriter->writeCall(array('eol',array(),$this->pos));
104295c19ce7SChris Smith                    $this->CallWriter->writeCall(array('eol',array(),$this->pos));
10430cecf9d5Sandi                break;
10440cecf9d5Sandi            }
10450cecf9d5Sandi        }
10460cecf9d5Sandi    }
1047f4f02a0fSchris
10480cecf9d5Sandi}
10490cecf9d5Sandi
10500cecf9d5Sandi//------------------------------------------------------------------------
10510cecf9d5Sandiclass Doku_Handler_Quote {
10520cecf9d5Sandi
10530cecf9d5Sandi    var $CallWriter;
10540cecf9d5Sandi
10550cecf9d5Sandi    var $calls = array();
10560cecf9d5Sandi
10570cecf9d5Sandi    var $quoteCalls = array();
10580cecf9d5Sandi
10590cecf9d5Sandi    function Doku_Handler_Quote(& $CallWriter) {
10600cecf9d5Sandi        $this->CallWriter = & $CallWriter;
10610cecf9d5Sandi    }
10620cecf9d5Sandi
10630cecf9d5Sandi    function writeCall($call) {
10640cecf9d5Sandi        $this->calls[] = $call;
10650cecf9d5Sandi    }
10660cecf9d5Sandi
10670cecf9d5Sandi    // Probably not needed but just in case...
10680cecf9d5Sandi    function writeCalls($calls) {
10690cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1070f4f02a0fSchris    }
1071f4f02a0fSchris
1072f4f02a0fSchris    function finalise() {
1073f4f02a0fSchris        $last_call = end($this->calls);
1074f4f02a0fSchris        $this->writeCall(array('quote_end',array(), $last_call[2]));
1075f4f02a0fSchris
1076f4f02a0fSchris        $this->process();
1077f4f02a0fSchris        $this->CallWriter->finalise();
10780cecf9d5Sandi    }
10790cecf9d5Sandi
10800cecf9d5Sandi    function process() {
10810cecf9d5Sandi
10820cecf9d5Sandi        $quoteDepth = 1;
10830cecf9d5Sandi
10840cecf9d5Sandi        foreach ( $this->calls as $call ) {
10850cecf9d5Sandi            switch ($call[0]) {
10860cecf9d5Sandi
10870cecf9d5Sandi                case 'quote_start':
10880cecf9d5Sandi
10890cecf9d5Sandi                    $this->quoteCalls[] = array('quote_open',array(),$call[2]);
10900cecf9d5Sandi
10910cecf9d5Sandi                case 'quote_newline':
10920cecf9d5Sandi
10930cecf9d5Sandi                    $quoteLength = $this->getDepth($call[1][0]);
10940cecf9d5Sandi
10950cecf9d5Sandi                    if ( $quoteLength > $quoteDepth ) {
10960cecf9d5Sandi                        $quoteDiff = $quoteLength - $quoteDepth;
10970cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
10980cecf9d5Sandi                            $this->quoteCalls[] = array('quote_open',array(),$call[2]);
10990cecf9d5Sandi                        }
11000cecf9d5Sandi                    } else if ( $quoteLength < $quoteDepth ) {
11010cecf9d5Sandi                        $quoteDiff = $quoteDepth - $quoteLength;
11020cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11030cecf9d5Sandi                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11040cecf9d5Sandi                        }
110526426c64Schris                    } else {
110626426c64Schris                        if ($call[0] != 'quote_start') $this->quoteCalls[] = array('linebreak',array(),$call[2]);
11070cecf9d5Sandi                    }
11080cecf9d5Sandi
11090cecf9d5Sandi                    $quoteDepth = $quoteLength;
11100cecf9d5Sandi
11110cecf9d5Sandi                break;
11120cecf9d5Sandi
11130cecf9d5Sandi                case 'quote_end':
11140cecf9d5Sandi
11150cecf9d5Sandi                    if ( $quoteDepth > 1 ) {
11160cecf9d5Sandi                        $quoteDiff = $quoteDepth - 1;
11170cecf9d5Sandi                        for ( $i = 1; $i <= $quoteDiff; $i++ ) {
11180cecf9d5Sandi                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11190cecf9d5Sandi                        }
11200cecf9d5Sandi                    }
11210cecf9d5Sandi
11220cecf9d5Sandi                    $this->quoteCalls[] = array('quote_close',array(),$call[2]);
11230cecf9d5Sandi
11240cecf9d5Sandi                    $this->CallWriter->writeCalls($this->quoteCalls);
11250cecf9d5Sandi                break;
11260cecf9d5Sandi
11270cecf9d5Sandi                default:
11280cecf9d5Sandi                    $this->quoteCalls[] = $call;
11290cecf9d5Sandi                break;
11300cecf9d5Sandi            }
11310cecf9d5Sandi        }
11320cecf9d5Sandi    }
11330cecf9d5Sandi
11340cecf9d5Sandi    function getDepth($marker) {
11350cecf9d5Sandi        preg_match('/>{1,}/', $marker, $matches);
11360cecf9d5Sandi        $quoteLength = strlen($matches[0]);
11370cecf9d5Sandi        return $quoteLength;
11380cecf9d5Sandi    }
11390cecf9d5Sandi}
11400cecf9d5Sandi
11410cecf9d5Sandi//------------------------------------------------------------------------
11420cecf9d5Sandiclass Doku_Handler_Table {
11430cecf9d5Sandi
11440cecf9d5Sandi    var $CallWriter;
11450cecf9d5Sandi
11460cecf9d5Sandi    var $calls = array();
11470cecf9d5Sandi    var $tableCalls = array();
11480cecf9d5Sandi    var $maxCols = 0;
11490cecf9d5Sandi    var $maxRows = 1;
11500cecf9d5Sandi    var $currentCols = 0;
115144881bd0Shenning.noren    var $firstCell = false;
11520cecf9d5Sandi    var $lastCellType = 'tablecell';
11530cecf9d5Sandi
11540cecf9d5Sandi    function Doku_Handler_Table(& $CallWriter) {
11550cecf9d5Sandi        $this->CallWriter = & $CallWriter;
11560cecf9d5Sandi    }
11570cecf9d5Sandi
11580cecf9d5Sandi    function writeCall($call) {
11590cecf9d5Sandi        $this->calls[] = $call;
11600cecf9d5Sandi    }
11610cecf9d5Sandi
11620cecf9d5Sandi    // Probably not needed but just in case...
11630cecf9d5Sandi    function writeCalls($calls) {
11640cecf9d5Sandi        $this->calls = array_merge($this->calls, $calls);
1165f4f02a0fSchris    }
1166f4f02a0fSchris
1167f4f02a0fSchris    function finalise() {
1168f4f02a0fSchris        $last_call = end($this->calls);
1169f4f02a0fSchris        $this->writeCall(array('table_end',array(), $last_call[2]));
1170f4f02a0fSchris
1171f4f02a0fSchris        $this->process();
1172f4f02a0fSchris        $this->CallWriter->finalise();
11730cecf9d5Sandi    }
11740cecf9d5Sandi
11750cecf9d5Sandi    //------------------------------------------------------------------------
11760cecf9d5Sandi    function process() {
11770cecf9d5Sandi        foreach ( $this->calls as $call ) {
11780cecf9d5Sandi            switch ( $call[0] ) {
11790cecf9d5Sandi                case 'table_start':
11800cecf9d5Sandi                    $this->tableStart($call);
11810cecf9d5Sandi                break;
11820cecf9d5Sandi                case 'table_row':
11830cecf9d5Sandi                    $this->tableRowClose(array('tablerow_close',$call[1],$call[2]));
11840cecf9d5Sandi                    $this->tableRowOpen(array('tablerow_open',$call[1],$call[2]));
11850cecf9d5Sandi                break;
11860cecf9d5Sandi                case 'tableheader':
11870cecf9d5Sandi                case 'tablecell':
11880cecf9d5Sandi                    $this->tableCell($call);
11890cecf9d5Sandi                break;
11900cecf9d5Sandi                case 'table_end':
11918af66ab9SAdrian Lang                    $this->tableRowClose(array('tablerow_close', array()));
11920cecf9d5Sandi                    $this->tableEnd($call);
11930cecf9d5Sandi                break;
11940cecf9d5Sandi                default:
11950cecf9d5Sandi                    $this->tableDefault($call);
11960cecf9d5Sandi                break;
11970cecf9d5Sandi            }
11980cecf9d5Sandi        }
11990cecf9d5Sandi        $this->CallWriter->writeCalls($this->tableCalls);
12000cecf9d5Sandi    }
12010cecf9d5Sandi
12020cecf9d5Sandi    function tableStart($call) {
1203*90df9a4dSAdrian Lang        $this->tableCalls[] = array('table_open',$call[1],$call[2]);
12040cecf9d5Sandi        $this->tableCalls[] = array('tablerow_open',array(),$call[2]);
120544881bd0Shenning.noren        $this->firstCell = true;
12060cecf9d5Sandi    }
12070cecf9d5Sandi
12080cecf9d5Sandi    function tableEnd($call) {
120907c2b1c7SAdrian Lang        $this->tableCalls[] = array('table_close',$call[1],$call[2]);
12100cecf9d5Sandi        $this->finalizeTable();
12110cecf9d5Sandi    }
12120cecf9d5Sandi
12130cecf9d5Sandi    function tableRowOpen($call) {
12140cecf9d5Sandi        $this->tableCalls[] = $call;
12150cecf9d5Sandi        $this->currentCols = 0;
121644881bd0Shenning.noren        $this->firstCell = true;
12170cecf9d5Sandi        $this->lastCellType = 'tablecell';
12180cecf9d5Sandi        $this->maxRows++;
12190cecf9d5Sandi    }
12200cecf9d5Sandi
12210cecf9d5Sandi    function tableRowClose($call) {
12220cecf9d5Sandi        // Strip off final cell opening and anything after it
12230cecf9d5Sandi        while ( $discard = array_pop($this->tableCalls ) ) {
12240cecf9d5Sandi
12250cecf9d5Sandi            if ( $discard[0] == 'tablecell_open' || $discard[0] == 'tableheader_open') {
12260cecf9d5Sandi                break;
12270cecf9d5Sandi            }
12280cecf9d5Sandi        }
12290cecf9d5Sandi        $this->tableCalls[] = $call;
12300cecf9d5Sandi
12310cecf9d5Sandi        if ( $this->currentCols > $this->maxCols ) {
12320cecf9d5Sandi            $this->maxCols = $this->currentCols;
12330cecf9d5Sandi        }
12340cecf9d5Sandi    }
12350cecf9d5Sandi
12360cecf9d5Sandi    function tableCell($call) {
12370cecf9d5Sandi        if ( !$this->firstCell ) {
12380cecf9d5Sandi
12390cecf9d5Sandi            // Increase the span
12400cecf9d5Sandi            $lastCall = end($this->tableCalls);
12410cecf9d5Sandi
12420cecf9d5Sandi            // A cell call which follows an open cell means an empty cell so span
12430cecf9d5Sandi            if ( $lastCall[0] == 'tablecell_open' || $lastCall[0] == 'tableheader_open' ) {
12440cecf9d5Sandi                 $this->tableCalls[] = array('colspan',array(),$call[2]);
12450cecf9d5Sandi
12460cecf9d5Sandi            }
12470cecf9d5Sandi
12480cecf9d5Sandi            $this->tableCalls[] = array($this->lastCellType.'_close',array(),$call[2]);
124925b97867Shakan.sandell            $this->tableCalls[] = array($call[0].'_open',array(1,NULL,1),$call[2]);
12500cecf9d5Sandi            $this->lastCellType = $call[0];
12510cecf9d5Sandi
12520cecf9d5Sandi        } else {
12530cecf9d5Sandi
125425b97867Shakan.sandell            $this->tableCalls[] = array($call[0].'_open',array(1,NULL,1),$call[2]);
12550cecf9d5Sandi            $this->lastCellType = $call[0];
125644881bd0Shenning.noren            $this->firstCell = false;
12570cecf9d5Sandi
12580cecf9d5Sandi        }
12590cecf9d5Sandi
12600cecf9d5Sandi        $this->currentCols++;
12610cecf9d5Sandi    }
12620cecf9d5Sandi
12630cecf9d5Sandi    function tableDefault($call) {
12640cecf9d5Sandi        $this->tableCalls[] = $call;
12650cecf9d5Sandi    }
12660cecf9d5Sandi
12670cecf9d5Sandi    function finalizeTable() {
12680cecf9d5Sandi
12690cecf9d5Sandi        // Add the max cols and rows to the table opening
12700cecf9d5Sandi        if ( $this->tableCalls[0][0] == 'table_open' ) {
12710cecf9d5Sandi            // Adjust to num cols not num col delimeters
12720cecf9d5Sandi            $this->tableCalls[0][1][] = $this->maxCols - 1;
12730cecf9d5Sandi            $this->tableCalls[0][1][] = $this->maxRows;
1274*90df9a4dSAdrian Lang            $this->tableCalls[0][1][] = array_shift($this->tableCalls[0][1]);
12750cecf9d5Sandi        } else {
12760cecf9d5Sandi            trigger_error('First element in table call list is not table_open');
12770cecf9d5Sandi        }
12780cecf9d5Sandi
12790cecf9d5Sandi        $lastRow = 0;
12800cecf9d5Sandi        $lastCell = 0;
128125b97867Shakan.sandell        $cellKey = array();
12820cecf9d5Sandi        $toDelete = array();
12830cecf9d5Sandi
12840cecf9d5Sandi        // Look for the colspan elements and increment the colspan on the
12850cecf9d5Sandi        // previous non-empty opening cell. Once done, delete all the cells
12860cecf9d5Sandi        // that contain colspans
12870cecf9d5Sandi        foreach ( $this->tableCalls as $key => $call ) {
12880cecf9d5Sandi
12890cecf9d5Sandi            if ( $call[0] == 'tablerow_open' ) {
12900cecf9d5Sandi
129125b97867Shakan.sandell                $lastRow++;
129225b97867Shakan.sandell                $lastCell = 0;
12930cecf9d5Sandi
12940cecf9d5Sandi            } else if ( $call[0] == 'tablecell_open' || $call[0] == 'tableheader_open' ) {
12950cecf9d5Sandi
129625b97867Shakan.sandell                $lastCell++;
129725b97867Shakan.sandell                $cellKey[$lastRow][$lastCell] = $key;
12980cecf9d5Sandi
12990cecf9d5Sandi            } else if ( $call[0] == 'table_align' ) {
13000cecf9d5Sandi
1301e03b8b2eSAdrian Lang                $prev = in_array($this->tableCalls[$key-1][0], array('tablecell_open', 'tableheader_open'));
1302e03b8b2eSAdrian Lang                $next = in_array($this->tableCalls[$key+1][0], array('tablecell_close', 'tableheader_close'));
1303e03b8b2eSAdrian Lang                // If the cell is empty, align left
1304e03b8b2eSAdrian Lang                if ($prev && $next) {
1305e03b8b2eSAdrian Lang                    $this->tableCalls[$key-1][1][1] = 'left';
1306e03b8b2eSAdrian Lang
13070cecf9d5Sandi                // If the previous element was a cell open, align right
1308e03b8b2eSAdrian Lang                } elseif ($prev) {
13090cecf9d5Sandi                    $this->tableCalls[$key-1][1][1] = 'right';
13100cecf9d5Sandi
1311e03b8b2eSAdrian Lang                // If the next element is the close of an element, align either center or left
1312e03b8b2eSAdrian Lang                } elseif ( $next) {
131325b97867Shakan.sandell                    if ( $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] == 'right' ) {
131425b97867Shakan.sandell                        $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] = 'center';
13150cecf9d5Sandi                    } else {
131625b97867Shakan.sandell                        $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] = 'left';
13170cecf9d5Sandi                    }
13180cecf9d5Sandi
13190cecf9d5Sandi                }
13200cecf9d5Sandi
13210cecf9d5Sandi                // Now convert the whitespace back to cdata
13220cecf9d5Sandi                $this->tableCalls[$key][0] = 'cdata';
13230cecf9d5Sandi
13240cecf9d5Sandi            } else if ( $call[0] == '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;
134425b97867Shakan.sandell
134525b97867Shakan.sandell            } else if ( $call[0] == 'rowspan' ) {
134625b97867Shakan.sandell
134725b97867Shakan.sandell                if ( $this->tableCalls[$key-1][0] == 'cdata' ) {
134825b97867Shakan.sandell                    // ignore rowspan if previous call was cdata (text mixed with :::) we don't have to check next call as that wont match regex
134925b97867Shakan.sandell                    $this->tableCalls[$key][0] = 'cdata';
135025b97867Shakan.sandell
135125b97867Shakan.sandell                } else {
135225b97867Shakan.sandell
135325b97867Shakan.sandell                    $this->tableCalls[$key-1][1][2] = false;
135425b97867Shakan.sandell
135525b97867Shakan.sandell                    for($i = $lastRow-1; $i > 0; $i--) {
135625b97867Shakan.sandell
135725b97867Shakan.sandell                        if ( $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tablecell_open' || $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tableheader_open' ) {
135825b97867Shakan.sandell
135925b97867Shakan.sandell                            if ( false !== $this->tableCalls[$cellKey[$i][$lastCell]][1][2] ) {
136025b97867Shakan.sandell                                $this->tableCalls[$cellKey[$i][$lastCell]][1][2]++;
136125b97867Shakan.sandell                                break;
136225b97867Shakan.sandell                            }
136325b97867Shakan.sandell
136425b97867Shakan.sandell
136525b97867Shakan.sandell                        }
136625b97867Shakan.sandell                    }
136725b97867Shakan.sandell
136825b97867Shakan.sandell                    $toDelete[] = $key-1;
136925b97867Shakan.sandell                    $toDelete[] = $key;
137025b97867Shakan.sandell                    $toDelete[] = $key+1;
137125b97867Shakan.sandell                }
13720cecf9d5Sandi            }
13730cecf9d5Sandi        }
13740cecf9d5Sandi
13759ab75d9eSAndreas Gohr
13769ab75d9eSAndreas Gohr        // condense cdata
13779ab75d9eSAndreas Gohr        $cnt = count($this->tableCalls);
13789ab75d9eSAndreas Gohr        for( $key = 0; $key < $cnt; $key++){
13799ab75d9eSAndreas Gohr            if($this->tableCalls[$key][0] == 'cdata'){
13809ab75d9eSAndreas Gohr                $ckey = $key;
13819ab75d9eSAndreas Gohr                $key++;
13829ab75d9eSAndreas Gohr                while($this->tableCalls[$key][0] == 'cdata'){
13839ab75d9eSAndreas Gohr                    $this->tableCalls[$ckey][1][0] .= $this->tableCalls[$key][1][0];
13849ab75d9eSAndreas Gohr                    $toDelete[] = $key;
13859ab75d9eSAndreas Gohr                    $key++;
13869ab75d9eSAndreas Gohr                }
13879ab75d9eSAndreas Gohr                continue;
13889ab75d9eSAndreas Gohr            }
13899ab75d9eSAndreas Gohr        }
13909ab75d9eSAndreas Gohr
13910cecf9d5Sandi        foreach ( $toDelete as $delete ) {
13920cecf9d5Sandi            unset($this->tableCalls[$delete]);
13930cecf9d5Sandi        }
13940cecf9d5Sandi        $this->tableCalls = array_values($this->tableCalls);
13950cecf9d5Sandi    }
13960cecf9d5Sandi}
13970cecf9d5Sandi
13980cecf9d5Sandi
13992a27e99aSandi/**
14002a27e99aSandi * Handler for paragraphs
14012a27e99aSandi *
14020b7c14c2Sandi * @author Harry Fuecks <hfuecks@gmail.com>
14032a27e99aSandi */
14040cecf9d5Sandiclass Doku_Handler_Block {
14050cecf9d5Sandi
14060cecf9d5Sandi    var $calls = array();
14070cecf9d5Sandi
14080cecf9d5Sandi    var $blockStack = array();
14090cecf9d5Sandi
141044881bd0Shenning.noren    var $inParagraph = false;
141144881bd0Shenning.noren    var $atStart = true;
141258b56c06Sandi    var $skipEolKey = -1;
14130cecf9d5Sandi
1414af146da0Sandi    // Blocks these should not be inside paragraphs
14150cecf9d5Sandi    var $blockOpen = array(
14160cecf9d5Sandi            'header',
1417df9add72Schris            'listu_open','listo_open','listitem_open','listcontent_open',
14180cecf9d5Sandi            'table_open','tablerow_open','tablecell_open','tableheader_open',
14190cecf9d5Sandi            'quote_open',
14200cecf9d5Sandi            'section_open', // Needed to prevent p_open between header and section_open
142176aa94b7Schris            'code','file','hr','preformatted','rss',
142207f89c3cSAnika Henke            'htmlblock','phpblock',
14230cecf9d5Sandi        );
14240cecf9d5Sandi
14250cecf9d5Sandi    var $blockClose = array(
14260cecf9d5Sandi            'header',
1427df9add72Schris            'listu_close','listo_close','listitem_close','listcontent_close',
14280cecf9d5Sandi            'table_close','tablerow_close','tablecell_close','tableheader_close',
14290cecf9d5Sandi            'quote_close',
14300cecf9d5Sandi            'section_close', // Needed to prevent p_close after section_close
143176aa94b7Schris            'code','file','hr','preformatted','rss',
143207f89c3cSAnika Henke            'htmlblock','phpblock',
14330cecf9d5Sandi        );
14340cecf9d5Sandi
1435af146da0Sandi    // Stacks can contain paragraphs
14360cecf9d5Sandi    var $stackOpen = array(
14370cecf9d5Sandi        'footnote_open','section_open',
14380cecf9d5Sandi        );
14390cecf9d5Sandi
14400cecf9d5Sandi    var $stackClose = array(
14410cecf9d5Sandi        'footnote_close','section_close',
14420cecf9d5Sandi        );
14430cecf9d5Sandi
1444af146da0Sandi
1445af146da0Sandi    /**
1446af146da0Sandi     * Constructor. Adds loaded syntax plugins to the block and stack
1447af146da0Sandi     * arrays
1448af146da0Sandi     *
1449af146da0Sandi     * @author Andreas Gohr <andi@splitbrain.org>
1450af146da0Sandi     */
1451af146da0Sandi    function Doku_Handler_Block(){
1452af146da0Sandi        global $DOKU_PLUGINS;
1453af146da0Sandi        //check if syntax plugins were loaded
145403c4aec3Schris        if(empty($DOKU_PLUGINS['syntax'])) return;
1455af146da0Sandi        foreach($DOKU_PLUGINS['syntax'] as $n => $p){
1456af146da0Sandi            $ptype = $p->getPType();
1457af146da0Sandi            if($ptype == 'block'){
1458af146da0Sandi                $this->blockOpen[]  = 'plugin_'.$n;
1459af146da0Sandi                $this->blockClose[] = 'plugin_'.$n;
1460af146da0Sandi            }elseif($ptype == 'stack'){
1461af146da0Sandi                $this->stackOpen[]  = 'plugin_'.$n;
1462af146da0Sandi                $this->stackClose[] = 'plugin_'.$n;
1463af146da0Sandi            }
1464af146da0Sandi        }
1465af146da0Sandi    }
1466af146da0Sandi
14672a27e99aSandi    /**
14682a27e99aSandi     * Close a paragraph if needed
14692a27e99aSandi     *
14702a27e99aSandi     * This function makes sure there are no empty paragraphs on the stack
14712a27e99aSandi     *
14722a27e99aSandi     * @author Andreas Gohr <andi@splitbrain.org>
14732a27e99aSandi     */
1474506ae684Sandi    function closeParagraph($pos){
1475506ae684Sandi        // look back if there was any content - we don't want empty paragraphs
1476506ae684Sandi        $content = '';
1477506ae684Sandi        for($i=count($this->calls)-1; $i>=0; $i--){
1478506ae684Sandi            if($this->calls[$i][0] == 'p_open'){
1479506ae684Sandi                break;
1480506ae684Sandi            }elseif($this->calls[$i][0] == 'cdata'){
1481506ae684Sandi                $content .= $this->calls[$i][1][0];
1482506ae684Sandi            }else{
1483506ae684Sandi                $content = 'found markup';
1484506ae684Sandi                break;
1485506ae684Sandi            }
1486506ae684Sandi        }
1487506ae684Sandi
1488506ae684Sandi        if(trim($content)==''){
1489506ae684Sandi            //remove the whole paragraph
1490506ae684Sandi            array_splice($this->calls,$i);
1491506ae684Sandi        }else{
1492506ae684Sandi            $this->calls[] = array('p_close',array(), $pos);
1493506ae684Sandi        }
1494e1c10e4dSchris
149544881bd0Shenning.noren        $this->inParagraph = false;
1496506ae684Sandi    }
1497506ae684Sandi
14982a27e99aSandi    /**
14992a27e99aSandi     * Processes the whole instruction stack to open and close paragraphs
15002a27e99aSandi     *
15010b7c14c2Sandi     * @author Harry Fuecks <hfuecks@gmail.com>
15022a27e99aSandi     * @author Andreas Gohr <andi@splitbrain.org>
15032a27e99aSandi     * @todo   This thing is really messy and should be rewritten
15042a27e99aSandi     */
15050cecf9d5Sandi    function process($calls) {
15060cecf9d5Sandi        foreach ( $calls as $key => $call ) {
1507f0891737Sandi            $cname = $call[0];
1508e1c10e4dSchris            if($cname == 'plugin') {
1509e1c10e4dSchris                $cname='plugin_'.$call[1][0];
1510e1c10e4dSchris
1511e1c10e4dSchris                $plugin = true;
1512e1c10e4dSchris                $plugin_open = (($call[1][2] == DOKU_LEXER_ENTER) || ($call[1][2] == DOKU_LEXER_SPECIAL));
1513e1c10e4dSchris                $plugin_close = (($call[1][2] == DOKU_LEXER_EXIT) || ($call[1][2] == DOKU_LEXER_SPECIAL));
1514e1c10e4dSchris            } else {
1515e1c10e4dSchris                $plugin = false;
1516e1c10e4dSchris            }
15170cecf9d5Sandi
15180cecf9d5Sandi            // Process blocks which are stack like... (contain linefeeds)
1519e1c10e4dSchris            if ( in_array($cname,$this->stackOpen ) && (!$plugin || $plugin_open) ) {
1520e1c10e4dSchris
15210cecf9d5Sandi                $this->calls[] = $call;
15220cecf9d5Sandi
15230cecf9d5Sandi                // Hack - footnotes shouldn't immediately contain a p_open
1524f0891737Sandi                if ( $cname != 'footnote_open' ) {
15250cecf9d5Sandi                    $this->addToStack();
15260cecf9d5Sandi                } else {
152744881bd0Shenning.noren                    $this->addToStack(false);
15280cecf9d5Sandi                }
15290cecf9d5Sandi                continue;
15300cecf9d5Sandi            }
15310cecf9d5Sandi
1532e1c10e4dSchris            if ( in_array($cname,$this->stackClose ) && (!$plugin || $plugin_close)) {
15330cecf9d5Sandi
15340cecf9d5Sandi                if ( $this->inParagraph ) {
1535506ae684Sandi                    $this->closeParagraph($call[2]);
15360cecf9d5Sandi                }
15370cecf9d5Sandi                $this->calls[] = $call;
15380cecf9d5Sandi                $this->removeFromStack();
15390cecf9d5Sandi                continue;
15400cecf9d5Sandi            }
15410cecf9d5Sandi
15420cecf9d5Sandi            if ( !$this->atStart ) {
15430cecf9d5Sandi
1544f0891737Sandi                if ( $cname == 'eol' ) {
15450cecf9d5Sandi
1546e1c10e4dSchris                    // Check this isn't an eol instruction to skip...
154758b56c06Sandi                    if ( $this->skipEolKey != $key ) {
1548e1c10e4dSchris                        // Look to see if the next instruction is an EOL
154958b56c06Sandi                        if ( isset($calls[$key+1]) && $calls[$key+1][0] == 'eol' ) {
155058b56c06Sandi
155158b56c06Sandi                            if ( $this->inParagraph ) {
1552506ae684Sandi                                //$this->calls[] = array('p_close',array(), $call[2]);
1553506ae684Sandi                                $this->closeParagraph($call[2]);
155458b56c06Sandi                            }
155558b56c06Sandi
155658b56c06Sandi                            $this->calls[] = array('p_open',array(), $call[2]);
155744881bd0Shenning.noren                            $this->inParagraph = true;
155858b56c06Sandi
155958b56c06Sandi
1560e1c10e4dSchris                            // Mark the next instruction for skipping
156158b56c06Sandi                            $this->skipEolKey = $key+1;
156258b56c06Sandi
156358b56c06Sandi                        }else{
156458b56c06Sandi                            //if this is just a single eol make a space from it
156541624b31SChris Smith                            $this->addCall(array('cdata',array(DOKU_PARSER_EOL), $call[2]));
156658b56c06Sandi                        }
156758b56c06Sandi                    }
156858b56c06Sandi
15690cecf9d5Sandi
15700cecf9d5Sandi                } else {
15710cecf9d5Sandi
157244881bd0Shenning.noren                    $storeCall = true;
1573e1c10e4dSchris                    if ( $this->inParagraph && (in_array($cname, $this->blockOpen) && (!$plugin || $plugin_open))) {
1574506ae684Sandi                        $this->closeParagraph($call[2]);
15750cecf9d5Sandi                        $this->calls[] = $call;
157644881bd0Shenning.noren                        $storeCall = false;
15770cecf9d5Sandi                    }
15780cecf9d5Sandi
1579e1c10e4dSchris                    if ( in_array($cname, $this->blockClose) && (!$plugin || $plugin_close)) {
15800cecf9d5Sandi                        if ( $this->inParagraph ) {
1581506ae684Sandi                            $this->closeParagraph($call[2]);
15820cecf9d5Sandi                        }
15830cecf9d5Sandi                        if ( $storeCall ) {
15840cecf9d5Sandi                            $this->calls[] = $call;
158544881bd0Shenning.noren                            $storeCall = false;
15860cecf9d5Sandi                        }
15870cecf9d5Sandi
15880cecf9d5Sandi                        // This really sucks and suggests this whole class sucks but...
1589e1c10e4dSchris                        if ( isset($calls[$key+1])) {
1590e1c10e4dSchris                            $cname_plusone = $calls[$key+1][0];
1591e1c10e4dSchris                            if ($cname_plusone == 'plugin') {
1592e1c10e4dSchris                                $cname_plusone = 'plugin'.$calls[$key+1][1][0];
1593e1c10e4dSchris
1594e1c10e4dSchris                                // plugin test, true if plugin has a state which precludes it requiring blockOpen or blockClose
1595e1c10e4dSchris                                $plugin_plusone = true;
1596e1c10e4dSchris                                $plugin_test = ($call[$key+1][1][2] == DOKU_LEXER_MATCHED) || ($call[$key+1][1][2] == DOKU_LEXER_MATCHED);
1597e1c10e4dSchris                            } else {
1598e1c10e4dSchris                                $plugin_plusone = false;
1599e1c10e4dSchris                            }
1600e1c10e4dSchris                            if ((!in_array($cname_plusone, $this->blockOpen) && !in_array($cname_plusone, $this->blockClose)) ||
1601e1c10e4dSchris                                ($plugin_plusone && $plugin_test)
16020cecf9d5Sandi                                ) {
16030cecf9d5Sandi
16040cecf9d5Sandi                                $this->calls[] = array('p_open',array(), $call[2]);
160544881bd0Shenning.noren                                $this->inParagraph = true;
16060cecf9d5Sandi                            }
16070cecf9d5Sandi                        }
1608e1c10e4dSchris                    }
16090cecf9d5Sandi
16100cecf9d5Sandi                    if ( $storeCall ) {
161141624b31SChris Smith                        $this->addCall($call);
16120cecf9d5Sandi                    }
16130cecf9d5Sandi
16140cecf9d5Sandi                }
16150cecf9d5Sandi
16160cecf9d5Sandi
16170cecf9d5Sandi            } else {
16180cecf9d5Sandi
16190cecf9d5Sandi                // Unless there's already a block at the start, start a paragraph
1620f0891737Sandi                if ( !in_array($cname,$this->blockOpen) ) {
16210cecf9d5Sandi                    $this->calls[] = array('p_open',array(), $call[2]);
16220cecf9d5Sandi                    if ( $call[0] != 'eol' ) {
16230cecf9d5Sandi                        $this->calls[] = $call;
16240cecf9d5Sandi                    }
162544881bd0Shenning.noren                    $this->atStart = false;
162644881bd0Shenning.noren                    $this->inParagraph = true;
16270cecf9d5Sandi                } else {
162841624b31SChris Smith                    $this->addCall($call);
162944881bd0Shenning.noren                    $this->atStart = false;
16300cecf9d5Sandi                }
16310cecf9d5Sandi
16320cecf9d5Sandi            }
16330cecf9d5Sandi
16340cecf9d5Sandi        }
16350cecf9d5Sandi
16360cecf9d5Sandi        if ( $this->inParagraph ) {
1637f0891737Sandi            if ( $cname == 'p_open' ) {
16380cecf9d5Sandi                // Ditch the last call
16390cecf9d5Sandi                array_pop($this->calls);
1640f0891737Sandi            } else if ( !in_array($cname, $this->blockClose) ) {
1641506ae684Sandi                //$this->calls[] = array('p_close',array(), $call[2]);
1642506ae684Sandi                $this->closeParagraph($call[2]);
16430cecf9d5Sandi            } else {
16440cecf9d5Sandi                $last_call = array_pop($this->calls);
1645506ae684Sandi                //$this->calls[] = array('p_close',array(), $call[2]);
1646506ae684Sandi                $this->closeParagraph($call[2]);
16470cecf9d5Sandi                $this->calls[] = $last_call;
16480cecf9d5Sandi            }
16490cecf9d5Sandi        }
16500cecf9d5Sandi
16510cecf9d5Sandi        return $this->calls;
16520cecf9d5Sandi    }
16530cecf9d5Sandi
165444881bd0Shenning.noren    function addToStack($newStart = true) {
16550cecf9d5Sandi        $this->blockStack[] = array($this->atStart, $this->inParagraph);
16560cecf9d5Sandi        $this->atStart = $newStart;
165744881bd0Shenning.noren        $this->inParagraph = false;
16580cecf9d5Sandi    }
16590cecf9d5Sandi
16600cecf9d5Sandi    function removeFromStack() {
16610cecf9d5Sandi        $state = array_pop($this->blockStack);
16620cecf9d5Sandi        $this->atStart = $state[0];
16630cecf9d5Sandi        $this->inParagraph = $state[1];
16640cecf9d5Sandi    }
166541624b31SChris Smith
166641624b31SChris Smith    function addCall($call) {
166741624b31SChris Smith        $key = count($this->calls);
166841624b31SChris Smith        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
166941624b31SChris Smith            $this->calls[$key-1][1][0] .= $call[1][0];
167041624b31SChris Smith        } else {
167141624b31SChris Smith            $this->calls[] = $call;
167241624b31SChris Smith        }
167341624b31SChris Smith    }
16740cecf9d5Sandi}
16752a27e99aSandi
16764826ab45Sandi//Setup VIM: ex: et ts=4 enc=utf-8 :
1677