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 */ 574b80a8ebSEli 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'); 64*d852dc10SEli Fenton 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. 67*d852dc10SEli Fenton $abstract = preg_replace('/^' . $this->_pregEscape($title) . '(\r?\n)+/', '', $abstract); 686bfd3f23SEli Fenton 69812ebc9aSEli Fenton $link = $this->localRenderer->internallink($id, $content ?: $title, null, true); 706bfd3f23SEli Fenton 71969d3afcSEli Fenton if (!empty($linkStyle)) { 72969d3afcSEli Fenton $link = preg_replace('/<a /', '<a style="' . $linkStyle . '" ', $link); 73969d3afcSEli Fenton } 74969d3afcSEli Fenton 756bfd3f23SEli Fenton if (page_exists($id)) { 766bfd3f23SEli Fenton // Remove the title attribute, since we have a better tooltip. 776bfd3f23SEli Fenton $link = preg_replace('/title="[^"]*"/', '', $link); 786bfd3f23SEli Fenton 7907c401fcSEli Fenton return '<span class="plugin-autotooltip_linked" onmouseover="autotooltip.show(this)" onmouseout="autotooltip.hide()">' . 806bfd3f23SEli Fenton $link . 8107c401fcSEli Fenton '<span class="plugin-autotooltip-hidden-classes">plugin-autotooltip_big ' . $classes . '</span>' . 8207c401fcSEli Fenton '<span class="plugin-autotooltip-hidden-tip">' . 8307c401fcSEli Fenton ' <span class="plugin-autotooltip-title">' . $title . '</span>' . 8407c401fcSEli Fenton ($abstract ? ' <br><br><span class="plugin-autotooltip_abstract">' . $this->_formatTT($abstract) . '</span>' : '') . 8507c401fcSEli Fenton '</span>' . 8607c401fcSEli Fenton '</span>'; 876bfd3f23SEli Fenton } 886bfd3f23SEli Fenton else { 896bfd3f23SEli Fenton return $link; 906bfd3f23SEli Fenton } 916bfd3f23SEli Fenton } 926bfd3f23SEli Fenton 936bfd3f23SEli Fenton 946bfd3f23SEli Fenton /** 956bfd3f23SEli Fenton * Format tooltip text. 966bfd3f23SEli Fenton * 976bfd3f23SEli Fenton * @param string $tt - Tooltip text. 986bfd3f23SEli Fenton * @return string 996bfd3f23SEli Fenton */ 1006bfd3f23SEli Fenton private function _formatTT($tt) { 101b6c83415SEli Fenton $tt = preg_replace('/\r?\n/', '<br>', $tt); 102b6c83415SEli Fenton return preg_replace('/(<br>){3,}/', '<br><br>', $tt); 1036bfd3f23SEli Fenton } 104*d852dc10SEli Fenton 105*d852dc10SEli Fenton 106*d852dc10SEli Fenton /** 107*d852dc10SEli Fenton * Escape a string for inclusion in a regular expression, assuming forward slash is used as the delimiter. 108*d852dc10SEli Fenton * 109*d852dc10SEli Fenton * @param string $r - The regex string, without delimiters. 110*d852dc10SEli Fenton * @return string 111*d852dc10SEli Fenton */ 112*d852dc10SEli Fenton private function _pregEscape($r) { 113*d852dc10SEli Fenton return preg_replace('/\//', '\\/', preg_quote($r)); 114*d852dc10SEli Fenton } 1156bfd3f23SEli Fenton} 116