xref: /plugin/diagrams/syntax/embed.php (revision 95ed8ca06adb5fd6c820ea6e5ad119eac0d9199e)
18a822cc1SAndreas Gohr<?php
28a822cc1SAndreas Gohr
3100ff2a5SAndreas Gohruse dokuwiki\Logger;
4bc39777fSAndreas Gohruse dokuwiki\plugin\diagrams\Diagrams;
5100ff2a5SAndreas 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
39100ff2a5SAndreas Gohr        // auto load sanitizer
40100ff2a5SAndreas 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*95ed8ca0SAndreas Gohr        /** @var helper_plugin_diagrams $helper */
52*95ed8ca0SAndreas Gohr        $helper = plugin_load('helper', 'diagrams');
53*95ed8ca0SAndreas Gohr        if(!$helper->isDiagram($svg)) return false;
54*95ed8ca0SAndreas Gohr
55100ff2a5SAndreas Gohr        // sanitize svg
56100ff2a5SAndreas Gohr        $sanitizer = new Sanitizer();
57100ff2a5SAndreas Gohr        $svg = $sanitizer->sanitize($svg);
58100ff2a5SAndreas Gohr
59100ff2a5SAndreas Gohr        if(!$svg) {
60100ff2a5SAndreas Gohr            global $ID;
61100ff2a5SAndreas Gohr            Logger::debug('diagrams: invalid SVG on '.$ID, $sanitizer->getXmlIssues());
62100ff2a5SAndreas Gohr            return false;
63100ff2a5SAndreas Gohr        }
64100ff2a5SAndreas Gohr
658c8c7007SAndreas Gohr        $data = [
668c8c7007SAndreas Gohr            'svg' => $svg,
678c8c7007SAndreas Gohr            'align' => '',
688c8c7007SAndreas Gohr            'width' => '',
698c8c7007SAndreas Gohr            'height' => '',
708c8c7007SAndreas Gohr            'pos' => $pos,
718c8c7007SAndreas Gohr            'len' => strlen($match),
728c8c7007SAndreas Gohr        ];
738c8c7007SAndreas Gohr
748c8c7007SAndreas Gohr        if (preg_match('/\b(left|right|center)\b/', $params, $matches)) {
758c8c7007SAndreas Gohr            $data['align'] = $matches[1];
768c8c7007SAndreas Gohr        }
778c8c7007SAndreas Gohr        if (preg_match('/\b(\d+)x(\d+)\b/', $params, $matches)) {
788c8c7007SAndreas Gohr            $data['width'] = (int)$matches[1];
798c8c7007SAndreas Gohr            $data['height'] = (int)$matches[2];
808a822cc1SAndreas Gohr        }
818a822cc1SAndreas Gohr
828c8c7007SAndreas Gohr        return $data;
838c8c7007SAndreas Gohr    }
848c8c7007SAndreas Gohr
858c8c7007SAndreas Gohr    /** @inheritDoc */
868a822cc1SAndreas Gohr    public function render($format, Doku_Renderer $renderer, $data)
878a822cc1SAndreas Gohr    {
888a822cc1SAndreas Gohr        if ($format !== 'xhtml') return false;
89100ff2a5SAndreas Gohr        if(!$data) return false;
908c8c7007SAndreas Gohr
918c8c7007SAndreas Gohr        $style = '';
92100ff2a5SAndreas Gohr        if($data['width'] && $data['height']) {
93100ff2a5SAndreas Gohr            $style .= 'width: ' . $data['width'] . 'px; ';
94100ff2a5SAndreas Gohr            $style .= 'height: ' . $data['height'] . 'px; ';
95100ff2a5SAndreas Gohr            $class = 'fixedSize';
96100ff2a5SAndreas Gohr        } else {
97100ff2a5SAndreas Gohr            $class = 'autoSize';
98100ff2a5SAndreas Gohr        }
998c8c7007SAndreas Gohr
100100ff2a5SAndreas Gohr        $attr = [
101100ff2a5SAndreas Gohr            'class' => "plugin_diagrams_embed $class media" . $data['align'],
102100ff2a5SAndreas Gohr            'style' => $style,
103100ff2a5SAndreas Gohr            'data-pos' => $data['pos'],
104100ff2a5SAndreas Gohr            'data-len' => $data['len'],
105100ff2a5SAndreas Gohr        ];
106100ff2a5SAndreas Gohr
107100ff2a5SAndreas Gohr        $tag = '<div %s>%s</div>';
108100ff2a5SAndreas Gohr        $renderer->doc .= sprintf($tag, buildAttributes($attr), $data['svg']);
1098a822cc1SAndreas Gohr
1108a822cc1SAndreas Gohr        return true;
1118a822cc1SAndreas Gohr    }
1128a822cc1SAndreas Gohr}
1138c8c7007SAndreas Gohr
114