-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHTML.php
284 lines (271 loc) · 7.26 KB
/
HTML.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
<?php
/**
@file
@brief Tools for working with HTML, mostly for output.
*/
namespace Edoceo\Radix;
class HTML
{
/**
Turns HTML into Formatted Text
@param string $html
@return text/plain of HTML that is stripped of tags and formatted pretty(ish)
*/
static function asText($html)
{
$er = error_reporting(0);
$dom = new \DOMDocument();
$dom->loadHTML($html);
error_reporting($er);
$text = self::_html2text_node($dom);
return $text;
}
/**
@param $html String of HTML, hopefully well formed
*/
static function stripComments($html)
{
return preg_replace('/<!\-\-(.*?)\-\->/ms', null, $html);
}
/**
Recursive Utility function for html2text
@param object $node
@return string text/plain
*/
private static function _html2text_node($pn)
{
$node_post = null;
$node_text = null;
$nl = $pn->childNodes->length;
for ($i=0; $i<$nl; $i++) {
$cn = $pn->childNodes->item($i);
//if ($cn == null) continue;
// Process Nodes
// echo sprintf('%s #%d/%d',$cn->nodeName,$this->_node_step,count($this->_node_post)) . "\n";
// Analyse Specific Node Name
switch ($cn->nodeName) {
case 'a': // used to create a hyperlink
$node_post.= ' <' . $cn->getAttribute('href') . '> ';
break;
// case 'abbr':
case 'acronym':
$x = $cn->getAttribute('title');
if (!empty($x)) {
$node_post = sprintf(' (%s)',$x);
}
break;
// case 'applet':
// case 'area':
// case 'b': // deprecated
// case 'base':
// case 'basefont': // deprecated
// case 'bdo':
// case 'blackface': // deprecated
case 'blockquote': // used to identify larger amounts of quoted text
$node_post = "\n";
break;
// case 'big': // deprecated
// case 'body':
case 'br':
$node_post = "\n";
break;
// case 'button': // used to create button controls for forms
// case 'caption':
// case 'center': // deprecated
// case 'cite':
// case 'code':
// case 'col': // @todo Check Align, Width
// case 'colgroup': // @todo Check Align, Width
case 'dd':
$node_text.= " ";
$node_post = "\n";
break;
// case 'del':
// case 'dfn': // dfn - contains the defining instance of the enclosed term.
// case 'dir': // deprecated
// case 'div': // div - offers a generic way of grouping areas of content.
case 'dl': // used to create a list where each item in the list comprises two parts: a term and a description.
// $node_text.= "\n";
$node_post = "\n";
$node_pads = ' ';
break;
case 'dt': // dt - is a definition term for an item in a definition list
$node_text.= " ";
$node_post = "\n";
break;
// case 'em': // em - is used to indicate emphasis.
// case 'embed': // deprecated
// case 'fieldset': // adds structure to forms by grouping together related controls and labels.
// case 'font': // deprecated
// case 'form': // is used to create data entry forms.
// @todo Check Action && Method
case 'h1':
$node_text.= "\n# ";
$node_post = "#\n";
break;
case 'h2':
$node_text.= "\n## ";
$node_post = "##\n";
break;
case 'h3':
$node_text.= "\n### ";
$node_post = "###\n";
break;
case 'h4':
$node_text.= "\n#### ";
$node_post = "####\n";
break;
case 'h5':
$node_text.= "\n##### ";
$node_post = "#####\n";
break;
case 'h6':
$node_text.= "\n###### ";
$node_post = "######\n";
break;
case 'hr': // used to separate sections of content
$node_text.= sprintf("\n%s\n", str_repeat('-',72) );
break;
// case 'img':
// case 'meta':
// case 'input': // a multi-purpose form control
// case 'ins': // used to mark up content that has been inserted into the current version of a document
// case 'kbd': // indicates input to be entered by the user.
// case 'label': // associates a label with form controls such as input, textarea, select and object.
// case 'legend': // caption to a fieldset element.
case 'li': // represents a list item in ordered lists and unordered lists
$node_text.= ' -';
$node_post = "\n";
break;
// case 'link': // conveys relationship information that can be used by Web browsers and search engines
// case 'map': specifies a client-side image map that may be referenced by elements such as img, select and object.
// case 'meta':
// case 'noscript': // allows authors to provide alternate content when a script is not executed.
// case 'object': // provides a generic way of embedding objects such as images, movies and applications (Java applets, browser plug-ins, etc.) into Web pages.
case 'ol': // used to create ordered lists
$node_text.= "\n";
$node_post = "\n";
break;
// case 'optgroup': // used to group the choices offered in select form controls
// case 'option': // represents a choice offered by select form controls.
case 'p':
$node_text.= "\n";
$node_post = "\n";
break;
// case 'param':
case 'pre':
$node_text.= "\n";
$node_post = "\n";
break;
// case 'q':
// case 'rb':
// case 'rbc':
// case 'rp':
// case 'rt':
// case 'rtc':
// case 'ruby':
// case 's': // deprecated
// case 'samp':
// case 'script':
// case 'select':
// case 'shadow':
// case 'small':
// case 'span':
// case 'strike':
// case 'strong':
// case 'style':
// case 'sub':
// case 'sup':
case 'table':
$node_text.= sprintf("\n%s\n", str_repeat('-',72) );
$node_post = sprintf("\n%s\n", str_repeat('-',72) );
break;
// case 'tbody':
// case 'td':
// case 'textarea':
// case 'tfoot':
// case 'th':
// case 'thead':
case 'title':
// Remove Title
$cn->removeChild( $cn->firstChild );
break;
case 'tr':
$node_text.= "\n";
$node_post = "\n";
break;
// case 'tt':
case 'u': // deprecated
$node_text.= '_';
$node_post = '_ ';
break;
case 'ul':
// $node_text.= "\n";
$node_post = "\n";
break;
// case 'var':
// case 'isindex':
// case 'layer':
// case 'menu':
// case 'noembed':
// Default Handler!
default:
if ($cn->nodeType == XML_TEXT_NODE) {
$x = trim(html_entity_decode($cn->nodeValue));
if (!empty($x)) {
$node_text.= $x;
}
$node_text.= ' ';
}
}
// Sub-Routine to Check Children Elements
if ($cn->nodeType == XML_ELEMENT_NODE) {
$node_text.= self::_html2text_node($cn);
if (strlen($node_post)) {
$node_text.= $node_post;
$node_post = null;
}
}
}
// Close an Open Node
//if (count($this->_node_post)) {
// $this->_text.= array_pop($this->_node_post);
//}
//if (count($this->_node_open)) {
// $this->_page_dump.= sprintf('</%s>',array_pop($this->_node_open));
//}
// Back out and up!
//return ($dump . "\n");
return $node_text;
}
/**
*/
private static function _attr_html($attr)
{
ksort($attr);
$buf = array();
foreach ($attr as $k=>$v) {
$buf[] = sprintf('%s="%s"',$k,htmlspecialchars($v,ENT_QUOTES));
}
return implode(' ',$buf);
}
/**
*/
private static function _attr_init($name,$attr,$args=null)
{
if (!is_array($attr)) {
$attr = array();
}
if (!is_array($args)) {
$args = array();
}
$ret = array_merge($attr,$args);
if (empty($ret['id'])) {
$ret['id'] = $name;
}
if (empty($ret['name'])) {
$ret['name'] = $name;
}
return $ret;
}
}