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

Last change on this file since 617 was 617, checked in by sjboddie, 25 years ago

a few fixes

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 KB
Line 
1###########################################################################
2#
3# HTMLPlug.pm -- basic html plugin
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26# creates simple single-level document from .htm or .html files
27# (case-insensitive match on filenames). Adds Title metadata
28# taken from <title> tags if found otherwise first 100 characters
29# outside of tags.
30# will also attempt to include images which it will search for in
31# the same directory as the document itself (it will also search
32# directories relative to that directory).
33
34# this plugin currently does nothing with href links so relative links
35# may become broken.
36
37
38package HTMLPlug;
39
40use BasPlug;
41use sorttools;
42use util;
43
44sub BEGIN {
45 @ISA = ('BasPlug');
46}
47
48sub new {
49 my ($class) = @_;
50 $self = new BasPlug ();
51
52 return bless $self, $class;
53}
54
55sub is_recursive {
56 my $self = shift (@_);
57
58 return 0; # this is not a recursive plugin
59}
60
61
62# return number of files processed, undef if can't process
63# Note that $base_dir might be "" and that $file might
64# include directories
65sub read {
66 my $self = shift (@_);
67 my ($pluginfo, $base_dir, $file, $metadata, $processor) = @_;
68
69 my $filename = &util::filename_cat($base_dir, $file);
70 my $absdir = $filename;
71 $absdir =~ s/[^\/\\]*$//;
72
73 return undef unless ($filename =~ /\.(html?(\.gz)?)$/i && (-e $filename));
74
75 my $gz = 0;
76 if (defined $2) {
77 $gz = $2;
78 $gz = 1 if ($gz =~ /\.gz/i);
79 }
80
81 print STDERR "HTMLPlug: processing $filename\n" if $processor->{'verbosity'};
82
83 # create a new document
84 my $doc_obj = new doc ($file, "indexed_doc");
85
86 if ($gz) {
87 open (FILE, "zcat $filename |") || die "HTMLPlug::read - zcat can't open $filename\n";
88 } else {
89 open (FILE, $filename) || die "HTMLPlug::read - can't open $filename\n";
90 }
91 my $cursection = $doc_obj->get_top_section();
92
93 my $text = "";
94 my $line = "";
95 my $title = "";
96 while (defined ($line = <FILE>)) {
97 $text .= $line;
98 }
99
100 # we'll use the worthless alarm thingy to temporarily replace
101 # '\n' so we'd better check it doesn't occur naturally
102 if ($text =~ /\a/) {
103 print STDERR "HTMLPlug::read - 'WARNING '\a' character occurs in text!!\n";
104 }
105
106 # remove line breaks
107 $text =~ s/\n/\a/g;
108
109 # see if there's a <title> tag
110 my $foundtitle = 0;
111 if ($text =~ /<title[^>]*>([^<]*)<\/title[^>]*>/i) {
112 if (defined $1) {
113 my $title = $1;
114 if ($title =~ /\w/) {
115 $doc_obj->add_metadata ($cursection, "Title", $title);
116 $foundtitle = 1;
117 }
118 }
119 }
120 # if no title use first 100 characters
121 if (!$foundtitle) {
122 my $tmptext = $text;
123 $tmptext =~ s/<[^>]*>//g;
124 my $title = substr ($tmptext, 0, 100);
125 $doc_obj->add_metadata ($cursection, "Title", $title);
126 }
127
128 # remove header rubbish
129 $text =~ s/^.*?<body[^>]*>//i;
130
131 # and any other unwanted tags
132 $text =~ s/<(\/p|\/html|\/body)>//g;
133
134 # fix up the image links
135 $text =~ s/(<img[^>]*?src\s*=\s*\"?)([^\">]+)(\"?[^>]*>)/
136 &replace_image_links($absdir, $doc_obj, $1, $2, $3)/ige;
137
138 # put line breaks back in
139 $text =~ s/\a/\n/g;
140
141 $doc_obj->add_text ($cursection, $text);
142
143 foreach $field (keys(%$metadata)) {
144 # $metadata->{$field} may be an array reference
145 if (ref ($metadata->{$field}) eq "ARRAY") {
146 map {
147 $doc_obj->add_metadata ($cursection, $field, $_);
148 } @{$metadata->{$field}};
149 } else {
150 $doc_obj->add_metadata ($cursection, $field, $metadata->{$field});
151 }
152 }
153
154 # add OID
155 $doc_obj->set_OID ();
156
157 # process the document
158 $processor->process($doc_obj);
159
160 return 1; # processed the file
161}
162
163sub replace_image_links {
164
165 my ($dir, $doc_obj, $front, $link, $back) = @_;
166
167 my ($filename, $error);
168 my $foundimage = 0;
169
170 $link =~ s/\/\///;
171 my ($imagetype) = $link =~ /([^\.]*)$/;
172 $imagetype =~ tr/[A-Z]/[a-z]/;
173 if ($imagetype eq "jpg") {$imagetype = "jpeg";}
174 if ($imagetype !~ /^(jpg|gif|png)$/) {
175 print STDERR "HTMLPlug: Warning - unknown image type ($imagetype)\n";
176 }
177 my ($imagefile) = $link =~ /([^\/]*)$/;
178 my ($imagepath) = $link =~ /^[^\/]*(.*)$/;
179
180 if (defined $imagepath && $imagepath =~ /\w/) {
181 # relative link
182 $filename = &util::filename_cat ($dir, $imagepath);
183 if (-e $filename) {
184 $doc_obj->associate_file ($filename, $imagefile, "image/$imagetype");
185 $foundimage = 1;
186 } else {
187 $error = "HTMLPlug: Warning - couldn't find image file $imagefile in either $filename or";
188 }
189 }
190
191 if (!$foundimage) {
192 $filename = &util::filename_cat ($dir, $imagefile);
193 if (-e $filename) {
194 $doc_obj->associate_file ($filename, $imagefile, "image/$imagetype");
195 $foundimage = 1;
196 } elsif (defined $error) {
197 print STDERR "$error $filename\n";
198 } else {
199 print STDERR "HTMLPlug: Warning - couldn't find image file $imagefile in $filename\n";
200 }
201 }
202
203 if ($foundimage) {
204 return "${front}_httpcollection_/archives/_thisOID_/${imagefile}${back}";
205 } else {
206 return "";
207 }
208}
209
2101;
211
212
213
214
215
216
217
218
219
220
221
Note: See TracBrowser for help on using the repository browser.