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

Last change on this file since 10504 was 10496, checked in by kjdon, 19 years ago

added some sanity checks, renamed the checkout_toc option to delete_toc

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