xref: /plugin/diagrams/syntax/mediafile.php (revision 046ca1447d01a47b6caa55952ebefa80c90967a9)
18c8c7007SAndreas Gohr<?php
28c8c7007SAndreas Gohr
359e7180eSAndreas Gohruse dokuwiki\plugin\diagrams\Diagrams;
459e7180eSAndreas Gohr
58c8c7007SAndreas Gohr/**
68c8c7007SAndreas Gohr * Class syntax_plugin_diagrams
78c8c7007SAndreas Gohr */
88c8c7007SAndreas Gohrclass syntax_plugin_diagrams_mediafile extends DokuWiki_Syntax_Plugin {
98c8c7007SAndreas Gohr
108c8c7007SAndreas Gohr    /**
118c8c7007SAndreas Gohr     * @inheritdoc
128c8c7007SAndreas Gohr     */
138c8c7007SAndreas Gohr    public function getType()
148c8c7007SAndreas Gohr    {
158c8c7007SAndreas Gohr        return 'substition';
168c8c7007SAndreas Gohr    }
178c8c7007SAndreas Gohr
188c8c7007SAndreas Gohr    /**
198c8c7007SAndreas Gohr     * @inheritdoc
208c8c7007SAndreas Gohr     */
218c8c7007SAndreas Gohr    public function getSort()
228c8c7007SAndreas Gohr    {
238c8c7007SAndreas Gohr        return 319;
248c8c7007SAndreas Gohr    }
258c8c7007SAndreas Gohr
268c8c7007SAndreas Gohr    /**
278c8c7007SAndreas Gohr     * @inheritdoc
288c8c7007SAndreas Gohr     */
298c8c7007SAndreas Gohr    public function connectTo($mode)
308c8c7007SAndreas Gohr    {
3159e7180eSAndreas Gohr        // only register if mediafile mode is enabled
3259e7180eSAndreas Gohr        if(!($this->getConf('mode') & Diagrams::MODE_MEDIA)) return;
3359e7180eSAndreas Gohr
348c8c7007SAndreas Gohr        $this->Lexer->addSpecialPattern('\{\{[^\}]+(?:\.svg)[^\}]*?\}\}',$mode,'plugin_diagrams_mediafile');
358c8c7007SAndreas Gohr    }
368c8c7007SAndreas Gohr
378c8c7007SAndreas Gohr    /**
388c8c7007SAndreas Gohr     * Parse SVG syntax into media data
398c8c7007SAndreas Gohr     *
408c8c7007SAndreas Gohr     * @param string $match
418c8c7007SAndreas Gohr     * @param int $state
428c8c7007SAndreas Gohr     * @param int $pos
438c8c7007SAndreas Gohr     * @param Doku_Handler $handler
448c8c7007SAndreas Gohr     * @return array|bool
458c8c7007SAndreas Gohr     */
468c8c7007SAndreas Gohr    public function handle($match, $state, $pos, Doku_Handler $handler)
478c8c7007SAndreas Gohr    {
48*046ca144SAndreas Gohr        $data = Doku_Handler_Parse_Media($match);
49*046ca144SAndreas Gohr        $data['url'] = ml($data['src'], ['cache' => 'nocache'], true, '&');
50*046ca144SAndreas Gohr        return $data;
518c8c7007SAndreas Gohr    }
528c8c7007SAndreas Gohr
538c8c7007SAndreas Gohr    /**
548c8c7007SAndreas Gohr     * Render the diagram SVG as <object> instead of <img> to allow links,
558c8c7007SAndreas Gohr     * except when rendering to a PDF
568c8c7007SAndreas Gohr     *
578c8c7007SAndreas Gohr     * @param string $format
588c8c7007SAndreas Gohr     * @param Doku_Renderer $renderer
598c8c7007SAndreas Gohr     * @param array $data
608c8c7007SAndreas Gohr     * @return bool
618c8c7007SAndreas Gohr     */
628c8c7007SAndreas Gohr    public function render($format, Doku_Renderer $renderer, $data)
638c8c7007SAndreas Gohr    {
648c8c7007SAndreas Gohr        if ($format !== 'xhtml') return false;
658c8c7007SAndreas Gohr
668c8c7007SAndreas Gohr        $imageAttributes = array(
678c8c7007SAndreas Gohr            'class'   => 'media',
685f757686SAndreas Gohr            'width'   => $data['width'] ?: '',
695f757686SAndreas Gohr            'height'  => $data['height'] ?: '',
705f757686SAndreas Gohr            'title'   => $data['title'],
718c8c7007SAndreas Gohr        );
725f757686SAndreas Gohr
735f757686SAndreas Gohr
745f757686SAndreas Gohr        if(is_a($renderer, 'renderer_plugin_dw2pdf')) {
755f757686SAndreas Gohr            $imageAttributes['align'] = $data['align'];
76*046ca144SAndreas Gohr            $imageAttributes['src'] = $data['url'];
778c8c7007SAndreas Gohr            $renderer->doc .= '<img '. buildAttributes($imageAttributes) . '/>';
788c8c7007SAndreas Gohr        } else {
795f757686SAndreas Gohr            $imageAttributes['class'] .= ' diagrams-svg';
805f757686SAndreas Gohr            $imageAttributes['class'] .= ' media' . $data['align'];
81*046ca144SAndreas Gohr            $imageAttributes['data'] = $data['url'];
825f757686SAndreas Gohr            $imageAttributes['data-id'] = cleanID($data['src']);
83*046ca144SAndreas Gohr            $imageAttributes['type'] = 'image/svg+xml';
84*046ca144SAndreas Gohr            $imageAttributes['data-pos'] = $data['pos'] ?: '';
85*046ca144SAndreas Gohr            $imageAttributes['data-len'] = $data['len'] ?: '';
865f757686SAndreas Gohr
875f757686SAndreas Gohr            $tag = '<object %s></object>';
885f757686SAndreas Gohr            $renderer->doc .= sprintf($tag, buildAttributes($imageAttributes, true));
898c8c7007SAndreas Gohr        }
908c8c7007SAndreas Gohr
918c8c7007SAndreas Gohr        return true;
928c8c7007SAndreas Gohr    }
938c8c7007SAndreas Gohr}
94