xref: /template/strap/syntax/note.php (revision 007225e5fb2d3f64edaccd3bd447ca26effb9d68)
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     * No one of array('baseonly','container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs')
49     * because we manage self the content and we call self the parser
50     *
51     * Return an array of one or more of the mode types {@link $PARSER_MODES} in Parser.php
52     */
53    function getAllowedTypes()
54    {
55        return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs');
56    }
57
58    function getSort()
59    {
60        return 201;
61    }
62
63
64    function connectTo($mode)
65    {
66
67        $pattern = PluginUtility::getContainerTagPattern(self::TAG);
68        $this->Lexer->addEntryPattern($pattern, $mode, PluginUtility::getModeForComponent($this->getPluginComponent()));
69    }
70
71
72    function postConnect()
73    {
74
75        $this->Lexer->addExitPattern('</' . self::TAG . '>', PluginUtility::getModeForComponent($this->getPluginComponent()));
76
77    }
78
79    function handle($match, $state, $pos, Doku_Handler $handler)
80    {
81
82        switch ($state) {
83
84            case DOKU_LEXER_ENTER :
85                $defaultAttributes = array("type" => "info");
86                $inlineAttributes = PluginUtility::getTagAttributes($match);
87                $attributes = PluginUtility::mergeAttributes($inlineAttributes, $defaultAttributes);
88                return array($state, $attributes);
89
90            case DOKU_LEXER_UNMATCHED :
91                return array($state, $match);
92
93            case DOKU_LEXER_EXIT :
94
95                // Important otherwise we don't get an exit in the render
96                return array($state, '');
97
98
99        }
100        return array();
101
102    }
103
104    /**
105     * Render the output
106     * @param string $format
107     * @param Doku_Renderer $renderer
108     * @param array $data - what the function handle() return'ed
109     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
110     * @see DokuWiki_Syntax_Plugin::render()
111     *
112     *
113     */
114    function render($format, Doku_Renderer $renderer, $data)
115    {
116        if ($format == 'xhtml') {
117
118            /** @var Doku_Renderer_xhtml $renderer */
119            list($state, $payload) = $data;
120            switch ($state) {
121                case DOKU_LEXER_ENTER :
122                    $attributes = $payload;
123                    $classValue = "alert";
124                    $type = $attributes["type"];
125                    // Switch for the color
126                    switch ($type) {
127                        case "important":
128                            $type = "warning";
129                            break;
130                        case "warning":
131                            $type = "danger";
132                            break;
133                    }
134
135                    if ($type != "tip") {
136                        $classValue .= " alert-" . $type;
137                    } else {
138                        // There is no alert-tip color
139                        // base color was background color and we have modified the luminance
140                        if (!array_key_exists("color", $attributes)) {
141                            $attributes["color"] = "#6c6400"; // lum - 51
142                        }
143                        if (!array_key_exists("border-color", $attributes)) {
144                            $attributes["border-color"] = "#FFF78c"; // lum - 186
145                        }
146                        if (!array_key_exists("background-color", $attributes)) {
147                            $attributes["background-color"] = "#fff79f"; // lum - 195
148                        }
149                    }
150
151                    if (array_key_exists("class", $attributes)) {
152                        $attributes["class"] .= " {$classValue}";
153                    } else {
154                        $attributes["class"] = "{$classValue}";
155                    }
156
157                    $renderer->doc .= '<div ' . PluginUtility::array2HTMLAttributes($attributes) . ' role="note">';
158                    break;
159
160                case DOKU_LEXER_UNMATCHED :
161                    $renderer->doc .= $renderer->_xmlEntities($payload);
162                    break;
163
164                case DOKU_LEXER_EXIT :
165                    $renderer->doc .= '</div>';
166                    break;
167            }
168            return true;
169        }
170
171        // unsupported $mode
172        return false;
173    }
174
175
176}
177
178