1<?php 2/** 3 * DokuWiki Plugin mermaid (Syntax Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Robert Weinmeister <develop@weinmeister.org> 7 */ 8 9if (!defined('DOKU_INC')) die(); 10 11use dokuwiki\Parsing\Parser; 12 13class syntax_plugin_mermaid extends \dokuwiki\Extension\SyntaxPlugin 14{ 15 const DOKUWIKI_LINK_START_MERMAID = '<code>DOKUWIKILINKSTARTMERMAID</code>'; 16 const DOKUWIKI_LINK_END_MERMAID = '<code>DOKUWIKILINKENDMERMAID</code>'; 17 const DOKUWIKI_LINK_SPLITTER ='--'; 18 const DOKUWIKI_SVG_SAVE = '<svg fill="#000000" viewBox="0 0 52 52" enable-background="new 0 0 52 52" xml:space="preserve" style="width: 24px; height: 24px;"><path d="M37.1,4v13.6c0,1-0.8,1.9-1.9,1.9H13.9c-1,0-1.9-0.8-1.9-1.9V4H8C5.8,4,4,5.8,4,8v36c0,2.2,1.8,4,4,4h36 c2.2,0,4-1.8,4-4V11.2L40.8,4H37.1z M44.1,42.1c0,1-0.8,1.9-1.9,1.9H9.9c-1,0-1.9-0.8-1.9-1.9V25.4c0-1,0.8-1.9,1.9-1.9h32.3 c1,0,1.9,0.8,1.9,1.9V42.1z"/><path d="M24.8,13.6c0,1,0.8,1.9,1.9,1.9h4.6c1,0,1.9-0.8,1.9-1.9V4h-8.3L24.8,13.6L24.8,13.6z"/></svg>'; 19 const DOKUWIKI_SVG_LOCKED = '<svg viewBox="0 0 16 16" fill="none" style="width: 24px; height: 24px;"><path fill-rule="evenodd" clip-rule="evenodd" d="M4 6V4C4 1.79086 5.79086 0 8 0C10.2091 0 12 1.79086 12 4V6H14V16H2V6H4ZM6 4C6 2.89543 6.89543 2 8 2C9.10457 2 10 2.89543 10 4V6H6V4ZM7 13V9H9V13H7Z" fill="#000000"/></svg>'; 20 const DOKUWIKI_SVG_UNLOCKED = '<svg style="width: 24px; height: 24px;" viewBox="0 0 16 16" fill="none"><path fill-rule="evenodd" clip-rule="evenodd" d="M11.5 2C10.6716 2 10 2.67157 10 3.5V6H13V16H1V6H8V3.5C8 1.567 9.567 0 11.5 0C13.433 0 15 1.567 15 3.5V4H13V3.5C13 2.67157 12.3284 2 11.5 2ZM9 10H5V12H9V10Z" fill="#000000"/></svg>'; 21 22 private $mermaidCounter = -1; 23 private $mermaidContent; 24 private $currentMermaidIsLocked = false; 25 private $mermaidContentIfLocked; 26 27 function enable_gantt_links($instructions) 28 { 29 $modified_instructions = $instructions; 30 31 for ($i = 0; $i < count($modified_instructions); $i++) 32 { 33 if (in_array($modified_instructions[$i][0], ["externallink", "internallink"])) 34 { 35 // use the appropriate link 36 $link = $modified_instructions[$i][0] == "externallink" ? $modified_instructions[$i][1][0] : wl($modified_instructions[$i][1][0], '', true); 37 38 // change link here to just the name of the link 39 $modified_instructions[$i][0]= "cdata"; 40 if(!is_null($modified_instructions[$i][1][1])) 41 { 42 unset($modified_instructions[$i][1][0]); 43 } 44 45 // insert the click event 46 if (preg_match('/(?<=:\s)\S+(?=,)/', $modified_instructions[$i+1][1][0], $output_array)) 47 { 48 $click_reference = $output_array[0]; 49 } 50 array_splice($modified_instructions, $i + 2, 0, [["cdata", ["\nclick ".$click_reference." href \"".$link."\"\n"]]]); 51 52 // encode colons 53 $modified_instructions[$i][1][0] = str_replace(":", "#colon;", $modified_instructions[$i][1][0]); 54 } 55 } 56 57 return $modified_instructions; 58 } 59 60 function protect_brackets_from_dokuwiki($text) 61 { 62 $splitText = explode(self::DOKUWIKI_LINK_SPLITTER, $text); 63 foreach ($splitText as $key => $line) 64 { 65 $splitText[$key] = preg_replace('/(?<!["\[(\s])(\[\[)(.*)(\]\])/', self::DOKUWIKI_LINK_START_MERMAID . '$2' . self::DOKUWIKI_LINK_END_MERMAID, $line); 66 } 67 $text = implode(self::DOKUWIKI_LINK_SPLITTER, $splitText); 68 return $text; 69 } 70 71 function remove_protection_of_brackets_from_dokuwiki($text) 72 { 73 return str_replace(self::DOKUWIKI_LINK_START_MERMAID, '[[', str_replace(self::DOKUWIKI_LINK_END_MERMAID, ']]', $text)); 74 } 75 76 /** @inheritDoc */ 77 function getType() 78 { 79 return 'container'; 80 } 81 82 /** @inheritDoc */ 83 function getSort() 84 { 85 return 150; 86 } 87 88 /** 89 * Connect lookup pattern to lexer. 90 * 91 * @param string $mode Parser mode 92 */ 93 function connectTo($mode) 94 { 95 $this->Lexer->addEntryPattern('<mermaid.*?>(?=.*?</mermaid>)',$mode,'plugin_mermaid'); 96 } 97 98 function postConnect() 99 { 100 $this->Lexer->addExitPattern('</mermaid>','plugin_mermaid'); 101 } 102 103 /** 104 * Handle matches of the Mermaid syntax 105 */ 106 function handle($match, $state, $pos, Doku_Handler $handler) 107 { 108 switch ($state) 109 { 110 case DOKU_LEXER_ENTER: 111 return array($state, $match); 112 case DOKU_LEXER_UNMATCHED: 113 return array($state, $match); 114 case DOKU_LEXER_EXIT: 115 return array($state, ''); 116 } 117 return false; 118 } 119 120 /** 121 * Render xhtml output or metadata 122 */ 123 function render($mode, Doku_Renderer $renderer, $indata) 124 { 125 if($mode == 'xhtml'){ 126 list($state, $match) = $indata; 127 switch ($state) { 128 case DOKU_LEXER_ENTER: 129 $this->mermaidCounter++; 130 $values = explode(" ", $match); 131 $divwidth = count($values) < 2 ? 'auto' : $values[1]; 132 $divheight = count($values) < 3 ? 'auto' : substr($values[2], 0, -1); 133 $this->mermaidContent .= '<div id="mermaidContainer'.$this->mermaidCounter.'" style="position: relative; width:'.$divwidth.'; height:'.$divheight.'">'; 134 $this->mermaidContentIfLocked = $this->mermaidContent . '<span class="mermaidlocked" id=mermaidContent'.$this->mermaidCounter.' style="width:'.$divwidth.'; height:'.$divheight.'">'; 135 $this->mermaidContent .= '<span class="mermaid" id=mermaidContent'.$this->mermaidCounter.' style="width:'.$divwidth.'; height:'.$divheight.'">'; 136 break; 137 case DOKU_LEXER_UNMATCHED: 138 $explodedMatch = explode("\n", $match); 139 140 if(str_starts_with($explodedMatch[1], '%%<svg')) 141 { 142 $this->currentMermaidIsLocked = true; 143 $this->mermaidContent = $this->mermaidContentIfLocked . substr($explodedMatch[1], 2); 144 break; 145 } 146 else 147 { 148 $this->currentMermaidIsLocked = false; 149 } 150 151 $israwmode = isset($explodedMatch[1]) && strpos($explodedMatch[1], 'raw') !== false; 152 if($israwmode) 153 { 154 array_shift($explodedMatch); 155 array_shift($explodedMatch); 156 $actualContent = implode("\n", $explodedMatch); 157 $this->mermaidContent .= $actualContent; 158 } 159 else 160 { 161 $instructions = $this->p_get_instructions($this->protect_brackets_from_dokuwiki($match)); 162 if (strpos($instructions[2][1][0], "gantt")) 163 { 164 $instructions = $this->enable_gantt_links($instructions); 165 } 166 $xhtml = $this->remove_protection_of_brackets_from_dokuwiki($this->p_render($instructions)); 167 $this->mermaidContent .= preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $xhtml); 168 } 169 break; 170 case DOKU_LEXER_EXIT: 171 $this->mermaidContent .= "\r\n</span>"; 172 $this->mermaidContent .= '<fieldset id="mermaidFieldset'.$this->mermaidCounter.'" style="position: absolute; top: 0; left: 0; display: none; width:auto; border: none">'; 173 $this->mermaidContent .= '<button id="mermaidButtonSave'.$this->mermaidCounter.'" style="z-index: 10; display: block; padding: 0; margin: 0; border: none; background: none; width: 24px; height: 24px;">'.self::DOKUWIKI_SVG_SAVE.'</button>'; 174 $this->mermaidContent .= '<button id="mermaidButtonPermanent'.$this->mermaidCounter.'" style="z-index: 10; display: block; padding: 0; margin: 0; border: none; background: none; width: 24px; height: 24px;">'.($this->currentMermaidIsLocked ? self::DOKUWIKI_SVG_LOCKED : self::DOKUWIKI_SVG_UNLOCKED).'</button>'; 175 $this->mermaidContent .= '</fieldset></div>'; 176 177 $renderer->doc .= $this->mermaidContent; 178 $this->mermaidContent = ''; 179 break; 180 } 181 return true; 182 } 183 return false; 184 } 185 186 /* 187 * Get the parser instructions suitable for the mermaid 188 * 189 */ 190 function p_get_instructions($text) 191 { 192 //import parser classes and mode definitions 193 require_once DOKU_INC . 'inc/parser/parser.php'; 194 195 // https://www.dokuwiki.org/devel:parser 196 // https://www.dokuwiki.org/devel:parser#basic_invocation 197 // Create the parser and the handler 198 $Parser = new Parser(new Doku_Handler()); 199 200 $modes = array(); 201 202 // add default modes 203 $std_modes = array( 'internallink', 'media', 'externallink'); 204 205 foreach($std_modes as $m) 206 { 207 $class = 'dokuwiki\\Parsing\\ParserMode\\'.ucfirst($m); 208 $obj = new $class(); 209 $modes[] = array( 210 'sort' => $obj->getSort(), 211 'mode' => $m, 212 'obj' => $obj 213 ); 214 } 215 216 // add formatting modes 217 $fmt_modes = array( 'strong', 'emphasis', 'underline', 'monospace', 'subscript', 'superscript', 'deleted'); 218 foreach($fmt_modes as $m) 219 { 220 $obj = new \dokuwiki\Parsing\ParserMode\Formatting($m); 221 $modes[] = array( 222 'sort' => $obj->getSort(), 223 'mode' => $m, 224 'obj' => $obj 225 ); 226 } 227 228 //add modes to parser 229 foreach($modes as $mode) 230 { 231 $Parser->addMode($mode['mode'],$mode['obj']); 232 } 233 234 // Do the parsing 235 $p = $Parser->parse($text); 236 237 return $p; 238 } 239 240 public function p_render($instructions) 241 { 242 $Renderer = p_get_renderer('mermaid'); 243 244 // Loop through the instructions 245 foreach ($instructions as $instruction) { 246 if(method_exists($Renderer, $instruction[0])){ 247 call_user_func_array(array(&$Renderer, $instruction[0]), $instruction[1] ? $instruction[1] : array()); 248 } 249 } 250 251 return $Renderer->doc; 252 } 253}