|
a/src/internfile/myhtmlparse.cpp |
|
b/src/internfile/myhtmlparse.cpp |
|
|
1 |
/* myhtmlparse.cc: subclass of HtmlParser for extracting text
|
|
|
2 |
*
|
|
|
3 |
* ----START-LICENCE----
|
|
|
4 |
* Copyright 1999,2000,2001 BrightStation PLC
|
|
|
5 |
* Copyright 2002,2003,2004 Olly Betts
|
|
|
6 |
*
|
|
|
7 |
* This program is free software; you can redistribute it and/or
|
|
|
8 |
* modify it under the terms of the GNU General Public License as
|
|
|
9 |
* published by the Free Software Foundation; either version 2 of the
|
|
|
10 |
* License, or (at your option) any later version.
|
|
|
11 |
*
|
|
|
12 |
* This program is distributed in the hope that it will be useful,
|
|
|
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
15 |
* GNU General Public License for more details.
|
|
|
16 |
*
|
|
|
17 |
* You should have received a copy of the GNU General Public License
|
|
|
18 |
* along with this program; if not, write to the Free Software
|
|
|
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
20 |
* USA
|
|
|
21 |
* -----END-LICENCE-----
|
|
|
22 |
*/
|
|
|
23 |
|
|
|
24 |
#include "myhtmlparse.h"
|
|
|
25 |
|
|
|
26 |
#include "indextext.h" // for lowercase_term()
|
|
|
27 |
|
|
|
28 |
void
|
|
|
29 |
MyHtmlParser::process_text(const string &text)
|
|
|
30 |
{
|
|
|
31 |
if (!in_script_tag && !in_style_tag) {
|
|
|
32 |
string::size_type b = 0;
|
|
|
33 |
while ((b = text.find_first_not_of(WHITESPACE, b)) != string::npos) {
|
|
|
34 |
if (pending_space || b != 0)
|
|
|
35 |
if (!dump.empty()) dump += ' ';
|
|
|
36 |
pending_space = true;
|
|
|
37 |
string::size_type e = text.find_first_of(WHITESPACE, b);
|
|
|
38 |
if (e == string::npos) {
|
|
|
39 |
dump += text.substr(b);
|
|
|
40 |
pending_space = false;
|
|
|
41 |
break;
|
|
|
42 |
}
|
|
|
43 |
dump += text.substr(b, e - b);
|
|
|
44 |
b = e + 1;
|
|
|
45 |
}
|
|
|
46 |
}
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
void
|
|
|
50 |
MyHtmlParser::opening_tag(const string &tag, const map<string,string> &p)
|
|
|
51 |
{
|
|
|
52 |
#if 0
|
|
|
53 |
cout << "<" << tag;
|
|
|
54 |
map<string, string>::const_iterator x;
|
|
|
55 |
for (x = p.begin(); x != p.end(); x++) {
|
|
|
56 |
cout << " " << x->first << "=\"" << x->second << "\"";
|
|
|
57 |
}
|
|
|
58 |
cout << ">\n";
|
|
|
59 |
#endif
|
|
|
60 |
if (tag.empty()) return;
|
|
|
61 |
switch (tag[0]) {
|
|
|
62 |
case 'a':
|
|
|
63 |
if (tag == "address") pending_space = true;
|
|
|
64 |
break;
|
|
|
65 |
case 'b':
|
|
|
66 |
if (tag == "body") {
|
|
|
67 |
dump = "";
|
|
|
68 |
break;
|
|
|
69 |
}
|
|
|
70 |
if (tag == "blockquote" || tag == "br") pending_space = true;
|
|
|
71 |
break;
|
|
|
72 |
case 'c':
|
|
|
73 |
if (tag == "center") pending_space = true;
|
|
|
74 |
break;
|
|
|
75 |
case 'd':
|
|
|
76 |
if (tag == "dd" || tag == "dir" || tag == "div" || tag == "dl" ||
|
|
|
77 |
tag == "dt") pending_space = true;
|
|
|
78 |
break;
|
|
|
79 |
case 'e':
|
|
|
80 |
if (tag == "embed") pending_space = true;
|
|
|
81 |
break;
|
|
|
82 |
case 'f':
|
|
|
83 |
if (tag == "fieldset" || tag == "form") pending_space = true;
|
|
|
84 |
break;
|
|
|
85 |
case 'h':
|
|
|
86 |
// hr, and h1, ..., h6
|
|
|
87 |
if (tag.length() == 2 && strchr("r123456", tag[1]))
|
|
|
88 |
pending_space = true;
|
|
|
89 |
break;
|
|
|
90 |
case 'i':
|
|
|
91 |
if (tag == "iframe" || tag == "img" || tag == "isindex" ||
|
|
|
92 |
tag == "input") pending_space = true;
|
|
|
93 |
break;
|
|
|
94 |
case 'k':
|
|
|
95 |
if (tag == "keygen") pending_space = true;
|
|
|
96 |
break;
|
|
|
97 |
case 'l':
|
|
|
98 |
if (tag == "legend" || tag == "li" || tag == "listing")
|
|
|
99 |
pending_space = true;
|
|
|
100 |
break;
|
|
|
101 |
case 'm':
|
|
|
102 |
if (tag == "meta") {
|
|
|
103 |
map<string, string>::const_iterator i, j;
|
|
|
104 |
if ((i = p.find("content")) != p.end()) {
|
|
|
105 |
if ((j = p.find("name")) != p.end()) {
|
|
|
106 |
string name = j->second;
|
|
|
107 |
lowercase_term(name);
|
|
|
108 |
if (name == "description") {
|
|
|
109 |
if (sample.empty()) {
|
|
|
110 |
sample = i->second;
|
|
|
111 |
decode_entities(sample);
|
|
|
112 |
}
|
|
|
113 |
} else if (name == "keywords") {
|
|
|
114 |
if (!keywords.empty()) keywords += ' ';
|
|
|
115 |
string tmp = i->second;
|
|
|
116 |
decode_entities(tmp);
|
|
|
117 |
keywords += tmp;
|
|
|
118 |
} else if (name == "robots") {
|
|
|
119 |
string val = i->second;
|
|
|
120 |
decode_entities(val);
|
|
|
121 |
lowercase_term(val);
|
|
|
122 |
if (val.find("none") != string::npos ||
|
|
|
123 |
val.find("noindex") != string::npos) {
|
|
|
124 |
indexing_allowed = false;
|
|
|
125 |
throw true;
|
|
|
126 |
}
|
|
|
127 |
}
|
|
|
128 |
}
|
|
|
129 |
}
|
|
|
130 |
break;
|
|
|
131 |
}
|
|
|
132 |
if (tag == "marquee" || tag == "menu" || tag == "multicol")
|
|
|
133 |
pending_space = true;
|
|
|
134 |
break;
|
|
|
135 |
case 'o':
|
|
|
136 |
if (tag == "ol" || tag == "option") pending_space = true;
|
|
|
137 |
break;
|
|
|
138 |
case 'p':
|
|
|
139 |
if (tag == "p" || tag == "pre" || tag == "plaintext")
|
|
|
140 |
pending_space = true;
|
|
|
141 |
break;
|
|
|
142 |
case 'q':
|
|
|
143 |
if (tag == "q") pending_space = true;
|
|
|
144 |
break;
|
|
|
145 |
case 's':
|
|
|
146 |
if (tag == "style") {
|
|
|
147 |
in_style_tag = true;
|
|
|
148 |
break;
|
|
|
149 |
}
|
|
|
150 |
if (tag == "script") {
|
|
|
151 |
in_script_tag = true;
|
|
|
152 |
break;
|
|
|
153 |
}
|
|
|
154 |
if (tag == "select") pending_space = true;
|
|
|
155 |
break;
|
|
|
156 |
case 't':
|
|
|
157 |
if (tag == "table" || tag == "td" || tag == "textarea" ||
|
|
|
158 |
tag == "th") pending_space = true;
|
|
|
159 |
break;
|
|
|
160 |
case 'u':
|
|
|
161 |
if (tag == "ul") pending_space = true;
|
|
|
162 |
break;
|
|
|
163 |
case 'x':
|
|
|
164 |
if (tag == "xmp") pending_space = true;
|
|
|
165 |
break;
|
|
|
166 |
}
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
void
|
|
|
170 |
MyHtmlParser::closing_tag(const string &tag)
|
|
|
171 |
{
|
|
|
172 |
if (tag.empty()) return;
|
|
|
173 |
switch (tag[0]) {
|
|
|
174 |
case 'a':
|
|
|
175 |
if (tag == "address") pending_space = true;
|
|
|
176 |
break;
|
|
|
177 |
case 'b':
|
|
|
178 |
if (tag == "body") {
|
|
|
179 |
throw true;
|
|
|
180 |
}
|
|
|
181 |
if (tag == "blockquote" || tag == "br") pending_space = true;
|
|
|
182 |
break;
|
|
|
183 |
case 'c':
|
|
|
184 |
if (tag == "center") pending_space = true;
|
|
|
185 |
break;
|
|
|
186 |
case 'd':
|
|
|
187 |
if (tag == "dd" || tag == "dir" || tag == "div" || tag == "dl" ||
|
|
|
188 |
tag == "dt") pending_space = true;
|
|
|
189 |
break;
|
|
|
190 |
case 'f':
|
|
|
191 |
if (tag == "fieldset" || tag == "form") pending_space = true;
|
|
|
192 |
break;
|
|
|
193 |
case 'h':
|
|
|
194 |
// hr, and h1, ..., h6
|
|
|
195 |
if (tag.length() == 2 && strchr("r123456", tag[1]))
|
|
|
196 |
pending_space = true;
|
|
|
197 |
break;
|
|
|
198 |
case 'i':
|
|
|
199 |
if (tag == "iframe") pending_space = true;
|
|
|
200 |
break;
|
|
|
201 |
case 'l':
|
|
|
202 |
if (tag == "legend" || tag == "li" || tag == "listing")
|
|
|
203 |
pending_space = true;
|
|
|
204 |
break;
|
|
|
205 |
case 'm':
|
|
|
206 |
if (tag == "marquee" || tag == "menu") pending_space = true;
|
|
|
207 |
break;
|
|
|
208 |
case 'o':
|
|
|
209 |
if (tag == "ol" || tag == "option") pending_space = true;
|
|
|
210 |
break;
|
|
|
211 |
case 'p':
|
|
|
212 |
if (tag == "p" || tag == "pre") pending_space = true;
|
|
|
213 |
break;
|
|
|
214 |
case 'q':
|
|
|
215 |
if (tag == "q") pending_space = true;
|
|
|
216 |
break;
|
|
|
217 |
case 's':
|
|
|
218 |
if (tag == "style") {
|
|
|
219 |
in_style_tag = false;
|
|
|
220 |
break;
|
|
|
221 |
}
|
|
|
222 |
if (tag == "script") {
|
|
|
223 |
in_script_tag = false;
|
|
|
224 |
break;
|
|
|
225 |
}
|
|
|
226 |
if (tag == "select") pending_space = true;
|
|
|
227 |
break;
|
|
|
228 |
case 't':
|
|
|
229 |
if (tag == "title") {
|
|
|
230 |
if (title.empty()) {
|
|
|
231 |
title = dump;
|
|
|
232 |
dump = "";
|
|
|
233 |
}
|
|
|
234 |
break;
|
|
|
235 |
}
|
|
|
236 |
if (tag == "table" || tag == "td" || tag == "textarea" ||
|
|
|
237 |
tag == "th") pending_space = true;
|
|
|
238 |
break;
|
|
|
239 |
case 'u':
|
|
|
240 |
if (tag == "ul") pending_space = true;
|
|
|
241 |
break;
|
|
|
242 |
case 'x':
|
|
|
243 |
if (tag == "xmp") pending_space = true;
|
|
|
244 |
break;
|
|
|
245 |
}
|
|
|
246 |
}
|