source: main/tags/2.30/gsdl/perllib/plugins/HTMLPlug.pm@ 23841

Last change on this file since 23841 was 1699, checked in by say1, 24 years ago

fixed the bug in HTML plug which broke images for Dave

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