xref: /plugin/autotooltip/syntax.php (revision 07c401fc837ee0b6ef0a426e8e3936460a819126)
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		$content = [];
65		$tip = [];
66		$pageid = [];
67		preg_match('/<autott\s*([^>]+?)\s*>/', $match, $classes);
68		preg_match('/<autott[^>]*>\s*([\s\S]+)\s*<\/autott>/', $match, $inner);
69		if (count($inner) < 1) {
70			return 'ERROR';
71		}
72		$inner = $inner[1];
73
74		// <autott class1 class2>wikilink</autott>
75		if (cleanID($inner) == $inner) {
76			return ['pageid' => $inner];
77		}
78		// <autott class1 class2><content></content><tip></tip><pageid></pageid></autott>
79		else {
80			preg_match('/<content>(.+)<\/content>/', $inner, $content);
81			preg_match('/<tip>(.+)<\/tip>/', $inner, $tip);
82			preg_match('/<pageid>(.+)<\/pageid>/', $inner, $pageid);
83
84			if (count($content) >= 1 || count($pageid) >= 1) {
85				$data = ['content' => count($content) >= 1 ? $content[1] : ''];
86
87				$classes = count($classes) >= 1 ? preg_split('/\s+/', $classes[1]) : [];
88				$classes = implode(' ', array_map(function ($c) {
89					return 'plugin-autotooltip__' . $c;
90				}, $classes));
91				$data['classes'] = strlen($classes) ? $classes : 'plugin-autotooltip__default';
92
93				$data['pageid'] = count($pageid) >= 1 ? $pageid[1] : null;
94				$data['tip'] = count($tip) >= 1 ? $tip[1] : null;
95
96				return $data;
97			}
98		}
99
100		return 'ERROR';
101	}
102
103
104	/**
105	 * @param string $mode
106	 * @param Doku_Renderer $renderer
107	 * @param array|string $data - Data from handle()
108	 * @return bool|void
109	 */
110	function render($mode, Doku_Renderer $renderer, $data) {
111		if ($data == 'ERROR') {
112			msg('Error: Invalid instantiation of autotooltip plugin');
113		}
114		else if ($data['pageid']) {
115			$renderer->doc .= $this->m_helper->forWikilink($data['pageid'], $data['content'], $data['classes']);
116		}
117		else {
118			$renderer->doc .= $this->m_helper->forText($data['content'], $data['tip'], $data['classes']);
119		}
120	}
121}
122