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 $content = []; 65 $tip = []; 66 $pageid = []; 67 preg_match('/<autott\s*([^>]+?)\s*>/', $match, $classes); 68 preg_match('/<autott[^>]*>\s*([\s\S]+)\s*<\/autott>/', $match, $inner); 69 if (count($inner) < 1) { 70 return 'ERROR'; 71 } 72 $inner = $inner[1]; 73 74 $data = []; 75 $classes = count($classes) >= 1 ? preg_split('/\s+/', $classes[1]) : []; 76 $classes = implode(' ', array_map(function ($c) { 77 return 'plugin-autotooltip__' . $c; 78 }, $classes)); 79 $data['classes'] = strlen($classes) ? $classes : 'plugin-autotooltip__default'; 80 81 if (strchr($inner, '<') === FALSE) { 82 $parts = array_map(function($s) {return trim($s);}, explode('|', $inner)); 83 // <autott class1 class2>wikilink|desc</autott> 84 if (cleanID($parts[0]) == $parts[0]) { 85 $data['pageid'] = $parts[0]; 86 if (count($parts) > 1) { 87 $data['content'] = $parts[1]; 88 } 89 return $data; 90 } 91 } 92 // <autott class1 class2><content></content><tip></tip><pageid></pageid></autott> 93 else { 94 preg_match('/<content>([\s\S]+)<\/content>/', $inner, $content); 95 preg_match('/<tip>([\s\S]+)<\/tip>/', $inner, $tip); 96 97 if (count($content) >= 1 || count($pageid) >= 1) { 98 $data['content'] = count($content) >= 1 ? $content[1] : ''; 99 100 $data['tip'] = count($tip) >= 1 ? $tip[1] : null; 101 102 return $data; 103 } 104 } 105 106 return 'ERROR'; 107 } 108 109 110 /** 111 * @param string $mode 112 * @param Doku_Renderer $renderer 113 * @param array|string $data - Data from handle() 114 * @return bool|void 115 */ 116 function render($mode, Doku_Renderer $renderer, $data) { 117 if ($mode == 'xhtml') { 118 if ($data == 'ERROR') { 119 msg('Error: Invalid instantiation of autotooltip plugin'); 120 } 121 else if ($data['pageid']) { 122 $renderer->doc .= $this->m_helper->forWikilink($data['pageid'], $data['content'], $data['classes']); 123 } 124 else { 125 $renderer->doc .= $this->m_helper->forText($data['content'], $data['tip'], $data['classes']); 126 } 127 } 128 else { 129 if ($data == 'ERROR') { 130 $renderer->doc .= 'Error: Invalid instantiation of autotooltip plugin'; 131 } 132 else { 133 $renderer->doc .= $data['content']; 134 } 135 } 136 } 137} 138