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

Last change on this file since 3369 was 3369, checked in by sjboddie, 22 years ago

HTMLPlug will no longer prevent metadata extraction when the
-description_tags option is set unless it actually finds at least one
<Section> tag in the document. This allows documents with <section> tags
and those without to be built in the same collection without getting an
unexpected lack of metadata for the latter.

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