xref: /plugin/diagrams/syntax/embed.php (revision 8c8c70074e1156a58dbfd89fe9e41e3dd346fb54)
18a822cc1SAndreas Gohr<?php
28a822cc1SAndreas Gohr
38a822cc1SAndreas Gohr/**
4*8c8c7007SAndreas Gohr * DokuWiki Plugin diagrams (Syntax Component)
5*8c8c7007SAndreas Gohr *
6*8c8c7007SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7*8c8c7007SAndreas Gohr * @author  Innovakom + CosmoCode <dokuwiki@cosmocode.de>
88a822cc1SAndreas Gohr */
9*8c8c7007SAndreas Gohrclass syntax_plugin_diagrams_embed extends \dokuwiki\Extension\SyntaxPlugin
10*8c8c7007SAndreas Gohr{
11*8c8c7007SAndreas Gohr    /** @inheritDoc */
128a822cc1SAndreas Gohr    public function getType()
138a822cc1SAndreas Gohr    {
148a822cc1SAndreas Gohr        return 'substition';
158a822cc1SAndreas Gohr    }
168a822cc1SAndreas Gohr
17*8c8c7007SAndreas Gohr    /** @inheritDoc */
18*8c8c7007SAndreas Gohr    public function getPType()
19*8c8c7007SAndreas Gohr    {
20*8c8c7007SAndreas Gohr        return 'block';
21*8c8c7007SAndreas Gohr    }
22*8c8c7007SAndreas Gohr
23*8c8c7007SAndreas Gohr    /** @inheritDoc */
248a822cc1SAndreas Gohr    public function getSort()
258a822cc1SAndreas Gohr    {
268a822cc1SAndreas Gohr        return 319;
278a822cc1SAndreas Gohr    }
288a822cc1SAndreas Gohr
29*8c8c7007SAndreas Gohr    /** @inheritDoc */
308a822cc1SAndreas Gohr    public function connectTo($mode)
318a822cc1SAndreas Gohr    {
32*8c8c7007SAndreas Gohr        // FIXME only allow this when enabled in config
33*8c8c7007SAndreas Gohr        $this->Lexer->addSpecialPattern('<diagram(?: .*)?>.*?(?:</diagram>)', $mode, 'plugin_diagrams_embed');
348a822cc1SAndreas Gohr    }
358a822cc1SAndreas Gohr
36*8c8c7007SAndreas Gohr    /** @inheritDoc */
378a822cc1SAndreas Gohr    public function handle($match, $state, $pos, Doku_Handler $handler)
388a822cc1SAndreas Gohr    {
39*8c8c7007SAndreas Gohr        [$open, $rest] = sexplode('>', $match, 2);
40*8c8c7007SAndreas Gohr        $params = substr($open, 9);
41*8c8c7007SAndreas Gohr        $svg = substr($rest, 0, -10);
42*8c8c7007SAndreas Gohr
43*8c8c7007SAndreas Gohr        $data = [
44*8c8c7007SAndreas Gohr            'svg' => $svg,
45*8c8c7007SAndreas Gohr            'align' => '',
46*8c8c7007SAndreas Gohr            'width' => '',
47*8c8c7007SAndreas Gohr            'height' => '',
48*8c8c7007SAndreas Gohr            'pos' => $pos,
49*8c8c7007SAndreas Gohr            'len' => strlen($match),
50*8c8c7007SAndreas Gohr        ];
51*8c8c7007SAndreas Gohr
52*8c8c7007SAndreas Gohr        if (preg_match('/\b(left|right|center)\b/', $params, $matches)) {
53*8c8c7007SAndreas Gohr            $data['align'] = $matches[1];
54*8c8c7007SAndreas Gohr        }
55*8c8c7007SAndreas Gohr        if (preg_match('/\b(\d+)x(\d+)\b/', $params, $matches)) {
56*8c8c7007SAndreas Gohr            $data['width'] = (int)$matches[1];
57*8c8c7007SAndreas Gohr            $data['height'] = (int)$matches[2];
588a822cc1SAndreas Gohr        }
598a822cc1SAndreas Gohr
60*8c8c7007SAndreas Gohr        return $data;
61*8c8c7007SAndreas Gohr    }
62*8c8c7007SAndreas Gohr
63*8c8c7007SAndreas Gohr    /** @inheritDoc */
648a822cc1SAndreas Gohr    public function render($format, Doku_Renderer $renderer, $data)
658a822cc1SAndreas Gohr    {
668a822cc1SAndreas Gohr        if ($format !== 'xhtml') return false;
678a822cc1SAndreas Gohr
68*8c8c7007SAndreas Gohr        // FIXME currently insecure!
69*8c8c7007SAndreas Gohr        // maybe use https://github.com/darylldoyle/svg-sanitizer
70*8c8c7007SAndreas Gohr
71*8c8c7007SAndreas Gohr        $style = '';
72*8c8c7007SAndreas Gohr        if ($data['width']) $style .= 'width: ' . $data['width'] . 'px; ';
73*8c8c7007SAndreas Gohr        if ($data['height']) $style .= 'height: ' . $data['height'] . 'px; ';
74*8c8c7007SAndreas Gohr
75*8c8c7007SAndreas Gohr        $tag = '<div class="plugin_diagrams_inline media%s" style="%s">%s</divobject>';
76*8c8c7007SAndreas Gohr        $renderer->doc .= sprintf($tag, $data['align'], $style, $data['svg']);
778a822cc1SAndreas Gohr
788a822cc1SAndreas Gohr        return true;
798a822cc1SAndreas Gohr    }
808a822cc1SAndreas Gohr}
81*8c8c7007SAndreas Gohr
82