xref: /plugin/diagrams/syntax/mediafile.php (revision 5f757686d683fd550247221d4a6d2848f21773e6)
18c8c7007SAndreas Gohr<?php
28c8c7007SAndreas Gohr
38c8c7007SAndreas Gohr/**
48c8c7007SAndreas Gohr * Class syntax_plugin_diagrams
58c8c7007SAndreas Gohr */
68c8c7007SAndreas Gohrclass syntax_plugin_diagrams_mediafile extends DokuWiki_Syntax_Plugin {
78c8c7007SAndreas Gohr
88c8c7007SAndreas Gohr    /**
98c8c7007SAndreas Gohr     * @inheritdoc
108c8c7007SAndreas Gohr     */
118c8c7007SAndreas Gohr    public function getType()
128c8c7007SAndreas Gohr    {
138c8c7007SAndreas Gohr        return 'substition';
148c8c7007SAndreas Gohr    }
158c8c7007SAndreas Gohr
168c8c7007SAndreas Gohr    /**
178c8c7007SAndreas Gohr     * @inheritdoc
188c8c7007SAndreas Gohr     */
198c8c7007SAndreas Gohr    public function getSort()
208c8c7007SAndreas Gohr    {
218c8c7007SAndreas Gohr        return 319;
228c8c7007SAndreas Gohr    }
238c8c7007SAndreas Gohr
248c8c7007SAndreas Gohr    /**
258c8c7007SAndreas Gohr     * @inheritdoc
268c8c7007SAndreas Gohr     */
278c8c7007SAndreas Gohr    public function connectTo($mode)
288c8c7007SAndreas Gohr    {
298c8c7007SAndreas Gohr        $this->Lexer->addSpecialPattern('\{\{[^\}]+(?:\.svg)[^\}]*?\}\}',$mode,'plugin_diagrams_mediafile');
308c8c7007SAndreas Gohr    }
318c8c7007SAndreas Gohr
328c8c7007SAndreas Gohr    /**
338c8c7007SAndreas Gohr     * Parse SVG syntax into media data
348c8c7007SAndreas Gohr     *
358c8c7007SAndreas Gohr     * @param string $match
368c8c7007SAndreas Gohr     * @param int $state
378c8c7007SAndreas Gohr     * @param int $pos
388c8c7007SAndreas Gohr     * @param Doku_Handler $handler
398c8c7007SAndreas Gohr     * @return array|bool
408c8c7007SAndreas Gohr     */
418c8c7007SAndreas Gohr    public function handle($match, $state, $pos, Doku_Handler $handler)
428c8c7007SAndreas Gohr    {
438c8c7007SAndreas Gohr        return Doku_Handler_Parse_Media($match);
448c8c7007SAndreas Gohr    }
458c8c7007SAndreas Gohr
468c8c7007SAndreas Gohr    /**
478c8c7007SAndreas Gohr     * Render the diagram SVG as <object> instead of <img> to allow links,
488c8c7007SAndreas Gohr     * except when rendering to a PDF
498c8c7007SAndreas Gohr     *
508c8c7007SAndreas Gohr     * @param string $format
518c8c7007SAndreas Gohr     * @param Doku_Renderer $renderer
528c8c7007SAndreas Gohr     * @param array $data
538c8c7007SAndreas Gohr     * @return bool
548c8c7007SAndreas Gohr     */
558c8c7007SAndreas Gohr    public function render($format, Doku_Renderer $renderer, $data)
568c8c7007SAndreas Gohr    {
578c8c7007SAndreas Gohr        if ($format !== 'xhtml') return false;
588c8c7007SAndreas Gohr
598c8c7007SAndreas Gohr        $imageAttributes = array(
608c8c7007SAndreas Gohr            'class'   => 'media',
61*5f757686SAndreas Gohr            'width'   => $data['width'] ?: '',
62*5f757686SAndreas Gohr            'height'  => $data['height'] ?: '',
63*5f757686SAndreas Gohr            'title'   => $data['title'],
648c8c7007SAndreas Gohr        );
65*5f757686SAndreas Gohr
66*5f757686SAndreas Gohr
67*5f757686SAndreas Gohr        if(is_a($renderer, 'renderer_plugin_dw2pdf')) {
68*5f757686SAndreas Gohr            $imageAttributes['align'] = $data['align'];
69*5f757686SAndreas Gohr            $imageAttributes['src'] = ml($data['src'], ['cache' => 'nocache'], true, '&');
708c8c7007SAndreas Gohr            $renderer->doc .= '<img '. buildAttributes($imageAttributes) . '/>';
718c8c7007SAndreas Gohr        } else {
72*5f757686SAndreas Gohr            $imageAttributes['class'] .= ' diagrams-svg';
73*5f757686SAndreas Gohr            $imageAttributes['class'] .= ' media' . $data['align'];
74*5f757686SAndreas Gohr            $imageAttributes['data'] = ml($data['src'], ['cache' => 'nocache'], true, '&');
75*5f757686SAndreas Gohr            $imageAttributes['data-id'] = cleanID($data['src']);
76*5f757686SAndreas Gohr
77*5f757686SAndreas Gohr            $tag = '<object %s></object>';
78*5f757686SAndreas Gohr            $renderer->doc .= sprintf($tag, buildAttributes($imageAttributes, true));
798c8c7007SAndreas Gohr        }
808c8c7007SAndreas Gohr
818c8c7007SAndreas Gohr        return true;
828c8c7007SAndreas Gohr    }
838c8c7007SAndreas Gohr}
84