1<?php 2 3 4// must be run within Dokuwiki 5use ComboStrap\Bootstrap; 6use ComboStrap\ColorRgb; 7use ComboStrap\ExceptionCombo; 8use ComboStrap\LogUtility; 9use ComboStrap\PluginUtility; 10use ComboStrap\Site; 11use ComboStrap\Skin; 12use ComboStrap\Tag; 13use ComboStrap\TagAttributes; 14 15if (!defined('DOKU_INC')) die(); 16 17/** 18 * Class syntax_plugin_combo_badge 19 * Implementation of a badge 20 * called an alert in <a href="https://getbootstrap.com/docs/4.0/components/badge/">bootstrap</a> 21 */ 22class syntax_plugin_combo_badge extends DokuWiki_Syntax_Plugin 23{ 24 25 const TAG = "badge"; 26 27 const CONF_DEFAULT_ATTRIBUTES_KEY = 'defaultBadgeAttributes'; 28 29 const ATTRIBUTE_TYPE = "type"; 30 const ATTRIBUTE_ROUNDED = "rounded"; 31 32 /** 33 * Syntax Type. 34 * 35 * Needs to return one of the mode types defined in $PARSER_MODES in parser.php 36 * @see https://www.dokuwiki.org/devel:syntax_plugins#syntax_types 37 * @see DokuWiki_Syntax_Plugin::getType() 38 */ 39 function getType() 40 { 41 return 'formatting'; 42 } 43 44 /** 45 * How Dokuwiki will add P element 46 * 47 * * 'normal' - The plugin can be used inside paragraphs (inline or inside) 48 * * 'block' - Open paragraphs need to be closed before plugin output (box) - block should not be inside paragraphs 49 * * 'stack' - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs 50 * 51 * @see DokuWiki_Syntax_Plugin::getPType() 52 * @see https://www.dokuwiki.org/devel:syntax_plugins#ptype 53 */ 54 function getPType() 55 { 56 return 'normal'; 57 } 58 59 /** 60 * @return array 61 * Allow which kind of plugin inside 62 * 63 * No one of array('baseonly','container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs') 64 * because we manage self the content and we call self the parser 65 * 66 * Return an array of one or more of the mode types {@link $PARSER_MODES} in Parser.php 67 */ 68 function getAllowedTypes() 69 { 70 return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs'); 71 } 72 73 /** 74 * @see Doku_Parser_Mode::getSort() 75 * the mode with the lowest sort number will win out 76 */ 77 function getSort() 78 { 79 return 201; 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 /** 99 * 100 * The handle function goal is to parse the matched syntax through the pattern function 101 * and to return the result for use in the renderer 102 * This result is always cached until the page is modified. 103 * @param string $match 104 * @param int $state 105 * @param int $pos - byte position in the original source file 106 * @param Doku_Handler $handler 107 * @return array|bool 108 * @see DokuWiki_Syntax_Plugin::handle() 109 * 110 */ 111 function handle($match, $state, $pos, Doku_Handler $handler) 112 { 113 114 switch ($state) { 115 116 case DOKU_LEXER_ENTER : 117 118 $defaultConfValue = PluginUtility::parseAttributes($this->getConf(self::CONF_DEFAULT_ATTRIBUTES_KEY)); 119 120 $knownTypes = ["primary", "secondary", "success", "danger", "warning", "info", "tip", "light", "dark"]; 121 $tagAttributes = TagAttributes::createFromTagMatch($match, $defaultConfValue, $knownTypes); 122 123 124 /** 125 * Brand and tip colors 126 */ 127 $tagAttributes->addClassName("badge"); 128 $type = $tagAttributes->getType(); 129 $color = null; 130 switch ($type) { 131 case "tip": 132 $color = ColorRgb::TIP_COLOR; 133 break; 134 case ColorRgb::PRIMARY_VALUE: 135 $color = Site::getPrimaryColorValue(); 136 break; 137 case ColorRgb::SECONDARY_VALUE: 138 $color = Site::getSecondaryColorValue(); 139 break; 140 default: 141 } 142 $colorObject = null; 143 if ($color !== null) { 144 try { 145 $colorObject = ColorRgb::createFromString($color); 146 } catch (ExceptionCombo $e) { 147 LogUtility::msg("The color value ($color) for the badge type ($type) is not valid. Error: {$e->getMessage()}"); 148 } 149 } 150 if ($colorObject !== null) { 151 /** 152 * https://getbootstrap.com/docs/5.0/components/alerts/ 153 * $alert-bg-scale: -80%; 154 * $alert-border-scale: -70%; 155 * $alert-color-scale: 40%; 156 */ 157 $backgroundColor = $tagAttributes->getValue(ColorRgb::BACKGROUND_COLOR); 158 if ($backgroundColor === null) { 159 try { 160 $backgroundColor = $colorObject 161 ->scale(-80) 162 ->toHsl() 163 ->setLightness(80) 164 ->toRgb() 165 ->toCssValue(); 166 } catch (ExceptionCombo $e) { 167 LogUtility::msg("Error while trying to set the lightness for the badge background color"); 168 $backgroundColor = $colorObject 169 ->scale(-80) 170 ->toCssValue(); 171 } 172 $tagAttributes->addStyleDeclarationIfNotSet(ColorRgb::BACKGROUND_COLOR, $backgroundColor); 173 } 174 if (!$tagAttributes->hasComponentAttribute(ColorRgb::BORDER_COLOR)) { 175 $borderColor = $colorObject->scale(-70)->toCssValue(); 176 $tagAttributes->addStyleDeclarationIfNotSet(ColorRgb::BORDER_COLOR, $borderColor); 177 } 178 if (!$tagAttributes->hasComponentAttribute(ColorRgb::COLOR)) { 179 try { 180 $textColor = $colorObject 181 ->scale(40) 182 ->toMinimumContrastRatio($backgroundColor) 183 ->toCssValue(); 184 } catch (ExceptionCombo $e) { 185 LogUtility::msg("Error while scaling the text color ($color) for the badge type ($type). Error: {$e->getMessage()}"); 186 $textColor = $colorObject 187 ->scale(40) 188 ->toCssValue(); 189 } 190 $tagAttributes->addStyleDeclarationIfNotSet(ColorRgb::COLOR, $textColor); 191 } 192 } else { 193 $tagAttributes->addClassName("alert-" . $type); 194 } 195 196 $rounded = $tagAttributes->getValueAndRemove(self::ATTRIBUTE_ROUNDED); 197 if (!empty($rounded)) { 198 $badgePillClass = "badge-pill"; 199 if (Bootstrap::getBootStrapMajorVersion() == Bootstrap::BootStrapFiveMajorVersion) { 200 // https://getbootstrap.com/docs/5.0/migration/#badges-1 201 $badgePillClass = "rounded-pill"; 202 } 203 $tagAttributes->addClassName($badgePillClass); 204 } 205 206 return array( 207 PluginUtility::STATE => $state, 208 PluginUtility::ATTRIBUTES => $tagAttributes->toCallStackArray() 209 ); 210 211 case DOKU_LEXER_UNMATCHED : 212 return PluginUtility::handleAndReturnUnmatchedData(self::TAG, $match, $handler); 213 214 case DOKU_LEXER_EXIT : 215 216 // Important otherwise we don't get an exit in the render 217 return array( 218 PluginUtility::STATE => $state 219 ); 220 221 222 } 223 return array(); 224 225 } 226 227 /** 228 * Render the output 229 * @param string $format 230 * @param Doku_Renderer $renderer 231 * @param array $data - what the function handle() return'ed 232 * @return boolean - rendered correctly? (however, returned value is not used at the moment) 233 * @see DokuWiki_Syntax_Plugin::render() 234 * 235 * 236 */ 237 function render($format, Doku_Renderer $renderer, $data) 238 { 239 if ($format == 'xhtml') { 240 241 /** @var Doku_Renderer_xhtml $renderer */ 242 $state = $data[PluginUtility::STATE]; 243 switch ($state) { 244 245 case DOKU_LEXER_ENTER : 246 247 PluginUtility::getSnippetManager()->attachCssInternalStyleSheetForSlot(self::TAG); 248 249 $attributes = $data[PluginUtility::ATTRIBUTES]; 250 $tagAttributes = TagAttributes::createFromCallStackArray($attributes, self::TAG); 251 // badge on boostrap does not allow 252 $tagAttributes->addStyleDeclarationIfNotSet("white-space", "normal"); 253 $renderer->doc .= $tagAttributes->toHtmlEnterTag("span") . DOKU_LF; 254 break; 255 256 case DOKU_LEXER_UNMATCHED : 257 $renderer->doc .= PluginUtility::renderUnmatched($data); 258 break; 259 260 case DOKU_LEXER_EXIT : 261 $renderer->doc .= "</span>"; 262 break; 263 264 } 265 return true; 266 } 267 268 // unsupported $mode 269 return false; 270 } 271 272 273} 274 275