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 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 PluginUtility::PAYLOAD=> '</span>' 165 ); 166 167 168 } 169 return array(); 170 171 } 172 173 /** 174 * Render the output 175 * @param string $format 176 * @param Doku_Renderer $renderer 177 * @param array $data - what the function handle() return'ed 178 * @return boolean - rendered correctly? (however, returned value is not used at the moment) 179 * @see DokuWiki_Syntax_Plugin::render() 180 * 181 * 182 */ 183 function render($format, Doku_Renderer $renderer, $data) 184 { 185 if ($format == 'xhtml') { 186 187 /** @var Doku_Renderer_xhtml $renderer */ 188 $state = $data[PluginUtility::STATE]; 189 switch ($state) { 190 case DOKU_LEXER_EXIT : 191 case DOKU_LEXER_ENTER : 192 193 PluginUtility::getSnippetManager()->upsertCssSnippetForBar(self::TAG); 194 195 $renderer->doc .= $data[PluginUtility::PAYLOAD].DOKU_LF; 196 break; 197 198 case DOKU_LEXER_UNMATCHED : 199 $renderer->doc .= PluginUtility::renderUnmatched($data); 200 break; 201 202 } 203 return true; 204 } 205 206 // unsupported $mode 207 return false; 208 } 209 210 211} 212 213