xref: /plugin/diagrams/syntax/mediafile.php (revision 50c16f31eb7272ab441eb721895b0830622db302)
1<?php
2
3use dokuwiki\plugin\diagrams\Diagrams;
4
5/**
6 * Class syntax_plugin_diagrams
7 */
8class syntax_plugin_diagrams_mediafile extends DokuWiki_Syntax_Plugin {
9
10    /**
11     * @inheritdoc
12     */
13    public function getType()
14    {
15        return 'substition';
16    }
17
18    /**
19     * @inheritdoc
20     */
21    public function getSort()
22    {
23        return 319;
24    }
25
26    /**
27     * @inheritdoc
28     */
29    public function connectTo($mode)
30    {
31        // only register if mediafile mode is enabled
32        if(!($this->getConf('mode') & Diagrams::MODE_MEDIA)) return;
33
34        $this->Lexer->addSpecialPattern('\{\{[^\}]+(?:\.svg)[^\}]*?\}\}',$mode,'plugin_diagrams_mediafile');
35    }
36
37    /**
38     * Parse SVG syntax into media data
39     *
40     * @param string $match
41     * @param int $state
42     * @param int $pos
43     * @param Doku_Handler $handler
44     * @return array|bool
45     */
46    public function handle($match, $state, $pos, Doku_Handler $handler)
47    {
48        return Doku_Handler_Parse_Media($match);
49    }
50
51    /**
52     * Render the diagram SVG as <object> instead of <img> to allow links,
53     * except when rendering to a PDF
54     *
55     * @param string $format
56     * @param Doku_Renderer $renderer
57     * @param array $data
58     * @return bool
59     */
60    public function render($format, Doku_Renderer $renderer, $data)
61    {
62        if ($format !== 'xhtml') return false;
63
64        $imageAttributes = array(
65            'class'   => 'media',
66            'width'   => $data['width'] ?: '',
67            'height'  => $data['height'] ?: '',
68            'title'   => $data['title'],
69        );
70
71
72        if(is_a($renderer, 'renderer_plugin_dw2pdf')) {
73            $imageAttributes['align'] = $data['align'];
74            $imageAttributes['src'] = ml($data['src'], ['cache' => 'nocache'], true, '&');
75            $renderer->doc .= '<img '. buildAttributes($imageAttributes) . '/>';
76        } else {
77            $imageAttributes['class'] .= ' diagrams-svg';
78            $imageAttributes['class'] .= ' media' . $data['align'];
79            $imageAttributes['data'] = ml($data['src'], ['cache' => 'nocache'], true, '&');
80            $imageAttributes['data-id'] = cleanID($data['src']);
81
82            $tag = '<object %s></object>';
83            $renderer->doc .= sprintf($tag, buildAttributes($imageAttributes, true));
84        }
85
86        return true;
87    }
88}
89