1<?php
2
3
4use ComboStrap\PluginUtility;
5use ComboStrap\TagAttributes;
6use ComboStrap\TagAttribute\Toggle;
7use ComboStrap\XmlTagProcessing;
8
9class syntax_plugin_combo_togglecollapse extends DokuWiki_Syntax_Plugin
10{
11
12    const TAG = "collapse";
13    const CANONICAL = syntax_plugin_combo_toggle::CANONICAL;
14
15
16    /**
17     * Syntax Type.
18     *
19     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
20     * @see DokuWiki_Syntax_Plugin::getType()
21     */
22    function getType(): string
23    {
24        return 'substition';
25    }
26
27    /**
28     * How Dokuwiki will add P element
29     *
30     *  * 'normal' - The plugin can be used inside paragraphs
31     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
32     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
33     *
34     * @see DokuWiki_Syntax_Plugin::getPType()
35     */
36    function getPType(): string
37    {
38        // button or link
39        return 'normal';
40    }
41
42    /**
43     * @return array
44     * Allow which kind of plugin inside
45     *
46     * array('container', 'baseonly', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs')
47     *
48     */
49    function getAllowedTypes(): array
50    {
51        return array('baseonly', 'formatting', 'substition', 'protected', 'disabled');
52    }
53
54    function getSort(): int
55    {
56        return 201;
57    }
58
59    public
60    function accepts($mode): bool
61    {
62        return syntax_plugin_combo_preformatted::disablePreformatted($mode)
63            && Toggle::disableEntity($mode);
64    }
65
66
67    /**
68     * Create a pattern that will called this plugin
69     *
70     * @param string $mode
71     * @see Doku_Parser_Mode::connectTo()
72     */
73    function connectTo($mode)
74    {
75
76        /**
77         * Tag only valid in a toggle tag
78         */
79        if ($mode == PluginUtility::getModeFromTag(syntax_plugin_combo_toggle::TAG)) {
80            $pattern = XmlTagProcessing::getContainerTagPattern(self::getTag());
81            $this->Lexer->addEntryPattern($pattern, $mode, 'plugin_' . PluginUtility::PLUGIN_BASE_NAME . '_' . $this->getPluginComponent());
82        }
83
84
85    }
86
87    function postConnect()
88    {
89
90        $this->Lexer->addExitPattern('</' . self::getTag() . '>', 'plugin_' . PluginUtility::PLUGIN_BASE_NAME . '_' . $this->getPluginComponent());
91
92    }
93
94    function handle($match, $state, $pos, Doku_Handler $handler)
95    {
96
97
98        switch ($state) {
99
100            case DOKU_LEXER_ENTER :
101
102                /**
103                 * Default parameters, type definition and parsing
104                 */
105                $defaultParameters = [];
106                $knownTypes = [];
107                $tagAttributes = TagAttributes::createFromTagMatch($match, $defaultParameters, $knownTypes)
108                    ->setLogicalTag(self::TAG);
109
110                return array(
111                    PluginUtility::STATE => $state,
112                    PluginUtility::ATTRIBUTES => $tagAttributes->toCallStackArray()
113                );
114            case DOKU_LEXER_UNMATCHED :
115                return PluginUtility::handleAndReturnUnmatchedData(self::TAG, $match, $handler);
116
117            case DOKU_LEXER_EXIT :
118                return array(
119                    PluginUtility::STATE => $state
120                );
121
122
123        }
124        return array();
125
126    }
127
128    /**
129     * Render the output
130     * @param string $format
131     * @param Doku_Renderer $renderer
132     * @param array $data - what the function handle() return
133     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
134     * @see DokuWiki_Syntax_Plugin::render()
135     *
136     *
137     */
138    function render($format, Doku_Renderer $renderer, $data): bool
139    {
140
141        if ($format === "xhtml") {
142            $state = $data[PluginUtility::STATE];
143            switch ($state) {
144                case DOKU_LEXER_ENTER:
145                    $tagAttributes = TagAttributes::createFromCallStackArray($data[PluginUtility::ATTRIBUTES])
146                        ->setLogicalTag(self::TAG);
147                    $renderer->doc .= $tagAttributes->toHtmlEnterTag("span");
148                    break;
149                case DOKU_LEXER_UNMATCHED:
150                    $renderer->doc .= PluginUtility::renderUnmatched($data);
151                    break;
152                case DOKU_LEXER_EXIT:
153                    $renderer->doc .= "</span>";
154                    break;
155
156            }
157            return true;
158        }
159
160        // unsupported $mode
161        return false;
162    }
163
164    public
165    static function getTag(): string
166    {
167        return self::TAG;
168    }
169
170
171}
172
173