source: main/trunk/greenstone2/perllib/plugins/WordPlugin.pm@ 22507

Last change on this file since 22507 was 22507, checked in by kjdon, 14 years ago

some moving around and tidying up of code

  • Property svn:keywords set to Author Date Id Revision
File size: 11.3 KB
Line 
1###########################################################################
2#
3# WordPlugin.pm -- plugin for importing Microsoft Word documents
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###########################################################################
25package WordPlugin;
26
27use ConvertBinaryFile;
28
29
30use strict;
31no strict 'refs'; # allow filehandles to be variables and viceversa
32no strict 'subs';
33use gsprintf 'gsprintf';
34
35# @ISA dynamically configured to be either OpenOfficeConverter or ConvertBinaryFile
36
37# do not initialise these variables
38my $openoffice_ext_installed;
39my $openoffice_ext_working;
40sub BEGIN {
41 eval("require OpenOfficeConverter");
42 if ($@) {
43 # Useful debugging statement if there is a syntax error in OpenOfficeConverter
44 #print STDERR "$@\n";
45 @WordPlugin::ISA = ('ConvertBinaryFile');
46 $openoffice_ext_installed = 0;
47 $openoffice_ext_working = 0;
48 }
49 else {
50 # Successfully found
51 $openoffice_ext_installed = 1;
52 # now check whether it can run soffice
53 if ($OpenOfficeConverter::openoffice_conversion_available) {
54 @WordPlugin::ISA = ('OpenOfficeConverter');
55 $openoffice_ext_working = 1;
56
57 } else {
58 @WordPlugin::ISA = ('ConvertBinaryFile');
59 $openoffice_ext_working = 0;
60 }
61 }
62}
63
64my $arguments =
65 [ { 'name' => "process_exp",
66 'desc' => "{BasePlugin.process_exp}",
67 'type' => "regexp",
68 'deft' => &get_default_process_exp(),
69 'reqd' => "no" },
70 { 'name' => "description_tags",
71 'desc' => "{HTMLPlugin.description_tags}",
72 'type' => "flag" }
73 ];
74
75
76my $opt_windows_args = [ { 'name' => "windows_scripting",
77 'desc' => "{WordPlugin.windows_scripting}",
78 'type' => "flag",
79 'reqd' => "no" } ];
80
81my $opt_openoffice_args =
82 [ { 'name' => "openoffice_scripting",
83 'desc' => "{WordPlugin.openoffice_scripting}",
84 'type' => "flag",
85 'reqd' => "no" } ];
86
87my $opt_office_args = [ { 'name' => "metadata_fields",
88 'desc' => "{WordPlugin.metadata_fields}",
89 'type' => "string",
90 'deft' => "Title" },
91 { 'name' => "level1_header",
92 'desc' => "{StructuredHTMLPlugin.level1_header}",
93 'type' => "regexp",
94 'reqd' => "no",
95 'deft' => "" },
96 { 'name' => "level2_header",
97 'desc' => "{StructuredHTMLPlugin.level2_header}",
98 'type' => "regexp",
99 'reqd' => "no",
100 'deft' => "" },
101 { 'name' => "level3_header",
102 'desc' => "{StructuredHTMLPlugin.level3_header}",
103 'type' => "regexp",
104 'reqd' => "no",
105 'deft' => "" },
106 { 'name' => "title_header",
107 'desc' => "{StructuredHTMLPlugin.title_header}",
108 'type' => "regexp",
109 'reqd' => "no",
110 'deft' => "" },
111 { 'name' => "delete_toc",
112 'desc' => "{StructuredHTMLPlugin.delete_toc}",
113 'type' => "flag",
114 'reqd' => "no" },
115 { 'name' => "toc_header",
116 'desc' => "{StructuredHTMLPlugin.toc_header}",
117 'type' => "regexp",
118 'reqd' => "no",
119 'deft' => "" } ];
120
121
122my $options = { 'name' => "WordPlugin",
123 'desc' => "{WordPlugin.desc}",
124 'abstract' => "no",
125 'inherits' => "yes",
126 'srcreplaceable' => "yes", # Source docs in Word can be replaced with GS-generated html
127 'args' => $arguments };
128
129sub new {
130 my ($class) = shift (@_);
131 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
132 push(@$pluginlist, $class);
133
134 if ($openoffice_ext_installed) {
135 print STDERR "WordPlugin: OpenOffice Extension to Greenstone detected\n";
136 if ($openoffice_ext_working) {
137 print STDERR "... and it appears to be working\n";
138 } else {
139 print STDERR "... but it appears to be broken\n";
140 &gsprintf(STDERR, "OpenOfficeConverter: {OpenOfficeConverter.noconversionavailable} ({OpenOfficeConverter.$OpenOfficeConverter::no_openoffice_conversion_reason})\n");
141 }
142 }
143
144 my $office_capable = 0;
145 if ($ENV{'GSDLOS'} =~ m/^windows$/i) {
146 push(@$arguments,@$opt_windows_args);
147 $office_capable = 1;
148 }
149 if ($openoffice_ext_working) {
150 push(@$arguments,@$opt_openoffice_args);
151 $office_capable = 1;
152 }
153 # these office args apply to windows scripting or to openoffice scripting
154 if ($office_capable) {
155 push(@$arguments,@$opt_office_args);
156 }
157
158 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
159 push(@{$hashArgOptLists->{"OptList"}},$options);
160
161 my $self = {};
162 my $outhandle;
163
164 if ($openoffice_ext_working) {
165
166 $self = new OpenOfficeConverter($pluginlist, $inputargs, $hashArgOptLists);
167 $outhandle = $self->{'outhandle'};
168
169 }
170 else {
171 $self = new ConvertBinaryFile($pluginlist, $inputargs, $hashArgOptLists);
172 $outhandle = $self->{'outhandle'};
173 }
174
175 if ($self->{'info_only'}) {
176 # don't worry about any options etc
177 return bless $self, $class;
178 }
179
180 $self->{'filename_extension'} = "doc";
181 $self->{'file_type'} = "Word";
182
183 if ($self->{'windows_scripting'}) {
184 $self->{'convert_options'} = "-windows_scripting";
185 $self->{'office_scripting'} = 1;
186 }
187 if ($self->{'openoffice_scripting'}) {
188 if ($self->{'windows_scripting'}) {
189 print $outhandle "Warning: Cannot have -windows_scripting and -openoffice_scripting\n";
190 print $outhandle " on at the same time. Defaulting to -windows_scripting\n";
191 }
192 else {
193 $self->{'convert_options'} = "-openoffice_scripting";
194 $self->{'office_scripting'} = 1;
195 }
196 }
197
198 # we always save as utf-8
199# if ($self->{'input_encoding'} eq "auto") {
200# $self->{'input_encoding'} = "utf8";
201# }
202
203 my $secondary_plugin_options = $self->{'secondary_plugin_options'};
204 if (defined $self->{'office_scripting'}) {
205 if (!defined $secondary_plugin_options->{'StructuredHTMLPlugin'}){
206 $secondary_plugin_options->{'StructuredHTMLPlugin'} = [];
207 my $structhtml_options = $secondary_plugin_options->{'StructuredHTMLPlugin'};
208
209 # Instruct HTMLPlugin (when eventually accessed through read_into_doc_obj)
210 # to extract these metadata fields from the HEAD META fields
211 push (@$structhtml_options, "-metadata_fields","Title,GENERATOR,date,author<Creator>");
212 push (@$structhtml_options, "-title_sub", '^(Page\s+\d+)?(\s*1\s+)?');
213 push (@$structhtml_options, "-description_tags") if $self->{'office_scripting'};
214 push (@$structhtml_options, "-extract_language") if $self->{'extract_language'};
215 push (@$structhtml_options, "-delete_toc") if $self->{'delete_toc'};
216 push (@$structhtml_options, "-toc_header", $self->{'toc_header'}) if $self->{'toc_header'};
217 push (@$structhtml_options, "-title_header", $self->{'title_header'}) if $self->{'title_header'};
218 push (@$structhtml_options, "-level1_header", $self->{'level1_header'}) if $self->{'level1_header'};
219 push (@$structhtml_options, "-level2_header", $self->{'level2_header'})if $self->{'level2_header'};
220 push (@$structhtml_options, "-level3_header", $self->{'level3_header'}) if $self->{'level3_header'};
221 push (@$structhtml_options, "-metadata_fields", $self->{'metadata_fields'}) if $self->{'metadata_fields'};
222 push (@$structhtml_options, "-metadata_field_separator", $self->{'metadata_field_separator'}) if $self->{'metadata_field_separator'};
223 }
224 }
225 if (!defined $secondary_plugin_options->{'HTMLPlugin'}) {
226 $secondary_plugin_options->{'HTMLPlugin'} = [];
227 }
228 if (!defined $secondary_plugin_options->{'TextPlugin'}) {
229 $secondary_plugin_options->{'TextPlugin'} = [];
230 }
231
232 my $html_options = $secondary_plugin_options->{'HTMLPlugin'};
233 my $text_options = $secondary_plugin_options->{'TextPlugin'};
234 my $structhtml_options = $secondary_plugin_options->{'StructuredHTMLPlugin'};
235 # tell the secondary plugins that they are processing tmp files
236 push(@$html_options, "-processing_tmp_files");
237 push(@$structhtml_options, "-processing_tmp_files");
238
239 # wvWare will always produce html files encoded as utf-8, so make sure the secondary HTMLPlugin knows this
240 push(@$html_options,"-input_encoding", "utf8");
241 push(@$html_options,"-extract_language") if $self->{'extract_language'};
242 push(@$html_options, "-description_tags") if $self->{'description_tags'};
243
244 # Instruct HTMLPlugin (when eventually accessed through read_into_doc_obj)
245 # to extract these metadata fields from the HEAD META fields
246 push(@$html_options,"-metadata_fields","Title,GENERATOR,date,author<Creator>");
247 push(@$html_options , "-title_sub", '^(Page\s+\d+)?(\s*1\s+)?');
248
249 my $associate_tail_re = $self->{'associate_tail_re'};
250 if ((defined $associate_tail_re) && ($associate_tail_re ne "")) {
251 push(@$html_options, "-associate_tail_re", $associate_tail_re);
252 push(@$text_options, "-associate_tail_re", $associate_tail_re);
253 push(@$structhtml_options, "-associate_tail_re", $associate_tail_re) if defined $structhtml_options;
254 }
255
256 push(@$html_options, "-file_rename_method", "none");
257 push(@$text_options, "-file_rename_method", "none");
258 push(@$structhtml_options, "-file_rename_method", "none") if defined $structhtml_options;
259
260 $self = bless $self, $class;
261 $self->load_secondary_plugins($class,$secondary_plugin_options,$hashArgOptLists);
262
263 return bless $self;
264}
265
266sub init {
267 my $self = shift (@_);
268 my ($verbosity, $outhandle, $failhandle) = @_;
269
270 $self->SUPER::init($verbosity,$outhandle,$failhandle);
271}
272
273sub deinit {
274 # called only once, after all plugin passes have been done
275 my ($self) = @_;
276
277 $self->SUPER::deinit();
278}
279
280sub get_default_process_exp {
281 my $self = shift (@_);
282 if ($openoffice_ext_working) {
283 return q^(?i)\.(doc|dot|docx|odt)$^;
284 }
285 return q^(?i)\.(doc|dot)$^;
286}
287
288sub convert_post_process_old
289{
290 my $self = shift (@_);
291 my ($conv_filename) = @_;
292
293 my $outhandle=$self->{'outhandle'};
294
295 my ($language, $encoding) = $self->textcat_get_language_encoding ($conv_filename);
296
297 # read in file ($text will be in utf8)
298 my $text = "";
299 $self->read_file ($conv_filename, $encoding, $language, \$text);
300
301 # turn any high bytes that aren't valid utf-8 into utf-8.
302 #unicode::ensure_utf8(\$text);
303
304 # Write it out again!
305 #$self->utf8_write_file (\$text, $conv_filename);
306}
307
308# Modified to cache HTML files for efficieny reasons rather
309# than delete all. HTML is modified not to use IE's VML.
310# VML uses WML files, so these can be deleted.
311sub cleanup_tmp_area {
312 my ($self) = @_;
313 if (defined $self->{'files_dir'}) {
314 my $html_files_dir = $self->{'files_dir'};
315
316 if (opendir(DIN,$html_files_dir)) {
317 my @wmz_files = grep( /\.wmz$/, readdir(DIN));
318 foreach my $f (@wmz_files) {
319 my $full_f = &util::filename_cat($html_files_dir,$f);
320 &util::rm($full_f);
321 }
322 closedir(DIN);
323 }
324 else {
325 # if HTML file has no supporting images, then no _files dir made
326 # => do nothing
327 }
328 }
329}
330
331
3321;
333
Note: See TracBrowser for help on using the repository browser.