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

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

code tidy up. rearranged how convertbinaryfile plugins set up their secondary plugins - now only set up the options for the one they are using. all subclass specific code moved out of convertbinaryfile.new into the appropriate plugin file.

  • Property svn:keywords set to Author Date Id Revision
File size: 10.7 KB
RevLine 
[1410]1###########################################################################
2#
[15872]3# WordPlugin.pm -- plugin for importing Microsoft Word documents
[1410]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###########################################################################
[15872]25package WordPlugin;
[1410]26
[15872]27use ConvertBinaryFile;
[22428]28
29
[10254]30use strict;
31no strict 'refs'; # allow filehandles to be variables and viceversa
[22507]32no strict 'subs';
33use gsprintf 'gsprintf';
[1410]34
[22428]35# @ISA dynamically configured to be either OpenOfficeConverter or ConvertBinaryFile
36
[22507]37# do not initialise these variables
38my $openoffice_ext_installed;
39my $openoffice_ext_working;
40sub BEGIN {
41 eval("require OpenOfficeConverter");
42 if ($@) {
[22597]43 # Useful debugging statement if there is a syntax error in OpenOfficeConverter:
[22507]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
[4744]64my $arguments =
65 [ { 'name' => "process_exp",
[15872]66 'desc' => "{BasePlugin.process_exp}",
[6408]67 'type' => "regexp",
[4744]68 'deft' => &get_default_process_exp(),
[10514]69 'reqd' => "no" },
70 { 'name' => "description_tags",
[15872]71 'desc' => "{HTMLPlugin.description_tags}",
[10514]72 'type' => "flag" }
[10355]73 ];
[3540]74
[22428]75
76my $opt_windows_args = [ { 'name' => "windows_scripting",
77 'desc' => "{WordPlugin.windows_scripting}",
78 'type' => "flag",
79 'reqd' => "no" } ];
80
[22505]81my $opt_openoffice_args =
82 [ { 'name' => "openoffice_scripting",
[22514]83 'desc' => "{OpenOfficeConverter.openoffice_scripting}",
[22505]84 'type' => "flag",
85 'reqd' => "no" } ];
86
[22428]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
[15872]122my $options = { 'name' => "WordPlugin",
123 'desc' => "{WordPlugin.desc}",
[6408]124 'abstract' => "no",
[4744]125 'inherits' => "yes",
[15114]126 'srcreplaceable' => "yes", # Source docs in Word can be replaced with GS-generated html
[4744]127 'args' => $arguments };
[3540]128
[2811]129sub new {
[10218]130 my ($class) = shift (@_);
131 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
132 push(@$pluginlist, $class);
[3540]133
[22507]134 if ($openoffice_ext_installed) {
[22428]135 print STDERR "WordPlugin: OpenOffice Extension to Greenstone detected\n";
[22507]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 }
[22428]142 }
[22507]143
144 my $office_capable = 0;
[10279]145 if ($ENV{'GSDLOS'} =~ m/^windows$/i) {
[22428]146 push(@$arguments,@$opt_windows_args);
[22507]147 $office_capable = 1;
[10279]148 }
[22507]149 if ($openoffice_ext_working) {
[22505]150 push(@$arguments,@$opt_openoffice_args);
[22507]151 $office_capable = 1;
152 }
153 # these office args apply to windows scripting or to openoffice scripting
154 if ($office_capable) {
[22428]155 push(@$arguments,@$opt_office_args);
156 }
[22507]157
[15872]158 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
159 push(@{$hashArgOptLists->{"OptList"}},$options);
[10441]160
[22428]161 my $self = {};
[10218]162
[22507]163 if ($openoffice_ext_working) {
164
[22428]165 $self = new OpenOfficeConverter($pluginlist, $inputargs, $hashArgOptLists);
166 }
167 else {
168 $self = new ConvertBinaryFile($pluginlist, $inputargs, $hashArgOptLists);
169 }
170
[10580]171 if ($self->{'info_only'}) {
172 # don't worry about any options etc
173 return bless $self, $class;
174 }
175
[15872]176 $self->{'filename_extension'} = "doc";
177 $self->{'file_type'} = "Word";
178
[22597]179 my $outhandle = $self->{'outhandle'};
180
[22428]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 }
[10405]195
[22597]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 }
[12834]204
[22597]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'};
[10279]209 my $secondary_plugin_options = $self->{'secondary_plugin_options'};
[22597]210
211 if (!defined $secondary_plugin_options->{$secondary_plugin_name}) {
212 $secondary_plugin_options->{$secondary_plugin_name} = [];
[10405]213 }
[22597]214 my $specific_options = $secondary_plugin_options->{$secondary_plugin_name};
[10279]215
[22597]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+)?');
[11122]220
221 my $associate_tail_re = $self->{'associate_tail_re'};
222 if ((defined $associate_tail_re) && ($associate_tail_re ne "")) {
[22597]223 push(@$specific_options, "-associate_tail_re", $associate_tail_re);
[11122]224 }
[22597]225 push(@$specific_options, "-file_rename_method", "none");
[18406]226
[22597]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 }
[18406]254
[10279]255 $self = bless $self, $class;
[10428]256 $self->load_secondary_plugins($class,$secondary_plugin_options,$hashArgOptLists);
[2811]257
[10279]258 return bless $self;
[2811]259}
260
[22428]261sub init {
262 my $self = shift (@_);
263 my ($verbosity, $outhandle, $failhandle) = @_;
264
265 $self->SUPER::init($verbosity,$outhandle,$failhandle);
266}
267
268sub deinit {
269 # called only once, after all plugin passes have been done
270 my ($self) = @_;
271
272 $self->SUPER::deinit();
273}
274
[1410]275sub get_default_process_exp {
276 my $self = shift (@_);
[22507]277 if ($openoffice_ext_working) {
278 return q^(?i)\.(doc|dot|docx|odt)$^;
279 }
[3400]280 return q^(?i)\.(doc|dot)$^;
[1410]281}
282
[15872]283sub convert_post_process_old
[10279]284{
[1410]285 my $self = shift (@_);
[10279]286 my ($conv_filename) = @_;
[2515]287
[10279]288 my $outhandle=$self->{'outhandle'};
[10441]289
[10279]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.
[10441]297 #unicode::ensure_utf8(\$text);
298
[10279]299 # Write it out again!
[10441]300 #$self->utf8_write_file (\$text, $conv_filename);
[1410]301}
302
[10279]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.
306sub 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
[10441]326
[1410]3271;
[10279]328
Note: See TracBrowser for help on using the repository browser.