[14665] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
| 3 | # HTMLPlug.pm -- basic html plugin
|
---|
| 4 | #
|
---|
| 5 | # A component of the Greenstone digital library software
|
---|
| 6 | # from the New Zealand Digital Library Project at the
|
---|
| 7 | # University of Waikato, New Zealand.
|
---|
| 8 | #
|
---|
| 9 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
| 10 | #
|
---|
| 11 | # This program is free software; you can redistribute it and/or modify
|
---|
| 12 | # it under the terms of the GNU General Public License as published by
|
---|
| 13 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 14 | # (at your option) any later version.
|
---|
| 15 | #
|
---|
| 16 | # This program is distributed in the hope that it will be useful,
|
---|
| 17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | # GNU General Public License for more details.
|
---|
| 20 | #
|
---|
| 21 | # You should have received a copy of the GNU General Public License
|
---|
| 22 | # along with this program; if not, write to the Free Software
|
---|
| 23 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 24 | #
|
---|
| 25 | ###########################################################################
|
---|
| 26 |
|
---|
| 27 | #
|
---|
| 28 | # Note that this plugin handles frames only in a very simple way
|
---|
| 29 | # i.e. each frame is treated as a separate document. This means
|
---|
| 30 | # search results will contain links to individual frames rather
|
---|
| 31 | # than linking to the top level frameset.
|
---|
| 32 | # There may also be some problems caused by the _parent target
|
---|
| 33 | # (it's removed by this plugin)
|
---|
| 34 | #
|
---|
| 35 |
|
---|
| 36 | package HTMLPlug;
|
---|
| 37 |
|
---|
| 38 | use BasPlug;
|
---|
| 39 | use ghtml;
|
---|
| 40 | use unicode;
|
---|
| 41 | use util;
|
---|
| 42 | use XMLParser;
|
---|
| 43 |
|
---|
| 44 | use Image::Size;
|
---|
[14913] | 45 | use File::Copy;
|
---|
[14665] | 46 |
|
---|
| 47 | sub BEGIN {
|
---|
| 48 | @HTMLPlug::ISA = ('BasPlug');
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | use strict; # every perl program should have this!
|
---|
| 52 | no strict 'refs'; # make an exception so we can use variables as filehandles
|
---|
| 53 |
|
---|
| 54 | my $arguments =
|
---|
| 55 | [ { 'name' => "process_exp",
|
---|
| 56 | 'desc' => "{BasPlug.process_exp}",
|
---|
| 57 | 'type' => "regexp",
|
---|
| 58 | 'deft' => &get_default_process_exp() },
|
---|
| 59 | { 'name' => "block_exp",
|
---|
| 60 | 'desc' => "{BasPlug.block_exp}",
|
---|
| 61 | 'type' => 'regexp',
|
---|
| 62 | 'deft' => &get_default_block_exp() },
|
---|
| 63 | { 'name' => "nolinks",
|
---|
| 64 | 'desc' => "{HTMLPlug.nolinks}",
|
---|
| 65 | 'type' => "flag" },
|
---|
| 66 | { 'name' => "keep_head",
|
---|
| 67 | 'desc' => "{HTMLPlug.keep_head}",
|
---|
| 68 | 'type' => "flag" },
|
---|
| 69 | { 'name' => "no_metadata",
|
---|
| 70 | 'desc' => "{HTMLPlug.no_metadata}",
|
---|
| 71 | 'type' => "flag" },
|
---|
| 72 | { 'name' => "metadata_fields",
|
---|
| 73 | 'desc' => "{HTMLPlug.metadata_fields}",
|
---|
| 74 | 'type' => "string",
|
---|
| 75 | 'deft' => "Title" },
|
---|
| 76 | { 'name' => "hunt_creator_metadata",
|
---|
| 77 | 'desc' => "{HTMLPlug.hunt_creator_metadata}",
|
---|
| 78 | 'type' => "flag" },
|
---|
| 79 | { 'name' => "file_is_url",
|
---|
| 80 | 'desc' => "{HTMLPlug.file_is_url}",
|
---|
| 81 | 'type' => "flag" },
|
---|
| 82 | { 'name' => "assoc_files",
|
---|
| 83 | 'desc' => "{HTMLPlug.assoc_files}",
|
---|
| 84 | 'type' => "regexp",
|
---|
| 85 | 'deft' => &get_default_block_exp() },
|
---|
| 86 | { 'name' => "rename_assoc_files",
|
---|
| 87 | 'desc' => "{HTMLPlug.rename_assoc_files}",
|
---|
| 88 | 'type' => "flag" },
|
---|
| 89 | { 'name' => "title_sub",
|
---|
| 90 | 'desc' => "{HTMLPlug.title_sub}",
|
---|
| 91 | 'type' => "string",
|
---|
| 92 | 'deft' => "" },
|
---|
| 93 | { 'name' => "description_tags",
|
---|
| 94 | 'desc' => "{HTMLPlug.description_tags}",
|
---|
| 95 | 'type' => "flag" },
|
---|
| 96 | # retain this for backward compatibility (w3mir option was replaced by
|
---|
| 97 | # file_is_url)
|
---|
| 98 | { 'name' => "w3mir",
|
---|
| 99 | # 'desc' => "{HTMLPlug.w3mir}",
|
---|
| 100 | 'type' => "flag",
|
---|
| 101 | 'hiddengli' => "yes"},
|
---|
| 102 | { 'name' => "no_strip_metadata_html",
|
---|
| 103 | 'desc' => "{HTMLPlug.no_strip_metadata_html}",
|
---|
| 104 | 'type' => "string",
|
---|
| 105 | 'deft' => "",
|
---|
| 106 | 'reqd' => "no"},
|
---|
| 107 | { 'name' => "sectionalise_using_h_tags",
|
---|
| 108 | 'desc' => "{HTMLPlug.sectionalise_using_h_tags}",
|
---|
| 109 | 'type' => "flag" },
|
---|
[14913] | 110 | { 'name' => "use_realistic_book",
|
---|
[14665] | 111 | 'desc' => "{HTMLPlug.tidy_html}",
|
---|
| 112 | 'type' => "flag"},
|
---|
[14913] | 113 | { 'name' => "is_old_HDL_tags",
|
---|
[14665] | 114 | 'desc' => "{HTMLPlug.old_style_HDL}",
|
---|
| 115 | 'type' => "flag"}
|
---|
| 116 | ];
|
---|
| 117 |
|
---|
| 118 | my $options = { 'name' => "HTMLPlug",
|
---|
| 119 | 'desc' => "{HTMLPlug.desc}",
|
---|
| 120 | 'abstract' => "no",
|
---|
| 121 | 'inherits' => "yes",
|
---|
| 122 | 'args' => $arguments };
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 | sub HB_read_html_file {
|
---|
| 126 | my $self = shift (@_);
|
---|
| 127 | my ($htmlfile, $text) = @_;
|
---|
| 128 |
|
---|
| 129 | # load in the file
|
---|
| 130 | if (!open (FILE, $htmlfile)) {
|
---|
| 131 | print STDERR "ERROR - could not open $htmlfile\n";
|
---|
| 132 | return;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | my $foundbody = 0;
|
---|
| 136 | $self->HB_gettext (\$foundbody, $text, "FILE");
|
---|
| 137 | close FILE;
|
---|
| 138 |
|
---|
| 139 | # just in case there was no <body> tag
|
---|
| 140 | if (!$foundbody) {
|
---|
| 141 | $foundbody = 1;
|
---|
| 142 | open (FILE, $htmlfile) || return;
|
---|
| 143 | $self->HB_gettext (\$foundbody, $text, "FILE");
|
---|
| 144 | close FILE;
|
---|
| 145 | }
|
---|
| 146 | # text is in utf8
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | # converts the text to utf8, as ghtml does that for é etc.
|
---|
| 150 | sub HB_gettext {
|
---|
| 151 | my $self = shift (@_);
|
---|
| 152 | my ($foundbody, $text, $handle) = @_;
|
---|
| 153 |
|
---|
| 154 | my $line = "";
|
---|
| 155 | while (defined ($line = <$handle>)) {
|
---|
| 156 | # look for body tag
|
---|
| 157 | if (!$$foundbody) {
|
---|
| 158 | if ($line =~ s/^.*<body[^>]*>//i) {
|
---|
| 159 | $$foundbody = 1;
|
---|
| 160 | } else {
|
---|
| 161 | next;
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | # check for symbol fonts
|
---|
| 166 | if ($line =~ /<font [^>]*?face\s*=\s*\"?(\w+)\"?/i) {
|
---|
| 167 | my $font = $1;
|
---|
| 168 | print STDERR "HBPlug::HB_gettext - warning removed font $font\n"
|
---|
| 169 | if ($font !~ /^arial$/i);
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | $$text .= $line;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | if ($self->{'input_encoding'} eq "iso_8859_1") {
|
---|
| 176 | # convert to utf-8
|
---|
| 177 | $$text=&unicode::unicode2utf8(&unicode::convert2unicode("iso_8859_1", $text));
|
---|
| 178 | }
|
---|
| 179 | # convert any alphanumeric character entities to their utf-8
|
---|
| 180 | # equivalent for indexing purposes
|
---|
| 181 | #&ghtml::convertcharentities ($$text);
|
---|
| 182 |
|
---|
| 183 | $$text =~ s/\s+/ /g; # remove \n's
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | sub HB_clean_section {
|
---|
| 187 | my $self = shift (@_);
|
---|
| 188 | my ($section) = @_;
|
---|
| 189 |
|
---|
| 190 | # remove tags without a starting tag from the section
|
---|
| 191 | my ($tag, $tagstart);
|
---|
| 192 | while ($section =~ /<\/([^>]{1,10})>/) {
|
---|
| 193 | $tag = $1;
|
---|
| 194 | $tagstart = index($section, "<$tag");
|
---|
| 195 | last if (($tagstart >= 0) && ($tagstart < index($section, "<\/$tag")));
|
---|
| 196 | $section =~ s/<\/$tag>//;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | # remove extra paragraph tags
|
---|
| 200 | while ($section =~ s/<p\b[^>]*>\s*<p\b/<p/ig) {}
|
---|
| 201 |
|
---|
| 202 | # remove extra stuff at the end of the section
|
---|
| 203 | while ($section =~ s/(<u>|<i>|<b>|<p\b[^>]*>| |\s)$//i) {}
|
---|
| 204 |
|
---|
| 205 | # add a newline at the beginning of each paragraph
|
---|
| 206 | $section =~ s/(.)\s*<p\b/$1\n\n<p/gi;
|
---|
| 207 |
|
---|
| 208 | # add a newline every 80 characters at a word boundary
|
---|
| 209 | # Note: this regular expression puts a line feed before
|
---|
| 210 | # the last word in each section, even when it is not
|
---|
| 211 | # needed.
|
---|
| 212 | $section =~ s/(.{1,80})\s/$1\n/g;
|
---|
| 213 |
|
---|
| 214 | # fix up the image links
|
---|
| 215 | $section =~ s/<img[^>]*?src=\"?([^\">]+)\"?[^>]*>/
|
---|
| 216 | <center><img src=\"$1\" \/><\/center><br\/>/ig;
|
---|
| 217 | $section =~ s/<<I>>\s*([^\.]+\.(png|jpg|gif))/
|
---|
| 218 | <center><img src=\"$1\" \/><\/center><br\/>/ig;
|
---|
| 219 |
|
---|
| 220 | return $section;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | # Will convert the oldHDL format to the new HDL format (using the Section tag)
|
---|
| 224 | sub convert_to_newHDLformat
|
---|
| 225 | {
|
---|
| 226 | my $self = shift (@_);
|
---|
| 227 | my ($file,$cnfile) = @_;
|
---|
| 228 | my $input_filename = $file;
|
---|
| 229 | my $tmp_filename = $cnfile;
|
---|
| 230 |
|
---|
| 231 | # write HTML tmp file with new HDL format
|
---|
| 232 | open (PROD, ">$tmp_filename") || die("Error Writing to File: $tmp_filename $!");
|
---|
| 233 |
|
---|
| 234 | # read in the file and do basic html cleaning (removing header etc)
|
---|
| 235 | my $html = "";
|
---|
| 236 | $self->HB_read_html_file ($input_filename, \$html);
|
---|
| 237 |
|
---|
| 238 | # process the file one section at a time
|
---|
| 239 | my $curtoclevel = 1;
|
---|
| 240 | my $firstsection = 1;
|
---|
| 241 | my $toclevel = 0;
|
---|
| 242 | while (length ($html) > 0) {
|
---|
| 243 | if ($html =~ s/^.*?(?:<p\b[^>]*>)?((<b>|<i>|<u>|\s)*)<<TOC(\d+)>>\s*(.*?)<p\b/<p/i) {
|
---|
| 244 | $toclevel = $3;
|
---|
| 245 | my $title = $4;
|
---|
| 246 | my $sectiontext = "";
|
---|
| 247 | if ($html =~ s/^(.*?)((?:<p\b[^>]*>)?((<b>|<i>|<u>|\s)*)<<TOC\d+>>)/$2/i) {
|
---|
| 248 | $sectiontext = $1;
|
---|
| 249 | } else {
|
---|
| 250 | $sectiontext = $html;
|
---|
| 251 | $html = "";
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | # remove tags and extra spaces from the title
|
---|
| 255 | $title =~ s/<\/?[^>]+>//g;
|
---|
| 256 | $title =~ s/^\s+|\s+$//g;
|
---|
| 257 |
|
---|
| 258 | # close any sections below the current level and
|
---|
| 259 | # create a new section (special case for the firstsection)
|
---|
| 260 | print PROD "<!--\n";
|
---|
| 261 | while (($curtoclevel > $toclevel) ||
|
---|
| 262 | (!$firstsection && $curtoclevel == $toclevel)) {
|
---|
| 263 | $curtoclevel--;
|
---|
| 264 | print PROD "</Section>\n";
|
---|
| 265 | }
|
---|
| 266 | if ($curtoclevel+1 < $toclevel) {
|
---|
| 267 | print STDERR "WARNING - jump in toc levels in $input_filename " .
|
---|
| 268 | "from $curtoclevel to $toclevel\n";
|
---|
| 269 | }
|
---|
| 270 | while ($curtoclevel < $toclevel) {
|
---|
| 271 | $curtoclevel++;
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | if ($curtoclevel == 1) {
|
---|
| 275 | # add the header tag
|
---|
| 276 | print PROD "-->\n";
|
---|
| 277 | print PROD "<HTML>\n<HEAD>\n<TITLE>$title</TITLE>\n</HEAD>\n<BODY>\n";
|
---|
| 278 | print PROD "<!--\n";
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | print PROD "<Section>\n\t<Description>\n\t\t<Metadata name=\"Title\">$title</Metadata>\n\t</Description>\n";
|
---|
| 282 |
|
---|
| 283 | print PROD "-->\n";
|
---|
| 284 |
|
---|
| 285 | # clean up the section html
|
---|
| 286 | $sectiontext = $self->HB_clean_section($sectiontext);
|
---|
| 287 |
|
---|
| 288 | print PROD "$sectiontext\n";
|
---|
| 289 |
|
---|
| 290 | } else {
|
---|
| 291 | print STDERR "WARNING - leftover text\n" , $self->shorten($html),
|
---|
| 292 | "\nin $input_filename\n";
|
---|
| 293 | last;
|
---|
| 294 | }
|
---|
| 295 | $firstsection = 0;
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | print PROD "<!--\n";
|
---|
| 299 | while ($curtoclevel > 0) {
|
---|
| 300 | $curtoclevel--;
|
---|
| 301 | print PROD "</Section>\n";
|
---|
| 302 | }
|
---|
| 303 | print PROD "-->\n";
|
---|
| 304 |
|
---|
| 305 | close (PROD) || die("Error Closing File: $tmp_filename $!");
|
---|
| 306 |
|
---|
| 307 | return $tmp_filename;
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | sub shorten {
|
---|
| 311 | my $self = shift (@_);
|
---|
| 312 | my ($text) = @_;
|
---|
| 313 |
|
---|
| 314 | return "\"$text\"" if (length($text) < 100);
|
---|
| 315 |
|
---|
| 316 | return "\"" . substr ($text, 0, 50) . "\" ... \"" .
|
---|
| 317 | substr ($text, length($text)-50) . "\"";
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | sub convert_tidy_or_oldHDL_file
|
---|
| 321 | {
|
---|
| 322 | my $self = shift (@_);
|
---|
| 323 | my ($file) = @_;
|
---|
| 324 | my $input_filename = $file;
|
---|
| 325 |
|
---|
| 326 | if (-d $input_filename)
|
---|
| 327 | {
|
---|
| 328 | return $input_filename;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | # get the input filename
|
---|
| 332 | my ($tailname, $dirname, $suffix) = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
|
---|
| 333 | my $base_dirname = $dirname;
|
---|
| 334 | $suffix = lc($suffix);
|
---|
| 335 |
|
---|
| 336 | # derive tmp filename from input filename
|
---|
| 337 | # Remove any white space from filename -- no risk of name collision, and
|
---|
| 338 | # makes later conversion by utils simpler. Leave spaces in path...
|
---|
| 339 | # tidy up the filename with space, dot, hyphen between
|
---|
| 340 | $tailname =~ s/\s+//g;
|
---|
| 341 | $tailname =~ s/\.+//g;
|
---|
| 342 | $tailname =~ s/\-+//g;
|
---|
| 343 | # convert to utf-8 otherwise we have problems with the doc.xml file
|
---|
| 344 | # later on
|
---|
| 345 | &unicode::ensure_utf8(\$tailname);
|
---|
| 346 |
|
---|
| 347 | # softlink to collection tmp dir
|
---|
| 348 | my $tmp_dirname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "tidytmp");
|
---|
| 349 | &util::mk_dir($tmp_dirname) if (!-e $tmp_dirname);
|
---|
| 350 |
|
---|
| 351 | my $test_dirname = "";
|
---|
[14913] | 352 | my $f_separator = &util::get_os_dirsep();
|
---|
| 353 |
|
---|
| 354 | if ($dirname =~ /import$f_separator/)
|
---|
[14665] | 355 | {
|
---|
| 356 | $test_dirname = $';
|
---|
| 357 |
|
---|
| 358 | #print STDERR "init $'\n";
|
---|
| 359 |
|
---|
[14913] | 360 | while ($test_dirname =~ /[$f_separator]/)
|
---|
[14665] | 361 | {
|
---|
| 362 | my $folderdirname = $`;
|
---|
| 363 | $tmp_dirname = &util::filename_cat($tmp_dirname,$folderdirname);
|
---|
| 364 | &util::mk_dir($tmp_dirname) if (!-e $tmp_dirname);
|
---|
| 365 | $test_dirname = $';
|
---|
| 366 | }
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | my $tmp_filename = &util::filename_cat($tmp_dirname, "$tailname$suffix");
|
---|
| 370 |
|
---|
| 371 | # tidy or convert the input file if it is a HTML-like file or it is accepted by the process_exp
|
---|
| 372 | if (($suffix eq ".htm") || ($suffix eq ".html") || ($suffix eq ".shtml"))
|
---|
| 373 | {
|
---|
| 374 | #convert the input file to a new style HDL
|
---|
| 375 | my $hdl_output_filename = $input_filename;
|
---|
| 376 | if ($self->{'old_style_HDL'})
|
---|
| 377 | {
|
---|
| 378 | $hdl_output_filename = &util::filename_cat($tmp_dirname, "$tailname$suffix");
|
---|
| 379 | $hdl_output_filename = $self->convert_to_newHDLformat($input_filename,$hdl_output_filename);
|
---|
| 380 | }
|
---|
| 381 |
|
---|
| 382 | #just for checking copy all other file from the base dir to tmp dir if it is not exists
|
---|
| 383 | opendir(DIR,$base_dirname) or die "Can't open base directory : $base_dirname!";
|
---|
| 384 | my @files = grep {!/^\.+$/} readdir(DIR);
|
---|
| 385 | close(DIR);
|
---|
| 386 |
|
---|
| 387 | foreach my $file (@files)
|
---|
| 388 | {
|
---|
| 389 | my $src_file = &util::filename_cat($base_dirname,$file);
|
---|
| 390 | my $dest_file = &util::filename_cat($tmp_dirname,$file);
|
---|
| 391 | if ((!-e $dest_file) && (!-d $src_file))
|
---|
| 392 | {
|
---|
| 393 | # just copy the original file back to the tmp directory
|
---|
[14913] | 394 | copy($src_file,$dest_file) or die "Can't copy file $src_file to $dest_file $!";
|
---|
[14665] | 395 | }
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | # tidy the input file
|
---|
| 399 | my $tidy_output_filename = $hdl_output_filename;
|
---|
| 400 | if ($self->{'tidy_html'})
|
---|
| 401 | {
|
---|
| 402 | $tidy_output_filename = &util::filename_cat($tmp_dirname, "$tailname$suffix");
|
---|
| 403 | $tidy_output_filename = $self->tmp_tidy_file($hdl_output_filename,$tidy_output_filename);
|
---|
| 404 | }
|
---|
| 405 | $tmp_filename = $tidy_output_filename;
|
---|
| 406 | }
|
---|
| 407 | else
|
---|
| 408 | {
|
---|
| 409 | if (!-e $tmp_filename)
|
---|
| 410 | {
|
---|
| 411 | # just copy the original file back to the tmp directory
|
---|
[14913] | 412 | copy($input_filename,$tmp_filename) or die "Can't copy file $input_filename to $tmp_filename $!";
|
---|
[14665] | 413 | }
|
---|
| 414 | }
|
---|
| 415 |
|
---|
| 416 | return $tmp_filename;
|
---|
| 417 | }
|
---|
| 418 |
|
---|
| 419 |
|
---|
| 420 | # Will make the html input file as a proper XML file with removed font tag and
|
---|
| 421 | # image size added to the img tag.
|
---|
| 422 | # The tidying process takes place in a collection specific 'tmp' directory so
|
---|
| 423 | # that we don't accidentally damage the input.
|
---|
| 424 | sub tmp_tidy_file
|
---|
| 425 | {
|
---|
| 426 | my $self = shift (@_);
|
---|
| 427 | my ($file,$cnfile) = @_;
|
---|
| 428 | my $input_filename = $file;
|
---|
| 429 | my $tmp_filename = $cnfile;
|
---|
| 430 |
|
---|
| 431 | # get the input filename
|
---|
| 432 | my ($tailname, $dirname, $suffix) = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
|
---|
| 433 |
|
---|
| 434 | require HTML::TokeParser::Simple;
|
---|
| 435 |
|
---|
| 436 | # create HTML parser to decode the input file
|
---|
| 437 | my $parser = HTML::TokeParser::Simple->new($input_filename);
|
---|
| 438 |
|
---|
| 439 | # write HTML tmp file without the font tag and image size are added to the img tag
|
---|
| 440 | open (PROD, ">$tmp_filename") || die("Error Writing to File: $tmp_filename $!");
|
---|
| 441 | while (my $token = $parser->get_token())
|
---|
| 442 | {
|
---|
| 443 | # is it an img tag
|
---|
| 444 | if ($token->is_start_tag('img'))
|
---|
| 445 | {
|
---|
| 446 | # get the attributes
|
---|
| 447 | my $attr = $token->return_attr;
|
---|
| 448 |
|
---|
| 449 | # get the full path to the image
|
---|
| 450 | my $img_file = &util::filename_cat($dirname,$attr->{src});
|
---|
| 451 |
|
---|
| 452 | # set the width and height attribute
|
---|
| 453 | ($attr->{width}, $attr->{height}) = imgsize($img_file);
|
---|
| 454 |
|
---|
| 455 | # recreate the tag
|
---|
| 456 | print PROD "<img";
|
---|
| 457 | print PROD map { qq { $_="$attr->{$_}"} } keys %$attr;
|
---|
| 458 | print PROD ">";
|
---|
| 459 | }
|
---|
| 460 | # is it a font tag
|
---|
| 461 | else
|
---|
| 462 | {
|
---|
| 463 | if (($token->is_start_tag('font')) || ($token->is_end_tag('font')))
|
---|
| 464 | {
|
---|
| 465 | # remove font tag
|
---|
| 466 | print PROD "";
|
---|
| 467 | }
|
---|
| 468 | else
|
---|
| 469 | {
|
---|
| 470 | # print without changes
|
---|
| 471 | print PROD $token->as_is;
|
---|
| 472 | }
|
---|
| 473 | }
|
---|
| 474 | }
|
---|
| 475 | close (PROD) || die("Error Closing File: $tmp_filename $!");
|
---|
| 476 |
|
---|
| 477 | # run html-tidy on the tmp file to make it a proper XML file
|
---|
[14913] | 478 | my $tidyfile = `tidy -utf8 -wrap 0 -asxml $tmp_filename`;
|
---|
[14665] | 479 |
|
---|
| 480 | # write result back to the tmp file
|
---|
| 481 | open (PROD, ">$tmp_filename") || die("Error Writing to File: $tmp_filename $!");
|
---|
| 482 | print PROD $tidyfile;
|
---|
| 483 | close (PROD) || die("Error Closing File: $tmp_filename $!");
|
---|
| 484 |
|
---|
| 485 | # return the output filename
|
---|
| 486 | return $tmp_filename;
|
---|
| 487 | }
|
---|
| 488 |
|
---|
| 489 | sub read_into_doc_obj
|
---|
| 490 | {
|
---|
| 491 | my $self = shift (@_);
|
---|
| 492 | my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
|
---|
| 493 |
|
---|
| 494 | # check the process_exp and block_exp thing
|
---|
| 495 | my ($block_status,$filename) = $self->read_block(@_);
|
---|
| 496 | return $block_status if ((!defined $block_status) || ($block_status==0));
|
---|
| 497 |
|
---|
| 498 | # get the input file
|
---|
| 499 | my $input_filename = $file;
|
---|
| 500 | my ($tailname, $dirname, $suffix) = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
|
---|
| 501 | $suffix = lc($suffix);
|
---|
| 502 |
|
---|
| 503 | if (($self->{'tidy_html'}) || ($self->{'old_style_HDL'}))
|
---|
| 504 | {
|
---|
[14913] | 505 | # because the document has to be sectionalized set the description tags
|
---|
| 506 | $self->{'description_tags'} = 1;
|
---|
| 507 |
|
---|
[14665] | 508 | # set the file to be tidied
|
---|
| 509 | $input_filename = &util::filename_cat($base_dir,$file) if $base_dir =~ /\w/;
|
---|
| 510 |
|
---|
| 511 | # get the tidied file
|
---|
| 512 | #my $tidy_filename = $self->tmp_tidy_file($input_filename);
|
---|
| 513 | my $tidy_filename = $self->convert_tidy_or_oldHDL_file($input_filename);
|
---|
| 514 |
|
---|
| 515 | # derive tmp filename from input filename
|
---|
| 516 | my ($tailname, $dirname, $suffix) = &File::Basename::fileparse($tidy_filename, "\\.[^\\.]+\$");
|
---|
| 517 |
|
---|
| 518 | # set the new input file and base_dir to be from the tidied file
|
---|
| 519 | $file = "$tailname$suffix";
|
---|
| 520 | $base_dir = $dirname;
|
---|
| 521 | }
|
---|
| 522 |
|
---|
| 523 | # call the parent read_into_doc_obj
|
---|
| 524 | my ($process_status,$doc_obj) = &BasPlug::read_into_doc_obj($self,$pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli);
|
---|
| 525 |
|
---|
| 526 | return ($process_status,$doc_obj);
|
---|
| 527 | }
|
---|
| 528 |
|
---|
| 529 | sub new {
|
---|
| 530 | my ($class) = shift (@_);
|
---|
| 531 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
| 532 | push(@$pluginlist, $class);
|
---|
| 533 |
|
---|
| 534 | if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
|
---|
| 535 | if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
|
---|
| 536 |
|
---|
| 537 |
|
---|
| 538 | my $self = (defined $hashArgOptLists)? new BasPlug($pluginlist,$inputargs,$hashArgOptLists): new BasPlug($pluginlist,$inputargs);
|
---|
| 539 |
|
---|
| 540 | if ($self->{'w3mir'}) {
|
---|
| 541 | $self->{'file_is_url'} = 1;
|
---|
| 542 | }
|
---|
| 543 | $self->{'aux_files'} = {};
|
---|
| 544 | $self->{'dir_num'} = 0;
|
---|
| 545 | $self->{'file_num'} = 0;
|
---|
| 546 |
|
---|
| 547 | return bless $self, $class;
|
---|
| 548 | }
|
---|
| 549 |
|
---|
| 550 | # may want to use (?i)\.(gif|jpe?g|jpe|png|css|js(?:@.*)?)$
|
---|
| 551 | # if have eg <script language="javascript" src="img/lib.js@123">
|
---|
| 552 | sub get_default_block_exp {
|
---|
| 553 | my $self = shift (@_);
|
---|
| 554 |
|
---|
| 555 | return q^(?i)\.(gif|jpe?g|jpe|jpg|png|css)$^;
|
---|
| 556 | }
|
---|
| 557 |
|
---|
| 558 | sub get_default_process_exp {
|
---|
| 559 | my $self = shift (@_);
|
---|
| 560 |
|
---|
| 561 | # the last option is an attempt to encode the concept of an html query ...
|
---|
| 562 | return q^(?i)(\.html?|\.shtml|\.shm|\.asp|\.php\d?|\.cgi|.+\?.+=.*)$^;
|
---|
| 563 | }
|
---|
| 564 |
|
---|
| 565 | sub store_block_files
|
---|
| 566 | {
|
---|
| 567 | my $self =shift (@_);
|
---|
| 568 | my ($filename) = @_;
|
---|
| 569 | my $html_fname = $filename;
|
---|
| 570 | my @file_blocks;
|
---|
| 571 |
|
---|
| 572 | my ($language, $encoding) = $self->textcat_get_language_encoding ($filename);
|
---|
| 573 |
|
---|
| 574 | # read in file ($text will be in utf8)
|
---|
| 575 | my $text = "";
|
---|
| 576 | $self->read_file ($filename, $encoding, $language, \$text);
|
---|
| 577 | my $textref = \$text;
|
---|
| 578 | my $opencom = '(?:<!--|<!(?:—|—|--))';
|
---|
| 579 | my $closecom = '(?:-->|(?:—|—|--)>)';
|
---|
| 580 | $$textref =~ s/$opencom(.*?)$closecom//gs;
|
---|
| 581 |
|
---|
| 582 | my $attval = "\\\"[^\\\"]+\\\"|[^\\s>]+";
|
---|
| 583 | my @img_matches = ($$textref =~ m/<img[^>]*?src\s*=\s*($attval)[^>]*>/igs);
|
---|
| 584 | my @usemap_matches = ($$textref =~ m/<img[^>]*?usemap\s*=\s*($attval)[^>]*>/igs);
|
---|
| 585 | my @link_matches = ($$textref =~ m/<link[^>]*?href\s*=\s*($attval)[^>]*>/igs);
|
---|
| 586 | my @embed_matches = ($$textref =~ m/<embed[^>]*?src\s*=\s*($attval)[^>]*>/igs);
|
---|
| 587 | my @tabbg_matches = ($$textref =~ m/<(?:table|tr|td)[^>]*?background\s*=\s*($attval)[^>]*>/igs);
|
---|
| 588 |
|
---|
| 589 | foreach my $link (@img_matches, @usemap_matches, @link_matches, @embed_matches, @tabbg_matches) {
|
---|
| 590 |
|
---|
| 591 | # remove quotes from link at start and end if necessary
|
---|
| 592 | if ($link=~/^\"/) {
|
---|
| 593 | $link=~s/^\"//;
|
---|
| 594 | $link=~s/\"$//;
|
---|
| 595 | }
|
---|
| 596 |
|
---|
| 597 | $link =~ s/\#.*$//s; # remove any anchor names, e.g. foo.html#name becomes foo.html
|
---|
| 598 |
|
---|
| 599 | if ($link !~ m@^/@ && $link !~ m/^([A-Z]:?)\\/) {
|
---|
| 600 | # Turn relative file path into full path
|
---|
| 601 | my $dirname = &File::Basename::dirname($filename);
|
---|
| 602 | $link = &util::filename_cat($dirname, $link);
|
---|
| 603 | }
|
---|
| 604 | $link = $self->eval_dir_dots($link);
|
---|
| 605 |
|
---|
| 606 | $self->{'file_blocks'}->{$link} = 1;
|
---|
| 607 | }
|
---|
| 608 | }
|
---|
| 609 |
|
---|
| 610 |
|
---|
| 611 | # do plugin specific processing of doc_obj
|
---|
| 612 | sub process {
|
---|
| 613 | my $self = shift (@_);
|
---|
| 614 | my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
|
---|
| 615 | my $outhandle = $self->{'outhandle'};
|
---|
| 616 |
|
---|
| 617 | print STDERR "<Processing n='$file' p='HTMLPlug'>\n" if ($gli);
|
---|
| 618 |
|
---|
| 619 | print $outhandle "HTMLPlug: processing $file\n"
|
---|
| 620 | if $self->{'verbosity'} > 1;
|
---|
| 621 |
|
---|
| 622 | if ($ENV{'GSDLOS'} =~ /^windows/i) {
|
---|
| 623 | # this makes life so much easier... perl can cope with unix-style '/'s.
|
---|
| 624 | $base_dir =~ s@(\\)+@/@g;
|
---|
| 625 | $file =~ s@(\\)+@/@g;
|
---|
| 626 | }
|
---|
| 627 |
|
---|
| 628 | # reset per-doc stuff...
|
---|
| 629 | $self->{'aux_files'} = {};
|
---|
| 630 | $self->{'dir_num'} = 0;
|
---|
| 631 | $self->{'file_num'} = 0;
|
---|
| 632 |
|
---|
| 633 | # process an HTML file where sections are divided by headings tags (H1, H2 ...)
|
---|
| 634 | # you can also include metadata in the format (X can be any number)
|
---|
| 635 | # <hX>Title<!--gsdl-metadata
|
---|
| 636 | # <Metadata name="name1">value1</Metadata>
|
---|
| 637 | # ...
|
---|
| 638 | # <Metadata name="nameN">valueN</Metadata>
|
---|
| 639 | #--></hX>
|
---|
| 640 | if ($self->{'sectionalise_using_h_tags'}) {
|
---|
| 641 | # description_tags should allways be activated because we convert headings to description tags
|
---|
| 642 | $self->{'description_tags'} = 1;
|
---|
| 643 |
|
---|
| 644 | my $arrSections = [];
|
---|
| 645 | $$textref =~ s/<h([0-9]+)[^>]*>(.*?)<\/h[0-9]+>/$self->process_heading($1, $2, $arrSections, $file)/isge;
|
---|
| 646 |
|
---|
| 647 | if (scalar(@$arrSections)) {
|
---|
| 648 | my $strMetadata = $self->update_section_data($arrSections, -1);
|
---|
| 649 | if (length($strMetadata)) {
|
---|
| 650 | $strMetadata = '<!--' . $strMetadata . "\n-->\n</body>";
|
---|
| 651 | $$textref =~ s/<\/body>/$strMetadata/ig;
|
---|
| 652 | }
|
---|
| 653 | }
|
---|
| 654 | }
|
---|
| 655 |
|
---|
| 656 | my $cursection = $doc_obj->get_top_section();
|
---|
| 657 |
|
---|
| 658 | $self->extract_metadata ($textref, $metadata, $doc_obj, $cursection)
|
---|
| 659 | unless $self->{'no_metadata'} || $self->{'description_tags'};
|
---|
| 660 |
|
---|
| 661 | # Store URL for page as metadata - this can be used for an
|
---|
| 662 | # altavista style search interface. The URL won't be valid
|
---|
| 663 | # unless the file structure contains the domain name (i.e.
|
---|
| 664 | # like when w3mir is used to download a website).
|
---|
| 665 |
|
---|
| 666 | # URL metadata (even invalid ones) are used to support internal
|
---|
| 667 | # links, so even if 'file_is_url' is off, still need to store info
|
---|
| 668 |
|
---|
| 669 | my $web_url = "http://$file";
|
---|
| 670 | $doc_obj->add_metadata($cursection, "URL", $web_url);
|
---|
| 671 |
|
---|
| 672 | if ($self->{'file_is_url'}) {
|
---|
| 673 | $doc_obj->add_metadata($cursection, "weblink", "<a href=\"$web_url\">");
|
---|
| 674 | $doc_obj->add_metadata($cursection, "webicon", "_iconworld_");
|
---|
| 675 | $doc_obj->add_metadata($cursection, "/weblink", "</a>");
|
---|
| 676 | }
|
---|
| 677 |
|
---|
| 678 | if ($self->{'description_tags'}) {
|
---|
| 679 | # remove the html header - note that doing this here means any
|
---|
| 680 | # sections defined within the header will be lost (so all <Section>
|
---|
| 681 | # tags must appear within the body of the HTML)
|
---|
| 682 | my ($head_keep) = ($$textref =~ m/^(.*?)<body[^>]*>/is);
|
---|
| 683 |
|
---|
| 684 | $$textref =~ s/^.*?<body[^>]*>//is;
|
---|
| 685 | $$textref =~ s/(<\/body[^>]*>|<\/html[^>]*>)//isg;
|
---|
| 686 |
|
---|
| 687 | my $opencom = '(?:<!--|<!(?:—|—|--))';
|
---|
| 688 | my $closecom = '(?:-->|(?:—|—|--)>)';
|
---|
| 689 |
|
---|
| 690 | my $lt = '(?:<|<)';
|
---|
| 691 | my $gt = '(?:>|>)';
|
---|
| 692 | my $quot = '(?:"|"|”|“)';
|
---|
| 693 |
|
---|
| 694 | my $dont_strip = '';
|
---|
| 695 | if ($self->{'no_strip_metadata_html'}) {
|
---|
| 696 | ($dont_strip = $self->{'no_strip_metadata_html'}) =~ s{,}{|}g;
|
---|
| 697 | }
|
---|
| 698 |
|
---|
| 699 | my $found_something = 0; my $top = 1;
|
---|
| 700 | while ($$textref =~ s/^(.*?)$opencom(.*?)$closecom//s) {
|
---|
| 701 | my $text = $1;
|
---|
| 702 | my $comment = $2;
|
---|
| 703 | if (defined $text) {
|
---|
| 704 | # text before a comment - note that getting to here
|
---|
| 705 | # doesn't necessarily mean there are Section tags in
|
---|
| 706 | # the document
|
---|
| 707 | $self->process_section(\$text, $base_dir, $file, $doc_obj, $cursection);
|
---|
| 708 | }
|
---|
| 709 | while ($comment =~ s/$lt(.*?)$gt//s) {
|
---|
| 710 | my $tag = $1;
|
---|
| 711 | if ($tag eq "Section") {
|
---|
| 712 | $found_something = 1;
|
---|
| 713 | $cursection = $doc_obj->insert_section($doc_obj->get_end_child($cursection)) unless $top;
|
---|
| 714 | $top = 0;
|
---|
| 715 | } elsif ($tag eq "/Section") {
|
---|
| 716 | $found_something = 1;
|
---|
| 717 | $cursection = $doc_obj->get_parent_section ($cursection);
|
---|
| 718 | } elsif ($tag =~ /^Metadata name=$quot(.*?)$quot/s) {
|
---|
| 719 | my $metaname = $1;
|
---|
| 720 | my $accumulate = $tag =~ /mode=${quot}accumulate${quot}/ ? 1 : 0;
|
---|
| 721 | $comment =~ s/^(.*?)$lt\/Metadata$gt//s;
|
---|
| 722 | my $metavalue = $1;
|
---|
| 723 | $metavalue =~ s/^\s+//;
|
---|
| 724 | $metavalue =~ s/\s+$//;
|
---|
| 725 | # assume that no metadata value intentionally includes
|
---|
| 726 | # carriage returns or HTML tags (if they're there they
|
---|
| 727 | # were probably introduced when converting to HTML from
|
---|
| 728 | # some other format).
|
---|
| 729 | # actually some people want to have html tags in their
|
---|
| 730 | # metadata.
|
---|
| 731 | $metavalue =~ s/[\cJ\cM]/ /sg;
|
---|
| 732 | $metavalue =~ s/<[^>]+>//sg
|
---|
| 733 | unless $dont_strip && ($dont_strip eq 'all' || $metaname =~ /^($dont_strip)$/);
|
---|
| 734 | $metavalue =~ s/\s+/ /sg;
|
---|
| 735 | if ($accumulate) {
|
---|
| 736 | $doc_obj->add_utf8_metadata($cursection, $metaname, $metavalue);
|
---|
| 737 | } else {
|
---|
| 738 | $doc_obj->set_utf8_metadata_element($cursection, $metaname, $metavalue);
|
---|
| 739 | }
|
---|
| 740 | } elsif ($tag eq "Description" || $tag eq "/Description") {
|
---|
| 741 | # do nothing with containing Description tags
|
---|
| 742 | } else {
|
---|
| 743 | # simple HTML tag (probably created by the conversion
|
---|
| 744 | # to HTML from some other format) - we'll ignore it and
|
---|
| 745 | # hope for the best ;-)
|
---|
| 746 | }
|
---|
| 747 | }
|
---|
| 748 | }
|
---|
| 749 | if ($cursection ne "") {
|
---|
| 750 | print $outhandle "HTMLPlug: WARNING: $file contains unmatched <Section></Section> tags\n";
|
---|
| 751 | }
|
---|
| 752 |
|
---|
| 753 | $$textref =~ s/^.*?<body[^>]*>//is;
|
---|
| 754 | $$textref =~ s/(<\/body[^>]*>|<\/html[^>]*>)//isg;
|
---|
| 755 | if ($$textref =~ /\S/) {
|
---|
| 756 | if (!$found_something) {
|
---|
| 757 | if ($self->{'verbosity'} > 2) {
|
---|
| 758 | print $outhandle "HTMLPlug: WARNING: $file appears to contain no Section tags so\n";
|
---|
| 759 | print $outhandle " will be processed as a single section document\n";
|
---|
| 760 | }
|
---|
| 761 |
|
---|
| 762 | # go ahead and process single-section document
|
---|
| 763 | $self->process_section($textref, $base_dir, $file, $doc_obj, $cursection);
|
---|
| 764 |
|
---|
| 765 | # if document contains no Section tags we'll go ahead
|
---|
| 766 | # and extract metadata (this won't have been done
|
---|
| 767 | # above as the -description_tags option prevents it)
|
---|
| 768 | my $complete_text = $head_keep.$doc_obj->get_text($cursection);
|
---|
| 769 | $self->extract_metadata (\$complete_text, $metadata, $doc_obj, $cursection)
|
---|
| 770 | unless $self->{'no_metadata'};
|
---|
| 771 |
|
---|
| 772 | } else {
|
---|
| 773 | print $outhandle "HTMLPlug: WARNING: $file contains the following text outside\n";
|
---|
| 774 | print $outhandle " of the final closing </Section> tag. This text will\n";
|
---|
| 775 | print $outhandle " be ignored.";
|
---|
| 776 |
|
---|
| 777 | my ($text);
|
---|
| 778 | if (length($$textref) > 30) {
|
---|
| 779 | $text = substr($$textref, 0, 30) . "...";
|
---|
| 780 | } else {
|
---|
| 781 | $text = $$textref;
|
---|
| 782 | }
|
---|
| 783 | $text =~ s/\n/ /isg;
|
---|
| 784 | print $outhandle " ($text)\n";
|
---|
| 785 | }
|
---|
| 786 | } elsif (!$found_something) {
|
---|
| 787 |
|
---|
| 788 | if ($self->{'verbosity'} > 2) {
|
---|
| 789 | # may get to here if document contained no valid Section
|
---|
| 790 | # tags but did contain some comments. The text will have
|
---|
| 791 | # been processed already but we should print the warning
|
---|
| 792 | # as above and extract metadata
|
---|
| 793 | print $outhandle "HTMLPlug: WARNING: $file appears to contain no Section tags and\n";
|
---|
| 794 | print $outhandle " is blank or empty. Metadata will be assigned if present.\n";
|
---|
| 795 | }
|
---|
| 796 |
|
---|
| 797 | my $complete_text = $head_keep.$doc_obj->get_text($cursection);
|
---|
| 798 | $self->extract_metadata (\$complete_text, $metadata, $doc_obj, $cursection)
|
---|
| 799 | unless $self->{'no_metadata'};
|
---|
| 800 | }
|
---|
| 801 |
|
---|
| 802 | } else {
|
---|
| 803 |
|
---|
| 804 | # remove header and footer
|
---|
| 805 | if (!$self->{'keep_head'} || $self->{'description_tags'}) {
|
---|
| 806 | $$textref =~ s/^.*?<body[^>]*>//is;
|
---|
| 807 | $$textref =~ s/(<\/body[^>]*>|<\/html[^>]*>)//isg;
|
---|
| 808 | }
|
---|
| 809 |
|
---|
| 810 | # single section document
|
---|
| 811 | $self->process_section($textref, $base_dir, $file, $doc_obj, $cursection);
|
---|
| 812 | }
|
---|
| 813 | return 1;
|
---|
| 814 | }
|
---|
| 815 |
|
---|
| 816 |
|
---|
| 817 | sub process_heading
|
---|
| 818 | {
|
---|
| 819 | my ($self, $nHeadNo, $strHeadingText, $arrSections, $file) = @_;
|
---|
| 820 | $strHeadingText = '' if (!defined($strHeadingText));
|
---|
| 821 |
|
---|
| 822 | my $strMetadata = $self->update_section_data($arrSections, int($nHeadNo));
|
---|
| 823 |
|
---|
| 824 | my $strSecMetadata = '';
|
---|
| 825 | while ($strHeadingText =~ s/<!--gsdl-metadata(.*?)-->//is)
|
---|
| 826 | {
|
---|
| 827 | $strSecMetadata .= $1;
|
---|
| 828 | }
|
---|
| 829 |
|
---|
| 830 | $strHeadingText =~ s/^\s+//g;
|
---|
| 831 | $strHeadingText =~ s/\s+$//g;
|
---|
| 832 | $strSecMetadata =~ s/^\s+//g;
|
---|
| 833 | $strSecMetadata =~ s/\s+$//g;
|
---|
| 834 |
|
---|
| 835 | $strMetadata .= "\n<Section>\n\t<Description>\n\t\t<Metadata name=\"Title\">" . $strHeadingText . "</Metadata>\n";
|
---|
| 836 |
|
---|
| 837 | if (length($strSecMetadata)) {
|
---|
| 838 | $strMetadata .= "\t\t" . $strSecMetadata . "\n";
|
---|
| 839 | }
|
---|
| 840 |
|
---|
| 841 | $strMetadata .= "\t</Description>\n";
|
---|
| 842 |
|
---|
| 843 | return "<!--" . $strMetadata . "-->";
|
---|
| 844 | }
|
---|
| 845 |
|
---|
| 846 |
|
---|
| 847 | sub update_section_data
|
---|
| 848 | {
|
---|
| 849 | my ($self, $arrSections, $nCurTocNo) = @_;
|
---|
| 850 | my ($strBuffer, $nLast, $nSections) = ('', 0, scalar(@$arrSections));
|
---|
| 851 |
|
---|
| 852 | if ($nSections == 0) {
|
---|
| 853 | push @$arrSections, $nCurTocNo;
|
---|
| 854 | return $strBuffer;
|
---|
| 855 | }
|
---|
| 856 | $nLast = $arrSections->[$nSections - 1];
|
---|
| 857 | if ($nCurTocNo > $nLast) {
|
---|
| 858 | push @$arrSections, $nCurTocNo;
|
---|
| 859 | return $strBuffer;
|
---|
| 860 | }
|
---|
| 861 | for(my $i = $nSections - 1; $i >= 0; $i--) {
|
---|
| 862 | if ($nCurTocNo <= $arrSections->[$i]) {
|
---|
| 863 | $strBuffer .= "\n</Section>";
|
---|
| 864 | pop @$arrSections;
|
---|
| 865 | }
|
---|
| 866 | }
|
---|
| 867 | push @$arrSections, $nCurTocNo;
|
---|
| 868 | return $strBuffer;
|
---|
| 869 | }
|
---|
| 870 |
|
---|
| 871 |
|
---|
| 872 | # note that process_section may be called multiple times for a single
|
---|
| 873 | # section (relying on the fact that add_utf8_text appends the text to any
|
---|
| 874 | # that may exist already).
|
---|
| 875 | sub process_section {
|
---|
| 876 | my $self = shift (@_);
|
---|
| 877 | my ($textref, $base_dir, $file, $doc_obj, $cursection) = @_;
|
---|
| 878 | # trap links
|
---|
| 879 | if (!$self->{'nolinks'}) {
|
---|
| 880 |
|
---|
| 881 | # usemap="./#index" not handled correctly => change to "#index"
|
---|
| 882 | $$textref =~ s/(<img[^>]*?usemap\s*=\s*[\"\']?)([^\"\'>\s]+)([\"\']?[^>]*>)/
|
---|
| 883 | $self->replace_usemap_links($1, $2, $3)/isge;
|
---|
| 884 |
|
---|
| 885 | $$textref =~ s/(<(?:a|area|frame|link|script)\s+[^>]*?\s*(?:href|src)\s*=\s*[\"\']?)([^\"\'>\s]+)([\"\']?[^>]*>)/
|
---|
| 886 | $self->replace_href_links ($1, $2, $3, $base_dir, $file, $doc_obj, $cursection)/isge;
|
---|
| 887 | }
|
---|
| 888 |
|
---|
| 889 | # trap images
|
---|
| 890 |
|
---|
| 891 | # allow spaces if inside quotes - jrm21
|
---|
| 892 | $$textref =~ s/(<(?:img|embed|table|tr|td)[^>]*?(?:src|background)\s*=\s*)([\"\'][^\"\']+[\"\']|[^\s>]+)([^>]*>)/
|
---|
| 893 | $self->replace_images ($1, $2, $3, $base_dir, $file, $doc_obj, $cursection)/isge;
|
---|
| 894 |
|
---|
| 895 | # add text to document object
|
---|
| 896 | # turn \ into \\ so that the rest of greenstone doesn't think there
|
---|
| 897 | # is an escape code following. (Macro parsing loses them...)
|
---|
| 898 | $$textref =~ s/\\/\\\\/go;
|
---|
| 899 |
|
---|
| 900 | $doc_obj->add_utf8_text($cursection, $$textref);
|
---|
| 901 | }
|
---|
| 902 |
|
---|
| 903 | sub replace_images {
|
---|
| 904 | my $self = shift (@_);
|
---|
| 905 | my ($front, $link, $back, $base_dir,
|
---|
| 906 | $file, $doc_obj, $section) = @_;
|
---|
| 907 |
|
---|
| 908 | # remove quotes from link at start and end if necessary
|
---|
| 909 | if ($link=~/^[\"\']/) {
|
---|
| 910 | $link=~s/^[\"\']//;$link=~s/[\"\']$//;
|
---|
| 911 | $front.='"';
|
---|
| 912 | $back="\"$back";
|
---|
| 913 | }
|
---|
| 914 |
|
---|
| 915 | $link =~ s/\n/ /g;
|
---|
| 916 |
|
---|
| 917 | # Hack to overcome Windows wv 0.7.1 bug that causes embedded images to be broken
|
---|
| 918 | # If the Word file path has spaces in it, wv messes up and you end up with
|
---|
| 919 | # absolute paths for the images, and without the "file://" prefix
|
---|
| 920 | # So check for this special case and massage the data to be correct
|
---|
| 921 | if ($ENV{'GSDLOS'} =~ /^windows/i && $self->{'plugin_type'} eq "WordPlug" && $link =~ /^[A-Za-z]\:\\/) {
|
---|
| 922 | $link =~ s/^.*\\([^\\]+)$/$1/;
|
---|
| 923 | }
|
---|
| 924 |
|
---|
| 925 | my ($href, $hash_part, $rl) = $self->format_link ($link, $base_dir, $file);
|
---|
| 926 |
|
---|
| 927 | my $img_file = $self->add_file ($href, $rl, $hash_part, $base_dir, $doc_obj, $section);
|
---|
| 928 |
|
---|
| 929 | my $anchor_name = $img_file;
|
---|
| 930 | #$anchor_name =~ s/^.*\///;
|
---|
| 931 | #$anchor_name = "<a name=\"$anchor_name\" ></a>";
|
---|
| 932 |
|
---|
| 933 | my $image_link = $front . $img_file .$back;
|
---|
| 934 | my $anchor_link = "<a href=\"$img_file\" >".$image_link."</a>";
|
---|
| 935 |
|
---|
| 936 | return $anchor_link;
|
---|
| 937 | #return $front . $img_file . $back . $anchor_name;
|
---|
| 938 | }
|
---|
| 939 |
|
---|
| 940 | sub replace_href_links {
|
---|
| 941 | my $self = shift (@_);
|
---|
| 942 | my ($front, $link, $back, $base_dir, $file, $doc_obj, $section) = @_;
|
---|
| 943 |
|
---|
| 944 | # attempt to sort out targets - frames are not handled
|
---|
| 945 | # well in this plugin and some cases will screw things
|
---|
| 946 | # up - e.g. the _parent target (so we'll just remove
|
---|
| 947 | # them all ;-)
|
---|
| 948 | $front =~ s/(target=\"?)_top(\"?)/$1_gsdltop_$2/is;
|
---|
| 949 | $back =~ s/(target=\"?)_top(\"?)/$1_gsdltop_$2/is;
|
---|
| 950 | $front =~ s/target=\"?_parent\"?//is;
|
---|
| 951 | $back =~ s/target=\"?_parent\"?//is;
|
---|
| 952 |
|
---|
| 953 | return $front . $link . $back if $link =~ /^\#/s;
|
---|
| 954 | $link =~ s/\n/ /g;
|
---|
| 955 |
|
---|
| 956 | my ($href, $hash_part, $rl) = $self->format_link ($link, $base_dir, $file);
|
---|
| 957 | # href may use '\'s where '/'s should be on Windows
|
---|
| 958 | $href =~ s/\\/\//g;
|
---|
| 959 |
|
---|
| 960 | my ($filename) = $href =~ /^(?:.*?):(?:\/\/)?(.*)/;
|
---|
| 961 |
|
---|
| 962 |
|
---|
| 963 | ##### leave all these links alone (they won't be picked up by intermediate
|
---|
| 964 | ##### pages). I think that's safest when dealing with frames, targets etc.
|
---|
| 965 | ##### (at least until I think of a better way to do it). Problems occur with
|
---|
| 966 | ##### mailto links from within small frames, the intermediate page is displayed
|
---|
| 967 | ##### within that frame and can't be seen. There is still potential for this to
|
---|
| 968 | ##### happen even with html pages - the solution seems to be to somehow tell
|
---|
| 969 | ##### the browser from the server side to display the page being sent (i.e.
|
---|
| 970 | ##### the intermediate page) in the top level window - I'm not sure if that's
|
---|
| 971 | ##### possible - the following line should probably be deleted if that can be done
|
---|
| 972 | return $front . $link . $back if $href =~ /^(mailto|news|gopher|nntp|telnet|javascript):/is;
|
---|
| 973 |
|
---|
| 974 |
|
---|
| 975 | if (($rl == 0) || ($filename =~ /$self->{'process_exp'}/) ||
|
---|
| 976 | ($href =~ /\/$/) || ($href =~ /^(mailto|news|gopher|nntp|telnet|javascript):/i)) {
|
---|
| 977 | &ghtml::urlsafe ($href);
|
---|
| 978 | return $front . "_httpextlink_&rl=" . $rl . "&href=" . $href . $hash_part . $back;
|
---|
| 979 | } else {
|
---|
| 980 | # link is to some other type of file (eg image) so we'll
|
---|
| 981 | # need to associate that file
|
---|
| 982 | return $front . $self->add_file ($href, $rl, $hash_part, $base_dir, $doc_obj, $section) . $back;
|
---|
| 983 | }
|
---|
| 984 | }
|
---|
| 985 |
|
---|
| 986 | sub add_file {
|
---|
| 987 | my $self = shift (@_);
|
---|
| 988 | my ($href, $rl, $hash_part, $base_dir, $doc_obj, $section) = @_;
|
---|
| 989 | my ($newname);
|
---|
| 990 |
|
---|
| 991 | my $filename = $href;
|
---|
| 992 | if ($base_dir eq "") {
|
---|
| 993 | # remove http:/ thereby leaving one slash at the start
|
---|
| 994 | $filename =~ s/^[^:]*:\///;
|
---|
| 995 | }
|
---|
| 996 | else {
|
---|
| 997 | # remove http://
|
---|
| 998 | $filename =~ s/^[^:]*:\/\///;
|
---|
| 999 | }
|
---|
| 1000 |
|
---|
| 1001 | $filename = &util::filename_cat($base_dir, $filename);
|
---|
| 1002 |
|
---|
| 1003 | # Replace %20's in URL with a space if required. Note that the filename
|
---|
| 1004 | # may include the %20 in some situations
|
---|
| 1005 | if ($filename =~ /\%20/) {
|
---|
| 1006 | if (!-e $filename) {
|
---|
| 1007 | $filename =~ s/\%20/ /g;
|
---|
| 1008 | }
|
---|
| 1009 | }
|
---|
| 1010 |
|
---|
| 1011 | my ($ext) = $filename =~ /(\.[^\.]*)$/;
|
---|
| 1012 |
|
---|
| 1013 | if ($rl == 0) {
|
---|
| 1014 | if ((!defined $ext) || ($ext !~ /$self->{'assoc_files'}/)) {
|
---|
| 1015 | return "_httpextlink_&rl=0&el=prompt&href=" . $href . $hash_part;
|
---|
| 1016 | }
|
---|
| 1017 | else {
|
---|
| 1018 | return "_httpextlink_&rl=0&el=direct&href=" . $href . $hash_part;
|
---|
| 1019 | }
|
---|
| 1020 | }
|
---|
| 1021 |
|
---|
| 1022 | if ((!defined $ext) || ($ext !~ /$self->{'assoc_files'}/)) {
|
---|
| 1023 | return "_httpextlink_&rl=" . $rl . "&href=" . $href . $hash_part;
|
---|
| 1024 | }
|
---|
| 1025 | if ($self->{'rename_assoc_files'}) {
|
---|
| 1026 | if (defined $self->{'aux_files'}->{$href}) {
|
---|
| 1027 | $newname = $self->{'aux_files'}->{$href}->{'dir_num'} . "/" .
|
---|
| 1028 | $self->{'aux_files'}->{$href}->{'file_num'} . $ext;
|
---|
| 1029 | } else {
|
---|
| 1030 | $newname = $self->{'dir_num'} . "/" . $self->{'file_num'} . $ext;
|
---|
| 1031 | $self->{'aux_files'}->{$href} = {'dir_num' => $self->{'dir_num'}, 'file_num' => $self->{'file_num'}};
|
---|
| 1032 | $self->inc_filecount ();
|
---|
| 1033 | }
|
---|
| 1034 | $doc_obj->associate_file($filename, $newname, undef, $section);
|
---|
| 1035 | return "_httpdocimg_/$newname";
|
---|
| 1036 | } else {
|
---|
| 1037 | ($newname) = $filename =~ /([^\/\\]*)$/;
|
---|
| 1038 | $doc_obj->associate_file($filename, $newname, undef, $section);
|
---|
| 1039 | return "_httpdocimg_/$newname";
|
---|
| 1040 | }
|
---|
| 1041 | }
|
---|
| 1042 |
|
---|
| 1043 |
|
---|
| 1044 | sub format_link {
|
---|
| 1045 | my $self = shift (@_);
|
---|
| 1046 | my ($link, $base_dir, $file) = @_;
|
---|
| 1047 |
|
---|
| 1048 | my ($before_hash, $hash_part) = $link =~ /^([^\#]*)(\#?.*)$/;
|
---|
| 1049 |
|
---|
| 1050 | $hash_part = "" if !defined $hash_part;
|
---|
| 1051 | if (!defined $before_hash || $before_hash !~ /[\w\.\/]/) {
|
---|
| 1052 | my $outhandle = $self->{'outhandle'};
|
---|
| 1053 | print $outhandle "HTMLPlug: ERROR - badly formatted tag ignored ($link)\n"
|
---|
| 1054 | if $self->{'verbosity'};
|
---|
| 1055 | return ($link, "", 0);
|
---|
| 1056 | }
|
---|
| 1057 |
|
---|
| 1058 | if ($before_hash =~ s@^((?:http|ftp|file)://)@@i) {
|
---|
| 1059 | my $type = $1;
|
---|
| 1060 |
|
---|
| 1061 | if ($link =~ /^(http|ftp):/i) {
|
---|
| 1062 | # Turn url (using /) into file name (possibly using \ on windows)
|
---|
| 1063 | my @http_dir_split = split('/', $before_hash);
|
---|
| 1064 | $before_hash = &util::filename_cat(@http_dir_split);
|
---|
| 1065 | }
|
---|
| 1066 |
|
---|
| 1067 | $before_hash = $self->eval_dir_dots($before_hash);
|
---|
| 1068 |
|
---|
| 1069 | my $linkfilename = &util::filename_cat ($base_dir, $before_hash);
|
---|
| 1070 |
|
---|
| 1071 | my $rl = 0;
|
---|
| 1072 | $rl = 1 if (-e $linkfilename);
|
---|
| 1073 |
|
---|
| 1074 | # make sure there's a slash on the end if it's a directory
|
---|
| 1075 | if ($before_hash !~ /\/$/) {
|
---|
| 1076 | $before_hash .= "/" if (-d $linkfilename);
|
---|
| 1077 | }
|
---|
| 1078 |
|
---|
| 1079 | return ($type . $before_hash, $hash_part, $rl);
|
---|
| 1080 |
|
---|
| 1081 | } elsif ($link !~ /^(mailto|news|gopher|nntp|telnet|javascript):/i && $link !~ /^\//) {
|
---|
| 1082 | if ($before_hash =~ s@^/@@ || $before_hash =~ /\\/) {
|
---|
| 1083 |
|
---|
| 1084 | # the first directory will be the domain name if file_is_url
|
---|
| 1085 | # to generate archives, otherwise we'll assume all files are
|
---|
| 1086 | # from the same site and base_dir is the root
|
---|
| 1087 |
|
---|
| 1088 | if ($self->{'file_is_url'}) {
|
---|
| 1089 | my @dirs = split /[\/\\]/, $file;
|
---|
| 1090 | my $domname = shift (@dirs);
|
---|
| 1091 | $before_hash = &util::filename_cat($domname, $before_hash);
|
---|
| 1092 | $before_hash =~ s@\\@/@g; # for windows
|
---|
| 1093 | }
|
---|
| 1094 | else
|
---|
| 1095 | {
|
---|
| 1096 | # see if link shares directory with source document
|
---|
| 1097 | # => turn into relative link if this is so!
|
---|
| 1098 |
|
---|
| 1099 | if ($ENV{'GSDLOS'} =~ /^windows/i) {
|
---|
| 1100 | # too difficult doing a pattern match with embedded '\'s...
|
---|
| 1101 | my $win_before_hash=$before_hash;
|
---|
| 1102 | $win_before_hash =~ s@(\\)+@/@g;
|
---|
| 1103 | # $base_dir is already similarly "converted" on windows.
|
---|
| 1104 | if ($win_before_hash =~ s@^$base_dir/@@o) {
|
---|
| 1105 | # if this is true, we removed a prefix
|
---|
| 1106 | $before_hash=$win_before_hash;
|
---|
| 1107 | }
|
---|
| 1108 | }
|
---|
| 1109 | else {
|
---|
| 1110 | # before_hash has lost leading slash by this point,
|
---|
| 1111 | # -> add back in prior to substitution with $base_dir
|
---|
| 1112 | $before_hash = "/$before_hash";
|
---|
| 1113 |
|
---|
| 1114 | $before_hash = &util::filename_cat("",$before_hash);
|
---|
| 1115 | $before_hash =~ s@^$base_dir/@@;
|
---|
| 1116 | }
|
---|
| 1117 | }
|
---|
| 1118 | } else {
|
---|
| 1119 | # Turn relative file path into full path
|
---|
| 1120 | my $dirname = &File::Basename::dirname($file);
|
---|
| 1121 | $before_hash = &util::filename_cat($dirname, $before_hash);
|
---|
| 1122 | $before_hash = $self->eval_dir_dots($before_hash);
|
---|
| 1123 | }
|
---|
| 1124 |
|
---|
| 1125 | my $linkfilename = &util::filename_cat ($base_dir, $before_hash);
|
---|
| 1126 | # make sure there's a slash on the end if it's a directory
|
---|
| 1127 | if ($before_hash !~ /\/$/) {
|
---|
| 1128 | $before_hash .= "/" if (-d $linkfilename);
|
---|
| 1129 | }
|
---|
| 1130 | return ("http://" . $before_hash, $hash_part, 1);
|
---|
| 1131 | } else {
|
---|
| 1132 | # mailto, news, nntp, telnet, javascript or gopher link
|
---|
| 1133 | return ($before_hash, "", 0);
|
---|
| 1134 | }
|
---|
| 1135 | }
|
---|
| 1136 |
|
---|
| 1137 | sub extract_first_NNNN_characters {
|
---|
| 1138 | my $self = shift (@_);
|
---|
| 1139 | my ($textref, $doc_obj, $thissection) = @_;
|
---|
| 1140 |
|
---|
| 1141 | foreach my $size (split /,/, $self->{'first'}) {
|
---|
| 1142 | my $tmptext = $$textref;
|
---|
| 1143 | # skip to the body
|
---|
| 1144 | $tmptext =~ s/.*<body[^>]*>//i;
|
---|
| 1145 | # remove javascript
|
---|
| 1146 | $tmptext =~ s@<script.*?</script>@ @sig;
|
---|
| 1147 | $tmptext =~ s/<[^>]*>/ /g;
|
---|
| 1148 | $tmptext =~ s/ / /g;
|
---|
| 1149 | $tmptext =~ s/^\s+//;
|
---|
| 1150 | $tmptext =~ s/\s+$//;
|
---|
| 1151 | $tmptext =~ s/\s+/ /gs;
|
---|
| 1152 | $tmptext = &unicode::substr ($tmptext, 0, $size);
|
---|
| 1153 | $tmptext =~ s/\s\S*$/…/; # adds an ellipse (...)
|
---|
| 1154 | $doc_obj->add_utf8_metadata ($thissection, "First$size", $tmptext);
|
---|
| 1155 | }
|
---|
| 1156 | }
|
---|
| 1157 |
|
---|
| 1158 |
|
---|
| 1159 | sub extract_metadata {
|
---|
| 1160 | my $self = shift (@_);
|
---|
| 1161 | my ($textref, $metadata, $doc_obj, $section) = @_;
|
---|
| 1162 | my $outhandle = $self->{'outhandle'};
|
---|
| 1163 | # if we don't want metadata, we may as well not be here ...
|
---|
| 1164 | return if (!defined $self->{'metadata_fields'});
|
---|
| 1165 |
|
---|
| 1166 | # metadata fields to extract/save. 'key' is the (lowercase) name of the
|
---|
| 1167 | # html meta, 'value' is the metadata name for greenstone to use
|
---|
| 1168 | my %find_fields = ();
|
---|
| 1169 |
|
---|
| 1170 | my %creator_fields = (); # short-cut for lookups
|
---|
| 1171 |
|
---|
| 1172 |
|
---|
| 1173 | foreach my $field (split /,/, $self->{'metadata_fields'}) {
|
---|
| 1174 | $field =~ s/^\s+//; # remove leading whitespace
|
---|
| 1175 | $field =~ s/\s+$//; # remove trailing whitespace
|
---|
| 1176 |
|
---|
| 1177 | # support tag<tagname>
|
---|
| 1178 | if ($field =~ /^(.*?)<(.*?)>$/) {
|
---|
| 1179 | # "$2" is the user's preferred gs metadata name
|
---|
| 1180 | $find_fields{lc($1)}=$2; # lc = lowercase
|
---|
| 1181 | } else { # no <tagname> for mapping
|
---|
| 1182 | # "$field" is the user's preferred gs metadata name
|
---|
| 1183 | $find_fields{lc($field)}=$field; # lc = lowercase
|
---|
| 1184 | }
|
---|
| 1185 | }
|
---|
| 1186 |
|
---|
| 1187 | if (defined $self->{'hunt_creator_metadata'} &&
|
---|
| 1188 | $self->{'hunt_creator_metadata'} == 1 ) {
|
---|
| 1189 | my @extra_fields =
|
---|
| 1190 | (
|
---|
| 1191 | 'author',
|
---|
| 1192 | 'author.email',
|
---|
| 1193 | 'creator',
|
---|
| 1194 | 'dc.creator',
|
---|
| 1195 | 'dc.creator.corporatename',
|
---|
| 1196 | );
|
---|
| 1197 |
|
---|
| 1198 | # add the creator_metadata fields to search for
|
---|
| 1199 | foreach my $field (@extra_fields) {
|
---|
| 1200 | $creator_fields{$field}=0; # add to lookup hash
|
---|
| 1201 | }
|
---|
| 1202 | }
|
---|
| 1203 |
|
---|
| 1204 |
|
---|
| 1205 | # find the header in the html file, which has the meta tags
|
---|
| 1206 | $$textref =~ m@<head>(.*?)</head>@si;
|
---|
| 1207 |
|
---|
| 1208 | my $html_header=$1;
|
---|
| 1209 |
|
---|
| 1210 | # go through every <meta... tag defined in the html and see if it is
|
---|
| 1211 | # one of the tags we want to match.
|
---|
| 1212 |
|
---|
| 1213 | # special case for title - we want to remember if its been found
|
---|
| 1214 | my $found_title = 0;
|
---|
| 1215 | # this assumes that ">" won't appear. (I don't think it's allowed to...)
|
---|
| 1216 | $html_header =~ /^/; # match the start of the string, for \G assertion
|
---|
| 1217 |
|
---|
| 1218 | while ($html_header =~ m/\G.*?<meta(.*?)>/sig) {
|
---|
| 1219 | my $metatag=$1;
|
---|
| 1220 | my ($tag, $value);
|
---|
| 1221 |
|
---|
| 1222 | # find the tag name
|
---|
| 1223 | $metatag =~ /(?:name|http-equiv)\s*=\s*([\"\'])?(.*?)\1/is;
|
---|
| 1224 | $tag=$2;
|
---|
| 1225 | # in case they're not using " or ', but they should...
|
---|
| 1226 | if (! $tag) {
|
---|
| 1227 | $metatag =~ /(?:name|http-equiv)\s*=\s*([^\s\>]+)/is;
|
---|
| 1228 | $tag=$1;
|
---|
| 1229 | }
|
---|
| 1230 |
|
---|
| 1231 | if (!defined $tag) {
|
---|
| 1232 | print $outhandle "HTMLPlug: can't find NAME in \"$metatag\"\n";
|
---|
| 1233 | next;
|
---|
| 1234 | }
|
---|
| 1235 |
|
---|
| 1236 | # don't need to assign this field if it was passed in from a previous
|
---|
| 1237 | # (recursive) plugin
|
---|
| 1238 | if (defined $metadata->{$tag}) {next}
|
---|
| 1239 |
|
---|
| 1240 | # find the tag content
|
---|
| 1241 | $metatag =~ /content\s*=\s*([\"\'])?(.*?)\1/is;
|
---|
| 1242 | $value=$2;
|
---|
| 1243 |
|
---|
| 1244 | if (! $value) {
|
---|
| 1245 | $metatag =~ /(?:name|http-equiv)\s*=\s*([^\s\>]+)/is;
|
---|
| 1246 | $value=$1;
|
---|
| 1247 | }
|
---|
| 1248 | if (!defined $value) {
|
---|
| 1249 | print $outhandle "HTMLPlug: can't find VALUE in \"$metatag\"\n";
|
---|
| 1250 | next;
|
---|
| 1251 | }
|
---|
| 1252 |
|
---|
| 1253 | # clean up and add
|
---|
| 1254 | $value =~ s/\s+/ /gs;
|
---|
| 1255 | chomp($value); # remove trailing \n, if any
|
---|
| 1256 | if (exists $creator_fields{lc($tag)}) {
|
---|
| 1257 | # map this value onto greenstone's "Creator" metadata
|
---|
| 1258 | $tag='Creator';
|
---|
| 1259 | } elsif (!exists $find_fields{lc($tag)}) {
|
---|
| 1260 | next; # don't want this tag
|
---|
| 1261 | } else {
|
---|
| 1262 | # get the user's preferred capitalisation
|
---|
| 1263 | $tag = $find_fields{lc($tag)};
|
---|
| 1264 | }
|
---|
| 1265 | if (lc($tag) eq "title") {
|
---|
| 1266 | $found_title = 1;
|
---|
| 1267 | }
|
---|
| 1268 | print $outhandle " extracted \"$tag\" metadata \"$value\"\n"
|
---|
| 1269 | if ($self->{'verbosity'} > 2);
|
---|
| 1270 | if ($tag =~ /date.*/i){
|
---|
| 1271 | $tag = lc($tag);
|
---|
| 1272 | }
|
---|
| 1273 | $doc_obj->add_utf8_metadata($section, $tag, $value);
|
---|
| 1274 |
|
---|
| 1275 | }
|
---|
| 1276 |
|
---|
| 1277 | # TITLE: extract the document title
|
---|
| 1278 | if (exists $find_fields{'title'} && !$found_title) {
|
---|
| 1279 | # we want a title, and didn't find one in the meta tags
|
---|
| 1280 | # see if there's a <title> tag
|
---|
| 1281 | my $title;
|
---|
| 1282 | my $from = ""; # for debugging output only
|
---|
| 1283 | if ($html_header =~ /<title[^>]*>([^<]+)<\/title[^>]*>/is) {
|
---|
| 1284 | $title = $1;
|
---|
| 1285 | $from = "<title> tags";
|
---|
| 1286 | }
|
---|
| 1287 |
|
---|
| 1288 | if (!defined $title) {
|
---|
| 1289 | $from = "first 100 chars";
|
---|
| 1290 | # if no title use first 100 or so characters
|
---|
| 1291 | $title = $$textref;
|
---|
| 1292 | $title =~ s/^\xFE\xFF//; # Remove unicode byte order mark
|
---|
| 1293 | $title =~ s/^.*?<body>//si;
|
---|
| 1294 | # ignore javascript!
|
---|
| 1295 | $title =~ s@<script.*?</script>@ @sig;
|
---|
| 1296 | $title =~ s/<\/([^>]+)><\1>//g; # (eg) </b><b> - no space
|
---|
| 1297 | $title =~ s/<[^>]*>/ /g; # remove all HTML tags
|
---|
| 1298 | $title = substr ($title, 0, 100);
|
---|
| 1299 | $title =~ s/\s\S*$/.../;
|
---|
| 1300 | }
|
---|
| 1301 | $title =~ s/<[^>]*>/ /g; # remove html tags
|
---|
| 1302 | $title =~ s/ / /g;
|
---|
| 1303 | $title =~ s/(?: |\xc2\xa0)/ /g; # utf-8 for nbsp...
|
---|
| 1304 | $title =~ s/\s+/ /gs; # collapse multiple spaces
|
---|
| 1305 | $title =~ s/^\s*//; # remove leading spaces
|
---|
| 1306 | $title =~ s/\s*$//; # remove trailing spaces
|
---|
| 1307 |
|
---|
| 1308 | $title =~ s/^$self->{'title_sub'}// if ($self->{'title_sub'});
|
---|
| 1309 | $title =~ s/^\s+//s; # in case title_sub introduced any...
|
---|
| 1310 | $doc_obj->add_utf8_metadata ($section, 'Title', $title);
|
---|
| 1311 | print $outhandle " extracted Title metadata \"$title\" from $from\n"
|
---|
| 1312 | if ($self->{'verbosity'} > 2);
|
---|
| 1313 | }
|
---|
| 1314 |
|
---|
| 1315 | # add FileFormat metadata
|
---|
| 1316 | $doc_obj->add_metadata($section,"FileFormat", "HTML");
|
---|
| 1317 |
|
---|
| 1318 | # Special, for metadata names such as tagH1 - extracts
|
---|
| 1319 | # the text between the first <H1> and </H1> tags into "H1" metadata.
|
---|
| 1320 |
|
---|
| 1321 | foreach my $field (keys %find_fields) {
|
---|
| 1322 | if ($field !~ /^tag([a-z0-9]+)$/i) {next}
|
---|
| 1323 | my $tag = $1;
|
---|
| 1324 | if ($$textref =~ m@<$tag[^>]*>(.*?)</$tag[^>]*>@g) {
|
---|
| 1325 | my $content = $1;
|
---|
| 1326 | $content =~ s/ / /g;
|
---|
| 1327 | $content =~ s/<[^>]*>/ /g;
|
---|
| 1328 | $content =~ s/^\s+//;
|
---|
| 1329 | $content =~ s/\s+$//;
|
---|
| 1330 | $content =~ s/\s+/ /gs;
|
---|
| 1331 | if ($content) {
|
---|
| 1332 | $tag=$find_fields{"tag$tag"}; # get the user's capitalisation
|
---|
| 1333 | $tag =~ s/^tag//i;
|
---|
| 1334 | $doc_obj->add_utf8_metadata ($section, $tag, $content);
|
---|
| 1335 | print $outhandle " extracted \"$tag\" metadata \"$content\"\n"
|
---|
| 1336 | if ($self->{'verbosity'} > 2);
|
---|
| 1337 | }
|
---|
| 1338 | }
|
---|
| 1339 | }
|
---|
| 1340 | }
|
---|
| 1341 |
|
---|
| 1342 |
|
---|
| 1343 | # evaluate any "../" to next directory up
|
---|
| 1344 | # evaluate any "./" as here
|
---|
| 1345 | sub eval_dir_dots {
|
---|
| 1346 | my $self = shift (@_);
|
---|
| 1347 | my ($filename) = @_;
|
---|
| 1348 | my $dirsep_os = &util::get_os_dirsep();
|
---|
| 1349 | my @dirsep = split(/$dirsep_os/,$filename);
|
---|
| 1350 |
|
---|
| 1351 | my @eval_dirs = ();
|
---|
| 1352 | foreach my $d (@dirsep) {
|
---|
| 1353 | if ($d eq "..") {
|
---|
| 1354 | pop(@eval_dirs);
|
---|
| 1355 |
|
---|
| 1356 | } elsif ($d eq ".") {
|
---|
| 1357 | # do nothing!
|
---|
| 1358 |
|
---|
| 1359 | } else {
|
---|
| 1360 | push(@eval_dirs,$d);
|
---|
| 1361 | }
|
---|
| 1362 | }
|
---|
| 1363 |
|
---|
| 1364 | # Need to fiddle with number of elements in @eval_dirs if the
|
---|
| 1365 | # first one is the empty string. This is because of a
|
---|
| 1366 | # modification to util::filename_cat that supresses the addition
|
---|
| 1367 | # of a leading '/' character (or \ if windows) (intended to help
|
---|
| 1368 | # filename cat with relative paths) if the first entry in the
|
---|
| 1369 | # array is the empty string. Making the array start with *two*
|
---|
| 1370 | # empty strings is a way to defeat this "smart" option.
|
---|
| 1371 | #
|
---|
| 1372 | if (scalar(@eval_dirs) > 0) {
|
---|
| 1373 | if ($eval_dirs[0] eq ""){
|
---|
| 1374 | unshift(@eval_dirs,"");
|
---|
| 1375 | }
|
---|
| 1376 | }
|
---|
| 1377 | return &util::filename_cat(@eval_dirs);
|
---|
| 1378 | }
|
---|
| 1379 |
|
---|
| 1380 | sub replace_usemap_links {
|
---|
| 1381 | my $self = shift (@_);
|
---|
| 1382 | my ($front, $link, $back) = @_;
|
---|
| 1383 |
|
---|
| 1384 | $link =~ s/^\.\///;
|
---|
| 1385 | return $front . $link . $back;
|
---|
| 1386 | }
|
---|
| 1387 |
|
---|
| 1388 | sub inc_filecount {
|
---|
| 1389 | my $self = shift (@_);
|
---|
| 1390 |
|
---|
| 1391 | if ($self->{'file_num'} == 1000) {
|
---|
| 1392 | $self->{'dir_num'} ++;
|
---|
| 1393 | $self->{'file_num'} = 0;
|
---|
| 1394 | } else {
|
---|
| 1395 | $self->{'file_num'} ++;
|
---|
| 1396 | }
|
---|
| 1397 | }
|
---|
| 1398 |
|
---|
| 1399 |
|
---|
| 1400 | # Extend the BasPlug read_file so that strings like é are
|
---|
| 1401 | # converted to UTF8 internally.
|
---|
| 1402 | #
|
---|
| 1403 | # We don't convert < or > or & or " in case
|
---|
| 1404 | # they interfere with the GML files
|
---|
| 1405 |
|
---|
| 1406 | sub read_file {
|
---|
| 1407 | my ($self, $filename, $encoding, $language, $textref) = @_;
|
---|
| 1408 |
|
---|
| 1409 | &BasPlug::read_file($self, $filename, $encoding, $language, $textref);
|
---|
| 1410 |
|
---|
| 1411 | # Convert entities to their UTF8 equivalents
|
---|
| 1412 | $$textref =~ s/&(lt|gt|amp|quot|nbsp);/&z$1;/go;
|
---|
| 1413 | $$textref =~ s/&([^;]+);/&ghtml::getcharequiv($1,1)/gseo;
|
---|
| 1414 | $$textref =~ s/&z(lt|gt|amp|quot|nbsp);/&$1;/go;
|
---|
| 1415 | }
|
---|
| 1416 |
|
---|
| 1417 | 1;
|
---|