xref: /plugin/diagrams/syntax/mediafile.php (revision ddc20d75deca382cf3d2cf544b151b1f395021dc)
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    /**
65*ddc20d75SAnna Dabrowska     * Handle rewrites made by the move plugin
66*ddc20d75SAnna Dabrowska     *
67*ddc20d75SAnna Dabrowska     * @param string $match
68*ddc20d75SAnna Dabrowska     * @param int $state
69*ddc20d75SAnna Dabrowska     * @param int $pos
70*ddc20d75SAnna Dabrowska     * @param string $plugin
71*ddc20d75SAnna Dabrowska     * @param helper_plugin_move_handler $handler
72*ddc20d75SAnna Dabrowska     * @return void
73*ddc20d75SAnna Dabrowska     */
74*ddc20d75SAnna Dabrowska    public function handleMove($match, $state, $pos, $plugin, $handler)
75*ddc20d75SAnna Dabrowska    {
76*ddc20d75SAnna Dabrowska        if ($plugin !== 'diagrams_mediafile') return;
77*ddc20d75SAnna Dabrowska
78*ddc20d75SAnna Dabrowska        $handler->media($match, $state, $pos);
79*ddc20d75SAnna Dabrowska    }
80*ddc20d75SAnna Dabrowska
81*ddc20d75SAnna 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    {
92*ddc20d75SAnna Dabrowska        if ($format === 'metadata') {
93*ddc20d75SAnna Dabrowska            $renderer->internalmedia($data['src']);
94*ddc20d75SAnna Dabrowska            return true;
95*ddc20d75SAnna Dabrowska        }
96*ddc20d75SAnna Dabrowska        if ($format !== 'xhtml') {
97*ddc20d75SAnna Dabrowska            return false;
98*ddc20d75SAnna Dabrowska        }
998c8c7007SAndreas Gohr
100dce55f69SAndreas Gohr        $baseAttributes = [
1018c8c7007SAndreas Gohr            'class' => 'media',
1025f757686SAndreas Gohr            'width' => $data['width'] ?: '',
1035f757686SAndreas Gohr            'height' => $data['height'] ?: '',
104dce55f69SAndreas Gohr            'title' => $data['title'] ?: '',
105dce55f69SAndreas Gohr        ];
1065f757686SAndreas Gohr
1075f757686SAndreas Gohr
1085f757686SAndreas Gohr        if (is_a($renderer, 'renderer_plugin_dw2pdf')) {
109dce55f69SAndreas Gohr            $imageAttributes = $baseAttributes;
1105f757686SAndreas Gohr            $imageAttributes['align'] = $data['align'];
111046ca144SAndreas Gohr            $imageAttributes['src'] = $data['url'];
1128c8c7007SAndreas Gohr            $renderer->doc .= '<img ' . buildAttributes($imageAttributes) . '/>';
1138c8c7007SAndreas Gohr        } else {
114dce55f69SAndreas Gohr            $wrapperAttributes = $baseAttributes;
115dce55f69SAndreas Gohr            $wrapperAttributes['class'] .= ' diagrams-svg-wrapper media' . $data['align'];
116dce55f69SAndreas Gohr
117dce55f69SAndreas Gohr            $imageAttributes = [];
118dce55f69SAndreas Gohr            $imageAttributes['class'] = 'diagrams-svg';
119046ca144SAndreas Gohr            $imageAttributes['data'] = $data['url'];
1205f757686SAndreas Gohr            $imageAttributes['data-id'] = cleanID($data['src']);
121046ca144SAndreas Gohr            $imageAttributes['type'] = 'image/svg+xml';
122046ca144SAndreas Gohr            $imageAttributes['data-pos'] = $data['pos'] ?: '';
123046ca144SAndreas Gohr            $imageAttributes['data-len'] = $data['len'] ?: '';
1245f757686SAndreas Gohr
125dce55f69SAndreas Gohr            $image = sprintf('<object %s></object>', buildAttributes($imageAttributes, true));
126dce55f69SAndreas Gohr            $wrapper = sprintf('<div %s>%s</div>', buildAttributes($wrapperAttributes, true), $image);
127dce55f69SAndreas Gohr            $renderer->doc .= $wrapper;
1288c8c7007SAndreas Gohr        }
1298c8c7007SAndreas Gohr
1308c8c7007SAndreas Gohr        return true;
1318c8c7007SAndreas Gohr    }
1328c8c7007SAndreas Gohr}
133