xref: /plugin/diagrams/syntax/mediafile.php (revision 59e7180efcc3e125683f24f98a466ef29145cfce)
18c8c7007SAndreas Gohr<?php
28c8c7007SAndreas Gohr
3*59e7180eSAndreas Gohruse dokuwiki\plugin\diagrams\Diagrams;
4*59e7180eSAndreas 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    {
31*59e7180eSAndreas Gohr        // only register if mediafile mode is enabled
32*59e7180eSAndreas Gohr        if(!($this->getConf('mode') & Diagrams::MODE_MEDIA)) return;
33*59e7180eSAndreas 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    {
488c8c7007SAndreas Gohr        return Doku_Handler_Parse_Media($match);
498c8c7007SAndreas Gohr    }
508c8c7007SAndreas Gohr
518c8c7007SAndreas Gohr    /**
528c8c7007SAndreas Gohr     * Render the diagram SVG as <object> instead of <img> to allow links,
538c8c7007SAndreas Gohr     * except when rendering to a PDF
548c8c7007SAndreas Gohr     *
558c8c7007SAndreas Gohr     * @param string $format
568c8c7007SAndreas Gohr     * @param Doku_Renderer $renderer
578c8c7007SAndreas Gohr     * @param array $data
588c8c7007SAndreas Gohr     * @return bool
598c8c7007SAndreas Gohr     */
608c8c7007SAndreas Gohr    public function render($format, Doku_Renderer $renderer, $data)
618c8c7007SAndreas Gohr    {
628c8c7007SAndreas Gohr        if ($format !== 'xhtml') return false;
638c8c7007SAndreas Gohr
648c8c7007SAndreas Gohr        $imageAttributes = array(
658c8c7007SAndreas Gohr            'class'   => 'media',
665f757686SAndreas Gohr            'width'   => $data['width'] ?: '',
675f757686SAndreas Gohr            'height'  => $data['height'] ?: '',
685f757686SAndreas Gohr            'title'   => $data['title'],
698c8c7007SAndreas Gohr        );
705f757686SAndreas Gohr
715f757686SAndreas Gohr
725f757686SAndreas Gohr        if(is_a($renderer, 'renderer_plugin_dw2pdf')) {
735f757686SAndreas Gohr            $imageAttributes['align'] = $data['align'];
745f757686SAndreas Gohr            $imageAttributes['src'] = ml($data['src'], ['cache' => 'nocache'], true, '&');
758c8c7007SAndreas Gohr            $renderer->doc .= '<img '. buildAttributes($imageAttributes) . '/>';
768c8c7007SAndreas Gohr        } else {
775f757686SAndreas Gohr            $imageAttributes['class'] .= ' diagrams-svg';
785f757686SAndreas Gohr            $imageAttributes['class'] .= ' media' . $data['align'];
795f757686SAndreas Gohr            $imageAttributes['data'] = ml($data['src'], ['cache' => 'nocache'], true, '&');
805f757686SAndreas Gohr            $imageAttributes['data-id'] = cleanID($data['src']);
815f757686SAndreas Gohr
825f757686SAndreas Gohr            $tag = '<object %s></object>';
835f757686SAndreas Gohr            $renderer->doc .= sprintf($tag, buildAttributes($imageAttributes, true));
848c8c7007SAndreas Gohr        }
858c8c7007SAndreas Gohr
868c8c7007SAndreas Gohr        return true;
878c8c7007SAndreas Gohr    }
888c8c7007SAndreas Gohr}
89