1<?php
2if (!defined('DOKU_INC')) die();
3if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
4require_once DOKU_INC . 'inc/parser/xhtml.php';
5use dokuwiki\File\PageResolver;
6
7/**
8 * Auto-Tooltip DokuWiki renderer plugin. If the current renderer is ActionRenderer, the action
9 * plugin will be used instead.
10 *
11 * @license    MIT
12 * @author     Eli Fenton
13 */
14class renderer_plugin_autotooltip extends Doku_Renderer_xhtml {
15	/** @type helper_plugin_autotooltip m_helper */
16	private $m_helper;
17	private $m_exclude;
18
19	public function __construct() {
20		global $ID;
21		$this->m_helper = plugin_load('helper', 'autotooltip');
22		$this->m_exclude = $this->m_helper->isExcluded($ID);
23	}
24
25
26	/**
27	 * @param $format
28	 * @return bool
29	 */
30	function canRender($format) {
31		return $format == 'xhtml';
32	}
33
34
35	/**
36	 * Intercept Doku_Renderer_xhtml:internallink to give every wikilink a tooltip!
37	 *
38	 * @param string $id
39	 * @param null $name
40	 * @param null $search
41	 * @param bool $returnonly
42	 * @param string $linktype
43	 * @return string
44	 */
45	function internallink($id, $name = null, $search = null, $returnonly = false, $linktype = 'content') {
46		global $ID;
47		$fullId = $id;
48
49		$id = explode('?', $id, 2)[0];
50		$id = preg_replace('/\#.*$/', '', $id);
51		$id = preg_replace('/\?.*$/', '', $id);
52		$id = $id === '' ? $ID : $id;
53		$id = (new PageResolver($ID))->resolveId($id, $this->date_at, true);
54
55		if (!$this->m_exclude && page_exists($id) && $id != $ID) {
56			$link = parent::internallink($fullId, $name, $search, true, $linktype);
57
58			$meta = $this->m_helper->read_meta_fast($id);
59			$abstract = $meta['abstract'];
60			$link = $this->m_helper->stripNativeTooltip($link);
61			$link = $this->m_helper->forText($link, $abstract, $meta['title']);
62
63			if (!$returnonly) {
64				$this->doc .= $link;
65			}
66			return $link;
67		}
68		return parent::internallink($fullId, $name, $search, $returnonly, $linktype);
69	}
70}
71