Ignore:
Timestamp:
1999-12-07T10:41:49+13:00 (24 years ago)
Author:
sjboddie
Message:

New html plugin with options

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/perllib/plugins/HTMLPlug.pm

    r734 r808  
    22#
    33# HTMLPlug.pm -- basic html plugin
     4#
    45# A component of the Greenstone digital library software
    56# from the New Zealand Digital Library Project at the
     
    2425###########################################################################
    2526
     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
    2637
    2738package HTMLPlug;
    2839
    2940use BasPlug;
    30 use sorttools;
    3141use html;
    3242use util;
    33 
    34 use File::Basename;
     43use parsargv;
    3544
    3645sub BEGIN {
     
    3847}
    3948
     49sub print_usage {
     50    print STDERR "\nIncorrect options passed to HTMLPlug, check your collect.cfg configuration file\n";
     51
     52    print STDERR "\n  usage: plugin HTMLPlug [options]\n\n";
     53    print STDERR "  options:\n";
     54    print STDERR "   -process_exp           A perl regular expression to match against filenames.\n";
     55    print STDERR "                          Matching filenames will be processed by this plugin.\n";
     56    print STDERR "                          Defaults to '(?i)\.html?\$' i.e. all documents ending in\n";
     57    print STDERR "                          .htm or .html (case-insensitive).\n";
     58    print STDERR "   -nolinks               Don't make any attempt to trap links (setting this flag may\n";
     59    print STDERR "                          improve speed of building/importing but any relative links within\n";
     60    print STDERR "                          documents will be broken).\n";
     61    print STDERR "   -block_exp             Files matching this regular expression will be blocked from\n";
     62    print STDERR "                          being passed to any further plugins in the list. By default\n";
     63    print STDERR "                          HTMLPlug blocks any files with .gif, .jpg, .jpeg, .png, .pdf\n";
     64    print STDERR "                          or .rtf file extensions.\n";
     65    print STDERR "   -keep_head             Don't remove headers from html files.\n";
     66    print STDERR "   -no_metadata           Don't attempt to extract any metadata from files.\n";
     67    print STDERR "   -metadata_fields       Comma separated list of metadata fields to attempt to extract.\n";
     68    print STDERR "                          Defaults to 'Title'\n";
     69    print STDERR "   -w3mir                 Set if w3mir was used to generate input file structure.\n\n";
     70}
     71
    4072sub new {
    41     my ($class) = @_;
     73    my $class = shift (@_);
    4274    $self = new BasPlug ();
    4375
    44     $self->{'f2d_mapping'} = undef;
    45     $self->{'f2d_partitioned_dirs'} = [];
     76    if (!parsargv::parse(\@_,
     77             q^process_exp/.*/(?i)\.html?$^, \$self->{'process_exp'},
     78             q^nolinks^, \$self->{'nolinks'},
     79             q^block_exp/.*/(?i)\.(gif|jpe?g|png|pdf|rtf)$^, \$self->{'block_exp'},
     80             q^keep_head^, \$self->{'keep_head'},
     81             q^no_metadata^, \$self->{'no_metadata'},
     82             q^metadata_fields/.*/Title^, \$self->{'metadata_fields'},
     83             q^w3mir^, \$self->{'w3mir'})) {
     84    &print_usage();
     85    die "\n";
     86    }
     87
     88    $self->{'aux_files'} = {};
     89    $self->{'dir_num'} = 0;
     90    $self->{'file_num'} = 0;
    4691
    4792    return bless $self, $class;
     
    5398    return 0; # this is not a recursive plugin
    5499}
    55 
    56 #--
    57 # Convert filename to lookup filename
    58 #--
    59 sub f2d_lookup_filename
    60 {
    61     my ($self,$filename) = @_;
    62 
    63     my $coldir = $ENV{'GSDLCOLLECTDIR'};
    64     my $dirsep_re = &util::get_re_dirsep();
    65     my $supress_dirs = "(import|building)";
    66 
    67     my @part_dirs = @{$self->{'f2d_partitioned_dirs'}};
    68 
    69     if (scalar(@part_dirs)>0)
    70     {
    71     my $joined_part_dirs = join('|',@part_dirs);
    72     $supress_dirs .= "($dirsep_re)($joined_part_dirs)";
    73     }
    74 
    75     my $lookup_filename = $filename;
    76     $lookup_filename =~ s/^$coldir($dirsep_re)//;
    77     $lookup_filename =~ s/^$supress_dirs($dirsep_re)//;
    78 
    79     return $lookup_filename;
    80 }
    81 
    82 
    83 #--
    84 # file to document mapping
    85 #--
    86 
    87 sub rec_f2d_mapping
    88 {
    89     my ($self,$dirname) = @_;
    90 
    91     # read all the files in the directory
    92     if (!opendir(DIR, $dirname))
    93     {
    94     print STDERR "HTMLPlug: WARNING - couldn't read directory $dirname";
    95     print STDERR " during file to doc mapping\n";
    96     return;
    97     }
    98     my @dir = readdir (DIR);
    99     closedir (DIR);
    100 
    101     # process each file
    102     my $subfile;
    103     foreach $subfile (@dir)
    104     {
    105     if ($subfile !~ /^\.\.?$/)
    106     {
    107         my $filename = &util::filename_cat($dirname, $subfile);
    108 
    109         if (-d $filename)
    110         {
    111         my $dirname = $filename;
    112         $self->rec_f2d_mapping($dirname);
    113         }
    114         else
    115         {
    116         if ($subfile =~ m/\.(html?(\.gz)?)$/i && (-e $filename))
    117         {
    118             # add mapping
    119 
    120             print STDERR "HTMLPlug: Precalculating OID for $subfile\n"
    121             if (defined($self->{'verbosity'}));
    122 
    123             my $oid = doc::_calc_OID(undef,$filename);
    124             my $lookup_filename
    125             = $self->f2d_lookup_filename($filename);
    126 
    127             $self->{'f2d_mapping'}->{$lookup_filename} = $oid;
    128 
    129             if ($subfile =~ m/index\.(html?(\.gz)?)$/i)
    130             {
    131             # Cater for links such as "/paper/" mapping to
    132             #  "/paper/index.html"
    133             my $lookup_dirname
    134                 = $self->f2d_lookup_filename($dirname);
    135 
    136             $self->{'f2d_mapping'}->{$lookup_dirname} = $oid;
    137             }
    138         }
    139         elsif ($subfile =~ /\.(gif|jpg|jpeg|png)$/i)
    140         {
    141             # convert to png ?
    142 
    143             # Hard link in build directory         
    144             #--
    145             my $coldir    = $ENV{'GSDLCOLLECTDIR'};
    146             my $dirsep_re = &util::get_re_dirsep();
    147             my $iorb_re   = "import|building";
    148 
    149             my ($dirsep,$iorb)
    150             = ($filename =~ m/^$coldir($dirsep_re)($iorb_re)/);
    151 
    152             my $copyname = $filename;
    153             $copyname
    154             =~ s/^${coldir}${dirsep}${iorb}
    155                 /${coldir}${dirsep}building_images${dirsep}imgsrc/x;
    156 
    157                 if (!-e $copyname)
    158             {
    159                         print STDERR "HTMLPlug: Hard linking $subfile\n"
    160                             if (defined($self->{'verbosity'}));
    161             &util::hard_link($filename,$copyname);
    162             }
    163 
    164         }
    165         }
    166        
    167     }
    168     }
    169 }
    170 
    171 sub build_file_to_doc_mapping
    172 {
    173     my ($self,$base_dir) = @_;
    174 
    175     $self->{'f2d_mapping'} = {};
    176 
    177     my $bimages_dir
    178     = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"building_images","imgsrc");
    179     if (!-e $bimages_dir && !mkdir ($bimages_dir, 0775))
    180     {
    181     print STDERR "HTMLPlug:: Could not create directory $bimages_dir\n";
    182     return;
    183     }
    184 
    185     $self->rec_f2d_mapping($base_dir);
    186 }
    187 
    188 #--
    189 #--
    190100
    191101# return number of files processed, undef if can't process
     
    196106    my ($pluginfo, $base_dir, $file, $metadata, $processor) = @_;
    197107
    198     my $verbosity = $processor->{'verbosity'};
    199     $self->{'verbosity'} = $verbosity;
    200 
    201     if (!defined($self->{'f2d_mapping'}))
    202     {
    203     print STDERR "HTMLPlug: building OID mapping\n"
    204         if $processor->{'verbosity'};
    205 
    206     $self->build_file_to_doc_mapping($base_dir,$verbosity);
    207     }
    208108    my $filename = &util::filename_cat($base_dir, $file);
    209     my $absdir = $filename;
    210     $absdir =~ s/[^\/\\]*$//;
    211 
    212     if ($file =~ /\.(gif|jpg|jpeg|png)$/i)
    213     {
    214     return 0;
    215     }
    216 
    217     return undef unless ($filename =~ /\.(html?(\.gz)?)$/i && (-e $filename));
    218     return undef if (-d $filename);
    219 
    220     my $gz = 0;
    221     if (defined $2) {
    222     $gz = $2;
    223     $gz = 1 if ($gz =~ /\.gz/i);
    224     }
    225 
    226     print STDERR "HTMLPlug: processing $file\n" if $processor->{'verbosity'};
     109    return 0 if $filename =~ /$self->{'block_exp'}/;
     110    if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
     111    return undef;
     112    }
     113    $file =~ s/^[\/\\]+//;
     114
     115    $self->{'verbosity'} = $processor->{'verbosity'};
     116    print STDERR "HTMLPlug: processing $file\n"
     117    if $self->{'verbosity'} > 1;
    227118
    228119    # create a new document
    229120    my $doc_obj = new doc ($file, "indexed_doc");
    230    
    231     if ($gz) {
    232     open (FILE, "zcat $filename |") || die "HTMLPlug::read - zcat can't open $filename\n";
     121    my $cursection = $doc_obj->get_top_section();
     122   
     123    # read in HTML file
     124    open (FILE, $filename) || die "HTMLPlug::read - can't open $filename\n";
     125    undef $/;
     126    my $text = <FILE>;
     127    $/ = "\n";
     128    close FILE;
     129    if ($text !~ /\w/) {
     130    print STDERR "HTMLPlug: ERROR: $file contains no text\n" if $self->{'verbosity'};
     131    return 0;
     132    }
     133
     134    $self->add_external_metadata ($metadata, $doc_obj, $cursection);
     135    $self->extract_metadata (\$text, $metadata, $doc_obj, $cursection)
     136    unless $self->{'no_metadata'};
     137
     138    # Store URL for page as metadata - this can be used for an
     139    # altavista style search interface. The URL won't be valid
     140    # unless the file structure contains the domain name (i.e.
     141    # like when w3mir is used to download a website).
     142    my $web_url = "http://$file";
     143    $web_url =~ s/\\/\//g; # for windows
     144    $doc_obj->add_metadata($cursection, "URL", $web_url);
     145
     146    # remove header and footer
     147    if (!$self->{'keep_head'}) {
     148    $text =~ s/^.*?<body[^>]*>//is;
     149    $text =~ s/(<\/body[^>]*>|<\/html[^>]*>)//isg;
     150    }
     151
     152    # trap links
     153    if (!$self->{'nolinks'}) {
     154
     155    # usemap="./#index" not handled correctly => change to "#index"
     156    $text =~ s/(<img[^>]*?usemap\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/
     157        $self->replace_usemap_links($1, $2, $3)/isge;
     158
     159    $text =~ s/(<(?:a|area|frame)\s+[^>]*?(?:href|src)\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/
     160        $self->replace_href_links ($1, $2, $3, $base_dir, $file, $doc_obj)/isge;
     161    }
     162
     163    # trap images
     164    $text =~ s/(<img[^>]*?src\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/
     165    $self->replace_images ($1, $2, $3, $base_dir, $file, $doc_obj)/isge;
     166
     167    $doc_obj->add_text ($cursection, $text);
     168
     169    # add an OID
     170    $doc_obj->set_OID();
     171
     172    # process the document
     173    $processor->process($doc_obj);
     174
     175    return 1; # processed the file
     176}
     177
     178sub replace_images {
     179    my $self = shift (@_);
     180    my ($front, $link, $back, $base_dir, $file, $doc_obj) = @_;
     181   
     182    $link =~ s/\n/ /g;
     183   
     184    my ($href, $hash_part, $rl) = $self->format_link ($link, $base_dir, $file);
     185    return $front . $self->add_file ($href, $base_dir, $doc_obj) . $back;
     186}
     187
     188sub replace_href_links {
     189    my $self = shift (@_);
     190    my ($front, $link, $back, $base_dir, $file, $doc_obj) = @_;
     191
     192    # attempt to sort out targets - frames are not handled
     193    # well in this plugin and some cases will screw things
     194    # up - e.g. the _parent target (so we'll just remove
     195    # them all ;-)
     196    $front =~ s/(target=\"?)_top(\"?)/$1_gsdltop_$2/is;
     197    $back =~ s/(target=\"?)_top(\"?)/$1_gsdltop_$2/is;
     198    $front =~ s/target=\"?_parent\"?//is;
     199    $back =~ s/target=\"?_parent\"?//is;
     200
     201    return $front . $link . $back if $link =~ /^\#/s;
     202    $link =~ s/\n/ /g;
     203
     204    my ($href, $hash_part, $rl) = $self->format_link ($link, $base_dir, $file);
     205
     206    if (($rl == 0) || ($href =~ /$self->{'process_exp'}/) ||
     207    ($href =~ /\/$/) || ($href =~ /^(mailto|news|gopher|nntp|telnet|javascript):/i)) {
     208    $link = $href . $hash_part;
     209    &html::urlsafe ($link);
     210    return $front . "_httpextlink_&href=" . $link . "&rl=" . $rl . $back;
     211   
    233212    } else {
    234     open (FILE, $filename) || die "HTMLPlug::read - can't open $filename\n";
    235     }
    236     my $cursection = $doc_obj->get_top_section();
    237 
    238     # look up precalculated OID
    239     my $lookup_filename = $self->f2d_lookup_filename($filename);
    240     my $precalc_oid = $self->{'f2d_mapping'}->{$lookup_filename};
    241 
    242     # read in HTML file
    243     my $text = "";
    244     my $line = "";
    245     my $donehead = 0;
    246     my $title = "";
    247     while (defined ($line = <FILE>)) {
    248     $text .= $line;
    249     }
    250 
    251     # remove line breaks
    252     $text =~ s/\s+/ /g;
    253 
    254     my $foundtitle = 0;
    255     # don't use title tag if Title has been passed
    256     # in from another plugin
    257     if (defined $metadata->{'Title'}) {
    258     $foundtitle = 1;
    259     }
    260     if (!$foundtitle) {
    261     # see if there's a <title> tag
    262     if ($text =~ /<title[^>]*>([^<]*)<\/title[^>]*>/i) {
    263         if (defined $1) {
    264         my $title = $1;
    265         if ($title =~ /\w/) {
    266             $doc_obj->add_metadata ($cursection, "Title", $title);
    267             $foundtitle = 1;
    268         }
     213    # link is to some other type of file (image, pdf etc.) so we'll
     214    # need to associate that file
     215    return $front . $self->add_file ($href, $base_dir, $doc_obj) . $back;
     216    }
     217}
     218
     219sub add_file {
     220    my $self = shift (@_);
     221    my ($href, $base_dir, $doc_obj) = @_;
     222    my ($newname);
     223
     224    my $filename = $href;
     225    $filename =~ s/^[^:]*:\/\///;
     226    $filename = &util::filename_cat ($base_dir, $filename);
     227    my ($ext) = $filename =~ /(\.[^\.]*)$/;
     228    if (defined $self->{'aux_files'}->{$href}) {
     229    $newname = $self->{'aux_files'}->{$href}->{'dir_num'} . "/" .
     230        $self->{'aux_files'}->{$href}->{'file_num'} . $ext;
     231    } else {
     232    $newname = $self->{'dir_num'} . "/" . $self->{'file_num'} . $ext;
     233    $self->inc_filecount ();
     234    }
     235    $doc_obj->associate_file($filename, $newname);
     236    return "_httpcollimg_/$newname";
     237}
     238
     239
     240sub format_link {
     241    my $self = shift (@_);
     242    my ($link, $base_dir, $file) = @_;
     243
     244    my ($before_hash, $hash_part) = $link =~ /^([^\#]*)(\#?.*)$/;
     245    $hash_part = "" if !defined $hash_part;
     246    if (!defined $before_hash || $before_hash !~ /[\w\.\/]/) {
     247    print STDERR "HTMLPlug: ERROR - badly formatted tag ignored ($front$link$back)\n"
     248        if $self->{'verbosity'};
     249    return ($link, "", 0);
     250    }
     251   
     252    if ($before_hash =~ s/^((?:http|ftp|file):\/\/)//i) {
     253    my $type = $1;
     254           
     255    if ($link =~ /^(http|ftp):/i) {
     256        # Turn url (using /) into file name (possibly using \ on windows)
     257        my @http_dir_split = split('/', $before_hash);
     258        $before_hash = &util::filename_cat(@http_dir_split);
     259    }
     260
     261    $before_hash = $self->eval_dir_dots($before_hash);
     262                     
     263    my $linkfilename = &util::filename_cat ($base_dir, $before_hash);
     264
     265    my $rl = 0;
     266    $rl = 1 if (-e $linkfilename);
     267
     268    # make sure there's a slash on the end if it's a directory
     269    if ($before_hash !~ /\/$/) {
     270        $before_hash .= "/" if (-d $linkfilename);
     271    }
     272
     273    return ($type . $before_hash, $hash_part, $rl);
     274   
     275    } elsif ($link !~ /^(mailto|news|gopher|nntp|telnet|javascript):/i) {
     276
     277    if ($before_hash =~ s/^\///) {
     278        # the first directory will be the domain name if w3mir was used
     279        # to generate archives, otherwise we'll assume all files are
     280        # from the same site and base_dir is the root
     281        if ($self->{'w3mir'}) {
     282        my @dirs = split /[\/\\]/, $file;
     283        my $domname = shift (@dirs);
     284        $before_hash = &util::filename_cat($domname, $before_hash);
     285        $before_hash =~ s/\\/\//g; # for windows
    269286        }
    270     }
    271     }
    272     # if no title use first 100 characters
    273     if (!$foundtitle) {
    274     my $tmptext = $text;
    275     $tmptext =~ s/<[^>]*>//g;
    276     my $title = substr ($tmptext, 0, 100);
    277     $doc_obj->add_metadata ($cursection, "Title", $title);
    278     }
    279    
    280     # remove header rubbish
    281     $text =~ s/^.*?<body[^>]*>//i;
    282 
    283     # usemap="./#index" not handled correctly => change to "#index"
    284     $text =~ s/(<img[^>]*?usemap\s*=\s*\"?)([^\">]+)(\"?[^>]*>)/
    285     &replace_usemap_links($1, $2, $3)/ige;
    286 
    287     # fix up the href links
    288     $f2d_mapping = $self->{'f2d_mapping'};
    289 
    290     $text =~ s/(<(a|area)\s+[^>]*?href\s*=\s*\"?)([^\">]+)(\"?[^>]*>)/
    291        &replace_href_links($filename,$precalc_oid,$f2d_mapping,$1,$3,$4)/ige;
    292 
    293     # add a newline at the beginning of each paragraph
    294     $text =~ s/(.)\s*<p\b/$1\n\n<p/gi;
    295    
    296     # add a newline every 80 characters at a word boundary
    297     # Note: this regular expression puts a line feed before
    298     # the last word in each section, even when it is not
    299     # needed.
    300     $text =~ s/(.{1,80})\s/$1\n/g;
    301 
    302 
    303     # Store URL for page as metadata
    304     my $web_url = "http://$lookup_filename";
    305     $doc_obj->add_metadata($cursection, "URL", $web_url);
    306 
    307     my $import_url = $filename;
    308     $import_url =~ s/^$ENV{'GSDLCOLLECTDIR'}/_httpcollection_/;
    309 
    310     # Add base tag so images can find correct location
    311     my $index_url = $import_url;
    312     $index_url =~ s/_httpcollection_\/import/_httpcollection_\/index\/imgsrc/;
    313 
    314     my $dirsep_re = &util::get_re_dirsep();
    315     my @base_url_split = split(/$dirsep_re/,$index_url);
    316     pop(@base_url_split);
    317     my $base_url = join('/',@base_url_split,""); # force / at end
    318 
    319     $text = "<base href=\"$base_url\">\n$text";
    320     $doc_obj->add_text ($cursection, $text);
    321 
    322     # Add metadata that has been provided externally
    323     #--
     287
     288    } else {
     289        # Turn relative file path into full path
     290        my $dirname = &File::Basename::dirname($file);
     291        $before_hash = &util::filename_cat($dirname, $before_hash);
     292        $before_hash = $self->eval_dir_dots($before_hash);
     293    }
     294
     295    # make sure there's a slash on the end if it's a directory
     296    my $linkfilename = &util::filename_cat ($base_dir, $before_hash);
     297    if ($before_hash !~ /\/$/) {
     298        $before_hash .= "/" if (-d $linkfilename);
     299    }
     300
     301    return ("http://" . $before_hash, $hash_part, 1);
     302
     303    } else {
     304    # mailto, news, nntp, telnet, javascript or gopher link
     305    return ($before_hash, "", 0);
     306    }
     307}
     308
     309# Add metadata that has been provided externally (i.e. by
     310# a previous (recursive) plugin
     311sub add_external_metadata {
     312    my $self = shift (@_);
     313    my ($metadata, $doc_obj, $section) = @_;
     314
    324315    foreach $field (keys(%$metadata)) {
    325316    # $metadata->{$field} may be an array reference
    326317    if (ref ($metadata->{$field}) eq "ARRAY") {
    327318        map {
    328         $doc_obj->add_metadata ($cursection, $field, $_);
     319        $doc_obj->add_metadata ($section, $field, $_);
    329320        } @{$metadata->{$field}};
    330321    } else {
    331         $doc_obj->add_metadata ($cursection, $field, $metadata->{$field});
    332     }
    333     }
    334 
    335     # Fix OID so it is the same as the pre-calculated OID
    336     $doc_obj->set_OID($precalc_oid);
    337 
    338     my $set_oid = $doc_obj->get_OID();
    339     if ($precalc_oid ne $set_oid) # check (for super safety!)
    340     {
    341     print STDERR "Warning: pre-calculated OID and current OID differnt:";
    342     print STDERR " $filename\n";
    343     }
    344 
    345 
    346     # process the document
    347     $processor->process($doc_obj);
    348 
    349     return 1; # processed the file
    350 }
    351 
    352 # support for fixing up links to work as a GSDL collection
    353 #--
    354 
    355 sub eval_dir_dots
    356 {
    357     # evaluate any "../" to next directory up
    358     # evaluate any "./" as here
    359     #--
    360     my ($self,$filename) = @_;
     322        $doc_obj->add_metadata ($section, $field, $metadata->{$field});
     323    }
     324    }
     325}
     326
     327sub extract_metadata {
     328    my $self = shift (@_);
     329    my ($textref, $metadata, $doc_obj, $section) = @_;
     330
     331    foreach $field (split /,/, $self->{'metadata_fields'}) {
     332   
     333    # don't need to extract field if it was passed in from a previous
     334    # (recursive) plugin
     335    next if defined $metadata->{$field};
     336
     337    # see if there's a <meta> tag for this field
     338    if ($$textref =~ /<meta(.*?)(?:name|http-equiv)\s*=\s*\"?$field\"?([^>]*)/is) {
     339        my $content = $1 . $2;
     340        if ($content =~ /content\s*=\s*\"?(.*?)\"?/is) {
     341        if (defined $1) {
     342            my $value = $1;
     343            $value =~ s/\s+/ /gs;
     344            $doc_obj->add_metadata($section, $field, $value);
     345            next;
     346        }
     347        }
     348    }
     349   
     350    # special case for Title metadata - try <title> tags
     351    # then first 100 characters of text
     352   
     353    if ($field =~ /^title$/i) {
     354
     355        # see if there's a <title> tag
     356        if ($$textref =~ /<title[^>]*>([^<]*)<\/title[^>]*>/is) {
     357        if (defined $1) {
     358            my $title = $1;
     359            if ($title =~ /\w/) {
     360            $title =~ s/\s+/ /gs;
     361            $doc_obj->add_metadata ($section, $field, $title);
     362            next;
     363            }
     364        }
     365        }
     366       
     367        # if no title use first 100 characters
     368        my $tmptext = $$textref;
     369        $tmptext =~ s/<[^>]*>//g;
     370        my $title = substr ($tmptext, 0, 100);
     371        $title =~ s/\s+/ /gs;
     372        $doc_obj->add_metadata ($section, $field, $title);
     373    }
     374    }
     375}
     376
     377# evaluate any "../" to next directory up
     378# evaluate any "./" as here
     379sub eval_dir_dots {
     380    my $self = shift (@_);
     381    my ($filename) = @_;
    361382
    362383    my $dirsep_os = &util::get_os_dirsep();
     
    364385
    365386    my @eval_dirs = ();
    366     foreach $d (@dirsep)
    367     {
    368     if ($d eq "..")
    369     {
     387    foreach $d (@dirsep) {
     388    if ($d eq "..") {
    370389        pop(@eval_dirs);
    371     }
    372     elsif ($d eq ".")
    373     {
     390
     391    } elsif ($d eq ".") {
    374392        # do nothing!
    375     }
    376     else
    377     {
     393
     394    } else {
    378395        push(@eval_dirs,$d);
    379396    }
     
    383400}
    384401
    385 sub replace_href_links
    386 {
    387     my ($this_filename,$this_oid, $f2d_mapping, $front,$link,$back) = @_;
    388 
    389     return $front.$link.$back if ($link =~ m/\.(gif|jpg|jpeg|png)$/i);
    390 
    391     if ($link =~ m/^(http|ftp|file):/i)
    392     {
    393         # this should really check that the link ends in .htm*
    394     #****
    395     my $http_as_filename = $link;
    396     $http_as_filename =~ s/^(http|ftp|file):\/\///i;
    397 
    398     my ($before_hash,$after_hash)
    399         = ($http_as_filename =~ m/^([^\#]*)\#?(.*)$/);
    400            
    401     if ($link =~ m/^(http|ftp):/i)
    402     {
    403         # Turn url (using /) into file name (possibly using \ on windows)
    404         my @http_dir_split = split('/',$before_hash);
    405         $http_as_filename = &util::filename_cat(@http_dir_split);
    406     }
    407 
    408     $http_as_filename = $self->eval_dir_dots($http_as_filename);
    409                      
    410     if (defined($f2d_mapping->{$http_as_filename}))
    411     {
    412         # transform link into a local link (and then let it be
    413         #   processed by later code)
    414         $link = $http_as_filename;
    415 
    416         $link = "_httpdocument_&cl=_cgiargcl_&d=$oid";
    417         $link .= "#$after_hash" if ($after_hash ne "");
    418     }
    419     else
    420     {
    421         # external link => set it up to pass through off-site page
    422 
    423         my $link_safe = $link;
    424         &html::urlsafe($link_safe);
    425 
    426         $link = "_httpextlink_&href=$link_safe&d=$this_oid";
    427 
    428     }
    429     }   
    430     elsif ($link !~ m/^(mailto|news):/i)
    431     {
    432     my ($before_hash,$after_hash) = ($link =~ m/^([^\#]*)\#?(.*)$/);
    433     my $link_filename;
    434 
    435     if ($before_hash =~ m/^\//)
    436     {
    437         my $dirsep_re = &util::get_re_dirsep();
    438         my $lookup = $self->f2d_lookup_filename($this_filename);
    439         my @lookup_split = split(/$dirsep_re/,$lookup);
    440         my $domname = shift(@lookup_split);
    441         $link_filename = &util::filename_cat($domname,$before_hash);
    442     }
    443     else
    444     {
    445         # Turn relative file path into full path
    446         if ($before_hash eq "") # handle links such as <a href="#x">
    447         {
    448         $link_filename = $this_filename;
    449         }
    450         else
    451         {
    452         my $dirname = &File::Basename::dirname($this_filename);
    453         $link_filename = &util::filename_cat($dirname,$before_hash);
    454         }
    455     }
    456 
    457     $link_filename = $self->eval_dir_dots($link_filename);
    458     my $lookup_filename = $self->f2d_lookup_filename($link_filename);
    459 
    460     my $oid = $f2d_mapping->{$lookup_filename};
    461     if (defined($oid))
    462     {
    463         $link = "_httpdocument_&cl=_cgiargcl_&d=$oid";
    464         $link .= "#$after_hash" if ($after_hash ne "");
    465     }
    466     else
    467     {
    468         print STDERR "HTMLPlug WARNING:";
    469         print STDERR " Could not find link: \"$lookup_filename\". ";
    470         print STDERR " Deactivating link\n";
    471        
    472         $link = "_httpextlink_&d=$this_oid";
    473     }
    474     }
    475     else
    476     {
    477     if ($link !~ m/^(mailto|news):/i)
    478     {
    479         print STDERR "HTMLPlug WARNING: Unhandled type of link, \"$link\"\n";
    480     }
    481     else
    482     {
    483         my $link_safe = $link;
    484         &html::urlsafe($link_safe);
    485 
    486         $link = "_httpextlink_&href=$link_safe&d=$this_oid";
    487     }
    488     }
    489 
    490     my $fixed_tag = "${front}${link}${back}";
    491 
    492     return $fixed_tag;
    493 }
    494 
    495 
    496 sub replace_usemap_links
    497 {
     402sub replace_usemap_links {
     403    my $self = shift (@_);
    498404    my ($front, $link, $back) = @_;
    499405
    500406    $link =~ s/^\.\///;
    501 
    502     return "${front}${link}${back}";
    503 }
    504 
    505 
     407    return $front . $link . $back;
     408}
     409
     410sub inc_filecount {
     411    my $self = shift (@_);
     412
     413    if ($self->{'file_num'} == 1000) {
     414    $self->{'dir_num'} ++;
     415    $self->{'file_num'} = 0;
     416    } else {
     417    $self->{'file_num'} ++;
     418    }
     419}
    506420
    5074211;
    508 
    509 
    510 
    511 
    512 
    513 
    514 
    515 
    516 
    517 
    518 
Note: See TracChangeset for help on using the changeset viewer.