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