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

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

Jeffrey's new parsing modifications, committed approx 6 July, 15.16

  • Property svn:keywords set to Author Date Id Revision
File size: 5.4 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
33sub BEGIN {
34 @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' => "block_exp",
44 'desc' => "{BasPlug.block_exp}",
45 'type' => 'regexp',
46 'deft' => &get_default_block_exp() },
47 { 'name' => "extract_date",
48 'desc' => "{PSPlug.extract_date}",
49 'type' => "flag" },
50 { 'name' => "extract_pages",
51 'desc' => "{PSPlug.extract_pages}",
52 'type' => "flag" },
53 { 'name' => "extract_title",
54 'desc' => "{PSPlug.extract_title}",
55 'type' => "flag" } ];
56
57my $options = { 'name' => "PSPlug",
58 'desc' => "{PSPlug.desc}",
59 'abstract' => "no",
60 'inherits' => "yes",
61 'args' => $arguments };
62
63sub new {
64 my ($class) = shift (@_);
65 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
66 push(@$pluginlist, $class);
67
68 push(@$inputargs,"-convert_to");
69 push(@$inputargs,"text");
70 push(@$inputargs,"-title_sub");
71 push(@$inputargs,'^(Page\s+\d+)?(\s*1\s+)?');
72
73 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
74 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
75
76 my $self = (defined $hashArgOptLists)? new ConvertToPlug($pluginlist,$inputargs,$hashArgOptLists): new ConvertToPlug($pluginlist,$inputargs);
77
78 return bless $self, $class;
79}
80
81
82sub get_default_block_exp {
83 my $self = shift (@_);
84
85 return q^(?i)\.(eps)$^;
86}
87
88sub get_default_process_exp {
89 my $self = shift (@_);
90
91 return q^(?i)\.ps$^;
92}
93
94sub extract_metadata_from_postscript {
95 my $self = shift (@_);
96 my $filename = shift (@_);
97 my $doc = shift (@_);
98 my $section = $doc->get_top_section();
99
100 my $title_found = 0;
101 my $pages_found = 0;
102 my $date_found = 0;
103
104 print STDERR "PSPlug: extracting PostScript metadata from \"$filename\"\n"
105 if $self->{'verbosity'} > 1;
106
107 open(INPUT, "<$filename");
108 my $date;
109
110 while(my $line =<INPUT>) {
111 if ($self->{'extract_title'} && !$title_found) {
112 foreach my $word ($line =~ m|Title: ([-A-Za-z0-9@/\/\(\):,. ]*)|g) {
113 my $new_word = $word;
114 $new_word =~ s/\(Untitled\)//i;
115 $new_word =~ s/\(Microsoft Word\)//i;
116 $new_word =~ s/Microsoft Word//i;
117 $new_word =~ s/^\(//i;
118 $new_word =~ s/\)$//i;
119 $new_word =~ s/^ - //i;
120 if ($new_word ne "") {
121 $doc->add_utf8_metadata($section, "Title", $new_word );
122 $title_found = 1;
123 }
124 }
125 }
126 if ($self->{'extract_date'} && !$date_found) {
127 foreach my $word ($line =~ m/(Creation[-A-Za-z0-9@\/\(\):,. ]*)/g) {
128 if ($word =~ m/ ([A-Za-z][A-Za-z][A-Za-z]) ([0-9 ][0-9]) ?[0-9: ]+ ([0-9]{4})/) {
129 $date = &sorttools::format_date($2,$1,$3);
130 if (defined $date) {
131 $doc->add_utf8_metadata($section, "Date", $date );
132 }
133 }
134 if ($word =~ m/D:([0-9]{4})([0-9]{2})([0-9]{2})[0-9]{6}\)/) {
135 $date = &sorttools::format_date($3,$2,$1);
136 if (defined $date) {
137 $doc->add_utf8_metadata($section, "Date", $date );
138 }
139 }
140 if ($word =~ m/CreationDate: ([0-9]{4}) ([A-Za-z][A-Za-z][A-Za-z]) ([0-9 ][0-9]) [0-9:]*/) {
141 $date = &sorttools::format_date($3,$2,$1);
142 if (defined $date) {
143 $doc->add_utf8_metadata($section, "Date", $date );
144 }
145 }
146 $date_found = 1;
147 }
148 }
149 if ($self->{'extract_pages'} && !$pages_found) {
150 foreach my $word ($line =~ m/(Pages: [0-9]*)/g) {
151 my $digits = $word;
152 $digits =~ s/[^0-9]//g;
153 if ($digits ne "" && $digits ne "0") {
154 $doc->add_utf8_metadata($section, "Pages", $digits );
155 $pages_found = 1;
156 }
157 }
158 }
159 }
160}
161
162# do plugin specific processing of doc_obj for HTML type
163sub process {
164 my $self = shift (@_);
165 my ($trash, $trash2, $path, $file, $trash3, $doc, $gli) = @_;
166
167 my $outhandle = $self->{'outhandle'};
168 print $outhandle "PSPlug: passing $_[3] on to $self->{'converted_to'}Plug\n"
169 if $self->{'verbosity'} > 1;
170 print STDERR "<Processing n='$_[3]' p='PSPlug'>\n" if ($gli);
171
172 &extract_metadata_from_postscript($self,"$path/$file", $doc);
173
174 return ConvertToPlug::process_type($self,"ps",@_);
175}
176
177
1781;
179
Note: See TracBrowser for help on using the repository browser.