18a822cc1SAndreas Gohr<?php 28a822cc1SAndreas Gohr 3*100ff2a5SAndreas Gohruse dokuwiki\Logger; 4bc39777fSAndreas Gohruse dokuwiki\plugin\diagrams\Diagrams; 5*100ff2a5SAndreas Gohruse enshrined\svgSanitize\Sanitizer; 6bc39777fSAndreas Gohr 78a822cc1SAndreas Gohr/** 88c8c7007SAndreas Gohr * DokuWiki Plugin diagrams (Syntax Component) 98c8c7007SAndreas Gohr * 108c8c7007SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 118c8c7007SAndreas Gohr * @author Innovakom + CosmoCode <dokuwiki@cosmocode.de> 128a822cc1SAndreas Gohr */ 138c8c7007SAndreas Gohrclass syntax_plugin_diagrams_embed extends \dokuwiki\Extension\SyntaxPlugin 148c8c7007SAndreas Gohr{ 158c8c7007SAndreas Gohr /** @inheritDoc */ 168a822cc1SAndreas Gohr public function getType() 178a822cc1SAndreas Gohr { 188a822cc1SAndreas Gohr return 'substition'; 198a822cc1SAndreas Gohr } 208a822cc1SAndreas Gohr 218c8c7007SAndreas Gohr /** @inheritDoc */ 228c8c7007SAndreas Gohr public function getPType() 238c8c7007SAndreas Gohr { 248c8c7007SAndreas Gohr return 'block'; 258c8c7007SAndreas Gohr } 268c8c7007SAndreas Gohr 278c8c7007SAndreas Gohr /** @inheritDoc */ 288a822cc1SAndreas Gohr public function getSort() 298a822cc1SAndreas Gohr { 308a822cc1SAndreas Gohr return 319; 318a822cc1SAndreas Gohr } 328a822cc1SAndreas Gohr 338c8c7007SAndreas Gohr /** @inheritDoc */ 348a822cc1SAndreas Gohr public function connectTo($mode) 358a822cc1SAndreas Gohr { 36bc39777fSAndreas Gohr // only register if embed mode is enabled 37bc39777fSAndreas Gohr if(!$this->getConf('mode') & Diagrams::MODE_EMBED) return; 38bc39777fSAndreas Gohr 39*100ff2a5SAndreas Gohr // auto load sanitizer 40*100ff2a5SAndreas Gohr require_once __DIR__ . '/../vendor/autoload.php'; 418c8c7007SAndreas Gohr $this->Lexer->addSpecialPattern('<diagram(?: .*)?>.*?(?:</diagram>)', $mode, 'plugin_diagrams_embed'); 428a822cc1SAndreas Gohr } 438a822cc1SAndreas Gohr 448c8c7007SAndreas Gohr /** @inheritDoc */ 458a822cc1SAndreas Gohr public function handle($match, $state, $pos, Doku_Handler $handler) 468a822cc1SAndreas Gohr { 478c8c7007SAndreas Gohr [$open, $rest] = sexplode('>', $match, 2); 488c8c7007SAndreas Gohr $params = substr($open, 9); 498c8c7007SAndreas Gohr $svg = substr($rest, 0, -10); 508c8c7007SAndreas Gohr 51*100ff2a5SAndreas Gohr // sanitize svg 52*100ff2a5SAndreas Gohr $sanitizer = new Sanitizer(); 53*100ff2a5SAndreas Gohr $svg = $sanitizer->sanitize($svg); 54*100ff2a5SAndreas Gohr 55*100ff2a5SAndreas Gohr if(!$svg) { 56*100ff2a5SAndreas Gohr global $ID; 57*100ff2a5SAndreas Gohr Logger::debug('diagrams: invalid SVG on '.$ID, $sanitizer->getXmlIssues()); 58*100ff2a5SAndreas Gohr return false; 59*100ff2a5SAndreas Gohr } 60*100ff2a5SAndreas Gohr 618c8c7007SAndreas Gohr $data = [ 628c8c7007SAndreas Gohr 'svg' => $svg, 638c8c7007SAndreas Gohr 'align' => '', 648c8c7007SAndreas Gohr 'width' => '', 658c8c7007SAndreas Gohr 'height' => '', 668c8c7007SAndreas Gohr 'pos' => $pos, 678c8c7007SAndreas Gohr 'len' => strlen($match), 688c8c7007SAndreas Gohr ]; 698c8c7007SAndreas Gohr 708c8c7007SAndreas Gohr if (preg_match('/\b(left|right|center)\b/', $params, $matches)) { 718c8c7007SAndreas Gohr $data['align'] = $matches[1]; 728c8c7007SAndreas Gohr } 738c8c7007SAndreas Gohr if (preg_match('/\b(\d+)x(\d+)\b/', $params, $matches)) { 748c8c7007SAndreas Gohr $data['width'] = (int)$matches[1]; 758c8c7007SAndreas Gohr $data['height'] = (int)$matches[2]; 768a822cc1SAndreas Gohr } 778a822cc1SAndreas Gohr 788c8c7007SAndreas Gohr return $data; 798c8c7007SAndreas Gohr } 808c8c7007SAndreas Gohr 818c8c7007SAndreas Gohr /** @inheritDoc */ 828a822cc1SAndreas Gohr public function render($format, Doku_Renderer $renderer, $data) 838a822cc1SAndreas Gohr { 848a822cc1SAndreas Gohr if ($format !== 'xhtml') return false; 85*100ff2a5SAndreas Gohr if(!$data) return false; 868c8c7007SAndreas Gohr 878c8c7007SAndreas Gohr $style = ''; 88*100ff2a5SAndreas Gohr if($data['width'] && $data['height']) { 89*100ff2a5SAndreas Gohr $style .= 'width: ' . $data['width'] . 'px; '; 90*100ff2a5SAndreas Gohr $style .= 'height: ' . $data['height'] . 'px; '; 91*100ff2a5SAndreas Gohr $class = 'fixedSize'; 92*100ff2a5SAndreas Gohr } else { 93*100ff2a5SAndreas Gohr $class = 'autoSize'; 94*100ff2a5SAndreas Gohr } 958c8c7007SAndreas Gohr 96*100ff2a5SAndreas Gohr $attr = [ 97*100ff2a5SAndreas Gohr 'class' => "plugin_diagrams_embed $class media" . $data['align'], 98*100ff2a5SAndreas Gohr 'style' => $style, 99*100ff2a5SAndreas Gohr 'data-pos' => $data['pos'], 100*100ff2a5SAndreas Gohr 'data-len' => $data['len'], 101*100ff2a5SAndreas Gohr ]; 102*100ff2a5SAndreas Gohr 103*100ff2a5SAndreas Gohr $tag = '<div %s>%s</div>'; 104*100ff2a5SAndreas Gohr $renderer->doc .= sprintf($tag, buildAttributes($attr), $data['svg']); 1058a822cc1SAndreas Gohr 1068a822cc1SAndreas Gohr return true; 1078a822cc1SAndreas Gohr } 1088a822cc1SAndreas Gohr} 1098c8c7007SAndreas Gohr 110