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

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

StructuredHTMLPlugin needs -description tags if office_scripting is on, not just if windows_scripting is on.

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