1<?php
2/**
3 * DokuWiki Plugin mcitem (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  drcrazy <hcb@wowhcb.ru>
7 * some code borrowed from dokuwiki
8 */
9
10// must be run within Dokuwiki
11if (!defined('DOKU_INC')) die();
12
13class syntax_plugin_mcitem extends DokuWiki_Syntax_Plugin {
14
15	/**
16	 * @return array Plugin info
17	 */
18	public function getInfo() {
19		return array(
20				'author' => 'drcrazy',
21				'email'  => 'hcb@wowhcb.ru',
22				'date'   => '2017-05-17',
23				'name'   => 'Minecraft Item Plugin',
24				'desc'   => 'Adds Minecraft item link with icon to Dokuwiki.',
25				'url'    => 'https://github.com/drcrazy/mcitem'
26		);
27	}
28
29    /**
30     * @return string Syntax mode type
31     */
32    public function getType() {
33        return 'substition';
34    }
35    /**
36     * @return string Paragraph type
37     */
38    public function getPType() {
39        return 'normal';
40    }
41    /**
42     * @return int Sort order - Low numbers go before high numbers
43     */
44    public function getSort() {
45        return 55;
46    }
47
48    /**
49     * Connect lookup pattern to lexer.
50     *
51     * @param string $mode Parser mode
52     */
53    public function connectTo($mode) {
54        $this->Lexer->addEntryPattern('<mcitem>',$mode,'plugin_mcitem');
55    }
56
57   public function postConnect() {
58       $this->Lexer->addExitPattern('<\/mcitem>','plugin_mcitem');
59   }
60
61    /**
62     * Handle matches of the mcitem syntax
63     *
64     * @param string          $match   The match of the syntax
65     * @param int             $state   The state of the handler
66     * @param int             $pos     The position in the document
67     * @param Doku_Handler    $handler The handler
68     * @return array Data for the renderer
69     */
70    public function handle($match, $state, $pos, Doku_Handler $handler){
71        $data = array();
72        switch ($state) {
73        	case DOKU_LEXER_ENTER :
74        		break;
75        	case DOKU_LEXER_MATCHED :
76        		break;
77        	case DOKU_LEXER_UNMATCHED :
78        		$match = htmlspecialchars($match);
79        		$rawName = 'mods:' . $match;
80        		$match = explode(':', $match);
81        		$prettyName = str_replace('_', ' ', $match[1]);
82        		$prettyName = ucwords($prettyName);
83        		$data = array($state, $rawName, $prettyName);
84        		break;
85        	case DOKU_LEXER_EXIT :
86        		break;
87        	case DOKU_LEXER_SPECIAL :
88        		break;
89        }
90
91        return $data;
92    }
93
94    /**
95     * Render xhtml output or metadata
96     *
97     * @param string         $mode      Renderer mode (supported modes: xhtml)
98     * @param Doku_Renderer  $renderer  The renderer
99     * @param array          $data      The data from the handler() function
100     * @return bool If rendering was successful.
101     */
102    public function render($mode, Doku_Renderer $renderer, $data) {
103        if($mode != 'xhtml') return false;
104        $state = $data[0];
105        switch ($state) {
106        	case DOKU_LEXER_ENTER :
107        		break;
108        	case DOKU_LEXER_MATCHED :
109        		break;
110        	case DOKU_LEXER_UNMATCHED :
111        		$showIcon = $this->getConf('showIcon');
112        		if ($showIcon === 1) {
113        			global $ID;
114
115        			$imageName = $data[1] . '.png';
116        			list($src, $hash) = explode('#', $src, 2);
117        			resolve_mediaid(getNS($ID), $imageName, $exists, $this->date_at, true);
118
119        			if ($exists) {
120        				$renderer->internalmedia($imageName, $data[2], null, $this->getConf('iconWidth'), null, 'cache', 'nolink', FALSE);
121        			}
122
123        		}
124        		$boldLink = $this->getConf('boldLink');
125        		if ($boldLink === 1) {
126        			$renderer->strong_open();
127        		}
128        		$renderer->internallink($data[1], $data[2]);
129        		if ($boldLink === 1) {
130        			$renderer->strong_close();
131        		}
132        		break;
133        	case DOKU_LEXER_EXIT :
134        		break;
135        	case DOKU_LEXER_SPECIAL :
136        		break;
137        }
138
139
140        return true;
141    }
142}
143
144// vim:ts=4:sw=4:et:
145