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::getModeForComponent($this->getPluginComponent())); 83 84 } 85 86 function postConnect() 87 { 88 89 $this->Lexer->addExitPattern('</' . self::TAG . '>', PluginUtility::getModeForComponent($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 if ($tag->isDescendantOf(syntax_plugin_combo_list::TAG)) { 124 $tagAttributes->addStyleDeclaration("margin-left", "auto"); 125 } 126 127 /** 128 * Type attributes 129 */ 130 $tagAttributes->addClassName("badge"); 131 $type = $tagAttributes->getType(); 132 if ($type != "tip") { 133 $tagAttributes->addClassName("alert-" . $type); 134 } else { 135 if (!$tagAttributes->hasComponentAttribute("background-color")) { 136 $tagAttributes->addStyleDeclaration("background-color","#fff79f"); // lum - 195 137 $tagAttributes->addClassName("text-dark"); 138 } 139 } 140 141 $rounded = $tagAttributes->getValueAndRemove(self::ATTRIBUTE_ROUNDED); 142 if (!empty($rounded)) { 143 $badgePillClass = "badge-pill"; 144 if (Bootstrap::getBootStrapMajorVersion() == Bootstrap::BootStrapFiveMajorVersion) { 145 // https://getbootstrap.com/docs/5.0/migration/#badges-1 146 $badgePillClass = "rounded-pill"; 147 } 148 $tagAttributes->addClassName($badgePillClass); 149 } 150 151 return array( 152 PluginUtility::STATE => $state, 153 PluginUtility::ATTRIBUTES => $tagAttributes->toCallStackArray() 154 ); 155 156 case DOKU_LEXER_UNMATCHED : 157 return PluginUtility::handleAndReturnUnmatchedData(self::TAG, $match, $handler); 158 159 case DOKU_LEXER_EXIT : 160 161 // Important otherwise we don't get an exit in the render 162 return array( 163 PluginUtility::STATE => $state 164 ); 165 166 167 } 168 return array(); 169 170 } 171 172 /** 173 * Render the output 174 * @param string $format 175 * @param Doku_Renderer $renderer 176 * @param array $data - what the function handle() return'ed 177 * @return boolean - rendered correctly? (however, returned value is not used at the moment) 178 * @see DokuWiki_Syntax_Plugin::render() 179 * 180 * 181 */ 182 function render($format, Doku_Renderer $renderer, $data) 183 { 184 if ($format == 'xhtml') { 185 186 /** @var Doku_Renderer_xhtml $renderer */ 187 $state = $data[PluginUtility::STATE]; 188 switch ($state) { 189 190 case DOKU_LEXER_ENTER : 191 192 PluginUtility::getSnippetManager()->attachCssSnippetForBar(self::TAG); 193 194 $attributes = $data[PluginUtility::ATTRIBUTES]; 195 $tagAttributes = TagAttributes::createFromCallStackArray($attributes, self::TAG); 196 $renderer->doc .= $tagAttributes->toHtmlEnterTag("span") . DOKU_LF; 197 break; 198 199 case DOKU_LEXER_UNMATCHED : 200 $renderer->doc .= PluginUtility::renderUnmatched($data); 201 break; 202 203 case DOKU_LEXER_EXIT : 204 $renderer->doc .= "</span>"; 205 break; 206 207 } 208 return true; 209 } 210 211 // unsupported $mode 212 return false; 213 } 214 215 216} 217 218