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

Last change on this file since 3148 was 3148, checked in by jrm21, 22 years ago

If a document has associated files that are also given a subdirectory, that subdirectory is now a subdir of the HASH... dir and not just a subdir of the assoc dir. If the given subdir starts with a '/' then it will be a subdir of the assoc dir.

Eg
"1.jpg" will be put into building/assoc/HASHnnn.dir/1.jpg
"0/1.jpg" will be put into building/assoc/HASHnnn.dir/0/1.jpg
"/foo/1.jpg" -> building/assoc/foo/1.jpg

This is because HTMLPlug was putting first 1000 assoc files into "assoc/0", next thousand into "assoc/1", etc. But this meant that if you updated a collection, files in "0" would get overridden.

The only change to HTMLPlug is to use the _httpdocimg_ macro instead of the _httpcolling_ so we link to the right place.

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