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