xref: /plugin/autotooltip/helper.php (revision 9e2add7bf23d160b622bec18dc3d2e97783985a5)
16bfd3f23SEli Fenton<?php
26bfd3f23SEli Fentonif(!defined('DOKU_INC')) die();
36bfd3f23SEli Fenton
46bfd3f23SEli Fenton/**
56bfd3f23SEli Fenton * Auto-Tooltip DokuWiki plugin
66bfd3f23SEli Fenton *
76bfd3f23SEli Fenton * @license    MIT
86bfd3f23SEli Fenton * @author     Eli Fenton
96bfd3f23SEli Fenton */
106bfd3f23SEli Fentonclass helper_plugin_autotooltip extends DokuWiki_Admin_Plugin {
116bfd3f23SEli Fenton	private $localRenderer;
126bfd3f23SEli Fenton
136bfd3f23SEli Fenton	public function __construct() {
146bfd3f23SEli Fenton		$this->localRenderer = new Doku_Renderer_xhtml;
156bfd3f23SEli Fenton	}
166bfd3f23SEli Fenton
176bfd3f23SEli Fenton
186bfd3f23SEli Fenton	/**
19*9e2add7bSEli Fenton	 * Return methods of this helper
20*9e2add7bSEli Fenton	 *
21*9e2add7bSEli Fenton	 * @return array with methods description
22*9e2add7bSEli Fenton	 */
23*9e2add7bSEli Fenton	function getMethods() {
24*9e2add7bSEli Fenton		$result = array();
25*9e2add7bSEli Fenton		$result[] = array(
26*9e2add7bSEli Fenton			'name' => 'forText',
27*9e2add7bSEli Fenton			'desc' => 'Manually construct a tooltip',
28*9e2add7bSEli Fenton			'params' => array(
29*9e2add7bSEli Fenton				'content' => 'string',
30*9e2add7bSEli Fenton				'tooltip' => 'string',
31*9e2add7bSEli Fenton				'title (optional)' => 'string',
32*9e2add7bSEli Fenton				'preTitle (optional)' => 'string',
33*9e2add7bSEli Fenton				'classes (optional)' => 'string',
34*9e2add7bSEli Fenton				'textStyles (optional)' => 'string',
35*9e2add7bSEli Fenton			),
36*9e2add7bSEli Fenton			'return' => array('result' => 'string')
37*9e2add7bSEli Fenton		);
38*9e2add7bSEli Fenton		$result[] = array(
39*9e2add7bSEli Fenton			'name' => 'forWikilink',
40*9e2add7bSEli Fenton			'desc' => 'Generate a tooltip from a wikilink',
41*9e2add7bSEli Fenton			'params' => array(
42*9e2add7bSEli Fenton				'id' => 'string',
43*9e2add7bSEli Fenton				'content (optional)' => 'string',
44*9e2add7bSEli Fenton				'preTitle (optional)' => 'string',
45*9e2add7bSEli Fenton				'classes (optional)' => 'string',
46*9e2add7bSEli Fenton				'textStyles (optional)' => 'string',
47*9e2add7bSEli Fenton			),
48*9e2add7bSEli Fenton			'return' => array('result' => 'string')
49*9e2add7bSEli Fenton		);
50*9e2add7bSEli Fenton		return $result;
51*9e2add7bSEli Fenton	}
52*9e2add7bSEli Fenton
53*9e2add7bSEli Fenton
54*9e2add7bSEli Fenton	/**
556bfd3f23SEli Fenton	 * Return a simple tooltip.
566bfd3f23SEli Fenton	 *
5707c401fcSEli Fenton	 * @param string $content - The on-page content. May contain newlines.
58948a1374SEli Fenton	 * @param string $tooltip - The tooltip content. Newlines will be rendered as line breaks.
59948a1374SEli Fenton	 * @param string $title - The title inside the tooltip.
60948a1374SEli Fenton	 * @param string $preTitle - Text to display before the title. Newlines will be rendered as line breaks.
616bfd3f23SEli Fenton	 * @param string $classes - CSS classes to add to this tooltip.
62969d3afcSEli Fenton	 * @param string $textStyle - CSS styles for the linked content
636bfd3f23SEli Fenton	 * @return string
646bfd3f23SEli Fenton	 */
65e4338dabSEli Fenton	function forText($content, $tooltip, $title='', $preTitle = '', $classes = '', $textStyle = '') {
66b7bccb09SEli Fenton		if (empty($classes)) {
67be213c94SEli Fenton			$classes = $this->getConf('style');
68be213c94SEli Fenton		}
69b7bccb09SEli Fenton		if (empty($classes)) {
70be213c94SEli Fenton			$classes = 'default';
71be213c94SEli Fenton		}
72be213c94SEli Fenton
73be213c94SEli Fenton		// Sanitize
74be213c94SEli Fenton		$classes = htmlspecialchars($classes);
75be213c94SEli Fenton		// Add the plugin prefix to all classes.
76be213c94SEli Fenton		$classes = preg_replace('/(\w+)/', 'plugin-autotooltip__$1', $classes);
77be213c94SEli Fenton
78be213c94SEli Fenton		$partCount = (empty($title) ? 0 : 1) + (empty($preTitle) ? 0 : 1) + (empty($tooltip) ? 0 : 1);
79be213c94SEli Fenton		if ($partCount > 1 || strchr($tooltip, "\n") !== FALSE || strlen($tooltip) > 40) {
80be213c94SEli Fenton			$classes .= ' plugin-autotooltip_big';
816bfd3f23SEli Fenton		}
826bfd3f23SEli Fenton
83969d3afcSEli Fenton		$textClass = '';
84969d3afcSEli Fenton		if (empty($textStyle)) {
85969d3afcSEli Fenton			$textClass = 'plugin-autotooltip_linked';
86969d3afcSEli Fenton			if (strstr($content, '<a ') === FALSE) {
87b7bccb09SEli Fenton				$textClass .= ' plugin-autotooltip_simple';
88969d3afcSEli Fenton			}
89969d3afcSEli Fenton		}
906bfd3f23SEli Fenton
91e4338dabSEli Fenton		$contentParts = [];
92e4338dabSEli Fenton		if (!empty($preTitle)) {
93948a1374SEli Fenton			$contentParts[] = $this->_formatTT($preTitle);
94e4338dabSEli Fenton		}
95e4338dabSEli Fenton		if (!empty($title)) {
96e4338dabSEli Fenton			$contentParts[] = '<span class="plugin-autotooltip-title">' . $title . '</span>';
97e4338dabSEli Fenton		}
98e4338dabSEli Fenton		if (!empty($tooltip)) {
99948a1374SEli Fenton			$contentParts[] = $this->_formatTT($tooltip);
100e4338dabSEli Fenton		}
101e4338dabSEli Fenton
102969d3afcSEli Fenton		return '<span class="' . $textClass . '" style="' . $textStyle . '" onmouseover="autotooltip.show(this)" onmouseout="autotooltip.hide()">' .
1036bfd3f23SEli Fenton			$content .
10407c401fcSEli Fenton			'<span class="plugin-autotooltip-hidden-classes">' . $classes . '</span>' .
105e4338dabSEli Fenton			'<span class="plugin-autotooltip-hidden-tip">' .
106e4338dabSEli Fenton			implode('<br><br>', $contentParts) .
107e4338dabSEli Fenton			'</span>' .
10807c401fcSEli Fenton		'</span>';
1096bfd3f23SEli Fenton	}
1106bfd3f23SEli Fenton
1116bfd3f23SEli Fenton
1126bfd3f23SEli Fenton	/**
1136bfd3f23SEli Fenton	 * Render a tooltip, with the title and abstract of a page.
1146bfd3f23SEli Fenton	 *
1156bfd3f23SEli Fenton	 * @param string $id - A page id.
116948a1374SEli Fenton	 * @param string $content - The on-page content. Newlines will be rendered as line breaks. Omit to use the page's title.
117948a1374SEli Fenton	 * @param string $preTitle - Text to display before the title in the tooltip. Newlines will be rendered as line breaks.
1186bfd3f23SEli Fenton	 * @param string $classes - CSS classes to add to this tooltip.
119969d3afcSEli Fenton	 * @param string $linkStyle - Style attribute for the link.
1206bfd3f23SEli Fenton	 * @return string
1216bfd3f23SEli Fenton	 */
122e4338dabSEli Fenton	function forWikilink($id, $content = null, $preTitle = '', $classes = '', $linkStyle = '') {
1236bfd3f23SEli Fenton		$title = p_get_metadata($id, 'title');
1246bfd3f23SEli Fenton		$abstract = p_get_metadata($id, 'description abstract');
125d852dc10SEli Fenton
1266bfd3f23SEli Fenton		// By default, the abstract starts with the title. Remove it so it's not displayed twice, but still fetch
1276bfd3f23SEli Fenton		// both pieces of metadata, in case another plugin rewrote the abstract.
128d852dc10SEli Fenton		$abstract = preg_replace('/^' . $this->_pregEscape($title) . '(\r?\n)+/', '', $abstract);
1296bfd3f23SEli Fenton
130812ebc9aSEli Fenton		$link = $this->localRenderer->internallink($id, $content ?: $title, null, true);
1316bfd3f23SEli Fenton
132969d3afcSEli Fenton		if (!empty($linkStyle)) {
133969d3afcSEli Fenton			$link = preg_replace('/<a /', '<a style="' . $linkStyle . '" ', $link);
134969d3afcSEli Fenton		}
135969d3afcSEli Fenton
1366bfd3f23SEli Fenton		if (page_exists($id)) {
1376bfd3f23SEli Fenton			// Remove the title attribute, since we have a better tooltip.
1386bfd3f23SEli Fenton			$link = preg_replace('/title="[^"]*"/', '', $link);
139be213c94SEli Fenton			return $this->forText($link, $abstract, $title, $preTitle, $classes);
1406bfd3f23SEli Fenton		}
1416bfd3f23SEli Fenton		else {
1426bfd3f23SEli Fenton			return $link;
1436bfd3f23SEli Fenton		}
1446bfd3f23SEli Fenton	}
1456bfd3f23SEli Fenton
1466bfd3f23SEli Fenton
1476bfd3f23SEli Fenton	/**
1486bfd3f23SEli Fenton	 * Format tooltip text.
1496bfd3f23SEli Fenton	 *
1506bfd3f23SEli Fenton	 * @param string $tt - Tooltip text.
1516bfd3f23SEli Fenton	 * @return string
1526bfd3f23SEli Fenton	 */
1536bfd3f23SEli Fenton	private function _formatTT($tt) {
154948a1374SEli Fenton		// Convert double-newlines into vertical space.
155948a1374SEli Fenton		$tt = preg_replace('/(\r?\n){2,}/', '<br><br>', $tt);
156948a1374SEli Fenton		// Single newlines get collapsed, just like in HTML.
157948a1374SEli Fenton		return preg_replace('/(\r?\n)/', ' ', $tt);
1586bfd3f23SEli Fenton	}
159d852dc10SEli Fenton
160d852dc10SEli Fenton
161d852dc10SEli Fenton	/**
162d852dc10SEli Fenton	 * Escape a string for inclusion in a regular expression, assuming forward slash is used as the delimiter.
163d852dc10SEli Fenton	 *
164d852dc10SEli Fenton	 * @param string $r - The regex string, without delimiters.
165d852dc10SEli Fenton	 * @return string
166d852dc10SEli Fenton	 */
167d852dc10SEli Fenton	private function _pregEscape($r) {
168d852dc10SEli Fenton		return preg_replace('/\//', '\\/', preg_quote($r));
169d852dc10SEli Fenton	}
1706bfd3f23SEli Fenton}
171