xref: /plugin/autotooltip/helper.php (revision e4338dab5ad278b21b1fd28f5d282f72c8c18fa5)
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	/**
196bfd3f23SEli Fenton	 * Return a simple tooltip.
206bfd3f23SEli Fenton	 *
2107c401fcSEli Fenton	 * @param string $content - The on-page content. May contain newlines.
2207c401fcSEli Fenton	 * @param string $tooltip - Tooltip content. May contain newlines.
23*e4338dabSEli Fenton	 * @param string $title - Tooltip title.
24*e4338dabSEli Fenton	 * @param string $preTitle - Text to display before the title.
256bfd3f23SEli Fenton	 * @param string $classes - CSS classes to add to this tooltip.
26969d3afcSEli Fenton	 * @param string $textStyle - CSS styles for the linked content
276bfd3f23SEli Fenton	 * @return string
286bfd3f23SEli Fenton	 */
29*e4338dabSEli Fenton	function forText($content, $tooltip, $title='', $preTitle = '', $classes = '', $textStyle = '') {
306bfd3f23SEli Fenton		if (!$classes) {
316bfd3f23SEli Fenton			$classes = 'plugin-autotooltip__default';
326bfd3f23SEli Fenton		}
336bfd3f23SEli Fenton
34969d3afcSEli Fenton		$textClass = '';
35969d3afcSEli Fenton		if (empty($textStyle)) {
36969d3afcSEli Fenton			$textClass = 'plugin-autotooltip_linked';
37969d3afcSEli Fenton			if (strstr($content, '<a ') === FALSE) {
38969d3afcSEli Fenton				$textClass .= ' plugin-autotooltip__simple';
39969d3afcSEli Fenton			}
40969d3afcSEli Fenton		}
416bfd3f23SEli Fenton
42*e4338dabSEli Fenton		$contentParts = [];
43*e4338dabSEli Fenton		if (!empty($preTitle)) {
44*e4338dabSEli Fenton			$contentParts[] = '<span>' . $this->_formatTT($preTitle) . '</span>';
45*e4338dabSEli Fenton		}
46*e4338dabSEli Fenton		if (!empty($title)) {
47*e4338dabSEli Fenton			$contentParts[] = '<span class="plugin-autotooltip-title">' . $title . '</span>';
48*e4338dabSEli Fenton		}
49*e4338dabSEli Fenton		if (!empty($tooltip)) {
50*e4338dabSEli Fenton			$contentParts[] = '<span>' . $this->_formatTT($tooltip) . '</span>';
51*e4338dabSEli Fenton		}
52*e4338dabSEli Fenton
53969d3afcSEli Fenton		return '<span class="' . $textClass . '" style="' . $textStyle . '" onmouseover="autotooltip.show(this)" onmouseout="autotooltip.hide()">' .
546bfd3f23SEli Fenton			$content .
5507c401fcSEli Fenton			'<span class="plugin-autotooltip-hidden-classes">' . $classes . '</span>' .
56*e4338dabSEli Fenton			'<span class="plugin-autotooltip-hidden-tip">' .
57*e4338dabSEli Fenton			implode('<br><br>', $contentParts) .
58*e4338dabSEli Fenton			'</span>' .
5907c401fcSEli Fenton		'</span>';
606bfd3f23SEli Fenton	}
616bfd3f23SEli Fenton
626bfd3f23SEli Fenton
636bfd3f23SEli Fenton	/**
646bfd3f23SEli Fenton	 * Render a tooltip, with the title and abstract of a page.
656bfd3f23SEli Fenton	 *
666bfd3f23SEli Fenton	 * @param string $id - A page id.
6707c401fcSEli Fenton	 * @param string $content - The on-page content. May contain newlines.
68*e4338dabSEli Fenton	 * @param string $preTitle - Text to display before the title.
696bfd3f23SEli Fenton	 * @param string $classes - CSS classes to add to this tooltip.
70969d3afcSEli Fenton	 * @param string $linkStyle - Style attribute for the link.
716bfd3f23SEli Fenton	 * @return string
726bfd3f23SEli Fenton	 */
73*e4338dabSEli Fenton	function forWikilink($id, $content = null, $preTitle = '', $classes = '', $linkStyle = '') {
746bfd3f23SEli Fenton		if (!$classes) {
756bfd3f23SEli Fenton			$classes = 'plugin-autotooltip__default';
766bfd3f23SEli Fenton		}
776bfd3f23SEli Fenton
786bfd3f23SEli Fenton		$title = p_get_metadata($id, 'title');
796bfd3f23SEli Fenton		$abstract = p_get_metadata($id, 'description abstract');
80d852dc10SEli Fenton
816bfd3f23SEli Fenton		// By default, the abstract starts with the title. Remove it so it's not displayed twice, but still fetch
826bfd3f23SEli Fenton		// both pieces of metadata, in case another plugin rewrote the abstract.
83d852dc10SEli Fenton		$abstract = preg_replace('/^' . $this->_pregEscape($title) . '(\r?\n)+/', '', $abstract);
846bfd3f23SEli Fenton
85812ebc9aSEli Fenton		$link = $this->localRenderer->internallink($id, $content ?: $title, null, true);
866bfd3f23SEli Fenton
87969d3afcSEli Fenton		if (!empty($linkStyle)) {
88969d3afcSEli Fenton			$link = preg_replace('/<a /', '<a style="' . $linkStyle . '" ', $link);
89969d3afcSEli Fenton		}
90969d3afcSEli Fenton
916bfd3f23SEli Fenton		if (page_exists($id)) {
926bfd3f23SEli Fenton			// Remove the title attribute, since we have a better tooltip.
936bfd3f23SEli Fenton			$link = preg_replace('/title="[^"]*"/', '', $link);
94*e4338dabSEli Fenton			return $this->forText($link, $abstract, $title, $preTitle, "plugin-autotooltip_big $classes");
956bfd3f23SEli Fenton		}
966bfd3f23SEli Fenton		else {
976bfd3f23SEli Fenton			return $link;
986bfd3f23SEli Fenton		}
996bfd3f23SEli Fenton	}
1006bfd3f23SEli Fenton
1016bfd3f23SEli Fenton
1026bfd3f23SEli Fenton	/**
1036bfd3f23SEli Fenton	 * Format tooltip text.
1046bfd3f23SEli Fenton	 *
1056bfd3f23SEli Fenton	 * @param string $tt - Tooltip text.
1066bfd3f23SEli Fenton	 * @return string
1076bfd3f23SEli Fenton	 */
1086bfd3f23SEli Fenton	private function _formatTT($tt) {
109b6c83415SEli Fenton		$tt = preg_replace('/\r?\n/', '<br>', $tt);
110b6c83415SEli Fenton		return preg_replace('/(<br>){3,}/', '<br><br>', $tt);
1116bfd3f23SEli Fenton	}
112d852dc10SEli Fenton
113d852dc10SEli Fenton
114d852dc10SEli Fenton	/**
115d852dc10SEli Fenton	 * Escape a string for inclusion in a regular expression, assuming forward slash is used as the delimiter.
116d852dc10SEli Fenton	 *
117d852dc10SEli Fenton	 * @param string $r - The regex string, without delimiters.
118d852dc10SEli Fenton	 * @return string
119d852dc10SEli Fenton	 */
120d852dc10SEli Fenton	private function _pregEscape($r) {
121d852dc10SEli Fenton		return preg_replace('/\//', '\\/', preg_quote($r));
122d852dc10SEli Fenton	}
1236bfd3f23SEli Fenton}
124