/
EMT_Tret.php
529 lines (458 loc) · 16.6 KB
/
EMT_Tret.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
<?php
namespace corpsepk\yii2emt;
use corpsepk\yii2emt\EMT_Lib;
/**
* Базовый класс для группы правил обработки текста
* Класс группы должен наследовать, данный класс и задавать
* в нём EMT_Tret::rules и EMT_Tret::$name
*
*/
class EMT_Tret {
/**
* Набор правил в данной группе, который задан изначально
* Его можно менять динамически добавляя туда правила с помощью put_rule
*
* @var unknown_type
*/
public $rules;
public $title;
private $disabled = array();
private $enabled = array();
protected $_text= '';
public $logging = false;
public $logs = false;
public $errors = false;
public $debug_enabled = false;
public $debug_info = array();
private $use_layout = false;
private $use_layout_set = false;
private $class_layout_prefix = false;
public $class_names = array();
public $classes = array();
public $settings = array();
/**
* Защищенные теги
*
* @todo привязать к методам из Jare_Typograph_Tool
*/
const BASE64_PARAGRAPH_TAG = 'cA==='; // p
const BASE64_BREAKLINE_TAG = 'YnIgLw==='; // br / (с пробелом и слэшем)
const BASE64_NOBR_OTAG = 'bm9icg==='; // nobr
const BASE64_NOBR_CTAG = 'L25vYnI=='; // /nobr
/**
* Типы кавычек
*/
const QUOTE_FIRS_OPEN = '«';
const QUOTE_FIRS_CLOSE = '»';
const QUOTE_CRAWSE_OPEN = '„';
const QUOTE_CRAWSE_CLOSE = '“';
private function log($str, $data = null)
{
if(!$this->logging) return;
$this->logs[] = array('info' => $str, 'data' => $data);
}
private function error($info, $data = null)
{
$this->errors[] = array('info' => $info, 'data' => $data);
$this->log('ERROR: '. $info , $data);
}
public function debug($place, &$after_text)
{
if(!$this->debug_enabled) return;
$this->debug_info[] = array(
'place' => $place,
'text' => $after_text,
);
}
/**
* Установить режим разметки для данного Трэта если не было раньше установлено,
* EMT_Lib::LAYOUT_STYLE - с помощью стилей
* EMT_Lib::LAYOUT_CLASS - с помощью классов
*
* @param int $kind
*/
public function set_tag_layout_ifnotset($layout)
{
if($this->use_layout_set) return;
$this->use_layout = $layout;
}
/**
* Установить режим разметки для данного Трэта,
* EMT_Lib::LAYOUT_STYLE - с помощью стилей
* EMT_Lib::LAYOUT_CLASS - с помощью классов
* EMT_Lib::LAYOUT_STYLE|EMT_Lib::LAYOUT_CLASS - оба метода
*
* @param int $kind
*/
public function set_tag_layout($layout = EMT_Lib::LAYOUT_STYLE)
{
$this->use_layout = $layout;
$this->use_layout_set = true;
}
public function set_class_layout_prefix($prefix)
{
$this->class_layout_prefix = $prefix;
}
public function debug_on()
{
$this->debug_enabled = true;
}
public function log_on()
{
$this->debug_enabled = true;
}
private function getmethod($name)
{
if(!$name) return false;
if(!method_exists($this, $name)) return false;
return array($this, $name);
}
private function _pre_parse()
{
$this->pre_parse();
foreach($this->rules as $rule)
{
if(!isset($rule['init'])) continue;
$m = $this->getmethod($rule['init']);
if(!$m) continue;
call_user_func($m);
}
}
private function _post_parse()
{
foreach($this->rules as $rule)
{
if(!isset($rule['deinit'])) continue;
$m = $this->getmethod($rule['deinit']);
if(!$m) continue;
call_user_func($m);
}
$this->post_parse();
}
private function rule_order_sort($a, $b)
{
if($a['order'] == $b['order']) return 0;
if($a['order'] < $b['order']) return -1;
return 1;
}
private function apply_rule($rule)
{
$name = $rule['id'];
//$this->log("Правило $name", "Применяем правило");
$disabled = (isset($this->disabled[$rule['id']]) && $this->disabled[$rule['id']]) || ((isset($rule['disabled']) && $rule['disabled']) && !(isset($this->enabled[$rule['id']]) && $this->enabled[$rule['id']]));
if($disabled)
{
$this->log("Правило $name", "Правило отключено" . ((isset($rule['disabled']) && $rule['disabled'])? " (по умолчанию)" : ""));
return;
}
if(isset($rule['function']) && $rule['function'])
{
if(!(isset($rule['pattern']) && $rule['pattern']))
{
if(method_exists($this, $rule['function']))
{
$this->log("Правило $name", "Используется метод ".$rule['function']." в правиле");
call_user_func(array($this, $rule['function']));
return;
}
if(function_exists($rule['function']))
{
$this->log("Правило $name", "Используется функция ".$rule['function']." в правиле");
call_user_func($rule['function']);
return;
}
$this->error('Функция '.$rule['function'].' из правила '.$rule['id']. " не найдена");
return ;
} else {
if(preg_match("/^[a-z_0-9]+$/i", $rule['function']))
{
if(method_exists($this, $rule['function']))
{
$this->log("Правило $name", "Замена с использованием preg_replace_callback с методом ".$rule['function']."");
$this->_text = preg_replace_callback($rule['pattern'], array($this, $rule['function']), $this->_text);
return;
}
if(function_exists($rule['function']))
{
$this->log("Правило $name", "Замена с использованием preg_replace_callback с функцией ".$rule['function']."");
$this->_text = preg_replace_callback($rule['pattern'], $rule['function'], $this->_text);
return;
}
$this->error('Функция '.$rule['function'].' из правила '.$rule['id']. " не найдена");
} else {
$this->_text = preg_replace_callback(
$rule['pattern'],
call_user_func($rule['function']),
$this->_text
);
$this->log('Замена с использованием preg_replace_callback с инлайн функцией из правила '.$rule['id']);
return;
}
return ;
}
}
if(isset($rule['simple_replace']) && $rule['simple_replace'])
{
if(isset($rule['case_sensitive']) && $rule['case_sensitive'])
{
$this->log("Правило $name", "Простая замена с использованием str_replace");
$this->_text = str_replace($rule['pattern'], $rule['replacement'], $this->_text);
return;
}
$this->log("Правило $name", "Простая замена с использованием str_ireplace");
$this->_text = str_ireplace($rule['pattern'], $rule['replacement'], $this->_text);
return;
}
$pattern = $rule['pattern'];
if(is_string($pattern)) $pattern = array($pattern);
$eval = false;
foreach($pattern as $patt)
{
$chr = substr($patt,0,1);
$preg_arr = explode($chr, $patt);
if(strpos($preg_arr[count($preg_arr)-1], "e")!==false)
{
$eval = true;
break;
}
}
if(!$eval)
{
$this->log("Правило $name", "Замена с использованием preg_replace");
do {
$this->_text = preg_replace($rule['pattern'], $rule['replacement'], $this->_text);
if(!(isset($rule['cycled']) && $rule['cycled'])) break;
} while(preg_match($rule['pattern'], $this->_text));
return;
}
$this->log("Правило $name", "Замена с использованием preg_replace_callback вместо eval");
$k = 0;
foreach($pattern as $patt)
{
$repl = is_string($rule['replacement']) ? $rule['replacement'] : $rule['replacement'][$k];
$chr = substr($patt,0,1);
$preg_arr = explode($chr, $patt);
if(strpos($preg_arr[count($preg_arr)-1], "e")!==false) // eval система
{
$preg_arr[count($preg_arr)-1] = str_replace("e","",$preg_arr[count($preg_arr)-1]);
$patt = implode($chr, $preg_arr);
$this->thereplacement = $repl;
do {
$this->_text = preg_replace_callback($patt, array($this, "thereplcallback"), $this->_text);
if(!(isset($rule['cycled']) && $rule['cycled'])) break;
} while(preg_match($patt, $this->_text));
} else {
do {
$this->_text = preg_replace($patt, $repl, $this->_text);
if(!(isset($rule['cycled']) && $rule['cycled'])) break;
} while(preg_match($patt, $this->_text));
}
$k++;
}
}
protected function preg_replace_e($pattern, $replacement, $text)
{
$chr = substr($pattern,0,1);
$preg_arr = explode($chr, $pattern);
if(strpos($preg_arr[count($preg_arr)-1], "e")===false) return preg_replace($pattern, $replacement, $text);
$preg_arr[count($preg_arr)-1] = str_replace("e","",$preg_arr[count($preg_arr)-1]);
$patt = implode($chr, $preg_arr);
$this->thereplacement = $replacement;
return preg_replace_callback($patt, array($this, "thereplcallback"), $text);
}
private $thereplacement = "";
private function thereplcallback($m)
{
$x = "";
eval('$x = '.($this->thereplacement?$this->thereplacement:'""').';');
return $x;
}
private function _apply($list)
{
$this->errors = array();
$this->_pre_parse();
$this->log("Применяется набор правил", implode(",",$list));
$rulelist = array();
foreach($list as $k)
{
$rule = $this->rules[$k];
$rule['id'] = $k;
$rule['order'] = isset($rule['order'])? $rule['order'] : 5 ;
$rulelist[] = $rule;
}
//usort($rulelist, array($this, "rule_order_sort"));
foreach($rulelist as $rule)
{
$this->apply_rule($rule);
$this->debug($rule['id'], $this->_text);
}
$this->_post_parse();
}
/**
* Создание защищенного тега с содержимым
*
* @see EMT_lib::build_safe_tag
* @param string $content
* @param string $tag
* @param array $attribute
* @return string
*/
protected function tag($content, $tag = 'span', $attribute = array())
{
if(isset($attribute['class']))
{
$classname = $attribute['class'];
if($classname == "nowrap")
{
if(!$this->is_on('nowrap'))
{
$tag = "nobr";
$attribute = array();
$classname = "";
}
}
if(isset($this->classes[$classname]))
{
$style_inline = $this->classes[$classname];
if($style_inline) $attribute['__style'] = $style_inline;
}
$classname = (isset($this->class_names[$classname]) ? $this->class_names[$classname] :$classname);
$classname = ($this->class_layout_prefix ? $this->class_layout_prefix : "" ).$classname;
$attribute['class'] = $classname;
}
return EMT_Lib::build_safe_tag($content, $tag, $attribute,
$this->use_layout === false? EMT_Lib::LAYOUT_STYLE : $this->use_layout );
}
/**
* Добавить правило в группу
*
* @param string $name
* @param array $params
*/
public function put_rule($name, $params)
{
$this->rules[$name] = $params;
return $this;
}
/**
* Отключить правило, в обработке
*
* @param string $name
*/
public function disable_rule($name)
{
$this->disabled[$name] = true;
unset($this->enabled[$name]);
}
/**
* Включить правило
*
* @param string $name
*/
public function enable_rule($name)
{
$this->enabled[$name] = true;
unset($this->disabled[$name]);
}
/**
* Добавить настройку в трет
*
* @param string $key ключ
* @param mixed $value значение
*/
public function set($key, $value)
{
$this->settings[$key] = $value;
}
/**
* Установлена ли настройка
*
* @param string $key
*/
public function is_on($key)
{
if(!isset($this->settings[$key])) return false;
$kk = $this->settings[$key];
return ((strtolower($kk)=="on") || ($kk === "1") || ($kk === true) || ($kk === 1));
}
/**
* Получить строковое значение настройки
*
* @param unknown_type $key
* @return unknown
*/
public function ss($key)
{
if(!isset($this->settings[$key])) return "";
return strval($this->settings[$key]);
}
/**
* Добавить настройку в правило
*
* @param string $rulename идентификатор правила
* @param string $key ключ
* @param mixed $value значение
*/
public function set_rule($rulename, $key, $value)
{
$this->rules[$rulename][$key] = $value;
}
/**
* Включить правила, согласно списку
*
* @param array $list список правил
* @param boolean $disable выкллючить их или включить
* @param boolean $strict строго, т.е. те которые не в списку будут тоже обработаны
*/
public function activate($list,$disable =false, $strict = true)
{
if(!is_array($list)) return ;
foreach($list as $rulename)
{
if($disable) $this->disable_rule($rulename); else $this->enable_rule($rulename);
}
if($strict)
{
foreach($this->rules as $rulename => $v)
{
if(in_array($rulename, $list)) continue;
if(!$disable) $this->disable_rule($rulename); else $this->enable_rule($rulename);
}
}
}
public function set_text(&$text)
{
$this->_text = &$text;
$this->debug_info = array();
$this->logs = array();
}
/**
* Применить к тексту
*
* @param string $text - текст к которому применить
* @param mixed $list - список правил, null - все правила
* @return string
*/
public function apply($list = null)
{
if(is_string($list)) $rlist = array($list);
elseif(is_array($list)) $rlist = $list;
else $rlist = array_keys($this->rules);
$this->_apply($rlist);
return $this->_text;
}
/**
* Код, выполняем до того, как применить правила
*
*/
public function pre_parse()
{
}
/**
* После выполнения всех правил, выполняется этот метод
*
*/
public function post_parse()
{
}
}