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

Last change on this file since 2819 was 2819, checked in by sjboddie, 23 years ago

Altered HTMLPlug's description_tags option a bit so it should now also
work for plugins derived from HTMLPlug (i.e. RTFPlug, WordPlug, PDFPlug,
PSPlug etc). Tested briefly with RTFPlug and WordPlug and it seems ok.

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