xref: /plugin/combo/syntax/note.php (revision 5f891b7e09648e05e78f5882f3fdde1e9df9b0f1)
1<?php
2
3
4// must be run within Dokuwiki
5use ComboStrap\PluginUtility;
6
7if (!defined('DOKU_INC')) die();
8
9/**
10 * Class syntax_plugin_combo_note
11 * Implementation of a note
12 * called an alert in <a href="https://getbootstrap.com/docs/4.0/components/alerts/">bootstrap</a>
13 */
14class syntax_plugin_combo_note extends DokuWiki_Syntax_Plugin
15{
16
17    const TAG = "note";
18
19    /**
20     * Syntax Type.
21     *
22     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
23     * @see DokuWiki_Syntax_Plugin::getType()
24     */
25    function getType()
26    {
27        return 'container';
28    }
29
30    /**
31     * How Dokuwiki will add P element
32     *
33     * * 'normal' - The plugin can be used inside paragraphs
34     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
35     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
36     *
37     * @see DokuWiki_Syntax_Plugin::getPType()
38     */
39    function getPType()
40    {
41        return 'block';
42    }
43
44    /**
45     * @return array
46     * Allow which kind of plugin inside
47     *
48     * ************************
49     * This function has no effect because {@link SyntaxPlugin::accepts()} is used
50     * ************************
51     */
52    function getAllowedTypes()
53    {
54        return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs');
55    }
56
57
58
59    function getSort()
60    {
61        return 201;
62    }
63
64    public function accepts($mode)
65    {
66        /**
67         * header mode is disable to take over
68         * and replace it with {@link syntax_plugin_combo_title}
69         */
70        if ($mode == "header"){
71            return false;
72        }
73        /**
74         * If preformatted is disable, we does not accept it
75         */
76        if (!$this->getConf(syntax_plugin_combo_preformatted::CONF_PREFORMATTED_ENABLE)) {
77            return PluginUtility::disablePreformatted($mode);
78        } else {
79            return true;
80        }
81    }
82
83
84
85    function connectTo($mode)
86    {
87
88        $pattern = PluginUtility::getContainerTagPattern(self::TAG);
89        $this->Lexer->addEntryPattern($pattern, $mode, PluginUtility::getModeForComponent($this->getPluginComponent()));
90    }
91
92
93    function postConnect()
94    {
95
96        $this->Lexer->addExitPattern('</' . self::TAG . '>', PluginUtility::getModeForComponent($this->getPluginComponent()));
97
98    }
99
100    function handle($match, $state, $pos, Doku_Handler $handler)
101    {
102
103        switch ($state) {
104
105            case DOKU_LEXER_ENTER :
106                $defaultAttributes = array("type" => "info");
107                $inlineAttributes = PluginUtility::getTagAttributes($match);
108                $attributes = PluginUtility::mergeAttributes($inlineAttributes, $defaultAttributes);
109                return array($state, $attributes);
110
111            case DOKU_LEXER_UNMATCHED :
112                return array($state, $match);
113
114            case DOKU_LEXER_EXIT :
115
116                // Important otherwise we don't get an exit in the render
117                return array($state, '');
118
119
120        }
121        return array();
122
123    }
124
125    /**
126     * Render the output
127     * @param string $format
128     * @param Doku_Renderer $renderer
129     * @param array $data - what the function handle() return'ed
130     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
131     * @see DokuWiki_Syntax_Plugin::render()
132     *
133     *
134     */
135    function render($format, Doku_Renderer $renderer, $data)
136    {
137        if ($format == 'xhtml') {
138
139            /** @var Doku_Renderer_xhtml $renderer */
140            list($state, $payload) = $data;
141            switch ($state) {
142                case DOKU_LEXER_ENTER :
143                    $attributes = $payload;
144                    $classValue = "alert";
145                    $type = $attributes["type"];
146                    // Switch for the color
147                    switch ($type) {
148                        case "important":
149                            $type = "warning";
150                            break;
151                        case "warning":
152                            $type = "danger";
153                            break;
154                    }
155
156                    if ($type != "tip") {
157                        $classValue .= " alert-" . $type;
158                    } else {
159                        // There is no alert-tip color
160                        // base color was background color and we have modified the luminance
161                        if (!array_key_exists("color", $attributes)) {
162                            $attributes["color"] = "#6c6400"; // lum - 51
163                        }
164                        if (!array_key_exists("border-color", $attributes)) {
165                            $attributes["border-color"] = "#FFF78c"; // lum - 186
166                        }
167                        if (!array_key_exists("background-color", $attributes)) {
168                            $attributes["background-color"] = "#fff79f"; // lum - 195
169                        }
170                    }
171
172                    if (array_key_exists("class", $attributes)) {
173                        $attributes["class"] .= " {$classValue}";
174                    } else {
175                        $attributes["class"] = "{$classValue}";
176                    }
177
178                    $renderer->doc .= '<div ' . PluginUtility::array2HTMLAttributes($attributes) . ' role="note">';
179                    break;
180
181                case DOKU_LEXER_UNMATCHED :
182                    $renderer->doc .= $renderer->_xmlEntities($payload);
183                    break;
184
185                case DOKU_LEXER_EXIT :
186                    $renderer->doc .= '</div>';
187                    break;
188            }
189            return true;
190        }
191
192        // unsupported $mode
193        return false;
194    }
195
196
197}
198
199