-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.php
307 lines (240 loc) · 7.87 KB
/
helper.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
/**
* Helper Component for the Ad Hoc Tags Plugin
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Anika Henke <[email protected]>
* @author Sascha Leib <sascha.leib(at)kolmio.com>
*/
class helper_plugin_adhoctags extends DokuWiki_Plugin {
/* list of languages which normally use RTL scripts */
static protected $rtllangs = array('ar','dv','fa','ha','he','ks','ku','ps','ur','yi','arc');
/* list of right-to-left scripts (may override the language defaults to rtl) */
static protected $rtlscripts = array('arab','thaa','hebr','deva','shrd');
/* selection of left-to-right scripts (may override the language defaults to ltr) */
static protected $ltrscripts = array('latn','cyrl','grek','cyrs','armn');
/* Helper plugins should return info about the methods supported. */
public function getMethods() {
$result = array();
$result[] = array(
'name' => 'buildAttributes',
'desc' => 'writes out element attributes',
'params' => array(
'data' => 'string',
'custom' => 'array',
'addClass (optional)' => 'string',
'mode (optional)' => 'string'
),
'return' => array('attributes' => 'array')
);
return $result;
}
/**
* build attributes
*/
function buildAttributes($data, $myObj, $addClass='', $mode='xhtml') {
$attList = $this->getAttributes($data);
$out = '';
//dbg('attList=' . print_r($attList, true));
if ($mode=='xhtml') {
foreach($attList as $key => $val) {
switch ($key) {
/* common HTML attributes (always enabled) */
case 'id': /* id */
case 'class': /* custom classes */
case 'title': /* title */
case 'lang': /* language */
case 'tabindex': /* tabindex */
case 'is': /* is */
/* Microformat attributes */
case 'itemprop': /* item property */
case 'itemscope': /* item scope */
case 'itemref': /* item reference */
case 'itemid': /* item id (microformat) */
$out .= ' '.$key. (is_null($val) ? '' : '="'.$val.'"');
break;
case 'dir': /* custom attribute: direction */
if (in_array(strtolower(trim($val)), array('ltr','rtl','auto'))) {
$out .= ' dir="'. hsc($val).'"';
}
break;
case 'hidden': /* custom attribute: hidden */
if (in_array(strtolower(trim($val)), array('hidden','until-found'))) {
$out .= ' hidden="'. hsc($val).'"';
} else {
$out .= ' hidden';
}
break;
case 'style': /* style can be disabled */
if ($this->getConf('allowStyle') == '1') {
$out .= ' '.$key.'="'.hsc($val).'"';
}
break;
default:
/* special case 1: data-* attributes: */
if (preg_match('/^data-[a-z][a-z0-9_-]*$/', $key)) {
$out .= ' '.$key.'="'.hsc($val).'"';
}
/* special case 2: aria-* attributes: */
if (preg_match('/^aria-[a-z]+$/', $key)) {
$out .= ' '.$key.'="'.hsc($val).'"';
}
/* any other attribute: ask the class if it is allowed: */
if ($myObj->allowAttribute($key, $val)) {
$out .= ' '.$key. (is_null($val) ? '' : '="'.$val.'"');
}
}
}
// special case: no class name specified, but there is one passed down from a plugin:
if($addClass !== '' && !isset($attr['class'])) {
$out .= ' class="'.$addClass.'"';
}
}
return $out;
}
/**
* get attributes (pull apart the string between '<wrap' and '>')
* and identify classes, width, lang and dir
*
* @author Sascha Leib <sascha.leib(at)kolmio.com>
* @author Anika Henke <[email protected]>
* @author Christopher Smith <[email protected]>
* (parts taken from http://www.dokuwiki.org/plugin:box)
*/
function getAttributes($data) {
//dbg('getAttributes("$data="' . $data . '"');
// store the attributes here:
$attr = array();
// split up the attributes string (keep quoted and square brackets intact):
$tokens = $this->tokenizeAttr($data);
foreach ($tokens as $token) {
//get language attribute
if (preg_match('/^:([a-z\-]+)/', $token)) {
$attr['lang'] = strtolower(trim($token,':'));
continue;
}
//get id (IDs can not start with a number!)
if (preg_match('/^#([A-Za-z]\w+)/', $token)) {
$attr['id'] = trim($token,'#');
continue;
}
// get title (any quoted string)
if (preg_match('/^\"(.*)\"$/', $token)) {
$attr['title'] = trim($token,'"');
continue;
}
/* custom attributes */
if (preg_match('/^\[([^\]]+)\]$/', $token)) {
$cAttr = explode('=', trim($token,'[]'), 2);
//dbg('$token = ' . $token . ', $cAttr = ' . print_r($cAttr, true));
if ($cAttr) {
$attr[$cAttr[0]] = ( isset($cAttr[1]) ? $cAttr[1] : null );
}
continue;
}
//add to list of classes if it matches the pattern for class names:
if (preg_match('/^[\w\d\-\\_]*$/',$token)) {
$attr['class'] = (isset($attr['class']) ? $attr['class'].' ' : '') . $token;
}
}
/* improved RTL detection to make sure it covers more cases: */
if(!array_key_exists('dir', $attr) && array_key_exists('lang', $attr) && $attr['lang'] !== '') {
// turn the language code into an array of components:
$arr = explode('-', $attr['lang']);
// is the language iso code (first field) in the list of RTL languages?
$rtl = in_array($arr[0], self::$rtllangs);
// is there a Script specified somewhere which overrides the text direction?
$rtl = ($rtl xor (bool) array_intersect( $rtl ? self::$ltrscripts : self::$rtlscripts, $arr));
$attr['dir'] = ( $rtl ? 'rtl' : 'ltr' );
}
return $attr;
}
/**
* Split the input data into suitable tokens
*
* @author Sascha Leib <sascha.leib(at)kolmio.com>
*/
function tokenizeAttr($data) {
$result = array();
$token = ''; // temporary storage of each item
$escaped = false; // should the next character be treated "as is"?
$state = 0; // parser state (0 = default, 1 = in quotation, 2 = in square backets)
// loop over all characters:
forEach(str_split($data) as $c) {
switch($c) {
case ' ': // Space
case '\t': // Horizontal tabulation
case '\n': // Newline
case '\r': // Carriage return
if (!$escaped && $state == 0) {
if (trim($token)!==''){array_push($result, $token);}
$token = '';
} else {
$token .= ' ';
$escaped = false;
}
break;
case '"': // Quote
if (!$escaped) {
switch ($state) {
case 0:
if (trim($token)!==''){array_push($result, $token);}
$state = 1;
$token = $c;
break;
case 1:
$token .= $c;
array_push($result, $token);
$state = 0;
$token = '';
break;
case 2:
$token .= $c;
break;
default:
// should never happen!
}
} else {
$token .= $c;
$escaped = false;
}
break;
case '[': // Opening Square Brackets
if (!$escaped && $state == 0) {
if (trim($token)!==''){array_push($result, $token);}
$token = $c;
$state = 2;
} else {
$token .= $c;
}
break;
case ']': // Opening Square Brackets
if (!$escaped && $state == 2) {
$token .= $c;
array_push($result, $token);
$token = '';
$state = 0;
} else {
$token .= $c;
$escaped = false;
}
break;
case '\\': // Escape character
if (!$escaped) {
// next character is escaped:
$escaped = true;
} else {
$token .= $c;
$escaped = false;
}
break;
default:
$token .= $c;
$escaped = false;
}
}
if (trim($token)!=='') {array_push($result, $token);}
return $result;
}
/* Does anyone miss ODT support? */
}