-
Notifications
You must be signed in to change notification settings - Fork 3
/
e_parse.php
203 lines (169 loc) · 3.95 KB
/
e_parse.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
<?php
/**
* @file
* Addon file for extending e_parser.
*/
if(!defined('e107_INIT'))
{
exit;
}
// This file is generated by Composer.
e107_require_once(e_PLUGIN . 'simplemde/vendor/autoload.php');
use League\HTMLToMarkdown\HtmlConverter;
/**
* Class simplemde_parse.
*/
class simplemde_parse
{
/**
* Plugin preferences.
*
* @var array
*/
private $plugPrefs = array();
/**
* Core preferences.
*
* @var array
*/
private $corePrefs = array();
/**
* Constructor.
*/
function __construct()
{
$this->plugPrefs = e107::getPlugConfig('simplemde')->getPref();
$this->corePrefs = e107::getPref();
}
/**
* @param string $text
* HTML/text to be processed.
* @param string $context
* Current context ie:
* OLDDEFAULT | BODY | TITLE | SUMMARY | DESCRIPTION | WYSIWYG
*
* @return string
*/
function toHtml($text, $context = '')
{
return $text;
}
/**
* @param string $text
* HTML/text to be processed.
* @param array $param
*
* @return string
*/
function toDB($text, $param = array())
{
$type = varset($param['type'], '');
$field = varset($param['field'], '');
if($type == 'bbarea')
{
return '[markdown]' . $text . '[/markdown]';
}
$fields = array(
'news_body',
'news_extended',
);
if(in_array($field, $fields) && $type == 'method')
{
return '[markdown]' . $text . '[/markdown]';
}
return $text;
}
/**
* @param string $text
* HTML/text to be processed.
* @param array $param
*
* @return string
*/
function toWYSIWYG($text, $param = array())
{
// If SimpleMDE is not in use, we returns with the original text.
if(!$this->simpleMDEisInUse())
{
return $text;
}
// Remove wrapping BBCode tag (if available).
$text = str_replace(array('[markdown]', '[/markdown]'), '', $text);
$isHtml = false;
// If text contains [html], need to parse it to get HTML contents.
if(substr($text, 0, 6) == '[html]')
{
$tp = e107::getParser();
$text = $tp->toHTML($text, true);
$isHtml = true;
}
// Convert special HTML entities back to characters.
$text = htmlspecialchars_decode($text);
// Remove HTML comments.
$text = preg_replace('/<!--(.*)-->/Uis', '', $text);
// If text contains HTML tags, we need to convert it to Markdown format.
if($isHtml && $this->isHTML($text))
{
$converter = new HtmlConverter();
// Strip HTML tags that don't have a Markdown equivalent.
$converter->getConfig()->setOption('strip_tags', true);
// Convert <br> to \n
$converter->getConfig()->setOption('hard_break', true);
if((bool) varset($this->plugPrefs['allowAtxHeaderWithoutSpace'], 0) === true)
{
// Set to 'atx' to output H1 and H2 headers as # Header1 and ## Header2.
$converter->getConfig()->setOption('header_style', 'atx');
}
$text = $converter->convert($text);
}
return $text;
}
/**
* Checking whether $string is HTML or not.
*
* @param string $string
* String to be checked.
*
* @return bool
* True if the string contains HTML, otherwise false.
*/
function isHTML($string)
{
if($string != strip_tags($string))
{
return true;
}
return preg_match("/<[^<]+>/", $string, $m) != 0;
}
/**
* Checking whether SimpleMDE Editor is in use or not.
*
* @return bool
* If true, need to convert HTML to Markdown format. Otherwise false.
*/
function simpleMDEisInUse()
{
$enableOn = (int) varset($this->plugPrefs['enableEditor'], 1);
$enable = false;
// "Enable Markdown Editor in Admin Area and on Frontend too"
if(!$enable && $enableOn === 1)
{
$enable = true;
}
// "Enable Markdown Editor only in Admin Area"
if(!$enable && $enableOn === 2 && deftrue('e_ADMIN_AREA', false))
{
$enable = true;
}
// "Enable Markdown Editor only on Frontend"
if(!$enable && $enableOn === 3 && !deftrue('e_ADMIN_AREA', false))
{
$enable = true;
}
if($enable === true && e107::wysiwyg() === true && check_class($this->corePrefs['post_html']))
{
return true;
}
return false;
}
}