xref: /plugin/combo/syntax/note.php (revision 32b85071e019dd3646a67c17fac4051338e495eb)
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    function getSort()
59    {
60        return 201;
61    }
62
63    public function accepts($mode)
64    {
65        /**
66         * header mode is disable to take over
67         * and replace it with {@link syntax_plugin_combo_title}
68         */
69        if ($mode == "header") {
70            return false;
71        }
72        /**
73         * If preformatted is disable, we does not accept it
74         */
75        if (!$this->getConf(syntax_plugin_combo_preformatted::CONF_PREFORMATTED_ENABLE)) {
76            return PluginUtility::disablePreformatted($mode);
77        } else {
78            return true;
79        }
80    }
81
82
83    function connectTo($mode)
84    {
85
86        $pattern = PluginUtility::getContainerTagPattern(self::TAG);
87        $this->Lexer->addEntryPattern($pattern, $mode, PluginUtility::getModeForComponent($this->getPluginComponent()));
88    }
89
90
91    function postConnect()
92    {
93
94        $this->Lexer->addExitPattern('</' . self::TAG . '>', PluginUtility::getModeForComponent($this->getPluginComponent()));
95
96    }
97
98    function handle($match, $state, $pos, Doku_Handler $handler)
99    {
100
101        switch ($state) {
102
103            case DOKU_LEXER_ENTER :
104                $defaultAttributes = array("type" => "info");
105                $inlineAttributes = PluginUtility::getTagAttributes($match);
106                $attributes = PluginUtility::mergeAttributes($inlineAttributes, $defaultAttributes);
107                return array(
108                    PluginUtility::STATE => $state,
109                    PluginUtility::ATTRIBUTES => $attributes
110                );
111
112            case DOKU_LEXER_UNMATCHED :
113                return PluginUtility::handleAndReturnUnmatchedData(self::TAG, $match, $handler);
114
115            case DOKU_LEXER_EXIT :
116
117                // Important otherwise we don't get an exit in the render
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'ed
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)
139    {
140        if ($format == 'xhtml') {
141
142            /** @var Doku_Renderer_xhtml $renderer */
143            $state = $data[PluginUtility::STATE];
144            switch ($state) {
145                case DOKU_LEXER_ENTER :
146                    $attributes = $data[PluginUtility::ATTRIBUTES];
147                    $classValue = "alert";
148                    $type = $attributes["type"];
149                    // Switch for the color
150                    switch ($type) {
151                        case "important":
152                            $type = "warning";
153                            break;
154                        case "warning":
155                            $type = "danger";
156                            break;
157                    }
158
159                    if ($type != "tip") {
160                        $classValue .= " alert-" . $type;
161                    } else {
162                        // There is no alert-tip color
163                        // base color was background color and we have modified the luminance
164                        if (!array_key_exists("color", $attributes)) {
165                            $attributes["color"] = "#6c6400"; // lum - 51
166                        }
167                        if (!array_key_exists("border-color", $attributes)) {
168                            $attributes["border-color"] = "#FFF78c"; // lum - 186
169                        }
170                        if (!array_key_exists("background-color", $attributes)) {
171                            $attributes["background-color"] = "#fff79f"; // lum - 195
172                        }
173                    }
174
175                    if (array_key_exists("class", $attributes)) {
176                        $attributes["class"] .= " {$classValue}";
177                    } else {
178                        $attributes["class"] = "{$classValue}";
179                    }
180
181                    $renderer->doc .= '<div ' . PluginUtility::array2HTMLAttributes($attributes) . ' role="note">';
182                    break;
183
184                case DOKU_LEXER_UNMATCHED :
185                    $renderer->doc .= PluginUtility::renderUnmatched($data);
186                    break;
187
188                case DOKU_LEXER_EXIT :
189                    $renderer->doc .= '</div>';
190                    break;
191            }
192            return true;
193        }
194
195        // unsupported $mode
196        return false;
197    }
198
199
200}
201
202