1<?php 2if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); 3if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 4if(!defined('DOKU_REL')) define('DOKU_REL', '/dokuwiki/'); 5 6/** 7 * Auto-Tooltip DokuWiki plugin 8 * 9 * @license MIT 10 * @author Eli Fenton 11 */ 12class syntax_plugin_autotooltip extends DokuWiki_Syntax_Plugin { 13 /** @type helper_plugin_autotooltip m_helper */ 14 private $m_helper; 15 16 public function __construct() { 17 $this->m_helper = plugin_load('helper', 'autotooltip'); 18 } 19 20 21 /** 22 * @return string 23 */ 24 function getType() { 25 return 'substition'; 26 } 27 28 29 /** 30 * @return string 31 */ 32 function getPType() { 33 return 'normal'; 34 } 35 36 37 /** 38 * @return int 39 */ 40 function getSort() { 41 return 165; 42 } 43 44 45 /** 46 * @param $mode 47 */ 48 function connectTo($mode) { 49 $this->Lexer->addSpecialPattern('<autott[^>]*>(?:[\s\S]*?</autott>)', $mode, 'plugin_autotooltip'); 50 } 51 52 53 /** 54 * @param string $match - The match from addEntryPattern. 55 * @param int $state - The DokuWiki event state. 56 * @param int $pos - The position in the full text. 57 * @param Doku_Handler $handler 58 * @return array|string 59 */ 60 function handle($match, $state, $pos, Doku_Handler $handler) { 61 $inner = []; 62 $classes = []; 63 $pageid = []; 64 preg_match('/<autott\s*([^>]+?)\s*>/', $match, $classes); 65 preg_match('/<autott[^>]*>\s*([\s\S]+)\s*<\/autott>/', $match, $inner); 66 if (count($inner) < 1) { 67 return 'ERROR'; 68 } 69 $inner = $inner[1]; 70 71 $data = []; 72 $data['classes'] = count($classes) >= 1 ? $classes[1] : ''; 73 74 if (strchr($inner, '<') === FALSE) { 75 $parts = array_map(function($s) {return trim($s);}, explode('|', $inner)); 76 // <autott class1 class2>wikilink|desc</autott> 77 if (cleanID($parts[0]) == $parts[0]) { 78 $data['pageid'] = $parts[0]; 79 if (count($parts) > 1) { 80 $data['content'] = $parts[1]; 81 } 82 return $data; 83 } 84 } 85 // <autott class1 class2><content></content><tip></tip><title></title><pageid></pageid></autott> 86 else { 87 $content = []; 88 $tip = []; 89 $title = []; 90 preg_match('/<content>([\s\S]+)<\/content>/', $inner, $content); 91 preg_match('/<tip>([\s\S]+)<\/tip>/', $inner, $tip); 92 preg_match('/<title>([\s\S]+)<\/title>/', $inner, $title); 93 94 if (count($content) >= 1 || count($pageid) >= 1) { 95 $data['content'] = count($content) >= 1 ? $content[1] : ''; 96 $data['tip'] = count($tip) >= 1 ? $tip[1] : null; 97 $data['title'] = count($title) >= 1 ? $title[1] : null; 98 99 return $data; 100 } 101 } 102 103 return 'ERROR'; 104 } 105 106 107 /** 108 * @param string $mode 109 * @param Doku_Renderer $renderer 110 * @param array|string $data - Data from handle() 111 * @return bool|void 112 */ 113 function render($mode, Doku_Renderer $renderer, $data) { 114 if ($mode == 'xhtml') { 115 if ($data == 'ERROR') { 116 msg('Error: Invalid instantiation of autotooltip plugin'); 117 } 118 else if (isset($data['pageid'])) { 119 $renderer->doc .= $this->m_helper->forWikilink($data['pageid'], $data['content']??'', '', $data['classes']??''); 120 } 121 else { 122 $renderer->doc .= $this->m_helper->forText($data['content']??'', $data['tip']??'', $data['title']??'', '', $data['classes']??''); 123 } 124 } 125 else { 126 if ($data == 'ERROR') { 127 $renderer->doc .= 'Error: Invalid instantiation of autotooltip plugin'; 128 } 129 else { 130 $renderer->doc .= $data['content'] ?? ''; 131 } 132 } 133 } 134} 135