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