source: trunk/gsdl/perllib/plugins/PagedImgPlug.pm@ 8909

Last change on this file since 8909 was 8909, checked in by davidb, 19 years ago

PageImgPlug updated so read function follows more consistently the elements of
BasPlug read.

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