xref: /plugin/mediathumbnails/syntax.php (revision 2b9233e23d5a9d8495894e671495925f651635c3)
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		$thumb = new thumbnail($mediapath_file,$this);
73		if ($thumb->create()) {
74			return array($mediapath_file,$thumb->getMediapath());
75		}
76
77		return array($mediapath_file);
78    }
79
80    /**
81     * Render xhtml output or metadata
82     *
83     * @param string        $mode     Renderer mode (supported modes: xhtml)
84     * @param Doku_Renderer $renderer The renderer
85     * @param array         $data     The data from the handler() function
86     *
87     * @return bool If rendering was successful.
88     */
89    public function render($mode, Doku_Renderer $renderer, $data)
90    {
91		list ($mediapath_file, $mediapath_thumbnail) = $data;
92
93        if ($mode == 'xhtml') {
94
95			// check if a thumbnail file was found
96			if (!$mediapath_thumbnail) {
97				if ($this->getConf('show_missing_thumb_error')) {
98					$renderer->doc .= trim($this->getConf('no_thumb_error_message')) . " " . $mediapath_file;
99					return true;
100				} else {
101					return false;
102				}
103			}
104
105			$src = ml($mediapath_thumbnail,array());
106
107			$i          = array();
108
109			$i['title'] = $mediapath_file;
110			$i['style'] = "max-width:".$this->getConf('thumb_max_dimension')."px;max-height:".$this->getConf('thumb_max_dimension')."px";
111
112			$iatt = buildAttributes($i);
113
114			$renderer->doc .= 	($this->getConf('link_to_media_file') ? '<a href="/lib/exe/fetch.php?media=' . $mediapath_file . '">' : '') .
115								'<img src="'.$src.'" '.$iatt.' />' .
116								($this->getConf('link_to_media_file') ? '</a>' : '');
117            return true;
118
119        } elseif ($mode == 'odt') {
120
121			// TODO: yet to implement
122			$renderer->cdata("");
123			return true;
124
125		}
126
127        return false;
128    }
129}