########################################################################### # # HTMLPlug.pm -- basic html plugin # # A component of the Greenstone digital library software # from the New Zealand Digital Library Project at the # University of Waikato, New Zealand. # # Copyright (C) 1999 New Zealand Digital Library Project # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # ########################################################################### # # Note that this plugin handles frames only in a very simple way # i.e. each frame is treated as a separate document. This means # search results will contain links to individual frames rather # than linking to the top level frameset. # There may also be some problems caused by the _parent target # (it's removed by this plugin) # To use frames properly you'll need to use the WebPlug plugin. # package HTMLPlug; use BasPlug; use html; use util; use parsargv; sub BEGIN { @ISA = ('BasPlug'); } use strict; sub print_usage { print STDERR "\nIncorrect options passed to HTMLPlug, check your collect.cfg configuration file\n"; print STDERR "\n usage: plugin HTMLPlug [options]\n\n"; print STDERR " options:\n"; print STDERR " -process_exp A perl regular expression to match against filenames.\n"; print STDERR " Matching filenames will be processed by this plugin.\n"; print STDERR " Defaults to '(?i)\.html?\$' i.e. all documents ending in\n"; print STDERR " .htm or .html (case-insensitive).\n"; print STDERR " -nolinks Don't make any attempt to trap links (setting this flag may\n"; print STDERR " improve speed of building/importing but any relative links within\n"; print STDERR " documents will be broken).\n"; print STDERR " -block_exp Files matching this regular expression will be blocked from\n"; print STDERR " being passed to any further plugins in the list. By default\n"; print STDERR " HTMLPlug blocks any files with .gif, .jpg, .jpeg, .png, .pdf,\n"; print STDERR " .rtf or .css file extensions.\n"; print STDERR " -keep_head Don't remove headers from html files.\n"; print STDERR " -no_metadata Don't attempt to extract any metadata from files.\n"; print STDERR " -metadata_fields Comma separated list of metadata fields to attempt to extract.\n"; print STDERR " Defaults to 'Title'\n"; print STDERR " -w3mir Set if w3mir was used to generate input file structure.\n"; print STDERR " w3mir \n"; print STDERR " -assoc_files Perl regular expression of file extensions to associate with\n"; print STDERR " html documents. Defaults to '(?i)\.(jpe?g|gif|png|css|pdf)$'\n"; print STDERR " -rename_assoc_files Renames files associated with documents (e.g. images). Also\n"; print STDERR " creates much shallower directory structure (useful when creating\n"; print STDERR " collections to go on cd-rom).\n\n"; } sub new { my $class = shift (@_); my $self = new BasPlug (); if (!parsargv::parse(\@_, q^process_exp/.*/(?i)\.html?$^, \$self->{'process_exp'}, q^nolinks^, \$self->{'nolinks'}, q^block_exp/.*/(?i)\.(gif|jpe?g|png|pdf|rtf|css)$^, \$self->{'block_exp'}, q^keep_head^, \$self->{'keep_head'}, q^no_metadata^, \$self->{'no_metadata'}, q^metadata_fields/.*/Title^, \$self->{'metadata_fields'}, q^w3mir^, \$self->{'w3mir'}, q^assoc_files/.*/(?i)\.(jpe?g|gif|png|css|pdf)$^, \$self->{'assoc_files'}, q^rename_assoc_files^, \$self->{'rename_assoc_files'})) { &print_usage(); die "\n"; } $self->{'aux_files'} = {}; $self->{'dir_num'} = 0; $self->{'file_num'} = 0; return bless $self, $class; } sub is_recursive { my $self = shift (@_); return 0; # this is not a recursive plugin } # return number of files processed, undef if can't process # Note that $base_dir might be "" and that $file might # include directories sub read { my $self = shift (@_); my ($pluginfo, $base_dir, $file, $metadata, $processor) = @_; my $filename = &util::filename_cat($base_dir, $file); return 0 if $filename =~ /$self->{'block_exp'}/; if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) { return undef; } $file =~ s/^[\/\\]+//; $self->{'verbosity'} = $processor->{'verbosity'}; print STDERR "HTMLPlug: processing $file\n" if $self->{'verbosity'} > 1; # create a new document my $doc_obj = new doc ($file, "indexed_doc"); my $cursection = $doc_obj->get_top_section(); # read in HTML file open (FILE, $filename) || die "HTMLPlug::read - can't open $filename\n"; undef $/; my $text = ; $/ = "\n"; close FILE; if (!defined $text || $text !~ /\w/) { print STDERR "HTMLPlug: ERROR: $file contains no text\n" if $self->{'verbosity'}; return 0; } $self->extra_metadata ($doc_obj, $cursection, $metadata); $self->extract_metadata (\$text, $metadata, $doc_obj, $cursection) unless $self->{'no_metadata'}; # Store URL for page as metadata - this can be used for an # altavista style search interface. The URL won't be valid # unless the file structure contains the domain name (i.e. # like when w3mir is used to download a website). my $web_url = "http://$file"; $web_url =~ s/\\/\//g; # for windows $doc_obj->add_metadata($cursection, "URL", $web_url); # remove header and footer if (!$self->{'keep_head'}) { $text =~ s/^.*?]*>//is; $text =~ s/(<\/body[^>]*>|<\/html[^>]*>)//isg; } # trap links if (!$self->{'nolinks'}) { # usemap="./#index" not handled correctly => change to "#index" $text =~ s/(]*?usemap\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/ $self->replace_usemap_links($1, $2, $3)/isge; $text =~ s/(<(?:a|area|frame|link)\s+[^>]*?(?:href|src)\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/ $self->replace_href_links ($1, $2, $3, $base_dir, $file, $doc_obj, $cursection)/isge; } # trap images $text =~ s/(]*?src\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/ $self->replace_images ($1, $2, $3, $base_dir, $file, $doc_obj, $cursection)/isge; $doc_obj->add_text ($cursection, $text); # add an OID $doc_obj->set_OID(); # process the document $processor->process($doc_obj); return 1; # processed the file } sub replace_images { my $self = shift (@_); my ($front, $link, $back, $base_dir, $file, $doc_obj, $section) = @_; $link =~ s/\n/ /g; my ($href, $hash_part, $rl) = $self->format_link ($link, $base_dir, $file); return $front . $self->add_file ($href, $rl, $hash_part, $base_dir, $doc_obj, $section) . $back; } sub replace_href_links { my $self = shift (@_); my ($front, $link, $back, $base_dir, $file, $doc_obj, $section) = @_; # attempt to sort out targets - frames are not handled # well in this plugin and some cases will screw things # up - e.g. the _parent target (so we'll just remove # them all ;-) $front =~ s/(target=\"?)_top(\"?)/$1_gsdltop_$2/is; $back =~ s/(target=\"?)_top(\"?)/$1_gsdltop_$2/is; $front =~ s/target=\"?_parent\"?//is; $back =~ s/target=\"?_parent\"?//is; return $front . $link . $back if $link =~ /^\#/s; $link =~ s/\n/ /g; my ($href, $hash_part, $rl) = $self->format_link ($link, $base_dir, $file); my ($filename) = $href =~ /^(?:.*?):(?:\/\/)?(.*)/; ##### leave all these links alone (they won't be picked up by intermediate ##### pages). I think that's safest when dealing with frames, targets etc. ##### (at least until I think of a better way to do it). Problems occur with ##### mailto links from within small frames, the intermediate page is displayed ##### within that frame and can't be seen. There is still potential for this to ##### happen even with html pages - the solution seems to be to somehow tell ##### the browser from the server side to display the page being sent (i.e. ##### the intermediate page) in the top level window - I'm not sure if that's ##### possible - the following line should probably be deleted if that can be done return $front . $link . $back if $href =~ /^(mailto|news|gopher|nntp|telnet|javascript):/is; if (($rl == 0) || ($filename =~ /$self->{'process_exp'}/) || ($href =~ /\/$/) || ($href =~ /^(mailto|news|gopher|nntp|telnet|javascript):/i)) { &html::urlsafe ($href); return $front . "_httpextlink_&rl=" . $rl . "&href=" . $href . $hash_part . $back; } else { # link is to some other type of file (image, pdf etc.) so we'll # need to associate that file return $front . $self->add_file ($href, $rl, $hash_part, $base_dir, $doc_obj, $section) . $back; } } sub add_file { my $self = shift (@_); my ($href, $rl, $hash_part, $base_dir, $doc_obj, $section) = @_; my ($newname); my $filename = $href; $filename =~ s/^[^:]*:\/\///; $filename = &util::filename_cat ($base_dir, $filename); my ($ext) = $filename =~ /(\.[^\.]*)$/; if ((!defined $ext) || ($ext !~ /$self->{'assoc_files'}/)) { return "_httpextlink_&rl=" . $rl . "&href=" . $href . $hash_part; } if ($self->{'rename_assoc_files'}) { if (defined $self->{'aux_files'}->{$href}) { $newname = $self->{'aux_files'}->{$href}->{'dir_num'} . "/" . $self->{'aux_files'}->{$href}->{'file_num'} . $ext; } else { $newname = $self->{'dir_num'} . "/" . $self->{'file_num'} . $ext; $self->{'aux_files'}->{$href} = {'dir_num' => $self->{'dir_num'}, 'file_num' => $self->{'file_num'}}; $self->inc_filecount (); } $doc_obj->associate_file($filename, $newname, undef, $section); return "_httpcollimg_/$newname"; } else { ($newname) = $filename =~ /([^\/\\]*)$/; $doc_obj->associate_file($filename, $newname, undef, $section); return "_httpcollimg_/_thisOID_/$newname"; } } sub format_link { my $self = shift (@_); my ($link, $base_dir, $file) = @_; my ($before_hash, $hash_part) = $link =~ /^([^\#]*)(\#?.*)$/; $hash_part = "" if !defined $hash_part; if (!defined $before_hash || $before_hash !~ /[\w\.\/]/) { print STDERR "HTMLPlug: ERROR - badly formatted tag ignored ($link)\n" if $self->{'verbosity'}; return ($link, "", 0); } if ($before_hash =~ s/^((?:http|ftp|file):\/\/)//i) { my $type = $1; if ($link =~ /^(http|ftp):/i) { # Turn url (using /) into file name (possibly using \ on windows) my @http_dir_split = split('/', $before_hash); $before_hash = &util::filename_cat(@http_dir_split); } $before_hash = $self->eval_dir_dots($before_hash); my $linkfilename = &util::filename_cat ($base_dir, $before_hash); my $rl = 0; $rl = 1 if (-e $linkfilename); # make sure there's a slash on the end if it's a directory if ($before_hash !~ /\/$/) { $before_hash .= "/" if (-d $linkfilename); } return ($type . $before_hash, $hash_part, $rl); } elsif ($link !~ /^(mailto|news|gopher|nntp|telnet|javascript):/i) { if ($before_hash =~ s/^\///) { # the first directory will be the domain name if w3mir was used # to generate archives, otherwise we'll assume all files are # from the same site and base_dir is the root if ($self->{'w3mir'}) { my @dirs = split /[\/\\]/, $file; my $domname = shift (@dirs); $before_hash = &util::filename_cat($domname, $before_hash); $before_hash =~ s/\\/\//g; # for windows } } else { # Turn relative file path into full path my $dirname = &File::Basename::dirname($file); $before_hash = &util::filename_cat($dirname, $before_hash); $before_hash = $self->eval_dir_dots($before_hash); } # make sure there's a slash on the end if it's a directory my $linkfilename = &util::filename_cat ($base_dir, $before_hash); if ($before_hash !~ /\/$/) { $before_hash .= "/" if (-d $linkfilename); } return ("http://" . $before_hash, $hash_part, 1); } else { # mailto, news, nntp, telnet, javascript or gopher link return ($before_hash, "", 0); } } sub extract_metadata { my $self = shift (@_); my ($textref, $metadata, $doc_obj, $section) = @_; foreach my $field (split /,/, $self->{'metadata_fields'}) { # don't need to extract field if it was passed in from a previous # (recursive) plugin next if defined $metadata->{$field}; # see if there's a tag for this field if ($$textref =~ /]*)/is) { my $content = $1 . $2; if ($content =~ /content\s*=\s*\"?(.*?)\"?/is) { if (defined $1) { my $value = $1; $value =~ s/\s+/ /gs; $doc_obj->add_metadata($section, $field, $value); next; } } } # special case for Title metadata - try tags # then first 100 characters of text if ($field =~ /^title$/i) { # see if there's a <title> tag if ($$textref =~ /<title[^>]*>([^<]*)<\/title[^>]*>/is) { if (defined $1) { my $title = $1; if ($title =~ /\w/) { $title =~ s/\s+/ /gs; $doc_obj->add_metadata ($section, $field, $title); next; } } } # if no title use first 100 characters my $tmptext = $$textref; $tmptext =~ s/<[^>]*>//g; my $title = substr ($tmptext, 0, 100); $title =~ s/\s+/ /gs; $doc_obj->add_metadata ($section, $field, $title); } } } # evaluate any "../" to next directory up # evaluate any "./" as here sub eval_dir_dots { my $self = shift (@_); my ($filename) = @_; my $dirsep_os = &util::get_os_dirsep(); my @dirsep = split(/$dirsep_os/,$filename); my @eval_dirs = (); foreach my $d (@dirsep) { if ($d eq "..") { pop(@eval_dirs); } elsif ($d eq ".") { # do nothing! } else { push(@eval_dirs,$d); } } return &util::filename_cat(@eval_dirs); } sub replace_usemap_links { my $self = shift (@_); my ($front, $link, $back) = @_; $link =~ s/^\.\///; return $front . $link . $back; } sub inc_filecount { my $self = shift (@_); if ($self->{'file_num'} == 1000) { $self->{'dir_num'} ++; $self->{'file_num'} = 0; } else { $self->{'file_num'} ++; } } 1;