1<?php 2/** 3 * 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Myron Turner <turnermm02@shaw.ca> 7 * 8 */ 9// must be run within Dokuwiki 10if(!defined('DOKU_INC')) die(); 11 12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 13require_once(DOKU_PLUGIN.'syntax.php'); 14define('REPLACE_DIR', DOKU_INC . 'data/meta/macros/'); 15define('MACROS_FILE', REPLACE_DIR . 'macros.ser'); 16 17 18/** 19 * All DokuWiki plugins to extend the parser/rendering mechanism 20 * need to inherit from this class 21 */ 22class syntax_plugin_textinsert extends DokuWiki_Syntax_Plugin { 23 var $macros; 24 var $translations; 25 var $ns; 26 /** 27 * return some info 28 */ 29 30 /** 31 * What kind of syntax are we? 32 */ 33 function getType(){ 34 return 'substition'; 35 } 36 37 /** 38 * Where to sort in? 39 */ 40 function getSort(){ 41 return 155; 42 } 43 44 45 /** 46 * Connect pattern to lexer 47 */ 48 function connectTo($mode) { 49 $this->Lexer->addSpecialPattern('#@\!?[\w\-\._]+\!?@#',$mode,'plugin_textinsert'); 50 $this->Lexer->addSpecialPattern('#@\!\![\w\-\._]+@#',$mode,'plugin_textinsert'); 51 $this->Lexer->addSpecialPattern('#@[\w\-\._]+~.*?~@#',$mode,'plugin_textinsert'); 52 $this->Lexer->addSpecialPattern('#@[\w\-\._]+[\r\n]+~[^\r\n]+~@#',$mode,'plugin_textinsert'); 53 } 54 55 56 /** 57 * Handle the match 58 */ 59 function handle($match, $state, $pos, Doku_Handler $handler){ 60 61 $html=false; 62 $translation = false; 63 $match = substr($match,2,-2); 64 $match = trim($match); 65 if(strpos($match, 'HTML')) $html=true; 66 if(strpos($match, 'LANG_') !== false) { 67 $translation=true; 68 list($prefix,$trans) = explode('_',$match,2); 69 } 70 71 global $ID; 72 list($ns,$rest) = explode(':',$ID,2); 73 if(@file_exists($filename = DOKU_PLUGIN . "textinsert/lang/$ns/lang.php")) { 74 include $filename; 75 $this->translations = $lang; 76 77 } 78 79 if(@file_exists($filename = DOKU_PLUGIN . "textinsert/lang/$ns/macros.php")) { 80 include $filename; 81 $ar = 'lang_' .$ns; 82 $tr = $$ar; 83 if($this->translations) { 84 $this->translations = array_merge($lang,$tr); 85 } 86 else $this->translations = $tr; 87 } 88 89 if(!empty($ns)) { 90 $this->ns = $ns; 91 } 92 $this->macros = $this->get_macros(); 93 94 95 96 while(preg_match('#(\*\*|//|__|\'\').*?\1#m',$match )) { 97 $match = preg_replace_callback( 98 '#(\*\*|//|__|\'\')(.*?)(\1)#', 99 function($matches) { 100 $matches[1] = str_replace(array('**','//','__','\'\'',),array('<b>','<em>','<u>','<code>'),$matches[1]); 101 $matches[3] = str_replace(array('**','//','__','\'\''),array('</b>','</em>','</u>','</code>'),$matches[3]); 102 return $matches[1] . $matches[2] . $matches[3]; 103 },$match ); 104 } 105 106 if(preg_match('/(.*?)~([\s\S]+)~$/',$match,$subtitution)) { 107 $match=$subtitution[1]; 108 $subtitution[2] = str_replace('\\,',',',$subtitution[2]); 109 $substitutions=explode(',',$subtitution[2]); 110 $substitutions = preg_replace('#\/\/.+#',"",$substitutions); 111 $substitutions = preg_replace('#\\\n#',"<br />",$substitutions); 112 } 113 114 if(!array_key_exists($match, $this->macros) ) { 115 $err = $this->getLang('not_found'); 116 msg("$match $err", -1); 117 $match = ""; 118 } 119 else { 120 if($translation && isset($this->translations[$trans])){ 121 $match = $this->translations[$trans]; 122 } 123 else { 124 $match =$this->macros[$match]; 125 } 126 } 127 128 if(!is_array($substitutions)) $substitutions = array(); 129 for($i=0; $i<count($substitutions); $i++) { 130 $search = '%' . ($i+1); 131 $match = str_replace ($search , trim($substitutions[$i]), $match); 132 } 133 134 $match = $this->get_inserts($match,$translation); 135 136 if($html) { 137 $match = str_replace('<','<',$match); 138 $match = str_replace('>','>',$match); 139 } 140 141 return array($state,$match); 142 } 143 144 /** 145 * Create output 146 */ 147 function render($mode, Doku_Renderer $renderer, $data) { 148 global $INFO; 149 if($mode == 'xhtml'){ 150 list($state, $word) = $data; 151 If(strpos($word,'_ID_') !== false ) { 152 $word = str_replace('_ID_',$INFO['id'], $word); 153 } 154 $renderer->doc .= $word; 155 return true; 156 } 157 return false; 158 } 159 160 function get_macros() { 161 $a = array(); 162 if(file_exists(MACROS_FILE)) { 163 $a = unserialize(file_get_contents(MACROS_FILE)); 164 } 165 else if($this->getConf('farm')) { 166 $a = unserialize(file_get_contents(metaFN('macros','.ser'))); 167 } 168 $r = $this->get_std_replacements() ; 169 $result = array_merge($r,$a); 170 return array_merge($r,$a); 171 } 172 173 function get_inserts($match,$translation) { 174 $inserts = array(); 175 176 // replace embedded macros 177 if(preg_match_all('/#@(.*?)@#/',$match,$inserts)) { 178 $keys = $inserts[1]; 179 $pats = $inserts[0]; 180 181 for($i=0; $i<count($keys); $i++) { 182 $insert = $this->macros[$keys[$i]]; 183 if($translation ||strpos($keys[$i], 'LANG_') !== false) { 184 list($prefix,$trans) = explode('_',$keys[$i],2); 185 $_insert = $this->translations[$trans]; 186 if($_insert) $insert =$_insert; 187 } 188 $match = str_replace($pats[$i],$insert,$match); 189 } 190 191 } // end replace embedded macros 192 193 194 $entities = getEntities(); 195 $e_keys = array_keys($entities); 196 $e_values = array_values($entities); 197 $match = str_replace($e_keys,$e_values,$match); 198 199 return $match; 200 } 201 202 function get_std_replacements() { 203 if(!$this->getConf('stdreplace')) return array(); 204 global $conf; 205 global $INFO; 206 global $ID; 207 208 $file = noNS($ID); 209 $page = cleanID($file) ; 210 211 $names =array( 212 'ID', 213 'NS', 214 'FILE', 215 '!FILE', 216 '!FILE!', 217 'PAGE', 218 '!PAGE', 219 '!!PAGE', 220 '!PAGE!', 221 'USER', 222 'DATE', 223 '_ID_' 224 ); 225 226 $values = array( 227 $ID, 228 getNS($ID), 229 $file, 230 utf8_ucfirst($file), 231 utf8_strtoupper($file), 232 $page, 233 utf8_ucfirst($page), 234 utf8_ucwords($page), 235 utf8_strtoupper($page), 236 $_SERVER['REMOTE_USER'], 237 strftime($conf['dformat'], time()), 238 '_ID_' 239 ); 240 $std_replacements = array(); 241 for($i=0; $i<count($names) ; $i++) { 242 $std_replacements[$names[$i]] = $values[$i]; 243 } 244 245 return $std_replacements; 246} 247 248 function write_debug($what, $screen = false) { 249 return; 250 $what=print_r($what,true); 251 if($screen) { 252 msg('<pre>' . $what . '</pre>'); 253 return; 254 } 255 $handle=fopen("textinsert.txt",'a'); 256 fwrite($handle,"$what\n"); 257 fclose($handle); 258 } 259} 260 261 262