xref: /plugin/combo/syntax/badge.php (revision 5f891b7e09648e05e78f5882f3fdde1e9df9b0f1)
1<?php
2
3
4// must be run within Dokuwiki
5use ComboStrap\PluginUtility;
6use ComboStrap\Tag;
7
8if (!defined('DOKU_INC')) die();
9
10/**
11 * Class syntax_plugin_combo_badge
12 * Implementation of a badge
13 * called an alert in <a href="https://getbootstrap.com/docs/4.0/components/badge/">bootstrap</a>
14 */
15class syntax_plugin_combo_badge extends DokuWiki_Syntax_Plugin
16{
17
18    const TAG = "badge";
19
20    const CONF_DEFAULT_ATTRIBUTES_KEY = 'defaultBadgeAttributes';
21
22    const ATTRIBUTE_TYPE = "type";
23    const ATTRIBUTE_ROUNDED = "rounded";
24
25    /**
26     * Syntax Type.
27     *
28     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
29     * @see https://www.dokuwiki.org/devel:syntax_plugins#syntax_types
30     * @see DokuWiki_Syntax_Plugin::getType()
31     */
32    function getType()
33    {
34        return 'formatting';
35    }
36
37    /**
38     * How Dokuwiki will add P element
39     *
40     *  * 'normal' - The plugin can be used inside paragraphs (inline or inside)
41     *  * 'block'  - Open paragraphs need to be closed before plugin output (box) - block should not be inside paragraphs
42     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
43     *
44     * @see DokuWiki_Syntax_Plugin::getPType()
45     * @see https://www.dokuwiki.org/devel:syntax_plugins#ptype
46     */
47    function getPType()
48    {
49        return 'normal';
50    }
51
52    /**
53     * @return array
54     * Allow which kind of plugin inside
55     *
56     * No one of array('baseonly','container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs')
57     * because we manage self the content and we call self the parser
58     *
59     * Return an array of one or more of the mode types {@link $PARSER_MODES} in Parser.php
60     */
61    function getAllowedTypes()
62    {
63        return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs');
64    }
65
66    /**
67     * @see Doku_Parser_Mode::getSort()
68     * the mode with the lowest sort number will win out
69     */
70    function getSort()
71    {
72        return 201;
73    }
74
75
76    function connectTo($mode)
77    {
78
79        $pattern = PluginUtility::getContainerTagPattern(self::TAG);
80        $this->Lexer->addEntryPattern($pattern, $mode, PluginUtility::getModeForComponent($this->getPluginComponent()));
81
82    }
83
84    function postConnect()
85    {
86
87        $this->Lexer->addExitPattern('</' . self::TAG . '>', PluginUtility::getModeForComponent($this->getPluginComponent()));
88
89    }
90
91    /**
92     *
93     * The handle function goal is to parse the matched syntax through the pattern function
94     * and to return the result for use in the renderer
95     * This result is always cached until the page is modified.
96     * @param string $match
97     * @param int $state
98     * @param int $pos - byte position in the original source file
99     * @param Doku_Handler $handler
100     * @return array|bool
101     * @see DokuWiki_Syntax_Plugin::handle()
102     *
103     */
104    function handle($match, $state, $pos, Doku_Handler $handler)
105    {
106
107        switch ($state) {
108
109            case DOKU_LEXER_ENTER :
110                $defaultConfValue = PluginUtility::parse2HTMLAttributes($this->getConf(self::CONF_DEFAULT_ATTRIBUTES_KEY));
111                $originalAttributes = PluginUtility::getTagAttributes($match);
112                $originalAttributes = PluginUtility::mergeAttributes($originalAttributes,$defaultConfValue);
113
114                /**
115                 * Context Rendering attributes
116                 */
117                $attributesToRender = $originalAttributes;
118                $tag = new Tag(self::TAG,$originalAttributes,$state,$handler);
119
120                if($tag->isDescendantOf(syntax_plugin_combo_list::TAG)){
121                    PluginUtility::addStyleProperty("margin-left","auto",$attributesToRender);
122                }
123
124
125                /**
126                 * Type attributes
127                 */
128                $classValue = "badge";
129                $type = $attributesToRender[self::ATTRIBUTE_TYPE];
130                if (empty($type)) {
131                    $type = "info";
132                }
133                if ($type != "tip") {
134                    $classValue .= " alert-" . $type;
135                } else {
136                    if (!array_key_exists("background-color", $attributesToRender)) {
137                        $attributesToRender["background-color"] = "#fff79f"; // lum - 195
138                    }
139                }
140
141                PluginUtility::addClass2Attributes($classValue,$attributesToRender);
142
143                $rounded = $attributesToRender[self::ATTRIBUTE_ROUNDED];
144                if (!empty($rounded)){
145                    $attributesToRender["class"] .= " badge-pill";
146                    unset($attributesToRender[self::ATTRIBUTE_ROUNDED]);
147                }
148
149                $html = '<span ' . PluginUtility::array2HTMLAttributes($attributesToRender) . '>';
150
151                return array(
152                    PluginUtility::STATE => $state,
153                    PluginUtility::ATTRIBUTES => $originalAttributes,
154                    PluginUtility::PAYLOAD => $html);
155
156            case DOKU_LEXER_UNMATCHED :
157                return array(
158                    PluginUtility::STATE=> $state,
159                    PluginUtility::PAYLOAD=> PluginUtility::escape($match)
160                );
161
162            case DOKU_LEXER_EXIT :
163
164                // Important otherwise we don't get an exit in the render
165                return array(
166                    PluginUtility::STATE=> $state,
167                    PluginUtility::PAYLOAD=> '</span>'
168                );
169
170
171        }
172        return array();
173
174    }
175
176    /**
177     * Render the output
178     * @param string $format
179     * @param Doku_Renderer $renderer
180     * @param array $data - what the function handle() return'ed
181     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
182     * @see DokuWiki_Syntax_Plugin::render()
183     *
184     *
185     */
186    function render($format, Doku_Renderer $renderer, $data)
187    {
188        if ($format == 'xhtml') {
189
190            /** @var Doku_Renderer_xhtml $renderer */
191            $state = $data[PluginUtility::STATE];
192            switch ($state) {
193                case DOKU_LEXER_EXIT :
194                case DOKU_LEXER_ENTER :
195
196                    PluginUtility::getSnippetManager()->addCssSnippetOnlyOnce(self::TAG);
197
198                    $renderer->doc .= $data[PluginUtility::PAYLOAD].DOKU_LF;
199                    break;
200
201                case DOKU_LEXER_UNMATCHED :
202                    $renderer->doc .= $data[PluginUtility::PAYLOAD];
203                    break;
204
205            }
206            return true;
207        }
208
209        // unsupported $mode
210        return false;
211    }
212
213
214}
215
216