1<?php 2 3use dokuwiki\plugin\diagrams\Diagrams; 4 5/** 6 * Class syntax_plugin_diagrams 7 */ 8class syntax_plugin_diagrams_mediafile extends DokuWiki_Syntax_Plugin 9{ 10 11 /** 12 * @inheritdoc 13 */ 14 public function getType() 15 { 16 return 'substition'; 17 } 18 19 /** 20 * @inheritdoc 21 */ 22 public function getSort() 23 { 24 return 319; 25 } 26 27 /** 28 * @inheritdoc 29 */ 30 public function connectTo($mode) 31 { 32 // only register if mediafile mode is enabled 33 if (!($this->getConf('mode') & Diagrams::MODE_MEDIA)) return; 34 35 // grab all SVG images 36 $this->Lexer->addSpecialPattern('\{\{[^\}]+(?:\.svg)[^\}]*?\}\}', $mode, 'plugin_diagrams_mediafile'); 37 } 38 39 /** 40 * Parse SVG syntax into media data 41 * 42 * @param string $match 43 * @param int $state 44 * @param int $pos 45 * @param Doku_Handler $handler 46 * @return array|bool 47 */ 48 public function handle($match, $state, $pos, Doku_Handler $handler) 49 { 50 $data = Doku_Handler_Parse_Media($match); 51 52 /** @var helper_plugin_diagrams $helper */ 53 $helper = plugin_load('helper', 'diagrams'); 54 if (!$data['type'] == 'internalmedia' || !$helper->isDiagramFile(mediaFN($data['src']))) { 55 // This is not a local diagrams file, but some other SVG media file 56 $handler->media($match, $state, $pos); 57 return false; 58 } 59 60 $data['url'] = ml($data['src'], ['cache' => 'nocache'], true, '&'); 61 return $data; 62 } 63 64 /** 65 * Handle rewrites made by the move plugin 66 * 67 * @param string $match 68 * @param int $state 69 * @param int $pos 70 * @param string $plugin 71 * @param helper_plugin_move_handler $handler 72 * @return void 73 */ 74 public function handleMove($match, $state, $pos, $plugin, $handler) 75 { 76 if ($plugin !== 'diagrams_mediafile') return; 77 78 $handler->media($match, $state, $pos); 79 } 80 81 /** 82 * Render the diagram SVG as <object> instead of <img> to allow links, 83 * except when rendering to a PDF 84 * 85 * @param string $format 86 * @param Doku_Renderer $renderer 87 * @param array $data 88 * @return bool 89 */ 90 public function render($format, Doku_Renderer $renderer, $data) 91 { 92 if ($format === 'metadata') { 93 $renderer->internalmedia($data['src']); 94 return true; 95 } 96 if ($format !== 'xhtml') { 97 return false; 98 } 99 100 // check for cached PNG 101 $cachefile = $this->getCachedPNG($data); 102 103 if (is_a($renderer, 'renderer_plugin_dw2pdf')) { 104 $imageAttributes = [ 105 'class' => 'media', 106 'width' => empty($data['width']) ? '' : $data['width'], 107 'height' => empty($data['height']) ? '' : $data['height'], 108 'title' => $data['title'] ?? '', 109 'align' => $data['align'], 110 'src' => $data['url'], 111 ]; 112 113 // if a PNG cache exists, use it instead of the real URL 114 if ($cachefile) { 115 $imageAttributes['src'] = 'dw2pdf://' . $cachefile; 116 } 117 118 $renderer->doc .= '<img ' . buildAttributes($imageAttributes) . '/>'; 119 } else { 120 $wrapperAttributes = []; 121 $wrapperAttributes['title'] = $data['title'] ?? ''; 122 $wrapperAttributes['class'] = 'media diagrams-svg-wrapper media' . $data['align']; 123 124 $imageAttributes = []; 125 $imageAttributes['class'] = 'diagrams-svg'; 126 $imageAttributes['data'] = $data['url']; 127 $imageAttributes['data-id'] = cleanID($data['src'] ?? ''); 128 $imageAttributes['type'] = 'image/svg+xml'; 129 $imageAttributes['data-pos'] = $data['pos'] ?? ''; 130 $imageAttributes['data-len'] = $data['len'] ?? ''; 131 $imageAttributes['width'] = empty($data['width']) ? '' : $data['width']; 132 $imageAttributes['height'] = empty($data['height']) ? '' : $data['height']; 133 134 if ($cachefile) { 135 $imageAttributes['data-cached'] = str_replace(DOKU_INC, DOKU_URL, $cachefile); 136 } 137 $image = sprintf('<object %s></object>', buildAttributes($imageAttributes, true)); 138 $actionButtons = '<div class="diagrams-buttons"></div>'; 139 $wrapper = sprintf('<div %s>%s%s</div>', buildAttributes($wrapperAttributes, true), $image, $actionButtons); 140 $renderer->doc .= $wrapper; 141 } 142 143 return true; 144 } 145 146 /** 147 * PNG cache file, if caching is enabled and file exists 148 * 149 * @param array $data 150 * @return string 151 */ 152 protected function getCachedPNG($data) 153 { 154 if (!$this->getConf('pngcache')) return ''; 155 156 if (!$data['svg']) $data['svg'] = file_get_contents(mediaFN($data['src'])); 157 $cachefile = getCacheName($data['svg'], '.diagrams.png'); 158 if (file_exists($cachefile)) return $cachefile; 159 160 return ''; 161 } 162} 163