xref: /plugin/combo/syntax/media.php (revision 21913ab3235d516e2fa19c7e3929b555b3a2bda1)
1<?php
2
3
4use ComboStrap\InternalMediaLink;
5use ComboStrap\PluginUtility;
6use ComboStrap\Tag;
7use ComboStrap\TagAttributes;
8
9require_once(__DIR__ . '/../class/RasterImageLink.php');
10
11if (!defined('DOKU_INC')) die();
12
13
14/**
15 * Internal media
16 */
17class syntax_plugin_combo_media extends DokuWiki_Syntax_Plugin
18{
19
20
21    const TAG = "media";
22
23    /**
24     * Used in the move plugin
25     * !!! The two last word of the plugin class !!!
26     */
27    const COMPONENT = 'combo_' . self::TAG;
28
29    /**
30     * The attribute that defines if the image is the first image in
31     * the component
32     *
33     */
34    const IS_FIRST_IMAGE_KEY = "isFirstImage";
35
36    /**
37     * If the image is a card illustration it's not printed
38     * The call is kept to update the index
39     */
40    const IS_CARD_ILLUSTRATION = "isCardIllustration";
41
42
43    function getType()
44    {
45        return 'formatting';
46    }
47
48    /**
49     * How Dokuwiki will add P element
50     *
51     *  * 'normal' - The plugin can be used inside paragraphs (inline)
52     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
53     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
54     *
55     * @see DokuWiki_Syntax_Plugin::getPType()
56     */
57    function getPType()
58    {
59        return 'normal';
60    }
61
62    function getAllowedTypes()
63    {
64        return array('substition', 'formatting', 'disabled');
65    }
66
67    function getSort()
68    {
69        return 201;
70    }
71
72
73    function connectTo($mode)
74    {
75        $enable = $this->getConf(InternalMediaLink::CONF_IMAGE_ENABLE, 1);
76        if (!$enable) {
77
78            // Inside a card, we need to take over and enable it
79            $modes = [
80                PluginUtility::getModeForComponent(syntax_plugin_combo_card::TAG),
81            ];
82            $enable = in_array($mode, $modes);
83        }
84
85        if ($enable) {
86            $this->Lexer->addSpecialPattern(InternalMediaLink::INTERNAL_MEDIA_PATTERN, $mode, PluginUtility::getModeForComponent($this->getPluginComponent()));
87        }
88    }
89
90
91    function handle($match, $state, $pos, Doku_Handler $handler)
92    {
93
94        switch ($state) {
95
96
97            // As this is a container, this cannot happens but yeah, now, you know
98            case DOKU_LEXER_SPECIAL :
99                $media = InternalMediaLink::createFromRenderMatch($match);
100                $attributes = $media->toCallStackArray();
101                $tag = new Tag(self::TAG, $attributes, $state, $handler);
102                $parent = $tag->getParent();
103                $parentTag = "";
104                if (!empty($parent)) {
105                    $parentTag = $parent->getName();
106                    if ($parentTag == syntax_plugin_combo_link::TAG) {
107                        /**
108                         * The image is in a link, we don't want another link
109                         * to the image
110                         */
111                        $attributes[TagAttributes::LINKING_KEY] = InternalMediaLink::LINKING_NOLINK_VALUE;
112                    }
113                }
114                $isFirstSibling = $tag->isFirstMeaningFullSibling();
115                return array(
116                    PluginUtility::STATE => $state,
117                    PluginUtility::ATTRIBUTES => $attributes,
118                    PluginUtility::CONTEXT => $parentTag,
119                    self::IS_FIRST_IMAGE_KEY => $isFirstSibling
120                );
121
122
123        }
124        return array();
125
126    }
127
128    /**
129     * Render the output
130     * @param string $format
131     * @param Doku_Renderer $renderer
132     * @param array $data - what the function handle() return'ed
133     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
134     * @see DokuWiki_Syntax_Plugin::render()
135     *
136     *
137     */
138    function render($format, Doku_Renderer $renderer, $data)
139    {
140
141        $attributes = $data[PluginUtility::ATTRIBUTES];
142        switch ($format) {
143
144            case 'xhtml':
145
146                /** @var Doku_Renderer_xhtml $renderer */
147                $attributes = $data[PluginUtility::ATTRIBUTES];
148                $media = InternalMediaLink::createFromCallStackArray($attributes, $renderer->date_at);
149                if ($media->isImage()) {
150
151                    $renderer->doc .= $media->renderMediaTagWithLink();
152
153                } else {
154
155                    /**
156                     * This is not a media image (a video)
157                     * Dokuwiki takes over
158                     */
159                    $src = $attributes['src'];
160                    $title = $attributes['title'];
161                    $align = $attributes['align'];
162                    $width = $attributes['width'];
163                    $height = $attributes['height'];
164                    $cache = $attributes['cache'];
165                    $linking = $attributes['linking'];
166                    $renderer->doc .= $renderer->internalmedia($src, $title, $align, $width, $height, $cache, $linking, true);
167
168                }
169
170                break;
171
172            case "metadata":
173
174                /**
175                 * Keep track of the metadata
176                 * @var Doku_Renderer_metadata $renderer
177                 */
178                self::registerImageMeta($attributes, $renderer);
179                break;
180
181        }
182        // unsupported $mode
183        return false;
184    }
185
186    /**
187     * @param array $attributes
188     * @param Doku_Renderer_metadata $renderer
189     */
190    static public function registerImageMeta($attributes, $renderer)
191    {
192        $src = $attributes['src'];
193        $title = $attributes['title'];
194        $align = $attributes['align'];
195        $width = $attributes['width'];
196        $height = $attributes['height'];
197        $cache = $attributes['cache']; // Cache: https://www.dokuwiki.org/images#caching
198        $linking = $attributes['linking'];
199        $renderer->internalmedia($src, $title, $align, $width, $height, $cache, $linking);
200    }
201
202
203}
204
205