-
Notifications
You must be signed in to change notification settings - Fork 2
/
class.html-to-wiki.php
238 lines (205 loc) · 7.52 KB
/
class.html-to-wiki.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
<?php
/**
* Created by PhpStorm.
* User: Sicz-Mesziár János
* Date: 2015.11.10.
* Time: 21:43
*/
class HtmlToWiki {
//@formatter:off
private static $inline = array(
'<i>' => "''", // italic
'</i>' => "''",
'<b>' => " '''", // bold
'</b>' => "''' ",
'<strong>' => " '''",
'</strong>' => "''' ",
'<span>' => "", // span
'</span>' => "",
' ' => " "
);
private static $block = array(
'<div>' => "", // div
'</div>' => "\n",
'<p>' => "\n", // p
'</p>' => "\n",
'<ul>' => "\n", // ul
'</ul>' => "\n",
'<ol>' => "\n", // ol
'</ol>' => "\n",
'<li>' => "* ", // li
'</li>' => "\n"
);
private static $heading = array(
'<h1>' => "\n= ",
'</h1>' => " =\n",
'<h2>' => "\n== ",
'</h2>' => " ==\n",
'<h3>' => "\n=== ",
'</h3>' => " ===\n",
'<h4>' => "\n==== ",
'</h4>' => " ====\n",
'<h5>' => "\n===== ",
'</h5>' => " =====\n",
'<h6>' => "\n====== ",
'</h6>' => " ======\n",
);
private static $separator = array(
'<br>' => "\n",
'<br/>' => "\n",
'<hr>' => "---\n",
'<hr/>' => "---\n"
);
private static $bullets_and_hyphens = array(
// BULLETS
"•", // \x{2022} --> BULLET
"∙", // \x{2219} --> BULLET OPERATOR
"◦", // \x{25E6} --> WHITE BULLET
"◘", // \x{25D8} --> INVERSE BULLET
"⦿", // \x{29BF} --> CIRCLED BULLET
"‣", // \x{2023} --> TRIANGULAR BULLET
"⁃", // \x{2043} --> HYPHEN BULLET
"·", // \x{00B7} --> MIDDLE DOT
// HYPHENS
"-", // \x{002D} --> HYPHEN-MINUS
"⁻", // \x{207B} --> SUPERSCRIPT MINUS
"₋", // \x{208B} --> SUBSCRIPT MINUS
"﹣", // \x{FE63} --> SMALL HYPHEN-MINUS
"-" // \x{FF0D} --> FULLWIDTH HYPHEN-MINUS
);
//@formatter:on
/**
* @param $html String
* @return String
*/
public function toWiki($html) {
// TRIM --------------------------------
$result = trim($html);
// CONVERSION --------------------------
// 1. Get the payload
$result = $this->parseHTMLBody($result);
// 2. Make single line to prevent new-lines problems
$result = $this->removeNewLines($result);
// 3. Parse elements (that depend attributes like <a href...>)
$result = $this->replaceHTMLLinks($result);
// 4. Clean unnecessary HTML attributes
$result = $this->removeHTMLArguments($result);
// 5. Parse elements
$result = $this->replaceHTMLInlineElements($result);
$result = $this->replaceHTMLBlockElements($result);
$result = $this->replaceHTMLHeadingElements($result);
$result = $this->replaceHTMLSeparatorElements($result);
// 6. Clean Wiki
$result = $this->trimLines($result);
$result = $this->removeWhitespaces($result);
$result = $this->removeBlankLines($result);
$result = $this->removeHTMLElements($result);
// 7. Fix Wiki content
$result = $this->fixWiki($result);
return trim($result);
}
// HTML CLEANING & CONVERTER FUNCTIONS ----------------------------------------------
public function parseHTMLBody($html) {
preg_match('/<body[^>]*>(.*?)<\/body>/s', $html, $matches);
return !empty($matches) ? trim($matches[1]) : trim($html);
}
public function replaceHTMLLinks($html) {
// Based on: http://www.the-art-of-web.com/php/parse-links/
preg_match_all('/<a\s[^>]*href\s*=\s*(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>/siU', $html, $matches, PREG_PATTERN_ORDER);
if (!empty($matches)) {
for ($i = 0; $i < count($matches[0]); $i++) {
$match = trim($matches[0][$i]);
$link = trim($matches[2][$i]);
$text = $this->removeHTMLElements(trim($matches[3][$i]));
if (!empty($match) && !empty($link) && !empty($text)) {
$wiki_link = '[' . $link . ' ' . $text . ']';
$html = str_replace($match, $wiki_link, $html);
}
}
}
return $html;
}
public function removeHTMLArguments($html) {
return preg_replace('/<([a-z][a-z0-9]*)[^>]*?(\/?)>/i', '<$1$2>', $html);
}
public function removeHTMLElements($html) {
return trim(strip_tags($html));
}
public function replaceHTMLInlineElements($html) {
foreach (self::$inline as $k => $v)
$html = str_replace($k, $v, $html);
return $html;
}
public function replaceHTMLBlockElements($html) {
foreach (self::$block as $k => $v)
$html = str_replace($k, $v, $html);
return $html;
}
public function replaceHTMLHeadingElements($html) {
foreach (self::$heading as $k => $v)
$html = str_replace($k, $v, $html);
return $html;
}
public function replaceHTMLSeparatorElements($html) {
foreach (self::$separator as $k => $v)
$html = str_replace($k, $v, $html);
return $html;
}
// SIMPLE TEXT CLEANING FUNCTIONS ----------------------------------------------
public function removeNewLines($text) {
return trim(preg_replace('/\s+/', ' ', $text));
}
public function removeWhitespaces($text) {
for ($i = 0; $i < 5; $i++)
$text = str_replace(' ', ' ', $text);
return $text;
}
public function removeBlankLines($text) {
return preg_replace('/\n(\s*\n)+/', "\n\n", $text);
}
public function trimLines($text) {
return implode("\n", array_map('trim', explode("\n", $text)));
}
// CONTENT FIXING -------------------------------------------------------------
public function fixWiki($wiki) {
$rows = explode("\n", $wiki);
for($i=0; $i<count($rows); $i++){
if(!empty($rows[$i])){
$rows[$i] = $this -> fixHeadings($rows[$i]);
$rows[$i] = $this -> fixBulletsAndHyphens($rows[$i]);
$rows[$i] = $this -> fixRow($rows[$i]);
}
}
$wiki = implode("\n", $rows);
$wiki = $this -> fixListsEmptyRows($wiki);
return $wiki;
}
public function fixHeadings($row){
if($this->startsWith($row, "="))
return preg_replace("/^(=+\s+)'''(.*)'''\s+(=+)$/u", '$1 $2 $3', $row);
return $row;
}
public function fixBulletsAndHyphens($row){
$end = min(mb_strpos($row, ' '), mb_strlen($row));
$part = mb_substr($row, 0, $end);
foreach(self::$bullets_and_hyphens as $bullet)
$part = str_ireplace($bullet, "*", $part);
$only_first_match = 1;
$row = substr_replace($row, $part, 0, $end);
$row = str_replace("'''* '''", "* ", $row, $only_first_match);
return $row;
}
public function fixRow($row){
if(trim($row, "'= ") == '') return '';
return $row;
}
public function fixListsEmptyRows($wiki){
return preg_replace("/\n\n\*/i", "\n*", $wiki);
}
// Utils
private function startsWith($haystack, $needle)
{
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
}