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

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

Fixed a little bug that was causing pluginfo.pl to print some dodgy looking
stuff for plugins inheriting from ConvertToPlug

  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 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
50use strict;
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 if (!parsargv::parse($args,
76 q^convert_to/(html|text)/html^, \$generate_format,
77 "allow_extra_options")) {
78
79 print STDERR "\nIncorrect options passed to $plugin_name, ";
80 print STDERR "check your collect.cfg configuration file\n";
81 &print_usage($plugin_name);
82 die "\n";
83 }
84
85 return ($plugin_name,$generate_format);
86}
87
88sub new {
89 my $class = shift (@_);
90 my ($plugin_name,$generate_format) = $class->parse_args(\@_);
91 my $self;
92
93 if ($generate_format eq "text")
94 {
95 $self = new TEXTPlug ($class, @_);
96 $self->{'convert_to'} = "TEXT";
97 $self->{'convert_to_ext'} = "txt";
98 }
99 else
100 {
101 $self = new HTMLPlug ($class, @_);
102 $self->{'convert_to'} = "HTML";
103 $self->{'convert_to_ext'} = "html";
104
105 $self->{'rename_assoc_files'} = 1;
106 $self->{'metadata_fields'} .= ",GENERATOR";
107 }
108
109 return bless $self, $class;
110}
111
112
113
114# Run conversion utility on the input file.
115#
116# The conversion takes place in a collection specific 'tmp' directory so
117# that we don't accidentally damage the input.
118#
119# The desired output type is indicated by $output_ext. This is usually
120# something like "html" or "word", but can be "best" (or the empty string)
121# to indicate that the conversion utility should do the best it can.
122
123sub tmp_area_convert_file {
124 my $self = shift (@_);
125 my ($output_ext,$input_filename, $textref) = @_;
126
127 my $convert_to = $self->{'convert_to'};
128
129 # softlink to collection tmp dir
130 my $colname = &util::use_collection();
131 my $tmp_dirname
132 = &util::filename_cat($ENV{'GSDLHOME'},"collect",$colname,"tmp");
133 &util::mk_dir($tmp_dirname) if (!-e $tmp_dirname);
134
135 # derive tmp filename from input filename
136 my ($tailname,$dirname,$suffix)
137 = File::Basename::fileparse($input_filename,'\.[^\.]+$');
138
139 my $tmp_filename = &util::filename_cat($tmp_dirname,"$tailname$suffix");
140 # Remove any white space from filename -- no risk of name collision, and
141 # makes later conversion by utils simpler
142 $tmp_filename =~ s/\s+//g;
143
144 &util::soft_link($input_filename,$tmp_filename);
145
146 my $verbosity = $self->{'verbosity'};
147 if ($verbosity>0)
148 {
149 print STDERR "Converting $tailname$suffix to $convert_to format\n";
150 }
151
152 # Execute the conversion command and get the type of the result,
153 # making sure the converter gives us the appropriate output type
154 my $output_type = lc($convert_to);
155 my $cmd = "gsConvert.pl -verbose $verbosity -output $output_type \"$tmp_filename\"";
156 $output_type = `$cmd`;
157
158 # Check STDERR here
159
160 chomp $output_type;
161 if ($output_type eq "fail") {
162 print STDERR "Could not convert $tailname$suffix to $convert_to format\n";
163 return "";
164### exit 1;
165 }
166
167 # remove symbolic link to original file
168 &util::rm($tmp_filename);
169
170 # store the *actual* output type and return the output filename
171 $self->{'convert_to_ext'} = $output_type;
172 my $output_filename = $tmp_filename;
173 $output_filename =~ s/$suffix$/.$output_type/;
174
175 return $output_filename;
176}
177
178
179# Remove collection specific tmp directory and all its contents.
180
181sub cleanup_tmp_area {
182 my $self = shift (@_);
183
184 my $colname = &util::use_collection();
185 my $tmp_dirname
186 = &util::filename_cat($ENV{'GSDLHOME'},"collect",$colname,"tmp");
187 &util::rm_r($tmp_dirname);
188 &util::mk_dir($tmp_dirname);
189}
190
191
192
193# Override BasPlug read_file
194
195sub read_file {
196 my $self = shift (@_);
197 my ($src_filename, $textref) = @_;
198
199 my $output_ext = $self->{'convert_to_ext'};
200 my $conv_filename = $self->tmp_area_convert_file($output_ext,$src_filename);
201 if ("$conv_filename" eq "") {return "";} # allows continue on errors
202 $self->{'conv_filename'} = $conv_filename;
203
204 BasPlug::read_file($self,$conv_filename,$textref);
205}
206
207
208# Override BasPlug read
209
210sub read {
211 my $self = shift (@_);
212
213 my $ret_val = BasPlug::read($self,@_);
214
215 $self->cleanup_tmp_area();
216
217 return $ret_val;
218}
219
220
221# do plugin specific processing of doc_obj for HTML type
222sub process_type {
223 my $self = shift (@_);
224 my ($doc_ext, $textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
225
226 my $conv_filename = $self->{'conv_filename'};
227 my $tmp_dirname = File::Basename::dirname($conv_filename);
228 my $tmp_tailname = File::Basename::basename($conv_filename);
229
230 my $convert_to = $self->{'convert_to'};
231 my $ret_val;
232
233 if ($convert_to eq "TEXT")
234 {
235
236 $ret_val = TEXTPlug::process($self,$textref,$pluginfo,
237 $tmp_dirname,$tmp_tailname,
238 $metadata,$doc_obj);
239 }
240 else
241 {
242 $ret_val = HTMLPlug::process($self,$textref,$pluginfo,
243 $tmp_dirname,$tmp_tailname,
244 $metadata,$doc_obj);
245 }
246
247 # associate original file with doc object
248 my $cursection = $doc_obj->get_top_section();
249 my $filename = &util::filename_cat($base_dir,$file);
250 $doc_obj->associate_file($filename, "doc.$doc_ext", undef, $cursection);
251
252 my $doclink = "<a href=_httpcollection_/index/assoc/[archivedir]/doc.$doc_ext>";
253 $doc_obj->add_utf8_metadata ($cursection, "srclink", $doclink);
254 $doc_obj->add_utf8_metadata ($cursection, "srcicon", "_icon".$doc_ext."_");
255 $doc_obj->add_utf8_metadata ($cursection, "/srclink", "</a>");
256
257 return $ret_val;
258}
259
2601;
Note: See TracBrowser for help on using the repository browser.