xref: /plugin/autotooltip/syntax.php (revision 812ebc9a70338273c7d1acbe1b9b23e4709e1c95)
16bfd3f23SEli Fenton<?php
26bfd3f23SEli Fentonif(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
36bfd3f23SEli Fentonif(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
46bfd3f23SEli Fentonif(!defined('DOKU_REL')) define('DOKU_REL', '/dokuwiki/');
56bfd3f23SEli Fentonrequire_once(DOKU_PLUGIN.'syntax.php');
66bfd3f23SEli Fenton
76bfd3f23SEli Fenton/**
86bfd3f23SEli Fenton * Auto-Tooltip DokuWiki plugin
96bfd3f23SEli Fenton *
106bfd3f23SEli Fenton * @license    MIT
116bfd3f23SEli Fenton * @author     Eli Fenton
126bfd3f23SEli Fenton */
136bfd3f23SEli Fentonclass syntax_plugin_autotooltip extends DokuWiki_Syntax_Plugin {
146bfd3f23SEli Fenton	/** @type helper_plugin_autotooltip m_helper */
156bfd3f23SEli Fenton	private $m_helper;
166bfd3f23SEli Fenton
176bfd3f23SEli Fenton	public function __construct() {
186bfd3f23SEli Fenton		$this->m_helper = plugin_load('helper', 'autotooltip');
196bfd3f23SEli Fenton	}
206bfd3f23SEli Fenton
216bfd3f23SEli Fenton
226bfd3f23SEli Fenton	/**
236bfd3f23SEli Fenton	 * @return string
246bfd3f23SEli Fenton	 */
256bfd3f23SEli Fenton	function getType() {
266bfd3f23SEli Fenton		return 'substition';
276bfd3f23SEli Fenton	}
286bfd3f23SEli Fenton
296bfd3f23SEli Fenton
306bfd3f23SEli Fenton	/**
316bfd3f23SEli Fenton	 * @return string
326bfd3f23SEli Fenton	 */
336bfd3f23SEli Fenton	function getPType() {
346bfd3f23SEli Fenton		return 'normal';
356bfd3f23SEli Fenton	}
366bfd3f23SEli Fenton
376bfd3f23SEli Fenton
386bfd3f23SEli Fenton	/**
396bfd3f23SEli Fenton	 * @return int
406bfd3f23SEli Fenton	 */
416bfd3f23SEli Fenton	function getSort() {
426bfd3f23SEli Fenton		return 165;
436bfd3f23SEli Fenton	}
446bfd3f23SEli Fenton
456bfd3f23SEli Fenton
466bfd3f23SEli Fenton	/**
476bfd3f23SEli Fenton	 * @param $mode
486bfd3f23SEli Fenton	 */
496bfd3f23SEli Fenton	function connectTo($mode) {
506bfd3f23SEli Fenton		$this->Lexer->addSpecialPattern('<tt2[^>]*>(?:[\s\S]*?</tt2>)', $mode, 'plugin_autotooltip');
516bfd3f23SEli Fenton	}
526bfd3f23SEli Fenton
536bfd3f23SEli Fenton	/**
546bfd3f23SEli Fenton	 * @param string $match - The match from addEntryPattern.
556bfd3f23SEli Fenton	 * @param int $state - The DokuWiki event state.
566bfd3f23SEli Fenton	 * @param int $pos - The position in the full text.
576bfd3f23SEli Fenton	 * @param Doku_Handler $handler
586bfd3f23SEli Fenton	 * @return array|string
596bfd3f23SEli Fenton	 */
606bfd3f23SEli Fenton	function handle($match, $state, $pos, Doku_Handler $handler) {
616bfd3f23SEli Fenton		// <tt2 class1 class2><content></content><tip></tip><pageid></pageid></tt2>
626bfd3f23SEli Fenton		$classes = [];
636bfd3f23SEli Fenton		$content = [];
646bfd3f23SEli Fenton		$tip = [];
656bfd3f23SEli Fenton		$pageid = [];
666bfd3f23SEli Fenton		preg_match('/<tt2\s*([^>]+?)\s*>/', $match, $classes);
676bfd3f23SEli Fenton		preg_match('/<content>(.+)<\/content>/', $match, $content);
686bfd3f23SEli Fenton		preg_match('/<tip>(.+)<\/tip>/', $match, $tip);
696bfd3f23SEli Fenton		preg_match('/<pageid>(.+)<\/pageid>/', $match, $pageid);
706bfd3f23SEli Fenton
71*812ebc9aSEli Fenton		if (count($content) >= 1 || count($pageid) >= 1) {
72*812ebc9aSEli Fenton			$data = ['content' => count($content) >= 1 ? $content[1] : ''];
736bfd3f23SEli Fenton
746bfd3f23SEli Fenton			$classes = count($classes) >= 1 ? preg_split('/\s+/', $classes[1]) : [];
756bfd3f23SEli Fenton			$classes = implode(' ', array_map(function($c) {return 'plugin-autotooltip__' . $c;}, $classes));
766bfd3f23SEli Fenton			$data['classes'] = strlen($classes) ? $classes : 'plugin-autotooltip__default';
776bfd3f23SEli Fenton
786bfd3f23SEli Fenton			$data['pageid'] = count($pageid) >= 1 ? $pageid[1] : null;
796bfd3f23SEli Fenton			$data['tip'] = count($tip) >= 1 ? $tip[1] : null;
806bfd3f23SEli Fenton
816bfd3f23SEli Fenton			return $data;
826bfd3f23SEli Fenton		}
836bfd3f23SEli Fenton
846bfd3f23SEli Fenton		return 'ERROR';
856bfd3f23SEli Fenton	}
866bfd3f23SEli Fenton
876bfd3f23SEli Fenton
886bfd3f23SEli Fenton	/**
896bfd3f23SEli Fenton	 * @param string $mode
906bfd3f23SEli Fenton	 * @param Doku_Renderer $renderer
916bfd3f23SEli Fenton	 * @param array|string $data - Data from handle()
926bfd3f23SEli Fenton	 * @return bool|void
936bfd3f23SEli Fenton	 */
946bfd3f23SEli Fenton	function render($mode, Doku_Renderer $renderer, $data) {
956bfd3f23SEli Fenton		if ($data == 'ERROR') {
966bfd3f23SEli Fenton			msg('Error: Invalid instantiation of autotooltip plugin');
976bfd3f23SEli Fenton		}
986bfd3f23SEli Fenton		else if ($data['pageid']) {
996bfd3f23SEli Fenton			$renderer->doc .= $this->m_helper->forWikilink($data['pageid'], $data['content'], $data['classes']);
1006bfd3f23SEli Fenton		}
1016bfd3f23SEli Fenton		else {
1026bfd3f23SEli Fenton			$renderer->doc .= $this->m_helper->forText($data['content'], $data['tip'], $data['classes']);
1036bfd3f23SEli Fenton		}
1046bfd3f23SEli Fenton	}
1056bfd3f23SEli Fenton}
106