xref: /plugin/combo/syntax/code.php (revision 007225e5fb2d3f64edaccd3bd447ca26effb9d68)
1<?php
2
3// implementation of
4// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/code
5
6// must be run within Dokuwiki
7use ComboStrap\Prism;
8use ComboStrap\StringUtility;
9use ComboStrap\PluginUtility;
10
11require_once(__DIR__ . '/../class/StringUtility.php');
12require_once(__DIR__ . '/../class/Prism.php');
13
14if (!defined('DOKU_INC')) die();
15
16
17class syntax_plugin_combo_code extends DokuWiki_Syntax_Plugin
18{
19
20    /**
21     * Enable or disable the code component
22     */
23    const CONF_CODE_ENABLE = 'codeEnable';
24
25
26    /**
27     * The tag of the ui component
28     */
29    const CODE_TAG = "code";
30
31
32    function getType()
33    {
34        /**
35         * You can't write in a code block
36         */
37        return 'protected';
38    }
39
40    /**
41     * How DokuWiki will add P element
42     *
43     *  * 'normal' - The plugin can be used inside paragraphs
44     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
45     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
46     *
47     * @see DokuWiki_Syntax_Plugin::getPType()
48     */
49    function getPType()
50    {
51        return 'block';
52    }
53
54    /**
55     * @return array
56     * Allow which kind of plugin inside
57     *
58     * No one of array('baseonly','container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs')
59     * because we manage self the content and we call self the parser
60     *
61     * Return an array of one or more of the mode types {@link $PARSER_MODES} in Parser.php
62     */
63    function getAllowedTypes()
64    {
65        return array();
66    }
67
68    function getSort()
69    {
70        /**
71         * Should be less than the code syntax plugin
72         * which is 200
73         **/
74        return 199;
75    }
76
77
78    function connectTo($mode)
79    {
80
81        if ($this->getConf(self::CONF_CODE_ENABLE)) {
82            $pattern = PluginUtility::getContainerTagPattern(self::CODE_TAG);
83            $this->Lexer->addEntryPattern($pattern, $mode, PluginUtility::getModeForComponent($this->getPluginComponent()));
84        }
85
86
87    }
88
89
90    function postConnect()
91    {
92        if ($this->getConf(self::CONF_CODE_ENABLE)) {
93            $this->Lexer->addExitPattern('</' . self::CODE_TAG . '>', PluginUtility::getModeForComponent($this->getPluginComponent()));
94        }
95
96
97    }
98
99    /**
100     *
101     * The handle function goal is to parse the matched syntax through the pattern function
102     * and to return the result for use in the renderer
103     * This result is always cached until the page is modified.
104     * @param string $match
105     * @param int $state
106     * @param int $pos - byte position in the original source file
107     * @param Doku_Handler $handler
108     * @return array|bool
109     * @see DokuWiki_Syntax_Plugin::handle()
110     *
111     */
112    function handle($match, $state, $pos, Doku_Handler $handler)
113    {
114
115        switch ($state) {
116
117            case DOKU_LEXER_ENTER :
118                $tagAttributes = PluginUtility::getQualifiedTagAttributes($match,true, Prism::FILE_PATH_KEY);
119                return array(
120                    PluginUtility::STATE => $state,
121                    PluginUtility::ATTRIBUTES => $tagAttributes
122                );
123
124            case DOKU_LEXER_UNMATCHED :
125                return array(
126                    PluginUtility::STATE => $state,
127                    PluginUtility::PAYLOAD => $match
128                );
129
130            case DOKU_LEXER_EXIT :
131                return array(PluginUtility::STATE => $state);
132
133
134        }
135        return array();
136
137    }
138
139    /**
140     * Render the output
141     * @param string $format
142     * @param Doku_Renderer $renderer
143     * @param array $data - what the function handle() return'ed
144     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
145     * @see DokuWiki_Syntax_Plugin::render()
146     *
147     *
148     */
149    function render($format, Doku_Renderer $renderer, $data)
150    {
151
152
153        if ($format == 'xhtml') {
154
155            /** @var Doku_Renderer_xhtml $renderer */
156            $state = $data [PluginUtility::STATE];
157            switch ($state) {
158                case DOKU_LEXER_ENTER :
159
160                    $attributes = $data[PluginUtility::ATTRIBUTES];
161                    Prism::htmlEnter($renderer,$attributes,$this);
162                    break;
163
164                case DOKU_LEXER_UNMATCHED :
165                    $renderer->doc .= PluginUtility::escape($data[PluginUtility::PAYLOAD]) . DOKU_LF;
166                    break;
167
168                case DOKU_LEXER_EXIT :
169                    Prism::htmlExit($renderer);
170                    break;
171
172            }
173            return true;
174        }
175
176        // unsupported $mode
177        return false;
178
179    }
180
181
182}
183
184