source: branches/New_Config_Format-branch/gsdl/perllib/plugins/HTMLPlug.pm@ 1279

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

merged changes to trunk into New_Config_Format branch

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 14.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 util;
43use parsargv;
44
45sub BEGIN {
46 @ISA = ('BasPlug');
47}
48
49use strict;
50
51sub print_usage {
52 print STDERR "\n usage: plugin HTMLPlug [options]\n\n";
53 print STDERR " options:\n";
54 print STDERR " -nolinks Don't make any attempt to trap links (setting this flag may\n";
55 print STDERR " improve speed of building/importing but any relative links within\n";
56 print STDERR " documents will be broken).\n";
57 print STDERR " -keep_head Don't remove headers from html files.\n";
58 print STDERR " -no_metadata Don't attempt to extract any metadata from files.\n";
59 print STDERR " -metadata_fields Comma separated list of metadata fields to attempt to extract.\n";
60 print STDERR " Defaults to 'Title'.\n";
61 print STDERR " Use `first200` to get the first 200 characters of the body.\n";
62 print STDERR " Use `H1` to get the text inside the first <H1> and </H1> tags in the text.\n";
63 print STDERR " -w3mir Set if w3mir was used to generate input file structure.\n";
64 print STDERR " -assoc_files Perl regular expression of file extensions to associate with\n";
65 print STDERR " html documents. Defaults to '(?i)\.(jpe?g|gif|png|css|pdf)\$'\n";
66 print STDERR " -rename_assoc_files Renames files associated with documents (e.g. images). Also\n";
67 print STDERR " creates much shallower directory structure (useful when creating\n";
68 print STDERR " collections to go on cd-rom).\n\n";
69}
70
71sub new {
72 my $class = shift (@_);
73 my $self = new BasPlug ("HTMLPlug", @_);
74
75 if (!parsargv::parse(\@_,
76 q^nolinks^, \$self->{'nolinks'},
77 q^keep_head^, \$self->{'keep_head'},
78 q^no_metadata^, \$self->{'no_metadata'},
79 q^metadata_fields/.*/Title^, \$self->{'metadata_fields'},
80 q^w3mir^, \$self->{'w3mir'},
81 q^assoc_files/.*/(?i)\.(jpe?g|gif|png|css|pdf)$^, \$self->{'assoc_files'},
82 q^rename_assoc_files^, \$self->{'rename_assoc_files'},
83 "allow_extra_options")) {
84
85 print STDERR "\nIncorrect options passed to HTMLPlug, check your collect.cfg configuration file\n";
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
97
98sub get_default_block_exp {
99 my $self = shift (@_);
100
101 return q^(?i)\.(gif|jpe?g|png|pdf|rtf|css)$^;
102}
103
104sub get_default_process_exp {
105 my $self = shift (@_);
106
107 return q^(?i)\.html?$^;
108}
109
110
111# do plugin specific processing of doc_obj
112sub process {
113 my $self = shift (@_);
114 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
115
116 print STDERR "HTMLPlug: processing $file\n"
117 if $self->{'verbosity'} > 1;
118
119 my $cursection = $doc_obj->get_top_section();
120
121 $self->extract_metadata ($textref, $metadata, $doc_obj, $cursection)
122 unless $self->{'no_metadata'};
123
124 # Store URL for page as metadata - this can be used for an
125 # altavista style search interface. The URL won't be valid
126 # unless the file structure contains the domain name (i.e.
127 # like when w3mir is used to download a website).
128 my $web_url = "http://$file";
129 $web_url =~ s/\\/\//g; # for windows
130 $doc_obj->add_utf8_metadata($cursection, "URL", $web_url);
131
132 # remove header and footer
133 if (!$self->{'keep_head'}) {
134 $$textref =~ s/^.*?<body[^>]*>//is;
135 $$textref =~ s/(<\/body[^>]*>|<\/html[^>]*>)//isg;
136 }
137
138 # trap links
139 if (!$self->{'nolinks'}) {
140
141 # usemap="./#index" not handled correctly => change to "#index"
142 $$textref =~ s/(<img[^>]*?usemap\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/
143 $self->replace_usemap_links($1, $2, $3)/isge;
144
145 $$textref =~ s/(<(?:a|area|frame|link)\s+[^>]*? (?:href|src)\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/
146 $self->replace_href_links ($1, $2, $3, $base_dir, $file, $doc_obj, $cursection)/isge;
147 }
148
149 # trap images
150 $$textref =~ s/(<img[^>]*? src\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/
151 $self->replace_images ($1, $2, $3, $base_dir, $file, $doc_obj, $cursection)/isge;
152
153 # add text to document object
154 $doc_obj->add_utf8_text($cursection, "<pre>\n$$textref\n</pre>");
155
156 return 1;
157}
158
159sub replace_images {
160 my $self = shift (@_);
161 my ($front, $link, $back, $base_dir,
162 $file, $doc_obj, $section) = @_;
163
164 $link =~ s/\n/ /g;
165
166 my ($href, $hash_part, $rl) = $self->format_link ($link, $base_dir, $file);
167 return $front . $self->add_file ($href, $rl, $hash_part, $base_dir, $doc_obj, $section) . $back;
168}
169
170sub replace_href_links {
171 my $self = shift (@_);
172 my ($front, $link, $back, $base_dir, $file, $doc_obj, $section) = @_;
173
174 # attempt to sort out targets - frames are not handled
175 # well in this plugin and some cases will screw things
176 # up - e.g. the _parent target (so we'll just remove
177 # them all ;-)
178 $front =~ s/(target=\"?)_top(\"?)/$1_gsdltop_$2/is;
179 $back =~ s/(target=\"?)_top(\"?)/$1_gsdltop_$2/is;
180 $front =~ s/target=\"?_parent\"?//is;
181 $back =~ s/target=\"?_parent\"?//is;
182
183 return $front . $link . $back if $link =~ /^\#/s;
184 $link =~ s/\n/ /g;
185
186 my ($href, $hash_part, $rl) = $self->format_link ($link, $base_dir, $file);
187
188 my ($filename) = $href =~ /^(?:.*?):(?:\/\/)?(.*)/;
189
190 ##### leave all these links alone (they won't be picked up by intermediate
191 ##### pages). I think that's safest when dealing with frames, targets etc.
192 ##### (at least until I think of a better way to do it). Problems occur with
193 ##### mailto links from within small frames, the intermediate page is displayed
194 ##### within that frame and can't be seen. There is still potential for this to
195 ##### happen even with html pages - the solution seems to be to somehow tell
196 ##### the browser from the server side to display the page being sent (i.e.
197 ##### the intermediate page) in the top level window - I'm not sure if that's
198 ##### possible - the following line should probably be deleted if that can be done
199 return $front . $link . $back if $href =~ /^(mailto|news|gopher|nntp|telnet|javascript):/is;
200
201
202 if (($rl == 0) || ($filename =~ /$self->{'process_exp'}/) ||
203 ($href =~ /\/$/) || ($href =~ /^(mailto|news|gopher|nntp|telnet|javascript):/i)) {
204 &ghtml::urlsafe ($href);
205 return $front . "_httpextlink_&rl=" . $rl . "&href=" . $href . $hash_part . $back;
206
207 } else {
208 # link is to some other type of file (image, pdf etc.) so we'll
209 # need to associate that file
210 return $front . $self->add_file ($href, $rl, $hash_part, $base_dir, $doc_obj, $section) . $back;
211 }
212}
213
214sub add_file {
215 my $self = shift (@_);
216 my ($href, $rl, $hash_part, $base_dir, $doc_obj, $section) = @_;
217 my ($newname);
218
219 my $filename = $href;
220 $filename =~ s/^[^:]*:\/\///;
221 $filename = &util::filename_cat ($base_dir, $filename);
222 my ($ext) = $filename =~ /(\.[^\.]*)$/;
223
224 if ((!defined $ext) || ($ext !~ /$self->{'assoc_files'}/)) {
225 return "_httpextlink_&rl=" . $rl . "&href=" . $href . $hash_part;
226 }
227
228 if ($self->{'rename_assoc_files'}) {
229 if (defined $self->{'aux_files'}->{$href}) {
230 $newname = $self->{'aux_files'}->{$href}->{'dir_num'} . "/" .
231 $self->{'aux_files'}->{$href}->{'file_num'} . $ext;
232 } else {
233 $newname = $self->{'dir_num'} . "/" . $self->{'file_num'} . $ext;
234 $self->{'aux_files'}->{$href} = {'dir_num' => $self->{'dir_num'}, 'file_num' => $self->{'file_num'}};
235 $self->inc_filecount ();
236 }
237 $doc_obj->associate_file($filename, $newname, undef, $section);
238 return "_httpcollimg_/$newname";
239
240 } else {
241 ($newname) = $filename =~ /([^\/\\]*)$/;
242 $doc_obj->associate_file($filename, $newname, undef, $section);
243 return "_httpdocimg_/$newname";
244 }
245}
246
247
248sub format_link {
249 my $self = shift (@_);
250 my ($link, $base_dir, $file) = @_;
251
252 my ($before_hash, $hash_part) = $link =~ /^([^\#]*)(\#?.*)$/;
253 $hash_part = "" if !defined $hash_part;
254 if (!defined $before_hash || $before_hash !~ /[\w\.\/]/) {
255 print STDERR "HTMLPlug: ERROR - badly formatted tag ignored ($link)\n"
256 if $self->{'verbosity'};
257 return ($link, "", 0);
258 }
259
260 if ($before_hash =~ s/^((?:http|ftp|file):\/\/)//i) {
261 my $type = $1;
262
263 if ($link =~ /^(http|ftp):/i) {
264 # Turn url (using /) into file name (possibly using \ on windows)
265 my @http_dir_split = split('/', $before_hash);
266 $before_hash = &util::filename_cat(@http_dir_split);
267 }
268
269 $before_hash = $self->eval_dir_dots($before_hash);
270
271 my $linkfilename = &util::filename_cat ($base_dir, $before_hash);
272
273 my $rl = 0;
274 $rl = 1 if (-e $linkfilename);
275
276 # make sure there's a slash on the end if it's a directory
277 if ($before_hash !~ /\/$/) {
278 $before_hash .= "/" if (-d $linkfilename);
279 }
280
281 return ($type . $before_hash, $hash_part, $rl);
282
283 } elsif ($link !~ /^(mailto|news|gopher|nntp|telnet|javascript):/i) {
284
285 if ($before_hash =~ s/^\///) {
286 # the first directory will be the domain name if w3mir was used
287 # to generate archives, otherwise we'll assume all files are
288 # from the same site and base_dir is the root
289 if ($self->{'w3mir'}) {
290 my @dirs = split /[\/\\]/, $file;
291 my $domname = shift (@dirs);
292 $before_hash = &util::filename_cat($domname, $before_hash);
293 $before_hash =~ s/\\/\//g; # for windows
294 }
295
296 } else {
297 # Turn relative file path into full path
298 my $dirname = &File::Basename::dirname($file);
299 $before_hash = &util::filename_cat($dirname, $before_hash);
300 $before_hash = $self->eval_dir_dots($before_hash);
301 }
302
303 # make sure there's a slash on the end if it's a directory
304 my $linkfilename = &util::filename_cat ($base_dir, $before_hash);
305 if ($before_hash !~ /\/$/) {
306 $before_hash .= "/" if (-d $linkfilename);
307 }
308
309 return ("http://" . $before_hash, $hash_part, 1);
310
311 } else {
312 # mailto, news, nntp, telnet, javascript or gopher link
313 return ($before_hash, "", 0);
314 }
315}
316
317sub extract_metadata {
318 my $self = shift (@_);
319 my ($textref, $metadata, $doc_obj, $section) = @_;
320
321 foreach my $field (split /,/, $self->{'metadata_fields'}) {
322
323 # don't need to extract field if it was passed in from a previous
324 # (recursive) plugin
325 next if defined $metadata->{$field};
326
327 # see if there's a <meta> tag for this field
328 if ($$textref =~ /<meta(.*?)(?:name|http-equiv)\s*=\s*\"?$field\"?([^>]*)/is) {
329 my $content = $1 . $2;
330 if ($content =~ /content\s*=\s*\"?(.*?)\"?/is) {
331 if (defined $1) {
332 my $value = $1;
333 $value =~ s/\s+/ /gs;
334 $doc_obj->add_utf8_metadata($section, $field, $value);
335 next;
336 }
337 }
338 }
339
340 # TITLE: extract the document title
341
342 if ($field =~ /^title$/i) {
343
344 # see if there's a <title> tag
345 if ($$textref =~ /<title[^>]*>([^<]*)<\/title[^>]*>/is) {
346 if (defined $1) {
347 my $title = $1;
348 if ($title =~ /\w/) {
349 $title =~ s/\s+/ /gs;
350 $title =~ s/^\s+//;
351 $title =~ s/\s+$//;
352 $doc_obj->add_utf8_metadata ($section, $field, $title);
353 next;
354 }
355 }
356 }
357
358 # if no title use first 100 characters
359 my $tmptext = $$textref;
360 $tmptext =~ s/\s+/ /gs;
361 $tmptext =~ s/<[^>]*>//g;
362 $tmptext = substr ($tmptext, 0, 100);
363 $tmptext =~ s/^\s+//;
364 $tmptext =~ s/\s+$//;
365 $tmptext =~ s/\s\S*$/.../;
366 $doc_obj->add_utf8_metadata ($section, $field, $tmptext);
367 next;
368 }
369
370 # FIRST200: extract the first 200 characters as metadata
371
372 if ($field =~ /^first200$/i) {
373 my $tmptext = $$textref;
374 $tmptext =~ s/\s+/ /gs;
375 $tmptext =~ s/.*<body[^>]*>//i;
376 $tmptext =~ s/<[^>]*>//g;
377 $tmptext = substr ($tmptext, 0, 200);
378 $tmptext =~ s/^\s+//;
379 $tmptext =~ s/\s+$//;
380 $tmptext =~ s/\s\S*$/.../;
381 $doc_obj->add_utf8_metadata ($section, $field, $tmptext);
382 next;
383 }
384
385 # H1: extract the text between the first <H1> and </H1> tags
386 if ($field =~ /^H1$/i) {
387 my $tmptext = $$textref;
388 $tmptext =~ s/\s+/ /gs;
389 if ($tmptext =~ /<H1[^>]*>/i) {
390 $tmptext =~ s/.*<H1[^>]*>//i;
391 $tmptext =~ s/<\/H1[^>]*>.*//i;
392 $tmptext =~ s/^\s+//;
393 $tmptext =~ s/\s+$//;
394 $doc_obj->add_utf8_metadata ($section, $field, $tmptext);
395 }
396 next;
397 }
398 }
399}
400
401
402# evaluate any "../" to next directory up
403# evaluate any "./" as here
404sub eval_dir_dots {
405 my $self = shift (@_);
406 my ($filename) = @_;
407
408 my $dirsep_os = &util::get_os_dirsep();
409 my @dirsep = split(/$dirsep_os/,$filename);
410
411 my @eval_dirs = ();
412 foreach my $d (@dirsep) {
413 if ($d eq "..") {
414 pop(@eval_dirs);
415
416 } elsif ($d eq ".") {
417 # do nothing!
418
419 } else {
420 push(@eval_dirs,$d);
421 }
422 }
423
424 return &util::filename_cat(@eval_dirs);
425}
426
427sub replace_usemap_links {
428 my $self = shift (@_);
429 my ($front, $link, $back) = @_;
430
431 $link =~ s/^\.\///;
432 return $front . $link . $back;
433}
434
435sub inc_filecount {
436 my $self = shift (@_);
437
438 if ($self->{'file_num'} == 1000) {
439 $self->{'dir_num'} ++;
440 $self->{'file_num'} = 0;
441 } else {
442 $self->{'file_num'} ++;
443 }
444}
445
4461;
Note: See TracBrowser for help on using the repository browser.