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

Last change on this file since 35401 was 32760, checked in by kjdon, 5 years ago

merge_inheritance, if it finds a conflict in option values, will keep the first value it had seen. When calling a plugin constructor with a 1 as the last arg, this means 'auxiliary' and don't parse the user options. -cover_image option is set to 1 unless -no_cover_image is specified. this means that any auxiliary plugins will end up with -cover_image=1 - as they are not parsing args, and therefore don't see -no_cover_image. Only the rxf plugin will actually get the correct value for -cover_image. therefore it needs to come first in the list in merge_inheritance. I don't know if there are any other ramifications for this change of order in merge_inheritance. All I know is that I needed to change it to make the -no_cover_image option work.

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