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