1<?php
2/**
3 * Plugin Webcode: Show webcode (Css, HTML) in a iframe
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Nicolas GERARD
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11require_once(__DIR__ . '/../webcomponent.php');
12
13/**
14 * All DokuWiki plugins to extend the parser/rendering mechanism
15 * need to inherit from this class
16 *
17 * Format
18 *
19 * syntax_plugin_PluginName_PluginComponent
20 */
21class syntax_plugin_webcomponent_unit extends DokuWiki_Syntax_Plugin
22{
23
24
25
26
27    private static function getTag()
28    {
29        return webcomponent::getTagName(get_called_class());
30    }
31
32    /*
33     * What is the type of this plugin ?
34     * This a plugin categorization
35     * This is only important for other plugin
36     * See @getAllowedTypes
37     */
38    public function getType()
39    {
40        return 'formatting';
41    }
42
43
44    // Sort order in which the plugin are applied
45    public function getSort()
46    {
47        return 168;
48    }
49
50    /**
51     *
52     * @return array
53     * The plugin type that are allowed inside
54     * this node (All !)
55     * Otherwise the node that are in the matched content are not processed
56     */
57    function getAllowedTypes() {
58        return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs');
59
60    }
61
62    /**
63     * Handle the node
64     * @return string
65     * See
66     * https://www.dokuwiki.org/devel:syntax_plugins#ptype
67     */
68    function getPType(){ return 'block';}
69
70    // This where the addEntryPattern must bed defined
71    public function connectTo($mode)
72    {
73        // This define the DOKU_LEXER_ENTER state
74        $pattern = webcomponent::getLookAheadPattern(self::getElementName());
75        $this->Lexer->addEntryPattern($pattern, $mode, 'plugin_' . webcomponent::PLUGIN_NAME . '_' . $this->getPluginComponent());
76
77    }
78
79    public function postConnect()
80    {
81        // We define the DOKU_LEXER_EXIT state
82        $this->Lexer->addExitPattern('</' . self::getElementName() . '>', 'plugin_' . webcomponent::PLUGIN_NAME . '_' . $this->getPluginComponent());
83
84    }
85
86
87    /**
88     * Handle the match
89     * You get the match for each pattern in the $match variable
90     * $state says if it's an entry, exit or match pattern
91     *
92     * This is an instruction block and is cached apart from the rendering output
93     * There is two caches levels
94     * This cache may be suppressed with the url parameters ?purge=true
95     */
96    public function handle($match, $state, $pos, Doku_Handler $handler)
97    {
98        switch ($state) {
99
100            case DOKU_LEXER_ENTER :
101
102                // Suppress the tag name
103                $match = utf8_substr($match, strlen(self::getTag()) + 1, -1);
104                $parameters = webcomponent::parseMatch($match);
105                return array($state, $parameters);
106
107                break;
108
109            case DOKU_LEXER_UNMATCHED :
110
111
112
113                //
114                // The nested authorized plugin are given in the function
115                // getAllowedTypes
116                //
117                // cdata  means normal text ??? See xhtml.php function cdata
118                // What it does exactly, I don't know
119                // but as we want to process the content
120                // we need to add a call to the lexer to go further
121                // Comes from the wrap plugin
122                $handler->_addCall('cdata', array($match), $pos, null);
123                break;
124
125            case DOKU_LEXER_EXIT:
126
127                return array($state, '');
128                break;
129
130        }
131
132    }
133
134    /**
135     * Create output
136     * The rendering process
137     * @param string $mode
138     * @param Doku_Renderer $renderer
139     * @param array $data
140     * @return bool
141     */
142    public function render($mode, Doku_Renderer $renderer, $data)
143    {
144        // The $data variable comes from the handle() function
145        //
146        // $mode = 'xhtml' means that we output html
147        // There is other mode such as metadata, odt
148        if ($mode == 'xhtml') {
149
150            /**
151             * To help the parser recognize that _xmlEntities is a function of Doku_Renderer_xhtml
152             * and not of Doku_Renderer
153             *
154             * @var Doku_Renderer_xhtml $renderer
155             */
156
157            list($state, $parameters) = $data;
158            switch ($state) {
159
160                case DOKU_LEXER_ENTER :
161
162                    $renderer->doc .= '<div class="webcomponent_'.self::getTag() .'"';
163                    // Normally none
164                    if ($parameters['display']){
165                        $renderer->doc .= ' style="display:'.$parameters['display'].'" ';
166                    }
167                    $renderer->doc .= '>';
168                    break;
169
170
171                case DOKU_LEXER_EXIT :
172
173                    $renderer->doc .= '</div>';
174                    break;
175            }
176
177            return true;
178        }
179        return false;
180    }
181
182    public static function getElementName()
183    {
184        return webcomponent::getTagName(get_called_class());
185    }
186
187}
188