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 * @return string 25 */ 26 function forText($content, $tooltip, $classes = '') { 27 if (!$classes) { 28 $classes = 'plugin-autotooltip__default'; 29 } 30 31 $textclass = strstr($content, '<a ') !== FALSE ? '' : 'plugin-autotooltip__simple'; 32 33 return '<span class="plugin-autotooltip_linked ' . $textclass . '" onmouseover="autotooltip.show(this)" onmouseout="autotooltip.hide()">' . 34 $content . 35 '<span class="plugin-autotooltip-hidden-classes">' . $classes . '</span>' . 36 '<span class="plugin-autotooltip-hidden-tip">' . $this->_formatTT($tooltip) . '</span>' . 37 '</span>'; 38 } 39 40 41 /** 42 * Render a tooltip, with the title and abstract of a page. 43 * 44 * @param string $id - A page id. 45 * @param string $content - The on-page content. May contain newlines. 46 * @param string $classes - CSS classes to add to this tooltip. 47 * @return string 48 */ 49 function forWikilink($id, $content, $classes = '') { 50 if (!$classes) { 51 $classes = 'plugin-autotooltip__default'; 52 } 53 54 $title = p_get_metadata($id, 'title'); 55 $abstract = p_get_metadata($id, 'description abstract'); 56 try { 57 // By default, the abstract starts with the title. Remove it so it's not displayed twice, but still fetch 58 // both pieces of metadata, in case another plugin rewrote the abstract. 59 $abstract = preg_replace('/^' . preg_quote($title) . '(\r?\n)+/', '', $abstract); 60 } catch(\Exception $e) { 61 // Ignore. 62 } 63 64 $link = $this->localRenderer->internallink($id, $content ?: $title, null, true); 65 66 if (page_exists($id)) { 67 // Remove the title attribute, since we have a better tooltip. 68 $link = preg_replace('/title="[^"]*"/', '', $link); 69 70 return '<span class="plugin-autotooltip_linked" onmouseover="autotooltip.show(this)" onmouseout="autotooltip.hide()">' . 71 $link . 72 '<span class="plugin-autotooltip-hidden-classes">plugin-autotooltip_big ' . $classes . '</span>' . 73 '<span class="plugin-autotooltip-hidden-tip">' . 74 ' <span class="plugin-autotooltip-title">' . $title . '</span>' . 75 ($abstract ? ' <br><br><span class="plugin-autotooltip_abstract">' . $this->_formatTT($abstract) . '</span>' : '') . 76 '</span>' . 77 '</span>'; 78 } 79 else { 80 return $link; 81 } 82 } 83 84 85 /** 86 * Format tooltip text. 87 * 88 * @param string $tt - Tooltip text. 89 * @return string 90 */ 91 private function _formatTT($tt) { 92 $tt = preg_replace('/\r?\n/', '<br>', $tt); 93 return preg_replace('/(<br>){3,}/', '<br><br>', $tt); 94 } 95} 96