xref: /plugin/diagrams/syntax/embed.php (revision bc39777fa364696c1e0053573aadb2d26ce57bd2)
18a822cc1SAndreas Gohr<?php
28a822cc1SAndreas Gohr
3*bc39777fSAndreas Gohruse dokuwiki\plugin\diagrams\Diagrams;
4*bc39777fSAndreas Gohr
58a822cc1SAndreas Gohr/**
68c8c7007SAndreas Gohr * DokuWiki Plugin diagrams (Syntax Component)
78c8c7007SAndreas Gohr *
88c8c7007SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
98c8c7007SAndreas Gohr * @author  Innovakom + CosmoCode <dokuwiki@cosmocode.de>
108a822cc1SAndreas Gohr */
118c8c7007SAndreas Gohrclass syntax_plugin_diagrams_embed extends \dokuwiki\Extension\SyntaxPlugin
128c8c7007SAndreas Gohr{
138c8c7007SAndreas Gohr    /** @inheritDoc */
148a822cc1SAndreas Gohr    public function getType()
158a822cc1SAndreas Gohr    {
168a822cc1SAndreas Gohr        return 'substition';
178a822cc1SAndreas Gohr    }
188a822cc1SAndreas Gohr
198c8c7007SAndreas Gohr    /** @inheritDoc */
208c8c7007SAndreas Gohr    public function getPType()
218c8c7007SAndreas Gohr    {
228c8c7007SAndreas Gohr        return 'block';
238c8c7007SAndreas Gohr    }
248c8c7007SAndreas Gohr
258c8c7007SAndreas Gohr    /** @inheritDoc */
268a822cc1SAndreas Gohr    public function getSort()
278a822cc1SAndreas Gohr    {
288a822cc1SAndreas Gohr        return 319;
298a822cc1SAndreas Gohr    }
308a822cc1SAndreas Gohr
318c8c7007SAndreas Gohr    /** @inheritDoc */
328a822cc1SAndreas Gohr    public function connectTo($mode)
338a822cc1SAndreas Gohr    {
34*bc39777fSAndreas Gohr        // only register if embed mode is enabled
35*bc39777fSAndreas Gohr        if(!$this->getConf('mode') & Diagrams::MODE_EMBED) return;
36*bc39777fSAndreas Gohr
378c8c7007SAndreas Gohr        $this->Lexer->addSpecialPattern('<diagram(?: .*)?>.*?(?:</diagram>)', $mode, 'plugin_diagrams_embed');
388a822cc1SAndreas Gohr    }
398a822cc1SAndreas Gohr
408c8c7007SAndreas Gohr    /** @inheritDoc */
418a822cc1SAndreas Gohr    public function handle($match, $state, $pos, Doku_Handler $handler)
428a822cc1SAndreas Gohr    {
438c8c7007SAndreas Gohr        [$open, $rest] = sexplode('>', $match, 2);
448c8c7007SAndreas Gohr        $params = substr($open, 9);
458c8c7007SAndreas Gohr        $svg = substr($rest, 0, -10);
468c8c7007SAndreas Gohr
478c8c7007SAndreas Gohr        $data = [
488c8c7007SAndreas Gohr            'svg' => $svg,
498c8c7007SAndreas Gohr            'align' => '',
508c8c7007SAndreas Gohr            'width' => '',
518c8c7007SAndreas Gohr            'height' => '',
528c8c7007SAndreas Gohr            'pos' => $pos,
538c8c7007SAndreas Gohr            'len' => strlen($match),
548c8c7007SAndreas Gohr        ];
558c8c7007SAndreas Gohr
568c8c7007SAndreas Gohr        if (preg_match('/\b(left|right|center)\b/', $params, $matches)) {
578c8c7007SAndreas Gohr            $data['align'] = $matches[1];
588c8c7007SAndreas Gohr        }
598c8c7007SAndreas Gohr        if (preg_match('/\b(\d+)x(\d+)\b/', $params, $matches)) {
608c8c7007SAndreas Gohr            $data['width'] = (int)$matches[1];
618c8c7007SAndreas Gohr            $data['height'] = (int)$matches[2];
628a822cc1SAndreas Gohr        }
638a822cc1SAndreas Gohr
648c8c7007SAndreas Gohr        return $data;
658c8c7007SAndreas Gohr    }
668c8c7007SAndreas Gohr
678c8c7007SAndreas Gohr    /** @inheritDoc */
688a822cc1SAndreas Gohr    public function render($format, Doku_Renderer $renderer, $data)
698a822cc1SAndreas Gohr    {
708a822cc1SAndreas Gohr        if ($format !== 'xhtml') return false;
718a822cc1SAndreas Gohr
728c8c7007SAndreas Gohr        // FIXME currently insecure!
738c8c7007SAndreas Gohr        // maybe use https://github.com/darylldoyle/svg-sanitizer
748c8c7007SAndreas Gohr
758c8c7007SAndreas Gohr        $style = '';
768c8c7007SAndreas Gohr        if ($data['width']) $style .= 'width: ' . $data['width'] . 'px; ';
778c8c7007SAndreas Gohr        if ($data['height']) $style .= 'height: ' . $data['height'] . 'px; ';
788c8c7007SAndreas Gohr
798c8c7007SAndreas Gohr        $tag = '<div class="plugin_diagrams_inline media%s" style="%s">%s</divobject>';
808c8c7007SAndreas Gohr        $renderer->doc .= sprintf($tag, $data['align'], $style, $data['svg']);
818a822cc1SAndreas Gohr
828a822cc1SAndreas Gohr        return true;
838a822cc1SAndreas Gohr    }
848a822cc1SAndreas Gohr}
858c8c7007SAndreas Gohr
86