source: main/trunk/greenstone2/perllib/plugins/PagedImagePlugin.pm@ 22814

Last change on this file since 22814 was 22814, checked in by kjdon, 14 years ago

removes tidy_item_file from store_block_files as it makes the file new again (reads it in and writes it out) before we have worked out what is now/old, and means that it will always be reindexed

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 25.3 KB
RevLine 
[14661]1###########################################################################
2#
[15872]3# PagedImagePlugin.pm -- plugin for sets of images and OCR text that
[14661]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
[15872]27# PagedImagePlugin
[14661]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#There are two formats for the item files: a plain text format, and an xml
37#format. You can use either format, and can have both formats in the same
38#collection if you like. If you use the plain format, you must not start the
39#file off with <PagedDocument>
40
41#### PLAIN FORMAT
42# The format of the xxx.item file is as follows:
43# The first lines contain any metadata for the whole document
44# <metadata-name>metadata-value
45# eg.
46# <Title>Snail farming
47# <Date>19230102
48# Then comes a list of pages, one page per line, each line has the format
49#
50# pagenum:imagefile:textfile:r
51#
52# page num and imagefile are required. pagenum is used for the Title
53# of the section, and in the display is shown as page <pagenum>.
54# imagefile is the image for the page. textfile is an optional text
55# file containing the OCR (or any) text for the page - this gets added
56# as the text for the section. r is optional, and signals that the image
57# should be rotated 180deg. Eg use this if the image has been made upside down.
58# So an example item file looks like:
59# <Title>Snail farming
60# <Date>19960403
61# 1:p1.gif:p1.txt:
62# 2:p2.gif::
63# 3:p3.gif:p3.txt:
64# 3b:p3b.gif:p3b.txt:r
65# The second page has no text, the fourth page is a back page, and
66# should be rotated.
67#
68
69#### XML FORMAT
70# The xml format looks like the following
71#<PagedDocument>
72#<Metadata name="Title">The Title of the entire document</Metadata>
[15018]73#<Page pagenum="1" imgfile="xxx.jpg" txtfile="yyy.txt">
[14661]74#<Metadata name="Title">The Title of this page</Metadata>
75#</Page>
76#... more pages
77#</PagedDocument>
78#PagedDocument contains a list of Pages, Metadata and PageGroups. Any metadata
79#that is not inside another tag will belong to the document.
80#Each Page has a pagenum (not used at the moment), an imgfile and/or a txtfile.
81#These are both optional - if neither is used, the section will have no content.
82#Pages can also have metadata associated with them.
83#PageGroups can be introduced at any point - they can contain Metadata and Pages and other PageGroups. They are used to introduce hierarchical structure into the document.
84#For example
85#<PagedDocument>
86#<PageGroup>
87#<Page>
88#<Page>
89#</PageGroup>
90#<Page>
91#</PagedDocument>
92#would generate a structure like
93#X
94#--X
95# --X
96# --X
97#--X
98#PageGroup tags can also have imgfile/textfile metadata if you like - this way they get some content themselves.
99
100#Currently the XML structure doesn't work very well with the paged document type, unless you use numerical Titles for each section.
101#There is still a bit of work to do on this format:
102#* enable other text file types, eg html, pdf etc
103#* make the document paging work properly
104#* add pagenum as Title unless a Title is present?
105
106# All the supplemetary image amd text files should be in the same folder as
107# the .item file.
108#
109# To display the images instead of the document text, you can use [srcicon]
110# in the DocumentText format statement.
111# For example,
112#
113# format DocumentText "<center><table width=_pagewidth_><tr><td>[srcicon]</td></tr></table></center>"
114#
[15905]115# To have it create thumbnail size images, use the '-create_thumbnail' option.
116# To have it create medium size images for display, use the '-create_screenview'
[14661]117# option. As usual, running
[15872]118# 'perl -S pluginfo.pl PagedImagePlugin' will list all the options.
[14661]119
120# If you want the resulting documents to be presented with a table of
121# contents, use '-documenttype hierarchy', otherwise they will have
122# next and previous arrows, and a goto page X box.
123
[15905]124# If you have used -create_screenview, you can also use [screenicon] in the format
[14661]125# statement to display the smaller image. Here is an example that switches
126# between the two:
127#
128# 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>"
129#
130# Additional metadata can be added into the .item files, alternatively you can
131# use normal metadata.xml files, with the name of the xxx.item file as the
132# FileName (only for document level metadata).
133
[15872]134package PagedImagePlugin;
[14661]135
[15872]136use ReadXMLFile;
137use ReadTextFile;
138use ImageConverter;
139
[14661]140use strict;
141no strict 'refs'; # allow filehandles to be variables and viceversa
142
143sub BEGIN {
[15872]144 @PagedImagePlugin::ISA = ('ReadXMLFile', 'ReadTextFile', 'ImageConverter');
[14661]145}
146
147my $type_list =
148 [ { 'name' => "paged",
[15872]149 'desc' => "{PagedImagePlugin.documenttype.paged}" },
[14661]150 { 'name' => "hierarchy",
[15872]151 'desc' => "{PagedImagePlugin.documenttype.hierarchy}" } ];
[14661]152
153my $arguments =
154 [ { 'name' => "process_exp",
[16009]155 'desc' => "{BasePlugin.process_exp}",
[14661]156 'type' => "string",
157 'deft' => &get_default_process_exp(),
158 'reqd' => "no" },
159 { 'name' => "title_sub",
[16009]160 'desc' => "{HTMLPlugin.title_sub}",
[14661]161 'type' => "string",
162 'deft' => "" },
163 { 'name' => "headerpage",
[15872]164 'desc' => "{PagedImagePlugin.headerpage}",
[14661]165 'type' => "flag",
166 'reqd' => "no" },
167 { 'name' => "documenttype",
[15872]168 'desc' => "{PagedImagePlugin.documenttype}",
[14661]169 'type' => "enum",
170 'list' => $type_list,
171 'deft' => "paged",
[20791]172 'reqd' => "no" },
173 {'name' => "processing_tmp_files",
174 'desc' => "{BasePlugin.processing_tmp_files}",
175 'type' => "flag",
176 'hiddengli' => "yes"}
177];
[14661]178
179
[15872]180my $options = { 'name' => "PagedImagePlugin",
181 'desc' => "{PagedImagePlugin.desc}",
[14661]182 'abstract' => "no",
183 'inherits' => "yes",
184 'args' => $arguments };
185
186sub new {
187 my ($class) = shift (@_);
188 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
189 push(@$pluginlist, $class);
190
[15872]191 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
192 push(@{$hashArgOptLists->{"OptList"}},$options);
[14661]193
[16827]194 my $imc_self = new ImageConverter($pluginlist, $inputargs, $hashArgOptLists);
195 my $rtf_self = new ReadTextFile($pluginlist, $inputargs, $hashArgOptLists, 1);
196 my $rxf_self = new ReadXMLFile($pluginlist, $inputargs, $hashArgOptLists);
197
198 my $self = BasePlugin::merge_inheritance($imc_self,$rtf_self,$rxf_self);
199
200 # Update $self used by XML::Parser so it finds callback functions
201 # such as start_document here and not in ReadXMLFile (which is what
202 # $self was when new XML::Parser was done)
203 #
204 # If the $self returned by this constructor is the same as the one
[17741]205 # used in ReadXMLFile (e.g. in the GreenstoneXMLPlugin) then this step isn't necessary
[16827]206 #
207 # Consider embedding this type of assignment into merge_inheritance
208 # to help catch all cases?
209
210 $rxf_self->{'parser'}->{'PluginObj'} = $self;
211
[14661]212 return bless $self, $class;
213}
214
[15872]215
216sub init {
217 my $self = shift (@_);
218 my ($verbosity, $outhandle, $failhandle) = @_;
219
220 $self->SUPER::init(@_);
221 $self->ImageConverter::init();
222}
223
[16847]224sub begin {
225 my $self = shift (@_);
226 my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
227
[16849]228 $self->SUPER::begin(@_);
[18468]229 $self->ImageConverter::begin(@_);
[16847]230}
231
[14661]232sub get_default_process_exp {
233 my $self = shift (@_);
234
235 return q^\.item$^;
236}
237
238sub get_doctype {
239 my $self = shift(@_);
240
241 return "PagedDocument";
242}
243
244
[17822]245# want to use BasePlugin's version of this, not ReadXMLFile's
246sub can_process_this_file {
247 my $self = shift(@_);
248 return $self->BasePlugin::can_process_this_file(@_);
249}
250
[22565]251# instead of a block exp, now we scan the file and record all text and img files mentioned there for blocking.
252sub store_block_files
253{
254 my $self = shift (@_);
255 my ($filename_full_path, $block_hash) = @_;
256
257 my $xml_version = $self->is_xml_item_file($filename_full_path);
258
[22814]259 # do we need to do this?
260 # does BOM interfere just with XML parsing? In that case don't need it here
261 # if we do it here, we are modifying the file before we have worked out if
262 # its new or not, so it will always be reimported.
263 #$self->tidy_item_file($filename_full_path);
[22565]264
265 my ($dir, $file) = $filename_full_path =~ /^(.*?)([^\/\\]*)$/;
266 if ($xml_version) {
267
268 # do something
269 $self->scan_xml_for_files_to_block($filename_full_path, $dir, $block_hash);
270 } else {
271
272 $self->scan_item_for_files_to_block($filename_full_path, $dir, $block_hash);
273 }
274
275}
276
[15914]277# we want to use BasePlugin's read, not ReadXMLFile's
278sub read
279{
[14661]280 my $self = shift (@_);
[16827]281
[15914]282 $self->BasePlugin::read(@_);
[15872]283}
[14661]284
285
[18468]286
[14661]287sub read_into_doc_obj {
288 my $self = shift (@_);
[16384]289 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
[15872]290 my $outhandle = $self->{'outhandle'};
[16827]291 my $verbosity = $self->{'verbosity'};
[14661]292
[16384]293 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
[15872]294
295 print $outhandle "PagedImagePlugin processing \"$filename_full_path\"\n"
[16827]296 if $verbosity > 1;
[15872]297 print STDERR "<Processing n='$file' p='PagedImagePlugin'>\n" if ($gli);
[14661]298
[19054]299 $self->{'MaxImageWidth'} = 0;
300 $self->{'MaxImageHeight'} = 0;
[15872]301
[14661]302 # here we need to decide if we have an old text .item file, or a new xml
[15872]303 # .item file
304 my $xml_version = $self->is_xml_item_file($filename_full_path);
305
[22814]306 $self->tidy_item_file($filename_full_path);
[15872]307
308 my $doc_obj;
309 if ($xml_version) {
310 # careful checking needed here!! are we using local xml handlers or super ones
[16384]311 $self->ReadXMLFile::read($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli);
[15872]312 $doc_obj = $self->{'doc_obj'};
313 } else {
314 my ($dir);
315 ($dir, $file) = $filename_full_path =~ /^(.*?)([^\/\\]*)$/;
316
317 #process the .item file
318 $doc_obj = $self->process_item($filename_full_path, $dir, $file, $processor);
319
320 }
321
322 my $section = $doc_obj->get_top_section();
323
324 $doc_obj->add_utf8_metadata($section, "Plugin", "$self->{'plugin_type'}");
325 $doc_obj->add_metadata($section, "FileFormat", "PagedImage");
326
327 # include any metadata passed in from previous plugins
328 # note that this metadata is associated with the top level section
329 $self->add_associated_files($doc_obj, $filename_full_path);
330 $self->extra_metadata ($doc_obj, $section, $metadata);
331 $self->auto_extract_metadata ($doc_obj);
332
333 # if we haven't found any Title so far, assign one
334 $self->title_fallback($doc_obj,$section,$filename_no_path);
335
336 $self->add_OID($doc_obj);
337 return (1,$doc_obj);
338}
339
340# for now, the test is if the first non-empty line is <PagedDocument>, then its xml
341sub is_xml_item_file {
342 my $self = shift(@_);
343 my ($filename) = @_;
344
[14661]345 my $xml_version = 0;
346 open (ITEMFILE, $filename) || die "couldn't open $filename\n";
[19999]347
[14661]348 my $line = "";
349 my $num = 0;
[19999]350
[14661]351 $line = <ITEMFILE>;
[19999]352 while (defined ($line) && ($line !~ /\w/)) {
[14661]353 $line = <ITEMFILE>;
354 }
[19999]355
356 if (defined $line) {
357 chomp $line;
358 if ($line =~ /<PagedDocument/) {
359 $xml_version = 1;
360 }
361 }
362
[15872]363 close ITEMFILE;
364 return $xml_version;
365}
366
367sub tidy_item_file {
368 my $self = shift(@_);
369 my ($filename) = @_;
370
[14661]371 open (ITEMFILE, $filename) || die "couldn't open $filename\n";
[15872]372 my $backup_filename = "backup.item";
373 open (BACKUP,">$backup_filename")|| die "couldn't write to $backup_filename\n";
374 my $line = "";
[14661]375 $line = <ITEMFILE>;
376 $line =~ s/^\xEF\xBB\xBF//; # strip BOM
377 $line =~ s/\x0B+//ig;
378 $line =~ s/&/&amp;/g;
379 print BACKUP ($line);
380 #Tidy up the item file some metadata title contains \vt-vertical tab
381 while ($line = <ITEMFILE>) {
382 $line =~ s/\x0B+//ig;
383 $line =~ s/&/&amp;/g;
384 print BACKUP ($line);
385 }
386 close ITEMFILE;
387 close BACKUP;
388 &File::Copy::copy ($backup_filename, $filename);
389 &util::rm($backup_filename);
390
391}
[15914]392
393sub rotate_image {
[14661]394 my $self = shift (@_);
[15914]395 my ($filename_full_path) = @_;
396
397 my ($this_filetype) = $filename_full_path =~ /\.([^\.]*)$/;
398 my $result = $self->convert($filename_full_path, $this_filetype, "-rotate 180", "ROTATE");
399 my ($new_filename) = ($result =~ /=>(.*\.$this_filetype)/);
400 if (-e "$new_filename") {
401 return $new_filename;
402 }
403 # somethings gone wrong
404 return $filename_full_path;
405
[14661]406}
407
[15914]408sub process_image {
409 my $self = shift(@_);
410 my ($filename_full_path, $filename_no_path, $doc_obj, $section, $rotate) = @_;
[20778]411 # check the filenames
412 return 0 if ($filename_no_path eq "" || !-f $filename_full_path);
413
[20791]414 # remember that this image file was one of our source files, but only
415 # if we are not processing a tmp file
416 if (!$self->{'processing_tmp_files'} ) {
417 $doc_obj->associate_source_file($filename_full_path);
418 }
[15914]419 # do rotation
[16827]420 if ((defined $rotate) && ($rotate eq "r")) {
[15914]421 # we get a new temporary file which is rotated
422 $filename_full_path = $self->rotate_image($filename_full_path);
423 }
424
425 # do generate images
[16009]426 my $result = 0;
427 if ($self->{'image_conversion_available'} == 1) {
[16384]428 # do we need to convert $filename_no_path to utf8? We are already reading in from a file, what encoding is it in???
[16009]429 $result = $self->generate_images($filename_full_path, $filename_no_path, $doc_obj, $section);
430 }
[15914]431 #overwrite one set in ImageConverter
432 $doc_obj->set_metadata_element ($section, "FileFormat", "PagedImage");
433 return $result;
434}
435
436
[14661]437sub xml_start_tag {
438 my $self = shift(@_);
439 my ($expat, $element) = @_;
440 $self->{'element'} = $element;
441
442 my $doc_obj = $self->{'doc_obj'};
443 if ($element eq "PagedDocument") {
444 $self->{'current_section'} = $doc_obj->get_top_section();
445 } elsif ($element eq "PageGroup" || $element eq "Page") {
446 # create a new section as a child
447 $self->{'current_section'} = $doc_obj->insert_section($doc_obj->get_end_child($self->{'current_section'}));
448 $self->{'num_pages'}++;
449 # assign pagenum as what??
450 my $pagenum = $_{'pagenum'}; #TODO!!
451 if (defined $pagenum) {
452 $doc_obj->set_utf8_metadata_element($self->{'current_section'}, 'PageNum', $pagenum);
453 }
454 my ($imgfile) = $_{'imgfile'};
455 if (defined $imgfile) {
[16827]456 # *****
457 # What about support for rotate image (e.g. old ':r' notation)?
458 $self->process_image($self->{'xml_file_dir'}.$imgfile, $imgfile, $doc_obj, $self->{'current_section'});
[14661]459 }
460 my ($txtfile) = $_{'txtfile'};
461 if (defined($txtfile)&& $txtfile ne "") {
[16827]462 $self->process_text ($self->{'xml_file_dir'}.$txtfile, $txtfile, $doc_obj, $self->{'current_section'});
[15872]463 } else {
464 $self->add_dummy_text($doc_obj, $self->{'current_section'});
[14661]465 }
466 } elsif ($element eq "Metadata") {
467 $self->{'metadata_name'} = $_{'name'};
468 }
469}
470
471sub xml_end_tag {
472 my $self = shift(@_);
473 my ($expat, $element) = @_;
474
475 my $doc_obj = $self->{'doc_obj'};
476 if ($element eq "Page" || $element eq "PageGroup") {
477 # if Title hasn't been assigned, set PageNum as Title
478 if (!defined $doc_obj->get_metadata_element ($self->{'current_section'}, "Title") && defined $doc_obj->get_metadata_element ($self->{'current_section'}, "PageNum" )) {
479 $doc_obj->add_utf8_metadata ($self->{'current_section'}, "Title", $doc_obj->get_metadata_element ($self->{'current_section'}, "PageNum" ));
480 }
481 # move the current section back to the parent
482 $self->{'current_section'} = $doc_obj->get_parent_section($self->{'current_section'});
483 } elsif ($element eq "Metadata") {
[22349]484 my $meta_name = $self->{'metadata_name'};
485 if ($meta_name =~ /\./) {
486 $meta_name = "ex.$meta_name";
487 }
488 $doc_obj->add_utf8_metadata ($self->{'current_section'}, $meta_name, $self->{'metadata_value'});
[14661]489 $self->{'metadata_name'} = "";
490 $self->{'metadata_value'} = "";
491
492 }
493 # otherwise we ignore the end tag
494}
495
496
497sub xml_text {
498 my $self = shift(@_);
499 my ($expat) = @_;
500
501 if ($self->{'element'} eq "Metadata" && $self->{'metadata_name'}) {
502 $self->{'metadata_value'} .= $_;
503 }
504}
505
506sub xml_doctype {
507}
508
509sub open_document {
510 my $self = shift(@_);
511
512 # create a new document
[18327]513 $self->{'doc_obj'} = new doc ($self->{'filename'}, "indexed_doc", $self->{'file_rename_method'});
[15872]514 # TODO is file filenmae_no_path??
515 $self->set_initial_doc_fields($self->{'doc_obj'}, $self->{'file'}, $self->{'processor'});
516
[14661]517 my ($dir, $file) = $self->{'filename'} =~ /^(.*?)([^\/\\]*)$/;
[16827]518 $self->{'xml_file_dir'} = $dir;
[14661]519 $self->{'num_pages'} = 0;
520
521}
522
523sub close_document {
524 my $self = shift(@_);
525 my $doc_obj = $self->{'doc_obj'};
526
527 # add numpages metadata
[19054]528 my $topsection = $doc_obj->get_top_section();
[14661]529
[19054]530 $doc_obj->set_utf8_metadata_element ($topsection, 'NumPages', $self->{'num_pages'});
531
532 $doc_obj->set_utf8_metadata_element($topsection,"MaxImageWidth",$self->{'MaxImageWidth'});
533 $doc_obj->set_utf8_metadata_element($topsection,"MaxImageHeight",$self->{'MaxImageHeight'});
534 $self->{'MaxImageWidth'} = undef;
535 $self->{'MaxImageHeight'} = undef;
[14661]536
537}
538
539
[15872]540sub set_initial_doc_fields {
541 my $self = shift(@_);
542 my ($doc_obj, $filename_no_path, $processor) = @_;
543
[14661]544 my $topsection = $doc_obj->get_top_section();
545
546 if ($self->{'documenttype'} eq 'paged') {
547 # set the gsdlthistype metadata to Paged - this ensures this document will
548 # be treated as a Paged doc, even if Titles are not numeric
549 $doc_obj->set_utf8_metadata_element ($topsection, "gsdlthistype", "Paged");
550 } else {
551 $doc_obj->set_utf8_metadata_element ($topsection, "gsdlthistype", "Hierarchy");
552 }
553
[15872]554 $self->set_Source_metadata($doc_obj, $filename_no_path);
555
556 # if we want a header page, we need to add some text into the top section, otherwise this section will become invisible
557 if ($self->{'headerpage'}) {
[15911]558 $self->add_dummy_text($doc_obj, $topsection);
[15872]559 }
[14661]560
[15872]561
562}
563
[22565]564sub scan_xml_for_files_to_block
565{
566 my $self = shift (@_);
567 my ($filename_full_path, $dir, $block_hash) = @_;
[15872]568
[22565]569 open (ITEMFILE, $filename_full_path) || die "couldn't open $filename_full_path to work out which files to block\n";
570 my $line = "";
571 while (defined ($line = <ITEMFILE>)) {
572 next unless $line =~ /\w/;
573
574 if ($line =~ /imgfile=\"([^\"]+)\"/) {
575 $block_hash->{'file_blocks'}->{$dir.$1} = 1;
576 }
577 if ($line =~ /txtfile=\"([^\"]+)\"/) {
578 $block_hash->{'file_blocks'}->{$dir.$1} = 1;
579 }
580 }
581 close ITEMFILE;
582
583}
584
585sub scan_item_for_files_to_block
586{
587 my $self = shift (@_);
588 my ($filename_full_path, $dir, $block_hash) = @_;
589
590 open (ITEMFILE, $filename_full_path) || die "couldn't open $filename_full_path to work out which files to block\n";
591 my $line = "";
592 while (defined ($line = <ITEMFILE>)) {
593 next unless $line =~ /\w/;
594 chomp $line;
595 next if $line =~ /^#/; # ignore comment lines
596 next if ($line =~ /^<([^>]*)>\s*(.*?)\s*$/); # ignore metadata lines
597 # line should be like page:imagefilename:textfilename:r
598 $line =~ s/^\s+//; #remove space at the front
599 $line =~ s/\s+$//; #remove space at the end
600 my ($pagenum, $imgname, $txtname, $rotate) = split /:/, $line;
601
602 # find the image file if there is one
603 if (defined $imgname && $imgname ne "") {
604 $block_hash->{'file_blocks'}->{$dir.$imgname}=1;
605 }
606 # find the text file if there is one
607 if (defined $txtname && $txtname ne "") {
608 $block_hash->{'file_blocks'}->{$dir.$txtname} = 1;
609 }
610 }
611 close ITEMFILE;
612
613}
614
[15872]615sub process_item {
616 my $self = shift (@_);
617 my ($filename_full_path, $dir, $filename_no_path, $processor) = @_;
618
[18327]619 my $doc_obj = new doc ($filename_full_path, "indexed_doc", $self->{'file_rename_method'});
[15872]620 $self->set_initial_doc_fields($doc_obj, $filename_no_path, $processor);
621 my $topsection = $doc_obj->get_top_section();
622 open (ITEMFILE, $filename_full_path) || die "couldn't open $filename_full_path\n";
[14661]623 my $line = "";
624 my $num = 0;
625 while (defined ($line = <ITEMFILE>)) {
626 next unless $line =~ /\w/;
627 chomp $line;
628 next if $line =~ /^#/; # ignore comment lines
629 if ($line =~ /^<([^>]*)>\s*(.*?)\s*$/) {
[22349]630 my $meta_name = $1;
631 my $meta_value = $2;
632 if ($meta_name =~ /\./) {
633 $meta_name = "ex.$meta_name";
634 }
635 $doc_obj->set_utf8_metadata_element ($topsection, $meta_name, $meta_value);
[14661]636 #$meta->{$1} = $2;
637 } else {
638 $num++;
639 # line should be like page:imagefilename:textfilename:r - the r is optional -> means rotate the image 180 deg
640 $line =~ s/^\s+//; #remove space at the front
641 $line =~ s/\s+$//; #remove space at the end
642 my ($pagenum, $imgname, $txtname, $rotate) = split /:/, $line;
643
644 # create a new section for each image file
645 my $cursection = $doc_obj->insert_section($doc_obj->get_end_child($topsection));
646 # the page number becomes the Title
647 $doc_obj->set_utf8_metadata_element($cursection, 'Title', $pagenum);
648
649 # process the image for this page if there is one
650 if (defined $imgname && $imgname ne "") {
651 my $result1 = $self->process_image($dir.$imgname, $imgname, $doc_obj, $cursection, $rotate);
652 if (!defined $result1)
653 {
[21721]654 print "PagedImagePlugin: couldn't process image \"$dir$imgname\" for item \"$filename_full_path\"\n";
[14661]655 }
656 }
657 # process the text file if one is there
658 if (defined $txtname && $txtname ne "") {
659 my $result2 = $self->process_text ($dir.$txtname, $txtname, $doc_obj, $cursection);
660
661 if (!defined $result2) {
[15872]662 print "PagedImagePlugin: couldn't process text file \"$dir.$txtname\" for item \"$filename_full_path\"\n";
663 $self->add_dummy_text($doc_obj, $cursection);
[14661]664 }
665 } else {
666 # otherwise add in some dummy text
[15872]667 $self->add_dummy_text($doc_obj, $cursection);
668 }
[14661]669 }
670 }
671
672 close ITEMFILE;
673
674 # add numpages metadata
675 $doc_obj->set_utf8_metadata_element ($topsection, 'NumPages', "$num");
[19054]676
677 $doc_obj->set_utf8_metadata_element($topsection,"MaxImageWidth",$self->{'MaxImageWidth'});
678 $doc_obj->set_utf8_metadata_element($topsection,"MaxImageHeight",$self->{'MaxImageHeight'});
679 $self->{'MaxImageWidth'} = undef;
680 $self->{'MaxImageHeight'} = undef;
681
682
[14661]683 return $doc_obj;
684}
685
686sub process_text {
687 my $self = shift (@_);
[15872]688 my ($filename_full_path, $file, $doc_obj, $cursection) = @_;
[14661]689
690 # check that the text file exists!!
[15872]691 if (!-f $filename_full_path) {
692 print "PagedImagePlugin: ERROR: File $filename_full_path does not exist, skipping\n";
[14661]693 return 0;
694 }
695
[20791]696 # remember that this text file was one of our source files, but only
697 # if we are not processing a tmp file
698 if (!$self->{'processing_tmp_files'} ) {
699 $doc_obj->associate_source_file($filename_full_path);
700 }
[14661]701 # Do encoding stuff
[15872]702 my ($language, $encoding) = $self->textcat_get_language_encoding ($filename_full_path);
[14661]703
704 my $text="";
[15872]705 &ReadTextFile::read_file($self, $filename_full_path, $encoding, $language, \$text);
[14661]706 if (!length ($text)) {
707 # It's a bit unusual but not out of the question to have no text, so just give a warning
[15872]708 print "PagedImagePlugin: WARNING: $filename_full_path contains no text\n";
[14661]709 }
710
711 # we need to escape the escape character, or else mg will convert into
712 # eg literal newlines, instead of leaving the text as '\n'
713 $text =~ s/\\/\\\\/g; # macro language
714 $text =~ s/_/\\_/g; # macro language
[15018]715
716
[16827]717 if ($text =~ m/<html.*?>\s*<head.*?>.*<\/head>\s*<body.*?>(.*)<\/body>\s*<\/html>\s*$/is) {
[15018]718 # looks like HTML input
719 # no need to escape < and > or put in <pre> tags
720
721 $text = $1;
722
[22565]723 # add text to document object
[15018]724 $doc_obj->add_utf8_text($cursection, "$text");
725 }
726 else {
727 $text =~ s/</&lt;/g;
728 $text =~ s/>/&gt;/g;
729
730 # insert preformat tags and add text to document object
731 $doc_obj->add_utf8_text($cursection, "<pre>\n$text\n</pre>");
732 }
733
[14661]734
735 return 1;
736}
737
738
[15872]739sub clean_up_after_doc_obj_processing {
740 my $self = shift(@_);
741
742 $self->ImageConverter::clean_up_temporary_files();
743}
744
[14661]7451;
Note: See TracBrowser for help on using the repository browser.