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

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

fixed some of my earlier mistakes. sorry Stefan

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