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 |
|
---|
134 | package PagedImagePlugin;
|
---|
135 |
|
---|
136 | use ReadXMLFile;
|
---|
137 | use ReadTextFile;
|
---|
138 | use ImageConverter;
|
---|
139 |
|
---|
140 | use strict;
|
---|
141 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
142 |
|
---|
143 | sub BEGIN {
|
---|
144 | @PagedImagePlugin::ISA = ('ReadXMLFile', 'ReadTextFile', 'ImageConverter');
|
---|
145 | }
|
---|
146 |
|
---|
147 | my $type_list =
|
---|
148 | [ { 'name' => "paged",
|
---|
149 | 'desc' => "{PagedImagePlugin.documenttype.paged}" },
|
---|
150 | { 'name' => "hierarchy",
|
---|
151 | 'desc' => "{PagedImagePlugin.documenttype.hierarchy}" } ];
|
---|
152 |
|
---|
153 | my $arguments =
|
---|
154 | [ { 'name' => "process_exp",
|
---|
155 | 'desc' => "{BasePlugin.process_exp}",
|
---|
156 | 'type' => "string",
|
---|
157 | 'deft' => &get_default_process_exp(),
|
---|
158 | 'reqd' => "no" },
|
---|
159 | { 'name' => "block_exp",
|
---|
160 | 'desc' => "{BasePlugin.block_exp}",
|
---|
161 | 'type' => "string",
|
---|
162 | 'deft' => &get_default_block_exp(),
|
---|
163 | 'reqd' => "no" },
|
---|
164 | { 'name' => "title_sub",
|
---|
165 | 'desc' => "{HTMLPlugin.title_sub}",
|
---|
166 | 'type' => "string",
|
---|
167 | 'deft' => "" },
|
---|
168 | { 'name' => "headerpage",
|
---|
169 | 'desc' => "{PagedImagePlugin.headerpage}",
|
---|
170 | 'type' => "flag",
|
---|
171 | 'reqd' => "no" },
|
---|
172 | { 'name' => "documenttype",
|
---|
173 | 'desc' => "{PagedImagePlugin.documenttype}",
|
---|
174 | 'type' => "enum",
|
---|
175 | 'list' => $type_list,
|
---|
176 | 'deft' => "paged",
|
---|
177 | 'reqd' => "no" },
|
---|
178 | {'name' => "processing_tmp_files",
|
---|
179 | 'desc' => "{BasePlugin.processing_tmp_files}",
|
---|
180 | 'type' => "flag",
|
---|
181 | 'hiddengli' => "yes"}
|
---|
182 | ];
|
---|
183 |
|
---|
184 |
|
---|
185 | my $options = { 'name' => "PagedImagePlugin",
|
---|
186 | 'desc' => "{PagedImagePlugin.desc}",
|
---|
187 | 'abstract' => "no",
|
---|
188 | 'inherits' => "yes",
|
---|
189 | 'args' => $arguments };
|
---|
190 |
|
---|
191 | sub new {
|
---|
192 | my ($class) = shift (@_);
|
---|
193 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
194 | push(@$pluginlist, $class);
|
---|
195 |
|
---|
196 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
197 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
198 |
|
---|
199 | my $imc_self = new ImageConverter($pluginlist, $inputargs, $hashArgOptLists);
|
---|
200 | my $rtf_self = new ReadTextFile($pluginlist, $inputargs, $hashArgOptLists, 1);
|
---|
201 | my $rxf_self = new ReadXMLFile($pluginlist, $inputargs, $hashArgOptLists);
|
---|
202 |
|
---|
203 | my $self = BasePlugin::merge_inheritance($imc_self,$rtf_self,$rxf_self);
|
---|
204 |
|
---|
205 | # Update $self used by XML::Parser so it finds callback functions
|
---|
206 | # such as start_document here and not in ReadXMLFile (which is what
|
---|
207 | # $self was when new XML::Parser was done)
|
---|
208 | #
|
---|
209 | # If the $self returned by this constructor is the same as the one
|
---|
210 | # used in ReadXMLFile (e.g. in the GreenstoneXMLPlugin) then this step isn't necessary
|
---|
211 | #
|
---|
212 | # Consider embedding this type of assignment into merge_inheritance
|
---|
213 | # to help catch all cases?
|
---|
214 |
|
---|
215 | $rxf_self->{'parser'}->{'PluginObj'} = $self;
|
---|
216 |
|
---|
217 | return bless $self, $class;
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 | sub init {
|
---|
222 | my $self = shift (@_);
|
---|
223 | my ($verbosity, $outhandle, $failhandle) = @_;
|
---|
224 |
|
---|
225 | $self->SUPER::init(@_);
|
---|
226 | $self->ImageConverter::init();
|
---|
227 | }
|
---|
228 |
|
---|
229 | sub begin {
|
---|
230 | my $self = shift (@_);
|
---|
231 | my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
|
---|
232 |
|
---|
233 | $self->SUPER::begin(@_);
|
---|
234 | $self->ImageConverter::begin(@_);
|
---|
235 | }
|
---|
236 |
|
---|
237 | sub get_default_process_exp {
|
---|
238 | my $self = shift (@_);
|
---|
239 |
|
---|
240 | return q^\.item$^;
|
---|
241 | }
|
---|
242 |
|
---|
243 | sub get_doctype {
|
---|
244 | my $self = shift(@_);
|
---|
245 |
|
---|
246 | return "PagedDocument";
|
---|
247 | }
|
---|
248 |
|
---|
249 |
|
---|
250 | # want to block everything except the .item ones
|
---|
251 | # but instead we will block images and txt files
|
---|
252 | sub get_default_block_exp {
|
---|
253 | my $self = shift (@_);
|
---|
254 |
|
---|
255 | return q^(?i)(\.jpe?g|\.gif|\.png|\.tif?f|\.te?xt|\.html?|~)$^
|
---|
256 | ### return q^(?i)(\.jpe?g|\.gif|\.png|\.tif?f|\.te?xt|\.html?|\.css|\.opd|\.pdf|~)$^
|
---|
257 | }
|
---|
258 |
|
---|
259 |
|
---|
260 | # want to use BasePlugin's version of this, not ReadXMLFile's
|
---|
261 | sub can_process_this_file {
|
---|
262 | my $self = shift(@_);
|
---|
263 |
|
---|
264 | return $self->BasePlugin::can_process_this_file(@_);
|
---|
265 | }
|
---|
266 |
|
---|
267 | # we want to use BasePlugin's read, not ReadXMLFile's
|
---|
268 | sub read
|
---|
269 | {
|
---|
270 | my $self = shift (@_);
|
---|
271 |
|
---|
272 | $self->BasePlugin::read(@_);
|
---|
273 | }
|
---|
274 |
|
---|
275 |
|
---|
276 |
|
---|
277 | sub read_into_doc_obj {
|
---|
278 | my $self = shift (@_);
|
---|
279 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
|
---|
280 | my $outhandle = $self->{'outhandle'};
|
---|
281 | my $verbosity = $self->{'verbosity'};
|
---|
282 |
|
---|
283 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
284 |
|
---|
285 | print $outhandle "PagedImagePlugin processing \"$filename_full_path\"\n"
|
---|
286 | if $verbosity > 1;
|
---|
287 | print STDERR "<Processing n='$file' p='PagedImagePlugin'>\n" if ($gli);
|
---|
288 |
|
---|
289 | $self->{'MaxImageWidth'} = 0;
|
---|
290 | $self->{'MaxImageHeight'} = 0;
|
---|
291 |
|
---|
292 | # here we need to decide if we have an old text .item file, or a new xml
|
---|
293 | # .item file
|
---|
294 | my $xml_version = $self->is_xml_item_file($filename_full_path);
|
---|
295 |
|
---|
296 | $self->tidy_item_file($filename_full_path);
|
---|
297 |
|
---|
298 | my $doc_obj;
|
---|
299 | if ($xml_version) {
|
---|
300 | # careful checking needed here!! are we using local xml handlers or super ones
|
---|
301 | $self->ReadXMLFile::read($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli);
|
---|
302 | $doc_obj = $self->{'doc_obj'};
|
---|
303 | } else {
|
---|
304 | my ($dir);
|
---|
305 | ($dir, $file) = $filename_full_path =~ /^(.*?)([^\/\\]*)$/;
|
---|
306 |
|
---|
307 | #process the .item file
|
---|
308 | $doc_obj = $self->process_item($filename_full_path, $dir, $file, $processor);
|
---|
309 |
|
---|
310 | }
|
---|
311 |
|
---|
312 | my $section = $doc_obj->get_top_section();
|
---|
313 |
|
---|
314 | $doc_obj->add_utf8_metadata($section, "Plugin", "$self->{'plugin_type'}");
|
---|
315 | $doc_obj->add_metadata($section, "FileFormat", "PagedImage");
|
---|
316 |
|
---|
317 | # include any metadata passed in from previous plugins
|
---|
318 | # note that this metadata is associated with the top level section
|
---|
319 | $self->add_associated_files($doc_obj, $filename_full_path);
|
---|
320 | $self->extra_metadata ($doc_obj, $section, $metadata);
|
---|
321 | $self->auto_extract_metadata ($doc_obj);
|
---|
322 |
|
---|
323 | # if we haven't found any Title so far, assign one
|
---|
324 | $self->title_fallback($doc_obj,$section,$filename_no_path);
|
---|
325 |
|
---|
326 | $self->add_OID($doc_obj);
|
---|
327 | return (1,$doc_obj);
|
---|
328 | }
|
---|
329 |
|
---|
330 | # for now, the test is if the first non-empty line is <PagedDocument>, then its xml
|
---|
331 | sub is_xml_item_file {
|
---|
332 | my $self = shift(@_);
|
---|
333 | my ($filename) = @_;
|
---|
334 |
|
---|
335 | my $xml_version = 0;
|
---|
336 | open (ITEMFILE, $filename) || die "couldn't open $filename\n";
|
---|
337 |
|
---|
338 | my $line = "";
|
---|
339 | my $num = 0;
|
---|
340 |
|
---|
341 | $line = <ITEMFILE>;
|
---|
342 | while (defined ($line) && ($line !~ /\w/)) {
|
---|
343 | $line = <ITEMFILE>;
|
---|
344 | }
|
---|
345 |
|
---|
346 | if (defined $line) {
|
---|
347 | chomp $line;
|
---|
348 | if ($line =~ /<PagedDocument/) {
|
---|
349 | $xml_version = 1;
|
---|
350 | }
|
---|
351 | }
|
---|
352 |
|
---|
353 | close ITEMFILE;
|
---|
354 | return $xml_version;
|
---|
355 | }
|
---|
356 |
|
---|
357 | sub tidy_item_file {
|
---|
358 | my $self = shift(@_);
|
---|
359 | my ($filename) = @_;
|
---|
360 |
|
---|
361 | open (ITEMFILE, $filename) || die "couldn't open $filename\n";
|
---|
362 | my $backup_filename = "backup.item";
|
---|
363 | open (BACKUP,">$backup_filename")|| die "couldn't write to $backup_filename\n";
|
---|
364 | my $line = "";
|
---|
365 | $line = <ITEMFILE>;
|
---|
366 | $line =~ s/^\xEF\xBB\xBF//; # strip BOM
|
---|
367 | $line =~ s/\x0B+//ig;
|
---|
368 | $line =~ s/&/&/g;
|
---|
369 | print BACKUP ($line);
|
---|
370 | #Tidy up the item file some metadata title contains \vt-vertical tab
|
---|
371 | while ($line = <ITEMFILE>) {
|
---|
372 | $line =~ s/\x0B+//ig;
|
---|
373 | $line =~ s/&/&/g;
|
---|
374 | print BACKUP ($line);
|
---|
375 | }
|
---|
376 | close ITEMFILE;
|
---|
377 | close BACKUP;
|
---|
378 | &File::Copy::copy ($backup_filename, $filename);
|
---|
379 | &util::rm($backup_filename);
|
---|
380 |
|
---|
381 | }
|
---|
382 |
|
---|
383 | sub rotate_image {
|
---|
384 | my $self = shift (@_);
|
---|
385 | my ($filename_full_path) = @_;
|
---|
386 |
|
---|
387 | my ($this_filetype) = $filename_full_path =~ /\.([^\.]*)$/;
|
---|
388 | my $result = $self->convert($filename_full_path, $this_filetype, "-rotate 180", "ROTATE");
|
---|
389 | my ($new_filename) = ($result =~ /=>(.*\.$this_filetype)/);
|
---|
390 | if (-e "$new_filename") {
|
---|
391 | return $new_filename;
|
---|
392 | }
|
---|
393 | # somethings gone wrong
|
---|
394 | return $filename_full_path;
|
---|
395 |
|
---|
396 | }
|
---|
397 |
|
---|
398 | sub process_image {
|
---|
399 | my $self = shift(@_);
|
---|
400 | my ($filename_full_path, $filename_no_path, $doc_obj, $section, $rotate) = @_;
|
---|
401 | # check the filenames
|
---|
402 | return 0 if ($filename_no_path eq "" || !-f $filename_full_path);
|
---|
403 |
|
---|
404 | # remember that this image file was one of our source files, but only
|
---|
405 | # if we are not processing a tmp file
|
---|
406 | if (!$self->{'processing_tmp_files'} ) {
|
---|
407 | $doc_obj->associate_source_file($filename_full_path);
|
---|
408 | }
|
---|
409 | # do rotation
|
---|
410 | if ((defined $rotate) && ($rotate eq "r")) {
|
---|
411 | # we get a new temporary file which is rotated
|
---|
412 | $filename_full_path = $self->rotate_image($filename_full_path);
|
---|
413 | }
|
---|
414 |
|
---|
415 | # do generate images
|
---|
416 | my $result = 0;
|
---|
417 | if ($self->{'image_conversion_available'} == 1) {
|
---|
418 | # do we need to convert $filename_no_path to utf8? We are already reading in from a file, what encoding is it in???
|
---|
419 | $result = $self->generate_images($filename_full_path, $filename_no_path, $doc_obj, $section);
|
---|
420 | }
|
---|
421 | #overwrite one set in ImageConverter
|
---|
422 | $doc_obj->set_metadata_element ($section, "FileFormat", "PagedImage");
|
---|
423 | return $result;
|
---|
424 | }
|
---|
425 |
|
---|
426 |
|
---|
427 | sub xml_start_tag {
|
---|
428 | my $self = shift(@_);
|
---|
429 | my ($expat, $element) = @_;
|
---|
430 | $self->{'element'} = $element;
|
---|
431 |
|
---|
432 | my $doc_obj = $self->{'doc_obj'};
|
---|
433 | if ($element eq "PagedDocument") {
|
---|
434 | $self->{'current_section'} = $doc_obj->get_top_section();
|
---|
435 | } elsif ($element eq "PageGroup" || $element eq "Page") {
|
---|
436 | # create a new section as a child
|
---|
437 | $self->{'current_section'} = $doc_obj->insert_section($doc_obj->get_end_child($self->{'current_section'}));
|
---|
438 | $self->{'num_pages'}++;
|
---|
439 | # assign pagenum as what??
|
---|
440 | my $pagenum = $_{'pagenum'}; #TODO!!
|
---|
441 | if (defined $pagenum) {
|
---|
442 | $doc_obj->set_utf8_metadata_element($self->{'current_section'}, 'PageNum', $pagenum);
|
---|
443 | }
|
---|
444 | my ($imgfile) = $_{'imgfile'};
|
---|
445 | if (defined $imgfile) {
|
---|
446 | # *****
|
---|
447 | # What about support for rotate image (e.g. old ':r' notation)?
|
---|
448 | $self->process_image($self->{'xml_file_dir'}.$imgfile, $imgfile, $doc_obj, $self->{'current_section'});
|
---|
449 | }
|
---|
450 | my ($txtfile) = $_{'txtfile'};
|
---|
451 | if (defined($txtfile)&& $txtfile ne "") {
|
---|
452 | $self->process_text ($self->{'xml_file_dir'}.$txtfile, $txtfile, $doc_obj, $self->{'current_section'});
|
---|
453 | } else {
|
---|
454 | $self->add_dummy_text($doc_obj, $self->{'current_section'});
|
---|
455 | }
|
---|
456 | } elsif ($element eq "Metadata") {
|
---|
457 | $self->{'metadata_name'} = $_{'name'};
|
---|
458 | }
|
---|
459 | }
|
---|
460 |
|
---|
461 | sub xml_end_tag {
|
---|
462 | my $self = shift(@_);
|
---|
463 | my ($expat, $element) = @_;
|
---|
464 |
|
---|
465 | my $doc_obj = $self->{'doc_obj'};
|
---|
466 | if ($element eq "Page" || $element eq "PageGroup") {
|
---|
467 | # if Title hasn't been assigned, set PageNum as Title
|
---|
468 | if (!defined $doc_obj->get_metadata_element ($self->{'current_section'}, "Title") && defined $doc_obj->get_metadata_element ($self->{'current_section'}, "PageNum" )) {
|
---|
469 | $doc_obj->add_utf8_metadata ($self->{'current_section'}, "Title", $doc_obj->get_metadata_element ($self->{'current_section'}, "PageNum" ));
|
---|
470 | }
|
---|
471 | # move the current section back to the parent
|
---|
472 | $self->{'current_section'} = $doc_obj->get_parent_section($self->{'current_section'});
|
---|
473 | } elsif ($element eq "Metadata") {
|
---|
474 |
|
---|
475 | $doc_obj->add_utf8_metadata ($self->{'current_section'}, $self->{'metadata_name'}, $self->{'metadata_value'});
|
---|
476 | $self->{'metadata_name'} = "";
|
---|
477 | $self->{'metadata_value'} = "";
|
---|
478 |
|
---|
479 | }
|
---|
480 | # otherwise we ignore the end tag
|
---|
481 | }
|
---|
482 |
|
---|
483 |
|
---|
484 | sub xml_text {
|
---|
485 | my $self = shift(@_);
|
---|
486 | my ($expat) = @_;
|
---|
487 |
|
---|
488 | if ($self->{'element'} eq "Metadata" && $self->{'metadata_name'}) {
|
---|
489 | $self->{'metadata_value'} .= $_;
|
---|
490 | }
|
---|
491 | }
|
---|
492 |
|
---|
493 | sub xml_doctype {
|
---|
494 | }
|
---|
495 |
|
---|
496 | sub open_document {
|
---|
497 | my $self = shift(@_);
|
---|
498 |
|
---|
499 | # create a new document
|
---|
500 | $self->{'doc_obj'} = new doc ($self->{'filename'}, "indexed_doc", $self->{'file_rename_method'});
|
---|
501 | # TODO is file filenmae_no_path??
|
---|
502 | $self->set_initial_doc_fields($self->{'doc_obj'}, $self->{'file'}, $self->{'processor'});
|
---|
503 |
|
---|
504 | my ($dir, $file) = $self->{'filename'} =~ /^(.*?)([^\/\\]*)$/;
|
---|
505 | $self->{'xml_file_dir'} = $dir;
|
---|
506 | $self->{'num_pages'} = 0;
|
---|
507 |
|
---|
508 | }
|
---|
509 |
|
---|
510 | sub close_document {
|
---|
511 | my $self = shift(@_);
|
---|
512 | my $doc_obj = $self->{'doc_obj'};
|
---|
513 |
|
---|
514 | # add numpages metadata
|
---|
515 | my $topsection = $doc_obj->get_top_section();
|
---|
516 |
|
---|
517 | $doc_obj->set_utf8_metadata_element ($topsection, 'NumPages', $self->{'num_pages'});
|
---|
518 |
|
---|
519 | $doc_obj->set_utf8_metadata_element($topsection,"MaxImageWidth",$self->{'MaxImageWidth'});
|
---|
520 | $doc_obj->set_utf8_metadata_element($topsection,"MaxImageHeight",$self->{'MaxImageHeight'});
|
---|
521 | $self->{'MaxImageWidth'} = undef;
|
---|
522 | $self->{'MaxImageHeight'} = undef;
|
---|
523 |
|
---|
524 | }
|
---|
525 |
|
---|
526 |
|
---|
527 | sub set_initial_doc_fields {
|
---|
528 | my $self = shift(@_);
|
---|
529 | my ($doc_obj, $filename_no_path, $processor) = @_;
|
---|
530 |
|
---|
531 | my $topsection = $doc_obj->get_top_section();
|
---|
532 |
|
---|
533 | if ($self->{'documenttype'} eq 'paged') {
|
---|
534 | # set the gsdlthistype metadata to Paged - this ensures this document will
|
---|
535 | # be treated as a Paged doc, even if Titles are not numeric
|
---|
536 | $doc_obj->set_utf8_metadata_element ($topsection, "gsdlthistype", "Paged");
|
---|
537 | } else {
|
---|
538 | $doc_obj->set_utf8_metadata_element ($topsection, "gsdlthistype", "Hierarchy");
|
---|
539 | }
|
---|
540 |
|
---|
541 | $self->set_Source_metadata($doc_obj, $filename_no_path);
|
---|
542 |
|
---|
543 | # if we want a header page, we need to add some text into the top section, otherwise this section will become invisible
|
---|
544 | if ($self->{'headerpage'}) {
|
---|
545 | $self->add_dummy_text($doc_obj, $topsection);
|
---|
546 | }
|
---|
547 |
|
---|
548 |
|
---|
549 | }
|
---|
550 |
|
---|
551 |
|
---|
552 | sub process_item {
|
---|
553 | my $self = shift (@_);
|
---|
554 | my ($filename_full_path, $dir, $filename_no_path, $processor) = @_;
|
---|
555 |
|
---|
556 | my $doc_obj = new doc ($filename_full_path, "indexed_doc", $self->{'file_rename_method'});
|
---|
557 | $self->set_initial_doc_fields($doc_obj, $filename_no_path, $processor);
|
---|
558 | my $topsection = $doc_obj->get_top_section();
|
---|
559 | open (ITEMFILE, $filename_full_path) || die "couldn't open $filename_full_path\n";
|
---|
560 | my $line = "";
|
---|
561 | my $num = 0;
|
---|
562 | while (defined ($line = <ITEMFILE>)) {
|
---|
563 | next unless $line =~ /\w/;
|
---|
564 | chomp $line;
|
---|
565 | next if $line =~ /^#/; # ignore comment lines
|
---|
566 | if ($line =~ /^<([^>]*)>\s*(.*?)\s*$/) {
|
---|
567 | $doc_obj->set_utf8_metadata_element ($topsection, $1, $2);
|
---|
568 | #$meta->{$1} = $2;
|
---|
569 | } else {
|
---|
570 | $num++;
|
---|
571 | # line should be like page:imagefilename:textfilename:r - the r is optional -> means rotate the image 180 deg
|
---|
572 | $line =~ s/^\s+//; #remove space at the front
|
---|
573 | $line =~ s/\s+$//; #remove space at the end
|
---|
574 | my ($pagenum, $imgname, $txtname, $rotate) = split /:/, $line;
|
---|
575 |
|
---|
576 | # create a new section for each image file
|
---|
577 | my $cursection = $doc_obj->insert_section($doc_obj->get_end_child($topsection));
|
---|
578 | # the page number becomes the Title
|
---|
579 | $doc_obj->set_utf8_metadata_element($cursection, 'Title', $pagenum);
|
---|
580 |
|
---|
581 | # process the image for this page if there is one
|
---|
582 | if (defined $imgname && $imgname ne "") {
|
---|
583 | my $result1 = $self->process_image($dir.$imgname, $imgname, $doc_obj, $cursection, $rotate);
|
---|
584 |
|
---|
585 | if (!defined $result1)
|
---|
586 | {
|
---|
587 | print "PagedImagePlugin: couldn't process image \"$dir.$imgname\" for item \"$filename_full_path\"\n";
|
---|
588 | }
|
---|
589 | }
|
---|
590 | # process the text file if one is there
|
---|
591 | if (defined $txtname && $txtname ne "") {
|
---|
592 | my $result2 = $self->process_text ($dir.$txtname, $txtname, $doc_obj, $cursection);
|
---|
593 |
|
---|
594 | if (!defined $result2) {
|
---|
595 | print "PagedImagePlugin: couldn't process text file \"$dir.$txtname\" for item \"$filename_full_path\"\n";
|
---|
596 | $self->add_dummy_text($doc_obj, $cursection);
|
---|
597 | }
|
---|
598 | } else {
|
---|
599 | # otherwise add in some dummy text
|
---|
600 | $self->add_dummy_text($doc_obj, $cursection);
|
---|
601 | }
|
---|
602 | }
|
---|
603 | }
|
---|
604 |
|
---|
605 | close ITEMFILE;
|
---|
606 |
|
---|
607 | # add numpages metadata
|
---|
608 | $doc_obj->set_utf8_metadata_element ($topsection, 'NumPages', "$num");
|
---|
609 |
|
---|
610 | $doc_obj->set_utf8_metadata_element($topsection,"MaxImageWidth",$self->{'MaxImageWidth'});
|
---|
611 | $doc_obj->set_utf8_metadata_element($topsection,"MaxImageHeight",$self->{'MaxImageHeight'});
|
---|
612 | $self->{'MaxImageWidth'} = undef;
|
---|
613 | $self->{'MaxImageHeight'} = undef;
|
---|
614 |
|
---|
615 |
|
---|
616 | return $doc_obj;
|
---|
617 | }
|
---|
618 |
|
---|
619 | sub process_text {
|
---|
620 | my $self = shift (@_);
|
---|
621 | my ($filename_full_path, $file, $doc_obj, $cursection) = @_;
|
---|
622 |
|
---|
623 | # check that the text file exists!!
|
---|
624 | if (!-f $filename_full_path) {
|
---|
625 | print "PagedImagePlugin: ERROR: File $filename_full_path does not exist, skipping\n";
|
---|
626 | return 0;
|
---|
627 | }
|
---|
628 |
|
---|
629 | # remember that this text file was one of our source files, but only
|
---|
630 | # if we are not processing a tmp file
|
---|
631 | if (!$self->{'processing_tmp_files'} ) {
|
---|
632 | $doc_obj->associate_source_file($filename_full_path);
|
---|
633 | }
|
---|
634 | # Do encoding stuff
|
---|
635 | my ($language, $encoding) = $self->textcat_get_language_encoding ($filename_full_path);
|
---|
636 |
|
---|
637 | my $text="";
|
---|
638 | &ReadTextFile::read_file($self, $filename_full_path, $encoding, $language, \$text);
|
---|
639 | if (!length ($text)) {
|
---|
640 | # It's a bit unusual but not out of the question to have no text, so just give a warning
|
---|
641 | print "PagedImagePlugin: WARNING: $filename_full_path contains no text\n";
|
---|
642 | }
|
---|
643 |
|
---|
644 | # we need to escape the escape character, or else mg will convert into
|
---|
645 | # eg literal newlines, instead of leaving the text as '\n'
|
---|
646 | $text =~ s/\\/\\\\/g; # macro language
|
---|
647 | $text =~ s/_/\\_/g; # macro language
|
---|
648 |
|
---|
649 |
|
---|
650 | if ($text =~ m/<html.*?>\s*<head.*?>.*<\/head>\s*<body.*?>(.*)<\/body>\s*<\/html>\s*$/is) {
|
---|
651 | # looks like HTML input
|
---|
652 | # no need to escape < and > or put in <pre> tags
|
---|
653 |
|
---|
654 | $text = $1;
|
---|
655 |
|
---|
656 | # insert preformat tags and add text to document object
|
---|
657 | $doc_obj->add_utf8_text($cursection, "$text");
|
---|
658 | }
|
---|
659 | else {
|
---|
660 | $text =~ s/</</g;
|
---|
661 | $text =~ s/>/>/g;
|
---|
662 |
|
---|
663 | # insert preformat tags and add text to document object
|
---|
664 | $doc_obj->add_utf8_text($cursection, "<pre>\n$text\n</pre>");
|
---|
665 | }
|
---|
666 |
|
---|
667 |
|
---|
668 | return 1;
|
---|
669 | }
|
---|
670 |
|
---|
671 |
|
---|
672 | sub clean_up_after_doc_obj_processing {
|
---|
673 | my $self = shift(@_);
|
---|
674 |
|
---|
675 | $self->ImageConverter::clean_up_temporary_files();
|
---|
676 | }
|
---|
677 |
|
---|
678 | 1;
|
---|