source: trunk/gsdl/perllib/plugins/HTMLPlug.pm@ 7202

Last change on this file since 7202 was 7202, checked in by jrm21, 20 years ago

rewrote the <meta> tag handling to be more robust and more efficient.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 24.7 KB
Line 
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# To use frames properly you'll need to use the WebPlug plugin.
35#
36
37
38package HTMLPlug;
39
40use BasPlug;
41use ghtml;
42use unicode;
43use util;
44use parsargv;
45
46sub BEGIN {
47 @ISA = ('BasPlug');
48}
49
50use strict; # every perl program should have this!
51no strict 'refs'; # make an exception so we can use variables as filehandles
52
53my $arguments =
54 [ { 'name' => "process_exp",
55 'desc' => "{BasPlug.process_exp}",
56 'type' => "regexp",
57 'deft' => &get_default_process_exp() },
58 { 'name' => "block_exp",
59 'desc' => "{BasPlug.block_exp}",
60 'type' => 'regexp',
61 'deft' => &get_default_block_exp() },
62 { 'name' => "nolinks",
63 'desc' => "{HTMLPlug.nolinks}",
64 'type' => "flag" },
65 { 'name' => "keep_head",
66 'desc' => "{HTMLPlug.keep_head}",
67 'type' => "flag" },
68 { 'name' => "no_metadata",
69 'desc' => "{HTMLPlug.no_metadata}",
70 'type' => "flag" },
71 { 'name' => "metadata_fields",
72 'desc' => "{HTMLPlug.metadata_fields}",
73 'type' => "string",
74 'deft' => "Title" },
75 { 'name' => "hunt_creator_metadata",
76 'desc' => "{HTMLPlug.hunt_creator_metadata}",
77 'type' => "flag" },
78 { 'name' => "file_is_url",
79 'desc' => "{HTMLPlug.file_is_url}",
80 'type' => "flag" },
81 { 'name' => "assoc_files",
82 'desc' => "{HTMLPlug.assoc_files}",
83 'type' => "regexp",
84 'deft' => &get_default_block_exp() },
85 { 'name' => "rename_assoc_files",
86 'desc' => "{HTMLPlug.rename_assoc_files}",
87 'type' => "flag" },
88 { 'name' => "title_sub",
89 'desc' => "{HTMLPlug.title_sub}",
90 'type' => "string",
91 'deft' => "" },
92 { 'name' => "description_tags",
93 'desc' => "{HTMLPlug.description_tags}",
94 'type' => "flag" } ];
95
96my $options = { 'name' => "HTMLPlug",
97 'desc' => "{HTMLPlug.desc}",
98 'abstract' => "no",
99 'inherits' => "yes",
100 'args' => $arguments };
101
102sub new {
103 my $class = shift (@_);
104 my $self = new BasPlug ($class, @_);
105 $self->{'plugin_type'} = "HTMLPlug";
106 # 14-05-02 To allow for proper inheritance of arguments - John Thompson
107 my $option_list = $self->{'option_list'};
108 push( @{$option_list}, $options );
109
110 if (!parsargv::parse(\@_,
111 q^nolinks^, \$self->{'nolinks'},
112 q^keep_head^, \$self->{'keep_head'},
113 q^no_metadata^, \$self->{'no_metadata'},
114 q^metadata_fields/.*/Title^, \$self->{'metadata_fields'},
115 q^hunt_creator_metadata^, \$self->{'hunt_creator_metadata'},
116 q^w3mir^, \$self->{'w3mir'},
117 q^file_is_url^, \$self->{'file_is_url'},
118 q^assoc_files/.*/(?i)\.(jpe?g|jpe|gif|png|css)$^, \$self->{'assoc_files'},
119 q^rename_assoc_files^, \$self->{'rename_assoc_files'},
120 q^title_sub/.*/^, \$self->{'title_sub'},
121 q^description_tags^, \$self->{'description_tags'},
122 "allow_extra_options")) {
123
124 print STDERR "\nIncorrect options passed to HTMLPlug, check your collect.cfg configuration file\n";
125 $self->print_txt_usage(""); # Use default resource bundle
126 die "\n";
127 }
128
129 # retain this for backward compatibility (w3mir option was replaced by
130 # file_is_url)
131 if ($self->{'w3mir'}) {
132 $self->{'file_is_url'} = 1;
133 }
134
135 $self->{'aux_files'} = {};
136 $self->{'dir_num'} = 0;
137 $self->{'file_num'} = 0;
138 return bless $self, $class;
139}
140
141
142sub get_default_block_exp {
143 my $self = shift (@_);
144
145 return q^(?i)\.(gif|jpe?g|jpe|png|css)$^;
146}
147
148sub get_default_process_exp {
149 my $self = shift (@_);
150
151 # the last option is an attempt to encode the concept of an html query ...
152 return q^(?i)(\.html?|\.shtml|\.shm|\.asp|\.php\d?|\.cgi|.+\?.+=.*)$^;
153}
154
155
156# do plugin specific processing of doc_obj
157sub process {
158 my $self = shift (@_);
159 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
160 my $outhandle = $self->{'outhandle'};
161
162 print STDERR "<Processing n='$file' p='HTMLPlug'>\n" if ($gli);
163
164 print $outhandle "HTMLPlug: processing $file\n"
165 if $self->{'verbosity'} > 1;
166
167 if ($ENV{'GSDLOS'} =~ /^windows/i) {
168 # this makes life so much easier... perl can cope with unix-style '/'s.
169 $base_dir =~ s@(\\)+@/@g;
170 $file =~ s@(\\)+@/@g;
171 }
172
173 # reset per-doc stuff...
174 $self->{'aux_files'} = {};
175 $self->{'dir_num'} = 0;
176 $self->{'file_num'} = 0;
177
178 my $cursection = $doc_obj->get_top_section();
179
180 $self->extract_metadata ($textref, $metadata, $doc_obj, $cursection)
181 unless $self->{'no_metadata'} || $self->{'description_tags'};
182
183 # Store URL for page as metadata - this can be used for an
184 # altavista style search interface. The URL won't be valid
185 # unless the file structure contains the domain name (i.e.
186 # like when w3mir is used to download a website).
187 my $web_url = "http://$file";
188 $doc_obj->add_metadata($cursection, "URL", $web_url);
189
190 if ($self->{'description_tags'}) {
191
192 # remove the html header - note that doing this here means any
193 # sections defined within the header will be lost (so all <Section>
194 # tags must appear within the body of the HTML)
195 $$textref =~ s/^.*?<body[^>]*>//is;
196 $$textref =~ s/(<\/body[^>]*>|<\/html[^>]*>)//isg;
197
198 my $opencom = '(?:<!--|&lt;!(?:&mdash;|&#151;|--))';
199 my $closecom = '(?:-->|(?:&mdash;|&#151;|--)&gt;)';
200 my $lt = '(?:<|&lt;)';
201 my $gt = '(?:>|&gt;)';
202 my $quot = '(?:"|&quot;|&rdquo;|&ldquo;)';
203
204 my $found_something = 0; my $top = 1;
205 while ($$textref =~ s/^(.*?)$opencom(.*?)$closecom//s) {
206 my $text = $1;
207 my $comment = $2;
208 if (defined $text) {
209 # text before a comment - note that getting to here
210 # doesn't necessarily mean there are Section tags in
211 # the document
212 $self->process_section(\$text, $base_dir, $file, $doc_obj, $cursection);
213 }
214 while ($comment =~ s/$lt(.*?)$gt//s) {
215
216 my $tag = $1;
217 if ($tag eq "Section") {
218 $found_something = 1;
219 $cursection = $doc_obj->insert_section($doc_obj->get_end_child($cursection)) unless $top;
220 $top = 0;
221 } elsif ($tag eq "/Section") {
222 $found_something = 1;
223 $cursection = $doc_obj->get_parent_section ($cursection);
224 } elsif ($tag =~ /^Metadata name=$quot(.*?)$quot/s) {
225 my $metaname = $1;
226 $comment =~ s/^(.*?)$lt\/Metadata$gt//s;
227 my $metavalue = $1;
228 $metavalue =~ s/^\s+//;
229 $metavalue =~ s/\s+$//;
230 # assume that no metadata value intentionally includes
231 # carriage returns or HTML tags (if they're there they
232 # were probably introduced when converting to HTML from
233 # some other format).
234 $metavalue =~ s/[\cJ\cM]/ /sg;
235 $metavalue =~ s/<[^>]+>//sg;
236 $metavalue =~ s/\s+/ /sg;
237 $doc_obj->set_utf8_metadata_element($cursection, $metaname, $metavalue);
238 } elsif ($tag eq "Description" || $tag eq "/Description") {
239 # do nothing with containing Description tags
240 } else {
241 # simple HTML tag (probably created by the conversion
242 # to HTML from some other format) - we'll ignore it and
243 # hope for the best ;-)
244 }
245 }
246 }
247 if ($cursection ne "") {
248 print $outhandle "HTMLPlug: WARNING: $file contains unmatched <Section></Section> tags\n";
249 }
250
251 $$textref =~ s/^.*?<body[^>]*>//is;
252 $$textref =~ s/(<\/body[^>]*>|<\/html[^>]*>)//isg;
253 if ($$textref =~ /\S/) {
254 if (!$found_something) {
255 print $outhandle "HTMLPlug: WARNING: $file appears to contain no Section tags so\n";
256 print $outhandle " will be processed as a single section document\n";
257
258 # go ahead and process single-section document
259 $self->process_section($textref, $base_dir, $file, $doc_obj, $cursection);
260
261 # if document contains no Section tags we'll go ahead
262 # and extract metadata (this won't have been done
263 # above as the -description_tags option prevents it)
264 $self->extract_metadata (\$doc_obj->get_text($cursection), $metadata, $doc_obj, $cursection)
265 unless $self->{'no_metadata'};
266
267 } else {
268 print $outhandle "HTMLPlug: WARNING: $file contains the following text outside\n";
269 print $outhandle " of the final closing </Section> tag. This text will\n";
270 print $outhandle " be ignored.";
271 my ($text);
272 if (length($$textref) > 30) {
273 $text = substr($$textref, 0, 30) . "...";
274 } else {
275 $text = $$textref;
276 }
277 $text =~ s/\n/ /isg;
278 print $outhandle " ($text)\n";
279 }
280 } elsif (!$found_something) {
281
282 # may get to here if document contained no valid Section
283 # tags but did contain some comments. The text will have
284 # been processed already but we should print the warning
285 # as above and extract metadata
286 print $outhandle "HTMLPlug: WARNING: $file appears to contain no Section tags so\n";
287 print $outhandle " will be processed as a single section document\n";
288
289 $self->extract_metadata (\$doc_obj->get_text($cursection), $metadata, $doc_obj, $cursection)
290 unless $self->{'no_metadata'};
291 }
292
293 } else {
294
295 # remove header and footer
296 if (!$self->{'keep_head'} || $self->{'description_tags'}) {
297 $$textref =~ s/^.*?<body[^>]*>//is;
298 $$textref =~ s/(<\/body[^>]*>|<\/html[^>]*>)//isg;
299 }
300
301 # single section document
302 $self->process_section($textref, $base_dir, $file, $doc_obj, $cursection);
303 }
304 return 1;
305}
306
307# note that process_section may be called multiple times for a single
308# section (relying on the fact that add_utf8_text appends the text to any
309# that may exist already).
310sub process_section {
311 my $self = shift (@_);
312 my ($textref, $base_dir, $file, $doc_obj, $cursection) = @_;
313 # trap links
314 if (!$self->{'nolinks'}) {
315
316 # usemap="./#index" not handled correctly => change to "#index"
317 $$textref =~ s/(<img[^>]*?usemap\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/
318 $self->replace_usemap_links($1, $2, $3)/isge;
319
320 $$textref =~ s/(<(?:a|area|frame|link)\s+[^>]*?\s*(?:href|src)\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/
321 $self->replace_href_links ($1, $2, $3, $base_dir, $file, $doc_obj, $cursection)/isge;
322 }
323
324 # trap images
325
326 # allow spaces if inside quotes - jrm21
327 $$textref =~ s/(<img[^>]*?src\s*=\s*)(\"[^\"]+\"|[^\s>]+)([^>]*>)/
328 $self->replace_images ($1, $2, $3, $base_dir, $file, $doc_obj, $cursection)/isge;
329
330 # add text to document object
331 # turn \ into \\ so that the rest of greenstone doesn't think there
332 # is an escape code following. (Macro parsing loses them...)
333 $$textref =~ s/\\/\\\\/go;
334 $doc_obj->add_utf8_text($cursection, $$textref);
335}
336
337sub replace_images {
338 my $self = shift (@_);
339 my ($front, $link, $back, $base_dir,
340 $file, $doc_obj, $section) = @_;
341 # remove quotes from link at start and end if necessary
342 if ($link=~/^\"/) {
343 $link=~s/^\"//;$link=~s/\"$//;
344 $front.='"';
345 $back="\"$back";
346 }
347
348 $link =~ s/\n/ /g;
349 my ($href, $hash_part, $rl) = $self->format_link ($link, $base_dir, $file);
350
351 my $img_file = $self->add_file ($href, $rl, $hash_part, $base_dir, $doc_obj, $section);
352 my $anchor_name = $img_file;
353 $anchor_name =~ s/^.*\///;
354 $anchor_name = "<a name=\"$anchor_name\">";
355
356 return $anchor_name . $front . $img_file . $back;
357}
358
359sub replace_href_links {
360 my $self = shift (@_);
361 my ($front, $link, $back, $base_dir, $file, $doc_obj, $section) = @_;
362
363 # attempt to sort out targets - frames are not handled
364 # well in this plugin and some cases will screw things
365 # up - e.g. the _parent target (so we'll just remove
366 # them all ;-)
367 $front =~ s/(target=\"?)_top(\"?)/$1_gsdltop_$2/is;
368 $back =~ s/(target=\"?)_top(\"?)/$1_gsdltop_$2/is;
369 $front =~ s/target=\"?_parent\"?//is;
370 $back =~ s/target=\"?_parent\"?//is;
371
372 return $front . $link . $back if $link =~ /^\#/s;
373 $link =~ s/\n/ /g;
374
375 my ($href, $hash_part, $rl) = $self->format_link ($link, $base_dir, $file);
376 # href may use '\'s where '/'s should be on Windows
377 $href =~ s/\\/\//g;
378
379 my ($filename) = $href =~ /^(?:.*?):(?:\/\/)?(.*)/;
380
381 ##### leave all these links alone (they won't be picked up by intermediate
382 ##### pages). I think that's safest when dealing with frames, targets etc.
383 ##### (at least until I think of a better way to do it). Problems occur with
384 ##### mailto links from within small frames, the intermediate page is displayed
385 ##### within that frame and can't be seen. There is still potential for this to
386 ##### happen even with html pages - the solution seems to be to somehow tell
387 ##### the browser from the server side to display the page being sent (i.e.
388 ##### the intermediate page) in the top level window - I'm not sure if that's
389 ##### possible - the following line should probably be deleted if that can be done
390 return $front . $link . $back if $href =~ /^(mailto|news|gopher|nntp|telnet|javascript):/is;
391
392
393 if (($rl == 0) || ($filename =~ /$self->{'process_exp'}/) ||
394 ($href =~ /\/$/) || ($href =~ /^(mailto|news|gopher|nntp|telnet|javascript):/i)) {
395 &ghtml::urlsafe ($href);
396 return $front . "_httpextlink_&rl=" . $rl . "&href=" . $href . $hash_part . $back;
397
398 } else {
399 # link is to some other type of file (eg image) so we'll
400 # need to associate that file
401 return $front . $self->add_file ($href, $rl, $hash_part, $base_dir, $doc_obj, $section) . $back;
402 }
403}
404
405sub add_file {
406 my $self = shift (@_);
407 my ($href, $rl, $hash_part, $base_dir, $doc_obj, $section) = @_;
408 my ($newname);
409
410 my $filename = $href;
411 $filename =~ s/^[^:]*:\/\///;
412 $filename = &util::filename_cat($base_dir, $filename);
413
414 # Replace %20's in URL with a space if required. Note that the filename
415 # may include the %20 in some situations
416 if ($filename =~ /\%20/) {
417 if (!-e $filename) {
418 $filename =~ s/\%20/ /g;
419 }
420 }
421
422 my ($ext) = $filename =~ /(\.[^\.]*)$/;
423
424 if ((!defined $ext) || ($ext !~ /$self->{'assoc_files'}/)) {
425 return "_httpextlink_&rl=" . $rl . "&href=" . $href . $hash_part;
426 }
427
428 if ($self->{'rename_assoc_files'}) {
429 if (defined $self->{'aux_files'}->{$href}) {
430 $newname = $self->{'aux_files'}->{$href}->{'dir_num'} . "/" .
431 $self->{'aux_files'}->{$href}->{'file_num'} . $ext;
432 } else {
433 $newname = $self->{'dir_num'} . "/" . $self->{'file_num'} . $ext;
434 $self->{'aux_files'}->{$href} = {'dir_num' => $self->{'dir_num'}, 'file_num' => $self->{'file_num'}};
435 $self->inc_filecount ();
436 }
437 $doc_obj->associate_file($filename, $newname, undef, $section);
438 return "_httpdocimg_/$newname";
439
440 } else {
441 ($newname) = $filename =~ /([^\/\\]*)$/;
442 $doc_obj->associate_file($filename, $newname, undef, $section);
443 return "_httpdocimg_/$newname";
444 }
445}
446
447
448sub format_link {
449 my $self = shift (@_);
450 my ($link, $base_dir, $file) = @_;
451
452 my ($before_hash, $hash_part) = $link =~ /^([^\#]*)(\#?.*)$/;
453
454 $hash_part = "" if !defined $hash_part;
455 if (!defined $before_hash || $before_hash !~ /[\w\.\/]/) {
456 my $outhandle = $self->{'outhandle'};
457 print $outhandle "HTMLPlug: ERROR - badly formatted tag ignored ($link)\n"
458 if $self->{'verbosity'};
459 return ($link, "", 0);
460 }
461
462 if ($before_hash =~ s@^((?:http|ftp|file)://)@@i) {
463 my $type = $1;
464
465 if ($link =~ /^(http|ftp):/i) {
466 # Turn url (using /) into file name (possibly using \ on windows)
467 my @http_dir_split = split('/', $before_hash);
468 $before_hash = &util::filename_cat(@http_dir_split);
469 }
470
471 $before_hash = $self->eval_dir_dots($before_hash);
472
473 my $linkfilename = &util::filename_cat ($base_dir, $before_hash);
474
475 my $rl = 0;
476 $rl = 1 if (-e $linkfilename);
477
478 # make sure there's a slash on the end if it's a directory
479 if ($before_hash !~ /\/$/) {
480 $before_hash .= "/" if (-d $linkfilename);
481 }
482
483 return ($type . $before_hash, $hash_part, $rl);
484
485 } elsif ($link !~ /^(mailto|news|gopher|nntp|telnet|javascript):/i) {
486 if ($before_hash =~ s@^/@@ || $before_hash =~ /\\/) {
487
488 # the first directory will be the domain name if file_is_url
489 # to generate archives, otherwise we'll assume all files are
490 # from the same site and base_dir is the root
491
492 if ($self->{'file_is_url'}) {
493 my @dirs = split /[\/\\]/, $file;
494 my $domname = shift (@dirs);
495 $before_hash = &util::filename_cat($domname, $before_hash);
496 $before_hash =~ s@\\@/@g; # for windows
497 }
498 else
499 {
500 # see if link shares directory with source document
501 # => turn into relative link if this is so!
502
503 if ($ENV{'GSDLOS'} =~ /^windows/i) {
504 # too difficult doing a pattern match with embedded '\'s...
505 my $win_before_hash=$before_hash;
506 $win_before_hash =~ s@(\\)+@/@g;
507 # $base_dir is already similarly "converted" on windows.
508 if ($win_before_hash =~ s@^$base_dir/@@o) {
509 # if this is true, we removed a prefix
510 $before_hash=$win_before_hash;
511 }
512
513 }
514 else {
515 $before_hash = &util::filename_cat("",$before_hash);
516 $before_hash =~ s@^$base_dir/@@;
517 }
518
519 }
520
521 } else {
522 # Turn relative file path into full path
523 my $dirname = &File::Basename::dirname($file);
524 $before_hash = &util::filename_cat($dirname, $before_hash);
525 $before_hash = $self->eval_dir_dots($before_hash);
526 }
527
528 my $linkfilename = &util::filename_cat ($base_dir, $before_hash);
529
530 # make sure there's a slash on the end if it's a directory
531 if ($before_hash !~ /\/$/) {
532 $before_hash .= "/" if (-d $linkfilename);
533 }
534
535 return ("http://" . $before_hash, $hash_part, 1);
536
537 } else {
538 # mailto, news, nntp, telnet, javascript or gopher link
539 return ($before_hash, "", 0);
540 }
541}
542
543sub extract_first_NNNN_characters {
544 my $self = shift (@_);
545 my ($textref, $doc_obj, $thissection) = @_;
546
547 foreach my $size (split /,/, $self->{'first'}) {
548 my $tmptext = $$textref;
549 # skip to the body
550 $tmptext =~ s/.*<body[^>]*>//i;
551 # remove javascript
552 $tmptext =~ s@<script.*?</script>@ @sig;
553 $tmptext =~ s/<[^>]*>/ /g;
554 $tmptext =~ s/&nbsp;/ /g;
555 $tmptext =~ s/^\s+//;
556 $tmptext =~ s/\s+$//;
557 $tmptext =~ s/\s+/ /gs;
558 $tmptext = substr ($tmptext, 0, $size);
559 $tmptext =~ s/\s\S*$/&#8230;/; # adds an ellipse (...)
560 $doc_obj->add_utf8_metadata ($thissection, "First$size", $tmptext);
561 }
562}
563
564
565sub extract_metadata {
566 my $self = shift (@_);
567 my ($textref, $metadata, $doc_obj, $section) = @_;
568 my $outhandle = $self->{'outhandle'};
569 # if we don't want metadata, we may as well not be here ...
570 return if (!defined $self->{'metadata_fields'});
571
572 my %find_fields = (); # metadata fields to extract/save
573
574 my %creator_fields = (); # short-cut for lookups
575
576
577 foreach my $field (split /,/, $self->{'metadata_fields'}) {
578 $find_fields{lc($field)}=$field; # lc = lowercase
579 }
580
581 if (defined $self->{'hunt_creator_metadata'} &&
582 $self->{'hunt_creator_metadata'} == 1 ) {
583 my @extra_fields =
584 (
585 'author',
586 'author.email',
587 'creator',
588 'dc.creator',
589 'dc.creator.corporatename',
590 );
591
592 # add the creator_metadata fields to search for
593 foreach my $field (@extra_fields) {
594 $creator_fields{$field}=0; # add to lookup hash
595 }
596 }
597
598 # find the header in the html file, which has the meta tags
599 $$textref =~ m@<head>(.*?)</head>@si;
600
601 my $html_header=$1;
602
603 # go through every <meta... tag defined in the html and see if it is
604 # one of the tags we want to match.
605
606 # this assumes that ">" won't appear. (I don't think it's allowed to...)
607 $html_header =~ /^/; # match the start of the string, for \G assertion
608 while ($html_header =~ m/\G.*?<meta(.*?)>/sig) {
609 my $metatag=$1;
610 my ($tag, $value);
611
612 # find the tag name
613 $metatag =~ /(?:name|http-equiv)\s*=\s*([\"\'])?(.*?)\1/is;
614 $tag=$2;
615 # in case they're not using " or ', but they should...
616 if (! $tag) {
617 $metatag =~ /(?:name|http-equiv)\s*=\s*(.*?)(?!\w)/is;
618 $tag=$1;
619 }
620
621 if (!defined $tag) {
622 print $outhandle "HTMLPlug: can't find NAME in \"$metatag\"\n";
623 next;
624 }
625
626 # don't need to assign this field if it was passed in from a previous
627 # (recursive) plugin
628 if (defined $metadata->{$tag}) {next}
629
630 # find the tag content
631 $metatag =~ /content\s*=\s*([\"\'])?(.*?)\1/is;
632 $value=$2;
633 if (! $value) {
634 $metatag =~ /(?:name|http-equiv)\s*=\s*(.*?)(?!\w)/is;
635 $value=$1;
636 }
637 if (!defined $value) {
638 print $outhandle "HTMLPlug: can't find VALUE in \"$metatag\"\n";
639 next;
640 }
641
642 # clean up and add
643 $value =~ s/\s+/ /gs;
644 if (exists $creator_fields{lc($tag)}) {
645 # map this value onto greenstone's "Creator" metadata
646 $tag='Creator';
647 } elsif (!exists $find_fields{lc($tag)}) {
648 next; # don't want this tag
649 } else {
650 # get the user's preferred capitalisation
651 $tag = $find_fields{lc($tag)};
652 }
653 print $outhandle " extracted \"$tag\" metadata \"$value\"\n"
654 if ($self->{'verbosity'} > 2);
655 $doc_obj->add_utf8_metadata($section, $tag, $value);
656
657 }
658
659 # TITLE: extract the document title
660 if (exists $find_fields{'title'} && $find_fields{'title'} == 0) {
661 # we want a title, and didn't find one in the meta tags
662 # see if there's a <title> tag
663 my $title;
664 if ($html_header =~ /<title[^>]*>([^<]*)<\/title[^>]*>/is) {
665 $title = $1;
666 }
667 if (!defined $title) {
668 # if no title use first 100 or so characters
669 $title = $$textref;
670 $title =~ s/^.*?<body>//si;
671 # ignore javascript!
672 $title =~ s@<script.*?</script>@ @sig;
673 $title =~ s/<\/([^>]+)><\1>//g; # (eg) </b><b> - no space
674 $title =~ s/<[^>]*>/ /g; # remove all HTML tags
675 $title = substr ($title, 0, 100);
676 $title =~ s/\s\S*$/.../;
677 }
678 $title =~ s/<[^>]*>/ /g; # remove html tags
679 $title =~ s/&nbsp;/ /g;
680 $title =~ s/(?:&nbsp;|\xc2\xa0)/ /g; # utf-8 for nbsp...
681 $title =~ s/\s+/ /gs; # collapse multiple spaces
682 $title =~ s/^\s*//; # remove leading spaces
683 $title =~ s/\s*$//; # remove trailing spaces
684 $title =~ s/^$self->{'title_sub'}// if ($self->{'title_sub'});
685 $title =~ s/^\s+//s; # in case title_sub introduced any...
686 $doc_obj->add_utf8_metadata ($section, 'Title', $title);
687 print $outhandle " extracted Title metadata \"$title\"\n"
688 if ($self->{'verbosity'} > 2);
689 }
690
691 # Special, for metadata names such as tagH1 - extracts
692 # the text between the first <H1> and </H1> tags into "H1" metadata.
693
694 foreach my $field (keys %find_fields) {
695 if ($field !~ /^tag([a-z0-9]+)$/i) {next}
696 my $tag = $1;
697 if ($$textref =~ m@<$tag[^>]*>(.*?)</$tag[^>]*>@g) {
698 my $content = $1;
699 $content =~ s/&nbsp;/ /g;
700 $content =~ s/<[^>]*>/ /g;
701 $content =~ s/^\s+//;
702 $content =~ s/\s+$//;
703 $content =~ s/\s+/ /gs;
704 if ($content) {
705 $tag=$find_fields{"tag$tag"}; # get the user's capitalisation
706 $tag =~ s/^tag//i;
707 $doc_obj->add_utf8_metadata ($section, $tag, $content);
708 print $outhandle " extracted \"$tag\" metadata \"$content\"\n"
709 if ($self->{'verbosity'} > 2);
710 }
711 }
712 }
713}
714
715
716# evaluate any "../" to next directory up
717# evaluate any "./" as here
718sub eval_dir_dots {
719 my $self = shift (@_);
720 my ($filename) = @_;
721
722 my $dirsep_os = &util::get_os_dirsep();
723 my @dirsep = split(/$dirsep_os/,$filename);
724
725 my @eval_dirs = ();
726 foreach my $d (@dirsep) {
727 if ($d eq "..") {
728 pop(@eval_dirs);
729
730 } elsif ($d eq ".") {
731 # do nothing!
732
733 } else {
734 push(@eval_dirs,$d);
735 }
736 }
737
738 return &util::filename_cat(@eval_dirs);
739}
740
741sub replace_usemap_links {
742 my $self = shift (@_);
743 my ($front, $link, $back) = @_;
744
745 $link =~ s/^\.\///;
746 return $front . $link . $back;
747}
748
749sub inc_filecount {
750 my $self = shift (@_);
751
752 if ($self->{'file_num'} == 1000) {
753 $self->{'dir_num'} ++;
754 $self->{'file_num'} = 0;
755 } else {
756 $self->{'file_num'} ++;
757 }
758}
759
760
761# Extend the BasPlug read_file so that strings like &eacute; are
762# converted to UTF8 internally.
763#
764# We don't convert &lt; or &gt; or &amp; or &quot; in case
765# they interfere with the GML files
766
767sub read_file {
768 my ($self, $filename, $encoding, $language, $textref) = @_;
769
770 &BasPlug::read_file($self, $filename, $encoding, $language, $textref);
771
772 # Convert entities to their UTF8 equivalents
773 $$textref =~ s/&(lt|gt|amp|quot|nbsp);/&z$1;/go;
774 $$textref =~ s/&([^;]+);/&ghtml::getcharequiv($1,1)/gseo;
775 $$textref =~ s/&z(lt|gt|amp|quot|nbsp);/&$1;/go;
776}
777
7781;
Note: See TracBrowser for help on using the repository browser.