xref: /plugin/combo/syntax/note.php (revision 4cadd4f8c541149bdda95f080e38a6d4e3a640ca)
1<?php
2
3
4// must be run within Dokuwiki
5use ComboStrap\Background;
6use ComboStrap\CallStack;
7use ComboStrap\ColorRgb;
8use ComboStrap\Dimension;
9use ComboStrap\PluginUtility;
10use ComboStrap\TagAttributes;
11
12if (!defined('DOKU_INC')) die();
13
14/**
15 * Class syntax_plugin_combo_note
16 * Implementation of a note
17 * called an alert in <a href="https://getbootstrap.com/docs/4.0/components/alerts/">bootstrap</a>
18 */
19class syntax_plugin_combo_note extends DokuWiki_Syntax_Plugin
20{
21
22    const TAG = "note";
23    const COMPONENT = "combo_note";
24
25    /**
26     * Syntax Type.
27     *
28     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
29     * @see DokuWiki_Syntax_Plugin::getType()
30     */
31    function getType()
32    {
33        return 'container';
34    }
35
36    /**
37     * How Dokuwiki will add P element
38     *
39     * * 'normal' - The plugin can be used inside paragraphs
40     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
41     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
42     *
43     * @see DokuWiki_Syntax_Plugin::getPType()
44     */
45    function getPType()
46    {
47        return 'stack';
48    }
49
50    /**
51     * @return array
52     * Allow which kind of plugin inside
53     *
54     * ************************
55     * This function has no effect because {@link SyntaxPlugin::accepts()} is used
56     * ************************
57     */
58    function getAllowedTypes()
59    {
60        return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs');
61    }
62
63
64    function getSort()
65    {
66        return 201;
67    }
68
69    public function accepts($mode)
70    {
71        /**
72         * header mode is disable to take over
73         * and replace it with {@link syntax_plugin_combo_heading}
74         */
75        if ($mode == "header") {
76            return false;
77        }
78        return syntax_plugin_combo_preformatted::disablePreformatted($mode);
79
80    }
81
82
83    function connectTo($mode)
84    {
85
86        $pattern = PluginUtility::getContainerTagPattern(self::TAG);
87        $this->Lexer->addEntryPattern($pattern, $mode, PluginUtility::getModeFromTag($this->getPluginComponent()));
88    }
89
90
91    function postConnect()
92    {
93
94        $this->Lexer->addExitPattern('</' . self::TAG . '>', PluginUtility::getModeFromTag($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                $callStack = CallStack::createFromHandler($handler);
118                Dimension::addScrollToggleOnClickIfNoControl($callStack);
119
120
121                // Important otherwise we don't get an exit in the render
122                return array(
123                    PluginUtility::STATE => $state
124                );
125
126
127        }
128        return array();
129
130    }
131
132    /**
133     * Render the output
134     * @param string $format
135     * @param Doku_Renderer $renderer
136     * @param array $data - what the function handle() return'ed
137     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
138     * @see DokuWiki_Syntax_Plugin::render()
139     *
140     *
141     */
142    function render($format, Doku_Renderer $renderer, $data)
143    {
144        if ($format == 'xhtml') {
145
146            /** @var Doku_Renderer_xhtml $renderer */
147            $state = $data[PluginUtility::STATE];
148            switch ($state) {
149                case DOKU_LEXER_ENTER :
150                    PluginUtility::getSnippetManager()->attachCssInternalStyleSheetForSlot(self::TAG);
151                    $attributes = TagAttributes::createFromCallStackArray($data[PluginUtility::ATTRIBUTES], self::TAG);
152                    $attributes->addClassName("alert");
153                    $type = $attributes->getValue(TagAttributes::TYPE_KEY);
154                    // Switch for the color
155                    switch ($type) {
156                        case "important":
157                            $type = "warning";
158                            break;
159                        case "warning":
160                            $type = "danger";
161                            break;
162                    }
163
164                    if ($type != "tip") {
165                        $attributes->addClassName("alert-" . $type);
166                    } else {
167                        // There is no alert-tip color
168                        // base color was background color and we have modified the luminance
169                        if (!$attributes->hasComponentAttribute(ColorRgb::COLOR)) {
170                            $attributes->addComponentAttributeValue(ColorRgb::COLOR, "#6c6400"); // lum - 51
171                        }
172                        if (!$attributes->hasComponentAttribute("border-color")) {
173                            $attributes->addComponentAttributeValue("border-color", "#FFF78c"); // lum - 186
174                        }
175                        if (!$attributes->hasComponentAttribute(Background::BACKGROUND_COLOR)) {
176                            $attributes->addComponentAttributeValue(Background::BACKGROUND_COLOR, "#fff79f"); // lum - 195
177                        }
178                    }
179
180                    $attributes->addOutputAttributeValue("role", "note");
181                    $renderer->doc .= $attributes->toHtmlEnterTag('div');
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