1
|
|
|
<?php |
2
|
|
|
namespace EMT\Tret; |
3
|
|
|
|
4
|
|
|
use EMT\Util; |
5
|
|
|
|
6
|
|
|
class Quote extends AbstractTret |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* Базовые параметры тофа |
10
|
|
|
* |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
public $title = "Кавычки"; |
14
|
|
|
|
15
|
|
|
public $rules = array( |
16
|
|
|
'quotes_outside_a' => array( |
17
|
|
|
'description' => 'Кавычки вне тэга <a>', |
18
|
|
|
'pattern' => '/(\<%%\_\_[^\>]+\>)\"(.+?)\"(\<\/%%\_\_[^\>]+\>)/s', |
19
|
|
|
'replacement' => '"\1\2\3"' |
20
|
|
|
), |
21
|
|
|
|
22
|
|
|
'open_quote' => array( |
23
|
|
|
'description' => 'Открывающая кавычка', |
24
|
|
|
'pattern' => '/(^|\(|\s|\>|-)(\"|\\\")(\S+)/iue', |
25
|
|
|
'replacement' => '$m[1] . \EMT\Tret\AbstractTret::QUOTE_FIRS_OPEN . $m[3]' |
26
|
|
|
), |
27
|
|
|
'close_quote' => array( |
28
|
|
|
'description' => 'Закрывающая кавычка', |
29
|
|
|
'pattern' => '/([a-zа-яё0-9]|\.|\&hellip\;|\!|\?|\>|\)|\:)((\"|\\\")+)(\.|\&hellip\;|\;|\:|\?|\!|\,|\s|\)|\<\/|$)/uie', |
30
|
|
|
'replacement' => '$m[1] . str_repeat(\EMT\Tret\AbstractTret::QUOTE_FIRS_CLOSE, substr_count($m[2],"\"") ) . $m[4]' |
31
|
|
|
), |
32
|
|
|
'close_quote_adv' => array( |
33
|
|
|
'description' => 'Закрывающая кавычка особые случаи', |
34
|
|
|
'pattern' => |
35
|
|
|
array( |
36
|
|
|
'/([a-zа-яё0-9]|\.|\&hellip\;|\!|\?|\>|\)|\:)((\"|\\\"|\«\;)+)(\<[^\>]+\>)(\.|\&hellip\;|\;|\:|\?|\!|\,|\)|\<\/|$| )/uie', |
37
|
|
|
'/([a-zа-яё0-9]|\.|\&hellip\;|\!|\?|\>|\)|\:)(\s+)((\"|\\\")+)(\s+)(\.|\&hellip\;|\;|\:|\?|\!|\,|\)|\<\/|$| )/uie', |
38
|
|
|
'/\>(\«\;)\.($|\s|\<)/ui', |
39
|
|
|
'/\>(\«\;),($|\s|\<|\S)/ui', |
40
|
|
|
), |
41
|
|
|
'replacement' => |
42
|
|
|
array( |
43
|
|
|
'$m[1] . str_repeat(\EMT\Tret\AbstractTret::QUOTE_FIRS_CLOSE, substr_count($m[2],"\"")+substr_count($m[2],"«") ) . $m[4]. $m[5]', |
44
|
|
|
'$m[1] .$m[2]. str_repeat(\EMT\Tret\AbstractTret::QUOTE_FIRS_CLOSE, substr_count($m[3],"\"")+substr_count($m[3],"«") ) . $m[5]. $m[6]', |
45
|
|
|
'>».\2', |
46
|
|
|
'>»,\2', |
47
|
|
|
), |
48
|
|
|
), |
49
|
|
|
'open_quote_adv' => array( |
50
|
|
|
'description' => 'Открывающая кавычка особые случаи', |
51
|
|
|
'pattern' => '/(^|\(|\s|\>)(\"|\\\")(\s)(\S+)/iue', |
52
|
|
|
'replacement' => '$m[1] . \EMT\Tret\AbstractTret::QUOTE_FIRS_OPEN .$m[4]' |
53
|
|
|
), |
54
|
|
|
'quotation' => array( |
55
|
|
|
'description' => 'Внутренние кавычки-лапки и дюймы', |
56
|
|
|
'function' => 'build_sub_quotations' |
57
|
|
|
), |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $text |
62
|
|
|
*/ |
63
|
|
|
protected function inject_in($pos, $text) |
64
|
|
|
{ |
65
|
|
|
for ($i = 0; $i < strlen($text); $i++) $this->_text[$pos + $i] = $text[$i]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function build_sub_quotations() |
69
|
|
|
{ |
70
|
|
|
global $__ax, $__ay; |
71
|
|
|
$okposstack = array('0'); |
72
|
|
|
$okpos = 0; |
73
|
|
|
$level = 0; |
74
|
|
|
$off = 0; |
75
|
|
|
while (true) { |
76
|
|
|
$p = Util::strpos_ex($this->_text, array("«", "»"), $off); |
77
|
|
|
if ($p === false) break; |
78
|
|
View Code Duplication |
if ($p['str'] == "«") { |
|
|
|
|
79
|
|
|
if ($level > 0) if (!$this->is_on('no_bdquotes')) $this->inject_in($p['pos'], self::QUOTE_CRAWSE_OPEN); |
80
|
|
|
$level++; |
81
|
|
|
} |
82
|
|
View Code Duplication |
if ($p['str'] == "»") { |
|
|
|
|
83
|
|
|
$level--; |
84
|
|
|
if ($level > 0) if (!$this->is_on('no_bdquotes')) $this->inject_in($p['pos'], self::QUOTE_CRAWSE_CLOSE); |
85
|
|
|
} |
86
|
|
|
$off = $p['pos'] + strlen($p['str']); |
87
|
|
|
if ($level == 0) { |
88
|
|
|
$okpos = $off; |
89
|
|
|
array_push($okposstack, $okpos); |
90
|
|
|
} elseif ($level < 0) { // уровень стал меньше нуля |
91
|
|
|
if (!$this->is_on('no_inches')) { |
92
|
|
|
do { |
93
|
|
|
$lokpos = array_pop($okposstack); |
94
|
|
|
$k = substr($this->_text, $lokpos, $off - $lokpos); |
95
|
|
|
$k = str_replace(self::QUOTE_CRAWSE_OPEN, self::QUOTE_FIRS_OPEN, $k); |
96
|
|
|
$k = str_replace(self::QUOTE_CRAWSE_CLOSE, self::QUOTE_FIRS_CLOSE, $k); |
97
|
|
|
//$k = preg_replace("/(^|[^0-9])([0-9]+)\»\;/ui", '\1\2″', $k, 1, $amount); |
98
|
|
|
|
99
|
|
|
$amount = 0; |
100
|
|
|
$__ax = preg_match_all("/(^|[^0-9])([0-9]+)\»\;/ui", $k, $m); |
101
|
|
|
$__ay = 0; |
102
|
|
|
if ($__ax) { |
103
|
|
|
$k = preg_replace_callback('/(^|[^0-9])([0-9]+)\»\;/ui', function ($m) { |
104
|
|
|
global $__ax, $__ay; |
105
|
|
|
|
106
|
|
|
$__ay++; |
107
|
|
|
|
108
|
|
|
if ($__ay == $__ax) { |
109
|
|
|
return $m[1] . $m[2] . '″'; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $m[0]; |
113
|
|
|
}, $k); |
114
|
|
|
$amount = 1; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
} while (($amount == 0) && count($okposstack)); |
118
|
|
|
|
119
|
|
|
// успешно сделали замену |
120
|
|
|
if ($amount == 1) { |
121
|
|
|
// заново просмотрим содержимое |
122
|
|
|
$this->_text = substr($this->_text, 0, $lokpos) . $k . substr($this->_text, $off); |
123
|
|
|
$off = $lokpos; |
124
|
|
|
$level = 0; |
125
|
|
|
continue; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// иначе просто заменим последнюю явно на " от отчаяния |
129
|
|
|
if ($amount == 0) { |
130
|
|
|
// говорим, что всё в порядке |
131
|
|
|
$level = 0; |
132
|
|
|
$this->_text = substr($this->_text, 0, $p['pos']) . '"' . substr($this->_text, $off); |
133
|
|
|
$off = $p['pos'] + strlen('"'); |
134
|
|
|
$okposstack = array($off); |
135
|
|
|
continue; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
// не совпало количество, отменяем все подкавычки |
142
|
|
|
if ($level != 0) { |
143
|
|
|
|
144
|
|
|
// закрывающих меньше, чем надо |
145
|
|
|
if ($level > 0) { |
146
|
|
|
$k = substr($this->_text, $okpos); |
147
|
|
|
$k = str_replace(self::QUOTE_CRAWSE_OPEN, self::QUOTE_FIRS_OPEN, $k); |
148
|
|
|
$k = str_replace(self::QUOTE_CRAWSE_CLOSE, self::QUOTE_FIRS_CLOSE, $k); |
149
|
|
|
$this->_text = substr($this->_text, 0, $okpos) . $k; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.