1<?php
2if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
3if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
4if(!defined('DOKU_REL')) define('DOKU_REL', '/dokuwiki/');
5require_once(DOKU_PLUGIN.'syntax.php');
6
7/**
8 * Auto-Tooltip DokuWiki plugin
9 *
10 * @license    MIT
11 * @author     Eli Fenton
12 */
13class syntax_plugin_autotooltip extends DokuWiki_Syntax_Plugin {
14	/** @type helper_plugin_autotooltip m_helper */
15	private $m_helper;
16
17	public function __construct() {
18		$this->m_helper = plugin_load('helper', 'autotooltip');
19	}
20
21
22	/**
23	 * @return string
24	 */
25	function getType() {
26		return 'substition';
27	}
28
29
30	/**
31	 * @return string
32	 */
33	function getPType() {
34		return 'normal';
35	}
36
37
38	/**
39	 * @return int
40	 */
41	function getSort() {
42		return 165;
43	}
44
45
46	/**
47	 * @param $mode
48	 */
49	function connectTo($mode) {
50		$this->Lexer->addSpecialPattern('<autott[^>]*>(?:[\s\S]*?</autott>)', $mode, 'plugin_autotooltip');
51	}
52
53
54	/**
55	 * @param string $match - The match from addEntryPattern.
56	 * @param int $state - The DokuWiki event state.
57	 * @param int $pos - The position in the full text.
58	 * @param Doku_Handler $handler
59	 * @return array|string
60	 */
61	function handle($match, $state, $pos, Doku_Handler $handler) {
62		$inner = [];
63		$classes = [];
64		$pageid = [];
65		preg_match('/<autott\s*([^>]+?)\s*>/', $match, $classes);
66		preg_match('/<autott[^>]*>\s*([\s\S]+)\s*<\/autott>/', $match, $inner);
67		if (count($inner) < 1) {
68			return 'ERROR';
69		}
70		$inner = $inner[1];
71
72		$data = [];
73		$data['classes'] = count($classes) >= 1 ? $classes[1] : '';
74
75		if (strchr($inner, '<') === FALSE) {
76			$parts = array_map(function($s) {return trim($s);}, explode('|', $inner));
77			// <autott class1 class2>wikilink|desc</autott>
78			if (cleanID($parts[0]) == $parts[0]) {
79				$data['pageid'] = $parts[0];
80				if (count($parts) > 1) {
81					$data['content'] = $parts[1];
82				}
83				return $data;
84			}
85		}
86		// <autott class1 class2><content></content><tip></tip><title></title><pageid></pageid></autott>
87		else {
88			$content = [];
89			$tip = [];
90			$title = [];
91			preg_match('/<content>([\s\S]+)<\/content>/', $inner, $content);
92			preg_match('/<tip>([\s\S]+)<\/tip>/', $inner, $tip);
93			preg_match('/<title>([\s\S]+)<\/title>/', $inner, $title);
94
95			if (count($content) >= 1 || count($pageid) >= 1) {
96				$data['content'] = count($content) >= 1 ? $content[1] : '';
97				$data['tip'] = count($tip) >= 1 ? $tip[1] : null;
98				$data['title'] = count($title) >= 1 ? $title[1] : null;
99
100				return $data;
101			}
102		}
103
104		return 'ERROR';
105	}
106
107
108	/**
109	 * @param string $mode
110	 * @param Doku_Renderer $renderer
111	 * @param array|string $data - Data from handle()
112	 * @return bool|void
113	 */
114	function render($mode, Doku_Renderer $renderer, $data) {
115		if ($mode == 'xhtml') {
116			if ($data == 'ERROR') {
117				msg('Error: Invalid instantiation of autotooltip plugin');
118			}
119			else if ($data['pageid']) {
120				$renderer->doc .= $this->m_helper->forWikilink($data['pageid'], $data['content'], '', $data['classes']);
121			}
122			else {
123				$renderer->doc .= $this->m_helper->forText($data['content'], $data['tip'], $data['title'], '', $data['classes']);
124			}
125		}
126		else {
127			if ($data == 'ERROR') {
128				$renderer->doc .= 'Error: Invalid instantiation of autotooltip plugin';
129			}
130			else {
131				$renderer->doc .= $data['content'];
132			}
133		}
134	}
135}
136