xref: /plugin/diagrams/syntax/mediafile.php (revision c3c3f4cfcb3e85feba6c72995e96d5612a70a21c)
18c8c7007SAndreas Gohr<?php
28c8c7007SAndreas Gohr
359e7180eSAndreas Gohruse dokuwiki\plugin\diagrams\Diagrams;
459e7180eSAndreas Gohr
58c8c7007SAndreas Gohr/**
68c8c7007SAndreas Gohr * Class syntax_plugin_diagrams
78c8c7007SAndreas Gohr */
8fa6a636cSAndreas Gohrclass syntax_plugin_diagrams_mediafile extends DokuWiki_Syntax_Plugin
9fa6a636cSAndreas Gohr{
108c8c7007SAndreas Gohr
118c8c7007SAndreas Gohr    /**
128c8c7007SAndreas Gohr     * @inheritdoc
138c8c7007SAndreas Gohr     */
148c8c7007SAndreas Gohr    public function getType()
158c8c7007SAndreas Gohr    {
168c8c7007SAndreas Gohr        return 'substition';
178c8c7007SAndreas Gohr    }
188c8c7007SAndreas Gohr
198c8c7007SAndreas Gohr    /**
208c8c7007SAndreas Gohr     * @inheritdoc
218c8c7007SAndreas Gohr     */
228c8c7007SAndreas Gohr    public function getSort()
238c8c7007SAndreas Gohr    {
248c8c7007SAndreas Gohr        return 319;
258c8c7007SAndreas Gohr    }
268c8c7007SAndreas Gohr
278c8c7007SAndreas Gohr    /**
288c8c7007SAndreas Gohr     * @inheritdoc
298c8c7007SAndreas Gohr     */
308c8c7007SAndreas Gohr    public function connectTo($mode)
318c8c7007SAndreas Gohr    {
3259e7180eSAndreas Gohr        // only register if mediafile mode is enabled
3359e7180eSAndreas Gohr        if (!($this->getConf('mode') & Diagrams::MODE_MEDIA)) return;
3459e7180eSAndreas Gohr
35fa6a636cSAndreas Gohr        // grab all SVG images
368c8c7007SAndreas Gohr        $this->Lexer->addSpecialPattern('\{\{[^\}]+(?:\.svg)[^\}]*?\}\}', $mode, 'plugin_diagrams_mediafile');
378c8c7007SAndreas Gohr    }
388c8c7007SAndreas Gohr
398c8c7007SAndreas Gohr    /**
408c8c7007SAndreas Gohr     * Parse SVG syntax into media data
418c8c7007SAndreas Gohr     *
428c8c7007SAndreas Gohr     * @param string $match
438c8c7007SAndreas Gohr     * @param int $state
448c8c7007SAndreas Gohr     * @param int $pos
458c8c7007SAndreas Gohr     * @param Doku_Handler $handler
468c8c7007SAndreas Gohr     * @return array|bool
478c8c7007SAndreas Gohr     */
488c8c7007SAndreas Gohr    public function handle($match, $state, $pos, Doku_Handler $handler)
498c8c7007SAndreas Gohr    {
50046ca144SAndreas Gohr        $data = Doku_Handler_Parse_Media($match);
51fa6a636cSAndreas Gohr
52fa6a636cSAndreas Gohr        /** @var helper_plugin_diagrams $helper */
53fa6a636cSAndreas Gohr        $helper = plugin_load('helper', 'diagrams');
54fa6a636cSAndreas Gohr        if (!$data['type'] == 'internalmedia' || !$helper->isDiagramFile(mediaFN($data['src']))) {
55fa6a636cSAndreas Gohr            // This is not a local diagrams file, but some other SVG media file
56e831c5ccSAnna Dabrowska            $handler->media($match, $state, $pos);
57e831c5ccSAnna Dabrowska            return false;
58fa6a636cSAndreas Gohr        }
59fa6a636cSAndreas Gohr
60046ca144SAndreas Gohr        $data['url'] = ml($data['src'], ['cache' => 'nocache'], true, '&');
61046ca144SAndreas Gohr        return $data;
628c8c7007SAndreas Gohr    }
638c8c7007SAndreas Gohr
648c8c7007SAndreas Gohr    /**
65ddc20d75SAnna Dabrowska     * Handle rewrites made by the move plugin
66ddc20d75SAnna Dabrowska     *
67ddc20d75SAnna Dabrowska     * @param string $match
68ddc20d75SAnna Dabrowska     * @param int $state
69ddc20d75SAnna Dabrowska     * @param int $pos
70ddc20d75SAnna Dabrowska     * @param string $plugin
71ddc20d75SAnna Dabrowska     * @param helper_plugin_move_handler $handler
72ddc20d75SAnna Dabrowska     * @return void
73ddc20d75SAnna Dabrowska     */
74ddc20d75SAnna Dabrowska    public function handleMove($match, $state, $pos, $plugin, $handler)
75ddc20d75SAnna Dabrowska    {
76ddc20d75SAnna Dabrowska        if ($plugin !== 'diagrams_mediafile') return;
77ddc20d75SAnna Dabrowska
78ddc20d75SAnna Dabrowska        $handler->media($match, $state, $pos);
79ddc20d75SAnna Dabrowska    }
80ddc20d75SAnna Dabrowska
81ddc20d75SAnna Dabrowska    /**
828c8c7007SAndreas Gohr     * Render the diagram SVG as <object> instead of <img> to allow links,
838c8c7007SAndreas Gohr     * except when rendering to a PDF
848c8c7007SAndreas Gohr     *
858c8c7007SAndreas Gohr     * @param string $format
868c8c7007SAndreas Gohr     * @param Doku_Renderer $renderer
878c8c7007SAndreas Gohr     * @param array $data
888c8c7007SAndreas Gohr     * @return bool
898c8c7007SAndreas Gohr     */
908c8c7007SAndreas Gohr    public function render($format, Doku_Renderer $renderer, $data)
918c8c7007SAndreas Gohr    {
92ddc20d75SAnna Dabrowska        if ($format === 'metadata') {
93ddc20d75SAnna Dabrowska            $renderer->internalmedia($data['src']);
94ddc20d75SAnna Dabrowska            return true;
95ddc20d75SAnna Dabrowska        }
96ddc20d75SAnna Dabrowska        if ($format !== 'xhtml') {
97ddc20d75SAnna Dabrowska            return false;
98ddc20d75SAnna Dabrowska        }
998c8c7007SAndreas Gohr
100*c3c3f4cfSAndreas Gohr        if (is_a($renderer, 'renderer_plugin_dw2pdf')) {
101*c3c3f4cfSAndreas Gohr            $imageAttributes = [
1028c8c7007SAndreas Gohr                'class' => 'media',
1035f757686SAndreas Gohr                'width' => $data['width'] ?: '',
1045f757686SAndreas Gohr                'height' => $data['height'] ?: '',
105dce55f69SAndreas Gohr                'title' => $data['title'] ?: '',
106*c3c3f4cfSAndreas Gohr                'align' => $data['align'],
107*c3c3f4cfSAndreas Gohr                'src' => $data['url'],
108dce55f69SAndreas Gohr            ];
1095f757686SAndreas Gohr
110*c3c3f4cfSAndreas Gohr            // if a PNG cache exists, use it instead of the real URL
1112bec1a22SAndreas Gohr            if (!$data['svg']) $data['svg'] = file_get_contents(mediaFN($data['src']));
1122bec1a22SAndreas Gohr            $cachefile = getCacheName($data['svg'], '.diagrams.png');
1132bec1a22SAndreas Gohr            if (file_exists($cachefile)) $imageAttributes['src'] = 'dw2pdf://' . $cachefile;
1142bec1a22SAndreas Gohr
1158c8c7007SAndreas Gohr            $renderer->doc .= '<img ' . buildAttributes($imageAttributes) . '/>';
1168c8c7007SAndreas Gohr        } else {
117*c3c3f4cfSAndreas Gohr            $wrapperAttributes = [];
118*c3c3f4cfSAndreas Gohr            $wrapperAttributes['title'] = $data['title'] ?: '';
119*c3c3f4cfSAndreas Gohr            $wrapperAttributes['class'] = 'media diagrams-svg-wrapper media' . $data['align'];
120*c3c3f4cfSAndreas Gohr            $wrapperAttributes['style'] = '';
121*c3c3f4cfSAndreas Gohr            if($data['width']) $wrapperAttributes['style'] .= 'width: ' . $data['width'] . 'px;';
122*c3c3f4cfSAndreas Gohr            if($data['height']) $wrapperAttributes['style'] .= 'height: ' . $data['height'] . 'px;';
123dce55f69SAndreas Gohr
124dce55f69SAndreas Gohr            $imageAttributes = [];
125dce55f69SAndreas Gohr            $imageAttributes['class'] = 'diagrams-svg';
126046ca144SAndreas Gohr            $imageAttributes['data'] = $data['url'];
1275f757686SAndreas Gohr            $imageAttributes['data-id'] = cleanID($data['src']);
128046ca144SAndreas Gohr            $imageAttributes['type'] = 'image/svg+xml';
129046ca144SAndreas Gohr            $imageAttributes['data-pos'] = $data['pos'] ?: '';
130046ca144SAndreas Gohr            $imageAttributes['data-len'] = $data['len'] ?: '';
1315f757686SAndreas Gohr
132dce55f69SAndreas Gohr            $image = sprintf('<object %s></object>', buildAttributes($imageAttributes, true));
133dce55f69SAndreas Gohr            $wrapper = sprintf('<div %s>%s</div>', buildAttributes($wrapperAttributes, true), $image);
134dce55f69SAndreas Gohr            $renderer->doc .= $wrapper;
1358c8c7007SAndreas Gohr        }
1368c8c7007SAndreas Gohr
1378c8c7007SAndreas Gohr        return true;
1388c8c7007SAndreas Gohr    }
1398c8c7007SAndreas Gohr}
140