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

Last change on this file since 1954 was 1954, checked in by jmt14, 23 years ago

* empty log message *

  • Property svn:keywords set to Author Date Id Revision
File size: 7.8 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 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 my $tmp_filename = &util::filename_cat($tmp_dirname,"$tailname$suffix");
148 # Remove any white space from filename -- no risk of name collision, and
149 # makes later conversion by utils simpler
150 $tmp_filename =~ s/\s+//g;
151
152 &util::soft_link($input_filename,$tmp_filename);
153
154 my $verbosity = $self->{'verbosity'};
155 if ($verbosity>0)
156 {
157 print STDERR "Converting $tailname$suffix to $convert_to format\n";
158 }
159
160 # Execute the conversion command and get the type of the result,
161 # making sure the converter gives us the appropriate output type
162 my $output_type = lc($convert_to);
163 my $cmd = "gsConvert.pl -verbose $verbosity -output $output_type \"$tmp_filename\"";
164 $output_type = `$cmd`;
165
166 # Check STDERR here
167
168 chomp $output_type;
169 if ($output_type eq "fail") {
170 print STDERR "Could not convert $tailname$suffix to $convert_to format\n";
171 return "";
172### exit 1;
173 }
174
175 # remove symbolic link to original file
176 &util::rm($tmp_filename);
177
178 # store the *actual* output type and return the output filename
179 $self->{'convert_to_ext'} = $output_type;
180 my $output_filename = $tmp_filename;
181 $output_filename =~ s/$suffix$/.$output_type/;
182
183 return $output_filename;
184}
185
186
187# Remove collection specific tmp directory and all its contents.
188
189sub cleanup_tmp_area {
190 my $self = shift (@_);
191
192 my $colname = &util::use_collection();
193 my $tmp_dirname
194 = &util::filename_cat($ENV{'GSDLHOME'},"collect",$colname,"tmp");
195 &util::rm_r($tmp_dirname);
196 &util::mk_dir($tmp_dirname);
197}
198
199
200
201# Override BasPlug read_file
202
203sub read_file {
204 my $self = shift (@_);
205 my ($src_filename, $textref) = @_;
206
207 my $output_ext = $self->{'convert_to_ext'};
208 my $conv_filename = $self->tmp_area_convert_file($output_ext,$src_filename);
209 if ("$conv_filename" eq "") {return "";} # allows continue on errors
210 $self->{'conv_filename'} = $conv_filename;
211
212 BasPlug::read_file($self,$conv_filename,$textref);
213}
214
215
216# Override BasPlug read
217
218sub read {
219 my $self = shift (@_);
220
221 my $ret_val = BasPlug::read($self,@_);
222
223 $self->cleanup_tmp_area();
224
225 return $ret_val;
226}
227
228
229# do plugin specific processing of doc_obj for HTML type
230sub process_type {
231 my $self = shift (@_);
232 my ($doc_ext, $textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
233
234 my $conv_filename = $self->{'conv_filename'};
235 my $tmp_dirname = File::Basename::dirname($conv_filename);
236 my $tmp_tailname = File::Basename::basename($conv_filename);
237
238 my $convert_to = $self->{'convert_to'};
239 my $ret_val;
240
241 if ($convert_to eq "TEXT")
242 {
243
244 $ret_val = TEXTPlug::process($self,$textref,$pluginfo,
245 $tmp_dirname,$tmp_tailname,
246 $metadata,$doc_obj);
247 }
248 else
249 {
250 $ret_val = HTMLPlug::process($self,$textref,$pluginfo,
251 $tmp_dirname,$tmp_tailname,
252 $metadata,$doc_obj);
253 }
254
255 # associate original file with doc object
256 my $cursection = $doc_obj->get_top_section();
257 my $filename = &util::filename_cat($base_dir,$file);
258 $doc_obj->associate_file($filename, "doc.$doc_ext", undef, $cursection);
259
260 my $doclink = "<a href=_httpcollection_/index/assoc/[archivedir]/doc.$doc_ext>";
261 $doc_obj->add_utf8_metadata ($cursection, "srclink", $doclink);
262 $doc_obj->add_utf8_metadata ($cursection, "srcicon", "_icon".$doc_ext."_");
263 $doc_obj->add_utf8_metadata ($cursection, "/srclink", "</a>");
264
265 return $ret_val;
266}
267
2681;
Note: See TracBrowser for help on using the repository browser.