xref: /plugin/diagrams/syntax/mediafile.php (revision e831c5cc3fad756743ad1a827aace9bcb53b7c8b)
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
56*e831c5ccSAnna Dabrowska            $handler->media($match, $state, $pos);
57*e831c5ccSAnna 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
778c8c7007SAndreas Gohr        $imageAttributes = array(
788c8c7007SAndreas Gohr            'class' => 'media',
795f757686SAndreas Gohr            'width' => $data['width'] ?: '',
805f757686SAndreas Gohr            'height' => $data['height'] ?: '',
815f757686SAndreas Gohr            'title' => $data['title'],
828c8c7007SAndreas Gohr        );
835f757686SAndreas Gohr
845f757686SAndreas Gohr
855f757686SAndreas Gohr        if (is_a($renderer, 'renderer_plugin_dw2pdf')) {
865f757686SAndreas Gohr            $imageAttributes['align'] = $data['align'];
87046ca144SAndreas Gohr            $imageAttributes['src'] = $data['url'];
888c8c7007SAndreas Gohr            $renderer->doc .= '<img ' . buildAttributes($imageAttributes) . '/>';
898c8c7007SAndreas Gohr        } else {
905f757686SAndreas Gohr            $imageAttributes['class'] .= ' diagrams-svg';
915f757686SAndreas Gohr            $imageAttributes['class'] .= ' media' . $data['align'];
92046ca144SAndreas Gohr            $imageAttributes['data'] = $data['url'];
935f757686SAndreas Gohr            $imageAttributes['data-id'] = cleanID($data['src']);
94046ca144SAndreas Gohr            $imageAttributes['type'] = 'image/svg+xml';
95046ca144SAndreas Gohr            $imageAttributes['data-pos'] = $data['pos'] ?: '';
96046ca144SAndreas Gohr            $imageAttributes['data-len'] = $data['len'] ?: '';
975f757686SAndreas Gohr
985f757686SAndreas Gohr            $tag = '<object %s></object>';
995f757686SAndreas Gohr            $renderer->doc .= sprintf($tag, buildAttributes($imageAttributes, true));
1008c8c7007SAndreas Gohr        }
1018c8c7007SAndreas Gohr
1028c8c7007SAndreas Gohr        return true;
1038c8c7007SAndreas Gohr    }
1048c8c7007SAndreas Gohr}
105