xref: /plugin/combo/syntax/note.php (revision 21913ab3235d516e2fa19c7e3929b555b3a2bda1)
1<?php
2
3
4// must be run within Dokuwiki
5use ComboStrap\Background;
6use ComboStrap\ColorUtility;
7use ComboStrap\PluginUtility;
8use ComboStrap\TagAttributes;
9
10if (!defined('DOKU_INC')) die();
11
12/**
13 * Class syntax_plugin_combo_note
14 * Implementation of a note
15 * called an alert in <a href="https://getbootstrap.com/docs/4.0/components/alerts/">bootstrap</a>
16 */
17class syntax_plugin_combo_note extends DokuWiki_Syntax_Plugin
18{
19
20    const TAG = "note";
21
22    /**
23     * Syntax Type.
24     *
25     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
26     * @see DokuWiki_Syntax_Plugin::getType()
27     */
28    function getType()
29    {
30        return 'container';
31    }
32
33    /**
34     * How Dokuwiki will add P element
35     *
36     * * 'normal' - The plugin can be used inside paragraphs
37     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
38     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
39     *
40     * @see DokuWiki_Syntax_Plugin::getPType()
41     */
42    function getPType()
43    {
44        return 'block';
45    }
46
47    /**
48     * @return array
49     * Allow which kind of plugin inside
50     *
51     * ************************
52     * This function has no effect because {@link SyntaxPlugin::accepts()} is used
53     * ************************
54     */
55    function getAllowedTypes()
56    {
57        return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs');
58    }
59
60
61    function getSort()
62    {
63        return 201;
64    }
65
66    public function accepts($mode)
67    {
68        /**
69         * header mode is disable to take over
70         * and replace it with {@link syntax_plugin_combo_title}
71         */
72        if ($mode == "header") {
73            return false;
74        }
75        return syntax_plugin_combo_preformatted::disablePreformatted($mode);
76
77    }
78
79
80    function connectTo($mode)
81    {
82
83        $pattern = PluginUtility::getContainerTagPattern(self::TAG);
84        $this->Lexer->addEntryPattern($pattern, $mode, PluginUtility::getModeForComponent($this->getPluginComponent()));
85    }
86
87
88    function postConnect()
89    {
90
91        $this->Lexer->addExitPattern('</' . self::TAG . '>', PluginUtility::getModeForComponent($this->getPluginComponent()));
92
93    }
94
95    function handle($match, $state, $pos, Doku_Handler $handler)
96    {
97
98        switch ($state) {
99
100            case DOKU_LEXER_ENTER :
101                $defaultAttributes = array("type" => "info");
102                $inlineAttributes = PluginUtility::getTagAttributes($match);
103                $attributes = PluginUtility::mergeAttributes($inlineAttributes, $defaultAttributes);
104                return array(
105                    PluginUtility::STATE => $state,
106                    PluginUtility::ATTRIBUTES => $attributes
107                );
108
109            case DOKU_LEXER_UNMATCHED :
110                return PluginUtility::handleAndReturnUnmatchedData(self::TAG, $match, $handler);
111
112            case DOKU_LEXER_EXIT :
113
114                // Important otherwise we don't get an exit in the render
115                return array(
116                    PluginUtility::STATE => $state
117                );
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            $state = $data[PluginUtility::STATE];
141            switch ($state) {
142                case DOKU_LEXER_ENTER :
143                    $attributes = TagAttributes::createFromCallStackArray($data[PluginUtility::ATTRIBUTES]);
144                    $attributes->addClassName("alert");
145                    $type = $attributes->getValue(TagAttributes::TYPE_KEY);
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                        $attributes->addClassName("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 (!$attributes->hasComponentAttribute(ColorUtility::COLOR)) {
162                            $attributes->addComponentAttributeValue(ColorUtility::COLOR, "#6c6400"); // lum - 51
163                        }
164                        if (!$attributes->hasComponentAttribute("border-color")) {
165                            $attributes->addComponentAttributeValue("border-color", "#FFF78c"); // lum - 186
166                        }
167                        if (!$attributes->hasComponentAttribute(Background::BACKGROUND_COLOR)) {
168                            $attributes->addComponentAttributeValue(Background::BACKGROUND_COLOR, "#fff79f"); // lum - 195
169                        }
170                    }
171
172                    $attributes->addHtmlAttributeValue("role", "note");
173                    $renderer->doc .= $attributes->toHtmlEnterTag('div');
174                    break;
175
176                case DOKU_LEXER_UNMATCHED :
177                    $renderer->doc .= PluginUtility::renderUnmatched($data);
178                    break;
179
180                case DOKU_LEXER_EXIT :
181                    $renderer->doc .= '</div>';
182                    break;
183            }
184            return true;
185        }
186
187        // unsupported $mode
188        return false;
189    }
190
191
192}
193
194