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