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