1<?php
2/**
3 * Span Syntax Component of the Wrap Plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Anika Henke <anika@selfthinker.org>
7 */
8
9class syntax_plugin_wrap_span extends DokuWiki_Syntax_Plugin {
10    protected $special_pattern = '<span\b[^>\r\n]*?/>';
11    protected $entry_pattern   = '<span\b.*?>(?=.*?</span>)';
12    protected $exit_pattern    = '</span>';
13
14    function getType(){ return 'formatting';}
15    function getAllowedTypes() { return array('formatting', 'substition', 'disabled'); }
16    function getPType(){ return 'normal';}
17    function getSort(){ return 195; }
18    // override default accepts() method to allow nesting - ie, to get the plugin accepts its own entry syntax
19    function accepts($mode) {
20        if ($mode == substr(get_class($this), 7)) return true;
21        return parent::accepts($mode);
22    }
23
24    /**
25     * Connect pattern to lexer
26     */
27    function connectTo($mode) {
28        $this->Lexer->addSpecialPattern($this->special_pattern,$mode,'plugin_wrap_'.$this->getPluginComponent());
29        $this->Lexer->addEntryPattern($this->entry_pattern,$mode,'plugin_wrap_'.$this->getPluginComponent());
30    }
31
32    function postConnect() {
33        $this->Lexer->addExitPattern($this->exit_pattern, 'plugin_wrap_'.$this->getPluginComponent());
34    }
35
36    /**
37     * Handle the match
38     */
39    function handle($match, $state, $pos, Doku_Handler $handler){
40        switch ($state) {
41            case DOKU_LEXER_ENTER:
42            case DOKU_LEXER_SPECIAL:
43                $data = strtolower(trim(substr($match,strpos($match,' '),-1)," \t\n/"));
44                return array($state, $data);
45
46            case DOKU_LEXER_UNMATCHED :
47                $handler->addCall('cdata', array($match), $pos);
48                return false;
49
50            case DOKU_LEXER_EXIT :
51                return array($state, '');
52
53        }
54        return false;
55    }
56
57    /**
58     * Create output
59     */
60    function render($format, Doku_Renderer $renderer, $indata) {
61        static $type_stack = array ();
62
63        if (empty($indata)) return false;
64        list($state, $data) = $indata;
65
66        if($format == 'xhtml'){
67            switch ($state) {
68                case DOKU_LEXER_ENTER:
69                case DOKU_LEXER_SPECIAL:
70                    $wrap = $this->loadHelper('wrap');
71                    $attr = $wrap->buildAttributes($data);
72
73                    $renderer->doc .= '<span'.$attr.'>';
74                    if ($state == DOKU_LEXER_SPECIAL) $renderer->doc .= '</span>';
75                    break;
76
77                case DOKU_LEXER_EXIT:
78                    $renderer->doc .= '</span>';
79                    break;
80            }
81            return true;
82        }
83        if($format == 'odt'){
84            switch ($state) {
85                case DOKU_LEXER_ENTER:
86                    $wrap = plugin_load('helper', 'wrap');
87                    array_push ($type_stack, $wrap->renderODTElementOpen($renderer, 'span', $data));
88                    break;
89
90                case DOKU_LEXER_EXIT:
91                    $element = array_pop ($type_stack);
92                    $wrap = plugin_load('helper', 'wrap');
93                    $wrap->renderODTElementClose ($renderer, $element);
94                    break;
95            }
96            return true;
97        }
98        return false;
99    }
100}
101