xref: /plugin/mediathumbnails/syntax.php (revision a99f150968d94c1d45e20e5e1fcfb35c280afe95)
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		// Locate the given media file and check if it can be opened as zip
65		$documenttype_command = substr($match, 12, -2); //strip markup
66
67        // if the command is using a pipe separator, the mediapath file must be extracted. As of now, the text behind the pipe will be ignored!
68        $params = explode("|", $documenttype_command);
69
70        $mediapath_file = $params[0];
71
72        $caption = $mediapath_file;
73
74        if (sizeof($params) > 1) {
75            $caption = $params[1];
76        }
77
78		$thumb = new thumbnail($mediapath_file,$this);
79
80        // if source file does not exist, return an array with the first element being null
81        if (!$thumb->getSourceFileExists()) {
82            return array(null,$mediapath_file,null);
83        }
84
85		if ($thumb->create()) {
86			return array($mediapath_file,$thumb->getMediapath(),$caption);
87		}
88
89		return array($mediapath_file,null,null);
90    }
91
92    /**
93     * Render xhtml output or metadata
94     *
95     * @param string        $mode     Renderer mode (supported modes: xhtml)
96     * @param Doku_Renderer $renderer The renderer
97     * @param array         $data     The data from the handler() function
98     *
99     * @return bool If rendering was successful.
100     */
101    public function render($mode, Doku_Renderer $renderer, $data)
102    {
103		list ($mediapath_file, $mediapath_thumbnail, $caption) = $data;
104
105        if ($mode == 'xhtml') {
106
107            // check if media source file exists
108			if (is_null($mediapath_file)) {
109				if ($this->getConf('show_missing_thumb_error')) {
110					$renderer->doc .= trim($this->getConf('no_media_error_message')) . " " . $mediapath_thumbnail;
111					return true;
112				} else {
113					return false;
114				}
115			}
116
117			// check if a thumbnail file was found
118			if (!$mediapath_thumbnail) {
119				if ($this->getConf('show_missing_thumb_error')) {
120					$renderer->doc .= trim($this->getConf('no_thumb_error_message')) . " " . $mediapath_file;
121					return true;
122				} else {
123					return false;
124				}
125			}
126
127			$src = ml($mediapath_thumbnail,array());
128
129			$i          = array();
130
131			$i['title'] = $mediapath_file;
132			$i['style'] = "max-width:".$this->getConf('thumb_max_dimension')."px;max-height:".$this->getConf('thumb_max_dimension')."px";
133
134			$iatt = buildAttributes($i);
135
136			$renderer->doc .= 	($this->getConf('link_to_media_file') ? '<a href="/lib/exe/fetch.php?media=' . $mediapath_file . '">' : '') .
137								'<img src="'.$src.'" title="'.$caption.'" '.$iatt.' />' .
138								($this->getConf('link_to_media_file') ? '</a>' : '');
139            return true;
140
141        } elseif ($mode == 'odt') {
142
143			// TODO: yet to implement
144			$renderer->cdata("");
145			return true;
146
147		}
148
149        return false;
150    }
151}