xref: /plugin/combo/syntax/unit.php (revision 37748cd8654635afbeca80942126742f0f4cc346)
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__ . '/../ComboStrap/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    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 = PluginUtility::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)
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                break;
126
127            case DOKU_LEXER_UNMATCHED :
128
129                return PluginUtility::handleAndReturnUnmatchedData(self::TAG, $match, $handler);
130                break;
131
132            case DOKU_LEXER_EXIT:
133
134                return array(PluginUtility::STATE => $state);
135                break;
136
137        }
138
139    }
140
141    /**
142     * Render the output
143     * @param string $format
144     * @param Doku_Renderer $renderer
145     * @param array $data - what the function handle() return'ed
146     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
147     * @see DokuWiki_Syntax_Plugin::render()
148     *
149     *
150     */
151    function render($format, Doku_Renderer $renderer, $data)
152    {
153        // The $data variable comes from the handle() function
154        //
155        // $mode = 'xhtml' means that we output html
156        // There is other mode such as metadata, odt
157        if ($format == 'xhtml') {
158
159            /**
160             * To help the parser recognize that _xmlEntities is a function of Doku_Renderer_xhtml
161             * and not of Doku_Renderer
162             *
163             * @var Doku_Renderer_xhtml $renderer
164             */
165
166            $state = $data[PluginUtility::STATE];
167            switch ($state) {
168
169                case DOKU_LEXER_ENTER :
170
171                    $renderer->doc .= '<div class="webcomponent_' . self::getTag() . '"';
172                    $attributes = $data[PluginUtility::ATTRIBUTES];
173                    // Normally none
174                    if ($attributes['display']) {
175                        $renderer->doc .= ' style="display:' . $attributes['display'] . '" ';
176                    }
177                    $renderer->doc .= '>';
178                    break;
179
180                case DOKU_LEXER_UNMATCHED:
181                    $renderer->doc .= PluginUtility::renderUnmatched($data);
182                    break;
183                case DOKU_LEXER_EXIT :
184
185                    $renderer->doc .= '</div>';
186                    break;
187            }
188
189            return true;
190        }
191        return false;
192    }
193
194    public static function getElementName()
195    {
196        return PluginUtility::getTagName(get_called_class());
197    }
198
199}
200