xref: /plugin/diagrams/syntax/mediafile.php (revision fa6a636c46fb412e19e3a1162fbbafc2b54652cb)
18c8c7007SAndreas Gohr<?php
28c8c7007SAndreas Gohr
359e7180eSAndreas Gohruse dokuwiki\plugin\diagrams\Diagrams;
459e7180eSAndreas Gohr
58c8c7007SAndreas Gohr/**
68c8c7007SAndreas Gohr * Class syntax_plugin_diagrams
78c8c7007SAndreas Gohr */
8*fa6a636cSAndreas Gohrclass syntax_plugin_diagrams_mediafile extends DokuWiki_Syntax_Plugin
9*fa6a636cSAndreas 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
35*fa6a636cSAndreas 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);
51*fa6a636cSAndreas Gohr
52*fa6a636cSAndreas Gohr        /** @var helper_plugin_diagrams $helper */
53*fa6a636cSAndreas Gohr        $helper = plugin_load('helper', 'diagrams');
54*fa6a636cSAndreas Gohr        if (!$data['type'] == 'internalmedia' || !$helper->isDiagramFile(mediaFN($data['src']))) {
55*fa6a636cSAndreas Gohr            // This is not a local diagrams file, but some other SVG media file
56*fa6a636cSAndreas Gohr            return $handler->media($match, $state, $pos);
57*fa6a636cSAndreas Gohr        }
58*fa6a636cSAndreas Gohr
59046ca144SAndreas Gohr        $data['url'] = ml($data['src'], ['cache' => 'nocache'], true, '&');
60046ca144SAndreas Gohr        return $data;
618c8c7007SAndreas Gohr    }
628c8c7007SAndreas Gohr
638c8c7007SAndreas Gohr    /**
648c8c7007SAndreas Gohr     * Render the diagram SVG as <object> instead of <img> to allow links,
658c8c7007SAndreas Gohr     * except when rendering to a PDF
668c8c7007SAndreas Gohr     *
678c8c7007SAndreas Gohr     * @param string $format
688c8c7007SAndreas Gohr     * @param Doku_Renderer $renderer
698c8c7007SAndreas Gohr     * @param array $data
708c8c7007SAndreas Gohr     * @return bool
718c8c7007SAndreas Gohr     */
728c8c7007SAndreas Gohr    public function render($format, Doku_Renderer $renderer, $data)
738c8c7007SAndreas Gohr    {
748c8c7007SAndreas Gohr        if ($format !== 'xhtml') return false;
758c8c7007SAndreas Gohr
768c8c7007SAndreas Gohr        $imageAttributes = array(
778c8c7007SAndreas Gohr            'class' => 'media',
785f757686SAndreas Gohr            'width' => $data['width'] ?: '',
795f757686SAndreas Gohr            'height' => $data['height'] ?: '',
805f757686SAndreas Gohr            'title' => $data['title'],
818c8c7007SAndreas Gohr        );
825f757686SAndreas Gohr
835f757686SAndreas Gohr
845f757686SAndreas Gohr        if (is_a($renderer, 'renderer_plugin_dw2pdf')) {
855f757686SAndreas Gohr            $imageAttributes['align'] = $data['align'];
86046ca144SAndreas Gohr            $imageAttributes['src'] = $data['url'];
878c8c7007SAndreas Gohr            $renderer->doc .= '<img ' . buildAttributes($imageAttributes) . '/>';
888c8c7007SAndreas Gohr        } else {
895f757686SAndreas Gohr            $imageAttributes['class'] .= ' diagrams-svg';
905f757686SAndreas Gohr            $imageAttributes['class'] .= ' media' . $data['align'];
91046ca144SAndreas Gohr            $imageAttributes['data'] = $data['url'];
925f757686SAndreas Gohr            $imageAttributes['data-id'] = cleanID($data['src']);
93046ca144SAndreas Gohr            $imageAttributes['type'] = 'image/svg+xml';
94046ca144SAndreas Gohr            $imageAttributes['data-pos'] = $data['pos'] ?: '';
95046ca144SAndreas Gohr            $imageAttributes['data-len'] = $data['len'] ?: '';
965f757686SAndreas Gohr
975f757686SAndreas Gohr            $tag = '<object %s></object>';
985f757686SAndreas Gohr            $renderer->doc .= sprintf($tag, buildAttributes($imageAttributes, true));
998c8c7007SAndreas Gohr        }
1008c8c7007SAndreas Gohr
1018c8c7007SAndreas Gohr        return true;
1028c8c7007SAndreas Gohr    }
1038c8c7007SAndreas Gohr}
104