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