xref: /plugin/autotooltip/helper.php (revision d852dc10afb59c22ceaaba2dafa976fd87a600ee)
1<?php
2if(!defined('DOKU_INC')) die();
3
4/**
5 * Auto-Tooltip DokuWiki plugin
6 *
7 * @license    MIT
8 * @author     Eli Fenton
9 */
10class helper_plugin_autotooltip extends DokuWiki_Admin_Plugin {
11	private $localRenderer;
12
13	public function __construct() {
14		$this->localRenderer = new Doku_Renderer_xhtml;
15	}
16
17
18	/**
19	 * Return a simple tooltip.
20	 *
21	 * @param string $content - The on-page content. May contain newlines.
22	 * @param string $tooltip - Tooltip content. May contain newlines.
23	 * @param string $classes - CSS classes to add to this tooltip.
24	 * @param string $textStyle - CSS styles for the linked content
25	 * @return string
26	 */
27	function forText($content, $tooltip, $classes = '', $textStyle = '') {
28		if (!$classes) {
29			$classes = 'plugin-autotooltip__default';
30		}
31
32		$textClass = '';
33		if (empty($textStyle)) {
34			$textClass = 'plugin-autotooltip_linked';
35			if (strstr($content, '<a ') === FALSE) {
36				$textClass .= ' plugin-autotooltip__simple';
37			}
38		}
39
40		return '<span class="' . $textClass . '" style="' . $textStyle . '" onmouseover="autotooltip.show(this)" onmouseout="autotooltip.hide()">' .
41			$content .
42			'<span class="plugin-autotooltip-hidden-classes">' . $classes . '</span>' .
43			'<span class="plugin-autotooltip-hidden-tip">' . $this->_formatTT($tooltip) . '</span>' .
44		'</span>';
45	}
46
47
48	/**
49	 * Render a tooltip, with the title and abstract of a page.
50	 *
51	 * @param string $id - A page id.
52	 * @param string $content - The on-page content. May contain newlines.
53	 * @param string $classes - CSS classes to add to this tooltip.
54	 * @param string $linkStyle - Style attribute for the link.
55	 * @return string
56	 */
57	function forWikilink($id, $content = null, $classes = '', $linkStyle = '') {
58		if (!$classes) {
59			$classes = 'plugin-autotooltip__default';
60		}
61
62		$title = p_get_metadata($id, 'title');
63		$abstract = p_get_metadata($id, 'description abstract');
64
65		// By default, the abstract starts with the title. Remove it so it's not displayed twice, but still fetch
66		// both pieces of metadata, in case another plugin rewrote the abstract.
67		$abstract = preg_replace('/^' . $this->_pregEscape($title) . '(\r?\n)+/', '', $abstract);
68
69		$link = $this->localRenderer->internallink($id, $content ?: $title, null, true);
70
71		if (!empty($linkStyle)) {
72			$link = preg_replace('/<a /', '<a style="' . $linkStyle . '" ', $link);
73		}
74
75		if (page_exists($id)) {
76			// Remove the title attribute, since we have a better tooltip.
77			$link = preg_replace('/title="[^"]*"/', '', $link);
78
79			return '<span class="plugin-autotooltip_linked" onmouseover="autotooltip.show(this)" onmouseout="autotooltip.hide()">' .
80				$link .
81				'<span class="plugin-autotooltip-hidden-classes">plugin-autotooltip_big ' . $classes . '</span>' .
82				'<span class="plugin-autotooltip-hidden-tip">' .
83				'  <span class="plugin-autotooltip-title">' . $title . '</span>' .
84				($abstract ? '  <br><br><span class="plugin-autotooltip_abstract">' . $this->_formatTT($abstract) . '</span>' : '') .
85				'</span>' .
86				'</span>';
87		}
88		else {
89			return $link;
90		}
91	}
92
93
94	/**
95	 * Format tooltip text.
96	 *
97	 * @param string $tt - Tooltip text.
98	 * @return string
99	 */
100	private function _formatTT($tt) {
101		$tt = preg_replace('/\r?\n/', '<br>', $tt);
102		return preg_replace('/(<br>){3,}/', '<br><br>', $tt);
103	}
104
105
106	/**
107	 * Escape a string for inclusion in a regular expression, assuming forward slash is used as the delimiter.
108	 *
109	 * @param string $r - The regex string, without delimiters.
110	 * @return string
111	 */
112	private function _pregEscape($r) {
113		return preg_replace('/\//', '\\/', preg_quote($r));
114	}
115}
116