localRenderer = new Doku_Renderer_xhtml;
}
/**
* Return methods of this helper
*
* @return array with methods description
*/
function getMethods() {
$result = array();
$result[] = array(
'name' => 'forText',
'desc' => 'Manually construct a tooltip',
'params' => array(
'content' => 'string',
'tooltip' => 'string',
'title (optional)' => 'string',
'preTitle (optional)' => 'string',
'classes (optional)' => 'string',
'textStyles (optional)' => 'string',
),
'return' => array('result' => 'string')
);
$result[] = array(
'name' => 'forWikilink',
'desc' => 'Generate a tooltip from a wikilink',
'params' => array(
'id' => 'string',
'content (optional)' => 'string',
'preTitle (optional)' => 'string',
'classes (optional)' => 'string',
'textStyles (optional)' => 'string',
),
'return' => array('result' => 'string')
);
return $result;
}
/**
* Return a simple tooltip.
*
* @param string $content - The on-page content. May contain newlines.
* @param string $tooltip - The tooltip content. Newlines will be rendered as line breaks.
* @param string $title - The title inside the tooltip.
* @param string $preTitle - Text to display before the title. Newlines will be rendered as line breaks.
* @param string $classes - CSS classes to add to this tooltip.
* @param string $textStyle - CSS styles for the linked content
* @return string
*/
function forText($content, $tooltip, $title='', $preTitle = '', $classes = '', $textStyle = '') {
if (empty($classes)) {
$classes = $this->getConf('style');
}
if (empty($classes)) {
$classes = 'default';
}
$delay = $this->getConf('delay') ?: 0;
// Sanitize
$classes = htmlspecialchars($classes);
// Add the plugin prefix to all classes.
$classes = preg_replace('/(\w+)/', 'plugin-autotooltip__$1', $classes);
$partCount = (empty($title) ? 0 : 1) + (empty($preTitle) ? 0 : 1) + (empty($tooltip) ? 0 : 1);
if ($partCount > 1 || strchr($tooltip, "\n") !== FALSE || strlen($tooltip) > 40) {
$classes .= ' plugin-autotooltip_big';
}
$textClass = '';
if (empty($textStyle)) {
$textClass = 'plugin-autotooltip_linked';
if (strstr($content, '_formatTT($preTitle);
}
if (!empty($title)) {
$contentParts[] = '' . $title . '';
}
if (!empty($tooltip)) {
$contentParts[] = $this->_formatTT($tooltip);
}
return '' .
$content .
'' . $classes . '' .
'' .
implode('
', $contentParts) .
'' .
'';
}
/**
* Render a tooltip, with the title and abstract of a page.
*
* @param string $id - A page id.
* @param string $content - The on-page content. Newlines will be rendered as line breaks. Omit to use the page's title.
* @param string $preTitle - Text to display before the title in the tooltip. Newlines will be rendered as line breaks.
* @param string $classes - CSS classes to add to this tooltip.
* @param string $linkStyle - Style attribute for the link.
* @return string
*/
function forWikilink($id, $content = null, $preTitle = '', $classes = '', $linkStyle = '') {
$title = p_get_metadata($id, 'title');
$link = $this->localRenderer->internallink($id, $content ?: $title, null, true);
if (!empty($linkStyle)) {
$link = preg_replace('/getAbstract($id, $title);
$link = $this->stripNativeTooltip($link);
return $this->forText($link, $abstract, $title, $preTitle, $classes);
}
else {
return $link;
}
}
/**
* Get a formatted abstract.
*
* @param string $id
* @param string $title
* @return string
*/
function getAbstract($id, $title) {
// Check the "description" plugin.
$abstract = p_get_metadata($id, 'plugin_description keywords');
// Default doku abstract is the first part of the page.
if (empty($abstract)) {
$abstract = p_get_metadata($id, 'description abstract');
}
// By default, the abstract starts with the title. Remove it so it's not displayed twice, but still fetch
// both pieces of metadata, in case another plugin rewrote the abstract.
return preg_replace('/^' . $this->_pregEscape($title) . '(\r?\n)+/', '', $abstract);
}
/**
* Strip the native title= tooltip from an anchor tag.
*
* @param string $link
* @return string
*/
function stripNativeTooltip($link) {
return preg_replace('/title="[^"]*"/', '', $link);
}
/**
* Format tooltip text.
*
* @param string $tt - Tooltip text.
* @return string
*/
private function _formatTT($tt) {
// Convert double-newlines into vertical space.
$tt = preg_replace('/(\r?\n){2,}/', '
', $tt);
// Single newlines get collapsed, just like in HTML.
return preg_replace('/(\r?\n)/', ' ', $tt);
}
/**
* Escape a string for inclusion in a regular expression, assuming forward slash is used as the delimiter.
*
* @param string $r - The regex string, without delimiters.
* @return string
*/
private function _pregEscape($r) {
return preg_replace('/\//', '\\/', preg_quote($r));
}
}