xref: /plugin/diagrams/syntax/embed.php (revision 146e874b1a88e6d45cbffef54a860c0c8fcec462)
1<?php
2
3use dokuwiki\Logger;
4use dokuwiki\plugin\diagrams\Diagrams;
5use enshrined\svgSanitize\Sanitizer;
6
7/**
8 * DokuWiki Plugin diagrams (Syntax Component)
9 *
10 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11 * @author  Innovakom + CosmoCode <dokuwiki@cosmocode.de>
12 */
13class syntax_plugin_diagrams_embed extends \dokuwiki\Extension\SyntaxPlugin
14{
15    /** @inheritDoc */
16    public function getType()
17    {
18        return 'substition';
19    }
20
21    /** @inheritDoc */
22    public function getPType()
23    {
24        return 'block';
25    }
26
27    /** @inheritDoc */
28    public function getSort()
29    {
30        return 319;
31    }
32
33    /** @inheritDoc */
34    public function connectTo($mode)
35    {
36        // only register if embed mode is enabled
37        if(!$this->getConf('mode') & Diagrams::MODE_EMBED) return;
38
39        // auto load sanitizer
40        require_once __DIR__ . '/../vendor/autoload.php';
41        $this->Lexer->addSpecialPattern('<diagram(?: .*)?>.*?(?:</diagram>)', $mode, 'plugin_diagrams_embed');
42    }
43
44    /** @inheritDoc */
45    public function handle($match, $state, $pos, Doku_Handler $handler)
46    {
47        [$open, $rest] = sexplode('>', $match, 2);
48        $params = substr($open, 9);
49        $svg = substr($rest, 0, -10);
50
51        // sanitize svg
52        $sanitizer = new Sanitizer();
53        $svg = $sanitizer->sanitize($svg);
54
55        if(!$svg) {
56            global $ID;
57            Logger::debug('diagrams: invalid SVG on '.$ID, $sanitizer->getXmlIssues());
58            return false;
59        }
60
61        $data = [
62            'svg' => $svg,
63            'align' => '',
64            'width' => '',
65            'height' => '',
66            'pos' => $pos,
67            'len' => strlen($match),
68        ];
69
70        if (preg_match('/\b(left|right|center)\b/', $params, $matches)) {
71            $data['align'] = $matches[1];
72        }
73        if (preg_match('/\b(\d+)x(\d+)\b/', $params, $matches)) {
74            $data['width'] = (int)$matches[1];
75            $data['height'] = (int)$matches[2];
76        }
77
78        return $data;
79    }
80
81    /** @inheritDoc */
82    public function render($format, Doku_Renderer $renderer, $data)
83    {
84        if ($format !== 'xhtml') return false;
85        if(!$data) return false;
86
87        $style = '';
88        if($data['width'] && $data['height']) {
89            $style .= 'width: ' . $data['width'] . 'px; ';
90            $style .= 'height: ' . $data['height'] . 'px; ';
91            $class = 'fixedSize';
92        } else {
93            $class = 'autoSize';
94        }
95
96        $attr = [
97            'class' => "plugin_diagrams_embed $class media" . $data['align'],
98            'style' => $style,
99            'data-pos' => $data['pos'],
100            'data-len' => $data['len'],
101        ];
102
103        $tag = '<div %s>%s</div>';
104        $renderer->doc .= sprintf($tag, buildAttributes($attr), $data['svg']);
105
106        return true;
107    }
108}
109
110