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

Last change on this file since 850 was 850, checked in by sjboddie, 24 years ago

added use strict - tidied a few things up etc.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 12.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 html;
42use util;
43use parsargv;
44
45sub BEGIN {
46 @ISA = ('BasPlug');
47}
48
49use strict;
50
51sub print_usage {
52 print STDERR "\nIncorrect options passed to HTMLPlug, check your collect.cfg configuration file\n";
53
54 print STDERR "\n usage: plugin HTMLPlug [options]\n\n";
55 print STDERR " options:\n";
56 print STDERR " -process_exp A perl regular expression to match against filenames.\n";
57 print STDERR " Matching filenames will be processed by this plugin.\n";
58 print STDERR " Defaults to '(?i)\.html?\$' i.e. all documents ending in\n";
59 print STDERR " .htm or .html (case-insensitive).\n";
60 print STDERR " -nolinks Don't make any attempt to trap links (setting this flag may\n";
61 print STDERR " improve speed of building/importing but any relative links within\n";
62 print STDERR " documents will be broken).\n";
63 print STDERR " -block_exp Files matching this regular expression will be blocked from\n";
64 print STDERR " being passed to any further plugins in the list. By default\n";
65 print STDERR " HTMLPlug blocks any files with .gif, .jpg, .jpeg, .png, .pdf\n";
66 print STDERR " or .rtf file extensions.\n";
67 print STDERR " -keep_head Don't remove headers from html files.\n";
68 print STDERR " -no_metadata Don't attempt to extract any metadata from files.\n";
69 print STDERR " -metadata_fields Comma separated list of metadata fields to attempt to extract.\n";
70 print STDERR " Defaults to 'Title'\n";
71 print STDERR " -w3mir Set if w3mir was used to generate input file structure.\n\n";
72}
73
74sub new {
75 my $class = shift (@_);
76 my $self = new BasPlug ();
77
78 if (!parsargv::parse(\@_,
79 q^process_exp/.*/(?i)\.html?$^, \$self->{'process_exp'},
80 q^nolinks^, \$self->{'nolinks'},
81 q^block_exp/.*/(?i)\.(gif|jpe?g|png|pdf|rtf)$^, \$self->{'block_exp'},
82 q^keep_head^, \$self->{'keep_head'},
83 q^no_metadata^, \$self->{'no_metadata'},
84 q^metadata_fields/.*/Title^, \$self->{'metadata_fields'},
85 q^w3mir^, \$self->{'w3mir'})) {
86 &print_usage();
87 die "\n";
88 }
89
90 $self->{'aux_files'} = {};
91 $self->{'dir_num'} = 0;
92 $self->{'file_num'} = 0;
93
94 return bless $self, $class;
95}
96
97sub is_recursive {
98 my $self = shift (@_);
99
100 return 0; # this is not a recursive plugin
101}
102
103# return number of files processed, undef if can't process
104# Note that $base_dir might be "" and that $file might
105# include directories
106sub read {
107 my $self = shift (@_);
108 my ($pluginfo, $base_dir, $file, $metadata, $processor) = @_;
109
110 my $filename = &util::filename_cat($base_dir, $file);
111 return 0 if $filename =~ /$self->{'block_exp'}/;
112 if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
113 return undef;
114 }
115 $file =~ s/^[\/\\]+//;
116
117 $self->{'verbosity'} = $processor->{'verbosity'};
118 print STDERR "HTMLPlug: processing $file\n"
119 if $self->{'verbosity'} > 1;
120
121 # create a new document
122 my $doc_obj = new doc ($file, "indexed_doc");
123 my $cursection = $doc_obj->get_top_section();
124
125 # read in HTML file
126 open (FILE, $filename) || die "HTMLPlug::read - can't open $filename\n";
127 undef $/;
128 my $text = <FILE>;
129 $/ = "\n";
130 close FILE;
131 if ($text !~ /\w/) {
132 print STDERR "HTMLPlug: ERROR: $file contains no text\n" if $self->{'verbosity'};
133 return 0;
134 }
135
136 $self->extra_metadata ($doc_obj, $cursection, $metadata);
137 $self->extract_metadata (\$text, $metadata, $doc_obj, $cursection)
138 unless $self->{'no_metadata'};
139
140 # Store URL for page as metadata - this can be used for an
141 # altavista style search interface. The URL won't be valid
142 # unless the file structure contains the domain name (i.e.
143 # like when w3mir is used to download a website).
144 my $web_url = "http://$file";
145 $web_url =~ s/\\/\//g; # for windows
146 $doc_obj->add_metadata($cursection, "URL", $web_url);
147
148 # remove header and footer
149 if (!$self->{'keep_head'}) {
150 $text =~ s/^.*?<body[^>]*>//is;
151 $text =~ s/(<\/body[^>]*>|<\/html[^>]*>)//isg;
152 }
153
154 # trap links
155 if (!$self->{'nolinks'}) {
156
157 # usemap="./#index" not handled correctly => change to "#index"
158 $text =~ s/(<img[^>]*?usemap\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/
159 $self->replace_usemap_links($1, $2, $3)/isge;
160
161 $text =~ s/(<(?:a|area|frame)\s+[^>]*?(?:href|src)\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/
162 $self->replace_href_links ($1, $2, $3, $base_dir, $file, $doc_obj)/isge;
163 }
164
165 # trap images
166 $text =~ s/(<img[^>]*?src\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/
167 $self->replace_images ($1, $2, $3, $base_dir, $file, $doc_obj)/isge;
168
169 $doc_obj->add_text ($cursection, $text);
170
171 # add an OID
172 $doc_obj->set_OID();
173
174 # process the document
175 $processor->process($doc_obj);
176
177 return 1; # processed the file
178}
179
180sub replace_images {
181 my $self = shift (@_);
182 my ($front, $link, $back, $base_dir, $file, $doc_obj) = @_;
183
184 $link =~ s/\n/ /g;
185
186 my ($href, $hash_part, $rl) = $self->format_link ($link, $base_dir, $file);
187 return $front . $self->add_file ($href, $base_dir, $doc_obj) . $back;
188}
189
190sub replace_href_links {
191 my $self = shift (@_);
192 my ($front, $link, $back, $base_dir, $file, $doc_obj) = @_;
193
194 # attempt to sort out targets - frames are not handled
195 # well in this plugin and some cases will screw things
196 # up - e.g. the _parent target (so we'll just remove
197 # them all ;-)
198 $front =~ s/(target=\"?)_top(\"?)/$1_gsdltop_$2/is;
199 $back =~ s/(target=\"?)_top(\"?)/$1_gsdltop_$2/is;
200 $front =~ s/target=\"?_parent\"?//is;
201 $back =~ s/target=\"?_parent\"?//is;
202
203 return $front . $link . $back if $link =~ /^\#/s;
204 $link =~ s/\n/ /g;
205
206 my ($href, $hash_part, $rl) = $self->format_link ($link, $base_dir, $file);
207
208 my ($filename) = $href =~ /^(?:.*?):(?:\/\/)?(.*)/;
209 if (($rl == 0) || ($filename =~ /$self->{'process_exp'}/) ||
210 ($href =~ /\/$/) || ($href =~ /^(mailto|news|gopher|nntp|telnet|javascript):/i)) {
211 $link = $href . $hash_part;
212 &html::urlsafe ($link);
213 return $front . "_httpextlink_&href=" . $link . "&rl=" . $rl . $back;
214
215 } else {
216 # link is to some other type of file (image, pdf etc.) so we'll
217 # need to associate that file
218 return $front . $self->add_file ($href, $base_dir, $doc_obj) . $back;
219 }
220}
221
222sub add_file {
223 my $self = shift (@_);
224 my ($href, $base_dir, $doc_obj) = @_;
225 my ($newname);
226
227 my $filename = $href;
228 $filename =~ s/^[^:]*:\/\///;
229 $filename = &util::filename_cat ($base_dir, $filename);
230 my ($ext) = $filename =~ /(\.[^\.]*)$/;
231 if (defined $self->{'aux_files'}->{$href}) {
232 $newname = $self->{'aux_files'}->{$href}->{'dir_num'} . "/" .
233 $self->{'aux_files'}->{$href}->{'file_num'} . $ext;
234 } else {
235 $newname = $self->{'dir_num'} . "/" . $self->{'file_num'} . $ext;
236 $self->inc_filecount ();
237 }
238 $doc_obj->associate_file($filename, $newname);
239 return "_httpcollimg_/$newname";
240}
241
242
243sub format_link {
244 my $self = shift (@_);
245 my ($link, $base_dir, $file) = @_;
246
247 my ($before_hash, $hash_part) = $link =~ /^([^\#]*)(\#?.*)$/;
248 $hash_part = "" if !defined $hash_part;
249 if (!defined $before_hash || $before_hash !~ /[\w\.\/]/) {
250 print STDERR "HTMLPlug: ERROR - badly formatted tag ignored ($link)\n"
251 if $self->{'verbosity'};
252 return ($link, "", 0);
253 }
254
255 if ($before_hash =~ s/^((?:http|ftp|file):\/\/)//i) {
256 my $type = $1;
257
258 if ($link =~ /^(http|ftp):/i) {
259 # Turn url (using /) into file name (possibly using \ on windows)
260 my @http_dir_split = split('/', $before_hash);
261 $before_hash = &util::filename_cat(@http_dir_split);
262 }
263
264 $before_hash = $self->eval_dir_dots($before_hash);
265
266 my $linkfilename = &util::filename_cat ($base_dir, $before_hash);
267
268 my $rl = 0;
269 $rl = 1 if (-e $linkfilename);
270
271 # make sure there's a slash on the end if it's a directory
272 if ($before_hash !~ /\/$/) {
273 $before_hash .= "/" if (-d $linkfilename);
274 }
275
276 return ($type . $before_hash, $hash_part, $rl);
277
278 } elsif ($link !~ /^(mailto|news|gopher|nntp|telnet|javascript):/i) {
279
280 if ($before_hash =~ s/^\///) {
281 # the first directory will be the domain name if w3mir was used
282 # to generate archives, otherwise we'll assume all files are
283 # from the same site and base_dir is the root
284 if ($self->{'w3mir'}) {
285 my @dirs = split /[\/\\]/, $file;
286 my $domname = shift (@dirs);
287 $before_hash = &util::filename_cat($domname, $before_hash);
288 $before_hash =~ s/\\/\//g; # for windows
289 }
290
291 } else {
292 # Turn relative file path into full path
293 my $dirname = &File::Basename::dirname($file);
294 $before_hash = &util::filename_cat($dirname, $before_hash);
295 $before_hash = $self->eval_dir_dots($before_hash);
296 }
297
298 # make sure there's a slash on the end if it's a directory
299 my $linkfilename = &util::filename_cat ($base_dir, $before_hash);
300 if ($before_hash !~ /\/$/) {
301 $before_hash .= "/" if (-d $linkfilename);
302 }
303
304 return ("http://" . $before_hash, $hash_part, 1);
305
306 } else {
307 # mailto, news, nntp, telnet, javascript or gopher link
308 return ($before_hash, "", 0);
309 }
310}
311
312sub extract_metadata {
313 my $self = shift (@_);
314 my ($textref, $metadata, $doc_obj, $section) = @_;
315
316 foreach my $field (split /,/, $self->{'metadata_fields'}) {
317
318 # don't need to extract field if it was passed in from a previous
319 # (recursive) plugin
320 next if defined $metadata->{$field};
321
322 # see if there's a <meta> tag for this field
323 if ($$textref =~ /<meta(.*?)(?:name|http-equiv)\s*=\s*\"?$field\"?([^>]*)/is) {
324 my $content = $1 . $2;
325 if ($content =~ /content\s*=\s*\"?(.*?)\"?/is) {
326 if (defined $1) {
327 my $value = $1;
328 $value =~ s/\s+/ /gs;
329 $doc_obj->add_metadata($section, $field, $value);
330 next;
331 }
332 }
333 }
334
335 # special case for Title metadata - try <title> tags
336 # then first 100 characters of text
337
338 if ($field =~ /^title$/i) {
339
340 # see if there's a <title> tag
341 if ($$textref =~ /<title[^>]*>([^<]*)<\/title[^>]*>/is) {
342 if (defined $1) {
343 my $title = $1;
344 if ($title =~ /\w/) {
345 $title =~ s/\s+/ /gs;
346 $doc_obj->add_metadata ($section, $field, $title);
347 next;
348 }
349 }
350 }
351
352 # if no title use first 100 characters
353 my $tmptext = $$textref;
354 $tmptext =~ s/<[^>]*>//g;
355 my $title = substr ($tmptext, 0, 100);
356 $title =~ s/\s+/ /gs;
357 $doc_obj->add_metadata ($section, $field, $title);
358 }
359 }
360}
361
362# evaluate any "../" to next directory up
363# evaluate any "./" as here
364sub eval_dir_dots {
365 my $self = shift (@_);
366 my ($filename) = @_;
367
368 my $dirsep_os = &util::get_os_dirsep();
369 my @dirsep = split(/$dirsep_os/,$filename);
370
371 my @eval_dirs = ();
372 foreach my $d (@dirsep) {
373 if ($d eq "..") {
374 pop(@eval_dirs);
375
376 } elsif ($d eq ".") {
377 # do nothing!
378
379 } else {
380 push(@eval_dirs,$d);
381 }
382 }
383
384 return &util::filename_cat(@eval_dirs);
385}
386
387sub replace_usemap_links {
388 my $self = shift (@_);
389 my ($front, $link, $back) = @_;
390
391 $link =~ s/^\.\///;
392 return $front . $link . $back;
393}
394
395sub inc_filecount {
396 my $self = shift (@_);
397
398 if ($self->{'file_num'} == 1000) {
399 $self->{'dir_num'} ++;
400 $self->{'file_num'} = 0;
401 } else {
402 $self->{'file_num'} ++;
403 }
404}
405
4061;
Note: See TracBrowser for help on using the repository browser.