xref: /plugin/mediathumbnails/syntax.php (revision 91bcf68122fa491d83d59539c3e2a68d3bd7814a)
1<?php
2/**
3 * DokuWiki Plugin mediathumbnails (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Thomas Schäfer <thomas.schaefer@itschert.net>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11    die();
12}
13
14require('thumbnail.php');
15
16class syntax_plugin_mediathumbnails extends DokuWiki_Syntax_Plugin {
17
18	/**
19     * @return string Syntax mode type
20     */
21    public function getType()
22    {
23        return 'substition';
24    }
25
26    /**
27     * @return string Paragraph type
28     */
29    public function getPType()
30    {
31        return 'normal';
32    }
33
34    /**
35     * @return int Sort order - Low numbers go before high numbers
36     */
37    public function getSort()
38    {
39        return 1;
40    }
41
42    /**
43     * Connect lookup pattern to lexer.
44     *
45     * @param string $mode Parser mode
46     */
47    public function connectTo($mode)
48    {
49		$this->Lexer->addSpecialPattern("{{[ ]*thumbnail>.+?}}", $mode, substr(get_class($this), 7));
50	}
51
52    /**
53     * Handle matches of the mediathumbnails syntax
54     *
55     * @param string       $match   The match of the syntax
56     * @param int          $state   The state of the handler
57     * @param int          $pos     The position in the document
58     * @param Doku_Handler $handler The handler
59     *
60     * @return array Data for the renderer
61     */
62    public function handle($match, $state, $pos, Doku_Handler $handler)
63    {
64        // extract the internal reference from the syntax so it can be handled like a normal media file
65        $internalreference = str_replace("thumbnail>","",$match);//substr($match, 12, -2); //strip markup
66
67        // let dokuwiki core parse the media syntax inside the thumbnail syntax
68		$image_params = Doku_Handler_Parse_Media($internalreference);
69
70		$thumb = new thumbnail($image_params['src'],$this);
71
72        // if source file does not exist, return an array with the first element being null
73        if (!$thumb->getSourceFileExists()) {
74            return array(null,null,null);
75        }
76
77		$thumb->create_if_missing();
78
79        // use the thumbnail's mediapath and the image reference's parameters for rendering
80        $thumbnail_params = $image_params;
81        $thumbnail_params['src'] = $thumb->getMediapath();
82
83		return array($thumb->getSourceFilepath(),$thumb->getMediapath(),$thumbnail_params);
84    }
85
86    /**
87     * Render xhtml output or metadata
88     *
89     * @param string        $mode     Renderer mode (supported modes: xhtml)
90     * @param Doku_Renderer $renderer The renderer
91     * @param array         $data     The data from the handler() function
92     *
93     * @return bool If rendering was successful.
94     */
95    public function render($mode, Doku_Renderer $renderer, $data)
96    {
97		list ($mediapath_src, $mediapath_thumbnail, $image_params) = $data;
98
99        if ($mode == 'xhtml' || $mode == 'odt') {
100
101            // check if media source file exists
102			if (is_null($mediapath_src)) {
103				if ($this->getConf('show_missing_thumb_error')) {
104					$renderer->doc .= trim($this->getConf('no_media_error_message')) . " " . $mediapath_src;
105					return true;
106				} else {
107					return false;
108				}
109			}
110
111			// check if a thumbnail file was found
112			if (is_null($mediapath_thumbnail)) {
113				if ($this->getConf('show_missing_thumb_error')) {
114					$renderer->doc .= trim($this->getConf('no_thumb_error_message')) . " " . $mediapath_thumbnail;
115					return true;
116				} else {
117					return false;
118				}
119			}
120
121            $capped_width = $image_params['width'] > $this->getConf('thumb_max_dimension') ? $this->getConf('thumb_max_dimension') : $image_params['width'];
122            $capped_height = $image_params['height'] > $this->getConf('thumb_max_dimension') ? $this->getConf('thumb_max_dimension') : $image_params['height'];
123
124            $renderer->internalmedia(
125                $image_params['src'],
126                $image_params['title'],
127                $image_params['align'],
128                $capped_width,
129                $capped_height,
130                $image_params['cache'],
131                $image_params['linking'],
132                false
133            );
134
135            return true;
136
137        }
138
139        return false;
140    }
141}