1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # PagedImgPlug.pm -- plugin for sets of images and OCR text that
|
---|
4 | # make up a document
|
---|
5 | # A component of the Greenstone digital library software
|
---|
6 | # from the New Zealand Digital Library Project at the
|
---|
7 | # University of Waikato, New Zealand.
|
---|
8 | #
|
---|
9 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
10 | #
|
---|
11 | # This program is free software; you can redistribute it and/or modify
|
---|
12 | # it under the terms of the GNU General Public License as published by
|
---|
13 | # the Free Software Foundation; either version 2 of the License, or
|
---|
14 | # (at your option) any later version.
|
---|
15 | #
|
---|
16 | # This program is distributed in the hope that it will be useful,
|
---|
17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | # GNU General Public License for more details.
|
---|
20 | #
|
---|
21 | # You should have received a copy of the GNU General Public License
|
---|
22 | # along with this program; if not, write to the Free Software
|
---|
23 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
24 | #
|
---|
25 | ###########################################################################
|
---|
26 |
|
---|
27 | # PagedImgPlug
|
---|
28 | # processes sequences of images, with optional OCR text
|
---|
29 | #
|
---|
30 | # This plugin takes .item files, which contain metadata and lists of image
|
---|
31 | # files, and produces a document containing sections, one for each page.
|
---|
32 | #
|
---|
33 | # The format of the .item file is as follows:
|
---|
34 | # The first lines contain any metadata for the whole document
|
---|
35 | # <metadata-name>metadata-value
|
---|
36 | # eg.
|
---|
37 | # <Title>Snail farming
|
---|
38 | # <Date>19230102
|
---|
39 | # Then comes a list of pages, one page per line, each line has the format
|
---|
40 | # pagenum:imagefile:textfile:r
|
---|
41 | # page num and imagefile are required. pagenum is used for the Title
|
---|
42 | # of the section, and in the display is shown as page <pagenum>.
|
---|
43 | # imagefile is the image for the page. textfile is an optional text
|
---|
44 | # file containing the OCR (or any) text for the page - this gets added
|
---|
45 | # as the text for the section. r is optional, and signals that the image
|
---|
46 | # should be rotated 180deg. Eg use this if the image has been made upside down.
|
---|
47 | # So an example item file looks like:
|
---|
48 | # <Title>Snail farming
|
---|
49 | # <Date>19960403
|
---|
50 | # 1:p1.gif:p1.txt:
|
---|
51 | # 2:p2.gif::
|
---|
52 | # 3:p3.gif:p3.txt:
|
---|
53 | # 3b:p3b.gif:p3b.txt:r
|
---|
54 | # The second page has no text, the fourth page is a back page, and
|
---|
55 | # should be rotated.
|
---|
56 | #
|
---|
57 | # All the supplemetary image amd text files should be in the same folder as
|
---|
58 | # the .item file.
|
---|
59 | #
|
---|
60 | # To display the images instead of the document text, you can use [srcicon]
|
---|
61 | # (the original image) or [screenicon] (the smaller image) in a format
|
---|
62 | # statement. Her is one that switches between the two:
|
---|
63 | #
|
---|
64 | # format DocumentText "<center><table width=_pagewidth_><tr><td>_If_('_cgiargp_' eq 'full',<a href='_httpdocument_&d=_cgiargd_&p=small'>Switch to small version.</a>,<a href='_httpdocument_&d=_cgiargd_&p=full'>Switch to fullsize version</a>)</td></tr><tr><td>_If_('_cgiargp_' eq 'full',<a href='_httpdocument_&d=_cgiargd_&p=small' title='Switch to small version'>[srcicon]</a>,<a href='_httpdocument_&d=_cgiargd_&p=full' title="Switch to fullsize version'>[screenicon]</a></td></tr></table></center>"
|
---|
65 | #
|
---|
66 | # Additional metadata can be added into the .item files, alternatively you can
|
---|
67 | # use normal metadata.xml files, with the name of the .item file as the
|
---|
68 | # FileName.
|
---|
69 |
|
---|
70 | package PagedImgPlug;
|
---|
71 |
|
---|
72 | use BasPlug;
|
---|
73 |
|
---|
74 | sub BEGIN {
|
---|
75 | @ISA = ('BasPlug');
|
---|
76 | }
|
---|
77 |
|
---|
78 | my $arguments =
|
---|
79 | [ { 'name' => "process_exp",
|
---|
80 | 'desc' => "{BasPlug.process_exp}",
|
---|
81 | 'type' => "string",
|
---|
82 | 'deft' => &get_default_process_exp(),
|
---|
83 | 'reqd' => "no" },
|
---|
84 | { 'name' => "block_exp",
|
---|
85 | 'desc' => "{BasPlug.block_exp}",
|
---|
86 | 'type' => "string",
|
---|
87 | 'deft' => &get_default_block_exp(),
|
---|
88 | 'reqd' => "no" },
|
---|
89 | { 'name' => "noscaleup",
|
---|
90 | 'desc' => "{ImagePlug.noscaleup}",
|
---|
91 | 'type' => "flag",
|
---|
92 | 'reqd' => "no" },
|
---|
93 | { 'name' => "thumbnail",
|
---|
94 | 'desc' => "{PagedImgPlug.thumbnail}",
|
---|
95 | 'type' => "flag",
|
---|
96 | 'reqd' => "no" },
|
---|
97 | { 'name' => "thumbnailsize",
|
---|
98 | 'desc' => "{ImagePlug.thumbnailsize}",
|
---|
99 | 'type' => "int",
|
---|
100 | 'deft' => "100",
|
---|
101 | 'reqd' => "no" },
|
---|
102 | { 'name' => "thumbnailtype",
|
---|
103 | 'desc' => "{ImagePlug.thumbnailtype}",
|
---|
104 | 'type' => "string",
|
---|
105 | 'deft' => "gif",
|
---|
106 | 'reqd' => "no" },
|
---|
107 | { 'name' => "screenview",
|
---|
108 | 'desc' => "{PagedImgPlug.screenview}",
|
---|
109 | 'type' => "flag",
|
---|
110 | 'reqd' => "no" },
|
---|
111 | { 'name' => "screenviewsize",
|
---|
112 | 'desc' => "{PagedImgPlug.screenviewsize}",
|
---|
113 | 'type' => "int",
|
---|
114 | 'deft' => "500",
|
---|
115 | 'reqd' => "no" },
|
---|
116 | { 'name' => "screenviewtype",
|
---|
117 | 'desc' => "{PagedImgPlug.screenviewtype}",
|
---|
118 | 'type' => "string",
|
---|
119 | 'deft' => "jpg",
|
---|
120 | 'reqd' => "no" },
|
---|
121 | { 'name' => "converttotype",
|
---|
122 | 'desc' => "{ImagePlug.converttotype}",
|
---|
123 | 'type' => "string",
|
---|
124 | 'deft' => "",
|
---|
125 | 'reqd' => "no" },
|
---|
126 | { 'name' => "minimumsize",
|
---|
127 | 'desc' => "{ImagePlug.minimumsize}",
|
---|
128 | 'type' => "int",
|
---|
129 | 'deft' => "100",
|
---|
130 | 'reqd' => "no" },
|
---|
131 | { 'name' => "noheaderpage",
|
---|
132 | 'desc' => "{PagedImgPlug.noheaderpage}",
|
---|
133 | 'type' => "flag",
|
---|
134 | 'reqd' => "no" },
|
---|
135 | { 'name' => "documenttype",
|
---|
136 | 'desc' => "{PagedImgPlug.documenttype}",
|
---|
137 | 'type' => "enum",
|
---|
138 | 'list' => $type_list,
|
---|
139 | 'deft' => "paged",
|
---|
140 | 'reqd' => "no" } ];
|
---|
141 |
|
---|
142 | my $type_list =
|
---|
143 | [ { 'name' => "paged",
|
---|
144 | 'desc' => "{PagedImgPlug.documenttype.paged}" },
|
---|
145 | { 'name' => "hierarchy",
|
---|
146 | 'desc' => "{PagedImgPlug.documenttype.hierarchy}" } ];
|
---|
147 |
|
---|
148 | my $options = { 'name' => "PagedImgPlug",
|
---|
149 | 'desc' => "{PagedImgPlug.desc}",
|
---|
150 | 'inherits' => "yes",
|
---|
151 | 'args' => $arguments };
|
---|
152 |
|
---|
153 |
|
---|
154 | sub new {
|
---|
155 | my ($class) = @_;
|
---|
156 | my $plugin_name = shift (@_);
|
---|
157 | my $self = new BasPlug ("PagedImgPlug", @_);
|
---|
158 |
|
---|
159 | my $option_list = $self->{'option_list'};
|
---|
160 | push( @{$option_list}, $options );
|
---|
161 |
|
---|
162 | if (!parsargv::parse(\@_,
|
---|
163 | q^noscaleup^, \$self->{'noscaleup'},
|
---|
164 | q^converttotype/.*/^, \$self->{'converttotype'},
|
---|
165 | q^minimumsize/[0-9]*/100^, \$self->{'minimumsize'},
|
---|
166 |
|
---|
167 | q^thumbnailsize/[0-9]*/100^, \$self->{'thumbnailsize'},
|
---|
168 | q^thumbnailtype/.*/gif^, \$self->{'thumbnailtype'},
|
---|
169 | q^screenviewsize/[0-9]*/0^, \$self->{'screenviewsize'},
|
---|
170 | q^screenviewtype/.*/jpg^, \$self->{'screenviewtype'},
|
---|
171 | q^thumbnail^, \$self->{'thumbnail'},
|
---|
172 | q^screenview^, \$self->{'screenview'},
|
---|
173 | q^noheaderpage^, \$self->{'noheaderpage'},
|
---|
174 | 'documenttype/^(paged|hierarchy)$/paged', \$self->{'doctype'},
|
---|
175 | "allow_extra_options")) {
|
---|
176 |
|
---|
177 | print STDERR "\nPagedImgPlug uses an incorrect option.\n";
|
---|
178 | print STDERR "Check your collect.cfg configuration file.\n";
|
---|
179 | $self->print_txt_usage(""); # Use default resource bundle
|
---|
180 | die "\n";
|
---|
181 | }
|
---|
182 |
|
---|
183 | return bless $self, $class;
|
---|
184 | }
|
---|
185 |
|
---|
186 | sub get_default_process_exp {
|
---|
187 | my $self = shift (@_);
|
---|
188 |
|
---|
189 | return q^\.item$^;
|
---|
190 | }
|
---|
191 |
|
---|
192 | # want to block everything except the .item ones
|
---|
193 | # but instead we will block images and txt files
|
---|
194 | sub get_default_block_exp {
|
---|
195 | my $self = shift (@_);
|
---|
196 |
|
---|
197 | return q^(?i)(\.jpe?g|\.gif|\.png|\.tif?f|\.te?xt|~)$^
|
---|
198 | }
|
---|
199 | # Create the thumbnail and screenview images, and discover the Image's
|
---|
200 | # size, width, and height using the convert utility.
|
---|
201 | sub process_image {
|
---|
202 | my $self = shift (@_);
|
---|
203 | my $filename = shift (@_); # filename with full path
|
---|
204 | my $srcfile = shift (@_); # filename without path
|
---|
205 | my $doc_obj = shift (@_);
|
---|
206 | my $section = shift (@_); #the current section
|
---|
207 | my $rotate = shift (@_); # whether to rotate the image or not
|
---|
208 |
|
---|
209 | my $top=0;
|
---|
210 | if ($section eq $doc_obj->get_top_section()) {
|
---|
211 | $top=1;
|
---|
212 | }
|
---|
213 | my $verbosity = $self->{'verbosity'};
|
---|
214 | my $outhandle = $self->{'outhandle'};
|
---|
215 |
|
---|
216 | # check the filename is okay
|
---|
217 | return 0 if ($srcfile eq "" || $filename eq "");
|
---|
218 |
|
---|
219 | my $minimumsize = $self->{'minimumsize'};
|
---|
220 | if (defined $minimumsize && (-s $filename < $minimumsize)) {
|
---|
221 | print $outhandle "PagedImgPlug: \"$filename\" too small, skipping\n"
|
---|
222 | if ($verbosity > 1);
|
---|
223 | }
|
---|
224 |
|
---|
225 | # Convert the image to a new type (if required), and rotate if required.
|
---|
226 | my $converttotype = $self->{'converttotype'};
|
---|
227 | my $originalfilename = ""; # only set if we do a conversion
|
---|
228 | my $type = "unknown";
|
---|
229 | my $converted = 0;
|
---|
230 | my $rotated=0;
|
---|
231 | if ($converttotype ne "" && $filename !~ /$converttotype$/) {
|
---|
232 | $converted=1;
|
---|
233 | $originalfilename = $filename;
|
---|
234 | my $filehead = &util::get_tmp_filename();
|
---|
235 | $filename = $filehead . ".$converttotype";
|
---|
236 | $n = 1;
|
---|
237 | while (-e $filename) {
|
---|
238 | $filename = "$filehead$n\.$converttotype";
|
---|
239 | $n++;
|
---|
240 | }
|
---|
241 | $self->{'tmp_filename1'} = $filename;
|
---|
242 |
|
---|
243 | my $rotate_option = "";
|
---|
244 | if ($rotate eq "r") {
|
---|
245 | $rotate_option = "-rotate 180 ";
|
---|
246 | }
|
---|
247 |
|
---|
248 | my $command = "convert -verbose \"$originalfilename\" $rotate_option \"$filename\"";
|
---|
249 | print $outhandle "CONVERT: $command\n" if ($verbosity > 2);
|
---|
250 | my $result = '';
|
---|
251 | $result = `$command`;
|
---|
252 | print $outhandle "CONVERT RESULT = $result\n" if ($verbosity > 2);
|
---|
253 |
|
---|
254 | $type = $converttotype;
|
---|
255 | } elsif ($rotate eq "r") {
|
---|
256 | $rotated=1;
|
---|
257 | $originalfilename = $filename;
|
---|
258 | $filename = &util::get_tmp_filename();
|
---|
259 |
|
---|
260 | my $command = "convert \"$originalfilename\" -rotate 180 \"$filename\"";
|
---|
261 | print $outhandle "ROTATE: $command\n" if ($verbosity > 2);
|
---|
262 | my $result = '';
|
---|
263 | $result = `$command`;
|
---|
264 | print $outhandle "ROTATE RESULT = $result\n" if ($verbosity > 2);
|
---|
265 |
|
---|
266 | }
|
---|
267 |
|
---|
268 |
|
---|
269 | # Add the image metadata
|
---|
270 | my $file; # the new file name
|
---|
271 | my ($id) = $srcfile =~ /^([^\.]*)/; #the new file name without an extension
|
---|
272 | if ($converted) {
|
---|
273 | # we have converted the image
|
---|
274 | # add on the new extension
|
---|
275 | $file .= "$id.$converttotype";
|
---|
276 | } else {
|
---|
277 | $file = $srcfile;
|
---|
278 | }
|
---|
279 |
|
---|
280 | my $url =$file; # the new file name prepared for a url
|
---|
281 | my $srcurl = $srcfile;
|
---|
282 | $url =~ s/ /%20/g;
|
---|
283 | $srcurl =~ s/ /%20/g;
|
---|
284 |
|
---|
285 | $doc_obj->add_metadata ($section, "Image", $url);
|
---|
286 |
|
---|
287 | # Also want to set filename as 'Source' metadata to be
|
---|
288 | # consistent with other plugins
|
---|
289 | $doc_obj->add_metadata ($section, "Source", $srcurl);
|
---|
290 |
|
---|
291 | my ($image_type, $image_width, $image_height, $image_size)
|
---|
292 | = &identify($filename, $outhandle, $verbosity);
|
---|
293 |
|
---|
294 | $doc_obj->add_metadata ($section, "ImageType", $image_type);
|
---|
295 | $doc_obj->add_metadata ($section, "ImageWidth", $image_width);
|
---|
296 | $doc_obj->add_metadata ($section, "ImageHeight", $image_height);
|
---|
297 | $doc_obj->add_metadata ($section, "ImageSize", $image_size);
|
---|
298 |
|
---|
299 | if ($type eq "unknown" && $image_type) {
|
---|
300 | $type = $image_type;
|
---|
301 | }
|
---|
302 |
|
---|
303 | if ($top) {
|
---|
304 | $doc_obj->add_metadata ($section, "srclink",
|
---|
305 | "<a href=_httpcollection_/index/assoc/[assocfilepath]/[Image]>");
|
---|
306 | $doc_obj->add_metadata ($section, "srcicon", "<img src=_httpcollection_/index/assoc/[assocfilepath]/[Image]>");
|
---|
307 |
|
---|
308 | } else {
|
---|
309 | $doc_obj->add_metadata ($section, "srclink",
|
---|
310 | "<a href=_httpcollection_/index/assoc/[parent:assocfilepath]/[Image]>");
|
---|
311 | $doc_obj->add_metadata ($section, "srcicon", "<img src=_httpcollection_/index/assoc/[parent:assocfilepath]/[Image]>");
|
---|
312 |
|
---|
313 | }
|
---|
314 | $doc_obj->add_metadata ($section, "/srclink", "</a>");
|
---|
315 |
|
---|
316 |
|
---|
317 | # Add the image as an associated file
|
---|
318 | $doc_obj->associate_file($filename,$file,"image/$type",$section);
|
---|
319 | print $outhandle "associating file $filename as name $file\n" if ($verbosity > 2);
|
---|
320 |
|
---|
321 | if ($self->{'thumbnail'}) {
|
---|
322 | # Make the thumbnail image
|
---|
323 | my $thumbnailsize = $self->{'thumbnailsize'} || 100;
|
---|
324 | my $thumbnailtype = $self->{'thumbnailtype'} || 'gif';
|
---|
325 |
|
---|
326 | my $filehead = &util::get_tmp_filename();
|
---|
327 | my $thumbnailfile = $filehead . ".$thumbnailtype";
|
---|
328 | my $n=1;
|
---|
329 | while (-e $thumbnailfile) {
|
---|
330 | $thumbnailfile = $filehead . $n . ".$thumbnailtype";
|
---|
331 | $n++;
|
---|
332 | }
|
---|
333 |
|
---|
334 | $self->{'tmp_filename2'} = $thumbnailfile;
|
---|
335 |
|
---|
336 | # Generate the thumbnail with convert
|
---|
337 | my $command = "convert -verbose -geometry $thumbnailsize"
|
---|
338 | . "x$thumbnailsize \"$filename\" \"$thumbnailfile\"";
|
---|
339 | print $outhandle "THUMBNAIL: $command\n" if ($verbosity > 2);
|
---|
340 | my $result = '';
|
---|
341 | $result = `$command 2>&1` ;
|
---|
342 | print $outhandle "THUMB RESULT: $result\n" if ($verbosity > 2);
|
---|
343 |
|
---|
344 | # Add the thumbnail as an associated file ...
|
---|
345 | if (-e "$thumbnailfile") {
|
---|
346 | $doc_obj->associate_file("$thumbnailfile", $id."thumb.$thumbnailtype", "image/$thumbnailtype",$section);
|
---|
347 | $doc_obj->add_metadata ($section, "ThumbType", $thumbnailtype);
|
---|
348 | $doc_obj->add_metadata ($section, "Thumb", $id."thumb.$thumbnailtype");
|
---|
349 |
|
---|
350 | $doc_obj->add_metadata ($section, "thumbicon", "<img src=_httpcollection_/index/assoc/[parent:assocfilepath]/[Thumb] width=[ThumbWidth] height=[ThumbHeight]>");
|
---|
351 | }
|
---|
352 |
|
---|
353 | # Extract Thumnail metadata from convert output
|
---|
354 | if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
|
---|
355 | $doc_obj->add_metadata ($section, "ThumbWidth", $1);
|
---|
356 | $doc_obj->add_metadata ($section, "ThumbHeight", $2);
|
---|
357 | }
|
---|
358 | }
|
---|
359 | # Make a screen-sized version of the picture if requested
|
---|
360 | if ($self->{'screenview'}) {
|
---|
361 |
|
---|
362 | # To do: if the actual image is smaller than the screenview size,
|
---|
363 | # we should use the original !
|
---|
364 |
|
---|
365 | my $screenviewsize = $self->{'screenviewsize'} || 500;
|
---|
366 | my $screenviewtype = $self->{'screenviewtype'} || 'jpeg';
|
---|
367 | my $filehead = &util::get_tmp_filename();
|
---|
368 | my $screenviewfilename = $filehead . ".$screenviewtype";
|
---|
369 | my $n=1;
|
---|
370 | while (-e $screenviewfilename) {
|
---|
371 | $screenviewfilename = "$filehead$n\.$screenviewtype";
|
---|
372 | $n++;
|
---|
373 | }
|
---|
374 | $self->{'tmp_filename3'} = $screenviewfilename;
|
---|
375 |
|
---|
376 | # make the screenview image
|
---|
377 | my $command = "convert -verbose -geometry $screenviewsize"
|
---|
378 | . "x$screenviewsize \"$filename\" \"$screenviewfilename\"";
|
---|
379 | print $outhandle "SCREENVIEW: $command\n" if ($verbosity > 2);
|
---|
380 | my $result = "";
|
---|
381 | $result = `$command 2>&1` ;
|
---|
382 | print $outhandle "SCREENVIEW RESULT: $result\n" if ($verbosity > 3);
|
---|
383 |
|
---|
384 | # get screenview dimensions, size and type
|
---|
385 | if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
|
---|
386 | $doc_obj->add_metadata ($section, "ScreenWidth", $1);
|
---|
387 | $doc_obj->add_metadata ($section, "ScreenHeight", $2);
|
---|
388 | }
|
---|
389 |
|
---|
390 | #add the screenview as an associated file ...
|
---|
391 | if (-e "$screenviewfilename") {
|
---|
392 | $doc_obj->associate_file("$screenviewfilename", $id."sv.$screenviewtype",
|
---|
393 | "image/$screenviewtype",$section);
|
---|
394 | print $outhandle "associating screen file $screenviewfilename as name $id sv.$screenviewtype\n" if ($verbosity > 2);
|
---|
395 |
|
---|
396 | $doc_obj->add_metadata ($section, "ScreenType", $screenviewtype);
|
---|
397 | $doc_obj->add_metadata ($section, "Screen", $id."sv.$screenviewtype");
|
---|
398 |
|
---|
399 | if ($top) {
|
---|
400 | $doc_obj->add_metadata ($section, "screenicon", "<img src=_httpcollection_/index/assoc/[assocfilepath]/[Screen] width=[ScreenWidth] height=[ScreenHeight]>");
|
---|
401 | } else {
|
---|
402 | $doc_obj->add_metadata ($section, "screenicon", "<img src=_httpcollection_/index/assoc/{If}{[parent:assocfilepath],[parent:assocfilepath],[assocfilepath]}/[Screen] width=[ScreenWidth] height=[ScreenHeight]>");
|
---|
403 |
|
---|
404 | }
|
---|
405 | } else {
|
---|
406 | print $outhandle "PagedImgPlug: couldn't find \"$screenviewfilename\"\n";
|
---|
407 | }
|
---|
408 | }
|
---|
409 |
|
---|
410 | return $type;
|
---|
411 |
|
---|
412 |
|
---|
413 | }
|
---|
414 |
|
---|
415 |
|
---|
416 |
|
---|
417 | # Discover the characteristics of an image file with the ImageMagick
|
---|
418 | # "identify" command.
|
---|
419 |
|
---|
420 | sub identify {
|
---|
421 | my ($image, $outhandle, $verbosity) = @_;
|
---|
422 |
|
---|
423 | # Use the ImageMagick "identify" command to get the file specs
|
---|
424 | my $command = "identify $image 2>&1";
|
---|
425 | print $outhandle "$command\n" if ($verbosity > 2);
|
---|
426 | my $result = '';
|
---|
427 | $result = `$command`;
|
---|
428 | print $outhandle "$result\n" if ($verbosity > 3);
|
---|
429 |
|
---|
430 | # Read the type, width, and height
|
---|
431 | my $type = 'unknown';
|
---|
432 | my $width = 'unknown';
|
---|
433 | my $height = 'unknown';
|
---|
434 |
|
---|
435 | my $image_safe = quotemeta $image;
|
---|
436 | if ($result =~ /^$image_safe (\w+) (\d+)x(\d+)/) {
|
---|
437 | $type = $1;
|
---|
438 | $width = $2;
|
---|
439 | $height = $3;
|
---|
440 | }
|
---|
441 |
|
---|
442 | # Read the size
|
---|
443 | my $size = "unknown";
|
---|
444 | if ($result =~ m/^.* ([0-9]+)b/) {
|
---|
445 | $size = $1;
|
---|
446 | } elsif ($result =~ m/^.* ([0-9]+)kb/) {
|
---|
447 | $size = 1024 * $1;
|
---|
448 | }
|
---|
449 |
|
---|
450 | print $outhandle "file: $image:\t $type, $width, $height, $size\n"
|
---|
451 | if ($verbosity > 3);
|
---|
452 |
|
---|
453 | # Return the specs
|
---|
454 | return ($type, $width, $height, $size);
|
---|
455 | }
|
---|
456 |
|
---|
457 |
|
---|
458 | # The PagedImgPlug read() function. This function does all the right things
|
---|
459 | # to make general options work for a given plugin. It calls the process()
|
---|
460 | # function which does all the work specific to a plugin (like the old
|
---|
461 | # read functions used to do). Most plugins should define their own
|
---|
462 | # process() function and let this read() function keep control.
|
---|
463 | #
|
---|
464 | # PagedImgPlug overrides read() because there is no need to read the actual
|
---|
465 | # text of the file in, because the contents of the file is not text...
|
---|
466 | #
|
---|
467 | # Return number of files processed, undef if can't process
|
---|
468 | # Note that $base_dir might be "" and that $file might
|
---|
469 | # include directories
|
---|
470 |
|
---|
471 | sub read {
|
---|
472 | my $self = shift (@_);
|
---|
473 | my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;
|
---|
474 | my $outhandle = $self->{'outhandle'};
|
---|
475 |
|
---|
476 | my $filename = &util::filename_cat($base_dir, $file);
|
---|
477 | return 0 if $self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/;
|
---|
478 | if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
|
---|
479 | return undef;
|
---|
480 | }
|
---|
481 |
|
---|
482 | print $outhandle "PagedImgPlug processing \"$filename\"\n"
|
---|
483 | if $self->{'verbosity'} > 1;
|
---|
484 |
|
---|
485 | my ($dir);
|
---|
486 | ($dir, $file) = $filename =~ /^(.*?)([^\/\\]*)$/;
|
---|
487 |
|
---|
488 | #process the .item file
|
---|
489 | my $doc_obj = $self->process_item($filename, $dir, $file, $processor);
|
---|
490 |
|
---|
491 | # include any metadata passed in from previous plugins
|
---|
492 | # note that this metadata is associated with the top level section
|
---|
493 | my $section = $doc_obj->get_top_section();
|
---|
494 | $self->extra_metadata ($doc_obj, $section, $metadata);
|
---|
495 |
|
---|
496 | # do plugin specific processing of doc_obj
|
---|
497 | return undef unless defined ($self->process (\$text, $pluginfo, $base_dir,
|
---|
498 | $file, $metadata, $doc_obj));
|
---|
499 |
|
---|
500 | # do any automatic metadata extraction
|
---|
501 | $self->auto_extract_metadata ($doc_obj);
|
---|
502 |
|
---|
503 | # process the document
|
---|
504 | $processor->process($doc_obj);
|
---|
505 |
|
---|
506 | # clean up temporary files - we do this here instead of in
|
---|
507 | # process_image becuase associated files aren't actually copied
|
---|
508 | # until after process has been run.
|
---|
509 | if (defined $self->{'tmp_filename1'} &&
|
---|
510 | -e $self->{'tmp_filename1'}) {
|
---|
511 | &util::rm($self->{'tmp_filename1'})
|
---|
512 | }
|
---|
513 | if (defined $self->{'tmp_filename2'} &&
|
---|
514 | -e $self->{'tmp_filename2'}) {
|
---|
515 | &util::rm($self->{'tmp_filename2'})
|
---|
516 | }
|
---|
517 | if (defined $self->{'tmp_filename3'} &&
|
---|
518 | -e $self->{'tmp_filename3'}) {
|
---|
519 | &util::rm($self->{'tmp_filename3'})
|
---|
520 | }
|
---|
521 |
|
---|
522 | $self->{'num_processed'}++;
|
---|
523 |
|
---|
524 | return 1;
|
---|
525 | }
|
---|
526 |
|
---|
527 | sub process_item {
|
---|
528 | my $self = shift (@_);
|
---|
529 | my ($filename, $dir, $file, $processor) = @_;
|
---|
530 |
|
---|
531 | my $doc_obj = new doc ($file, "indexed_doc");
|
---|
532 | my $topsection = $doc_obj->get_top_section();
|
---|
533 |
|
---|
534 | if ($self->{'doctype'} eq 'paged') {
|
---|
535 | # set the gsdlthistype metadata to Paged - this ensures this document will
|
---|
536 | # be treated as a Paged doc, even if Titles are not numeric
|
---|
537 |
|
---|
538 | $doc_obj->set_utf8_metadata_element ($topsection, "gsdlthistype", "Paged");
|
---|
539 | } else {
|
---|
540 | $doc_obj->set_utf8_metadata_element ($topsection, "gsdlthistype", "Hierarchy");
|
---|
541 | }
|
---|
542 | open (ITEMFILE, $filename) || die "couldn't open $filename\n";
|
---|
543 | my $line = "";
|
---|
544 | my $num = 0;
|
---|
545 | my $first = 1;
|
---|
546 | while (defined ($line = <ITEMFILE>)) {
|
---|
547 | next unless $line =~ /\w/;
|
---|
548 | chomp $line;
|
---|
549 | if ($line =~ /^<([^>]*)>(.*?)\s*$/) {
|
---|
550 | $doc_obj->set_utf8_metadata_element ($topsection, $1, $2);
|
---|
551 | $meta->{$1} = $2;
|
---|
552 | } else {
|
---|
553 | $num++;
|
---|
554 | # line should be like page:imagefilename:textfilename:r - the r is optional -> means rotate the image 180 deg
|
---|
555 | $line =~ s/^\s+//; #remove space at the front
|
---|
556 | $line =~ s/\s+$//; #remove space at the end
|
---|
557 | my ($pagenum, $imgname, $txtname, $rotate) = split /:/, $line;
|
---|
558 |
|
---|
559 | # create a new section for each image file
|
---|
560 | # the first image may go in the top section or in the first child section (if headerpage is true)
|
---|
561 | my $cursection;
|
---|
562 | if ($first) {
|
---|
563 | if ( $self->{'noheaderpage'}) {
|
---|
564 | $cursection = $topsection;
|
---|
565 | } else {
|
---|
566 | $cursection = $doc_obj->insert_section($doc_obj->get_end_child($topsection));
|
---|
567 | }
|
---|
568 | $first=0;
|
---|
569 | } else {
|
---|
570 | $cursection = $doc_obj->insert_section($doc_obj->get_end_child($topsection));
|
---|
571 | }
|
---|
572 | # the page number becomes the Title
|
---|
573 | $doc_obj->set_utf8_metadata_element($cursection, 'Title', $pagenum);
|
---|
574 | # process the image for this page
|
---|
575 | my $result = $self->process_image($dir.$imgname, $imgname, $doc_obj, $cursection, $rotate);
|
---|
576 |
|
---|
577 | if (!defined $result)
|
---|
578 | {
|
---|
579 | print "PagedImgPlug: couldn't process image \"$dir.$imgname\" for item \"$filename\"\n";
|
---|
580 | }
|
---|
581 |
|
---|
582 | # process the text file if one is there
|
---|
583 | if (defined $txtname && $txtname ne "") {
|
---|
584 | $result = undef;
|
---|
585 | $result = $self->process_text ($dir.$txtname, $txtname, $doc_obj, $cursection);
|
---|
586 | if (!defined $result) {
|
---|
587 | print "PagedImgPlug: couldn't process text file \"$dir.$txtname\" for item \"$filename\"\n";
|
---|
588 | }
|
---|
589 | } else {
|
---|
590 | # otherwise add in some dummy text
|
---|
591 | $doc_obj->add_text($cursection, "Dummy text to sidestep display bug");
|
---|
592 | }
|
---|
593 | }
|
---|
594 | }
|
---|
595 |
|
---|
596 | $file =~ s/\.item//i;
|
---|
597 | #use the name of the .item file as the OID
|
---|
598 | $doc_obj->set_OID ($file);
|
---|
599 | # add numpages metadata
|
---|
600 | $doc_obj->set_utf8_metadata_element ($topsection, 'NumPages', "$num");
|
---|
601 | return $doc_obj;
|
---|
602 | }
|
---|
603 |
|
---|
604 | sub process_text {
|
---|
605 | my $self = shift (@_);
|
---|
606 | my ($fullpath, $file, $doc_obj, $cursection) = @_;
|
---|
607 |
|
---|
608 | # Do encoding stuff
|
---|
609 | my ($language, $encoding) = $self->textcat_get_language_encoding ($fullpath);
|
---|
610 |
|
---|
611 | my $text="";
|
---|
612 | &BasPlug::read_file($self, $fullpath, $encoding, $language, \$text);
|
---|
613 | if (!length ($text)) {
|
---|
614 | my $plugin_name = ref ($self);
|
---|
615 | print "PagedImgPlug: ERROR: $fullpath contains no text\n" if $self->{'verbosity'};
|
---|
616 | return 0;
|
---|
617 | }
|
---|
618 |
|
---|
619 | # we need to escape the escape character, or else mg will convert into
|
---|
620 | # eg literal newlines, instead of leaving the text as '\n'
|
---|
621 | $text =~ s/\\/\\\\/g; # macro language
|
---|
622 | $text =~ s/_/\\_/g; # macro language
|
---|
623 | $text =~ s/</</g;
|
---|
624 | $text =~ s/>/>/g;
|
---|
625 |
|
---|
626 | # insert preformat tags and add text to document object
|
---|
627 | $doc_obj->add_utf8_text($cursection, "<pre>\n$text\n</pre>");
|
---|
628 |
|
---|
629 | return 1;
|
---|
630 | }
|
---|
631 |
|
---|
632 | # do plugin specific processing of doc_obj
|
---|
633 | sub process {
|
---|
634 | my $self = shift (@_);
|
---|
635 | my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
|
---|
636 | my $outhandle = $self->{'outhandle'};
|
---|
637 |
|
---|
638 | return 1;
|
---|
639 | }
|
---|
640 |
|
---|
641 | 1;
|
---|
642 |
|
---|
643 |
|
---|
644 |
|
---|
645 |
|
---|
646 |
|
---|
647 |
|
---|
648 |
|
---|
649 |
|
---|
650 |
|
---|
651 |
|
---|
652 |
|
---|