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