xref: /plugin/combo/syntax/navbargroup.php (revision 977ce05d19d8dab0a70c9a27f8da0b7039299e82)
1<?php
2/**
3 * DokuWiki Syntax Plugin Combostrap.
4 *
5 */
6
7use ComboStrap\Bootstrap;
8use ComboStrap\NavBarUtility;
9use ComboStrap\PluginUtility;
10
11
12require_once(__DIR__ . '/../ComboStrap/PluginUtility.php');
13
14
15/**
16 * All DokuWiki plugins to extend the parser/rendering mechanism
17 * need to inherit from this class
18 *
19 * The name of the class must follow a pattern (don't change it)
20 * ie:
21 *    syntax_plugin_PluginName_ComponentName
22 *
23 * A navbar group is a navbar nav
24 */
25class syntax_plugin_combo_navbargroup extends DokuWiki_Syntax_Plugin
26{
27
28    const TAG = "group";
29    const COMPONENT = "navbargroup";
30
31    /**
32     * Syntax Type.
33     *
34     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
35     * @see DokuWiki_Syntax_Plugin::getType()
36     */
37    function getType()
38    {
39        return 'container';
40    }
41
42    /**
43     * @return array
44     * Allow which kind of plugin inside
45     * All
46     */
47    public function getAllowedTypes()
48    {
49        return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs');
50    }
51
52    /**
53     * How Dokuwiki will add P element
54     *
55     *  * 'normal' - The plugin can be used inside paragraphs
56     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
57     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
58     *
59     * @see DokuWiki_Syntax_Plugin::getPType()
60     */
61    function getPType()
62    {
63        return 'normal';
64    }
65
66    /**
67     * @see Doku_Parser_Mode::getSort()
68     *
69     * the mode with the lowest sort number will win out
70     * the container (parent) must then have a lower number than the child
71     */
72    function getSort()
73    {
74        return 100;
75    }
76
77    /**
78     * Create a pattern that will called this plugin
79     *
80     * @param string $mode
81     * @see Doku_Parser_Mode::connectTo()
82     */
83    function connectTo($mode)
84    {
85        /**
86         * Only inside a navbar or collapse element
87         */
88        $authorizedMode = [
89            PluginUtility::getModeFromTag(syntax_plugin_combo_navbarcollapse::COMPONENT),
90            PluginUtility::getModeFromTag(syntax_plugin_combo_menubar::TAG)
91        ];
92
93
94        if (in_array($mode, $authorizedMode)) {
95
96            $pattern = PluginUtility::getContainerTagPattern(self::TAG);
97            $this->Lexer->addEntryPattern($pattern, $mode, PluginUtility::getModeFromTag($this->getPluginComponent()));
98
99        }
100
101    }
102
103    public function accepts($mode)
104    {
105
106        $accept = syntax_plugin_combo_preformatted::disablePreformatted($mode);
107
108        // P element are not welcome in a navbar
109        if ($mode == "eol") {
110            $accept = false;
111        }
112
113        return $accept;
114
115    }
116
117    public function postConnect()
118    {
119        $this->Lexer->addExitPattern('</' . self::TAG . '>', 'plugin_' . PluginUtility::PLUGIN_BASE_NAME . '_' . $this->getPluginComponent());
120
121    }
122
123    /**
124     *
125     * The handle function goal is to parse the matched syntax through the pattern function
126     * and to return the result for use in the renderer
127     * This result is always cached until the page is modified.
128     * @param string $match
129     * @param int $state
130     * @param int $pos
131     * @param Doku_Handler $handler
132     * @return array|bool
133     * @see DokuWiki_Syntax_Plugin::handle()
134     *
135     */
136    function handle($match, $state, $pos, Doku_Handler $handler)
137    {
138
139        switch ($state) {
140
141            case DOKU_LEXER_ENTER:
142
143                // Suppress the component name
144                $tagAttributes = PluginUtility::getTagAttributes($match);
145                return array(
146                    PluginUtility::STATE => $state,
147                    PluginUtility::ATTRIBUTES=> $tagAttributes
148                );
149
150            case DOKU_LEXER_UNMATCHED:
151                return PluginUtility::handleAndReturnUnmatchedData(self::TAG,$match,$handler);
152
153
154            case DOKU_LEXER_EXIT :
155
156                return array(PluginUtility::STATE => $state);
157
158
159        }
160
161        return array();
162
163    }
164
165    /**
166     * Render the output
167     * @param string $format
168     * @param Doku_Renderer $renderer
169     * @param array $data - what the function handle() return'ed
170     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
171     * @see DokuWiki_Syntax_Plugin::render()
172     *
173     *
174     */
175    function render($format, Doku_Renderer $renderer, $data)
176    {
177
178        $state = $data[PluginUtility::STATE];
179        if ($format == 'xhtml') {
180
181            /** @var Doku_Renderer_xhtml $renderer */
182
183            switch ($state) {
184
185                case DOKU_LEXER_ENTER :
186                    // https://getbootstrap.com/docs/4.0/components/navbar/#toggler splits the navbar-nav to another element
187                    // navbar-nav implementation
188                    $attributes = $data[PluginUtility::ATTRIBUTES];
189                    $classValue = "navbar-nav";
190                    if (array_key_exists("class", $attributes)) {
191                        $attributes["class"] .= " {$classValue}";
192                    } else {
193                        $attributes["class"] = $classValue;
194                    }
195
196                    if (array_key_exists("expand", $attributes)) {
197                        if ($attributes["expand"]=="true") {
198                            $bootstrapVersion = Bootstrap::getBootStrapMajorVersion();
199                            if($bootstrapVersion== Bootstrap::BootStrapFiveMajorVersion){
200                                $attributes["class"] .= " me-auto";
201                            } else {
202                                $attributes["class"] .= " mr-auto";
203                            }
204                        }
205                        unset($attributes["expand"]);
206                    }
207
208                    $inlineAttributes = PluginUtility::array2HTMLAttributesAsString($attributes);
209                    $renderer->doc .= "<ul {$inlineAttributes}>" . DOKU_LF;
210                    break;
211                case DOKU_LEXER_UNMATCHED :
212                    $renderer->doc .= NavBarUtility::text(PluginUtility::renderUnmatched($data));
213                    break;
214
215                case DOKU_LEXER_EXIT :
216                    $renderer->doc .= '</ul>' . DOKU_LF;
217                    break;
218            }
219            return true;
220        }
221        return false;
222    }
223
224
225}
226