18c8c7007SAndreas Gohr<?php 28c8c7007SAndreas Gohr 359e7180eSAndreas Gohruse dokuwiki\plugin\diagrams\Diagrams; 459e7180eSAndreas Gohr 58c8c7007SAndreas Gohr/** 68c8c7007SAndreas Gohr * Class syntax_plugin_diagrams 78c8c7007SAndreas Gohr */ 8fa6a636cSAndreas Gohrclass syntax_plugin_diagrams_mediafile extends DokuWiki_Syntax_Plugin 9fa6a636cSAndreas Gohr{ 108c8c7007SAndreas Gohr 118c8c7007SAndreas Gohr /** 128c8c7007SAndreas Gohr * @inheritdoc 138c8c7007SAndreas Gohr */ 148c8c7007SAndreas Gohr public function getType() 158c8c7007SAndreas Gohr { 168c8c7007SAndreas Gohr return 'substition'; 178c8c7007SAndreas Gohr } 188c8c7007SAndreas Gohr 198c8c7007SAndreas Gohr /** 208c8c7007SAndreas Gohr * @inheritdoc 218c8c7007SAndreas Gohr */ 228c8c7007SAndreas Gohr public function getSort() 238c8c7007SAndreas Gohr { 248c8c7007SAndreas Gohr return 319; 258c8c7007SAndreas Gohr } 268c8c7007SAndreas Gohr 278c8c7007SAndreas Gohr /** 288c8c7007SAndreas Gohr * @inheritdoc 298c8c7007SAndreas Gohr */ 308c8c7007SAndreas Gohr public function connectTo($mode) 318c8c7007SAndreas Gohr { 3259e7180eSAndreas Gohr // only register if mediafile mode is enabled 3359e7180eSAndreas Gohr if (!($this->getConf('mode') & Diagrams::MODE_MEDIA)) return; 3459e7180eSAndreas Gohr 35fa6a636cSAndreas Gohr // grab all SVG images 368c8c7007SAndreas Gohr $this->Lexer->addSpecialPattern('\{\{[^\}]+(?:\.svg)[^\}]*?\}\}', $mode, 'plugin_diagrams_mediafile'); 378c8c7007SAndreas Gohr } 388c8c7007SAndreas Gohr 398c8c7007SAndreas Gohr /** 408c8c7007SAndreas Gohr * Parse SVG syntax into media data 418c8c7007SAndreas Gohr * 428c8c7007SAndreas Gohr * @param string $match 438c8c7007SAndreas Gohr * @param int $state 448c8c7007SAndreas Gohr * @param int $pos 458c8c7007SAndreas Gohr * @param Doku_Handler $handler 468c8c7007SAndreas Gohr * @return array|bool 478c8c7007SAndreas Gohr */ 488c8c7007SAndreas Gohr public function handle($match, $state, $pos, Doku_Handler $handler) 498c8c7007SAndreas Gohr { 50046ca144SAndreas Gohr $data = Doku_Handler_Parse_Media($match); 51fa6a636cSAndreas Gohr 52fa6a636cSAndreas Gohr /** @var helper_plugin_diagrams $helper */ 53fa6a636cSAndreas Gohr $helper = plugin_load('helper', 'diagrams'); 54fa6a636cSAndreas Gohr if (!$data['type'] == 'internalmedia' || !$helper->isDiagramFile(mediaFN($data['src']))) { 55fa6a636cSAndreas Gohr // This is not a local diagrams file, but some other SVG media file 56e831c5ccSAnna Dabrowska $handler->media($match, $state, $pos); 57e831c5ccSAnna Dabrowska return false; 58fa6a636cSAndreas Gohr } 59fa6a636cSAndreas Gohr 60046ca144SAndreas Gohr $data['url'] = ml($data['src'], ['cache' => 'nocache'], true, '&'); 61046ca144SAndreas Gohr return $data; 628c8c7007SAndreas Gohr } 638c8c7007SAndreas Gohr 648c8c7007SAndreas Gohr /** 65ddc20d75SAnna Dabrowska * Handle rewrites made by the move plugin 66ddc20d75SAnna Dabrowska * 67ddc20d75SAnna Dabrowska * @param string $match 68ddc20d75SAnna Dabrowska * @param int $state 69ddc20d75SAnna Dabrowska * @param int $pos 70ddc20d75SAnna Dabrowska * @param string $plugin 71ddc20d75SAnna Dabrowska * @param helper_plugin_move_handler $handler 72ddc20d75SAnna Dabrowska * @return void 73ddc20d75SAnna Dabrowska */ 74ddc20d75SAnna Dabrowska public function handleMove($match, $state, $pos, $plugin, $handler) 75ddc20d75SAnna Dabrowska { 76ddc20d75SAnna Dabrowska if ($plugin !== 'diagrams_mediafile') return; 77ddc20d75SAnna Dabrowska 78ddc20d75SAnna Dabrowska $handler->media($match, $state, $pos); 79ddc20d75SAnna Dabrowska } 80ddc20d75SAnna Dabrowska 81ddc20d75SAnna Dabrowska /** 828c8c7007SAndreas Gohr * Render the diagram SVG as <object> instead of <img> to allow links, 838c8c7007SAndreas Gohr * except when rendering to a PDF 848c8c7007SAndreas Gohr * 858c8c7007SAndreas Gohr * @param string $format 868c8c7007SAndreas Gohr * @param Doku_Renderer $renderer 878c8c7007SAndreas Gohr * @param array $data 888c8c7007SAndreas Gohr * @return bool 898c8c7007SAndreas Gohr */ 908c8c7007SAndreas Gohr public function render($format, Doku_Renderer $renderer, $data) 918c8c7007SAndreas Gohr { 92ddc20d75SAnna Dabrowska if ($format === 'metadata') { 93ddc20d75SAnna Dabrowska $renderer->internalmedia($data['src']); 94ddc20d75SAnna Dabrowska return true; 95ddc20d75SAnna Dabrowska } 96ddc20d75SAnna Dabrowska if ($format !== 'xhtml') { 97ddc20d75SAnna Dabrowska return false; 98ddc20d75SAnna Dabrowska } 998c8c7007SAndreas Gohr 100dce55f69SAndreas Gohr $baseAttributes = [ 1018c8c7007SAndreas Gohr 'class' => 'media', 1025f757686SAndreas Gohr 'width' => $data['width'] ?: '', 1035f757686SAndreas Gohr 'height' => $data['height'] ?: '', 104dce55f69SAndreas Gohr 'title' => $data['title'] ?: '', 105dce55f69SAndreas Gohr ]; 1065f757686SAndreas Gohr 1075f757686SAndreas Gohr 1085f757686SAndreas Gohr if (is_a($renderer, 'renderer_plugin_dw2pdf')) { 109dce55f69SAndreas Gohr $imageAttributes = $baseAttributes; 1105f757686SAndreas Gohr $imageAttributes['align'] = $data['align']; 111046ca144SAndreas Gohr $imageAttributes['src'] = $data['url']; 112*2bec1a22SAndreas Gohr 113*2bec1a22SAndreas Gohr // if a PNG cache exists, use it 114*2bec1a22SAndreas Gohr if (!$data['svg']) $data['svg'] = file_get_contents(mediaFN($data['src'])); 115*2bec1a22SAndreas Gohr $cachefile = getCacheName($data['svg'], '.diagrams.png'); 116*2bec1a22SAndreas Gohr if (file_exists($cachefile)) $imageAttributes['src'] = 'dw2pdf://' . $cachefile; 117*2bec1a22SAndreas Gohr 1188c8c7007SAndreas Gohr $renderer->doc .= '<img ' . buildAttributes($imageAttributes) . '/>'; 1198c8c7007SAndreas Gohr } else { 120dce55f69SAndreas Gohr $wrapperAttributes = $baseAttributes; 121dce55f69SAndreas Gohr $wrapperAttributes['class'] .= ' diagrams-svg-wrapper media' . $data['align']; 122dce55f69SAndreas Gohr 123dce55f69SAndreas Gohr $imageAttributes = []; 124dce55f69SAndreas Gohr $imageAttributes['class'] = 'diagrams-svg'; 125046ca144SAndreas Gohr $imageAttributes['data'] = $data['url']; 1265f757686SAndreas Gohr $imageAttributes['data-id'] = cleanID($data['src']); 127046ca144SAndreas Gohr $imageAttributes['type'] = 'image/svg+xml'; 128046ca144SAndreas Gohr $imageAttributes['data-pos'] = $data['pos'] ?: ''; 129046ca144SAndreas Gohr $imageAttributes['data-len'] = $data['len'] ?: ''; 1305f757686SAndreas Gohr 131dce55f69SAndreas Gohr $image = sprintf('<object %s></object>', buildAttributes($imageAttributes, true)); 132dce55f69SAndreas Gohr $wrapper = sprintf('<div %s>%s</div>', buildAttributes($wrapperAttributes, true), $image); 133dce55f69SAndreas Gohr $renderer->doc .= $wrapper; 1348c8c7007SAndreas Gohr } 1358c8c7007SAndreas Gohr 1368c8c7007SAndreas Gohr return true; 1378c8c7007SAndreas Gohr } 1388c8c7007SAndreas Gohr} 139