1<?php 2require_once(DOKU_PLUGIN.'autolink4/consts.php'); 3 4/******** 5 * This is a work in progress. See regex.php for active code. 6 ********/ 7 8/** 9 * Autolink 4 DokuWiki plugin 10 * 11 * @license MIT 12 * @author Eli Fenton 13 */ 14class syntax_plugin_autolink4_allwords extends DokuWiki_Syntax_Plugin { 15 use autotooltip4_consts; 16 17 /** @type helper_plugin_autotooltip $tooltip */ 18 private $tooltip; 19 /** @type helper_plugin_autolink4 $tooltip */ 20 private $helper; 21 22 public function __construct() { 23 if (!plugin_isdisabled('autotooltip')) { 24 $this->tooltip = plugin_load('helper', 'autotooltip'); 25 } 26 27 $this->helper = plugin_load('helper', 'autolink4'); 28 $this->helper->loadAndProcessConfigFile(); 29 } 30 31 32 /** 33 * @return string 34 */ 35 function getType() { 36 return 'substition'; 37 } 38 39 40 /** 41 * @return string 42 */ 43 function getPType() { 44 return 'normal'; 45 } 46 47 48 /** 49 * @return int 50 */ 51 function getSort() { 52 // Try not to interfere with any other lexer patterns. 53 return 1000; 54 } 55 56 57 //TODO: Doesn't seem to do anything. 58 function getAllowedTypes() { 59 global $PARSER_MODES; 60 return array_keys($PARSER_MODES); 61 } 62 63 64 /** 65 * @param $mode 66 */ 67 function connectTo($mode) { 68 global $ID; 69 $ns = getNS($ID); 70 71 /* 72 TODO: Optimization 73 Match any string of words (hoping that the high getSort avoids other plugins). 74 Make my own word-by-word parser, after sorting match strings and caching. (only for non-regexes) 75 Looking for earliest starting, longest match. 76 - See a word. Make array of partial matches with start pos. 77 - Next word - Eliminate in-progress matches, and add new ones. 78 - If a match is found and there are no in-progress matches of its start pos or earlier, do the 79 replacement. 80 Autolink all titles: 81 - Every time a page is indexed, add the title to the cache. Remove deleted. 82 */ 83 //TODO: Because this always starts at the first word, it wins over the regex version except when the regex version matches the first word. 84 // If I make it non-greedy, it competes with itself, and always matches exactly one word. 85 // I could get rid of the regex plugin and only use this one, but it could interfere with other plugins. 86 //$this->Lexer->addSpecialPattern('(?:[\w\'\-]+ *)+?', $mode, 'plugin_autolink4_allwords'); 87 } 88 89 90 /** 91 * Handle the found text, and send it off to render(). 92 * 93 * @param string $match - The found text, from addSpecialPattern. 94 * @param int $state - The DokuWiki event state. 95 * @param int $pos - The position in the full text. 96 * @param Doku_Handler $handler 97 * @return array|string 98 */ 99 function handle($match, $state, $pos, Doku_Handler $handler) { 100 msg($match); 101 return $match; 102 /* 103 $this->helper->init(); 104 105 // Load from cache 106 if (isset($this->helper->getSimpleSubs()[$match])) { 107 return $this->helper->getSimpleSubs()[$match]; 108 } 109 110 // Annoyingly, there's no way (I know of) to determine which match sent us here, so we have to loop through the 111 // whole list. 112 foreach ($this->helper->getRegexSubs() as &$s) { 113 if (preg_match('/^' . $s[self::$MATCH] . '$/', $match)) { 114 // Add all found matches to simpleSubs, so we don't have to loop more than once for the same string. 115 $mod = null; 116 if (!isset($this->helper->getSimpleSubs()[$match])) { 117 $mod = $s; 118 $mod[self::$TEXT] = $match; 119 $this->helper->getSimpleSubs()[$match] = $mod; 120 } 121 122 return $mod; 123 } 124 } 125 126 return $match; 127 */ 128 } 129 130 131 /** 132 * Render the replaced links. 133 * 134 * @param string $mode 135 * @param Doku_Renderer|Doku_Renderer_metadata $renderer 136 * @param array|string $data - Data from handle() 137 * @return bool 138 */ 139 function render($mode, Doku_Renderer $renderer, $data) { 140 $renderer->doc .= $data; 141 return true; 142 143 144 if (is_string($data)) { 145 $renderer->doc .= $data; 146 } 147 else if ($mode == 'xhtml') { 148 if ($this->tooltip && $data[self::$TOOLTIP]) { 149 $renderer->doc .= $this->tooltip->forWikilink($data[self::$TO], $data[self::$TEXT]); 150 } 151 else { 152 $renderer->internallink($data[self::$TO], $data[self::$TEXT]); 153 } 154 } 155 else { 156 if (!$renderer->capture) { 157 return false; 158 } 159 $renderer->doc .= $data[self::$TEXT]; 160 } 161 return true; 162 } 163 164 165 /** 166 * Is one namespace inside another. 167 * 168 * @param string $ns - Search inside this namespace. 169 * @param string $test - Look for this namespace. 170 * @return bool 171 */ 172 function _inNS($ns, $test) { 173 $len = strlen($test); 174 return !$len || substr($ns, 0, $len) == $test; 175 } 176 177 178 /** 179 * Are these two the same page> 180 * 181 * @param string $p1 - One page. 182 * @param string $p2 - Another page. 183 * @return bool 184 */ 185 function _isSamePage($p1, $p2) { 186 return preg_replace('/#.*/', '', $p1) == preg_replace('/#.*/', '', $p2); 187 } 188} 189