root/main/trunk/greenstone2/perllib/plugins/WordPlugin.pm @ 22514

Revision 22514, 11.2 KB (checked in by kjdon, 9 years ago)

small tidyings

  • Property svn:keywords set to Author Date Id Revision
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' => "{OpenOfficeConverter.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
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    my $outhandle = $self->{'outhandle'};
177    $self->{'filename_extension'} = "doc";
178    $self->{'file_type'} = "Word";
179
180    if ($self->{'windows_scripting'}) {
181    $self->{'convert_options'} = "-windows_scripting";
182    $self->{'office_scripting'} = 1;
183    }   
184    if ($self->{'openoffice_scripting'}) {
185    if ($self->{'windows_scripting'}) {
186        print $outhandle "Warning: Cannot have -windows_scripting and -openoffice_scripting\n";
187        print $outhandle "         on at the same time.  Defaulting to -windows_scripting\n";
188    }
189    else {
190        $self->{'convert_options'} = "-openoffice_scripting";
191        $self->{'office_scripting'} = 1;
192    }
193    }
194
195    # we always save as utf-8
196#    if ($self->{'input_encoding'} eq "auto") {
197#   $self->{'input_encoding'} = "utf8";
198#    }
199
200    my $secondary_plugin_options = $self->{'secondary_plugin_options'};
201    if (defined $self->{'office_scripting'}) {
202    if (!defined $secondary_plugin_options->{'StructuredHTMLPlugin'}){
203        $secondary_plugin_options->{'StructuredHTMLPlugin'} = [];
204        my $structhtml_options = $secondary_plugin_options->{'StructuredHTMLPlugin'};
205       
206        # Instruct HTMLPlugin (when eventually accessed through read_into_doc_obj)
207        # to extract these metadata fields from the HEAD META fields
208        push (@$structhtml_options, "-metadata_fields","Title,GENERATOR,date,author<Creator>");
209        push (@$structhtml_options, "-title_sub", '^(Page\s+\d+)?(\s*1\s+)?');
210        push (@$structhtml_options, "-description_tags") if $self->{'office_scripting'};
211        push (@$structhtml_options, "-extract_language") if $self->{'extract_language'};
212        push (@$structhtml_options, "-delete_toc") if $self->{'delete_toc'};
213        push (@$structhtml_options, "-toc_header", $self->{'toc_header'}) if $self->{'toc_header'};
214        push (@$structhtml_options, "-title_header", $self->{'title_header'}) if $self->{'title_header'};
215        push (@$structhtml_options, "-level1_header", $self->{'level1_header'}) if $self->{'level1_header'};
216        push (@$structhtml_options, "-level2_header", $self->{'level2_header'})if $self->{'level2_header'};
217        push (@$structhtml_options, "-level3_header", $self->{'level3_header'}) if $self->{'level3_header'};
218        push (@$structhtml_options, "-metadata_fields", $self->{'metadata_fields'}) if $self->{'metadata_fields'};
219        push (@$structhtml_options, "-metadata_field_separator", $self->{'metadata_field_separator'}) if $self->{'metadata_field_separator'};
220    }
221    }
222    if (!defined $secondary_plugin_options->{'HTMLPlugin'}) {
223    $secondary_plugin_options->{'HTMLPlugin'} = [];
224    }
225    if (!defined $secondary_plugin_options->{'TextPlugin'}) {
226    $secondary_plugin_options->{'TextPlugin'} = [];
227    }
228
229    my $html_options = $secondary_plugin_options->{'HTMLPlugin'};
230    my $text_options = $secondary_plugin_options->{'TextPlugin'};
231    my $structhtml_options = $secondary_plugin_options->{'StructuredHTMLPlugin'};   
232    # tell the secondary plugins that they are processing tmp files
233    push(@$html_options, "-processing_tmp_files");
234    push(@$structhtml_options, "-processing_tmp_files");
235   
236    # wvWare will always produce html files encoded as utf-8, so make sure the secondary HTMLPlugin knows this
237    push(@$html_options,"-input_encoding", "utf8");
238    push(@$html_options,"-extract_language") if $self->{'extract_language'};
239    push(@$html_options, "-description_tags") if $self->{'description_tags'};
240
241    # Instruct HTMLPlugin (when eventually accessed through read_into_doc_obj)
242    # to extract these metadata fields from the HEAD META fields
243    push(@$html_options,"-metadata_fields","Title,GENERATOR,date,author<Creator>");
244    push(@$html_options , "-title_sub", '^(Page\s+\d+)?(\s*1\s+)?');
245
246    my $associate_tail_re = $self->{'associate_tail_re'};
247    if ((defined $associate_tail_re) && ($associate_tail_re ne "")) {
248    push(@$html_options, "-associate_tail_re", $associate_tail_re);
249    push(@$text_options, "-associate_tail_re", $associate_tail_re);
250    push(@$structhtml_options, "-associate_tail_re", $associate_tail_re) if defined $structhtml_options;
251    }
252
253    push(@$html_options, "-file_rename_method", "none");
254    push(@$text_options, "-file_rename_method", "none");
255    push(@$structhtml_options, "-file_rename_method", "none") if defined $structhtml_options;
256
257    $self = bless $self, $class;
258    $self->load_secondary_plugins($class,$secondary_plugin_options,$hashArgOptLists);
259
260    return bless $self;
261}
262
263sub init {
264    my $self = shift (@_);
265    my ($verbosity, $outhandle, $failhandle) = @_;
266   
267    $self->SUPER::init($verbosity,$outhandle,$failhandle);
268}
269
270sub deinit {
271    # called only once, after all plugin passes have been done
272    my ($self) = @_;
273   
274    $self->SUPER::deinit();
275}
276
277sub get_default_process_exp {
278    my $self = shift (@_);
279    if ($openoffice_ext_working) {
280    return q^(?i)\.(doc|dot|docx|odt)$^;
281    }
282    return q^(?i)\.(doc|dot)$^;
283}
284
285sub convert_post_process_old
286{
287    my $self = shift (@_);
288    my ($conv_filename) = @_;
289
290    my $outhandle=$self->{'outhandle'};
291     
292    my ($language, $encoding) = $self->textcat_get_language_encoding ($conv_filename);
293
294    # read in file ($text will be in utf8)
295    my $text = "";
296    $self->read_file ($conv_filename, $encoding, $language, \$text);
297
298    # turn any high bytes that aren't valid utf-8 into utf-8.
299    #unicode::ensure_utf8(\$text);
300   
301    # Write it out again!
302    #$self->utf8_write_file (\$text, $conv_filename);
303}
304
305# Modified to cache HTML files for efficieny reasons rather
306# than delete all.  HTML is modified not to use IE's VML.
307# VML uses WML files, so these can be deleted.
308sub cleanup_tmp_area {
309    my ($self) = @_;
310    if (defined $self->{'files_dir'}) {
311    my $html_files_dir = $self->{'files_dir'};
312
313    if (opendir(DIN,$html_files_dir)) {
314        my @wmz_files = grep( /\.wmz$/, readdir(DIN));
315        foreach my $f (@wmz_files) {
316        my $full_f = &util::filename_cat($html_files_dir,$f);
317        &util::rm($full_f);
318        }
319        closedir(DIN);
320    }
321    else {
322        # if HTML file has no supporting images, then no _files dir made
323        # => do nothing
324    }
325    }
326}
327
328
3291;
330
Note: See TracBrowser for help on using the browser.