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

Last change on this file since 1891 was 1891, checked in by paynter, 23 years ago

Named characters like é and ì are translated
to UTF8 internally.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 17.9 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 $$textref =~ s/(<img[^>]*?src\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/
161 $self->replace_images ($1, $2, $3, $base_dir, $file, $doc_obj, $cursection)/isge;
162
163 # add text to document object
164 $doc_obj->add_utf8_text($cursection, $$textref);
165
166 return 1;
167}
168
169
170
171
172sub replace_images {
173 my $self = shift (@_);
174 my ($front, $link, $back, $base_dir,
175 $file, $doc_obj, $section) = @_;
176
177 $link =~ s/\n/ /g;
178
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 $hash_part = "" if !defined $hash_part;
270 if (!defined $before_hash || $before_hash !~ /[\w\.\/]/) {
271 my $outhandle = $self->{'outhandle'};
272 print $outhandle "HTMLPlug: ERROR - badly formatted tag ignored ($link)\n"
273 if $self->{'verbosity'};
274 return ($link, "", 0);
275 }
276
277 if ($before_hash =~ s/^((?:http|ftp|file):\/\/)//i) {
278 my $type = $1;
279
280 if ($link =~ /^(http|ftp):/i) {
281 # Turn url (using /) into file name (possibly using \ on windows)
282 my @http_dir_split = split('/', $before_hash);
283 $before_hash = &util::filename_cat(@http_dir_split);
284 }
285
286 $before_hash = $self->eval_dir_dots($before_hash);
287
288 my $linkfilename = &util::filename_cat ($base_dir, $before_hash);
289
290 my $rl = 0;
291 $rl = 1 if (-e $linkfilename);
292
293 # make sure there's a slash on the end if it's a directory
294 if ($before_hash !~ /\/$/) {
295 $before_hash .= "/" if (-d $linkfilename);
296 }
297
298 return ($type . $before_hash, $hash_part, $rl);
299
300 } elsif ($link !~ /^(mailto|news|gopher|nntp|telnet|javascript):/i) {
301
302 if ($before_hash =~ s/^\///) {
303 # the first directory will be the domain name if w3mir was used
304 # to generate archives, otherwise we'll assume all files are
305 # from the same site and base_dir is the root
306 if ($self->{'w3mir'}) {
307 my @dirs = split /[\/\\]/, $file;
308 my $domname = shift (@dirs);
309 $before_hash = &util::filename_cat($domname, $before_hash);
310 $before_hash =~ s/\\/\//g; # for windows
311 }
312 else
313 {
314 # see if link shares directory with source document
315 # => turn into relative link if this is so!
316 my $before_hash_fix = &util::filename_cat("",$before_hash);
317 $before_hash_fix =~ s/^$base_dir(\\|\/)//;
318 $before_hash = $before_hash_fix;
319
320 }
321
322
323 } else {
324 # Turn relative file path into full path
325 my $dirname = &File::Basename::dirname($file);
326 $before_hash = &util::filename_cat($dirname, $before_hash);
327 $before_hash = $self->eval_dir_dots($before_hash);
328 }
329
330
331 my $linkfilename = &util::filename_cat ($base_dir, $before_hash);
332
333 # make sure there's a slash on the end if it's a directory
334 if ($before_hash !~ /\/$/) {
335 $before_hash .= "/" if (-d $linkfilename);
336 }
337
338 return ("http://" . $before_hash, $hash_part, 1);
339
340 } else {
341 # mailto, news, nntp, telnet, javascript or gopher link
342 return ($before_hash, "", 0);
343 }
344}
345
346sub extract_first_NNNN_characters {
347 my $self = shift (@_);
348 my ($textref, $doc_obj, $thissection) = @_;
349
350 foreach my $size (split /,/, $self->{'first'}) {
351 my $tmptext = $$textref;
352 $tmptext =~ s/.*<body[^>]*>//i;
353 $tmptext =~ s/$self->{'title_sub'}// if (defined $self->{'title_sub'});
354 $tmptext =~ s/<[^>]*>/ /g;
355 $tmptext =~ s/&nbsp;/ /g;
356 $tmptext =~ s/^\s+//;
357 $tmptext =~ s/\s+$//;
358 $tmptext =~ s/\s+/ /gs;
359 $tmptext = substr ($tmptext, 0, $size);
360 $tmptext =~ s/\s\S*$/&#8230;/;
361 $doc_obj->add_utf8_metadata ($thissection, "First$size", $tmptext);
362 }
363}
364
365sub extract_metadata {
366 my $self = shift (@_);
367 my ($textref, $metadata, $doc_obj, $section) = @_;
368 my $outhandle = $self->{'outhandle'};
369
370 # if we don't want metadata, we may as well not be here ...
371 return if (!defined $self->{'metadata_fields'});
372
373 # hunt for an author look in the metadata elements:
374 if (defined $self->{'hunt_creator_metadata'}) {
375 for my $name (split /,/, "AUTHOR,AUTHOR.EMAIL,CREATOR,DC.CREATOR,DC.CREATOR.CORPORATENAME") {
376 if ($$textref =~ /<meta(\s*?)(?:name|http-equiv)\s*=\s*\"?$name\"?([^>]*)/is) {
377 my $content = $1 . $2;
378 if ($content =~ /content\s*=\s*\"?(.*)\"?/is) {
379 if (defined $1) {
380 my $value = $1;
381 $value =~ s/\"$//;
382 $value =~ s/\s+/ /gs;
383 $doc_obj->add_utf8_metadata($section, "Creator", $value);
384 print $outhandle " extracted Creator metadata \"$value\"\n"
385 if ($self->{'verbosity'} > 2);
386 next;
387 }
388 }
389 }
390 }
391 }
392
393 foreach my $field (split /,/, $self->{'metadata_fields'}) {
394
395 # don't need to extract field if it was passed in from a previous
396 # (recursive) plugin
397 next if defined $metadata->{$field};
398
399 # see if there's a <meta> tag for this field
400 if ($$textref =~ /<meta(\s*?)(?:name|http-equiv)\s*=\s*\"?$field\"?([^>]*)/is) {
401 my $content = $1 . $2;
402 if ($content =~ /content\s*=\s*\"?(.*)\"?/is) {
403 if (defined $1) {
404 my $value = $1;
405 $value =~ s/\"$//;
406 $value =~ s/\s+/ /gs;
407 $value =~ s/\".*//gs;
408 $doc_obj->add_utf8_metadata($section, $field, $value);
409 print $outhandle " extracted \"$field\" metadata \"$value\"\n"
410 if ($self->{'verbosity'} > 2);
411 next;
412 }
413 }
414 }
415
416 # TITLE: extract the document title
417
418 if ($field =~ /^title$/i) {
419
420 # see if there's a <title> tag
421 if ($$textref =~ /<title[^>]*>([^<]*)<\/title[^>]*>/is) {
422 if (defined $1) {
423 my $title = $1;
424 if ($title =~ /\w/) {
425 $title =~ s/<[^>]*>/ /g;
426 $title =~ s/&nbsp;/ /g;
427 $title =~ s/\s+/ /gs;
428 $title =~ s/^\s+//;
429 $title =~ s/\s+$//;
430 $doc_obj->add_utf8_metadata ($section, $field, $title);
431 print $outhandle " extracted \"$field\" metadata \"$title\"\n"
432 if ($self->{'verbosity'} > 2);
433 next;
434 }
435 }
436 }
437
438 # if no title use first 100 characters
439 my $tmptext = $$textref;
440 $tmptext =~ s/$self->{'title_sub'}// if (defined $self->{'title_sub'});
441 $tmptext =~ s/<[^>]*>/ /g;
442 $tmptext =~ s/&nbsp;/ /g;
443 $tmptext =~ s/^\s+//;
444 $tmptext =~ s/\s+$//;
445 $tmptext =~ s/\s+/ /gs;
446 $tmptext = substr ($tmptext, 0, 100);
447 $tmptext =~ s/\s\S*$/.../;
448 $doc_obj->add_utf8_metadata ($section, $field, $tmptext);
449 print $outhandle " extracted \"$field\" metadata \"$tmptext\"\n"
450 if ($self->{'verbosity'} > 2);
451 next;
452 }
453
454
455 # tag: extract the text between the first <H1> and </H1> tags
456 if ($field =~ /^tag[a-z0-9]+$/i) {
457
458 my $tag = $field;
459 $tag =~ s/^tag//i;
460 my $tmptext = $$textref;
461 $tmptext =~ s/\s+/ /gs;
462 if ($tmptext =~ /<$tag[^>]*>/i) {
463 foreach my $word ($tmptext =~ m/<$tag[^>]*>(.*?)<\/$tag[^>]*>/g) {
464 $word =~ s/&nbsp;/ /g;
465 $word =~ s/<[^>]*>/ /g;
466 $word =~ s/^\s+//;
467 $word =~ s/\s+$//;
468 $word =~ s/\s+/ /gs;
469 if ($word ne "") {
470 $doc_obj->add_utf8_metadata ($section, $tag, $word);
471 print $outhandle " extracted \"$tag\" metadata \"$word\"\n"
472 if ($self->{'verbosity'} > 2);
473 }
474 }
475 }
476 next;
477 }
478 }
479}
480
481
482# evaluate any "../" to next directory up
483# evaluate any "./" as here
484sub eval_dir_dots {
485 my $self = shift (@_);
486 my ($filename) = @_;
487
488 my $dirsep_os = &util::get_os_dirsep();
489 my @dirsep = split(/$dirsep_os/,$filename);
490
491 my @eval_dirs = ();
492 foreach my $d (@dirsep) {
493 if ($d eq "..") {
494 pop(@eval_dirs);
495
496 } elsif ($d eq ".") {
497 # do nothing!
498
499 } else {
500 push(@eval_dirs,$d);
501 }
502 }
503
504 return &util::filename_cat(@eval_dirs);
505}
506
507sub replace_usemap_links {
508 my $self = shift (@_);
509 my ($front, $link, $back) = @_;
510
511 $link =~ s/^\.\///;
512 return $front . $link . $back;
513}
514
515sub inc_filecount {
516 my $self = shift (@_);
517
518 if ($self->{'file_num'} == 1000) {
519 $self->{'dir_num'} ++;
520 $self->{'file_num'} = 0;
521 } else {
522 $self->{'file_num'} ++;
523 }
524}
525
526
527# Extend the BasPlug read_file so that strings like &eacute; are
528# converted to UTF8 internally.
529#
530# We don't convert &lt; or &gt; or &amp; or &quot; in case
531# they interfere with the GML files
532
533sub read_file {
534 my ($self, $filename, $encoding, $textref) = @_;
535
536 &BasPlug::read_file($self, $filename, $encoding, $textref);
537
538 # Convert things like &eacute; to their UTF8 equivalents
539 $$textref =~ s/&(lt|gt|amp|quot);/&z$1;/go;
540 $$textref =~ s/&([^;]+);/&unicode::ascii2utf8(\&ghtml::getcharequiv($1,1))/gseo;
541 $$textref =~ s/&z(lt|gt|amp|quot);/&$1;/go;
542}
543
5441;
Note: See TracBrowser for help on using the repository browser.