1<?php 2 3use dokuwiki\Extension\SyntaxPlugin; 4 5/** 6 * DokuWiki Plugin tagbutton (Syntax Component) 7 * 8 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 9 * @author Louis Ouellet <louis_ouellet@hotmail.com> 10 */ 11class syntax_plugin_tagbutton extends SyntaxPlugin 12{ 13 /** @inheritDoc */ 14 public function getType() 15 { 16 return 'substition'; 17 } 18 19 /** @inheritDoc */ 20 public function getPType() 21 { 22 return 'block'; 23 } 24 25 /** @inheritDoc */ 26 public function getSort() 27 { 28 return 20; 29 } 30 31 /** @inheritDoc */ 32 public function connectTo($mode) 33 { 34 $this->Lexer->addSpecialPattern('\{\{tag-button>.+?\}\}', $mode, 'plugin_tagbutton'); 35 } 36 37 /** @inheritDoc */ 38 public function handle($match, $state, $pos, Doku_Handler $handler) 39 { 40 // Remove the wrapping syntax 41 $match = substr($match, 13, -2); 42 43 // Split the tag, parameter, and label 44 list($tagParam, $label) = array_pad(explode('|', $match, 2), 2, null); 45 list($tag, $param) = array_pad(explode(':', $tagParam, 2), 2, null); 46 47 return array(trim($tag), trim($param), trim($label)); 48 } 49 50 /** @inheritDoc */ 51 public function render($mode, Doku_Renderer $renderer, $data) 52 { 53 if($mode === 'xhtml') { 54 global $ID; 55 56 // list($tag, $label) = $data; 57 list($tag, $param, $label) = $data; 58 $buttonId = 'tagbutton-' . md5($tag); 59 60 // If the label is null or empty, use the tag as the label 61 if ($label === null || $label === '') { 62 $label = $tag; 63 } 64 65 // Generate the button class based on the parameter 66 $buttonClass = 'tagbutton'; 67 if ($param) { 68 $buttonClass .= ' tag' . htmlspecialchars($param); 69 } 70 71 // Check if the configuration indicates to hide the button if the tag exists 72 if ($this->getConf('hide_if_exists')) { 73 $pageContent = rawWiki($ID); 74 75 // Check if the tag already exists in the page 76 if (preg_match('/\{\{tag>(.*?)\}\}/', $pageContent, $matches)) { 77 $existingTags = explode(' ', strtolower(trim($matches[1]))); 78 79 // If the tag already exists, do not render the button 80 if (in_array(strtolower($tag), $existingTags)) { 81 return true; 82 } 83 } 84 } 85 86 // Render the button if the tag doesn't exist or the configuration doesn't hide it 87 $renderer->doc .= '<button class="' . $buttonClass . '" id="' . $buttonId . '" data-tag="' . htmlspecialchars($tag) . '">' 88 . htmlspecialchars($label) . '</button>'; 89 } 90 return true; 91 } 92} 93