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