xref: /dokuwiki/inc/parser/handler.php (revision deb02173166b57c71c367093674310ecb1f82631)
10cecf9d5Sandi<?php
25c2aad12SAndreas Gohr
3cbb44eabSAndreas Gohruse dokuwiki\Extension\Event;
4e1d9dcc8SAndreas Gohruse dokuwiki\Extension\SyntaxPlugin;
5be906b56SAndreas Gohruse dokuwiki\Parsing\Handler\Block;
6be906b56SAndreas Gohruse dokuwiki\Parsing\Handler\CallWriter;
7be906b56SAndreas Gohruse dokuwiki\Parsing\Handler\CallWriterInterface;
8be906b56SAndreas Gohruse dokuwiki\Parsing\Handler\Lists;
9be906b56SAndreas Gohruse dokuwiki\Parsing\Handler\Nest;
10be906b56SAndreas Gohruse dokuwiki\Parsing\Handler\Preformatted;
11be906b56SAndreas Gohruse dokuwiki\Parsing\Handler\Quote;
12be906b56SAndreas Gohruse dokuwiki\Parsing\Handler\Table;
135c2aad12SAndreas Gohr
148b1b81beSAndreas Gohr/**
158b1b81beSAndreas Gohr * Class Doku_Handler
168b1b81beSAndreas Gohr */
170cecf9d5Sandiclass Doku_Handler {
188b1b81beSAndreas Gohr    /** @var CallWriterInterface */
198b1b81beSAndreas Gohr    protected $callWriter = null;
200cecf9d5Sandi
218b1b81beSAndreas Gohr    /** @var array The current CallWriter will write directly to this list of calls, Parser reads it */
228b1b81beSAndreas Gohr    public $calls = array();
230cecf9d5Sandi
248b1b81beSAndreas Gohr    /** @var array internal status holders for some modes */
258b1b81beSAndreas Gohr    protected $status = array(
2644881bd0Shenning.noren        'section' => false,
27e950d12fSChristopher Smith        'doublequote' => 0,
280cecf9d5Sandi    );
290cecf9d5Sandi
308b1b81beSAndreas Gohr    /** @var bool should blocks be rewritten? FIXME seems to always be true */
318b1b81beSAndreas Gohr    protected $rewriteBlocks = true;
32b7c441b9SHarry Fuecks
338b1b81beSAndreas Gohr    /**
348b1b81beSAndreas Gohr     * Doku_Handler constructor.
358b1b81beSAndreas Gohr     */
36e1cdd96cSAndreas Gohr    public function __construct() {
378b1b81beSAndreas Gohr        $this->callWriter = new CallWriter($this);
380cecf9d5Sandi    }
390cecf9d5Sandi
40276820f7SScrutinizer Auto-Fixer    /**
418b1b81beSAndreas Gohr     * Add a new call by passing it to the current CallWriter
428b1b81beSAndreas Gohr     *
438b1b81beSAndreas Gohr     * @param string $handler handler method name (see mode handlers below)
448b1b81beSAndreas Gohr     * @param mixed $args arguments for this call
458b1b81beSAndreas Gohr     * @param int $pos  byte position in the original source file
46276820f7SScrutinizer Auto-Fixer     */
4731c0895aSAndreas Gohr    public function addCall($handler, $args, $pos) {
480cecf9d5Sandi        $call = array($handler,$args, $pos);
498b1b81beSAndreas Gohr        $this->callWriter->writeCall($call);
500cecf9d5Sandi    }
510cecf9d5Sandi
5231c0895aSAndreas Gohr    /** @deprecated 2019-10-31 use addCall() instead */
5331c0895aSAndreas Gohr    public function _addCall($handler, $args, $pos) {
5431c0895aSAndreas Gohr        dbg_deprecated('addCall');
5531c0895aSAndreas Gohr        $this->addCall($handler, $args, $pos);
5631c0895aSAndreas Gohr    }
5731c0895aSAndreas Gohr
588b1b81beSAndreas Gohr    /**
598b1b81beSAndreas Gohr     * Similar to addCall, but adds a plugin call
608b1b81beSAndreas Gohr     *
618b1b81beSAndreas Gohr     * @param string $plugin name of the plugin
628b1b81beSAndreas Gohr     * @param mixed $args arguments for this call
638b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
648b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
658b1b81beSAndreas Gohr     * @param string $match matched syntax
668b1b81beSAndreas Gohr     */
67767b83f9SAndreas Gohr    public function addPluginCall($plugin, $args, $state, $pos, $match) {
6882d61635Spierre.spring        $call = array('plugin',array($plugin, $args, $state, $match), $pos);
698b1b81beSAndreas Gohr        $this->callWriter->writeCall($call);
7004ebd214Schris    }
7104ebd214Schris
728b1b81beSAndreas Gohr    /**
738b1b81beSAndreas Gohr     * Finishes handling
748b1b81beSAndreas Gohr     *
758b1b81beSAndreas Gohr     * Called from the parser. Calls finalise() on the call writer, closes open
768b1b81beSAndreas Gohr     * sections, rewrites blocks and adds document_start and document_end calls.
778b1b81beSAndreas Gohr     *
788b1b81beSAndreas Gohr     * @triggers PARSER_HANDLER_DONE
798b1b81beSAndreas Gohr     */
808b1b81beSAndreas Gohr    public function finalize(){
818b1b81beSAndreas Gohr        $this->callWriter->finalise();
82f4f02a0fSchris
83e1c10e4dSchris        if ( $this->status['section'] ) {
84e1c10e4dSchris            $last_call = end($this->calls);
85e1c10e4dSchris            array_push($this->calls,array('section_close',array(), $last_call[2]));
860cecf9d5Sandi        }
870cecf9d5Sandi
88b7c441b9SHarry Fuecks        if ( $this->rewriteBlocks ) {
895c2aad12SAndreas Gohr            $B = new Block();
900cecf9d5Sandi            $this->calls = $B->process($this->calls);
91b7c441b9SHarry Fuecks        }
92e0ad864eSchris
93cbb44eabSAndreas Gohr        Event::createAndTrigger('PARSER_HANDLER_DONE',$this);
940cecf9d5Sandi
950cecf9d5Sandi        array_unshift($this->calls,array('document_start',array(),0));
960cecf9d5Sandi        $last_call = end($this->calls);
970cecf9d5Sandi        array_push($this->calls,array('document_end',array(),$last_call[2]));
980cecf9d5Sandi    }
990cecf9d5Sandi
1009e491c01SAndreas Gohr    /**
1019e491c01SAndreas Gohr     * fetch the current call and advance the pointer to the next one
1029e491c01SAndreas Gohr     *
1038b1b81beSAndreas Gohr     * @fixme seems to be unused?
1049e491c01SAndreas Gohr     * @return bool|mixed
1059e491c01SAndreas Gohr     */
1068b1b81beSAndreas Gohr    public function fetch() {
1079e491c01SAndreas Gohr        $call = current($this->calls);
1089e491c01SAndreas Gohr        if($call !== false) {
1099e491c01SAndreas Gohr            next($this->calls); //advance the pointer
1109e491c01SAndreas Gohr            return $call;
1110cecf9d5Sandi        }
11244881bd0Shenning.noren        return false;
1130cecf9d5Sandi    }
114ee20e7d1Sandi
115ee20e7d1Sandi
116ee20e7d1Sandi    /**
117e2d88156SLarsDW223     * Internal function for parsing highlight options.
118e2d88156SLarsDW223     * $options is parsed for key value pairs separated by commas.
119e2d88156SLarsDW223     * A value might also be missing in which case the value will simple
120e2d88156SLarsDW223     * be set to true. Commas in strings are ignored, e.g. option="4,56"
121e2d88156SLarsDW223     * will work as expected and will only create one entry.
122e2d88156SLarsDW223     *
12354f741e8SAndreas Gohr     * @param string $options space separated list of key-value pairs,
124e2d88156SLarsDW223     *                        e.g. option1=123, option2="456"
125e2d88156SLarsDW223     * @return array|null     Array of key-value pairs $array['key'] = 'value';
126e2d88156SLarsDW223     *                        or null if no entries found
127e2d88156SLarsDW223     */
128e2d88156SLarsDW223    protected function parse_highlight_options($options) {
129e2d88156SLarsDW223        $result = array();
13054f741e8SAndreas Gohr        preg_match_all('/(\w+(?:="[^"]*"))|(\w+(?:=[^\s]*))|(\w+[^=\s\]])(?:\s*)/', $options, $matches, PREG_SET_ORDER);
131e2d88156SLarsDW223        foreach ($matches as $match) {
132e2d88156SLarsDW223            $equal_sign = strpos($match [0], '=');
133e2d88156SLarsDW223            if ($equal_sign === false) {
13454f741e8SAndreas Gohr                $key = trim($match[0]);
135e2d88156SLarsDW223                $result [$key] = 1;
136e2d88156SLarsDW223            } else {
137e2d88156SLarsDW223                $key = substr($match[0], 0, $equal_sign);
138e2d88156SLarsDW223                $value = substr($match[0], $equal_sign+1);
139e2d88156SLarsDW223                $value = trim($value, '"');
140e2d88156SLarsDW223                if (strlen($value) > 0) {
141e2d88156SLarsDW223                    $result [$key] = $value;
142e2d88156SLarsDW223                } else {
143e2d88156SLarsDW223                    $result [$key] = 1;
144e2d88156SLarsDW223                }
145e2d88156SLarsDW223            }
146e2d88156SLarsDW223        }
147e2d88156SLarsDW223
148e2d88156SLarsDW223        // Check for supported options
149e2d88156SLarsDW223        $result = array_intersect_key(
150e2d88156SLarsDW223            $result,
151e2d88156SLarsDW223            array_flip(array(
152e2d88156SLarsDW223                           'enable_line_numbers',
153e2d88156SLarsDW223                           'start_line_numbers_at',
154e2d88156SLarsDW223                           'highlight_lines_extra',
155e2d88156SLarsDW223                           'enable_keyword_links')
156e2d88156SLarsDW223            )
157e2d88156SLarsDW223        );
158e2d88156SLarsDW223
159e2d88156SLarsDW223        // Sanitize values
160e2d88156SLarsDW223        if(isset($result['enable_line_numbers'])) {
16154f741e8SAndreas Gohr            if($result['enable_line_numbers'] === 'false') {
16254f741e8SAndreas Gohr                $result['enable_line_numbers'] = false;
16354f741e8SAndreas Gohr            }
164e2d88156SLarsDW223            $result['enable_line_numbers'] = (bool) $result['enable_line_numbers'];
165e2d88156SLarsDW223        }
166e2d88156SLarsDW223        if(isset($result['highlight_lines_extra'])) {
167e2d88156SLarsDW223            $result['highlight_lines_extra'] = array_map('intval', explode(',', $result['highlight_lines_extra']));
168e2d88156SLarsDW223            $result['highlight_lines_extra'] = array_filter($result['highlight_lines_extra']);
169e2d88156SLarsDW223            $result['highlight_lines_extra'] = array_unique($result['highlight_lines_extra']);
170e2d88156SLarsDW223        }
171e2d88156SLarsDW223        if(isset($result['start_line_numbers_at'])) {
172e2d88156SLarsDW223            $result['start_line_numbers_at'] = (int) $result['start_line_numbers_at'];
173e2d88156SLarsDW223        }
174e2d88156SLarsDW223        if(isset($result['enable_keyword_links'])) {
17554f741e8SAndreas Gohr            if($result['enable_keyword_links'] === 'false') {
17654f741e8SAndreas Gohr                $result['enable_keyword_links'] = false;
17754f741e8SAndreas Gohr            }
17854f741e8SAndreas Gohr            $result['enable_keyword_links'] = (bool) $result['enable_keyword_links'];
179e2d88156SLarsDW223        }
180e2d88156SLarsDW223        if (count($result) == 0) {
181e2d88156SLarsDW223            return null;
182e2d88156SLarsDW223        }
183e2d88156SLarsDW223
184e2d88156SLarsDW223        return $result;
185e2d88156SLarsDW223    }
186e2d88156SLarsDW223
1878b1b81beSAndreas Gohr    /**
1888b1b81beSAndreas Gohr     * Simplifies handling for the formatting tags which all behave the same
1898b1b81beSAndreas Gohr     *
1908b1b81beSAndreas Gohr     * @param string $match matched syntax
1918b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
1928b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
1938b1b81beSAndreas Gohr     * @param string $name actual mode name
1948b1b81beSAndreas Gohr     */
1958b1b81beSAndreas Gohr    protected function nestingTag($match, $state, $pos, $name) {
1968b1b81beSAndreas Gohr        switch ( $state ) {
1978b1b81beSAndreas Gohr            case DOKU_LEXER_ENTER:
1988b1b81beSAndreas Gohr                $this->addCall($name.'_open', array(), $pos);
1998b1b81beSAndreas Gohr                break;
2008b1b81beSAndreas Gohr            case DOKU_LEXER_EXIT:
2018b1b81beSAndreas Gohr                $this->addCall($name.'_close', array(), $pos);
2028b1b81beSAndreas Gohr                break;
2038b1b81beSAndreas Gohr            case DOKU_LEXER_UNMATCHED:
2048b1b81beSAndreas Gohr                $this->addCall('cdata', array($match), $pos);
2058b1b81beSAndreas Gohr                break;
2068b1b81beSAndreas Gohr        }
2078b1b81beSAndreas Gohr    }
2088b1b81beSAndreas Gohr
2098b1b81beSAndreas Gohr
2108b1b81beSAndreas Gohr    /**
2118b1b81beSAndreas Gohr     * The following methods define the handlers for the different Syntax modes
2128b1b81beSAndreas Gohr     *
213be906b56SAndreas Gohr     * The handlers are called from dokuwiki\Parsing\Lexer\Lexer\invokeParser()
2148b1b81beSAndreas Gohr     *
2158b1b81beSAndreas Gohr     * @todo it might make sense to move these into their own class or merge them with the
2168b1b81beSAndreas Gohr     *       ParserMode classes some time.
2178b1b81beSAndreas Gohr     */
2188b1b81beSAndreas Gohr    // region mode handlers
2198b1b81beSAndreas Gohr
2208b1b81beSAndreas Gohr    /**
2218b1b81beSAndreas Gohr     * Special plugin handler
2228b1b81beSAndreas Gohr     *
2238b1b81beSAndreas Gohr     * This handler is called for all modes starting with 'plugin_'.
2248b1b81beSAndreas Gohr     * An additional parameter with the plugin name is passed. The plugin's handle()
2258b1b81beSAndreas Gohr     * method is called here
2268b1b81beSAndreas Gohr     *
2278b1b81beSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
2288b1b81beSAndreas Gohr     *
2298b1b81beSAndreas Gohr     * @param string $match matched syntax
2308b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
2318b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
2328b1b81beSAndreas Gohr     * @param string $pluginname name of the plugin
2338b1b81beSAndreas Gohr     * @return bool mode handled?
2348b1b81beSAndreas Gohr     */
2358b1b81beSAndreas Gohr    public function plugin($match, $state, $pos, $pluginname){
2368b1b81beSAndreas Gohr        $data = array($match);
237e1d9dcc8SAndreas Gohr        /** @var SyntaxPlugin $plugin */
2388b1b81beSAndreas Gohr        $plugin = plugin_load('syntax',$pluginname);
2398b1b81beSAndreas Gohr        if($plugin != null){
2408b1b81beSAndreas Gohr            $data = $plugin->handle($match, $state, $pos, $this);
2418b1b81beSAndreas Gohr        }
2428b1b81beSAndreas Gohr        if ($data !== false) {
2438b1b81beSAndreas Gohr            $this->addPluginCall($pluginname,$data,$state,$pos,$match);
2448b1b81beSAndreas Gohr        }
2458b1b81beSAndreas Gohr        return true;
2468b1b81beSAndreas Gohr    }
2478b1b81beSAndreas Gohr
2488b1b81beSAndreas Gohr    /**
2498b1b81beSAndreas Gohr     * @param string $match matched syntax
2508b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
2518b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
2528b1b81beSAndreas Gohr     * @return bool mode handled?
2538b1b81beSAndreas Gohr     */
2548b1b81beSAndreas Gohr    public function base($match, $state, $pos) {
2558b1b81beSAndreas Gohr        switch ( $state ) {
2568b1b81beSAndreas Gohr            case DOKU_LEXER_UNMATCHED:
2578b1b81beSAndreas Gohr                $this->addCall('cdata', array($match), $pos);
2588b1b81beSAndreas Gohr                return true;
2598b1b81beSAndreas Gohr            break;
2608b1b81beSAndreas Gohr        }
2618b1b81beSAndreas Gohr        return false;
2628b1b81beSAndreas Gohr    }
2638b1b81beSAndreas Gohr
2648b1b81beSAndreas Gohr    /**
2658b1b81beSAndreas Gohr     * @param string $match matched syntax
2668b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
2678b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
2688b1b81beSAndreas Gohr     * @return bool mode handled?
2698b1b81beSAndreas Gohr     */
2708b1b81beSAndreas Gohr    public function header($match, $state, $pos) {
2718b1b81beSAndreas Gohr        // get level and title
2728b1b81beSAndreas Gohr        $title = trim($match);
2738b1b81beSAndreas Gohr        $level = 7 - strspn($title,'=');
2748b1b81beSAndreas Gohr        if($level < 1) $level = 1;
2758b1b81beSAndreas Gohr        $title = trim($title,'=');
2768b1b81beSAndreas Gohr        $title = trim($title);
2778b1b81beSAndreas Gohr
2788b1b81beSAndreas Gohr        if ($this->status['section']) $this->addCall('section_close', array(), $pos);
2798b1b81beSAndreas Gohr
2808b1b81beSAndreas Gohr        $this->addCall('header', array($title, $level, $pos), $pos);
2818b1b81beSAndreas Gohr
2828b1b81beSAndreas Gohr        $this->addCall('section_open', array($level), $pos);
2838b1b81beSAndreas Gohr        $this->status['section'] = true;
2848b1b81beSAndreas Gohr        return true;
2858b1b81beSAndreas Gohr    }
2868b1b81beSAndreas Gohr
2878b1b81beSAndreas Gohr    /**
2888b1b81beSAndreas Gohr     * @param string $match matched syntax
2898b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
2908b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
2918b1b81beSAndreas Gohr     * @return bool mode handled?
2928b1b81beSAndreas Gohr     */
2938b1b81beSAndreas Gohr    public function notoc($match, $state, $pos) {
2948b1b81beSAndreas Gohr        $this->addCall('notoc', array(), $pos);
2958b1b81beSAndreas Gohr        return true;
2968b1b81beSAndreas Gohr    }
2978b1b81beSAndreas Gohr
2988b1b81beSAndreas Gohr    /**
2998b1b81beSAndreas Gohr     * @param string $match matched syntax
3008b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
3018b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
3028b1b81beSAndreas Gohr     * @return bool mode handled?
3038b1b81beSAndreas Gohr     */
3048b1b81beSAndreas Gohr    public function nocache($match, $state, $pos) {
3058b1b81beSAndreas Gohr        $this->addCall('nocache', array(), $pos);
3068b1b81beSAndreas Gohr        return true;
3078b1b81beSAndreas Gohr    }
3088b1b81beSAndreas Gohr
3098b1b81beSAndreas Gohr    /**
3108b1b81beSAndreas Gohr     * @param string $match matched syntax
3118b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
3128b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
3138b1b81beSAndreas Gohr     * @return bool mode handled?
3148b1b81beSAndreas Gohr     */
3158b1b81beSAndreas Gohr    public function linebreak($match, $state, $pos) {
3168b1b81beSAndreas Gohr        $this->addCall('linebreak', array(), $pos);
3178b1b81beSAndreas Gohr        return true;
3188b1b81beSAndreas Gohr    }
3198b1b81beSAndreas Gohr
3208b1b81beSAndreas Gohr    /**
3218b1b81beSAndreas Gohr     * @param string $match matched syntax
3228b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
3238b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
3248b1b81beSAndreas Gohr     * @return bool mode handled?
3258b1b81beSAndreas Gohr     */
3268b1b81beSAndreas Gohr    public function eol($match, $state, $pos) {
3278b1b81beSAndreas Gohr        $this->addCall('eol', array(), $pos);
3288b1b81beSAndreas Gohr        return true;
3298b1b81beSAndreas Gohr    }
3308b1b81beSAndreas Gohr
3318b1b81beSAndreas Gohr    /**
3328b1b81beSAndreas Gohr     * @param string $match matched syntax
3338b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
3348b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
3358b1b81beSAndreas Gohr     * @return bool mode handled?
3368b1b81beSAndreas Gohr     */
3378b1b81beSAndreas Gohr    public function hr($match, $state, $pos) {
3388b1b81beSAndreas Gohr        $this->addCall('hr', array(), $pos);
3398b1b81beSAndreas Gohr        return true;
3408b1b81beSAndreas Gohr    }
3418b1b81beSAndreas Gohr
3428b1b81beSAndreas Gohr    /**
3438b1b81beSAndreas Gohr     * @param string $match matched syntax
3448b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
3458b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
3468b1b81beSAndreas Gohr     * @return bool mode handled?
3478b1b81beSAndreas Gohr     */
3488b1b81beSAndreas Gohr    public function strong($match, $state, $pos) {
3498b1b81beSAndreas Gohr        $this->nestingTag($match, $state, $pos, 'strong');
3508b1b81beSAndreas Gohr        return true;
3518b1b81beSAndreas Gohr    }
3528b1b81beSAndreas Gohr
3538b1b81beSAndreas Gohr    /**
3548b1b81beSAndreas Gohr     * @param string $match matched syntax
3558b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
3568b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
3578b1b81beSAndreas Gohr     * @return bool mode handled?
3588b1b81beSAndreas Gohr     */
3598b1b81beSAndreas Gohr    public function emphasis($match, $state, $pos) {
3608b1b81beSAndreas Gohr        $this->nestingTag($match, $state, $pos, 'emphasis');
3618b1b81beSAndreas Gohr        return true;
3628b1b81beSAndreas Gohr    }
3638b1b81beSAndreas Gohr
3648b1b81beSAndreas Gohr    /**
3658b1b81beSAndreas Gohr     * @param string $match matched syntax
3668b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
3678b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
3688b1b81beSAndreas Gohr     * @return bool mode handled?
3698b1b81beSAndreas Gohr     */
3708b1b81beSAndreas Gohr    public function underline($match, $state, $pos) {
3718b1b81beSAndreas Gohr        $this->nestingTag($match, $state, $pos, 'underline');
3728b1b81beSAndreas Gohr        return true;
3738b1b81beSAndreas Gohr    }
3748b1b81beSAndreas Gohr
3758b1b81beSAndreas Gohr    /**
3768b1b81beSAndreas Gohr     * @param string $match matched syntax
3778b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
3788b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
3798b1b81beSAndreas Gohr     * @return bool mode handled?
3808b1b81beSAndreas Gohr     */
3818b1b81beSAndreas Gohr    public function monospace($match, $state, $pos) {
3828b1b81beSAndreas Gohr        $this->nestingTag($match, $state, $pos, 'monospace');
3838b1b81beSAndreas Gohr        return true;
3848b1b81beSAndreas Gohr    }
3858b1b81beSAndreas Gohr
3868b1b81beSAndreas Gohr    /**
3878b1b81beSAndreas Gohr     * @param string $match matched syntax
3888b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
3898b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
3908b1b81beSAndreas Gohr     * @return bool mode handled?
3918b1b81beSAndreas Gohr     */
3928b1b81beSAndreas Gohr    public function subscript($match, $state, $pos) {
3938b1b81beSAndreas Gohr        $this->nestingTag($match, $state, $pos, 'subscript');
3948b1b81beSAndreas Gohr        return true;
3958b1b81beSAndreas Gohr    }
3968b1b81beSAndreas Gohr
3978b1b81beSAndreas Gohr    /**
3988b1b81beSAndreas Gohr     * @param string $match matched syntax
3998b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
4008b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
4018b1b81beSAndreas Gohr     * @return bool mode handled?
4028b1b81beSAndreas Gohr     */
4038b1b81beSAndreas Gohr    public function superscript($match, $state, $pos) {
4048b1b81beSAndreas Gohr        $this->nestingTag($match, $state, $pos, 'superscript');
4058b1b81beSAndreas Gohr        return true;
4068b1b81beSAndreas Gohr    }
4078b1b81beSAndreas Gohr
4088b1b81beSAndreas Gohr    /**
4098b1b81beSAndreas Gohr     * @param string $match matched syntax
4108b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
4118b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
4128b1b81beSAndreas Gohr     * @return bool mode handled?
4138b1b81beSAndreas Gohr     */
4148b1b81beSAndreas Gohr    public function deleted($match, $state, $pos) {
4158b1b81beSAndreas Gohr        $this->nestingTag($match, $state, $pos, 'deleted');
4168b1b81beSAndreas Gohr        return true;
4178b1b81beSAndreas Gohr    }
4188b1b81beSAndreas Gohr
4198b1b81beSAndreas Gohr    /**
4208b1b81beSAndreas Gohr     * @param string $match matched syntax
4218b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
4228b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
4238b1b81beSAndreas Gohr     * @return bool mode handled?
4248b1b81beSAndreas Gohr     */
4258b1b81beSAndreas Gohr    public function footnote($match, $state, $pos) {
4268b1b81beSAndreas Gohr        if (!isset($this->_footnote)) $this->_footnote = false;
4278b1b81beSAndreas Gohr
4288b1b81beSAndreas Gohr        switch ( $state ) {
4298b1b81beSAndreas Gohr            case DOKU_LEXER_ENTER:
4308b1b81beSAndreas Gohr                // footnotes can not be nested - however due to limitations in lexer it can't be prevented
4318b1b81beSAndreas Gohr                // we will still enter a new footnote mode, we just do nothing
4328b1b81beSAndreas Gohr                if ($this->_footnote) {
4338b1b81beSAndreas Gohr                    $this->addCall('cdata', array($match), $pos);
4348b1b81beSAndreas Gohr                    break;
4358b1b81beSAndreas Gohr                }
4368b1b81beSAndreas Gohr                $this->_footnote = true;
4378b1b81beSAndreas Gohr
4388b1b81beSAndreas Gohr                $this->callWriter = new Nest($this->callWriter, 'footnote_close');
4398b1b81beSAndreas Gohr                $this->addCall('footnote_open', array(), $pos);
4408b1b81beSAndreas Gohr            break;
4418b1b81beSAndreas Gohr            case DOKU_LEXER_EXIT:
4428b1b81beSAndreas Gohr                // check whether we have already exitted the footnote mode, can happen if the modes were nested
4438b1b81beSAndreas Gohr                if (!$this->_footnote) {
4448b1b81beSAndreas Gohr                    $this->addCall('cdata', array($match), $pos);
4458b1b81beSAndreas Gohr                    break;
4468b1b81beSAndreas Gohr                }
4478b1b81beSAndreas Gohr
4488b1b81beSAndreas Gohr                $this->_footnote = false;
4498b1b81beSAndreas Gohr                $this->addCall('footnote_close', array(), $pos);
4508b1b81beSAndreas Gohr
4518b1b81beSAndreas Gohr                /** @var Nest $reWriter */
4528b1b81beSAndreas Gohr                $reWriter = $this->callWriter;
4538b1b81beSAndreas Gohr                $this->callWriter = $reWriter->process();
4548b1b81beSAndreas Gohr            break;
4558b1b81beSAndreas Gohr            case DOKU_LEXER_UNMATCHED:
4568b1b81beSAndreas Gohr                $this->addCall('cdata', array($match), $pos);
4578b1b81beSAndreas Gohr            break;
4588b1b81beSAndreas Gohr        }
4598b1b81beSAndreas Gohr        return true;
4608b1b81beSAndreas Gohr    }
4618b1b81beSAndreas Gohr
4628b1b81beSAndreas Gohr    /**
4638b1b81beSAndreas Gohr     * @param string $match matched syntax
4648b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
4658b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
4668b1b81beSAndreas Gohr     * @return bool mode handled?
4678b1b81beSAndreas Gohr     */
4688b1b81beSAndreas Gohr    public function listblock($match, $state, $pos) {
4698b1b81beSAndreas Gohr        switch ( $state ) {
4708b1b81beSAndreas Gohr            case DOKU_LEXER_ENTER:
4718b1b81beSAndreas Gohr                $this->callWriter = new Lists($this->callWriter);
4728b1b81beSAndreas Gohr                $this->addCall('list_open', array($match), $pos);
4738b1b81beSAndreas Gohr            break;
4748b1b81beSAndreas Gohr            case DOKU_LEXER_EXIT:
4758b1b81beSAndreas Gohr                $this->addCall('list_close', array(), $pos);
4768b1b81beSAndreas Gohr                /** @var Lists $reWriter */
4778b1b81beSAndreas Gohr                $reWriter = $this->callWriter;
4788b1b81beSAndreas Gohr                $this->callWriter = $reWriter->process();
4798b1b81beSAndreas Gohr            break;
4808b1b81beSAndreas Gohr            case DOKU_LEXER_MATCHED:
4818b1b81beSAndreas Gohr                $this->addCall('list_item', array($match), $pos);
4828b1b81beSAndreas Gohr            break;
4838b1b81beSAndreas Gohr            case DOKU_LEXER_UNMATCHED:
4848b1b81beSAndreas Gohr                $this->addCall('cdata', array($match), $pos);
4858b1b81beSAndreas Gohr            break;
4868b1b81beSAndreas Gohr        }
4878b1b81beSAndreas Gohr        return true;
4888b1b81beSAndreas Gohr    }
4898b1b81beSAndreas Gohr
4908b1b81beSAndreas Gohr    /**
4918b1b81beSAndreas Gohr     * @param string $match matched syntax
4928b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
4938b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
4948b1b81beSAndreas Gohr     * @return bool mode handled?
4958b1b81beSAndreas Gohr     */
4968b1b81beSAndreas Gohr    public function unformatted($match, $state, $pos) {
4978b1b81beSAndreas Gohr        if ( $state == DOKU_LEXER_UNMATCHED ) {
4988b1b81beSAndreas Gohr            $this->addCall('unformatted', array($match), $pos);
4998b1b81beSAndreas Gohr        }
5008b1b81beSAndreas Gohr        return true;
5018b1b81beSAndreas Gohr    }
5028b1b81beSAndreas Gohr
5038b1b81beSAndreas Gohr    /**
5048b1b81beSAndreas Gohr     * @param string $match matched syntax
5058b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
5068b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
5078b1b81beSAndreas Gohr     * @return bool mode handled?
5088b1b81beSAndreas Gohr     */
5098b1b81beSAndreas Gohr    public function php($match, $state, $pos) {
5108b1b81beSAndreas Gohr        if ( $state == DOKU_LEXER_UNMATCHED ) {
5118b1b81beSAndreas Gohr            $this->addCall('php', array($match), $pos);
5128b1b81beSAndreas Gohr        }
5138b1b81beSAndreas Gohr        return true;
5148b1b81beSAndreas Gohr    }
5158b1b81beSAndreas Gohr
5168b1b81beSAndreas Gohr    /**
5178b1b81beSAndreas Gohr     * @param string $match matched syntax
5188b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
5198b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
5208b1b81beSAndreas Gohr     * @return bool mode handled?
5218b1b81beSAndreas Gohr     */
5228b1b81beSAndreas Gohr    public function phpblock($match, $state, $pos) {
5238b1b81beSAndreas Gohr        if ( $state == DOKU_LEXER_UNMATCHED ) {
5248b1b81beSAndreas Gohr            $this->addCall('phpblock', array($match), $pos);
5258b1b81beSAndreas Gohr        }
5268b1b81beSAndreas Gohr        return true;
5278b1b81beSAndreas Gohr    }
5288b1b81beSAndreas Gohr
5298b1b81beSAndreas Gohr    /**
5308b1b81beSAndreas Gohr     * @param string $match matched syntax
5318b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
5328b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
5338b1b81beSAndreas Gohr     * @return bool mode handled?
5348b1b81beSAndreas Gohr     */
5358b1b81beSAndreas Gohr    public function html($match, $state, $pos) {
5368b1b81beSAndreas Gohr        if ( $state == DOKU_LEXER_UNMATCHED ) {
5378b1b81beSAndreas Gohr            $this->addCall('html', array($match), $pos);
5388b1b81beSAndreas Gohr        }
5398b1b81beSAndreas Gohr        return true;
5408b1b81beSAndreas Gohr    }
5418b1b81beSAndreas Gohr
5428b1b81beSAndreas Gohr    /**
5438b1b81beSAndreas Gohr     * @param string $match matched syntax
5448b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
5458b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
5468b1b81beSAndreas Gohr     * @return bool mode handled?
5478b1b81beSAndreas Gohr     */
5488b1b81beSAndreas Gohr    public function htmlblock($match, $state, $pos) {
5498b1b81beSAndreas Gohr        if ( $state == DOKU_LEXER_UNMATCHED ) {
5508b1b81beSAndreas Gohr            $this->addCall('htmlblock', array($match), $pos);
5518b1b81beSAndreas Gohr        }
5528b1b81beSAndreas Gohr        return true;
5538b1b81beSAndreas Gohr    }
5548b1b81beSAndreas Gohr
5558b1b81beSAndreas Gohr    /**
5568b1b81beSAndreas Gohr     * @param string $match matched syntax
5578b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
5588b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
5598b1b81beSAndreas Gohr     * @return bool mode handled?
5608b1b81beSAndreas Gohr     */
5618b1b81beSAndreas Gohr    public function preformatted($match, $state, $pos) {
5628b1b81beSAndreas Gohr        switch ( $state ) {
5638b1b81beSAndreas Gohr            case DOKU_LEXER_ENTER:
5648b1b81beSAndreas Gohr                $this->callWriter = new Preformatted($this->callWriter);
5658b1b81beSAndreas Gohr                $this->addCall('preformatted_start', array(), $pos);
5668b1b81beSAndreas Gohr            break;
5678b1b81beSAndreas Gohr            case DOKU_LEXER_EXIT:
5688b1b81beSAndreas Gohr                $this->addCall('preformatted_end', array(), $pos);
5698b1b81beSAndreas Gohr                /** @var Preformatted $reWriter */
5708b1b81beSAndreas Gohr                $reWriter = $this->callWriter;
5718b1b81beSAndreas Gohr                $this->callWriter = $reWriter->process();
5728b1b81beSAndreas Gohr            break;
5738b1b81beSAndreas Gohr            case DOKU_LEXER_MATCHED:
5748b1b81beSAndreas Gohr                $this->addCall('preformatted_newline', array(), $pos);
5758b1b81beSAndreas Gohr            break;
5768b1b81beSAndreas Gohr            case DOKU_LEXER_UNMATCHED:
5778b1b81beSAndreas Gohr                $this->addCall('preformatted_content', array($match), $pos);
5788b1b81beSAndreas Gohr            break;
5798b1b81beSAndreas Gohr        }
5808b1b81beSAndreas Gohr
5818b1b81beSAndreas Gohr        return true;
5828b1b81beSAndreas Gohr    }
5838b1b81beSAndreas Gohr
5848b1b81beSAndreas Gohr    /**
5858b1b81beSAndreas Gohr     * @param string $match matched syntax
5868b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
5878b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
5888b1b81beSAndreas Gohr     * @return bool mode handled?
5898b1b81beSAndreas Gohr     */
5908b1b81beSAndreas Gohr    public function quote($match, $state, $pos) {
5918b1b81beSAndreas Gohr
5928b1b81beSAndreas Gohr        switch ( $state ) {
5938b1b81beSAndreas Gohr
5948b1b81beSAndreas Gohr            case DOKU_LEXER_ENTER:
5958b1b81beSAndreas Gohr                $this->callWriter = new Quote($this->callWriter);
5968b1b81beSAndreas Gohr                $this->addCall('quote_start', array($match), $pos);
5978b1b81beSAndreas Gohr            break;
5988b1b81beSAndreas Gohr
5998b1b81beSAndreas Gohr            case DOKU_LEXER_EXIT:
6008b1b81beSAndreas Gohr                $this->addCall('quote_end', array(), $pos);
6018b1b81beSAndreas Gohr                /** @var Lists $reWriter */
6028b1b81beSAndreas Gohr                $reWriter = $this->callWriter;
6038b1b81beSAndreas Gohr                $this->callWriter = $reWriter->process();
6048b1b81beSAndreas Gohr            break;
6058b1b81beSAndreas Gohr
6068b1b81beSAndreas Gohr            case DOKU_LEXER_MATCHED:
6078b1b81beSAndreas Gohr                $this->addCall('quote_newline', array($match), $pos);
6088b1b81beSAndreas Gohr            break;
6098b1b81beSAndreas Gohr
6108b1b81beSAndreas Gohr            case DOKU_LEXER_UNMATCHED:
6118b1b81beSAndreas Gohr                $this->addCall('cdata', array($match), $pos);
6128b1b81beSAndreas Gohr            break;
6138b1b81beSAndreas Gohr
6148b1b81beSAndreas Gohr        }
6158b1b81beSAndreas Gohr
6168b1b81beSAndreas Gohr        return true;
6178b1b81beSAndreas Gohr    }
6188b1b81beSAndreas Gohr
6198b1b81beSAndreas Gohr    /**
6208b1b81beSAndreas Gohr     * @param string $match matched syntax
6218b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
6228b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
6238b1b81beSAndreas Gohr     * @return bool mode handled?
6248b1b81beSAndreas Gohr     */
6258b1b81beSAndreas Gohr    public function file($match, $state, $pos) {
6263d491f75SAndreas Gohr        return $this->code($match, $state, $pos, 'file');
6273d491f75SAndreas Gohr    }
6283d491f75SAndreas Gohr
6298b1b81beSAndreas Gohr    /**
6308b1b81beSAndreas Gohr     * @param string $match matched syntax
6318b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
6328b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
6338b1b81beSAndreas Gohr     * @param string $type either 'code' or 'file'
6348b1b81beSAndreas Gohr     * @return bool mode handled?
6358b1b81beSAndreas Gohr     */
6368b1b81beSAndreas Gohr    public function code($match, $state, $pos, $type='code') {
6373d491f75SAndreas Gohr        if ( $state == DOKU_LEXER_UNMATCHED ) {
6384b7f9e70STom N Harris            $matches = explode('>',$match,2);
639e2d88156SLarsDW223            // Cut out variable options enclosed in []
640e2d88156SLarsDW223            preg_match('/\[.*\]/', $matches[0], $options);
641e2d88156SLarsDW223            if (!empty($options[0])) {
642e2d88156SLarsDW223                $matches[0] = str_replace($options[0], '', $matches[0]);
643e2d88156SLarsDW223            }
6440139312bSAdrian Lang            $param = preg_split('/\s+/', $matches[0], 2, PREG_SPLIT_NO_EMPTY);
6450139312bSAdrian Lang            while(count($param) < 2) array_push($param, null);
6460139312bSAdrian Lang            // We shortcut html here.
6470139312bSAdrian Lang            if ($param[0] == 'html') $param[0] = 'html4strict';
6480139312bSAdrian Lang            if ($param[0] == '-') $param[0] = null;
6490139312bSAdrian Lang            array_unshift($param, $matches[1]);
650e2d88156SLarsDW223            if (!empty($options[0])) {
651e2d88156SLarsDW223                $param [] = $this->parse_highlight_options ($options[0]);
652e2d88156SLarsDW223            }
6538b1b81beSAndreas Gohr            $this->addCall($type, $param, $pos);
6540cecf9d5Sandi        }
65544881bd0Shenning.noren        return true;
6560cecf9d5Sandi    }
6570cecf9d5Sandi
6588b1b81beSAndreas Gohr    /**
6598b1b81beSAndreas Gohr     * @param string $match matched syntax
6608b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
6618b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
6628b1b81beSAndreas Gohr     * @return bool mode handled?
6638b1b81beSAndreas Gohr     */
6648b1b81beSAndreas Gohr    public function acronym($match, $state, $pos) {
6658b1b81beSAndreas Gohr        $this->addCall('acronym', array($match), $pos);
66644881bd0Shenning.noren        return true;
6670cecf9d5Sandi    }
6680cecf9d5Sandi
6698b1b81beSAndreas Gohr    /**
6708b1b81beSAndreas Gohr     * @param string $match matched syntax
6718b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
6728b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
6738b1b81beSAndreas Gohr     * @return bool mode handled?
6748b1b81beSAndreas Gohr     */
6758b1b81beSAndreas Gohr    public function smiley($match, $state, $pos) {
6768b1b81beSAndreas Gohr        $this->addCall('smiley', array($match), $pos);
67744881bd0Shenning.noren        return true;
6780cecf9d5Sandi    }
6790cecf9d5Sandi
6808b1b81beSAndreas Gohr    /**
6818b1b81beSAndreas Gohr     * @param string $match matched syntax
6828b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
6838b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
6848b1b81beSAndreas Gohr     * @return bool mode handled?
6858b1b81beSAndreas Gohr     */
6868b1b81beSAndreas Gohr    public function wordblock($match, $state, $pos) {
6878b1b81beSAndreas Gohr        $this->addCall('wordblock', array($match), $pos);
68844881bd0Shenning.noren        return true;
6890cecf9d5Sandi    }
6900cecf9d5Sandi
6918b1b81beSAndreas Gohr    /**
6928b1b81beSAndreas Gohr     * @param string $match matched syntax
6938b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
6948b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
6958b1b81beSAndreas Gohr     * @return bool mode handled?
6968b1b81beSAndreas Gohr     */
6978b1b81beSAndreas Gohr    public function entity($match, $state, $pos) {
6988b1b81beSAndreas Gohr        $this->addCall('entity', array($match), $pos);
69944881bd0Shenning.noren        return true;
7000cecf9d5Sandi    }
7010cecf9d5Sandi
7028b1b81beSAndreas Gohr    /**
7038b1b81beSAndreas Gohr     * @param string $match matched syntax
7048b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
7058b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
7068b1b81beSAndreas Gohr     * @return bool mode handled?
7078b1b81beSAndreas Gohr     */
7088b1b81beSAndreas Gohr    public function multiplyentity($match, $state, $pos) {
7090cecf9d5Sandi        preg_match_all('/\d+/',$match,$matches);
7108b1b81beSAndreas Gohr        $this->addCall('multiplyentity', array($matches[0][0], $matches[0][1]), $pos);
71144881bd0Shenning.noren        return true;
7120cecf9d5Sandi    }
7130cecf9d5Sandi
7148b1b81beSAndreas Gohr    /**
7158b1b81beSAndreas Gohr     * @param string $match matched syntax
7168b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
7178b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
7188b1b81beSAndreas Gohr     * @return bool mode handled?
7198b1b81beSAndreas Gohr     */
7208b1b81beSAndreas Gohr    public function singlequoteopening($match, $state, $pos) {
7218b1b81beSAndreas Gohr        $this->addCall('singlequoteopening', array(), $pos);
72244881bd0Shenning.noren        return true;
7230cecf9d5Sandi    }
7240cecf9d5Sandi
7258b1b81beSAndreas Gohr    /**
7268b1b81beSAndreas Gohr     * @param string $match matched syntax
7278b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
7288b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
7298b1b81beSAndreas Gohr     * @return bool mode handled?
7308b1b81beSAndreas Gohr     */
7318b1b81beSAndreas Gohr    public function singlequoteclosing($match, $state, $pos) {
7328b1b81beSAndreas Gohr        $this->addCall('singlequoteclosing', array(), $pos);
73344881bd0Shenning.noren        return true;
7340cecf9d5Sandi    }
7350cecf9d5Sandi
7368b1b81beSAndreas Gohr    /**
7378b1b81beSAndreas Gohr     * @param string $match matched syntax
7388b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
7398b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
7408b1b81beSAndreas Gohr     * @return bool mode handled?
7418b1b81beSAndreas Gohr     */
7428b1b81beSAndreas Gohr    public function apostrophe($match, $state, $pos) {
7438b1b81beSAndreas Gohr        $this->addCall('apostrophe', array(), $pos);
74457d757d1SAndreas Gohr        return true;
74557d757d1SAndreas Gohr    }
74657d757d1SAndreas Gohr
7478b1b81beSAndreas Gohr    /**
7488b1b81beSAndreas Gohr     * @param string $match matched syntax
7498b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
7508b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
7518b1b81beSAndreas Gohr     * @return bool mode handled?
7528b1b81beSAndreas Gohr     */
7538b1b81beSAndreas Gohr    public function doublequoteopening($match, $state, $pos) {
7548b1b81beSAndreas Gohr        $this->addCall('doublequoteopening', array(), $pos);
755e950d12fSChristopher Smith        $this->status['doublequote']++;
75644881bd0Shenning.noren        return true;
7570cecf9d5Sandi    }
7580cecf9d5Sandi
7598b1b81beSAndreas Gohr    /**
7608b1b81beSAndreas Gohr     * @param string $match matched syntax
7618b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
7628b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
7638b1b81beSAndreas Gohr     * @return bool mode handled?
7648b1b81beSAndreas Gohr     */
7658b1b81beSAndreas Gohr    public function doublequoteclosing($match, $state, $pos) {
766e950d12fSChristopher Smith        if ($this->status['doublequote'] <= 0) {
767e950d12fSChristopher Smith            $this->doublequoteopening($match, $state, $pos);
768e950d12fSChristopher Smith        } else {
7698b1b81beSAndreas Gohr            $this->addCall('doublequoteclosing', array(), $pos);
770e950d12fSChristopher Smith            $this->status['doublequote'] = max(0, --$this->status['doublequote']);
771e950d12fSChristopher Smith        }
77244881bd0Shenning.noren        return true;
7730cecf9d5Sandi    }
7740cecf9d5Sandi
7758b1b81beSAndreas Gohr    /**
7768b1b81beSAndreas Gohr     * @param string $match matched syntax
7778b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
7788b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
7798b1b81beSAndreas Gohr     * @return bool mode handled?
7808b1b81beSAndreas Gohr     */
7818b1b81beSAndreas Gohr    public function camelcaselink($match, $state, $pos) {
7828b1b81beSAndreas Gohr        $this->addCall('camelcaselink', array($match), $pos);
78344881bd0Shenning.noren        return true;
7840cecf9d5Sandi    }
7850cecf9d5Sandi
7868b1b81beSAndreas Gohr    /**
7878b1b81beSAndreas Gohr     * @param string $match matched syntax
7888b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
7898b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
7908b1b81beSAndreas Gohr     * @return bool mode handled?
7910cecf9d5Sandi     */
7928b1b81beSAndreas Gohr    public function internallink($match, $state, $pos) {
7930cecf9d5Sandi        // Strip the opening and closing markup
7940cecf9d5Sandi        $link = preg_replace(array('/^\[\[/','/\]\]$/u'),'',$match);
7950cecf9d5Sandi
7960cecf9d5Sandi        // Split title from URL
7974b7f9e70STom N Harris        $link = explode('|',$link,2);
7980cecf9d5Sandi        if ( !isset($link[1]) ) {
7990ea51e63SMatt Perry            $link[1] = null;
8000cecf9d5Sandi        } else if ( preg_match('/^\{\{[^\}]+\}\}$/',$link[1]) ) {
8015578eb8fSandi            // If the title is an image, convert it to an array containing the image details
802b625487dSandi            $link[1] = Doku_Handler_Parse_Media($link[1]);
8030cecf9d5Sandi        }
8040b7c14c2Sandi        $link[0] = trim($link[0]);
8050cecf9d5Sandi
8060e1c636eSandi        //decide which kind of link it is
8070e1c636eSandi
8086efc45a2SDmitry Katsubo        if ( link_isinterwiki($link[0]) ) {
8090e1c636eSandi            // Interwiki
8104b7f9e70STom N Harris            $interwiki = explode('>',$link[0],2);
8118b1b81beSAndreas Gohr            $this->addCall(
8120cecf9d5Sandi                'interwikilink',
8130cecf9d5Sandi                array($link[0],$link[1],strtolower($interwiki[0]),$interwiki[1]),
8140cecf9d5Sandi                $pos
8150cecf9d5Sandi                );
81615f1b77cSAndreas Gohr        }elseif ( preg_match('/^\\\\\\\\[^\\\\]+?\\\\/u',$link[0]) ) {
8170e1c636eSandi            // Windows Share
8188b1b81beSAndreas Gohr            $this->addCall(
8190cecf9d5Sandi                'windowssharelink',
8200cecf9d5Sandi                array($link[0],$link[1]),
8210cecf9d5Sandi                $pos
8220cecf9d5Sandi                );
8234468cb4cSAndreas Gohr        }elseif ( preg_match('#^([a-z0-9\-\.+]+?)://#i',$link[0]) ) {
8240e1c636eSandi            // external link (accepts all protocols)
8258b1b81beSAndreas Gohr            $this->addCall(
8260cecf9d5Sandi                    'externallink',
8270cecf9d5Sandi                    array($link[0],$link[1]),
8280cecf9d5Sandi                    $pos
8290cecf9d5Sandi                    );
8300a1d30bfSchris        }elseif ( preg_match('<'.PREG_PATTERN_VALID_EMAIL.'>',$link[0]) ) {
8310a1d30bfSchris            // E-Mail (pattern above is defined in inc/mail.php)
8328b1b81beSAndreas Gohr            $this->addCall(
833a6755281Sandi                'emaillink',
834a6755281Sandi                array($link[0],$link[1]),
835a6755281Sandi                $pos
836a6755281Sandi                );
8370b7c14c2Sandi        }elseif ( preg_match('!^#.+!',$link[0]) ){
8380b7c14c2Sandi            // local link
8398b1b81beSAndreas Gohr            $this->addCall(
8400b7c14c2Sandi                'locallink',
8410b7c14c2Sandi                array(substr($link[0],1),$link[1]),
8420b7c14c2Sandi                $pos
8430b7c14c2Sandi                );
8440e1c636eSandi        }else{
8450e1c636eSandi            // internal link
8468b1b81beSAndreas Gohr            $this->addCall(
8470e1c636eSandi                'internallink',
8480e1c636eSandi                array($link[0],$link[1]),
8490e1c636eSandi                $pos
8500e1c636eSandi                );
8510cecf9d5Sandi        }
8520e1c636eSandi
85344881bd0Shenning.noren        return true;
8540cecf9d5Sandi    }
8550cecf9d5Sandi
8568b1b81beSAndreas Gohr    /**
8578b1b81beSAndreas Gohr     * @param string $match matched syntax
8588b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
8598b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
8608b1b81beSAndreas Gohr     * @return bool mode handled?
8618b1b81beSAndreas Gohr     */
8628b1b81beSAndreas Gohr    public function filelink($match, $state, $pos) {
8638b1b81beSAndreas Gohr        $this->addCall('filelink', array($match, null), $pos);
86444881bd0Shenning.noren        return true;
8650cecf9d5Sandi    }
8660cecf9d5Sandi
8678b1b81beSAndreas Gohr    /**
8688b1b81beSAndreas Gohr     * @param string $match matched syntax
8698b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
8708b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
8718b1b81beSAndreas Gohr     * @return bool mode handled?
8728b1b81beSAndreas Gohr     */
8738b1b81beSAndreas Gohr    public function windowssharelink($match, $state, $pos) {
8748b1b81beSAndreas Gohr        $this->addCall('windowssharelink', array($match, null), $pos);
87544881bd0Shenning.noren        return true;
8760cecf9d5Sandi    }
8770cecf9d5Sandi
8788b1b81beSAndreas Gohr    /**
8798b1b81beSAndreas Gohr     * @param string $match matched syntax
8808b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
8818b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
8828b1b81beSAndreas Gohr     * @return bool mode handled?
8838b1b81beSAndreas Gohr     */
8848b1b81beSAndreas Gohr    public function media($match, $state, $pos) {
8850cecf9d5Sandi        $p = Doku_Handler_Parse_Media($match);
8860cecf9d5Sandi
8878b1b81beSAndreas Gohr        $this->addCall(
8880cecf9d5Sandi            $p['type'],
889dc673a5bSjoe.lapp            array($p['src'], $p['title'], $p['align'], $p['width'],
8902a4a042dSSam                   $p['height'], $p['cache'], $p['linking'], $p["videoAtts"]),
8910cecf9d5Sandi            $pos
8920cecf9d5Sandi           );
89344881bd0Shenning.noren        return true;
8940cecf9d5Sandi    }
8950cecf9d5Sandi
8968b1b81beSAndreas Gohr    /**
8978b1b81beSAndreas Gohr     * @param string $match matched syntax
8988b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
8998b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
9008b1b81beSAndreas Gohr     * @return bool mode handled?
9018b1b81beSAndreas Gohr     */
9028b1b81beSAndreas Gohr    public function rss($match, $state, $pos) {
903b625487dSandi        $link = preg_replace(array('/^\{\{rss>/','/\}\}$/'),'',$match);
9043db95becSAndreas Gohr
9053db95becSAndreas Gohr        // get params
9063db95becSAndreas Gohr        list($link,$params) = explode(' ',$link,2);
9073db95becSAndreas Gohr
9083db95becSAndreas Gohr        $p = array();
9093db95becSAndreas Gohr        if(preg_match('/\b(\d+)\b/',$params,$match)){
9103db95becSAndreas Gohr            $p['max'] = $match[1];
9113db95becSAndreas Gohr        }else{
9123db95becSAndreas Gohr            $p['max'] = 8;
9133db95becSAndreas Gohr        }
9143db95becSAndreas Gohr        $p['reverse'] = (preg_match('/rev/',$params));
9153db95becSAndreas Gohr        $p['author']  = (preg_match('/\b(by|author)/',$params));
9163db95becSAndreas Gohr        $p['date']    = (preg_match('/\b(date)/',$params));
9173db95becSAndreas Gohr        $p['details'] = (preg_match('/\b(desc|detail)/',$params));
91838c6f603SRobin H. Johnson        $p['nosort']  = (preg_match('/\b(nosort)\b/',$params));
9193db95becSAndreas Gohr
9200a69dff7Schris        if (preg_match('/\b(\d+)([dhm])\b/',$params,$match)) {
9210a69dff7Schris            $period = array('d' => 86400, 'h' => 3600, 'm' => 60);
9220a69dff7Schris            $p['refresh'] = max(600,$match[1]*$period[$match[2]]);  // n * period in seconds, minimum 10 minutes
9230a69dff7Schris        } else {
9240a69dff7Schris            $p['refresh'] = 14400;   // default to 4 hours
9250a69dff7Schris        }
9260a69dff7Schris
9278b1b81beSAndreas Gohr        $this->addCall('rss', array($link, $p), $pos);
92844881bd0Shenning.noren        return true;
929b625487dSandi    }
930b625487dSandi
9318b1b81beSAndreas Gohr    /**
9328b1b81beSAndreas Gohr     * @param string $match matched syntax
9338b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
9348b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
9358b1b81beSAndreas Gohr     * @return bool mode handled?
9368b1b81beSAndreas Gohr     */
9378b1b81beSAndreas Gohr    public function externallink($match, $state, $pos) {
938da9f31c5SAndreas Gohr        $url   = $match;
939da9f31c5SAndreas Gohr        $title = null;
9400cecf9d5Sandi
941da9f31c5SAndreas Gohr        // add protocol on simple short URLs
942da9f31c5SAndreas Gohr        if(substr($url,0,3) == 'ftp' && (substr($url,0,6) != 'ftp://')){
943da9f31c5SAndreas Gohr            $title = $url;
944da9f31c5SAndreas Gohr            $url   = 'ftp://'.$url;
945da9f31c5SAndreas Gohr        }
946da9f31c5SAndreas Gohr        if(substr($url,0,3) == 'www' && (substr($url,0,7) != 'http://')){
947da9f31c5SAndreas Gohr            $title = $url;
948da9f31c5SAndreas Gohr            $url = 'http://'.$url;
949da9f31c5SAndreas Gohr        }
950da9f31c5SAndreas Gohr
9518b1b81beSAndreas Gohr        $this->addCall('externallink', array($url, $title), $pos);
95244881bd0Shenning.noren        return true;
9530cecf9d5Sandi    }
9540cecf9d5Sandi
9558b1b81beSAndreas Gohr    /**
9568b1b81beSAndreas Gohr     * @param string $match matched syntax
9578b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
9588b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
9598b1b81beSAndreas Gohr     * @return bool mode handled?
9608b1b81beSAndreas Gohr     */
9618b1b81beSAndreas Gohr    public function emaillink($match, $state, $pos) {
9620cecf9d5Sandi        $email = preg_replace(array('/^</','/>$/'),'',$match);
9638b1b81beSAndreas Gohr        $this->addCall('emaillink', array($email, null), $pos);
96444881bd0Shenning.noren        return true;
9650cecf9d5Sandi    }
9660cecf9d5Sandi
9678b1b81beSAndreas Gohr    /**
9688b1b81beSAndreas Gohr     * @param string $match matched syntax
9698b1b81beSAndreas Gohr     * @param int $state a LEXER_STATE_* constant
9708b1b81beSAndreas Gohr     * @param int $pos byte position in the original source file
9718b1b81beSAndreas Gohr     * @return bool mode handled?
9728b1b81beSAndreas Gohr     */
9738b1b81beSAndreas Gohr    public function table($match, $state, $pos) {
9740cecf9d5Sandi        switch ( $state ) {
9750cecf9d5Sandi
9760cecf9d5Sandi            case DOKU_LEXER_ENTER:
9770cecf9d5Sandi
9788b1b81beSAndreas Gohr                $this->callWriter = new Table($this->callWriter);
9790cecf9d5Sandi
9808b1b81beSAndreas Gohr                $this->addCall('table_start', array($pos + 1), $pos);
9810cecf9d5Sandi                if ( trim($match) == '^' ) {
9828b1b81beSAndreas Gohr                    $this->addCall('tableheader', array(), $pos);
9830cecf9d5Sandi                } else {
9848b1b81beSAndreas Gohr                    $this->addCall('tablecell', array(), $pos);
9850cecf9d5Sandi                }
9860cecf9d5Sandi            break;
9870cecf9d5Sandi
9880cecf9d5Sandi            case DOKU_LEXER_EXIT:
9898b1b81beSAndreas Gohr                $this->addCall('table_end', array($pos), $pos);
9905c2aad12SAndreas Gohr                /** @var Table $reWriter */
9918b1b81beSAndreas Gohr                $reWriter = $this->callWriter;
9928b1b81beSAndreas Gohr                $this->callWriter = $reWriter->process();
9930cecf9d5Sandi            break;
9940cecf9d5Sandi
9950cecf9d5Sandi            case DOKU_LEXER_UNMATCHED:
9960cecf9d5Sandi                if ( trim($match) != '' ) {
9978b1b81beSAndreas Gohr                    $this->addCall('cdata', array($match), $pos);
9980cecf9d5Sandi                }
9990cecf9d5Sandi            break;
10000cecf9d5Sandi
10010cecf9d5Sandi            case DOKU_LEXER_MATCHED:
10029ab75d9eSAndreas Gohr                if ( $match == ' ' ){
10038b1b81beSAndreas Gohr                    $this->addCall('cdata', array($match), $pos);
100425b97867Shakan.sandell                } else if ( preg_match('/:::/',$match) ) {
10058b1b81beSAndreas Gohr                    $this->addCall('rowspan', array($match), $pos);
1006e205b721SAndreas Gohr                } else if ( preg_match('/\t+/',$match) ) {
10078b1b81beSAndreas Gohr                    $this->addCall('table_align', array($match), $pos);
1008e205b721SAndreas Gohr                } else if ( preg_match('/ {2,}/',$match) ) {
10098b1b81beSAndreas Gohr                    $this->addCall('table_align', array($match), $pos);
10100cecf9d5Sandi                } else if ( $match == "\n|" ) {
10118b1b81beSAndreas Gohr                    $this->addCall('table_row', array(), $pos);
10128b1b81beSAndreas Gohr                    $this->addCall('tablecell', array(), $pos);
10130cecf9d5Sandi                } else if ( $match == "\n^" ) {
10148b1b81beSAndreas Gohr                    $this->addCall('table_row', array(), $pos);
10158b1b81beSAndreas Gohr                    $this->addCall('tableheader', array(), $pos);
10160cecf9d5Sandi                } else if ( $match == '|' ) {
10178b1b81beSAndreas Gohr                    $this->addCall('tablecell', array(), $pos);
10180cecf9d5Sandi                } else if ( $match == '^' ) {
10198b1b81beSAndreas Gohr                    $this->addCall('tableheader', array(), $pos);
10200cecf9d5Sandi                }
10210cecf9d5Sandi            break;
10220cecf9d5Sandi        }
102344881bd0Shenning.noren        return true;
10240cecf9d5Sandi    }
10258b1b81beSAndreas Gohr
10268b1b81beSAndreas Gohr    // endregion modes
10270cecf9d5Sandi}
10280cecf9d5Sandi
10290cecf9d5Sandi//------------------------------------------------------------------------
10300cecf9d5Sandifunction Doku_Handler_Parse_Media($match) {
10314a572c5aSSam    global $conf;
10320cecf9d5Sandi    // Strip the opening and closing markup
10330cecf9d5Sandi    $link = preg_replace(array('/^\{\{/','/\}\}$/u'),'',$match);
10340cecf9d5Sandi
10350cecf9d5Sandi    // Split title from URL
10364b7f9e70STom N Harris    $link = explode('|',$link,2);
10370cecf9d5Sandi
10380cecf9d5Sandi    // Check alignment
10390cecf9d5Sandi    $ralign = (bool)preg_match('/^ /',$link[0]);
10400cecf9d5Sandi    $lalign = (bool)preg_match('/ $/',$link[0]);
10410cecf9d5Sandi
10420cecf9d5Sandi    // Logic = what's that ;)...
10430cecf9d5Sandi    if ( $lalign & $ralign ) {
10440cecf9d5Sandi        $align = 'center';
10450cecf9d5Sandi    } else if ( $ralign ) {
10460cecf9d5Sandi        $align = 'right';
10470cecf9d5Sandi    } else if ( $lalign ) {
10480cecf9d5Sandi        $align = 'left';
10490cecf9d5Sandi    } else {
10500ea51e63SMatt Perry        $align = null;
10510cecf9d5Sandi    }
10520cecf9d5Sandi
10530cecf9d5Sandi    // The title...
10540cecf9d5Sandi    if ( !isset($link[1]) ) {
10550ea51e63SMatt Perry        $link[1] = null;
10560cecf9d5Sandi    }
10570cecf9d5Sandi
10584826ab45Sandi    //remove aligning spaces
10594826ab45Sandi    $link[0] = trim($link[0]);
10600cecf9d5Sandi
10614826ab45Sandi    //split into src and parameters (using the very last questionmark)
10624826ab45Sandi    $pos = strrpos($link[0], '?');
10634826ab45Sandi    if($pos !== false){
10644826ab45Sandi        $src   = substr($link[0],0,$pos);
10654826ab45Sandi        $param = substr($link[0],$pos+1);
10660cecf9d5Sandi    }else{
10674826ab45Sandi        $src   = $link[0];
10684826ab45Sandi        $param = '';
10690cecf9d5Sandi    }
10700cecf9d5Sandi
10714826ab45Sandi    //parse width and height
10724826ab45Sandi    if(preg_match('#(\d+)(x(\d+))?#i',$param,$size)){
1073443e135dSChristopher Smith        !empty($size[1]) ? $w = $size[1] : $w = null;
1074443e135dSChristopher Smith        !empty($size[3]) ? $h = $size[3] : $h = null;
1075fc1c55b1Shfuecks    } else {
10760ea51e63SMatt Perry        $w = null;
10770ea51e63SMatt Perry        $h = null;
10780cecf9d5Sandi    }
10790cecf9d5Sandi
1080dc673a5bSjoe.lapp    //get linking command
1081d35ab615Shenning.noren    if(preg_match('/nolink/i',$param)){
1082dc673a5bSjoe.lapp        $linking = 'nolink';
1083d35ab615Shenning.noren    }else if(preg_match('/direct/i',$param)){
1084dc673a5bSjoe.lapp        $linking = 'direct';
10858acb3108SAndreas Gohr    }else if(preg_match('/linkonly/i',$param)){
10868acb3108SAndreas Gohr        $linking = 'linkonly';
1087dc673a5bSjoe.lapp    }else{
1088dc673a5bSjoe.lapp        $linking = 'details';
1089dc673a5bSjoe.lapp    }
1090dc673a5bSjoe.lapp
10914826ab45Sandi    //get caching command
10924826ab45Sandi    if (preg_match('/(nocache|recache)/i',$param,$cachemode)){
10934826ab45Sandi        $cache = $cachemode[1];
10940cecf9d5Sandi    }else{
10954826ab45Sandi        $cache = 'cache';
10960cecf9d5Sandi    }
1097*deb02173SSam    list(, $mime) = mimetype($src, false);
109850598979SSam    $isVideo = media_supportedav($mime);
10992a4a042dSSam    $videoAtts = array();
1100*deb02173SSam    if($isVideo) {
11014a572c5aSSam        $confVideoAtts = explode(',',$conf['video_settings']);
11024a572c5aSSam        if((in_array("controls", $confVideoAtts) || preg_match('/controls/i',$param)) && !preg_match('/nocontrols/i',$param)){
11034a572c5aSSam            $videoAtts["controls"] = true;
11042a4a042dSSam        }
11054a572c5aSSam        if(((in_array("autoplay", $confVideoAtts)) || preg_match('/autoplay/i',$param)) && !preg_match('/noautoplay/i',$param)){
11064a572c5aSSam            $videoAtts["autoplay"] = true;
11072a4a042dSSam        }
11084a572c5aSSam        if((in_array("loop", $confVideoAtts) || preg_match('/loop/i',$param)) && !preg_match('/noloop/i',$param)){
11094a572c5aSSam            $videoAtts["loop"] = true;
11102a4a042dSSam        }
11114a572c5aSSam        if((in_array("muted", $confVideoAtts) || preg_match('/muted/i',$param)) && !preg_match('/nomuted/i',$param)){
11124a572c5aSSam            $videoAtts["muted"] = true;
11132a4a042dSSam        }
111450598979SSam    }
11156efc45a2SDmitry Katsubo    // Check whether this is a local or remote image or interwiki
11166efc45a2SDmitry Katsubo    if (media_isexternal($src) || link_isinterwiki($src)){
11174826ab45Sandi        $call = 'externalmedia';
11180cecf9d5Sandi    } else {
11194826ab45Sandi        $call = 'internalmedia';
11200cecf9d5Sandi    }
11210cecf9d5Sandi
11220cecf9d5Sandi    $params = array(
11230cecf9d5Sandi        'type'=>$call,
11244826ab45Sandi        'src'=>$src,
11250cecf9d5Sandi        'title'=>$link[1],
11260cecf9d5Sandi        'align'=>$align,
11274826ab45Sandi        'width'=>$w,
11284826ab45Sandi        'height'=>$h,
11290cecf9d5Sandi        'cache'=>$cache,
1130dc673a5bSjoe.lapp        'linking'=>$linking,
11310cecf9d5Sandi    );
113250598979SSam
113350598979SSam    if($isVideo && $linking != "linkonly") {
1134256ac28fSSam        $params['videoAtts'] = $videoAtts;
1135256ac28fSSam    }
11360cecf9d5Sandi
11370cecf9d5Sandi    return $params;
11380cecf9d5Sandi}
11390cecf9d5Sandi
1140