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('<tt2[^>]*>(?:[\s\S]*?</tt2>)', $mode, 'plugin_autotooltip'); 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 // <tt2 class1 class2><content></content><tip></tip><pageid></pageid></tt2> 62 $classes = []; 63 $content = []; 64 $tip = []; 65 $pageid = []; 66 preg_match('/<tt2\s*([^>]+?)\s*>/', $match, $classes); 67 preg_match('/<content>(.+)<\/content>/', $match, $content); 68 preg_match('/<tip>(.+)<\/tip>/', $match, $tip); 69 preg_match('/<pageid>(.+)<\/pageid>/', $match, $pageid); 70 71 if (count($content) >= 1) { 72 $data = ['content' => $content[1]]; 73 74 $classes = count($classes) >= 1 ? preg_split('/\s+/', $classes[1]) : []; 75 $classes = implode(' ', array_map(function($c) {return 'plugin-autotooltip__' . $c;}, $classes)); 76 $data['classes'] = strlen($classes) ? $classes : 'plugin-autotooltip__default'; 77 78 $data['pageid'] = count($pageid) >= 1 ? $pageid[1] : null; 79 $data['tip'] = count($tip) >= 1 ? $tip[1] : null; 80 81 return $data; 82 } 83 84 return 'ERROR'; 85 } 86 87 88 /** 89 * @param string $mode 90 * @param Doku_Renderer $renderer 91 * @param array|string $data - Data from handle() 92 * @return bool|void 93 */ 94 function render($mode, Doku_Renderer $renderer, $data) { 95 if ($data == 'ERROR') { 96 msg('Error: Invalid instantiation of autotooltip plugin'); 97 } 98 else if ($data['pageid']) { 99 $renderer->doc .= $this->m_helper->forWikilink($data['pageid'], $data['content'], $data['classes']); 100 } 101 else { 102 $renderer->doc .= $this->m_helper->forText($data['content'], $data['tip'], $data['classes']); 103 } 104 } 105} 106