Parent: [c7a241] (diff)

Child: [ec8737] (diff)

Download this file

myhtmlparse.cpp    552 lines (534 with data), 18.1 kB

  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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/* This file was copied from omega-0.8.5->1.2.6 and modified */
/* myhtmlparse.cc: subclass of HtmlParser for extracting text
*
* ----START-LICENCE----
* Copyright 1999,2000,2001 BrightStation PLC
* Copyright 2002,2003,2004 Olly Betts
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
* -----END-LICENCE-----
*/
#include <time.h>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include "cstr.h"
#include "myhtmlparse.h"
#include "indextext.h" // for lowercase_term()
#include "mimeparse.h"
#include "smallut.h"
#include "cancelcheck.h"
#include "debuglog.h"
#include "transcode.h"
inline static bool
p_notdigit(char c)
{
return !isdigit(static_cast<unsigned char>(c));
}
inline static bool
p_notxdigit(char c)
{
return !isxdigit(static_cast<unsigned char>(c));
}
inline static bool
p_notalnum(char c)
{
return !isalnum(static_cast<unsigned char>(c));
}
/*
* The following array was taken from Estraier. Estraier was
* written by Mikio Hirabayashi.
* Copyright (C) 2003-2004 Mikio Hirabayashi
* The version where this comes from
* is covered by the GNU licence, as this file.*/
static const char *epairs[] = {
/* basic symbols */
"amp", "&", "lt", "<", "gt", ">", "quot", "\"", "apos", "'",
/* ISO-8859-1 */
"nbsp", "\xc2\xa0", "iexcl", "\xc2\xa1", "cent", "\xc2\xa2",
"pound", "\xc2\xa3", "curren", "\xc2\xa4", "yen", "\xc2\xa5",
"brvbar", "\xc2\xa6", "sect", "\xc2\xa7", "uml", "\xc2\xa8",
"copy", "\xc2\xa9", "ordf", "\xc2\xaa", "laquo", "\xc2\xab",
"not", "\xc2\xac", "shy", "\xc2\xad", "reg", "\xc2\xae",
"macr", "\xc2\xaf", "deg", "\xc2\xb0", "plusmn", "\xc2\xb1",
"sup2", "\xc2\xb2", "sup3", "\xc2\xb3", "acute", "\xc2\xb4",
"micro", "\xc2\xb5", "para", "\xc2\xb6", "middot", "\xc2\xb7",
"cedil", "\xc2\xb8", "sup1", "\xc2\xb9", "ordm", "\xc2\xba",
"raquo", "\xc2\xbb", "frac14", "\xc2\xbc", "frac12", "\xc2\xbd",
"frac34", "\xc2\xbe", "iquest", "\xc2\xbf", "Agrave", "\xc3\x80",
"Aacute", "\xc3\x81", "Acirc", "\xc3\x82", "Atilde", "\xc3\x83",
"Auml", "\xc3\x84", "Aring", "\xc3\x85", "AElig", "\xc3\x86",
"Ccedil", "\xc3\x87", "Egrave", "\xc3\x88", "Eacute", "\xc3\x89",
"Ecirc", "\xc3\x8a", "Euml", "\xc3\x8b", "Igrave", "\xc3\x8c",
"Iacute", "\xc3\x8d", "Icirc", "\xc3\x8e", "Iuml", "\xc3\x8f",
"ETH", "\xc3\x90", "Ntilde", "\xc3\x91", "Ograve", "\xc3\x92",
"Oacute", "\xc3\x93", "Ocirc", "\xc3\x94", "Otilde", "\xc3\x95",
"Ouml", "\xc3\x96", "times", "\xc3\x97", "Oslash", "\xc3\x98",
"Ugrave", "\xc3\x99", "Uacute", "\xc3\x9a", "Ucirc", "\xc3\x9b",
"Uuml", "\xc3\x9c", "Yacute", "\xc3\x9d", "THORN", "\xc3\x9e",
"szlig", "\xc3\x9f", "agrave", "\xc3\xa0", "aacute", "\xc3\xa1",
"acirc", "\xc3\xa2", "atilde", "\xc3\xa3", "auml", "\xc3\xa4",
"aring", "\xc3\xa5", "aelig", "\xc3\xa6", "ccedil", "\xc3\xa7",
"egrave", "\xc3\xa8", "eacute", "\xc3\xa9", "ecirc", "\xc3\xaa",
"euml", "\xc3\xab", "igrave", "\xc3\xac", "iacute", "\xc3\xad",
"icirc", "\xc3\xae", "iuml", "\xc3\xaf", "eth", "\xc3\xb0",
"ntilde", "\xc3\xb1", "ograve", "\xc3\xb2", "oacute", "\xc3\xb3",
"ocirc", "\xc3\xb4", "otilde", "\xc3\xb5", "ouml", "\xc3\xb6",
"divide", "\xc3\xb7", "oslash", "\xc3\xb8", "ugrave", "\xc3\xb9",
"uacute", "\xc3\xba", "ucirc", "\xc3\xbb", "uuml", "\xc3\xbc",
"yacute", "\xc3\xbd", "thorn", "\xc3\xbe", "yuml", "\xc3\xbf",
/* ISO-10646 */
"fnof", "\xc6\x92", "Alpha", "\xce\x91", "Beta", "\xce\x92",
"Gamma", "\xce\x93", "Delta", "\xce\x94", "Epsilon", "\xce\x95",
"Zeta", "\xce\x96", "Eta", "\xce\x97", "Theta", "\xce\x98",
"Iota", "\xce\x99", "Kappa", "\xce\x9a", "Lambda", "\xce\x9b",
"Mu", "\xce\x9c", "Nu", "\xce\x9d", "Xi", "\xce\x9e",
"Omicron", "\xce\x9f", "Pi", "\xce\xa0", "Rho", "\xce\xa1",
"Sigma", "\xce\xa3", "Tau", "\xce\xa4", "Upsilon", "\xce\xa5",
"Phi", "\xce\xa6", "Chi", "\xce\xa7", "Psi", "\xce\xa8",
"Omega", "\xce\xa9", "alpha", "\xce\xb1", "beta", "\xce\xb2",
"gamma", "\xce\xb3", "delta", "\xce\xb4", "epsilon", "\xce\xb5",
"zeta", "\xce\xb6", "eta", "\xce\xb7", "theta", "\xce\xb8",
"iota", "\xce\xb9", "kappa", "\xce\xba", "lambda", "\xce\xbb",
"mu", "\xce\xbc", "nu", "\xce\xbd", "xi", "\xce\xbe",
"omicron", "\xce\xbf", "pi", "\xcf\x80", "rho", "\xcf\x81",
"sigmaf", "\xcf\x82", "sigma", "\xcf\x83", "tau", "\xcf\x84",
"upsilon", "\xcf\x85", "phi", "\xcf\x86", "chi", "\xcf\x87",
"psi", "\xcf\x88", "omega", "\xcf\x89", "thetasym", "\xcf\x91",
"upsih", "\xcf\x92", "piv", "\xcf\x96", "bull", "\xe2\x80\xa2",
"hellip", "\xe2\x80\xa6", "prime", "\xe2\x80\xb2", "Prime", "\xe2\x80\xb3",
"oline", "\xe2\x80\xbe", "frasl", "\xe2\x81\x84", "weierp", "\xe2\x84\x98",
"image", "\xe2\x84\x91", "real", "\xe2\x84\x9c", "trade", "\xe2\x84\xa2",
"alefsym", "\xe2\x84\xb5", "larr", "\xe2\x86\x90", "uarr", "\xe2\x86\x91",
"rarr", "\xe2\x86\x92", "darr", "\xe2\x86\x93", "harr", "\xe2\x86\x94",
"crarr", "\xe2\x86\xb5", "lArr", "\xe2\x87\x90", "uArr", "\xe2\x87\x91",
"rArr", "\xe2\x87\x92", "dArr", "\xe2\x87\x93", "hArr", "\xe2\x87\x94",
"forall", "\xe2\x88\x80", "part", "\xe2\x88\x82", "exist", "\xe2\x88\x83",
"empty", "\xe2\x88\x85", "nabla", "\xe2\x88\x87", "isin", "\xe2\x88\x88",
"notin", "\xe2\x88\x89", "ni", "\xe2\x88\x8b", "prod", "\xe2\x88\x8f",
"sum", "\xe2\x88\x91", "minus", "\xe2\x88\x92", "lowast", "\xe2\x88\x97",
"radic", "\xe2\x88\x9a", "prop", "\xe2\x88\x9d", "infin", "\xe2\x88\x9e",
"ang", "\xe2\x88\xa0", "and", "\xe2\x88\xa7", "or", "\xe2\x88\xa8",
"cap", "\xe2\x88\xa9", "cup", "\xe2\x88\xaa", "int", "\xe2\x88\xab",
"there4", "\xe2\x88\xb4", "sim", "\xe2\x88\xbc", "cong", "\xe2\x89\x85",
"asymp", "\xe2\x89\x88", "ne", "\xe2\x89\xa0", "equiv", "\xe2\x89\xa1",
"le", "\xe2\x89\xa4", "ge", "\xe2\x89\xa5", "sub", "\xe2\x8a\x82",
"sup", "\xe2\x8a\x83", "nsub", "\xe2\x8a\x84", "sube", "\xe2\x8a\x86",
"supe", "\xe2\x8a\x87", "oplus", "\xe2\x8a\x95", "otimes", "\xe2\x8a\x97",
"perp", "\xe2\x8a\xa5", "sdot", "\xe2\x8b\x85", "lceil", "\xe2\x8c\x88",
"rceil", "\xe2\x8c\x89", "lfloor", "\xe2\x8c\x8a", "rfloor", "\xe2\x8c\x8b",
"lang", "\xe2\x8c\xa9", "rang", "\xe2\x8c\xaa", "loz", "\xe2\x97\x8a",
"spades", "\xe2\x99\xa0", "clubs", "\xe2\x99\xa3", "hearts", "\xe2\x99\xa5",
"diams", "\xe2\x99\xa6", "OElig", "\xc5\x92", "oelig", "\xc5\x93",
"Scaron", "\xc5\xa0", "scaron", "\xc5\xa1", "Yuml", "\xc5\xb8",
"circ", "\xcb\x86", "tilde", "\xcb\x9c", "ensp", "\xe2\x80\x82",
"emsp", "\xe2\x80\x83", "thinsp", "\xe2\x80\x89", "zwnj", "\xe2\x80\x8c",
"zwj", "\xe2\x80\x8d", "lrm", "\xe2\x80\x8e", "rlm", "\xe2\x80\x8f",
"ndash", "\xe2\x80\x93", "mdash", "\xe2\x80\x94", "lsquo", "\xe2\x80\x98",
"rsquo", "\xe2\x80\x99", "sbquo", "\xe2\x80\x9a", "ldquo", "\xe2\x80\x9c",
"rdquo", "\xe2\x80\x9d", "bdquo", "\xe2\x80\x9e", "dagger", "\xe2\x80\xa0",
"Dagger", "\xe2\x80\xa1", "permil", "\xe2\x80\xb0", "lsaquo", "\xe2\x80\xb9",
"rsaquo", "\xe2\x80\xba", "euro", "\xe2\x82\xac",
NULL, NULL
};
map<string, string> my_named_ents;
class NamedEntsInitializer {
public:
NamedEntsInitializer()
{
for (int i = 0;;) {
const char *ent;
const char *val;
ent = epairs[i++];
if (ent == 0)
break;
val = epairs[i++];
if (val == 0)
break;
my_named_ents[string(ent)] = val;
}
}
};
static NamedEntsInitializer namedEntsInitializerInstance;
MyHtmlParser::MyHtmlParser()
: in_script_tag(false),
in_style_tag(false),
in_body_tag(false),
in_pre_tag(false),
pending_space(false),
indexing_allowed(true)
{
// The default html document charset is iso-8859-1. We'll update
// this value from the encoding tag if found.
charset = "iso-8859-1";
}
void MyHtmlParser::decode_entities(string &s)
{
LOGDEB2(("MyHtmlParser::decode_entities\n"));
// This has no meaning whatsoever if the character encoding is unknown,
// so don't do it. If charset known, caller has converted text to utf-8,
// and this is also how we translate entities
// if (tocharset != "utf-8")
// return;
// We need a const_iterator version of s.end() - otherwise the
// find() and find_if() templates don't work...
string::const_iterator amp = s.begin(), s_end = s.end();
while ((amp = find(amp, s_end, '&')) != s_end) {
unsigned int val = 0;
string::const_iterator end, p = amp + 1;
string subs;
if (p != s_end && *p == '#') {
p++;
if (p != s_end && (*p == 'x' || *p == 'X')) {
// hex
p++;
end = find_if(p, s_end, p_notxdigit);
sscanf(s.substr(p - s.begin(), end - p).c_str(), "%x", &val);
} else {
// number
end = find_if(p, s_end, p_notdigit);
val = atoi(s.substr(p - s.begin(), end - p).c_str());
}
} else {
end = find_if(p, s_end, p_notalnum);
string code = s.substr(p - s.begin(), end - p);
map<string, string>::const_iterator i;
i = my_named_ents.find(code);
if (i != my_named_ents.end())
subs = i->second;
}
if (end < s_end && *end == ';')
end++;
if (val) {
// The code is the code position for a unicode char. We need
// to translate it to an utf-8 string.
string utf16be;
utf16be += char(val / 256);
utf16be += char(val % 256);
transcode(utf16be, subs, "UTF-16BE", "UTF-8");
}
if (subs.length() > 0) {
string::size_type amp_pos = amp - s.begin();
s.replace(amp_pos, end - amp, subs);
s_end = s.end();
// We've modified the string, so the iterators are no longer
// valid...
amp = s.begin() + amp_pos + subs.length();
} else {
amp = end;
}
}
}
// Compress whitespace and suppress newlines
// Note that we independantly add some newlines to the output text in the
// tag processing code. Like this, the preview looks a bit more like what a
// browser would display.
// We keep whitespace inside <pre> tags
void
MyHtmlParser::process_text(const string &text)
{
LOGDEB2(("process_text: pending_space %d txt [%s]\n", pending_space,
text.c_str()));
CancelCheck::instance().checkCancel();
if (!in_script_tag && !in_style_tag) {
if (!in_pre_tag) {
string::size_type b = 0;
bool only_space = true;
while ((b = text.find_first_not_of(WHITESPACE, b)) != string::npos) {
only_space = false;
// If space specifically needed or chunk begins with
// whitespace, add exactly one space
if (pending_space || b != 0) {
dump += ' ';
}
pending_space = true;
string::size_type e = text.find_first_of(WHITESPACE, b);
if (e == string::npos) {
dump += text.substr(b);
pending_space = false;
break;
}
dump += text.substr(b, e - b);
b = e + 1;
}
if (only_space)
pending_space = true;
} else {
if (pending_space)
dump += ' ';
dump += text;
}
}
}
bool
MyHtmlParser::opening_tag(const string &tag)
{
LOGDEB2(("opening_tag: [%s]\n", tag.c_str()));
#if 0
cout << "TAG: " << tag << ": " << endl;
map<string, string>::const_iterator x;
for (x = p.begin(); x != p.end(); x++) {
cout << " " << x->first << " -> '" << x->second << "'" << endl;
}
#endif
if (tag.empty()) return true;
switch (tag[0]) {
case 'a':
if (tag == "address") pending_space = true;
break;
case 'b':
if (tag == "body") {
dump.resize(0);
in_body_tag = true;
break;
}
if (tag == "blockquote" || tag == "br") {
dump += '\n';
pending_space = true;
}
break;
case 'c':
if (tag == "center") pending_space = true;
break;
case 'd':
if (tag == "dd" || tag == "dir" || tag == "div" || tag == "dl" ||
tag == "dt") pending_space = true;
if (tag == "dt")
dump += '\n';
break;
case 'e':
if (tag == "embed") pending_space = true;
break;
case 'f':
if (tag == "fieldset" || tag == "form") pending_space = true;
break;
case 'h':
// hr, and h1, ..., h6
if (tag.length() == 2 && strchr("r123456", tag[1])) {
dump += '\n';
pending_space = true;
}
break;
case 'i':
if (tag == "iframe" || tag == "img" || tag == "isindex" ||
tag == "input") pending_space = true;
break;
case 'k':
if (tag == "keygen") pending_space = true;
break;
case 'l':
if (tag == "legend" || tag == "li" || tag == "listing") {
dump += '\n';
pending_space = true;
}
break;
case 'm':
if (tag == "meta") {
string content;
if (get_parameter(cstr_content, content)) {
string name;
if (get_parameter("name", name)) {
lowercase_term(name);
if (name == "date") {
// Yes this doesnt exist. It's output by filters
// And the format isn't even standard http/html
// FIXME
decode_entities(content);
struct tm tm;
if (strptime(content.c_str(),
" %Y-%m-%d %H:%M:%S ", &tm) ||
strptime(content.c_str(),
"%Y-%m-%dT%H:%M:%S", &tm)
) {
char ascuxtime[100];
sprintf(ascuxtime, "%ld", (long)mktime(&tm));
dmtime = ascuxtime;
}
} else if (name == "robots") {
} else {
if (!meta[name].empty())
meta[name] += ' ';
decode_entities(content);
meta[name] += content;
}
}
string hdr;
if (get_parameter("http-equiv", hdr)) {
lowercase_term(hdr);
if (hdr == "content-type") {
MimeHeaderValue p;
parseMimeHeaderValue(content, p);
map<string, string>::const_iterator k;
if ((k = p.params.find(cstr_charset)) !=
p.params.end()) {
charset = k->second;
if (!samecharset(charset, fromcharset)) {
LOGDEB1(("Doc specified charset '%s' "
"differs from dir deflt '%s'\n",
charset.c_str(),
fromcharset.c_str()));
throw false;
}
}
}
}
}
break;
}
if (tag == "marquee" || tag == "menu" || tag == "multicol")
pending_space = true;
break;
case 'o':
if (tag == "ol" || tag == "option") pending_space = true;
break;
case 'p':
if (tag == "p" || tag == "plaintext") {
dump += '\n';
pending_space = true;
} else if (tag == "pre") {
in_pre_tag = true;
dump += '\n';
pending_space = true;
}
break;
case 'q':
if (tag == "q") pending_space = true;
break;
case 's':
if (tag == "style") {
in_style_tag = true;
break;
}
if (tag == "script") {
in_script_tag = true;
break;
}
if (tag == "select") pending_space = true;
break;
case 't':
if (tag == "table" || tag == "td" || tag == "textarea" ||
tag == "th") pending_space = true;
break;
case 'u':
if (tag == "ul") pending_space = true;
break;
case 'x':
if (tag == "xmp") pending_space = true;
break;
}
return true;
}
bool
MyHtmlParser::closing_tag(const string &tag)
{
LOGDEB2(("closing_tag: [%s]\n", tag.c_str()));
if (tag.empty()) return true;
switch (tag[0]) {
case 'a':
if (tag == "address") pending_space = true;
break;
case 'b':
if (tag == "body") {
LOGDEB1(("Myhtmlparse: body close tag found\n"));
in_body_tag = false;
return false;
}
if (tag == "blockquote" || tag == "br") pending_space = true;
break;
case 'c':
if (tag == "center") pending_space = true;
break;
case 'd':
if (tag == "dd" || tag == "dir" || tag == "div" || tag == "dl" ||
tag == "dt") pending_space = true;
break;
case 'f':
if (tag == "fieldset" || tag == "form") pending_space = true;
break;
case 'h':
// hr, and h1, ..., h6
if (tag.length() == 2 && strchr("r123456", tag[1]))
pending_space = true;
break;
case 'i':
if (tag == "iframe") pending_space = true;
break;
case 'l':
if (tag == "legend" || tag == "li" || tag == "listing")
pending_space = true;
break;
case 'm':
if (tag == "marquee" || tag == "menu") pending_space = true;
break;
case 'o':
if (tag == "ol" || tag == "option") pending_space = true;
break;
case 'p':
if (tag == "p") {
pending_space = true;
} else if (tag == "pre") {
pending_space = true;
in_pre_tag = false;
}
break;
case 'q':
if (tag == "q") pending_space = true;
break;
case 's':
if (tag == "style") {
in_style_tag = false;
break;
}
if (tag == "script") {
in_script_tag = false;
break;
}
if (tag == "select") pending_space = true;
break;
case 't':
if (tag == "title") {
if (meta.find("title") == meta.end()|| meta["title"].empty()) {
meta["title"] = dump;
dump.clear();
}
break;
}
if (tag == "table" || tag == "td" || tag == "textarea" ||
tag == "th") pending_space = true;
break;
case 'u':
if (tag == "ul") pending_space = true;
break;
case 'x':
if (tag == "xmp") pending_space = true;
break;
}
return true;
}
// This gets called when hitting eof.
// We used to do:
// > If the <body> is open, do
// > something with the text (that is, don't throw up). Else, things are
// > too weird, throw an error. We don't get called if the parser finds
// > a closing body tag (exception gets thrown by closing_tag())
// But we don't throw any more. Whatever text we've extracted up to now is
// better than nothing.
void
MyHtmlParser::do_eof()
{
// if (!in_body_tag)
// throw(false);
}