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

Last change on this file since 25961 was 25961, checked in by kjdon, 12 years ago

more cunning document types. gs3 has a new one, pagedhierarchy - for documents with internal structure and sequences of pages. new documenttype option, auto, will select the most appropriate doc type for each document. paged/hierarchy for gs2, paged/pagedhierarchy for gs3. documenttype option is displayed differently for gs2/3 modes.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 28.3 KB
Line 
1###########################################################################
2#
3# PagedImagePlugin.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# PagedImagePlugin
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>
73#<Page pagenum="1" imgfile="xxx.jpg" txtfile="yyy.txt">
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#
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'
117# option. As usual, running
118# 'perl -S pluginfo.pl PagedImagePlugin' will list all the options.
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
124# If you have used -create_screenview, you can also use [screenicon] in the format
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
134package PagedImagePlugin;
135
136use Encode;
137use ReadXMLFile;
138use ReadTextFile;
139use ImageConverter;
140use MetadataRead;
141
142use strict;
143no strict 'refs'; # allow filehandles to be variables and viceversa
144
145sub BEGIN {
146 @PagedImagePlugin::ISA = ('MetadataRead', 'ReadXMLFile', 'ReadTextFile', 'ImageConverter');
147}
148
149my $gs2_type_list =
150 [ { 'name' => "auto",
151 'desc' => "{PagedImagePlugin.documenttype.auto2}" },
152 { 'name' => "paged",
153 'desc' => "{PagedImagePlugin.documenttype.paged2}" },
154 { 'name' => "hierarchy",
155 'desc' => "{PagedImagePlugin.documenttype.hierarchy}" }
156 ];
157
158my $gs3_type_list =
159 [ { 'name' => "auto",
160 'desc' => "{PagedImagePlugin.documenttype.auto3}" },
161 { 'name' => "paged",
162 'desc' => "{PagedImagePlugin.documenttype.paged3}" },
163 { 'name' => "hierarchy",
164 'desc' => "{PagedImagePlugin.documenttype.hierarchy}" },
165 { 'name' => "pagedhierarchy",
166 'desc' => "{PagedImagePlugin.documenttype.pagedhierarchy}" }
167 ];
168
169my $arguments =
170 [ { 'name' => "process_exp",
171 'desc' => "{BasePlugin.process_exp}",
172 'type' => "string",
173 'deft' => &get_default_process_exp(),
174 'reqd' => "no" },
175 { 'name' => "title_sub",
176 'desc' => "{HTMLPlugin.title_sub}",
177 'type' => "string",
178 'deft' => "" },
179 { 'name' => "headerpage",
180 'desc' => "{PagedImagePlugin.headerpage}",
181 'type' => "flag",
182 'reqd' => "no" },
183# { 'name' => "documenttype",
184# 'desc' => "{PagedImagePlugin.documenttype}",
185# 'type' => "enum",
186# 'list' => $type_list,
187# 'deft' => "auto",
188# 'reqd' => "no" },
189 {'name' => "processing_tmp_files",
190 'desc' => "{BasePlugin.processing_tmp_files}",
191 'type' => "flag",
192 'hiddengli' => "yes"}
193 ];
194
195my $doc_type_opt = { 'name' => "documenttype",
196 'desc' => "{PagedImagePlugin.documenttype}",
197 'type' => "enum",
198 'deft' => "auto",
199 'reqd' => "no" };
200
201my $options = { 'name' => "PagedImagePlugin",
202 'desc' => "{PagedImagePlugin.desc}",
203 'abstract' => "no",
204 'inherits' => "yes",
205 'args' => $arguments };
206
207sub new {
208 my ($class) = shift (@_);
209 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
210 push(@$pluginlist, $class);
211
212 push(@{$hashArgOptLists->{"OptList"}},$options);
213
214 my $imc_self = new ImageConverter($pluginlist, $inputargs, $hashArgOptLists);
215
216 # we can use this plugin to check gs3 version
217 if ($imc_self->{'gs_version'} eq "3") {
218 $doc_type_opt->{'list'} = $gs3_type_list;
219 }
220 else {
221 $doc_type_opt->{'list'} = $gs2_type_list;
222 }
223 push(@$arguments,$doc_type_opt);
224 # now we add the args to the list for parsing
225 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
226
227 my $rtf_self = new ReadTextFile($pluginlist, $inputargs, $hashArgOptLists, 1);
228 my $rxf_self = new ReadXMLFile($pluginlist, $inputargs, $hashArgOptLists);
229
230 my $self = BasePlugin::merge_inheritance($imc_self,$rtf_self,$rxf_self);
231
232 # Update $self used by XML::Parser so it finds callback functions
233 # such as start_document here and not in ReadXMLFile (which is what
234 # $self was when new XML::Parser was done)
235 #
236 # If the $self returned by this constructor is the same as the one
237 # used in ReadXMLFile (e.g. in the GreenstoneXMLPlugin) then this step isn't necessary
238 #
239 # Consider embedding this type of assignment into merge_inheritance
240 # to help catch all cases?
241
242 $rxf_self->{'parser'}->{'PluginObj'} = $self;
243
244 print STDERR "doc type = $self->{'documenttype'}\n";
245 return bless $self, $class;
246}
247
248
249sub init {
250 my $self = shift (@_);
251 my ($verbosity, $outhandle, $failhandle) = @_;
252
253 $self->SUPER::init(@_);
254 $self->ImageConverter::init();
255}
256
257sub begin {
258 my $self = shift (@_);
259 my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
260
261 $self->SUPER::begin(@_);
262 $self->ImageConverter::begin(@_);
263}
264
265sub get_default_process_exp {
266 my $self = shift (@_);
267
268 return q^\.item$^;
269}
270
271sub get_doctype {
272 my $self = shift(@_);
273
274 return "PagedDocument";
275}
276
277
278# want to use BasePlugin's version of this, not ReadXMLFile's
279sub can_process_this_file {
280 my $self = shift(@_);
281 return $self->BasePlugin::can_process_this_file(@_);
282}
283
284# instead of a block exp, now we scan the file and record all text and img files mentioned there for blocking.
285sub store_block_files
286{
287 my $self = shift (@_);
288 my ($filename_full_path, $block_hash) = @_;
289
290 my $xml_version = $self->is_xml_item_file($filename_full_path);
291
292 # do we need to do this?
293 # does BOM interfere just with XML parsing? In that case don't need it here
294 # if we do it here, we are modifying the file before we have worked out if
295 # its new or not, so it will always be reimported.
296 #$self->tidy_item_file($filename_full_path);
297
298 my ($dir, $file) = $filename_full_path =~ /^(.*?)([^\/\\]*)$/;
299 if ($xml_version) {
300
301 # do something
302 $self->scan_xml_for_files_to_block($filename_full_path, $dir, $block_hash);
303 } else {
304
305 $self->scan_item_for_files_to_block($filename_full_path, $dir, $block_hash);
306 }
307
308}
309
310# we want to use BasePlugin's read, not ReadXMLFile's
311sub read
312{
313 my $self = shift (@_);
314
315 $self->BasePlugin::read(@_);
316}
317
318
319
320sub read_into_doc_obj {
321 my $self = shift (@_);
322 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
323 my $outhandle = $self->{'outhandle'};
324 my $verbosity = $self->{'verbosity'};
325
326 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
327
328 print $outhandle "PagedImagePlugin processing \"$filename_full_path\"\n"
329 if $verbosity > 1;
330 print STDERR "<Processing n='$file' p='PagedImagePlugin'>\n" if ($gli);
331
332 $self->{'MaxImageWidth'} = 0;
333 $self->{'MaxImageHeight'} = 0;
334
335 # here we need to decide if we have an old text .item file, or a new xml
336 # .item file
337 my $xml_version = $self->is_xml_item_file($filename_full_path);
338
339 $self->tidy_item_file($filename_full_path);
340
341 my $doc_obj;
342 if ($xml_version) {
343 # careful checking needed here!! are we using local xml handlers or super ones
344 $self->ReadXMLFile::read($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli);
345 $doc_obj = $self->{'doc_obj'};
346 } else {
347 my ($dir);
348 ($dir, $file) = $filename_full_path =~ /^(.*?)([^\/\\]*)$/;
349
350 #process the .item file
351 $doc_obj = $self->process_item($filename_full_path, $dir, $file, $processor, $metadata);
352
353 }
354
355 my $section = $doc_obj->get_top_section();
356
357 $doc_obj->add_utf8_metadata($section, "Plugin", "$self->{'plugin_type'}");
358 $doc_obj->add_metadata($section, "FileFormat", "PagedImage");
359
360 # include any metadata passed in from previous plugins
361 # note that this metadata is associated with the top level section
362 $self->add_associated_files($doc_obj, $filename_full_path);
363 $self->extra_metadata ($doc_obj, $section, $metadata);
364 $self->auto_extract_metadata ($doc_obj);
365
366 # if we haven't found any Title so far, assign one
367 $self->title_fallback($doc_obj,$section,$filename_no_path);
368
369 $self->add_OID($doc_obj);
370 return (1,$doc_obj);
371}
372
373# for now, the test is if the first non-empty line is <PagedDocument>, then its xml
374sub is_xml_item_file {
375 my $self = shift(@_);
376 my ($filename) = @_;
377
378 my $xml_version = 0;
379 open (ITEMFILE, $filename) || die "couldn't open $filename\n";
380
381 my $line = "";
382 my $num = 0;
383
384 $line = <ITEMFILE>;
385 while (defined ($line) && ($line !~ /\w/)) {
386 $line = <ITEMFILE>;
387 }
388
389 if (defined $line) {
390 chomp $line;
391 if ($line =~ /<PagedDocument/) {
392 $xml_version = 1;
393 }
394 }
395
396 close ITEMFILE;
397 return $xml_version;
398}
399
400sub tidy_item_file {
401 my $self = shift(@_);
402 my ($filename) = @_;
403
404 open (ITEMFILE, $filename) || die "couldn't open $filename\n";
405 my $backup_filename = "backup.item";
406 open (BACKUP,">$backup_filename")|| die "couldn't write to $backup_filename\n";
407 my $line = "";
408 $line = <ITEMFILE>;
409 $line =~ s/^\xEF\xBB\xBF//; # strip BOM
410 $line =~ s/\x0B+//ig;
411 $line =~ s/&/&amp;/g;
412 print BACKUP ($line);
413 #Tidy up the item file some metadata title contains \vt-vertical tab
414 while ($line = <ITEMFILE>) {
415 $line =~ s/\x0B+//ig;
416 $line =~ s/&/&amp;/g;
417 print BACKUP ($line);
418 }
419 close ITEMFILE;
420 close BACKUP;
421 &File::Copy::copy ($backup_filename, $filename);
422 &util::rm($backup_filename);
423
424}
425
426sub rotate_image {
427 my $self = shift (@_);
428 my ($filename_full_path) = @_;
429
430 my ($this_filetype) = $filename_full_path =~ /\.([^\.]*)$/;
431 my $result = $self->convert($filename_full_path, $this_filetype, "-rotate 180", "ROTATE");
432 my ($new_filename) = ($result =~ /=>(.*\.$this_filetype)/);
433 if (-e "$new_filename") {
434 return $new_filename;
435 }
436 # somethings gone wrong
437 return $filename_full_path;
438
439}
440
441sub process_image {
442 my $self = shift(@_);
443 my ($filename_full_path, $filename_no_path, $doc_obj, $section, $rotate) = @_;
444 # check the filenames
445 return 0 if ($filename_no_path eq "" || !-f $filename_full_path);
446
447 # remember that this image file was one of our source files, but only
448 # if we are not processing a tmp file
449 if (!$self->{'processing_tmp_files'} ) {
450 $doc_obj->associate_source_file($filename_full_path);
451 }
452 # do rotation
453 if ((defined $rotate) && ($rotate eq "r")) {
454 # we get a new temporary file which is rotated
455 $filename_full_path = $self->rotate_image($filename_full_path);
456 }
457
458 # do generate images
459 my $result = 0;
460 if ($self->{'image_conversion_available'} == 1) {
461 # do we need to convert $filename_no_path to utf8/url encoded?
462 # We are already reading in from a file, what encoding is it in???
463 my $url_encoded_full_filename
464 = &unicode::raw_filename_to_url_encoded($filename_full_path);
465 $result = $self->generate_images($filename_full_path, $url_encoded_full_filename, $doc_obj, $section);
466 }
467 #overwrite one set in ImageConverter
468 $doc_obj->set_metadata_element ($section, "FileFormat", "PagedImage");
469 return $result;
470}
471
472
473sub xml_start_tag {
474 my $self = shift(@_);
475 my ($expat, $element) = @_;
476 $self->{'element'} = $element;
477
478 my $doc_obj = $self->{'doc_obj'};
479 if ($element eq "PagedDocument") {
480 $self->{'current_section'} = $doc_obj->get_top_section();
481 } elsif ($element eq "PageGroup" || $element eq "Page") {
482 if ($element eq "PageGroup") {
483 $self->{'has_internal_structure'} = 1;
484 }
485 # create a new section as a child
486 $self->{'current_section'} = $doc_obj->insert_section($doc_obj->get_end_child($self->{'current_section'}));
487 $self->{'num_pages'}++;
488 # assign pagenum as what??
489 my $pagenum = $_{'pagenum'}; #TODO!!
490 if (defined $pagenum) {
491 $doc_obj->set_utf8_metadata_element($self->{'current_section'}, 'PageNum', $pagenum);
492 }
493 my ($imgfile) = $_{'imgfile'};
494 if (defined $imgfile) {
495 # *****
496 # What about support for rotate image (e.g. old ':r' notation)?
497 $self->process_image($self->{'xml_file_dir'}.$imgfile, $imgfile, $doc_obj, $self->{'current_section'});
498 }
499 my ($txtfile) = $_{'txtfile'};
500 if (defined($txtfile)&& $txtfile ne "") {
501 $self->process_text ($self->{'xml_file_dir'}.$txtfile, $txtfile, $doc_obj, $self->{'current_section'});
502 } else {
503 $self->add_dummy_text($doc_obj, $self->{'current_section'});
504 }
505 } elsif ($element eq "Metadata") {
506 $self->{'metadata_name'} = $_{'name'};
507 }
508}
509
510sub xml_end_tag {
511 my $self = shift(@_);
512 my ($expat, $element) = @_;
513
514 my $doc_obj = $self->{'doc_obj'};
515 if ($element eq "Page" || $element eq "PageGroup") {
516 # if Title hasn't been assigned, set PageNum as Title
517 if (!defined $doc_obj->get_metadata_element ($self->{'current_section'}, "Title") && defined $doc_obj->get_metadata_element ($self->{'current_section'}, "PageNum" )) {
518 $doc_obj->add_utf8_metadata ($self->{'current_section'}, "Title", $doc_obj->get_metadata_element ($self->{'current_section'}, "PageNum" ));
519 }
520 # move the current section back to the parent
521 $self->{'current_section'} = $doc_obj->get_parent_section($self->{'current_section'});
522 } elsif ($element eq "Metadata") {
523
524 # text read in by XML::Parser is in Perl's binary byte value
525 # form ... need to explicitly make it UTF-8
526 my $meta_name = decode("utf-8",$self->{'metadata_name'});
527 my $metadata_value = decode("utf-8",$self->{'metadata_value'});
528
529 if ($meta_name =~ /\./) {
530 $meta_name = "ex.$meta_name";
531 }
532
533 $doc_obj->add_utf8_metadata ($self->{'current_section'}, $meta_name, $metadata_value);
534 $self->{'metadata_name'} = "";
535 $self->{'metadata_value'} = "";
536
537 }
538 # otherwise we ignore the end tag
539}
540
541
542sub xml_text {
543 my $self = shift(@_);
544 my ($expat) = @_;
545
546 if ($self->{'element'} eq "Metadata" && $self->{'metadata_name'}) {
547 $self->{'metadata_value'} .= $_;
548 }
549}
550
551sub xml_doctype {
552}
553
554sub open_document {
555 my $self = shift(@_);
556
557 # create a new document
558 $self->{'doc_obj'} = new doc ($self->{'filename'}, "indexed_doc", $self->{'file_rename_method'});
559 # TODO is file filenmae_no_path??
560 $self->set_initial_doc_fields($self->{'doc_obj'}, $self->{'filename'}, $self->{'processor'}, $self->{'metadata'});
561
562 my ($dir, $file) = $self->{'filename'} =~ /^(.*?)([^\/\\]*)$/;
563 $self->{'xml_file_dir'} = $dir;
564 $self->{'num_pages'} = 0;
565 $self->{'has_internal_structure'} = 0;
566
567}
568
569sub close_document {
570 my $self = shift(@_);
571 my $doc_obj = $self->{'doc_obj'};
572
573 my $topsection = $doc_obj->get_top_section();
574
575 # add numpages metadata
576 $doc_obj->set_utf8_metadata_element ($topsection, 'NumPages', $self->{'num_pages'});
577
578 # set the document type
579 print STDERR "close doc, doc type = $self->{'documenttype'}\n";
580 my $final_doc_type = "";
581 if ($self->{'documenttype'} eq "auto") {
582 if ($self->{'has_internal_structure'}) {
583 if ($self->{'gs_version'} eq "3") {
584 $final_doc_type = "pagedhierarchy";
585 }
586 else {
587 $final_doc_type = "hierarchy";
588 }
589 } else {
590 $final_doc_type = "paged";
591 }
592 } else {
593 # set to what doc type option was set to
594 $final_doc_type = $self->{'documenttype'};
595 }
596 $doc_obj->set_utf8_metadata_element ($topsection, "gsdlthistype", $final_doc_type);
597 ### capiatalisation????
598# if ($self->{'documenttype'} eq 'paged') {
599 # set the gsdlthistype metadata to Paged - this ensures this document will
600 # be treated as a Paged doc, even if Titles are not numeric
601# $doc_obj->set_utf8_metadata_element ($topsection, "gsdlthistype", "Paged");
602# } else {
603# $doc_obj->set_utf8_metadata_element ($topsection, "gsdlthistype", "Hierarchy");
604# }
605
606 $doc_obj->set_utf8_metadata_element($topsection,"MaxImageWidth",$self->{'MaxImageWidth'});
607 $doc_obj->set_utf8_metadata_element($topsection,"MaxImageHeight",$self->{'MaxImageHeight'});
608 $self->{'MaxImageWidth'} = undef;
609 $self->{'MaxImageHeight'} = undef;
610
611}
612
613
614sub set_initial_doc_fields {
615 my $self = shift(@_);
616 my ($doc_obj, $filename_full_path, $processor, $metadata) = @_;
617
618 my $topsection = $doc_obj->get_top_section();
619
620 my $plugin_filename_encoding = $self->{'filename_encoding'};
621 my $filename_encoding = $self->deduce_filename_encoding($filename_full_path,$metadata,$plugin_filename_encoding);
622 $self->set_Source_metadata($doc_obj, $filename_full_path, $filename_encoding);
623
624 # if we want a header page, we need to add some text into the top section, otherwise this section will become invisible
625 if ($self->{'headerpage'}) {
626 $self->add_dummy_text($doc_obj, $topsection);
627 }
628}
629
630sub scan_xml_for_files_to_block
631{
632 my $self = shift (@_);
633 my ($filename_full_path, $dir, $block_hash) = @_;
634
635 open (ITEMFILE, $filename_full_path) || die "couldn't open $filename_full_path to work out which files to block\n";
636 my $line = "";
637 while (defined ($line = <ITEMFILE>)) {
638 next unless $line =~ /\w/;
639
640 if ($line =~ /imgfile=\"([^\"]+)\"/) {
641 &util::block_filename($block_hash,&util::filename_cat($dir,$1));
642 }
643 if ($line =~ /txtfile=\"([^\"]+)\"/) {
644 &util::block_filename($block_hash,&util::filename_cat($dir,$1));
645 }
646 }
647 close ITEMFILE;
648
649}
650
651sub scan_item_for_files_to_block
652{
653 my $self = shift (@_);
654 my ($filename_full_path, $dir, $block_hash) = @_;
655
656 open (ITEMFILE, $filename_full_path) || die "couldn't open $filename_full_path to work out which files to block\n";
657 my $line = "";
658 while (defined ($line = <ITEMFILE>)) {
659 next unless $line =~ /\w/;
660 chomp $line;
661 next if $line =~ /^#/; # ignore comment lines
662 next if ($line =~ /^<([^>]*)>\s*(.*?)\s*$/); # ignore metadata lines
663 # line should be like page:imagefilename:textfilename:r
664 $line =~ s/^\s+//; #remove space at the front
665 $line =~ s/\s+$//; #remove space at the end
666 my ($pagenum, $imgname, $txtname, $rotate) = split /:/, $line;
667
668 # find the image file if there is one
669 if (defined $imgname && $imgname ne "") {
670 &util::block_filename($block_hash, &util::filename_cat( $dir,$imgname));
671 }
672 # find the text file if there is one
673 if (defined $txtname && $txtname ne "") {
674 &util::block_filename($block_hash, &util::filename_cat($dir,$txtname));
675 }
676 }
677 close ITEMFILE;
678
679}
680
681sub process_item {
682 my $self = shift (@_);
683 my ($filename_full_path, $dir, $filename_no_path, $processor, $metadata) = @_;
684
685 my $doc_obj = new doc ($filename_full_path, "indexed_doc", $self->{'file_rename_method'});
686 $self->set_initial_doc_fields($doc_obj, $filename_full_path, $processor, $metadata);
687 my $topsection = $doc_obj->get_top_section();
688 # simple item files are always paged unless user specified
689 if ($self->{'documenttype'} eq "auto") {
690 $doc_obj->set_utf8_metadata_element ($topsection, "gsdlthistype", "paged");
691 } else {
692 $doc_obj->set_utf8_metadata_element ($topsection, "gsdlthistype", $self->{'documenttype'});
693 }
694 open (ITEMFILE, $filename_full_path) || die "couldn't open $filename_full_path\n";
695 my $line = "";
696 my $num = 0;
697 while (defined ($line = <ITEMFILE>)) {
698
699 # Since process_item is called not on an XML item file, but a text item file
700 # don't decode into UTF8 the text that was read in, since it's already UTF-8
701 #$line = decode("utf-8",$line);
702
703 next unless $line =~ /\w/;
704 chomp $line;
705 next if $line =~ /^#/; # ignore comment lines
706 if ($line =~ /^<([^>]*)>\s*(.*?)\s*$/) {
707 my $meta_name = $1;
708 my $meta_value = $2;
709 if ($meta_name =~ /\./) {
710 $meta_name = "ex.$meta_name";
711 }
712 $doc_obj->set_utf8_metadata_element ($topsection, $meta_name, $meta_value);
713 #$meta->{$1} = $2;
714 } else {
715 $num++;
716 # line should be like page:imagefilename:textfilename:r - the r is optional -> means rotate the image 180 deg
717 $line =~ s/^\s+//; #remove space at the front
718 $line =~ s/\s+$//; #remove space at the end
719 my ($pagenum, $imgname, $txtname, $rotate) = split /:/, $line;
720
721 # create a new section for each image file
722 my $cursection = $doc_obj->insert_section($doc_obj->get_end_child($topsection));
723 # the page number becomes the Title
724 $doc_obj->set_utf8_metadata_element($cursection, 'Title', $pagenum);
725
726 # process the image for this page if there is one
727 if (defined $imgname && $imgname ne "") {
728 my $result1 = $self->process_image($dir.$imgname, $imgname, $doc_obj, $cursection, $rotate);
729 if (!defined $result1)
730 {
731 print "PagedImagePlugin: couldn't process image \"$dir$imgname\" for item \"$filename_full_path\"\n";
732 }
733 }
734 # process the text file if one is there
735 if (defined $txtname && $txtname ne "") {
736 my $result2 = $self->process_text ($dir.$txtname, $txtname, $doc_obj, $cursection);
737
738 if (!defined $result2) {
739 print "PagedImagePlugin: couldn't process text file \"$dir.$txtname\" for item \"$filename_full_path\"\n";
740 $self->add_dummy_text($doc_obj, $cursection);
741 }
742 } else {
743 # otherwise add in some dummy text
744 $self->add_dummy_text($doc_obj, $cursection);
745 }
746 }
747 }
748
749 close ITEMFILE;
750
751 # add numpages metadata
752 $doc_obj->set_utf8_metadata_element ($topsection, 'NumPages', "$num");
753
754 $doc_obj->set_utf8_metadata_element($topsection,"MaxImageWidth",$self->{'MaxImageWidth'});
755 $doc_obj->set_utf8_metadata_element($topsection,"MaxImageHeight",$self->{'MaxImageHeight'});
756 $self->{'MaxImageWidth'} = undef;
757 $self->{'MaxImageHeight'} = undef;
758
759
760 return $doc_obj;
761}
762
763sub process_text {
764 my $self = shift (@_);
765 my ($filename_full_path, $file, $doc_obj, $cursection) = @_;
766
767 # check that the text file exists!!
768 if (!-f $filename_full_path) {
769 print "PagedImagePlugin: ERROR: File $filename_full_path does not exist, skipping\n";
770 return 0;
771 }
772
773 # remember that this text file was one of our source files, but only
774 # if we are not processing a tmp file
775 if (!$self->{'processing_tmp_files'} ) {
776 $doc_obj->associate_source_file($filename_full_path);
777 }
778 # Do encoding stuff
779 my ($language, $encoding) = $self->textcat_get_language_encoding ($filename_full_path);
780
781 my $text="";
782 &ReadTextFile::read_file($self, $filename_full_path, $encoding, $language, \$text); # already decoded as utf8
783 if (!length ($text)) {
784 # It's a bit unusual but not out of the question to have no text, so just give a warning
785 print "PagedImagePlugin: WARNING: $filename_full_path contains no text\n";
786 }
787
788 # we need to escape the escape character, or else mg will convert into
789 # eg literal newlines, instead of leaving the text as '\n'
790 $text =~ s/\\/\\\\/g; # macro language
791 $text =~ s/_/\\_/g; # macro language
792
793
794 if ($text =~ m/<html.*?>\s*<head.*?>.*<\/head>\s*<body.*?>(.*)<\/body>\s*<\/html>\s*$/is) {
795 # looks like HTML input
796 # no need to escape < and > or put in <pre> tags
797
798 $text = $1;
799
800 # add text to document object
801 $doc_obj->add_utf8_text($cursection, "$text");
802 }
803 else {
804 $text =~ s/</&lt;/g;
805 $text =~ s/>/&gt;/g;
806
807 # insert preformat tags and add text to document object
808 $doc_obj->add_utf8_text($cursection, "<pre>\n$text\n</pre>");
809 }
810
811
812 return 1;
813}
814
815
816sub clean_up_after_doc_obj_processing {
817 my $self = shift(@_);
818
819 $self->ImageConverter::clean_up_temporary_files();
820}
821
8221;
Note: See TracBrowser for help on using the repository browser.