Index: trunk/gsdl/perllib/plugins/HTMLPlug.pm
===================================================================
--- trunk/gsdl/perllib/plugins/HTMLPlug.pm	(revision 807)
+++ trunk/gsdl/perllib/plugins/HTMLPlug.pm	(revision 808)
@@ -2,4 +2,5 @@
 #
 # HTMLPlug.pm -- basic html plugin
+#
 # A component of the Greenstone digital library software
 # from the New Zealand Digital Library Project at the 
@@ -24,13 +25,21 @@
 ###########################################################################
 
+# 
+# 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 sorttools;
 use html;
 use util;
-
-use File::Basename;
+use parsargv;
 
 sub BEGIN {
@@ -38,10 +47,46 @@
 }
 
+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 "                          or .rtf 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\n";
+}
+
 sub new {
-    my ($class) = @_;
+    my $class = shift (@_);
     $self = new BasPlug ();
 
-    $self->{'f2d_mapping'} = undef;
-    $self->{'f2d_partitioned_dirs'} = [];
+    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)$^, \$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'})) {
+	&print_usage();
+	die "\n";
+    }
+
+    $self->{'aux_files'} = {};
+    $self->{'dir_num'} = 0;
+    $self->{'file_num'} = 0;
 
     return bless $self, $class;
@@ -53,139 +98,4 @@
     return 0; # this is not a recursive plugin
 }
-
-#--
-# Convert filename to lookup filename
-#--
-sub f2d_lookup_filename
-{
-    my ($self,$filename) = @_;
-
-    my $coldir = $ENV{'GSDLCOLLECTDIR'};
-    my $dirsep_re = &util::get_re_dirsep();
-    my $supress_dirs = "(import|building)";
-
-    my @part_dirs = @{$self->{'f2d_partitioned_dirs'}};
-
-    if (scalar(@part_dirs)>0)
-    {
-	my $joined_part_dirs = join('|',@part_dirs);
-	$supress_dirs .= "($dirsep_re)($joined_part_dirs)";
-    }
-
-    my $lookup_filename = $filename;
-    $lookup_filename =~ s/^$coldir($dirsep_re)//;
-    $lookup_filename =~ s/^$supress_dirs($dirsep_re)//;
-
-    return $lookup_filename;
-}
-
-
-#--
-# file to document mapping
-#--
-
-sub rec_f2d_mapping
-{
-    my ($self,$dirname) = @_;
-
-    # read all the files in the directory
-    if (!opendir(DIR, $dirname))
-    {
-	print STDERR "HTMLPlug: WARNING - couldn't read directory $dirname";
-	print STDERR " during file to doc mapping\n";
-	return;
-    }
-    my @dir = readdir (DIR);
-    closedir (DIR);
-
-    # process each file
-    my $subfile;
-    foreach $subfile (@dir) 
-    {
-	if ($subfile !~ /^\.\.?$/) 
-	{
-	    my $filename = &util::filename_cat($dirname, $subfile);
-
-	    if (-d $filename)
-	    {
-		my $dirname = $filename;
-		$self->rec_f2d_mapping($dirname);
-	    }
-	    else
-	    {
-		if ($subfile =~ m/\.(html?(\.gz)?)$/i && (-e $filename))
-		{
-		    # add mapping
-
-		    print STDERR "HTMLPlug: Precalculating OID for $subfile\n"
-			if (defined($self->{'verbosity'}));
-
-		    my $oid = doc::_calc_OID(undef,$filename);
-		    my $lookup_filename 
-			= $self->f2d_lookup_filename($filename);
-
-		    $self->{'f2d_mapping'}->{$lookup_filename} = $oid;
-
-		    if ($subfile =~ m/index\.(html?(\.gz)?)$/i)
-		    {
-			# Cater for links such as "/paper/" mapping to
-			#  "/paper/index.html"
-			my $lookup_dirname 
-			    = $self->f2d_lookup_filename($dirname);
-
-			$self->{'f2d_mapping'}->{$lookup_dirname} = $oid;
-		    }
-		}
-		elsif ($subfile =~ /\.(gif|jpg|jpeg|png)$/i)
-		{
-		    # convert to png ?
-
-		    # Hard link in build directory		    
-		    #--
-		    my $coldir    = $ENV{'GSDLCOLLECTDIR'};
-		    my $dirsep_re = &util::get_re_dirsep();
-		    my $iorb_re   = "import|building";
-
-		    my ($dirsep,$iorb)
-			= ($filename =~ m/^$coldir($dirsep_re)($iorb_re)/);
-
-		    my $copyname = $filename;
-		    $copyname 
-			=~ s/^${coldir}${dirsep}${iorb}
-			    /${coldir}${dirsep}building_images${dirsep}imgsrc/x;
-
-  	            if (!-e $copyname)
-		    {
-                        print STDERR "HTMLPlug: Hard linking $subfile\n"
-                            if (defined($self->{'verbosity'}));
-			&util::hard_link($filename,$copyname);
-		    }
-
-		}
-	    }
-	    
-	}
-    }
-}
-
-sub build_file_to_doc_mapping
-{
-    my ($self,$base_dir) = @_;
-
-    $self->{'f2d_mapping'} = {};
-
-    my $bimages_dir 
-	= &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"building_images","imgsrc");
-    if (!-e $bimages_dir && !mkdir ($bimages_dir, 0775)) 
-    {
-	print STDERR "HTMLPlug:: Could not create directory $bimages_dir\n";
-	return;
-    }
-
-    $self->rec_f2d_mapping($base_dir);
-}
-
-#--
-#--
 
 # return number of files processed, undef if can't process
@@ -196,167 +106,278 @@
     my ($pluginfo, $base_dir, $file, $metadata, $processor) = @_;
 
-    my $verbosity = $processor->{'verbosity'};
-    $self->{'verbosity'} = $verbosity;
-
-    if (!defined($self->{'f2d_mapping'}))
-    {
-	print STDERR "HTMLPlug: building OID mapping\n" 
-	    if $processor->{'verbosity'};
-
-	$self->build_file_to_doc_mapping($base_dir,$verbosity);
-    }
     my $filename = &util::filename_cat($base_dir, $file);
-    my $absdir = $filename;
-    $absdir =~ s/[^\/\\]*$//;
-
-    if ($file =~ /\.(gif|jpg|jpeg|png)$/i)
-    {
-	return 0;
-    }
-
-    return undef unless ($filename =~ /\.(html?(\.gz)?)$/i && (-e $filename));
-    return undef if (-d $filename);
-
-    my $gz = 0;
-    if (defined $2) {
-	$gz = $2;
-	$gz = 1 if ($gz =~ /\.gz/i);
-    }
-
-    print STDERR "HTMLPlug: processing $file\n" if $processor->{'verbosity'};
+    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");
-   
-    if ($gz) {
-	open (FILE, "zcat $filename |") || die "HTMLPlug::read - zcat can't open $filename\n";
+    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 = <FILE>;
+    $/ = "\n";
+    close FILE;
+    if ($text !~ /\w/) {
+	print STDERR "HTMLPlug: ERROR: $file contains no text\n" if $self->{'verbosity'};
+	return 0;
+    }
+
+    $self->add_external_metadata ($metadata, $doc_obj, $cursection);
+    $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/^.*?<body[^>]*>//is;
+	$text =~ s/(<\/body[^>]*>|<\/html[^>]*>)//isg;
+    }
+
+    # trap links
+    if (!$self->{'nolinks'}) {
+
+	# usemap="./#index" not handled correctly => change to "#index"
+	$text =~ s/(<img[^>]*?usemap\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/
+	    $self->replace_usemap_links($1, $2, $3)/isge;
+
+	$text =~ s/(<(?:a|area|frame)\s+[^>]*?(?:href|src)\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/
+	    $self->replace_href_links ($1, $2, $3, $base_dir, $file, $doc_obj)/isge;
+    }
+
+    # trap images
+    $text =~ s/(<img[^>]*?src\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/
+	$self->replace_images ($1, $2, $3, $base_dir, $file, $doc_obj)/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) = @_;
+    
+    $link =~ s/\n/ /g;
+    
+    my ($href, $hash_part, $rl) = $self->format_link ($link, $base_dir, $file);
+    return $front . $self->add_file ($href, $base_dir, $doc_obj) . $back;
+}
+
+sub replace_href_links {
+    my $self = shift (@_);
+    my ($front, $link, $back, $base_dir, $file, $doc_obj) = @_;
+
+    # 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);
+
+    if (($rl == 0) || ($href =~ /$self->{'process_exp'}/) || 
+	($href =~ /\/$/) || ($href =~ /^(mailto|news|gopher|nntp|telnet|javascript):/i)) {
+	$link = $href . $hash_part;
+	&html::urlsafe ($link);
+	return $front . "_httpextlink_&href=" . $link . "&rl=" . $rl . $back;
+	
     } else {
-	open (FILE, $filename) || die "HTMLPlug::read - can't open $filename\n";
-    }
-    my $cursection = $doc_obj->get_top_section();
-
-    # look up precalculated OID
-    my $lookup_filename = $self->f2d_lookup_filename($filename);
-    my $precalc_oid = $self->{'f2d_mapping'}->{$lookup_filename};
-
-    # read in HTML file
-    my $text = "";
-    my $line = "";
-    my $donehead = 0;
-    my $title = "";
-    while (defined ($line = <FILE>)) {
-	$text .= $line;
-    }
-
-    # remove line breaks
-    $text =~ s/\s+/ /g;
-
-    my $foundtitle = 0;
-    # don't use title tag if Title has been passed
-    # in from another plugin
-    if (defined $metadata->{'Title'}) {
-	$foundtitle = 1;
-    }
-    if (!$foundtitle) {
-	# see if there's a <title> tag
-	if ($text =~ /<title[^>]*>([^<]*)<\/title[^>]*>/i) {
-	    if (defined $1) {
-		my $title = $1;
-		if ($title =~ /\w/) {
-		    $doc_obj->add_metadata ($cursection, "Title", $title);
-		    $foundtitle = 1;
-		}
+	# 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, $base_dir, $doc_obj) . $back;
+    }
+}
+
+sub add_file {
+    my $self = shift (@_);
+    my ($href, $base_dir, $doc_obj) = @_;
+    my ($newname);
+
+    my $filename = $href;
+    $filename =~ s/^[^:]*:\/\///;
+    $filename = &util::filename_cat ($base_dir, $filename);
+    my ($ext) = $filename =~ /(\.[^\.]*)$/;
+    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->inc_filecount ();
+    }
+    $doc_obj->associate_file($filename, $newname);
+    return "_httpcollimg_/$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 ($front$link$back)\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
 	    }
-	}
-    }
-    # if no title use first 100 characters
-    if (!$foundtitle) {
-	my $tmptext = $text;
-	$tmptext =~ s/<[^>]*>//g;
-	my $title = substr ($tmptext, 0, 100);
-	$doc_obj->add_metadata ($cursection, "Title", $title);
-    }
-    
-    # remove header rubbish
-    $text =~ s/^.*?<body[^>]*>//i;
-
-    # usemap="./#index" not handled correctly => change to "#index"
-    $text =~ s/(<img[^>]*?usemap\s*=\s*\"?)([^\">]+)(\"?[^>]*>)/
-	&replace_usemap_links($1, $2, $3)/ige;
-
-    # fix up the href links
-    $f2d_mapping = $self->{'f2d_mapping'};
-
-    $text =~ s/(<(a|area)\s+[^>]*?href\s*=\s*\"?)([^\">]+)(\"?[^>]*>)/
-       &replace_href_links($filename,$precalc_oid,$f2d_mapping,$1,$3,$4)/ige;
-
-    # add a newline at the beginning of each paragraph
-    $text =~ s/(.)\s*<p\b/$1\n\n<p/gi;
-    
-    # add a newline every 80 characters at a word boundary
-    # Note: this regular expression puts a line feed before
-    # the last word in each section, even when it is not
-    # needed.
-    $text =~ s/(.{1,80})\s/$1\n/g;
-
-
-    # Store URL for page as metadata
-    my $web_url = "http://$lookup_filename";
-    $doc_obj->add_metadata($cursection, "URL", $web_url);
-
-    my $import_url = $filename;
-    $import_url =~ s/^$ENV{'GSDLCOLLECTDIR'}/_httpcollection_/;
-
-    # Add base tag so images can find correct location
-    my $index_url = $import_url;
-    $index_url =~ s/_httpcollection_\/import/_httpcollection_\/index\/imgsrc/;
-
-    my $dirsep_re = &util::get_re_dirsep();
-    my @base_url_split = split(/$dirsep_re/,$index_url);
-    pop(@base_url_split);
-    my $base_url = join('/',@base_url_split,""); # force / at end
-
-    $text = "<base href=\"$base_url\">\n$text";
-    $doc_obj->add_text ($cursection, $text);
-
-    # Add metadata that has been provided externally
-    #--
+
+	} 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);
+    }
+}
+
+# Add metadata that has been provided externally (i.e. by 
+# a previous (recursive) plugin
+sub add_external_metadata {
+    my $self = shift (@_);
+    my ($metadata, $doc_obj, $section) = @_;
+
     foreach $field (keys(%$metadata)) {
 	# $metadata->{$field} may be an array reference
 	if (ref ($metadata->{$field}) eq "ARRAY") {
 	    map { 
-		$doc_obj->add_metadata ($cursection, $field, $_); 
+		$doc_obj->add_metadata ($section, $field, $_); 
 	    } @{$metadata->{$field}};
 	} else {
-	    $doc_obj->add_metadata ($cursection, $field, $metadata->{$field}); 
-	}
-    }
-
-    # Fix OID so it is the same as the pre-calculated OID
-    $doc_obj->set_OID($precalc_oid);
-
-    my $set_oid = $doc_obj->get_OID();
-    if ($precalc_oid ne $set_oid) # check (for super safety!)
-    {
-	print STDERR "Warning: pre-calculated OID and current OID differnt:";
-	print STDERR " $filename\n";
-    }
-
-
-    # process the document
-    $processor->process($doc_obj);
-
-    return 1; # processed the file
-}
-
-# support for fixing up links to work as a GSDL collection
-#--
-
-sub eval_dir_dots
-{
-    # evaluate any "../" to next directory up
-    # evaluate any "./" as here
-    #--
-    my ($self,$filename) = @_;
+	    $doc_obj->add_metadata ($section, $field, $metadata->{$field}); 
+	}
+    }
+}
+
+sub extract_metadata {
+    my $self = shift (@_);
+    my ($textref, $metadata, $doc_obj, $section) = @_;
+
+    foreach $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 <meta> tag for this field
+	if ($$textref =~ /<meta(.*?)(?:name|http-equiv)\s*=\s*\"?$field\"?([^>]*)/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 <title> 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();
@@ -364,16 +385,12 @@
 
     my @eval_dirs = ();
-    foreach $d (@dirsep)
-    {
-	if ($d eq "..")
-	{
+    foreach $d (@dirsep) {
+	if ($d eq "..") {
 	    pop(@eval_dirs);
-	}
-	elsif ($d eq ".")
-	{
+
+	} elsif ($d eq ".") {
 	    # do nothing!
-	}
-	else
-	{
+
+	} else {
 	    push(@eval_dirs,$d);
 	}
@@ -383,136 +400,22 @@
 }
 
-sub replace_href_links 
-{
-    my ($this_filename,$this_oid, $f2d_mapping, $front,$link,$back) = @_;
-
-    return $front.$link.$back if ($link =~ m/\.(gif|jpg|jpeg|png)$/i);
-
-    if ($link =~ m/^(http|ftp|file):/i) 
-    {
-        # this should really check that the link ends in .htm*
-	#****
-	my $http_as_filename = $link;
-	$http_as_filename =~ s/^(http|ftp|file):\/\///i;
-
-	my ($before_hash,$after_hash) 
-	    = ($http_as_filename =~ m/^([^\#]*)\#?(.*)$/);
-	       
-	if ($link =~ m/^(http|ftp):/i)
-	{
-	    # Turn url (using /) into file name (possibly using \ on windows)
-	    my @http_dir_split = split('/',$before_hash);
-	    $http_as_filename = &util::filename_cat(@http_dir_split);
-	}
-
-	$http_as_filename = $self->eval_dir_dots($http_as_filename);
-					 
-	if (defined($f2d_mapping->{$http_as_filename}))
-	{
-	    # transform link into a local link (and then let it be
-	    #   processed by later code)
-	    $link = $http_as_filename;
-
-	    $link = "_httpdocument_&cl=_cgiargcl_&d=$oid";
-	    $link .= "#$after_hash" if ($after_hash ne "");
-	}
-	else
-	{
-	    # external link => set it up to pass through off-site page
-
-	    my $link_safe = $link;
-	    &html::urlsafe($link_safe);
-
-	    $link = "_httpextlink_&href=$link_safe&d=$this_oid";
-
-	}
-    }	
-    elsif ($link !~ m/^(mailto|news):/i)
-    {
-	my ($before_hash,$after_hash) = ($link =~ m/^([^\#]*)\#?(.*)$/);
-	my $link_filename;
-
-	if ($before_hash =~ m/^\//)
-	{
-	    my $dirsep_re = &util::get_re_dirsep();
-	    my $lookup = $self->f2d_lookup_filename($this_filename);
-	    my @lookup_split = split(/$dirsep_re/,$lookup);
-	    my $domname = shift(@lookup_split);
-	    $link_filename = &util::filename_cat($domname,$before_hash);
-	}
-	else
-	{
-	    # Turn relative file path into full path
-	    if ($before_hash eq "") # handle links such as <a href="#x">
-	    {
-		$link_filename = $this_filename;
-	    }
-	    else
-	    {
-		my $dirname = &File::Basename::dirname($this_filename);
-		$link_filename = &util::filename_cat($dirname,$before_hash);
-	    }
-	}
-
-	$link_filename = $self->eval_dir_dots($link_filename);
-	my $lookup_filename = $self->f2d_lookup_filename($link_filename);
-
-	my $oid = $f2d_mapping->{$lookup_filename};	
-	if (defined($oid))
-	{
-	    $link = "_httpdocument_&cl=_cgiargcl_&d=$oid";
-	    $link .= "#$after_hash" if ($after_hash ne "");
-	}
-	else
-	{
-	    print STDERR "HTMLPlug WARNING:";
-	    print STDERR " Could not find link: \"$lookup_filename\". ";
-	    print STDERR " Deactivating link\n";
-	    
-	    $link = "_httpextlink_&d=$this_oid";
-	}
-    }
-    else
-    {
-	if ($link !~ m/^(mailto|news):/i)
-	{
-	    print STDERR "HTMLPlug WARNING: Unhandled type of link, \"$link\"\n";
-	}
-	else
-	{
-	    my $link_safe = $link;
-	    &html::urlsafe($link_safe);
-
-	    $link = "_httpextlink_&href=$link_safe&d=$this_oid";
-	}
-    }
-
-    my $fixed_tag = "${front}${link}${back}";
-
-    return $fixed_tag;
-}
-
-
-sub replace_usemap_links 
-{
+sub replace_usemap_links {
+    my $self = shift (@_);
     my ($front, $link, $back) = @_;
 
     $link =~ s/^\.\///;
-
-    return "${front}${link}${back}";
-}
-
-
+    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;
-
-
-
-
-
-
-
-
-
-
-
