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