1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # HTMLPlug.pm -- basic html plugin
|
---|
4 | #
|
---|
5 | # A component of the Greenstone digital library software
|
---|
6 | # from the New Zealand Digital Library Project at the
|
---|
7 | # University of Waikato, New Zealand.
|
---|
8 | #
|
---|
9 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
10 | #
|
---|
11 | # This program is free software; you can redistribute it and/or modify
|
---|
12 | # it under the terms of the GNU General Public License as published by
|
---|
13 | # the Free Software Foundation; either version 2 of the License, or
|
---|
14 | # (at your option) any later version.
|
---|
15 | #
|
---|
16 | # This program is distributed in the hope that it will be useful,
|
---|
17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | # GNU General Public License for more details.
|
---|
20 | #
|
---|
21 | # You should have received a copy of the GNU General Public License
|
---|
22 | # along with this program; if not, write to the Free Software
|
---|
23 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
24 | #
|
---|
25 | ###########################################################################
|
---|
26 |
|
---|
27 | #
|
---|
28 | # Note that this plugin handles frames only in a very simple way
|
---|
29 | # i.e. each frame is treated as a separate document. This means
|
---|
30 | # search results will contain links to individual frames rather
|
---|
31 | # than linking to the top level frameset.
|
---|
32 | # There may also be some problems caused by the _parent target
|
---|
33 | # (it's removed by this plugin)
|
---|
34 | # To use frames properly you'll need to use the WebPlug plugin.
|
---|
35 | #
|
---|
36 |
|
---|
37 |
|
---|
38 | package HTMLPlug;
|
---|
39 |
|
---|
40 | use BasPlug;
|
---|
41 | use html;
|
---|
42 | use util;
|
---|
43 | use parsargv;
|
---|
44 |
|
---|
45 | sub BEGIN {
|
---|
46 | @ISA = ('BasPlug');
|
---|
47 | }
|
---|
48 |
|
---|
49 | use strict;
|
---|
50 |
|
---|
51 | sub print_usage {
|
---|
52 | print STDERR "\nIncorrect options passed to HTMLPlug, check your collect.cfg configuration file\n";
|
---|
53 |
|
---|
54 | print STDERR "\n usage: plugin HTMLPlug [options]\n\n";
|
---|
55 | print STDERR " options:\n";
|
---|
56 | print STDERR " -process_exp A perl regular expression to match against filenames.\n";
|
---|
57 | print STDERR " Matching filenames will be processed by this plugin.\n";
|
---|
58 | print STDERR " Defaults to '(?i)\.html?\$' i.e. all documents ending in\n";
|
---|
59 | print STDERR " .htm or .html (case-insensitive).\n";
|
---|
60 | print STDERR " -nolinks Don't make any attempt to trap links (setting this flag may\n";
|
---|
61 | print STDERR " improve speed of building/importing but any relative links within\n";
|
---|
62 | print STDERR " documents will be broken).\n";
|
---|
63 | print STDERR " -block_exp Files matching this regular expression will be blocked from\n";
|
---|
64 | print STDERR " being passed to any further plugins in the list. By default\n";
|
---|
65 | print STDERR " HTMLPlug blocks any files with .gif, .jpg, .jpeg, .png, .pdf,\n";
|
---|
66 | print STDERR " .rtf or .css file extensions.\n";
|
---|
67 | print STDERR " -keep_head Don't remove headers from html files.\n";
|
---|
68 | print STDERR " -no_metadata Don't attempt to extract any metadata from files.\n";
|
---|
69 | print STDERR " -metadata_fields Comma separated list of metadata fields to attempt to extract.\n";
|
---|
70 | print STDERR " Defaults to 'Title'\n";
|
---|
71 | print STDERR " -w3mir Set if w3mir was used to generate input file structure.\n";
|
---|
72 | print STDERR " w3mir \n";
|
---|
73 | }
|
---|
74 |
|
---|
75 | sub new {
|
---|
76 | my $class = shift (@_);
|
---|
77 | my $self = new BasPlug ();
|
---|
78 |
|
---|
79 | if (!parsargv::parse(\@_,
|
---|
80 | q^process_exp/.*/(?i)\.html?$^, \$self->{'process_exp'},
|
---|
81 | q^nolinks^, \$self->{'nolinks'},
|
---|
82 | q^block_exp/.*/(?i)\.(gif|jpe?g|png|pdf|rtf|css)$^, \$self->{'block_exp'},
|
---|
83 | q^keep_head^, \$self->{'keep_head'},
|
---|
84 | q^no_metadata^, \$self->{'no_metadata'},
|
---|
85 | q^metadata_fields/.*/Title^, \$self->{'metadata_fields'},
|
---|
86 | q^w3mir^, \$self->{'w3mir'})) {
|
---|
87 | &print_usage();
|
---|
88 | die "\n";
|
---|
89 | }
|
---|
90 |
|
---|
91 | $self->{'aux_files'} = {};
|
---|
92 | $self->{'dir_num'} = 0;
|
---|
93 | $self->{'file_num'} = 0;
|
---|
94 |
|
---|
95 | return bless $self, $class;
|
---|
96 | }
|
---|
97 |
|
---|
98 | sub is_recursive {
|
---|
99 | my $self = shift (@_);
|
---|
100 |
|
---|
101 | return 0; # this is not a recursive plugin
|
---|
102 | }
|
---|
103 |
|
---|
104 | # return number of files processed, undef if can't process
|
---|
105 | # Note that $base_dir might be "" and that $file might
|
---|
106 | # include directories
|
---|
107 | sub read {
|
---|
108 | my $self = shift (@_);
|
---|
109 | my ($pluginfo, $base_dir, $file, $metadata, $processor) = @_;
|
---|
110 |
|
---|
111 | my $filename = &util::filename_cat($base_dir, $file);
|
---|
112 | return 0 if $filename =~ /$self->{'block_exp'}/;
|
---|
113 | if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
|
---|
114 | return undef;
|
---|
115 | }
|
---|
116 | $file =~ s/^[\/\\]+//;
|
---|
117 |
|
---|
118 | $self->{'verbosity'} = $processor->{'verbosity'};
|
---|
119 | print STDERR "HTMLPlug: processing $file\n"
|
---|
120 | if $self->{'verbosity'} > 1;
|
---|
121 |
|
---|
122 | # create a new document
|
---|
123 | my $doc_obj = new doc ($file, "indexed_doc");
|
---|
124 | my $cursection = $doc_obj->get_top_section();
|
---|
125 |
|
---|
126 | # read in HTML file
|
---|
127 | open (FILE, $filename) || die "HTMLPlug::read - can't open $filename\n";
|
---|
128 | undef $/;
|
---|
129 | my $text = <FILE>;
|
---|
130 | $/ = "\n";
|
---|
131 | close FILE;
|
---|
132 | if ($text !~ /\w/) {
|
---|
133 | print STDERR "HTMLPlug: ERROR: $file contains no text\n" if $self->{'verbosity'};
|
---|
134 | return 0;
|
---|
135 | }
|
---|
136 |
|
---|
137 | $self->extra_metadata ($doc_obj, $cursection, $metadata);
|
---|
138 | $self->extract_metadata (\$text, $metadata, $doc_obj, $cursection)
|
---|
139 | unless $self->{'no_metadata'};
|
---|
140 |
|
---|
141 | # Store URL for page as metadata - this can be used for an
|
---|
142 | # altavista style search interface. The URL won't be valid
|
---|
143 | # unless the file structure contains the domain name (i.e.
|
---|
144 | # like when w3mir is used to download a website).
|
---|
145 | my $web_url = "http://$file";
|
---|
146 | $web_url =~ s/\\/\//g; # for windows
|
---|
147 | $doc_obj->add_metadata($cursection, "URL", $web_url);
|
---|
148 |
|
---|
149 | # remove header and footer
|
---|
150 | if (!$self->{'keep_head'}) {
|
---|
151 | $text =~ s/^.*?<body[^>]*>//is;
|
---|
152 | $text =~ s/(<\/body[^>]*>|<\/html[^>]*>)//isg;
|
---|
153 | }
|
---|
154 |
|
---|
155 | # trap links
|
---|
156 | if (!$self->{'nolinks'}) {
|
---|
157 |
|
---|
158 | # usemap="./#index" not handled correctly => change to "#index"
|
---|
159 | $text =~ s/(<img[^>]*?usemap\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/
|
---|
160 | $self->replace_usemap_links($1, $2, $3)/isge;
|
---|
161 |
|
---|
162 | $text =~ s/(<(?:a|area|frame|link)\s+[^>]*?(?:href|src)\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/
|
---|
163 | $self->replace_href_links ($1, $2, $3, $base_dir, $file, $doc_obj, $cursection)/isge;
|
---|
164 | }
|
---|
165 |
|
---|
166 | # trap images
|
---|
167 | $text =~ s/(<img[^>]*?src\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/
|
---|
168 | $self->replace_images ($1, $2, $3, $base_dir, $file, $doc_obj, $cursection)/isge;
|
---|
169 |
|
---|
170 | $doc_obj->add_text ($cursection, $text);
|
---|
171 |
|
---|
172 | # add an OID
|
---|
173 | $doc_obj->set_OID();
|
---|
174 |
|
---|
175 | # process the document
|
---|
176 | $processor->process($doc_obj);
|
---|
177 |
|
---|
178 | return 1; # processed the file
|
---|
179 | }
|
---|
180 |
|
---|
181 | sub replace_images {
|
---|
182 | my $self = shift (@_);
|
---|
183 | my ($front, $link, $back, $base_dir,
|
---|
184 | $file, $doc_obj, $section) = @_;
|
---|
185 |
|
---|
186 | $link =~ s/\n/ /g;
|
---|
187 |
|
---|
188 | my ($href, $hash_part, $rl) = $self->format_link ($link, $base_dir, $file);
|
---|
189 | return $front . $self->add_file ($href, $base_dir, $doc_obj, $section) . $back;
|
---|
190 | }
|
---|
191 |
|
---|
192 | sub replace_href_links {
|
---|
193 | my $self = shift (@_);
|
---|
194 | my ($front, $link, $back, $base_dir, $file, $doc_obj, $section) = @_;
|
---|
195 |
|
---|
196 | # attempt to sort out targets - frames are not handled
|
---|
197 | # well in this plugin and some cases will screw things
|
---|
198 | # up - e.g. the _parent target (so we'll just remove
|
---|
199 | # them all ;-)
|
---|
200 | $front =~ s/(target=\"?)_top(\"?)/$1_gsdltop_$2/is;
|
---|
201 | $back =~ s/(target=\"?)_top(\"?)/$1_gsdltop_$2/is;
|
---|
202 | $front =~ s/target=\"?_parent\"?//is;
|
---|
203 | $back =~ s/target=\"?_parent\"?//is;
|
---|
204 |
|
---|
205 | return $front . $link . $back if $link =~ /^\#/s;
|
---|
206 | $link =~ s/\n/ /g;
|
---|
207 |
|
---|
208 | my ($href, $hash_part, $rl) = $self->format_link ($link, $base_dir, $file);
|
---|
209 |
|
---|
210 | my ($filename) = $href =~ /^(?:.*?):(?:\/\/)?(.*)/;
|
---|
211 |
|
---|
212 | ##### leave all these links alone (they won't be picked up by intermediate
|
---|
213 | ##### pages). I think that's safest when dealing with frames, targets etc.
|
---|
214 | ##### (at least until I think of a better way to do it). Problems occur with
|
---|
215 | ##### mailto links from within small frames, the intermediate page is displayed
|
---|
216 | ##### within that frame and can't be seen. There is still potential for this to
|
---|
217 | ##### happen even with html pages - the solution seems to be to somehow tell
|
---|
218 | ##### the browser from the server side to display the page being sent (i.e.
|
---|
219 | ##### the intermediate page) in the top level window - I'm not sure if that's
|
---|
220 | ##### possible - the following line should probably be deleted if that can be done
|
---|
221 | return $front . $link . $back if $href =~ /^(mailto|news|gopher|nntp|telnet|javascript):/is;
|
---|
222 |
|
---|
223 |
|
---|
224 | if (($rl == 0) || ($filename =~ /$self->{'process_exp'}/) ||
|
---|
225 | ($href =~ /\/$/) || ($href =~ /^(mailto|news|gopher|nntp|telnet|javascript):/i)) {
|
---|
226 | &html::urlsafe ($href);
|
---|
227 | return $front . "_httpextlink_&rl=" . $rl . "&href=" . $href . $hash_part . $back;
|
---|
228 |
|
---|
229 | } else {
|
---|
230 | # link is to some other type of file (image, pdf etc.) so we'll
|
---|
231 | # need to associate that file
|
---|
232 | return $front . $self->add_file ($href, $base_dir, $doc_obj, $section) . $back;
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | sub add_file {
|
---|
237 | my $self = shift (@_);
|
---|
238 | my ($href, $base_dir, $doc_obj, $section) = @_;
|
---|
239 | my ($newname);
|
---|
240 |
|
---|
241 | my $filename = $href;
|
---|
242 | $filename =~ s/^[^:]*:\/\///;
|
---|
243 | $filename = &util::filename_cat ($base_dir, $filename);
|
---|
244 | my ($ext) = $filename =~ /(\.[^\.]*)$/;
|
---|
245 | if (defined $self->{'aux_files'}->{$href}) {
|
---|
246 | $newname = $self->{'aux_files'}->{$href}->{'dir_num'} . "/" .
|
---|
247 | $self->{'aux_files'}->{$href}->{'file_num'} . $ext;
|
---|
248 | } else {
|
---|
249 | $newname = $self->{'dir_num'} . "/" . $self->{'file_num'} . $ext;
|
---|
250 | $self->inc_filecount ();
|
---|
251 | }
|
---|
252 | $doc_obj->associate_file($filename, $newname, undef, $section);
|
---|
253 | return "_httpcollimg_/$newname";
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 | sub format_link {
|
---|
258 | my $self = shift (@_);
|
---|
259 | my ($link, $base_dir, $file) = @_;
|
---|
260 |
|
---|
261 | my ($before_hash, $hash_part) = $link =~ /^([^\#]*)(\#?.*)$/;
|
---|
262 | $hash_part = "" if !defined $hash_part;
|
---|
263 | if (!defined $before_hash || $before_hash !~ /[\w\.\/]/) {
|
---|
264 | print STDERR "HTMLPlug: ERROR - badly formatted tag ignored ($link)\n"
|
---|
265 | if $self->{'verbosity'};
|
---|
266 | return ($link, "", 0);
|
---|
267 | }
|
---|
268 |
|
---|
269 | if ($before_hash =~ s/^((?:http|ftp|file):\/\/)//i) {
|
---|
270 | my $type = $1;
|
---|
271 |
|
---|
272 | if ($link =~ /^(http|ftp):/i) {
|
---|
273 | # Turn url (using /) into file name (possibly using \ on windows)
|
---|
274 | my @http_dir_split = split('/', $before_hash);
|
---|
275 | $before_hash = &util::filename_cat(@http_dir_split);
|
---|
276 | }
|
---|
277 |
|
---|
278 | $before_hash = $self->eval_dir_dots($before_hash);
|
---|
279 |
|
---|
280 | my $linkfilename = &util::filename_cat ($base_dir, $before_hash);
|
---|
281 |
|
---|
282 | my $rl = 0;
|
---|
283 | $rl = 1 if (-e $linkfilename);
|
---|
284 |
|
---|
285 | # make sure there's a slash on the end if it's a directory
|
---|
286 | if ($before_hash !~ /\/$/) {
|
---|
287 | $before_hash .= "/" if (-d $linkfilename);
|
---|
288 | }
|
---|
289 |
|
---|
290 | return ($type . $before_hash, $hash_part, $rl);
|
---|
291 |
|
---|
292 | } elsif ($link !~ /^(mailto|news|gopher|nntp|telnet|javascript):/i) {
|
---|
293 |
|
---|
294 | if ($before_hash =~ s/^\///) {
|
---|
295 | # the first directory will be the domain name if w3mir was used
|
---|
296 | # to generate archives, otherwise we'll assume all files are
|
---|
297 | # from the same site and base_dir is the root
|
---|
298 | if ($self->{'w3mir'}) {
|
---|
299 | my @dirs = split /[\/\\]/, $file;
|
---|
300 | my $domname = shift (@dirs);
|
---|
301 | $before_hash = &util::filename_cat($domname, $before_hash);
|
---|
302 | $before_hash =~ s/\\/\//g; # for windows
|
---|
303 | }
|
---|
304 |
|
---|
305 | } else {
|
---|
306 | # Turn relative file path into full path
|
---|
307 | my $dirname = &File::Basename::dirname($file);
|
---|
308 | $before_hash = &util::filename_cat($dirname, $before_hash);
|
---|
309 | $before_hash = $self->eval_dir_dots($before_hash);
|
---|
310 | }
|
---|
311 |
|
---|
312 | # make sure there's a slash on the end if it's a directory
|
---|
313 | my $linkfilename = &util::filename_cat ($base_dir, $before_hash);
|
---|
314 | if ($before_hash !~ /\/$/) {
|
---|
315 | $before_hash .= "/" if (-d $linkfilename);
|
---|
316 | }
|
---|
317 |
|
---|
318 | return ("http://" . $before_hash, $hash_part, 1);
|
---|
319 |
|
---|
320 | } else {
|
---|
321 | # mailto, news, nntp, telnet, javascript or gopher link
|
---|
322 | return ($before_hash, "", 0);
|
---|
323 | }
|
---|
324 | }
|
---|
325 |
|
---|
326 | sub extract_metadata {
|
---|
327 | my $self = shift (@_);
|
---|
328 | my ($textref, $metadata, $doc_obj, $section) = @_;
|
---|
329 |
|
---|
330 | foreach my $field (split /,/, $self->{'metadata_fields'}) {
|
---|
331 |
|
---|
332 | # don't need to extract field if it was passed in from a previous
|
---|
333 | # (recursive) plugin
|
---|
334 | next if defined $metadata->{$field};
|
---|
335 |
|
---|
336 | # see if there's a <meta> tag for this field
|
---|
337 | if ($$textref =~ /<meta(.*?)(?:name|http-equiv)\s*=\s*\"?$field\"?([^>]*)/is) {
|
---|
338 | my $content = $1 . $2;
|
---|
339 | if ($content =~ /content\s*=\s*\"?(.*?)\"?/is) {
|
---|
340 | if (defined $1) {
|
---|
341 | my $value = $1;
|
---|
342 | $value =~ s/\s+/ /gs;
|
---|
343 | $doc_obj->add_metadata($section, $field, $value);
|
---|
344 | next;
|
---|
345 | }
|
---|
346 | }
|
---|
347 | }
|
---|
348 |
|
---|
349 | # special case for Title metadata - try <title> tags
|
---|
350 | # then first 100 characters of text
|
---|
351 |
|
---|
352 | if ($field =~ /^title$/i) {
|
---|
353 |
|
---|
354 | # see if there's a <title> tag
|
---|
355 | if ($$textref =~ /<title[^>]*>([^<]*)<\/title[^>]*>/is) {
|
---|
356 | if (defined $1) {
|
---|
357 | my $title = $1;
|
---|
358 | if ($title =~ /\w/) {
|
---|
359 | $title =~ s/\s+/ /gs;
|
---|
360 | $doc_obj->add_metadata ($section, $field, $title);
|
---|
361 | next;
|
---|
362 | }
|
---|
363 | }
|
---|
364 | }
|
---|
365 |
|
---|
366 | # if no title use first 100 characters
|
---|
367 | my $tmptext = $$textref;
|
---|
368 | $tmptext =~ s/<[^>]*>//g;
|
---|
369 | my $title = substr ($tmptext, 0, 100);
|
---|
370 | $title =~ s/\s+/ /gs;
|
---|
371 | $doc_obj->add_metadata ($section, $field, $title);
|
---|
372 | }
|
---|
373 | }
|
---|
374 | }
|
---|
375 |
|
---|
376 | # evaluate any "../" to next directory up
|
---|
377 | # evaluate any "./" as here
|
---|
378 | sub eval_dir_dots {
|
---|
379 | my $self = shift (@_);
|
---|
380 | my ($filename) = @_;
|
---|
381 |
|
---|
382 | my $dirsep_os = &util::get_os_dirsep();
|
---|
383 | my @dirsep = split(/$dirsep_os/,$filename);
|
---|
384 |
|
---|
385 | my @eval_dirs = ();
|
---|
386 | foreach my $d (@dirsep) {
|
---|
387 | if ($d eq "..") {
|
---|
388 | pop(@eval_dirs);
|
---|
389 |
|
---|
390 | } elsif ($d eq ".") {
|
---|
391 | # do nothing!
|
---|
392 |
|
---|
393 | } else {
|
---|
394 | push(@eval_dirs,$d);
|
---|
395 | }
|
---|
396 | }
|
---|
397 |
|
---|
398 | return &util::filename_cat(@eval_dirs);
|
---|
399 | }
|
---|
400 |
|
---|
401 | sub replace_usemap_links {
|
---|
402 | my $self = shift (@_);
|
---|
403 | my ($front, $link, $back) = @_;
|
---|
404 |
|
---|
405 | $link =~ s/^\.\///;
|
---|
406 | return $front . $link . $back;
|
---|
407 | }
|
---|
408 |
|
---|
409 | sub inc_filecount {
|
---|
410 | my $self = shift (@_);
|
---|
411 |
|
---|
412 | if ($self->{'file_num'} == 1000) {
|
---|
413 | $self->{'dir_num'} ++;
|
---|
414 | $self->{'file_num'} = 0;
|
---|
415 | } else {
|
---|
416 | $self->{'file_num'} ++;
|
---|
417 | }
|
---|
418 | }
|
---|
419 |
|
---|
420 | 1;
|
---|