source: gsdl/trunk/perllib/plugins/WordPlugin.pm@ 15918

Last change on this file since 15918 was 15902, checked in by kjdon, 16 years ago

no longer uses input_encoding

  • Property svn:keywords set to Author Date Id Revision
File size: 8.1 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;
28use strict;
29no strict 'refs'; # allow filehandles to be variables and viceversa
30
31sub BEGIN {
32 @WordPlugin::ISA = ('ConvertBinaryFile');
33}
34
35my $arguments =
36 [ { 'name' => "process_exp",
37 'desc' => "{BasePlugin.process_exp}",
38 'type' => "regexp",
39 'deft' => &get_default_process_exp(),
40 'reqd' => "no" },
41 { 'name' => "description_tags",
42 'desc' => "{HTMLPlugin.description_tags}",
43 'type' => "flag" }
44 ];
45
46my $options = { 'name' => "WordPlugin",
47 'desc' => "{WordPlugin.desc}",
48 'abstract' => "no",
49 'inherits' => "yes",
50 'srcreplaceable' => "yes", # Source docs in Word can be replaced with GS-generated html
51 'args' => $arguments };
52
53sub new {
54 my ($class) = shift (@_);
55 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
56 push(@$pluginlist, $class);
57
58 if ($ENV{'GSDLOS'} =~ m/^windows$/i) {
59 my $ws_arg = [ { 'name' => "windows_scripting",
60 'desc' => "{WordPlugin.windows_scripting}",
61 'type' => "flag",
62 'reqd' => "no" },
63 { 'name' => "metadata_fields",
64 'type' => "string",
65 'deft' => "Title" },
66 { 'name' => "level1_header",
67 'desc' => "{StructuredHTMLPlugin.level1_header}",
68 'type' => "regexp",
69 'reqd' => "no",
70 'deft' => "" },
71 { 'name' => "level2_header",
72 'desc' => "{StructuredHTMLPlugin.level2_header}",
73 'type' => "regexp",
74 'reqd' => "no",
75 'deft' => "" },
76 { 'name' => "level3_header",
77 'desc' => "{StructuredHTMLPlugin.level3_header}",
78 'type' => "regexp",
79 'reqd' => "no",
80 'deft' => "" },
81 { 'name' => "title_header",
82 'desc' => "{StructuredHTMLPlugin.title_header}",
83 'type' => "regexp",
84 'reqd' => "no",
85 'deft' => "" },
86 { 'name' => "delete_toc",
87 'desc' => "{StructuredHTMLPlugin.delete_toc}",
88 'type' => "flag",
89 'reqd' => "no",
90 # set the mode so this doesn't show up unless
91 # all the following ones do
92 'modegli' => "3"},
93 { 'name' => "toc_header",
94 'desc' => "{StructuredHTMLPlugin.toc_header}",
95 'type' => "regexp",
96 'reqd' => "no",
97 'deft' => "" }
98 ];
99
100 push(@$arguments,@$ws_arg);
101 }
102
103 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
104 push(@{$hashArgOptLists->{"OptList"}},$options);
105
106 my $self = new ConvertBinaryFile($pluginlist, $inputargs, $hashArgOptLists);
107
108 if ($self->{'info_only'}) {
109 # don't worry about any options etc
110 return bless $self, $class;
111 }
112
113 $self->{'filename_extension'} = "doc";
114 $self->{'file_type'} = "Word";
115
116 #this is passed through to gsConvert.pl by ConvertBinaryFile.pm
117 $self->{'convert_options'} = "-windows_scripting" if $self->{'windows_scripting'};
118
119 # we always save as utf-8
120# if ($self->{'input_encoding'} eq "auto") {
121# $self->{'input_encoding'} = "utf8";
122# }
123
124 my $secondary_plugin_options = $self->{'secondary_plugin_options'};
125 if (defined $self->{'windows_scripting'}) {
126 if (!defined $secondary_plugin_options->{'StructuredHTMLPlugin'}){
127 $secondary_plugin_options->{'StructuredHTMLPlugin'} = [];
128 my $structhtml_options = $secondary_plugin_options->{'StructuredHTMLPlugin'};
129
130 # Instruct HTMLPlugin (when eventually accessed through read_into_doc_obj)
131 # to extract these metadata fields from the HEAD META fields
132 push (@$structhtml_options,"-metadata_fields","Title,GENERATOR,date,author<Creator>");
133 push (@$structhtml_options , "-title_sub", '^(Page\s+\d+)?(\s*1\s+)?');
134 push (@$structhtml_options, "-description_tags") if $self->{'windows_scripting'};
135 push(@$structhtml_options,"-extract_language") if $self->{'extract_language'};
136 push (@$structhtml_options, "-delete_toc") if $self->{'delete_toc'};
137 push (@$structhtml_options, "-toc_header", $self->{'toc_header'}) if $self->{'toc_header'};
138 push (@$structhtml_options, "-title_header", $self->{'title_header'}) if $self->{'title_header'};
139 push (@$structhtml_options, "-level1_header", $self->{'level1_header'}) if $self->{'level1_header'};
140 push (@$structhtml_options, "-level2_header", $self->{'level2_header'})if $self->{'level2_header'};
141 push (@$structhtml_options, "-level3_header", $self->{'level3_header'}) if $self->{'level3_header'};
142 push (@$structhtml_options, "-metadata_fields", $self->{'metadata_fields'}) if $self->{'metadata_fields'};
143 }
144 }
145 if (!defined $secondary_plugin_options->{'HTMLPlugin'}) {
146 $secondary_plugin_options->{'HTMLPlugin'} = [];
147 }
148 if (!defined $secondary_plugin_options->{'TextPlugin'}) {
149 $secondary_plugin_options->{'TextPlugin'} = [];
150 }
151
152 my $html_options = $secondary_plugin_options->{'HTMLPlugin'};
153 my $text_options = $secondary_plugin_options->{'TextPlugin'};
154 my $structhtml_options = $secondary_plugin_options->{'StructuredHTMLPlugin'};
155 # wvWare will always produce html files encoded as utf-8, so make sure the secondary HTMLPlugin knows this
156 push(@$html_options,"-input_encoding", "utf8");
157 push(@$html_options,"-extract_language") if $self->{'extract_language'};
158 push(@$html_options, "-description_tags") if $self->{'description_tags'};
159
160 # Instruct HTMLPlugin (when eventually accessed through read_into_doc_obj)
161 # to extract these metadata fields from the HEAD META fields
162 push(@$html_options,"-metadata_fields","Title,GENERATOR,date,author<Creator>");
163 push(@$html_options , "-title_sub", '^(Page\s+\d+)?(\s*1\s+)?');
164
165 my $associate_tail_re = $self->{'associate_tail_re'};
166 if ((defined $associate_tail_re) && ($associate_tail_re ne "")) {
167 push(@$html_options, "-associate_tail_re", $associate_tail_re);
168 push(@$text_options, "-associate_tail_re", $associate_tail_re);
169 push(@$structhtml_options, "-associate_tail_re", $associate_tail_re);
170 }
171
172 $self = bless $self, $class;
173 $self->load_secondary_plugins($class,$secondary_plugin_options,$hashArgOptLists);
174
175 return bless $self;
176}
177
178sub get_default_process_exp {
179 my $self = shift (@_);
180
181 return q^(?i)\.(doc|dot)$^;
182}
183
184sub convert_post_process_old
185{
186 my $self = shift (@_);
187 my ($conv_filename) = @_;
188
189 my $outhandle=$self->{'outhandle'};
190
191 my ($language, $encoding) = $self->textcat_get_language_encoding ($conv_filename);
192
193 # read in file ($text will be in utf8)
194 my $text = "";
195 $self->read_file ($conv_filename, $encoding, $language, \$text);
196
197 # turn any high bytes that aren't valid utf-8 into utf-8.
198 #unicode::ensure_utf8(\$text);
199
200 # Write it out again!
201 #$self->utf8_write_file (\$text, $conv_filename);
202}
203
204# Modified to cache HTML files for efficieny reasons rather
205# than delete all. HTML is modified not to use IE's VML.
206# VML uses WML files, so these can be deleted.
207sub cleanup_tmp_area {
208 my ($self) = @_;
209 if (defined $self->{'files_dir'}) {
210 my $html_files_dir = $self->{'files_dir'};
211
212 if (opendir(DIN,$html_files_dir)) {
213 my @wmz_files = grep( /\.wmz$/, readdir(DIN));
214 foreach my $f (@wmz_files) {
215 my $full_f = &util::filename_cat($html_files_dir,$f);
216 &util::rm($full_f);
217 }
218 closedir(DIN);
219 }
220 else {
221 # if HTML file has no supporting images, then no _files dir made
222 # => do nothing
223 }
224 }
225}
226
227
2281;
229
Note: See TracBrowser for help on using the repository browser.