source: trunk/gsdl/perllib/plugins/HTMLPlug.pm@ 1312

Last change on this file since 1312 was 1312, checked in by sjboddie, 24 years ago

fixed a bug in the HTML plugin that showed up under windows

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 14.3 KB
Line 
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
38package HTMLPlug;
39
40use BasPlug;
41use ghtml;
42use util;
43use parsargv;
44
45sub BEGIN {
46 @ISA = ('BasPlug');
47}
48
49use strict;
50
51sub print_usage {
52 print STDERR "\n usage: plugin HTMLPlug [options]\n\n";
53 print STDERR " options:\n";
54 print STDERR " -nolinks Don't make any attempt to trap links (setting this flag may\n";
55 print STDERR " improve speed of building/importing but any relative links within\n";
56 print STDERR " documents will be broken).\n";
57 print STDERR " -keep_head Don't remove headers from html files.\n";
58 print STDERR " -no_metadata Don't attempt to extract any metadata from files.\n";
59 print STDERR " -metadata_fields Comma separated list of metadata fields to attempt to extract.\n";
60 print STDERR " Defaults to 'Title'.\n";
61 print STDERR " Use `first200` to get the first 200 characters of the body.\n";
62 print STDERR " Use `H1` to get the text inside the first <H1> and </H1> tags in the text.\n";
63 print STDERR " -w3mir Set if w3mir was used to generate input file structure.\n";
64 print STDERR " -assoc_files Perl regular expression of file extensions to associate with\n";
65 print STDERR " html documents. Defaults to '(?i)\.(jpe?g|gif|png|css|pdf)\$'\n";
66 print STDERR " -rename_assoc_files Renames files associated with documents (e.g. images). Also\n";
67 print STDERR " creates much shallower directory structure (useful when creating\n";
68 print STDERR " collections to go on cd-rom).\n\n";
69}
70
71sub new {
72 my $class = shift (@_);
73 my $self = new BasPlug ("HTMLPlug", @_);
74
75 if (!parsargv::parse(\@_,
76 q^nolinks^, \$self->{'nolinks'},
77 q^keep_head^, \$self->{'keep_head'},
78 q^no_metadata^, \$self->{'no_metadata'},
79 q^metadata_fields/.*/Title^, \$self->{'metadata_fields'},
80 q^w3mir^, \$self->{'w3mir'},
81 q^assoc_files/.*/(?i)\.(jpe?g|gif|png|css|pdf)$^, \$self->{'assoc_files'},
82 q^rename_assoc_files^, \$self->{'rename_assoc_files'},
83 "allow_extra_options")) {
84
85 print STDERR "\nIncorrect options passed to HTMLPlug, check your collect.cfg configuration file\n";
86 &print_usage();
87 die "\n";
88 }
89
90 $self->{'aux_files'} = {};
91 $self->{'dir_num'} = 0;
92 $self->{'file_num'} = 0;
93
94 return bless $self, $class;
95}
96
97
98sub get_default_block_exp {
99 my $self = shift (@_);
100
101 return q^(?i)\.(gif|jpe?g|png|pdf|rtf|css)$^;
102}
103
104sub get_default_process_exp {
105 my $self = shift (@_);
106
107 return q^(?i)\.html?$^;
108}
109
110
111# do plugin specific processing of doc_obj
112sub process {
113 my $self = shift (@_);
114 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
115
116 print STDERR "HTMLPlug: processing $file\n"
117 if $self->{'verbosity'} > 1;
118
119 my $cursection = $doc_obj->get_top_section();
120
121 $self->extract_metadata ($textref, $metadata, $doc_obj, $cursection)
122 unless $self->{'no_metadata'};
123
124 # Store URL for page as metadata - this can be used for an
125 # altavista style search interface. The URL won't be valid
126 # unless the file structure contains the domain name (i.e.
127 # like when w3mir is used to download a website).
128 my $web_url = "http://$file";
129 $web_url =~ s/\\/\//g; # for windows
130 $doc_obj->add_utf8_metadata($cursection, "URL", $web_url);
131
132 # remove header and footer
133 if (!$self->{'keep_head'}) {
134 $$textref =~ s/^.*?<body[^>]*>//is;
135 $$textref =~ s/(<\/body[^>]*>|<\/html[^>]*>)//isg;
136 }
137
138 # trap links
139 if (!$self->{'nolinks'}) {
140
141 # usemap="./#index" not handled correctly => change to "#index"
142 $$textref =~ s/(<img[^>]*?usemap\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/
143 $self->replace_usemap_links($1, $2, $3)/isge;
144
145 $$textref =~ s/(<(?:a|area|frame|link)\s+[^>]*?\s*(?:href|src)\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/
146 $self->replace_href_links ($1, $2, $3, $base_dir, $file, $doc_obj, $cursection)/isge;
147 }
148
149 # trap images
150 $$textref =~ s/(<img[^>]*? src\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/
151 $self->replace_images ($1, $2, $3, $base_dir, $file, $doc_obj, $cursection)/isge;
152
153 # add text to document object
154 $doc_obj->add_utf8_text($cursection, "<pre>\n$$textref\n</pre>");
155
156 return 1;
157}
158
159sub replace_images {
160 my $self = shift (@_);
161 my ($front, $link, $back, $base_dir,
162 $file, $doc_obj, $section) = @_;
163
164 $link =~ s/\n/ /g;
165
166 my ($href, $hash_part, $rl) = $self->format_link ($link, $base_dir, $file);
167 return $front . $self->add_file ($href, $rl, $hash_part, $base_dir, $doc_obj, $section) . $back;
168}
169
170sub replace_href_links {
171 my $self = shift (@_);
172 my ($front, $link, $back, $base_dir, $file, $doc_obj, $section) = @_;
173
174 # attempt to sort out targets - frames are not handled
175 # well in this plugin and some cases will screw things
176 # up - e.g. the _parent target (so we'll just remove
177 # them all ;-)
178 $front =~ s/(target=\"?)_top(\"?)/$1_gsdltop_$2/is;
179 $back =~ s/(target=\"?)_top(\"?)/$1_gsdltop_$2/is;
180 $front =~ s/target=\"?_parent\"?//is;
181 $back =~ s/target=\"?_parent\"?//is;
182
183 return $front . $link . $back if $link =~ /^\#/s;
184 $link =~ s/\n/ /g;
185
186 my ($href, $hash_part, $rl) = $self->format_link ($link, $base_dir, $file);
187 # href may use '\'s where '/'s should be on Windows
188 $href =~ s/\\/\//g;
189
190 my ($filename) = $href =~ /^(?:.*?):(?:\/\/)?(.*)/;
191
192 ##### leave all these links alone (they won't be picked up by intermediate
193 ##### pages). I think that's safest when dealing with frames, targets etc.
194 ##### (at least until I think of a better way to do it). Problems occur with
195 ##### mailto links from within small frames, the intermediate page is displayed
196 ##### within that frame and can't be seen. There is still potential for this to
197 ##### happen even with html pages - the solution seems to be to somehow tell
198 ##### the browser from the server side to display the page being sent (i.e.
199 ##### the intermediate page) in the top level window - I'm not sure if that's
200 ##### possible - the following line should probably be deleted if that can be done
201 return $front . $link . $back if $href =~ /^(mailto|news|gopher|nntp|telnet|javascript):/is;
202
203
204 if (($rl == 0) || ($filename =~ /$self->{'process_exp'}/) ||
205 ($href =~ /\/$/) || ($href =~ /^(mailto|news|gopher|nntp|telnet|javascript):/i)) {
206 &ghtml::urlsafe ($href);
207 return $front . "_httpextlink_&rl=" . $rl . "&href=" . $href . $hash_part . $back;
208
209 } else {
210 # link is to some other type of file (image, pdf etc.) so we'll
211 # need to associate that file
212 return $front . $self->add_file ($href, $rl, $hash_part, $base_dir, $doc_obj, $section) . $back;
213 }
214}
215
216sub add_file {
217 my $self = shift (@_);
218 my ($href, $rl, $hash_part, $base_dir, $doc_obj, $section) = @_;
219 my ($newname);
220
221 my $filename = $href;
222 $filename =~ s/^[^:]*:\/\///;
223 $filename = &util::filename_cat ($base_dir, $filename);
224 my ($ext) = $filename =~ /(\.[^\.]*)$/;
225
226 if ((!defined $ext) || ($ext !~ /$self->{'assoc_files'}/)) {
227 return "_httpextlink_&rl=" . $rl . "&href=" . $href . $hash_part;
228 }
229
230 if ($self->{'rename_assoc_files'}) {
231 if (defined $self->{'aux_files'}->{$href}) {
232 $newname = $self->{'aux_files'}->{$href}->{'dir_num'} . "/" .
233 $self->{'aux_files'}->{$href}->{'file_num'} . $ext;
234 } else {
235 $newname = $self->{'dir_num'} . "/" . $self->{'file_num'} . $ext;
236 $self->{'aux_files'}->{$href} = {'dir_num' => $self->{'dir_num'}, 'file_num' => $self->{'file_num'}};
237 $self->inc_filecount ();
238 }
239 $doc_obj->associate_file($filename, $newname, undef, $section);
240 return "_httpcollimg_/$newname";
241
242 } else {
243 ($newname) = $filename =~ /([^\/\\]*)$/;
244 $doc_obj->associate_file($filename, $newname, undef, $section);
245 return "_httpdocimg_/$newname";
246 }
247}
248
249
250sub format_link {
251 my $self = shift (@_);
252 my ($link, $base_dir, $file) = @_;
253
254 my ($before_hash, $hash_part) = $link =~ /^([^\#]*)(\#?.*)$/;
255 $hash_part = "" if !defined $hash_part;
256 if (!defined $before_hash || $before_hash !~ /[\w\.\/]/) {
257 print STDERR "HTMLPlug: ERROR - badly formatted tag ignored ($link)\n"
258 if $self->{'verbosity'};
259 return ($link, "", 0);
260 }
261
262 if ($before_hash =~ s/^((?:http|ftp|file):\/\/)//i) {
263 my $type = $1;
264
265 if ($link =~ /^(http|ftp):/i) {
266 # Turn url (using /) into file name (possibly using \ on windows)
267 my @http_dir_split = split('/', $before_hash);
268 $before_hash = &util::filename_cat(@http_dir_split);
269 }
270
271 $before_hash = $self->eval_dir_dots($before_hash);
272
273 my $linkfilename = &util::filename_cat ($base_dir, $before_hash);
274
275 my $rl = 0;
276 $rl = 1 if (-e $linkfilename);
277
278 # make sure there's a slash on the end if it's a directory
279 if ($before_hash !~ /\/$/) {
280 $before_hash .= "/" if (-d $linkfilename);
281 }
282
283 return ($type . $before_hash, $hash_part, $rl);
284
285 } elsif ($link !~ /^(mailto|news|gopher|nntp|telnet|javascript):/i) {
286
287 if ($before_hash =~ s/^\///) {
288 # the first directory will be the domain name if w3mir was used
289 # to generate archives, otherwise we'll assume all files are
290 # from the same site and base_dir is the root
291 if ($self->{'w3mir'}) {
292 my @dirs = split /[\/\\]/, $file;
293 my $domname = shift (@dirs);
294 $before_hash = &util::filename_cat($domname, $before_hash);
295 $before_hash =~ s/\\/\//g; # for windows
296 }
297
298 } else {
299 # Turn relative file path into full path
300 my $dirname = &File::Basename::dirname($file);
301 $before_hash = &util::filename_cat($dirname, $before_hash);
302 $before_hash = $self->eval_dir_dots($before_hash);
303 }
304
305 # make sure there's a slash on the end if it's a directory
306 my $linkfilename = &util::filename_cat ($base_dir, $before_hash);
307 if ($before_hash !~ /\/$/) {
308 $before_hash .= "/" if (-d $linkfilename);
309 }
310
311 return ("http://" . $before_hash, $hash_part, 1);
312
313 } else {
314 # mailto, news, nntp, telnet, javascript or gopher link
315 return ($before_hash, "", 0);
316 }
317}
318
319sub extract_metadata {
320 my $self = shift (@_);
321 my ($textref, $metadata, $doc_obj, $section) = @_;
322
323 foreach my $field (split /,/, $self->{'metadata_fields'}) {
324
325 # don't need to extract field if it was passed in from a previous
326 # (recursive) plugin
327 next if defined $metadata->{$field};
328
329 # see if there's a <meta> tag for this field
330 if ($$textref =~ /<meta(.*?)(?:name|http-equiv)\s*=\s*\"?$field\"?([^>]*)/is) {
331 my $content = $1 . $2;
332 if ($content =~ /content\s*=\s*\"?(.*?)\"?/is) {
333 if (defined $1) {
334 my $value = $1;
335 $value =~ s/\s+/ /gs;
336 $doc_obj->add_utf8_metadata($section, $field, $value);
337 next;
338 }
339 }
340 }
341
342 # TITLE: extract the document title
343
344 if ($field =~ /^title$/i) {
345
346 # see if there's a <title> tag
347 if ($$textref =~ /<title[^>]*>([^<]*)<\/title[^>]*>/is) {
348 if (defined $1) {
349 my $title = $1;
350 if ($title =~ /\w/) {
351 $title =~ s/\s+/ /gs;
352 $title =~ s/^\s+//;
353 $title =~ s/\s+$//;
354 $doc_obj->add_utf8_metadata ($section, $field, $title);
355 next;
356 }
357 }
358 }
359
360 # if no title use first 100 characters
361 my $tmptext = $$textref;
362 $tmptext =~ s/\s+/ /gs;
363 $tmptext =~ s/<[^>]*>//g;
364 $tmptext = substr ($tmptext, 0, 100);
365 $tmptext =~ s/^\s+//;
366 $tmptext =~ s/\s+$//;
367 $tmptext =~ s/\s\S*$/.../;
368 $doc_obj->add_utf8_metadata ($section, $field, $tmptext);
369 next;
370 }
371
372 # FIRST200: extract the first 200 characters as metadata
373
374 if ($field =~ /^first200$/i) {
375 my $tmptext = $$textref;
376 $tmptext =~ s/\s+/ /gs;
377 $tmptext =~ s/.*<body[^>]*>//i;
378 $tmptext =~ s/<[^>]*>//g;
379 $tmptext = substr ($tmptext, 0, 200);
380 $tmptext =~ s/^\s+//;
381 $tmptext =~ s/\s+$//;
382 $tmptext =~ s/\s\S*$/.../;
383 $doc_obj->add_utf8_metadata ($section, $field, $tmptext);
384 next;
385 }
386
387 # H1: extract the text between the first <H1> and </H1> tags
388 if ($field =~ /^H1$/i) {
389 my $tmptext = $$textref;
390 $tmptext =~ s/\s+/ /gs;
391 if ($tmptext =~ /<H1[^>]*>/i) {
392 $tmptext =~ s/.*<H1[^>]*>//i;
393 $tmptext =~ s/<\/H1[^>]*>.*//i;
394 $tmptext =~ s/^\s+//;
395 $tmptext =~ s/\s+$//;
396 $doc_obj->add_utf8_metadata ($section, $field, $tmptext);
397 }
398 next;
399 }
400 }
401}
402
403
404# evaluate any "../" to next directory up
405# evaluate any "./" as here
406sub eval_dir_dots {
407 my $self = shift (@_);
408 my ($filename) = @_;
409
410 my $dirsep_os = &util::get_os_dirsep();
411 my @dirsep = split(/$dirsep_os/,$filename);
412
413 my @eval_dirs = ();
414 foreach my $d (@dirsep) {
415 if ($d eq "..") {
416 pop(@eval_dirs);
417
418 } elsif ($d eq ".") {
419 # do nothing!
420
421 } else {
422 push(@eval_dirs,$d);
423 }
424 }
425
426 return &util::filename_cat(@eval_dirs);
427}
428
429sub replace_usemap_links {
430 my $self = shift (@_);
431 my ($front, $link, $back) = @_;
432
433 $link =~ s/^\.\///;
434 return $front . $link . $back;
435}
436
437sub inc_filecount {
438 my $self = shift (@_);
439
440 if ($self->{'file_num'} == 1000) {
441 $self->{'dir_num'} ++;
442 $self->{'file_num'} = 0;
443 } else {
444 $self->{'file_num'} ++;
445 }
446}
447
4481;
Note: See TracBrowser for help on using the repository browser.