xref: /plugin/diagrams/syntax/mediafile.php (revision dce55f69c0abede1c1a16c969551e8f3fe04b18f)
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    /**
658c8c7007SAndreas Gohr     * Render the diagram SVG as <object> instead of <img> to allow links,
668c8c7007SAndreas Gohr     * except when rendering to a PDF
678c8c7007SAndreas Gohr     *
688c8c7007SAndreas Gohr     * @param string $format
698c8c7007SAndreas Gohr     * @param Doku_Renderer $renderer
708c8c7007SAndreas Gohr     * @param array $data
718c8c7007SAndreas Gohr     * @return bool
728c8c7007SAndreas Gohr     */
738c8c7007SAndreas Gohr    public function render($format, Doku_Renderer $renderer, $data)
748c8c7007SAndreas Gohr    {
758c8c7007SAndreas Gohr        if ($format !== 'xhtml') return false;
768c8c7007SAndreas Gohr
77*dce55f69SAndreas Gohr        $baseAttributes = [
788c8c7007SAndreas Gohr            'class' => 'media',
795f757686SAndreas Gohr            'width' => $data['width'] ?: '',
805f757686SAndreas Gohr            'height' => $data['height'] ?: '',
81*dce55f69SAndreas Gohr            'title' => $data['title'] ?: '',
82*dce55f69SAndreas Gohr        ];
835f757686SAndreas Gohr
845f757686SAndreas Gohr
855f757686SAndreas Gohr        if (is_a($renderer, 'renderer_plugin_dw2pdf')) {
86*dce55f69SAndreas Gohr            $imageAttributes = $baseAttributes;
875f757686SAndreas Gohr            $imageAttributes['align'] = $data['align'];
88046ca144SAndreas Gohr            $imageAttributes['src'] = $data['url'];
898c8c7007SAndreas Gohr            $renderer->doc .= '<img ' . buildAttributes($imageAttributes) . '/>';
908c8c7007SAndreas Gohr        } else {
91*dce55f69SAndreas Gohr            $wrapperAttributes = $baseAttributes;
92*dce55f69SAndreas Gohr            $wrapperAttributes['class'] .= ' diagrams-svg-wrapper media' . $data['align'];
93*dce55f69SAndreas Gohr
94*dce55f69SAndreas Gohr            $imageAttributes = [];
95*dce55f69SAndreas Gohr            $imageAttributes['class'] = 'diagrams-svg';
96046ca144SAndreas Gohr            $imageAttributes['data'] = $data['url'];
975f757686SAndreas Gohr            $imageAttributes['data-id'] = cleanID($data['src']);
98046ca144SAndreas Gohr            $imageAttributes['type'] = 'image/svg+xml';
99046ca144SAndreas Gohr            $imageAttributes['data-pos'] = $data['pos'] ?: '';
100046ca144SAndreas Gohr            $imageAttributes['data-len'] = $data['len'] ?: '';
1015f757686SAndreas Gohr
102*dce55f69SAndreas Gohr            $image = sprintf('<object %s></object>', buildAttributes($imageAttributes, true));
103*dce55f69SAndreas Gohr            $wrapper = sprintf('<div %s>%s</div>', buildAttributes($wrapperAttributes, true), $image);
104*dce55f69SAndreas Gohr            $renderer->doc .= $wrapper;
1058c8c7007SAndreas Gohr        }
1068c8c7007SAndreas Gohr
1078c8c7007SAndreas Gohr        return true;
1088c8c7007SAndreas Gohr    }
1098c8c7007SAndreas Gohr}
110