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