xref: /plugin/combo/syntax/navbargroup.php (revision 4cadd4f8c541149bdda95f080e38a6d4e3a640ca)
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 postConnect()
104    {
105        $this->Lexer->addExitPattern('</' . self::TAG . '>', 'plugin_' . PluginUtility::PLUGIN_BASE_NAME . '_' . $this->getPluginComponent());
106
107    }
108
109    /**
110     *
111     * The handle function goal is to parse the matched syntax through the pattern function
112     * and to return the result for use in the renderer
113     * This result is always cached until the page is modified.
114     * @param string $match
115     * @param int $state
116     * @param int $pos
117     * @param Doku_Handler $handler
118     * @return array|bool
119     * @see DokuWiki_Syntax_Plugin::handle()
120     *
121     */
122    function handle($match, $state, $pos, Doku_Handler $handler)
123    {
124
125        switch ($state) {
126
127            case DOKU_LEXER_ENTER:
128
129                // Suppress the component name
130                $tagAttributes = PluginUtility::getTagAttributes($match);
131                return array(
132                    PluginUtility::STATE => $state,
133                    PluginUtility::ATTRIBUTES=> $tagAttributes
134                );
135
136            case DOKU_LEXER_UNMATCHED:
137                return PluginUtility::handleAndReturnUnmatchedData(self::TAG,$match,$handler);
138
139
140            case DOKU_LEXER_EXIT :
141
142                return array(PluginUtility::STATE => $state);
143
144
145        }
146
147        return array();
148
149    }
150
151    /**
152     * Render the output
153     * @param string $format
154     * @param Doku_Renderer $renderer
155     * @param array $data - what the function handle() return'ed
156     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
157     * @see DokuWiki_Syntax_Plugin::render()
158     *
159     *
160     */
161    function render($format, Doku_Renderer $renderer, $data)
162    {
163
164        $state = $data[PluginUtility::STATE];
165        if ($format == 'xhtml') {
166
167            /** @var Doku_Renderer_xhtml $renderer */
168
169            switch ($state) {
170
171                case DOKU_LEXER_ENTER :
172                    // https://getbootstrap.com/docs/4.0/components/navbar/#toggler splits the navbar-nav to another element
173                    // navbar-nav implementation
174                    $attributes = $data[PluginUtility::ATTRIBUTES];
175                    $classValue = "navbar-nav";
176                    if (array_key_exists("class", $attributes)) {
177                        $attributes["class"] .= " {$classValue}";
178                    } else {
179                        $attributes["class"] = $classValue;
180                    }
181
182                    if (array_key_exists("expand", $attributes)) {
183                        if ($attributes["expand"]=="true") {
184                            $bootstrapVersion = Bootstrap::getBootStrapMajorVersion();
185                            if($bootstrapVersion== Bootstrap::BootStrapFiveMajorVersion){
186                                $attributes["class"] .= " me-auto";
187                            } else {
188                                $attributes["class"] .= " mr-auto";
189                            }
190                        }
191                        unset($attributes["expand"]);
192                    }
193
194                    $inlineAttributes = PluginUtility::array2HTMLAttributesAsString($attributes);
195                    $renderer->doc .= "<ul {$inlineAttributes}>" . DOKU_LF;
196                    break;
197                case DOKU_LEXER_UNMATCHED :
198                    $renderer->doc .= NavBarUtility::text(PluginUtility::renderUnmatched($data));
199                    break;
200
201                case DOKU_LEXER_EXIT :
202                    $renderer->doc .= '</ul>' . DOKU_LF;
203                    break;
204            }
205            return true;
206        }
207        return false;
208    }
209
210
211}
212