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 | ########################################################################### |
---|
25 | package WordPlugin; |
---|
26 | |
---|
27 | use ConvertBinaryFile; |
---|
28 | |
---|
29 | |
---|
30 | use strict; |
---|
31 | no strict 'refs'; # allow filehandles to be variables and viceversa |
---|
32 | no strict 'subs'; |
---|
33 | use gsprintf 'gsprintf'; |
---|
34 | |
---|
35 | # @ISA dynamically configured to be either OpenOfficeConverter or ConvertBinaryFile |
---|
36 | |
---|
37 | # do not initialise these variables |
---|
38 | my $openoffice_ext_installed; |
---|
39 | my $openoffice_ext_working; |
---|
40 | sub 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 | |
---|
64 | my $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 | |
---|
76 | my $opt_windows_args = [ { 'name' => "windows_scripting", |
---|
77 | 'desc' => "{WordPlugin.windows_scripting}", |
---|
78 | 'type' => "flag", |
---|
79 | 'reqd' => "no" } ]; |
---|
80 | |
---|
81 | my $opt_openoffice_args = |
---|
82 | [ { 'name' => "openoffice_scripting", |
---|
83 | 'desc' => "{OpenOfficeConverter.openoffice_scripting}", |
---|
84 | 'type' => "flag", |
---|
85 | 'reqd' => "no" } ]; |
---|
86 | |
---|
87 | my $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 | |
---|
122 | my $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 | |
---|
129 | sub 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 | |
---|
163 | if ($openoffice_ext_working) { |
---|
164 | |
---|
165 | $self = new OpenOfficeConverter($pluginlist, $inputargs, $hashArgOptLists); |
---|
166 | } |
---|
167 | else { |
---|
168 | $self = new ConvertBinaryFile($pluginlist, $inputargs, $hashArgOptLists); |
---|
169 | } |
---|
170 | |
---|
171 | if ($self->{'info_only'}) { |
---|
172 | # don't worry about any options etc |
---|
173 | return bless $self, $class; |
---|
174 | } |
---|
175 | |
---|
176 | $self->{'filename_extension'} = "doc"; |
---|
177 | $self->{'file_type'} = "Word"; |
---|
178 | |
---|
179 | my $outhandle = $self->{'outhandle'}; |
---|
180 | |
---|
181 | if ($self->{'windows_scripting'}) { |
---|
182 | $self->{'convert_options'} = "-windows_scripting"; |
---|
183 | $self->{'office_scripting'} = 1; |
---|
184 | } |
---|
185 | if ($self->{'openoffice_scripting'}) { |
---|
186 | if ($self->{'windows_scripting'}) { |
---|
187 | print $outhandle "Warning: Cannot have -windows_scripting and -openoffice_scripting\n"; |
---|
188 | print $outhandle " on at the same time. Defaulting to -windows_scripting\n"; |
---|
189 | } |
---|
190 | else { |
---|
191 | $self->{'convert_options'} = "-openoffice_scripting"; |
---|
192 | $self->{'office_scripting'} = 1; |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | # check convert_to |
---|
197 | if ($self->{'convert_to'} eq "auto") { |
---|
198 | $self->{'convert_to'} = "html"; |
---|
199 | } |
---|
200 | # windows or open office scripting, outputs structuredHTML |
---|
201 | if (defined $self->{'office_scripting'}) { |
---|
202 | $self->{'convert_to'} = "structuredhtml"; |
---|
203 | } |
---|
204 | |
---|
205 | # set convert_to_plugin and convert_to_ext |
---|
206 | $self->ConvertBinaryFile::set_standard_convert_settings(); |
---|
207 | |
---|
208 | my $secondary_plugin_name = $self->{'convert_to_plugin'}; |
---|
209 | my $secondary_plugin_options = $self->{'secondary_plugin_options'}; |
---|
210 | |
---|
211 | if (!defined $secondary_plugin_options->{$secondary_plugin_name}) { |
---|
212 | $secondary_plugin_options->{$secondary_plugin_name} = []; |
---|
213 | } |
---|
214 | my $specific_options = $secondary_plugin_options->{$secondary_plugin_name}; |
---|
215 | |
---|
216 | # following title_sub removes "Page 1" and a leading |
---|
217 | # "1", which is often the page number at the top of the page. Bad Luck |
---|
218 | # if your document title actually starts with "1 " - is there a better way? |
---|
219 | push(@$specific_options , "-title_sub", '^(Page\s+\d+)?(\s*1\s+)?'); |
---|
220 | |
---|
221 | my $associate_tail_re = $self->{'associate_tail_re'}; |
---|
222 | if ((defined $associate_tail_re) && ($associate_tail_re ne "")) { |
---|
223 | push(@$specific_options, "-associate_tail_re", $associate_tail_re); |
---|
224 | } |
---|
225 | push(@$specific_options, "-file_rename_method", "none"); |
---|
226 | |
---|
227 | if ($secondary_plugin_name eq "StructuredHTMLPlugin") { |
---|
228 | # Instruct HTMLPlugin (when eventually accessed through read_into_doc_obj) |
---|
229 | # to extract these metadata fields from the HEAD META fields |
---|
230 | push (@$specific_options, "-metadata_fields","Title,GENERATOR,date,author<Creator>"); |
---|
231 | push (@$specific_options, "-description_tags") if $self->{'office_scripting'}; |
---|
232 | push (@$specific_options, "-extract_language") if $self->{'extract_language'}; |
---|
233 | push (@$specific_options, "-delete_toc") if $self->{'delete_toc'}; |
---|
234 | push (@$specific_options, "-toc_header", $self->{'toc_header'}) if $self->{'toc_header'}; |
---|
235 | push (@$specific_options, "-title_header", $self->{'title_header'}) if $self->{'title_header'}; |
---|
236 | push (@$specific_options, "-level1_header", $self->{'level1_header'}) if $self->{'level1_header'}; |
---|
237 | push (@$specific_options, "-level2_header", $self->{'level2_header'})if $self->{'level2_header'}; |
---|
238 | push (@$specific_options, "-level3_header", $self->{'level3_header'}) if $self->{'level3_header'}; |
---|
239 | push (@$specific_options, "-metadata_fields", $self->{'metadata_fields'}) if $self->{'metadata_fields'}; |
---|
240 | push (@$specific_options, "-metadata_field_separator", $self->{'metadata_field_separator'}) if $self->{'metadata_field_separator'}; |
---|
241 | push(@$specific_options, "-processing_tmp_files"); |
---|
242 | |
---|
243 | } |
---|
244 | |
---|
245 | elsif ($secondary_plugin_name eq "HTMLPlugin") { |
---|
246 | push(@$specific_options, "-processing_tmp_files"); |
---|
247 | push(@$specific_options,"-input_encoding", "utf8"); |
---|
248 | push(@$specific_options,"-extract_language") if $self->{'extract_language'}; |
---|
249 | push(@$specific_options, "-description_tags") if $self->{'description_tags'}; |
---|
250 | # Instruct HTMLPlugin (when eventually accessed through read_into_doc_obj) |
---|
251 | # to extract these metadata fields from the HEAD META fields |
---|
252 | push(@$specific_options,"-metadata_fields","Title,GENERATOR,date,author<Creator>"); |
---|
253 | } |
---|
254 | |
---|
255 | $self = bless $self, $class; |
---|
256 | $self->load_secondary_plugins($class,$secondary_plugin_options,$hashArgOptLists); |
---|
257 | |
---|
258 | return bless $self; |
---|
259 | } |
---|
260 | |
---|
261 | sub init { |
---|
262 | my $self = shift (@_); |
---|
263 | my ($verbosity, $outhandle, $failhandle) = @_; |
---|
264 | |
---|
265 | $self->SUPER::init($verbosity,$outhandle,$failhandle); |
---|
266 | } |
---|
267 | |
---|
268 | sub deinit { |
---|
269 | # called only once, after all plugin passes have been done |
---|
270 | my ($self) = @_; |
---|
271 | |
---|
272 | $self->SUPER::deinit(); |
---|
273 | } |
---|
274 | |
---|
275 | sub get_default_process_exp { |
---|
276 | my $self = shift (@_); |
---|
277 | if ($openoffice_ext_working) { |
---|
278 | return q^(?i)\.(doc|dot|docx|odt)$^; |
---|
279 | } |
---|
280 | return q^(?i)\.(doc|dot)$^; |
---|
281 | } |
---|
282 | |
---|
283 | sub convert_post_process_old |
---|
284 | { |
---|
285 | my $self = shift (@_); |
---|
286 | my ($conv_filename) = @_; |
---|
287 | |
---|
288 | my $outhandle=$self->{'outhandle'}; |
---|
289 | |
---|
290 | my ($language, $encoding) = $self->textcat_get_language_encoding ($conv_filename); |
---|
291 | |
---|
292 | # read in file ($text will be in utf8) |
---|
293 | my $text = ""; |
---|
294 | $self->read_file ($conv_filename, $encoding, $language, \$text); |
---|
295 | |
---|
296 | # turn any high bytes that aren't valid utf-8 into utf-8. |
---|
297 | #unicode::ensure_utf8(\$text); |
---|
298 | |
---|
299 | # Write it out again! |
---|
300 | #$self->utf8_write_file (\$text, $conv_filename); |
---|
301 | } |
---|
302 | |
---|
303 | # Modified to cache HTML files for efficieny reasons rather |
---|
304 | # than delete all. HTML is modified not to use IE's VML. |
---|
305 | # VML uses WML files, so these can be deleted. |
---|
306 | sub cleanup_tmp_area { |
---|
307 | my ($self) = @_; |
---|
308 | if (defined $self->{'files_dir'}) { |
---|
309 | my $html_files_dir = $self->{'files_dir'}; |
---|
310 | |
---|
311 | if (opendir(DIN,$html_files_dir)) { |
---|
312 | my @wmz_files = grep( /\.wmz$/, readdir(DIN)); |
---|
313 | foreach my $f (@wmz_files) { |
---|
314 | my $full_f = &util::filename_cat($html_files_dir,$f); |
---|
315 | &util::rm($full_f); |
---|
316 | } |
---|
317 | closedir(DIN); |
---|
318 | } |
---|
319 | else { |
---|
320 | # if HTML file has no supporting images, then no _files dir made |
---|
321 | # => do nothing |
---|
322 | } |
---|
323 | } |
---|
324 | } |
---|
325 | |
---|
326 | |
---|
327 | 1; |
---|
328 | |
---|