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

Last change on this file since 1929 was 1929, checked in by dg5, 23 years ago

Modified: ConvertToPlug and HTMLPlug to handle files in binary mode to avoid

the problem of '\n' under Windows.

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