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 return $handler->media($match, $state, $pos); 57 } 58 59 $data['url'] = ml($data['src'], ['cache' => 'nocache'], true, '&'); 60 return $data; 61 } 62 63 /** 64 * Render the diagram SVG as <object> instead of <img> to allow links, 65 * except when rendering to a PDF 66 * 67 * @param string $format 68 * @param Doku_Renderer $renderer 69 * @param array $data 70 * @return bool 71 */ 72 public function render($format, Doku_Renderer $renderer, $data) 73 { 74 if ($format !== 'xhtml') return false; 75 76 $imageAttributes = array( 77 'class' => 'media', 78 'width' => $data['width'] ?: '', 79 'height' => $data['height'] ?: '', 80 'title' => $data['title'], 81 ); 82 83 84 if (is_a($renderer, 'renderer_plugin_dw2pdf')) { 85 $imageAttributes['align'] = $data['align']; 86 $imageAttributes['src'] = $data['url']; 87 $renderer->doc .= '<img ' . buildAttributes($imageAttributes) . '/>'; 88 } else { 89 $imageAttributes['class'] .= ' diagrams-svg'; 90 $imageAttributes['class'] .= ' media' . $data['align']; 91 $imageAttributes['data'] = $data['url']; 92 $imageAttributes['data-id'] = cleanID($data['src']); 93 $imageAttributes['type'] = 'image/svg+xml'; 94 $imageAttributes['data-pos'] = $data['pos'] ?: ''; 95 $imageAttributes['data-len'] = $data['len'] ?: ''; 96 97 $tag = '<object %s></object>'; 98 $renderer->doc .= sprintf($tag, buildAttributes($imageAttributes, true)); 99 } 100 101 return true; 102 } 103} 104