xref: /plugin/autotooltip/helper.php (revision 4b80a8eb613c7bdd03d3d270e86d640d321ddf01)
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.
236bfd3f23SEli Fenton	 * @param string $classes - CSS classes to add to this tooltip.
24969d3afcSEli Fenton	 * @param string $textStyle - CSS styles for the linked content
256bfd3f23SEli Fenton	 * @return string
266bfd3f23SEli Fenton	 */
27969d3afcSEli Fenton	function forText($content, $tooltip, $classes = '', $textStyle = '') {
286bfd3f23SEli Fenton		if (!$classes) {
296bfd3f23SEli Fenton			$classes = 'plugin-autotooltip__default';
306bfd3f23SEli Fenton		}
316bfd3f23SEli Fenton
32969d3afcSEli Fenton		$textClass = '';
33969d3afcSEli Fenton		if (empty($textStyle)) {
34969d3afcSEli Fenton			$textClass = 'plugin-autotooltip_linked';
35969d3afcSEli Fenton			if (strstr($content, '<a ') === FALSE) {
36969d3afcSEli Fenton				$textClass .= ' plugin-autotooltip__simple';
37969d3afcSEli Fenton			}
38969d3afcSEli Fenton		}
396bfd3f23SEli Fenton
40969d3afcSEli Fenton		return '<span class="' . $textClass . '" style="' . $textStyle . '" onmouseover="autotooltip.show(this)" onmouseout="autotooltip.hide()">' .
416bfd3f23SEli Fenton			$content .
4207c401fcSEli Fenton			'<span class="plugin-autotooltip-hidden-classes">' . $classes . '</span>' .
4307c401fcSEli Fenton			'<span class="plugin-autotooltip-hidden-tip">' . $this->_formatTT($tooltip) . '</span>' .
4407c401fcSEli Fenton		'</span>';
456bfd3f23SEli Fenton	}
466bfd3f23SEli Fenton
476bfd3f23SEli Fenton
486bfd3f23SEli Fenton	/**
496bfd3f23SEli Fenton	 * Render a tooltip, with the title and abstract of a page.
506bfd3f23SEli Fenton	 *
516bfd3f23SEli Fenton	 * @param string $id - A page id.
5207c401fcSEli Fenton	 * @param string $content - The on-page content. May contain newlines.
536bfd3f23SEli Fenton	 * @param string $classes - CSS classes to add to this tooltip.
54969d3afcSEli Fenton	 * @param string $linkStyle - Style attribute for the link.
556bfd3f23SEli Fenton	 * @return string
566bfd3f23SEli Fenton	 */
57*4b80a8ebSEli Fenton	function forWikilink($id, $content = null, $classes = '', $linkStyle = '') {
586bfd3f23SEli Fenton		if (!$classes) {
596bfd3f23SEli Fenton			$classes = 'plugin-autotooltip__default';
606bfd3f23SEli Fenton		}
616bfd3f23SEli Fenton
626bfd3f23SEli Fenton		$title = p_get_metadata($id, 'title');
636bfd3f23SEli Fenton		$abstract = p_get_metadata($id, 'description abstract');
646bfd3f23SEli Fenton		try {
656bfd3f23SEli Fenton			// By default, the abstract starts with the title. Remove it so it's not displayed twice, but still fetch
666bfd3f23SEli Fenton			// both pieces of metadata, in case another plugin rewrote the abstract.
676bfd3f23SEli Fenton			$abstract = preg_replace('/^' . preg_quote($title) . '(\r?\n)+/', '', $abstract);
686bfd3f23SEli Fenton		} catch(\Exception $e) {
696bfd3f23SEli Fenton			// Ignore.
706bfd3f23SEli Fenton		}
716bfd3f23SEli Fenton
72812ebc9aSEli Fenton		$link = $this->localRenderer->internallink($id, $content ?: $title, null, true);
736bfd3f23SEli Fenton
74969d3afcSEli Fenton		if (!empty($linkStyle)) {
75969d3afcSEli Fenton			$link = preg_replace('/<a /', '<a style="' . $linkStyle . '" ', $link);
76969d3afcSEli Fenton		}
77969d3afcSEli Fenton
786bfd3f23SEli Fenton		if (page_exists($id)) {
796bfd3f23SEli Fenton			// Remove the title attribute, since we have a better tooltip.
806bfd3f23SEli Fenton			$link = preg_replace('/title="[^"]*"/', '', $link);
816bfd3f23SEli Fenton
8207c401fcSEli Fenton			return '<span class="plugin-autotooltip_linked" onmouseover="autotooltip.show(this)" onmouseout="autotooltip.hide()">' .
836bfd3f23SEli Fenton				$link .
8407c401fcSEli Fenton				'<span class="plugin-autotooltip-hidden-classes">plugin-autotooltip_big ' . $classes . '</span>' .
8507c401fcSEli Fenton				'<span class="plugin-autotooltip-hidden-tip">' .
8607c401fcSEli Fenton				'  <span class="plugin-autotooltip-title">' . $title . '</span>' .
8707c401fcSEli Fenton				($abstract ? '  <br><br><span class="plugin-autotooltip_abstract">' . $this->_formatTT($abstract) . '</span>' : '') .
8807c401fcSEli Fenton				'</span>' .
8907c401fcSEli Fenton				'</span>';
906bfd3f23SEli Fenton		}
916bfd3f23SEli Fenton		else {
926bfd3f23SEli Fenton			return $link;
936bfd3f23SEli Fenton		}
946bfd3f23SEli Fenton	}
956bfd3f23SEli Fenton
966bfd3f23SEli Fenton
976bfd3f23SEli Fenton	/**
986bfd3f23SEli Fenton	 * Format tooltip text.
996bfd3f23SEli Fenton	 *
1006bfd3f23SEli Fenton	 * @param string $tt - Tooltip text.
1016bfd3f23SEli Fenton	 * @return string
1026bfd3f23SEli Fenton	 */
1036bfd3f23SEli Fenton	private function _formatTT($tt) {
104b6c83415SEli Fenton		$tt = preg_replace('/\r?\n/', '<br>', $tt);
105b6c83415SEli Fenton		return preg_replace('/(<br>){3,}/', '<br><br>', $tt);
1066bfd3f23SEli Fenton	}
1076bfd3f23SEli Fenton}
108