1<?php
2
3/**
4 * Class syntax_plugin_diagrams
5 */
6class syntax_plugin_diagrams 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');
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        if(is_a($renderer, 'renderer_plugin_dw2pdf')) {
60            $imageAttributes = array(
61                'class'   => 'media',
62                'src'     => ml($data['src']),
63                'width'   => $data['width'],
64                'height'  => $data['height'],
65                'align'   => $data['align'],
66                'title'   => $data['title']
67            );
68            $renderer->doc .= '<img '. buildAttributes($imageAttributes) . '/>';
69        } else {
70            $width = $data['width'] ? 'width="' . $data['width'] . '"' : '';
71            $height = $data['height'] ? 'height="' . $data['height'] . '"' : '';
72            $tag = '<object data="%s&cache=nocache" type="image/svg+xml" class="diagrams-svg media%s" %s %s></object>';
73            $renderer->doc .= sprintf($tag, ml($data['src']), $data['align'], $width, $height);
74        }
75
76        return true;
77    }
78}
79