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 HTML. 22 * @param string $tooltip - Tooltip content. May contain HTML. 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 '<div class="plugin-autotooltip_linked ' . $textclass . '" onmouseover="autotooltip.show(this)" onmouseout="autotooltip.hide()">' . 34 $content . 35 '<div class="plugin-autotooltip-hidden-classes">' . $classes . '</div>' . 36 '<div class="plugin-autotooltip-hidden-tip">' . $this->_formatTT($tooltip) . '</div>' . 37 '</div>'; 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 HTML. 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 '<div class="plugin-autotooltip_linked" onmouseover="autotooltip.show(this)" onmouseout="autotooltip.hide()">' . 71 $link . 72 '<div class="plugin-autotooltip-hidden-classes">plugin-autotooltip_big ' . $classes . '</div>' . 73 '<div class="plugin-autotooltip-hidden-tip">' . 74 ' <h3>' . $title . '</h3>' . 75 ' <div class="level3">' . 76 ' <p class="plugin-autotooltip_abstract">' . $this->_formatTT($abstract) . '</p>' . 77 ' </div>' . 78 '</div>' . 79 '</div>'; 80 } 81 else { 82 return $link; 83 } 84 } 85 86 87 /** 88 * Format tooltip text. 89 * 90 * @param string $tt - Tooltip text. 91 * @return string 92 */ 93 private function _formatTT($tt) { 94 return preg_replace('/\r?\n/', '<br>', $tt); 95 } 96} 97