source: trunk/gsdl/perllib/plugins/ConvertToPlug.pm@ 2041

Last change on this file since 2041 was 2041, checked in by jrm21, 23 years ago

don't strip all whitespace from tmp filename, only from base name.
(Hint: c:/ProgramFiles breaks stuff).

  • Property svn:keywords set to Author Date Id Revision
File size: 10.3 KB
Line 
1###########################################################################
2#
3# ConvertToPlug.pm -- plugin that inherits from HTML or TEXT Plug, depending
4# on plugin argument convert_to
5#
6# A component of the Greenstone digital library software
7# from the New Zealand Digital Library Project at the
8# University of Waikato, New Zealand.
9#
10# Copyright (C) 1999 New Zealand Digital Library Project
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25#
26###########################################################################
27
28# The plugin is inherited by such plugins as WordPlug and PDFPlug.
29# It facilitates the conversion of these document types to either HTML
30# or TEXT by setting up variable that instruct ConvertToBasPlug
31# how to work.
32
33# It works by dynamically inheriting HTMLPlug or TEXTPlug based on
34# the plugin argument 'convert_to'. If the argument is not present,
35# the default is to inherit HTMLPlug.
36
37
38package ConvertToPlug;
39
40use BasPlug;
41use HTMLPlug;
42use TEXTPlug;
43
44sub BEGIN {
45 @ISA = ('HTMLPlug');
46# @ISA = ('HTMLPlug', 'TEXTPlug');
47# @ISA = ('BasPlug'); #, 'HTMLPlug', 'TEXTPlug');
48}
49
50# use strict; # this breaks 'print $outhandle ' error msgs.
51
52sub print_usage {
53 my ($plugin_name) = @_;
54
55 # for when this function is called directly by pluginfo.pl
56 if (ref ($plugin_name)) {
57 $plugin_name = ref ($plugin_name);
58 }
59
60 print STDERR "\n usage: plugin $plugin_name [options]\n\n";
61 print STDERR " options:\n";
62 print STDERR " -convert_to (html|text) plugin converts to TEXT or HTML\n";
63 print STDERR " (default html)\n";
64}
65
66sub parse_args
67{
68 my $class = shift (@_);
69 my ($args) = @_;
70
71 my $plugin_name = $class;
72 $plugin_name =~ s/\.pm$//;
73
74 my $generate_format;
75 my $kea_arg;
76
77 if (!parsargv::parse($args,
78 q^extract_keyphrases^, \$kea_arg->{'kea'}, #with extra options
79 q^extract_keyphrase_options/.*/^, \$kea_arg->{'kea_options'}, #no extra options
80 q^convert_to/(html|text)/html^, \$generate_format,
81 "allow_extra_options")) {
82
83 print STDERR "\nIncorrect options passed to $plugin_name, ";
84 print STDERR "check your collect.cfg configuration file\n";
85 &print_usage($plugin_name);
86 die "\n";
87 }
88
89 return ($plugin_name,$generate_format, $kea_arg);
90}
91
92sub new {
93 my $class = shift (@_);
94 my ($plugin_name,$generate_format, $kea_arg) = $class->parse_args(\@_);
95 my $self;
96
97 if ($generate_format eq "text")
98 {
99 $self = new TEXTPlug ($class, @_);
100 $self->{'convert_to'} = "TEXT";
101 $self->{'convert_to_ext'} = "txt";
102 }
103 else
104 {
105 $self = new HTMLPlug ($class, @_);
106 $self->{'convert_to'} = "HTML";
107 $self->{'convert_to_ext'} = "html";
108
109 $self->{'rename_assoc_files'} = 1;
110 $self->{'metadata_fields'} .= ",GENERATOR";
111 }
112
113 #if kea data to be extracted...
114 $self->{'kea'} = 1 if($kea_arg->{'kea'});
115 $self->{'kea_options'} = 1 if($kea_arg->{'kea_options'});
116
117 return bless $self, $class;
118}
119
120
121
122# Run conversion utility on the input file.
123#
124# The conversion takes place in a collection specific 'tmp' directory so
125# that we don't accidentally damage the input.
126#
127# The desired output type is indicated by $output_ext. This is usually
128# something like "html" or "word", but can be "best" (or the empty string)
129# to indicate that the conversion utility should do the best it can.
130
131sub tmp_area_convert_file {
132 my $self = shift (@_);
133 my ($output_ext,$input_filename, $textref) = @_;
134
135 my $convert_to = $self->{'convert_to'};
136
137 # softlink to collection tmp dir
138 my $colname = &util::use_collection();
139 my $tmp_dirname
140 = &util::filename_cat($ENV{'GSDLHOME'},"collect",$colname,"tmp");
141 &util::mk_dir($tmp_dirname) if (!-e $tmp_dirname);
142
143 # derive tmp filename from input filename
144 my ($tailname,$dirname,$suffix)
145 = File::Basename::fileparse($input_filename,'\.[^\.]+$');
146
147 # Remove any white space from filename -- no risk of name collision, and
148 # makes later conversion by utils simpler. Leave spaces in path...
149 $tailname =~ s/\s+//g;
150
151 my $tmp_filename = &util::filename_cat($tmp_dirname,"$tailname$suffix");
152
153 &util::soft_link($input_filename,$tmp_filename);
154
155 my $verbosity = $self->{'verbosity'};
156 if ($verbosity>0)
157 {
158 print STDERR "Converting $tailname$suffix to $convert_to format\n";
159 }
160
161 # Execute the conversion command and get the type of the result,
162 # making sure the converter gives us the appropriate output type
163 my $output_type = lc($convert_to);
164 my $cmd = "gsConvert.pl -verbose $verbosity -output $output_type \"$tmp_filename\"";
165 $output_type = `$cmd`;
166
167 # Check STDERR here
168
169 chomp $output_type;
170 if ($output_type eq "fail") {
171 print STDERR "Could not convert $tailname$suffix to $convert_to format\n";
172 return "";
173### exit 1;
174 }
175
176 # remove symbolic link to original file
177 &util::rm($tmp_filename);
178
179 # store the *actual* output type and return the output filename
180 $self->{'convert_to_ext'} = $output_type;
181 my $output_filename = $tmp_filename;
182 $output_filename =~ s/$suffix$/.$output_type/;
183
184 return $output_filename;
185}
186
187
188# Remove collection specific tmp directory and all its contents.
189
190sub cleanup_tmp_area {
191 my $self = shift (@_);
192
193 my $colname = &util::use_collection();
194 my $tmp_dirname
195 = &util::filename_cat($ENV{'GSDLHOME'},"collect",$colname,"tmp");
196 &util::rm_r($tmp_dirname);
197 &util::mk_dir($tmp_dirname);
198}
199
200
201
202
203# Override BasPlug read
204# We don't want to get language encoding stuff until after we've converted
205# our file to either TEXT or HTML.
206sub read {
207 my $self = shift (@_);
208 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;
209# if ($self->is_recursive()) {
210# die "BasPlug::read function must be implemented in sub-class for recursive plugins\n";
211# }
212
213 my $outhandle = $self->{'outhandle'};
214
215 my $filename = &util::filename_cat($base_dir, $file);
216 return 0 if $self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/;
217 if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
218 return undef;
219 }
220 my $plugin_name = ref ($self);
221 $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
222
223 # read in file ($text will be in utf8)
224 my $text = "";
225
226 my $output_ext = $self->{'convert_to_ext'};
227 my $conv_filename = $self->tmp_area_convert_file($output_ext,$filename);
228 if ("$conv_filename" eq "") {return 0;} # allows continue on errors
229 if (! -e "$conv_filename") {return 0;} # allows continue on errors
230 $self->{'conv_filename'} = $conv_filename;
231
232# Do encoding stuff
233 my ($language, $encoding);
234 if ($self->{'input_encoding'} eq "auto") {
235 # use textcat to automatically work out the input encoding and language
236 ($language, $encoding) = $self->get_language_encoding ($conv_filename);
237 } elsif ($self->{'extract_language'}) {
238 # use textcat to get language metadata
239
240 my ($language, $extracted_encoding) = $self->get_language_encoding ($conv_filename);
241 $encoding = $self->{'input_encoding'};
242 if ($extracted_encoding ne $encoding && $self->{'verbosity'}) {
243 print $outhandle "$plugin_name: WARNING: $file was read using $encoding encoding but ";
244 print $outhandle "appears to be encoded as $extracted_encoding.\n";
245 }
246 } else {
247 $language = $self->{'default_language'};
248 $encoding = $self->{'input_encoding'};
249 }
250
251 BasPlug::read_file($self,$conv_filename, $encoding, \$text);
252 if (!length ($text)) {
253 print $outhandle "$plugin_name: ERROR: $file contains no text\n" if $self->{'verbosity'};
254 return 0;
255 }
256
257 # create a new document
258 my $doc_obj = new doc ($conv_filename, "indexed_doc");
259
260 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Language",
261 $language);
262 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Encoding",
263 $encoding);
264
265
266 # include any metadata passed in from previous plugins
267 # note that this metadata is associated with the top level section
268 $self->extra_metadata ($doc_obj, $doc_obj->get_top_section(), $metadata);
269 # do plugin specific processing of doc_obj
270 return undef unless defined ($self->process (\$text, $pluginfo, $base_dir, $file, $metadata, $doc_obj));
271 # do any automatic metadata extraction
272 $self->auto_extract_metadata ($doc_obj);
273 # add an OID
274 $doc_obj->set_OID();
275 # process the document
276 $processor->process($doc_obj);
277 $self->cleanup_tmp_area();
278
279
280 return 1;
281}
282
283
284# do plugin specific processing of doc_obj for HTML type
285sub process_type {
286 my $self = shift (@_);
287 my ($doc_ext, $textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
288
289 my $conv_filename = $self->{'conv_filename'};
290 my $tmp_dirname = File::Basename::dirname($conv_filename);
291 my $tmp_tailname = File::Basename::basename($conv_filename);
292
293 my $convert_to = $self->{'convert_to'};
294 my $ret_val;
295
296 if ($convert_to eq "TEXT")
297 {
298
299 $ret_val = TEXTPlug::process($self,$textref,$pluginfo,
300 $tmp_dirname,$tmp_tailname,
301 $metadata,$doc_obj);
302 }
303 else
304 {
305 $ret_val = HTMLPlug::process($self,$textref,$pluginfo,
306 $tmp_dirname,$tmp_tailname,
307 $metadata,$doc_obj);
308 }
309
310 # associate original file with doc object
311 my $cursection = $doc_obj->get_top_section();
312 my $filename = &util::filename_cat($base_dir,$file);
313 $doc_obj->associate_file($filename, "doc.$doc_ext", undef, $cursection);
314
315 my $doclink = "<a href=_httpcollection_/index/assoc/[archivedir]/doc.$doc_ext>";
316 $doc_obj->add_utf8_metadata ($cursection, "srclink", $doclink);
317 $doc_obj->add_utf8_metadata ($cursection, "srcicon", "_icon".$doc_ext."_");
318 $doc_obj->add_utf8_metadata ($cursection, "/srclink", "</a>");
319 return $ret_val;
320}
321
3221;
Note: See TracBrowser for help on using the repository browser.