xref: /plugin/combo/syntax/tab.php (revision 007225e5fb2d3f64edaccd3bd447ca26effb9d68)
1<?php
2/**
3 * DokuWiki Syntax Plugin Combostrap.
4 *
5 */
6
7use ComboStrap\LogUtility;
8use ComboStrap\PluginUtility;
9
10if (!defined('DOKU_INC')) {
11    die();
12}
13
14require_once(__DIR__ . '/../class/PluginUtility.php');
15
16/**
17 *
18 * The name of the class must follow a pattern (don't change it)
19 * ie:
20 *    syntax_plugin_PluginName_ComponentName
21 */
22class syntax_plugin_combo_tab extends DokuWiki_Syntax_Plugin
23{
24
25    const TAG = 'tab';
26    const SELECTED = 'selected';
27    const PANEL = 'panel';
28
29
30    /**
31     * Syntax Type.
32     *
33     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
34     * @see DokuWiki_Syntax_Plugin::getType()
35     */
36    function getType()
37    {
38        return 'container';
39    }
40
41    /**
42     * @return array
43     * Allow which kind of plugin inside
44     * All
45     */
46    public function getAllowedTypes()
47    {
48        return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs');
49    }
50
51    /**
52     * How Dokuwiki will add P element
53     *
54     *  * 'normal' - The plugin can be used inside paragraphs
55     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
56     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
57     *
58     * @see DokuWiki_Syntax_Plugin::getPType()
59     */
60    function getPType()
61    {
62        return 'block';
63    }
64
65    /**
66     * @see Doku_Parser_Mode::getSort()
67     *
68     * the mode with the lowest sort number will win out
69     * the container (parent) must then have a lower number than the child
70     */
71    function getSort()
72    {
73        return 100;
74    }
75
76    /**
77     * Create a pattern that will called this plugin
78     *
79     * @param string $mode
80     * @see Doku_Parser_Mode::connectTo()
81     */
82    function connectTo($mode)
83    {
84
85        if ($mode = PluginUtility::getModeForComponent(syntax_plugin_combo_tabs::TAG)) {
86            $pattern = PluginUtility::getContainerTagPattern(self::TAG);
87            $this->Lexer->addEntryPattern($pattern, $mode, PluginUtility::getModeForComponent($this->getPluginComponent()));
88        }
89
90    }
91
92    public function postConnect()
93    {
94
95        $this->Lexer->addExitPattern('</' . self::TAG . '>', PluginUtility::getModeForComponent($this->getPluginComponent()));
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
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
119                $tagAttributes = PluginUtility::getTagAttributes($match);
120                $liHtmlAttributes = $tagAttributes;
121
122                /**
123                 * Check all attributes for the link (not the li)
124                 * and delete them
125                 */
126                $active = "false";
127                $panel = "";
128                if(isset($liHtmlAttributes[self::SELECTED])){
129                    $active = $liHtmlAttributes[self::SELECTED];
130                    unset($liHtmlAttributes[self::SELECTED]);
131                }
132                if (isset($liHtmlAttributes[self::PANEL])){
133                    $panel = $liHtmlAttributes[self::PANEL];
134                    unset($liHtmlAttributes[self::SELECTED]);
135                } else {
136                    LogUtility::msg("A panel attribute is missing on a tab tag",LogUtility::LVL_MSG_ERROR,syntax_plugin_combo_tabs::TAG);
137                }
138
139                /**
140                 * Creating the li element
141                 */
142                PluginUtility::addClass2Attributes("nav-item",$liHtmlAttributes);
143                $html = "<li ".PluginUtility::array2HTMLAttributes($liHtmlAttributes).">".DOKU_LF;
144
145                /**
146                 * Creating the a element
147                 */
148                $aHtmlAttributes = array();
149                PluginUtility::addClass2Attributes("nav-link",$aHtmlAttributes);
150                if ($active==="true"){
151                    PluginUtility::addClass2Attributes("active",$aHtmlAttributes);
152                    $aHtmlAttributes["aria-selected"]="true";
153                }
154                $aHtmlAttributes['id']=$panel."-tab";
155                $aHtmlAttributes['data-toggle']="tab";
156                $aHtmlAttributes['aria-controls']=$panel;
157                $aHtmlAttributes['href']="#$panel";
158
159                $html .= "<a ".PluginUtility::array2HTMLAttributes($aHtmlAttributes).">";
160
161                return array(
162                    PluginUtility::STATE => $state,
163                    PluginUtility::ATTRIBUTES => $tagAttributes,
164                    PluginUtility::PAYLOAD => $html);
165
166            case DOKU_LEXER_UNMATCHED:
167
168                return
169                    array(
170                        PluginUtility::STATE => $state,
171                        PluginUtility::PAYLOAD => PluginUtility::escape($match)
172                    );
173
174
175            case DOKU_LEXER_EXIT :
176
177                return array(
178                    PluginUtility::STATE => $state,
179                    PluginUtility::PAYLOAD => "</a>".DOKU_LF."</li>"
180                );
181
182
183        }
184
185        return array();
186
187    }
188
189    /**
190     * Render the output
191     * @param string $format
192     * @param Doku_Renderer $renderer
193     * @param array $data - what the function handle() return'ed
194     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
195     * @see DokuWiki_Syntax_Plugin::render()
196     *
197     *
198     */
199    function render($format, Doku_Renderer $renderer, $data)
200    {
201
202        if ($format == 'xhtml') {
203
204            /** @var Doku_Renderer_xhtml $renderer */
205            $state = $data[PluginUtility::STATE];
206            switch ($state) {
207
208                case DOKU_LEXER_ENTER :
209                case DOKU_LEXER_EXIT :
210                    $renderer->doc .= $data[PluginUtility::PAYLOAD] . DOKU_LF;
211                    break;
212                case DOKU_LEXER_UNMATCHED:
213                    $renderer->doc .= $data[PluginUtility::PAYLOAD];
214                    break;
215            }
216            return true;
217        }
218        return false;
219    }
220
221
222}
223