-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathEMT.Tret.Text.php
142 lines (126 loc) · 7.03 KB
/
EMT.Tret.Text.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
<?php
/**
* @see EMT_Tret
*/
require_once('EMT.Tret.php');
class EMT_Tret_Text extends EMT_Tret
{
public $classes = array(
'nowrap' => 'word-spacing:nowrap;',
);
/**
* Базовые параметры тофа
*
* @var array
*/
public $title = "Текст и абзацы";
public $rules = array(
'auto_links' => array(
'description' => 'Выделение ссылок из текста',
'pattern' => '/(\s|^)(http|ftp|mailto|https)(:\/\/)([^\s\,\!\<]{4,})(\s|\.|\,|\!|\?|\<|$)/ieu',
'replacement' => '$m[1] . $this->tag((substr($m[4],-1)=="."?substr($m[4],0,-1):$m[4]), "a", array("href" => $m[2].$m[3].(substr($m[4],-1)=="."?substr($m[4],0,-1):$m[4]))) . (substr($m[4],-1)=="."?".":"") .$m[5]'
),
'email' => array(
'description' => 'Выделение эл. почты из текста',
'pattern' => '/(\s|^|\ \;|\()([a-z0-9\-\_\.]{2,})\@([a-z0-9\-\.]{2,})\.([a-z]{2,6})(\)|\s|\.|\,|\!|\?|$|\<)/ie',
'replacement' => '$m[1] . $this->tag($m[2]."@".$m[3].".".$m[4], "a", array("href" => "mailto:".$m[2]."@".$m[3].".".$m[4])) . $m[5]'
),
'no_repeat_words' => array(
'description' => 'Удаление повторяющихся слов',
'disabled' => true,
'pattern' => array(
'/([а-яё]{3,})( |\t|\ \;)\1/iu',
'/(\s|\ \;|^|\.|\!|\?)(([А-ЯЁ])([а-яё]{2,}))( |\t|\ \;)(([а-яё])\4)/eu',
),
'replacement' => array(
'\1',
'$m[1].($m[7] === EMT_Lib::strtolower($m[3]) ? $m[2] : $m[2].$m[5].$m[6] )',
)
),
'paragraphs' => array(
'description' => 'Простановка параграфов',
'function' => 'build_paragraphs'
),
'breakline' => array(
'description' => 'Простановка переносов строк',
'function' => 'build_brs'
),
);
/**
* Расстановка защищенных тегов параграфа (<p>...</p>) и переноса строки
*
* @return void
*/
protected function do_paragraphs($text) {
$text = str_replace("\r\n","\n",$text);
$text = str_replace("\r","\n",$text);
$text = '<' . self::BASE64_PARAGRAPH_TAG . '>' . trim($text) . '</' . self::BASE64_PARAGRAPH_TAG . '>';
//$text = $this->preg_replace_e('/([\040\t]+)?(\n|\r){2,}/e', '"</" . self::BASE64_PARAGRAPH_TAG . "><" .self::BASE64_PARAGRAPH_TAG . ">"', $text);
//$text = $this->preg_replace_e('/([\040\t]+)?(\n){2,}/e', '"</" . self::BASE64_PARAGRAPH_TAG . "><" .self::BASE64_PARAGRAPH_TAG . ">"', $text);
$text = $this->preg_replace_e('/([\040\t]+)?(\n)+([\040\t]*)(\n)+/e', '$m[1]."</" . self::BASE64_PARAGRAPH_TAG . ">".EMT_Lib::iblock($m[2].$m[3])."<" .self::BASE64_PARAGRAPH_TAG . ">"', $text);
//$text = $this->preg_replace_e('/([\040\t]+)?(\n)+([\040\t]*)(\n)+/e', '"</" . self::BASE64_PARAGRAPH_TAG . ">"."<" .self::BASE64_PARAGRAPH_TAG . ">"', $text);
//может от открвающего до закрывающего ?!
$text = preg_replace('/\<' . self::BASE64_PARAGRAPH_TAG . '\>('.EMT_Lib::INTERNAL_BLOCK_OPEN.'[a-zA-Z0-9\/=]+?'.EMT_Lib::INTERNAL_BLOCK_CLOSE.')?\<\/' . self::BASE64_PARAGRAPH_TAG . '\>/s', "", $text);
return $text;
}
/**
* Расстановка защищенных тегов параграфа (<p>...</p>) и переноса строки
*
* @return void
*/
protected function build_paragraphs()
{
$r = mb_strpos($this->_text, '<' . self::BASE64_PARAGRAPH_TAG . '>' );
$p = EMT_Lib::rstrpos($this->_text, '</' . self::BASE64_PARAGRAPH_TAG . '>' ) ;
if(($r!== false) && ($p !== false)) {
$beg = mb_substr($this->_text,0,$r);
$end = mb_substr($this->_text,$p+mb_strlen('</' . self::BASE64_PARAGRAPH_TAG . '>'));
$this->_text =
(trim($beg) ? $this->do_paragraphs($beg). "\n":"") .'<' . self::BASE64_PARAGRAPH_TAG . '>'.
mb_substr($this->_text,$r + mb_strlen('<' . self::BASE64_PARAGRAPH_TAG . '>'),$p -($r + mb_strlen('<' . self::BASE64_PARAGRAPH_TAG . '>')) ).'</' . self::BASE64_PARAGRAPH_TAG . '>'.
(trim($end) ? "\n".$this->do_paragraphs($end) :"") ;
} else {
$this->_text = $this->do_paragraphs($this->_text);
}
}
/**
* Расстановка защищенных тегов параграфа (<p>...</p>) и переноса строки
*
* @return void
*/
protected function build_brs()
{
$this->_text = $this->preg_replace_e('/(\<\/' . self::BASE64_PARAGRAPH_TAG . '\>)([\r\n \t]+)(\<' . self::BASE64_PARAGRAPH_TAG . '\>)/mse', '$m[1].EMT_Lib::iblock($m[2]).$m[3]', $this->_text);
if (!preg_match('/\<' . self::BASE64_BREAKLINE_TAG . '\>/', $this->_text)) {
$this->_text = str_replace("\r\n","\n",$this->_text);
$this->_text = str_replace("\r","\n",$this->_text);
//$this->_text = $this->preg_replace_e('/(\n|\r)/e', '"<" . self::BASE64_BREAKLINE_TAG . ">"', $this->_text);
$this->_text = $this->preg_replace_e('/(\n)/e', '"<" . self::BASE64_BREAKLINE_TAG . ">\n"', $this->_text);
}
}
}
/**PYTHON
def do_paragraphs(self, text):
text = EMT_Lib.str_replace(u"\r\n",u"\n",text)
text = EMT_Lib.str_replace(u"\r",u"\n",text)
text = u'<' + BASE64_PARAGRAPH_TAG + u'>' + text.strip() + u'</' + BASE64_PARAGRAPH_TAG + u'>'
text = self.preg_replace('/([\040\t]+)?(\n)+([\040\t]*)(\n)+/e', '(u"" if m.group(1) is None else m.group(1))+u"</" + BASE64_PARAGRAPH_TAG + u">"+EMT_Lib.iblock(m.group(2)+m.group(3))+u"<" +BASE64_PARAGRAPH_TAG + u">"', text)
text = self.preg_replace('/\<' + BASE64_PARAGRAPH_TAG + '\>(' + INTERNAL_BLOCK_OPEN + '[a-zA-Z0-9\/=]+?' + INTERNAL_BLOCK_CLOSE + ')?\<\/' + BASE64_PARAGRAPH_TAG + '\>/s', "", text)
return text
def build_paragraphs(self):
r = self._text.find( u'<' + BASE64_PARAGRAPH_TAG + u'>' )
p = self._text.rfind( u'</'+ BASE64_PARAGRAPH_TAG + u'>' )
if(( r != -1) and (p != -1)):
beg = EMT_Lib.substr(self._text,0,r);
end = EMT_Lib.substr(self._text,p+len(u'</' + BASE64_PARAGRAPH_TAG + u'>'))
self._text = (self.do_paragraphs(beg)+ u"\n" if beg.strip() else u"") +u'<' + BASE64_PARAGRAPH_TAG + u'>'+EMT_Lib.substr(self._text,r + len(u'<' + BASE64_PARAGRAPH_TAG + u'>'),p -(r + len(u'<' + BASE64_PARAGRAPH_TAG + u'>')) )+u'</' + BASE64_PARAGRAPH_TAG + u'>'+( u"\n"+self.do_paragraphs(end) if end.strip() else u"")
else:
self._text = self.do_paragraphs(self._text)
def build_brs(self):
self._text = self.preg_replace('/(\<\/' + BASE64_PARAGRAPH_TAG + '\>)([\r\n \t]+)(\<' + BASE64_PARAGRAPH_TAG + '\>)/mse', 'm.group(1)+EMT_Lib.iblock(m.group(2))+m.group(3)', self._text);
if (not re.match('\<' + BASE64_BREAKLINE_TAG + '\>', self._text)):
self._text = EMT_Lib.str_replace("\r\n","\n",self._text)
self._text = EMT_Lib.str_replace("\r","\n",self._text)
self._text = self.preg_replace('/(\n)/e', '"<" + BASE64_BREAKLINE_TAG + ">\\n"', self._text)
PYTHON**/
?>