xref: /plugin/combo/syntax/unit.php (revision 32b85071e019dd3646a67c17fac4051338e495eb)
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    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        if (!$this->getConf(syntax_plugin_combo_preformatted::CONF_PREFORMATTED_ENABLE)) {
68            return PluginUtility::disablePreformatted($mode);
69        } else {
70            return true;
71        }
72    }
73
74    /**
75     * Handle the node
76     * @return string
77     * See
78     * https://www.dokuwiki.org/devel:syntax_plugins#ptype
79     */
80    function getPType()
81    {
82        return 'block';
83    }
84
85    // This where the addEntryPattern must bed defined
86    public function connectTo($mode)
87    {
88        // This define the DOKU_LEXER_ENTER state
89        $pattern = PluginUtility::getContainerTagPattern(self::TAG);
90        $this->Lexer->addEntryPattern($pattern, $mode, 'plugin_' . PluginUtility::PLUGIN_BASE_NAME . '_' . $this->getPluginComponent());
91
92    }
93
94    public function postConnect()
95    {
96        // We define the DOKU_LEXER_EXIT state
97        $this->Lexer->addExitPattern('</' . self::TAG . '>', 'plugin_' . PluginUtility::PLUGIN_BASE_NAME . '_' . $this->getPluginComponent());
98
99    }
100
101
102    /**
103     * Handle the match
104     * You get the match for each pattern in the $match variable
105     * $state says if it's an entry, exit or match pattern
106     *
107     * This is an instruction block and is cached apart from the rendering output
108     * There is two caches levels
109     * This cache may be suppressed with the url parameters ?purge=true
110     * @param $match
111     * @param $state
112     * @param $pos
113     * @param Doku_Handler $handler
114     * @return array
115     */
116    public function handle($match, $state, $pos, Doku_Handler $handler)
117    {
118        switch ($state) {
119
120            case DOKU_LEXER_ENTER :
121
122                $parameters = PluginUtility::getTagAttributes($match);
123                return array(
124                    PluginUtility::STATE => $state,
125                    PluginUtility::ATTRIBUTES => $parameters);
126
127                break;
128
129            case DOKU_LEXER_UNMATCHED :
130
131                return PluginUtility::handleAndReturnUnmatchedData(self::TAG, $match, $handler);
132                break;
133
134            case DOKU_LEXER_EXIT:
135
136                return array(PluginUtility::STATE => $state);
137                break;
138
139        }
140
141    }
142
143    /**
144     * Render the output
145     * @param string $format
146     * @param Doku_Renderer $renderer
147     * @param array $data - what the function handle() return'ed
148     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
149     * @see DokuWiki_Syntax_Plugin::render()
150     *
151     *
152     */
153    function render($format, Doku_Renderer $renderer, $data)
154    {
155        // The $data variable comes from the handle() function
156        //
157        // $mode = 'xhtml' means that we output html
158        // There is other mode such as metadata, odt
159        if ($format == 'xhtml') {
160
161            /**
162             * To help the parser recognize that _xmlEntities is a function of Doku_Renderer_xhtml
163             * and not of Doku_Renderer
164             *
165             * @var Doku_Renderer_xhtml $renderer
166             */
167
168            $state = $data[PluginUtility::STATE];
169            switch ($state) {
170
171                case DOKU_LEXER_ENTER :
172
173                    $renderer->doc .= '<div class="webcomponent_' . self::getTag() . '"';
174                    $attributes = $data[PluginUtility::ATTRIBUTES];
175                    // Normally none
176                    if ($attributes['display']) {
177                        $renderer->doc .= ' style="display:' . $attributes['display'] . '" ';
178                    }
179                    $renderer->doc .= '>';
180                    break;
181
182                case DOKU_LEXER_UNMATCHED:
183                    $renderer->doc .= PluginUtility::renderUnmatched($data);
184                    break;
185                case DOKU_LEXER_EXIT :
186
187                    $renderer->doc .= '</div>';
188                    break;
189            }
190
191            return true;
192        }
193        return false;
194    }
195
196    public static function getElementName()
197    {
198        return PluginUtility::getTagName(get_called_class());
199    }
200
201}
202