source: gsdl/trunk/perllib/plugins/WordPlug.pm@ 14661

Last change on this file since 14661 was 12834, checked in by kjdon, 18 years ago

these convertto plugins were all setting extract_language=1 to their secondary plugins. we don't want this - only pass to secondary plugin if user has asked for it. textcat can be very slow, so don't want to run it unless we have to

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