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

Last change on this file since 10504 was 10425, checked in by chi, 19 years ago

Modification of the way passing argument and option lists for the secondary plugin.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.9 KB
Line 
1###########################################################################
2#
3# PSPlug.pm -- this might look VERY similar to the PDF plugin...
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
26# 12/05/02 Added usage datastructure - John Thompson
27
28package PSPlug;
29
30use ConvertToPlug;
31use sorttools;
32
33use strict;
34no strict 'refs'; # allow filehandles to be variables and viceversa
35
36sub BEGIN {
37 @PSPlug::ISA = ('ConvertToPlug');
38}
39
40my $arguments =
41 [ { 'name' => "process_exp",
42 'desc' => "{BasPlug.process_exp}",
43 'type' => "regexp",
44 'deft' => &get_default_process_exp(),
45 'reqd' => "no" },
46 { 'name' => "block_exp",
47 'desc' => "{BasPlug.block_exp}",
48 'type' => 'regexp',
49 'deft' => &get_default_block_exp() },
50 { 'name' => "extract_date",
51 'desc' => "{PSPlug.extract_date}",
52 'type' => "flag" },
53 { 'name' => "extract_pages",
54 'desc' => "{PSPlug.extract_pages}",
55 'type' => "flag" },
56 { 'name' => "extract_title",
57 'desc' => "{PSPlug.extract_title}",
58 'type' => "flag" } ];
59
60my $options = { 'name' => "PSPlug",
61 'desc' => "{PSPlug.desc}",
62 'abstract' => "no",
63 'inherits' => "yes",
64 'args' => $arguments };
65
66sub new {
67 my ($class) = shift (@_);
68 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
69 push(@$pluginlist, $class);
70
71 push(@$inputargs,"-convert_to");
72 push(@$inputargs,"text");
73 push(@$inputargs,"-title_sub");
74 push(@$inputargs,'^(Page\s+\d+)?(\s*1\s+)?');
75
76 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
77 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
78
79 my $self = (defined $hashArgOptLists)? new ConvertToPlug($pluginlist,$inputargs,$hashArgOptLists): new ConvertToPlug($pluginlist,$inputargs);
80
81 my $secondary_plugin_options = $self->{'secondary_plugin_options'};
82 if (!defined $secondary_plugin_options->{'TEXTPlug'}) {
83 $secondary_plugin_options->{'TEXTPlug'} = [];
84 }
85
86 my $text_options = $secondary_plugin_options->{'TEXTPlug'};
87
88 # following title_sub removes "Page 1" added by ps2ascii, and a leading
89 # "1", which is often the page number at the top of the page. Bad Luck
90 # if your document title actually starts with "1 " - is there a better way?
91 #$self->{'input_encoding'} = "utf8";
92 #$self->{'extract_language'} = 1;
93 push(@$text_options, "-input_encoding", "utf8");
94 push(@$text_options,"-extract_language");
95 push(@$text_options , "-title_sub", '^(Page\s+\d+)?(\s*1\s+)?');
96
97 $self = bless $self, $class;
98
99 $self->load_secondary_plugins($class,$secondary_plugin_options, $hashArgOptLists);
100
101 return $self;
102}
103
104
105sub get_default_block_exp {
106 my $self = shift (@_);
107
108 return q^(?i)\.(eps)$^;
109}
110
111sub get_default_process_exp {
112 my $self = shift (@_);
113
114 return q^(?i)\.ps$^;
115}
116
117sub convert_post_process
118{
119 my $self = shift (@_);
120 my ($conv_filename) = @_;
121
122 my $outhandle=$self->{'outhandle'};
123
124 my ($language, $encoding) = $self->textcat_get_language_encoding ($conv_filename);
125
126 # read in file ($text will be in utf8)
127 my $text = "";
128 $self->read_file ($conv_filename, $encoding, $language, \$text);
129
130 # turn any high bytes that aren't valid utf-8 into utf-8.
131 unicode::ensure_utf8(\$text);
132
133 # Write it out again!
134 $self->utf8_write_file (\$text, $conv_filename);
135}
136
137sub extract_metadata_from_postscript {
138 my $self = shift (@_);
139
140 my ($filename,$doc) = @_;
141
142 my $section = $doc->get_top_section();
143
144 my $title_found = 0;
145 my $pages_found = 0;
146 my $date_found = 0;
147
148 print STDERR "PSPlug: extracting PostScript metadata from \"$filename\"\n"
149 if $self->{'verbosity'} > 1;
150
151 open(INPUT, "<$filename");
152 my $date;
153
154 while(my $line =<INPUT>) {
155 if ($self->{'extract_title'} && !$title_found) {
156 foreach my $word ($line =~ m|Title: ([-A-Za-z0-9@/\/\(\):,. ]*)|g) {
157 my $new_word = $word;
158 $new_word =~ s/\(Untitled\)//i;
159 $new_word =~ s/\(Microsoft Word\)//i;
160 $new_word =~ s/Microsoft Word//i;
161 $new_word =~ s/^\(//i;
162 $new_word =~ s/\)$//i;
163 $new_word =~ s/^ - //i;
164 if ($new_word ne "") {
165 $doc->add_utf8_metadata($section, "Title", $new_word );
166 $title_found = 1;
167 }
168 }
169 }
170 if ($self->{'extract_date'} && !$date_found) {
171 foreach my $word ($line =~ m/(Creation[-A-Za-z0-9@\/\(\):,. ]*)/g) {
172 if ($word =~ m/ ([A-Za-z][A-Za-z][A-Za-z]) ([0-9 ][0-9]) ?[0-9: ]+ ([0-9]{4})/) {
173 $date = &sorttools::format_date($2,$1,$3);
174 if (defined $date) {
175 $doc->add_utf8_metadata($section, "Date", $date );
176 }
177 }
178 if ($word =~ m/D:([0-9]{4})([0-9]{2})([0-9]{2})[0-9]{6}\)/) {
179 $date = &sorttools::format_date($3,$2,$1);
180 if (defined $date) {
181 $doc->add_utf8_metadata($section, "Date", $date );
182 }
183 }
184 if ($word =~ m/CreationDate: ([0-9]{4}) ([A-Za-z][A-Za-z][A-Za-z]) ([0-9 ][0-9]) [0-9:]*/) {
185 $date = &sorttools::format_date($3,$2,$1);
186 if (defined $date) {
187 $doc->add_utf8_metadata($section, "Date", $date );
188 }
189 }
190 $date_found = 1;
191 }
192 }
193 if ($self->{'extract_pages'} && !$pages_found) {
194 foreach my $word ($line =~ m/(Pages: [0-9]*)/g) {
195 my $digits = $word;
196 $digits =~ s/[^0-9]//g;
197 if ($digits ne "" && $digits ne "0") {
198 $doc->add_utf8_metadata($section, "Pages", $digits );
199 $pages_found = 1;
200 }
201 }
202 }
203 }
204}
205
206# do plugin specific processing of doc_obj for HTML type
207sub process {
208 my $self = shift (@_);
209 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
210
211# my $outhandle = $self->{'outhandle'};
212
213# print $outhandle "PSPlug: passing $file on to $self->{'converted_to'}Plug\n"
214# if $self->{'verbosity'} > 1;
215# print STDERR "<Processing n='$file' p='PSPlug'>\n" if ($gli);
216
217 my $filename = &util::filename_cat($base_dir,$file);
218 $self->extract_metadata_from_postscript($filename, $doc_obj);
219
220 return $self->process_type("ps",$base_dir,$file,$doc_obj);
221}
222
223
2241;
225
Note: See TracBrowser for help on using the repository browser.