source: trunk/gsdl/perllib/plugins/BasPlug.pm@ 10434

Last change on this file since 10434 was 10347, checked in by kjdon, 19 years ago

removed the unneeded 'use parsargv'

  • Property svn:keywords set to Author Date Id Revision
File size: 41.7 KB
Line 
1###########################################################################
2#
3# BasPlug.pm -- base class for all the import plugins
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999-2005 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26package BasPlug;
27
28BEGIN {
29 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
30}
31
32eval {require bytes};
33
34# suppress the annoying "subroutine redefined" warning that various
35# plugins cause under perl 5.6
36$SIG{__WARN__} = sub {warn($_[0]) unless ($_[0] =~ /Subroutine\s+\S+\sredefined/)};
37
38use strict;
39no strict 'subs';
40no strict 'refs'; # allow filehandles to be variables and viceversa
41
42use File::Basename;
43
44use Kea;
45use multiread;
46use encodings;
47use cnseg;
48use acronym;
49use textcat;
50use doc;
51eval "require diagnostics"; # some perl distros (eg mac) don't have this
52use DateExtract;
53use ghtml;
54use gsprintf 'gsprintf';
55use printusage;
56use parse2;
57
58
59use GISBasPlug;
60
61@BasPlug::ISA = ( GISBasPlug );
62
63my $unicode_list =
64 [ { 'name' => "ascii",
65 'desc' => "{BasPlug.input_encoding.ascii}" },
66 { 'name' => "utf8",
67 'desc' => "{BasPlug.input_encoding.utf8}" },
68 { 'name' => "unicode",
69 'desc' => "{BasPlug.input_encoding.unicode}" } ];
70
71my $auto_unicode_list =
72 [ { 'name' => "auto",
73 'desc' => "{BasPlug.input_encoding.auto}" } ];
74
75my $arguments =
76 [ { 'name' => "process_exp",
77 'desc' => "{BasPlug.process_exp}",
78 'type' => "regexp",
79 'deft' => "",
80 'reqd' => "no" },
81 { 'name' => "block_exp",
82 'desc' => "{BasPlug.block_exp}",
83 'type' => "regexp",
84 'deft' => "",
85 'reqd' => "no" },
86 { 'name' => "smart_block",
87 'desc' => "{BasPlug.smart_block}",
88 'type' => "flag",
89 'reqd' => "no" },
90 { 'name' => "associate_ext",
91 'desc' => "{BasPlug.associate_ext}",
92 'type' => "string",
93 'reqd' => "no" },
94 { 'name' => "input_encoding",
95 'desc' => "{BasPlug.input_encoding}",
96 'type' => "enum",
97 'list' => $auto_unicode_list,
98 'reqd' => "no" ,
99 'deft' => "auto" } ,
100 { 'name' => "default_encoding",
101 'desc' => "{BasPlug.default_encoding}",
102 'type' => "enum",
103 'list' => $unicode_list,
104 'reqd' => "no",
105 'deft' => "utf8" },
106 { 'name' => "extract_language",
107 'desc' => "{BasPlug.extract_language}",
108 'type' => "flag",
109 'reqd' => "no" },
110 { 'name' => "default_language",
111 'desc' => "{BasPlug.default_language}",
112 'type' => "string",
113 'deft' => "en",
114 'reqd' => "no" },
115 { 'name' => "extract_acronyms",
116 'desc' => "{BasPlug.extract_acronyms}",
117 'type' => "flag",
118 'reqd' => "no" },
119 { 'name' => "markup_acronyms",
120 'desc' => "{BasPlug.markup_acronyms}",
121 'type' => "flag",
122 'reqd' => "no" },
123 { 'name' => "extract_keyphrases",
124 'desc' => "{BasPlug.extract_keyphrases}",
125 'type' => "flag",
126 'reqd' => "no" },
127 { 'name' => "extract_keyphrase_options",
128 'desc' => "{BasPlug.extract_keyphrase_options}",
129 'type' => "string",
130 'deft' => "",
131 'reqd' => "no" },
132 { 'name' => "first",
133 'desc' => "{BasPlug.first}",
134 'type' => "string",
135 'reqd' => "no" },
136 { 'name' => "extract_email",
137 'desc' => "{BasPlug.extract_email}",
138 'type' => "flag",
139 'reqd' => "no" },
140 { 'name' => "extract_historical_years",
141 'desc' => "{BasPlug.extract_historical_years}",
142 'type' => "flag",
143 'reqd' => "no" },
144 { 'name' => "maximum_year",
145 'desc' => "{BasPlug.maximum_year}",
146 'type' => "int",
147 'deft' => (localtime)[5]+1900,
148 'char_length' => "4",
149 #'range' => "2,100",
150 'reqd' => "no"},
151 { 'name' => "maximum_century",
152 'desc' => "{BasPlug.maximum_century}",
153 'type' => "string",
154 'deft' => "-1",
155 'reqd' => "no" },
156 { 'name' => "no_bibliography",
157 'desc' => "{BasPlug.no_bibliography}",
158 'type' => "flag",
159 'reqd' => "no"},
160 { 'name' => "no_cover_image",
161 'desc' => "{BasPlug.no_cover_image}",
162 'type' => "flag",
163 'reqd' => "no" },
164 { 'name' => "extract_keyphrases",
165 'desc' => "{BasPlug.extract_keyphrases}",
166 'type' => "flag",
167 'reqd' => "no",
168 'hiddengli' => "yes" },
169 { 'name' => "extract_keyphrase_options",
170 'desc' => "{BasPlug.extract_keyphrase_options}",
171 'type' => "string",
172 'reqd' => "no",
173 'hiddengli' => "yes" },
174 { 'name' => "separate_cjk",
175 'desc' => "{BasPlug.separate_cjk}",
176 'type' => "flag",
177 'reqd' => "no",
178 'hiddengli' => "yes" },
179 { 'name' => "smart_block",
180 'desc' => "{BasPlug.smart_block}",
181 'type' => "flag",
182 'reqd' => "no",
183 'hiddengli' => "yes" },
184 { 'name' => "new_extract_email",
185 'desc' => "",
186 'type' => "flag",
187 'reqd' => "no",
188 'hiddengli' => "yes" } ];
189
190my $gis_arguments =
191 [ { 'name' => "extract_placenames",
192 'desc' => "{GISBasPlug.extract_placenames}",
193 'type' => "flag",
194 'reqd' => "no" },
195 { 'name' => "gazetteer",
196 'desc' => "{GISBasPlug.gazetteer}",
197 'type' => "string",
198 'reqd' => "no" },
199 { 'name' => "place_list",
200 'desc' => "{GISBasPlug.place_list}",
201 'type' => "flag",
202 'reqd' => "no" } ];
203
204
205my $options = { 'name' => "BasPlug",
206 'desc' => "{BasPlug.desc}",
207 'abstract' => "yes",
208 'inherits' => "no",
209 'args' => $arguments };
210
211
212sub get_arguments
213{
214 my $self = shift(@_);
215 my $optionlistref = $self->{'option_list'};
216 my @optionlist = @$optionlistref;
217 my $pluginoptions = pop(@$optionlistref);
218 my $pluginarguments = $pluginoptions->{'args'};
219 return $pluginarguments;
220}
221
222
223sub print_xml_usage
224{
225 my $self = shift(@_);
226
227 # XML output is always in UTF-8
228 gsprintf::output_strings_in_UTF8;
229
230 PrintUsage::print_xml_header();
231 $self->print_xml();
232}
233
234
235sub print_xml
236{
237 my $self = shift(@_);
238
239 my $optionlistref = $self->{'option_list'};
240 my @optionlist = @$optionlistref;
241 my $pluginoptions = shift(@$optionlistref);
242 return if (!defined($pluginoptions));
243
244 gsprintf(STDERR, "<PlugInfo>\n");
245 gsprintf(STDERR, " <Name>$pluginoptions->{'name'}</Name>\n");
246 my $desc = gsprintf::lookup_string($pluginoptions->{'desc'});
247 $desc =~ s/</&amp;lt;/g; # doubly escaped
248 $desc =~ s/>/&amp;gt;/g;
249
250 gsprintf(STDERR, " <Desc>$desc</Desc>\n");
251 gsprintf(STDERR, " <Abstract>$pluginoptions->{'abstract'}</Abstract>\n");
252 gsprintf(STDERR, " <Inherits>$pluginoptions->{'inherits'}</Inherits>\n");
253 gsprintf(STDERR, " <Explodes>" . ($pluginoptions->{'explodes'} || "no") . "</Explodes>\n");
254 gsprintf(STDERR, " <Arguments>\n");
255 if (defined($pluginoptions->{'args'})) {
256 &PrintUsage::print_options_xml($pluginoptions->{'args'});
257 }
258
259 # Recurse up the plugin hierarchy
260 $self->print_xml();
261
262 gsprintf(STDERR, " </Arguments>\n");
263 gsprintf(STDERR, "</PlugInfo>\n");
264}
265
266
267sub print_txt_usage
268{
269 my $self = shift(@_);
270 # Print the usage message for a plugin (recursively)
271 my $descoffset = $self->determine_description_offset(0);
272 $self->print_plugin_usage($descoffset, 1);
273}
274
275
276sub determine_description_offset
277{
278 my $self = shift(@_);
279 my $maxoffset = shift(@_);
280
281 my $optionlistref = $self->{'option_list'};
282 my @optionlist = @$optionlistref;
283 my $pluginoptions = shift(@$optionlistref);
284 return $maxoffset if (!defined($pluginoptions));
285
286 # Find the length of the longest option string of this plugin
287 my $pluginargs = $pluginoptions->{'args'};
288 if (defined($pluginargs)) {
289 my $longest = &PrintUsage::find_longest_option_string($pluginargs);
290 if ($longest > $maxoffset) {
291 $maxoffset = $longest;
292 }
293 }
294
295 # Recurse up the plugin hierarchy
296 $maxoffset = $self->determine_description_offset($maxoffset);
297 $self->{'option_list'} = \@optionlist;
298 return $maxoffset;
299}
300
301
302sub print_plugin_usage
303{
304 my $self = shift(@_);
305 my $descoffset = shift(@_);
306 my $isleafclass = shift(@_);
307
308 my $optionlistref = $self->{'option_list'};
309 my @optionlist = @$optionlistref;
310 my $pluginoptions = shift(@$optionlistref);
311 return if (!defined($pluginoptions));
312
313 my $pluginname = $pluginoptions->{'name'};
314 my $pluginargs = $pluginoptions->{'args'};
315 my $plugindesc = $pluginoptions->{'desc'};
316
317 # Produce the usage information using the data structure above
318 if ($isleafclass) {
319 if (defined($plugindesc)) {
320 gsprintf(STDERR, "$plugindesc\n\n");
321 }
322 gsprintf(STDERR, " {common.usage}: plugin $pluginname [{common.options}]\n\n");
323 }
324
325 # Display the plugin options, if there are some
326 if (defined($pluginargs)) {
327 # Calculate the column offset of the option descriptions
328 my $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
329
330 if ($isleafclass) {
331 gsprintf(STDERR, " {common.specific_options}:\n");
332 }
333 else {
334 gsprintf(STDERR, " {common.general_options}:\n", $pluginname);
335 }
336
337 # Display the plugin options
338 &PrintUsage::print_options_txt($pluginargs, $optiondescoffset);
339 }
340
341 # Recurse up the plugin hierarchy
342 $self->print_plugin_usage($descoffset, 0);
343 $self->{'option_list'} = \@optionlist;
344}
345
346
347sub new {
348 # Set Encodings to the list!!
349
350 my $e = $encodings::encodings;
351 foreach my $enc (sort {$e->{$a}->{'name'} cmp $e->{$b}->{'name'}} keys (%$e))
352 {
353 my $hashEncode =
354 {'name' => $enc,
355 'desc' => $e->{$enc}->{'name'}};
356
357 push(@{$unicode_list},$hashEncode);
358 }
359
360 push(@{$auto_unicode_list},@{$unicode_list});
361
362 # Start the BasPlug Constructor
363 my $class = shift (@_);
364 my ($pluginlist,$args,$hashArgOptLists) = @_;
365 push(@$pluginlist, $class);
366 my $plugin_name = (defined $pluginlist->[0]) ? $pluginlist->[0] : $class;
367
368 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
369 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
370
371 if (GISBasPlug::has_mapdata()) {
372 push(@$arguments,@$gis_arguments);
373 }
374
375 my $self = {};
376 if(!parse2::parse($args,$hashArgOptLists->{"ArgList"},$self))
377 {
378 my $classTempClass = bless $self, $class;
379 &gsprintf(STDERR, "\n{BasPlug.bad_general_option}\n", $plugin_name);
380 $classTempClass->print_txt_usage(""); # Use default resource bundle
381 die "\n";
382 }
383
384
385 # else parsing was successful.
386
387 $self->{'plugin_type'} = $plugin_name;
388 $self->{'outhandle'} = STDERR;
389 $self->{'textcat'} = new textcat();
390 $self->{'num_processed'} = 0;
391 $self->{'num_not_processed'} = 0;
392 $self->{'num_blocked'} = 0;
393 $self->{'num_archives'} = 0;
394 $self->{'cover_image'} = 1; # cover image is on by default
395 $self->{'cover_image'} = 0 if ($self->{'no_cover_image'});
396 $self->{'file_blocks'} = {};
397 $self->{'option_list'} = $hashArgOptLists->{"OptList"};
398
399 my $associate_ext = $self->{'associate_ext'};
400 if ((defined $associate_ext) && ($associate_ext ne "")) {
401 my @exts = split(/,/,$associate_ext);
402
403 my %associate_ext_lookup = ();
404 foreach my $e (@exts) {
405 $associate_ext_lookup{$e} = 1;
406 }
407
408 $self->{'associate_ext_lookup'} = \%associate_ext_lookup;
409 }
410
411 $self->{'shared_fileroot'} = {};
412 $self->{'file_blocks'} = {};
413
414 if ($self->{'extract_placenames'}) {
415
416 my $outhandle = $self->{'outhandle'};
417
418 my $places_ref
419 = GISBasPlug::loadGISDatabase($outhandle,$self->{'gazetteer'});
420
421 if (!defined $places_ref) {
422 print $outhandle "Warning: Error loading mapdata gazetteer \"$self->{'gazetteer'}\"\n";
423 print $outhandle " No placename extraction will take place.\n";
424 $self->{'extract_placenames'} = undef;
425 }
426 else {
427 $self->{'places'} = $places_ref;
428 }
429 }
430 return bless $self, $class;
431
432}
433
434# initialize BasPlug options
435# if init() is overridden in a sub-class, remember to call BasPlug::init()
436sub init {
437 my $self = shift (@_);
438 my ($verbosity, $outhandle, $failhandle) = @_;
439
440 # verbosity is passed through from the processor
441 $self->{'verbosity'} = $verbosity;
442
443 # as are the outhandle and failhandle
444 $self->{'outhandle'} = $outhandle if defined $outhandle;
445 $self->{'failhandle'} = $failhandle;
446
447 # set process_exp and block_exp to defaults unless they were
448 # explicitly set
449
450 if ((!$self->is_recursive()) and
451 (!defined $self->{'process_exp'}) || ($self->{'process_exp'} eq "")) {
452
453 $self->{'process_exp'} = $self->get_default_process_exp ();
454 if ($self->{'process_exp'} eq "") {
455 warn ref($self) . " Warning: Non-recursive plugin has no process_exp\n";
456 }
457 }
458
459 if ((!defined $self->{'block_exp'}) || ($self->{'block_exp'} eq "")) {
460 $self->{'block_exp'} = $self->get_default_block_exp ();
461 }
462}
463
464sub begin {
465 my $self = shift (@_);
466 my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
467 $self->initialise_extractors();
468}
469
470sub end {
471 # potentially called at the end of each plugin pass
472 # import.pl only has one plugin pass, but buildcol.pl has multiple ones
473
474 my ($self) = @_;
475 $self->finalise_extractors();
476}
477
478sub deinit {
479 # called only once, after all plugin passes have been done
480
481 my ($self) = @_;
482}
483
484# this function should be overridden to return 1
485# in recursive plugins
486sub is_recursive {
487 my $self = shift (@_);
488
489 return 0;
490}
491
492sub get_default_block_exp {
493 my $self = shift (@_);
494
495 return "";
496}
497
498sub get_default_process_exp {
499 my $self = shift (@_);
500
501 return "";
502}
503
504# default implementation is to do nothing.
505sub store_block_files
506{
507 my $self =shift (@_);
508 my ($filename) = @_;
509 return;
510}
511
512#default implementation is to block a file with same name as this, but extension jpg or JPG, if cover_images is on.
513sub block_cover_image
514{
515 my $self =shift (@_);
516 my ($filename) = @_;
517 if ($self->{'cover_image'}) {
518 my $coverfile = $filename;
519 $coverfile =~ s/\.[^\\\/\.]+$/\.jpg/;
520 if (!-e $coverfile) {
521 $coverfile =~ s/jpg$/JPG/;
522 }
523 if (-e $coverfile) {
524 $self->{'file_blocks'}->{$coverfile} = 1;
525 }
526 }
527
528 return;
529}
530
531sub metadata_read {
532 my $self = shift (@_);
533 my ($pluginfo, $base_dir, $file, $metadata, $extrametakeys, $extrametadata, $processor, $maxdocs, $gli) = @_;
534 # Keep track of filenames with same root but different extensions
535 # Used to support -associate_ext
536
537 my $associate_ext = $self->{'associate_ext'};
538 if ((defined $associate_ext) && ($associate_ext ne "")) {
539
540 my ($file_prefix,$file_ext) = ($file =~ m/^(.*)\.(.*?)$/);
541 if ((defined $file_prefix) && (defined $file_ext)) {
542
543 my $shared_fileroot = $self->{'shared_fileroot'};
544 if (!defined $shared_fileroot->{$file_prefix}) {
545 my $file_prefix_rec = { 'tie_to' => undef, 'exts' => {} };
546 $shared_fileroot->{$file_prefix} = $file_prefix_rec;
547 }
548
549 my $file_prefix_rec = $shared_fileroot->{$file_prefix};
550
551 my $process_exp = $self->{'process_exp'};
552
553 if ($file =~ m/$self->{'process_exp'}/) {
554 # This is the document the others should be tied to
555 $file_prefix_rec->{'tie_to'} = $file_ext;
556 }
557 else {
558 if (defined $self->{'associate_ext_lookup'}->{$file_ext}) {
559 $file_prefix_rec->{'exts'}->{$file_ext} = 1;
560 }
561 }
562 }
563 }
564
565 # now check whether we are actually processing this
566 my $filename = $file;
567 $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
568 if ($self->{'process_exp'} eq "" || $filename !~ /$self->{'process_exp'}/ || !-f $filename) {
569 return undef; # can't recognise
570 }
571
572 # do smart blocking if appropriate
573 if (defined $self->{'smart_block'}) {
574 $self->block_cover_image($filename);
575 $self->store_block_files($filename);
576 }
577
578 return 1;
579}
580
581sub tie_to_filename
582{
583 my $self = shift (@_);
584
585 my ($file_ext,$file_prefix_rec) = @_;
586
587 if (defined $file_prefix_rec) {
588 my $tie_to = $file_prefix_rec->{'tie_to'};
589
590 if (defined $tie_to) {
591 if ($tie_to eq $file_ext) {
592 return 1;
593 }
594 }
595 }
596
597 return 0;
598}
599
600sub tie_to_assoc_file
601{
602 my $self = shift (@_);
603 my ($file_ext,$file_prefix_rec) = @_;
604
605 if (defined $file_prefix_rec) {
606 my $tie_to = $file_prefix_rec->{'tie_to'};
607 if (defined $tie_to) {
608
609 my $exts = $file_prefix_rec->{'exts'};
610
611 my $has_file_ext = $exts->{$file_ext};
612
613 if ($has_file_ext) {
614 return 1;
615 }
616 }
617 }
618
619 return 0;
620}
621
622
623sub associate_with
624{
625 my $self = shift (@_);
626 my ($file, $filename, $metadata) = @_;
627
628 my $associate_ext = $self->{'associate_ext'};
629
630
631 return 0 if (!$associate_ext);
632
633 # If file, see if matches with "tie_to" doc or is one of the
634 # associated filename extensions.
635
636 my ($file_prefix,$file_ext) = ($file =~ m/^(.*)\.(.*?)$/);
637 if ((defined $file_prefix) && (defined $file_ext)) {
638
639 my $file_prefix_rec = $self->{'shared_fileroot'}->{$file_prefix};
640
641 if ($self->tie_to_filename($file_ext,$file_prefix_rec)) {
642
643 # Set up gsdlassocfile_tobe
644
645 my $exts = $file_prefix_rec->{'exts'};
646
647 if (!defined $metadata->{'gsdlassocfile_tobe'}) {
648 $metadata->{'gsdlassocfile_tobe'} = [];
649 }
650
651 my $assoc_tobe = $metadata->{'gsdlassocfile_tobe'};
652
653 my ($full_prefix) = ($filename =~ m/^(.*)\..*?$/);
654 foreach my $e (keys %$exts) {
655 my $assoc_file = "$full_prefix.$e";
656 my $mime_type = ""; # let system auto detect this
657 push(@$assoc_tobe,"$assoc_file:$mime_type:");
658 }
659 }
660 elsif ($self->tie_to_assoc_file($file_ext,$file_prefix_rec)) {
661 # a form of smart block
662
663 return 1;
664 }
665 }
666
667 return 0;
668}
669
670
671sub read_block {
672 my $self = shift (@_);
673
674 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
675
676
677 my $filename = $file;
678 $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
679
680 if ($self->associate_with($file,$filename,$metadata)) {
681 # a form of smart block
682 $self->{'num_blocked'} ++;
683 return (0,undef); # blocked
684 }
685
686 my $smart_block = $self->{'smart_block'};
687 my $smart_block_BN = $self->{'smart_block_BN'};
688
689 if ($smart_block || $smart_block_BN) {
690 if (defined $self->{'file_blocks'}->{$filename} && $self->{'file_blocks'}->{$filename} == 1){
691 $self->{'num_blocked'} ++;
692 return (0,undef); # blocked
693 }
694 } elsif ($self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/) {
695 $self->{'num_blocked'} ++;
696 return (0,undef); # blocked
697 }
698
699 if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
700 return (undef,undef); # can't recognise
701 }
702
703 return (1,$filename);
704}
705
706sub read_tidy_file {
707
708 my $self = shift (@_);
709
710 my ($file) = @_;
711
712 $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
713
714 return $file;
715}
716
717
718
719# The BasPlug read_into_doc_obj() function. This function does all the
720# right things to make general options work for a given plugin. It reads in
721# a file and sets up a slew of metadata all saved in doc_obj, which
722# it then returns as part of a tuple (process_status,doc_obj)
723#
724# Much of this functionality used to reside in read, but it was broken
725# down into a supporting routine to make the code more flexible.
726#
727# recursive plugins (e.g. RecPlug) and specialized plugins like those
728# capable of processing many documents within a single file (e.g.
729# GMLPlug) will normally want to implement their own version of
730# read_into_doc_obj()
731#
732# Note that $base_dir might be "" and that $file might
733# include directories
734sub read_into_doc_obj {
735 my $self = shift (@_);
736 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
737
738 if ($self->is_recursive()) {
739 gsprintf(STDERR, "{BasPlug.read_must_be_implemented}") && die "\n";
740 }
741
742 my $outhandle = $self->{'outhandle'};
743
744 my ($block_status,$filename) = $self->read_block(@_);
745 return $block_status if ((!defined $block_status) || ($block_status==0));
746 $file = $self->read_tidy_file($file);
747
748 # Do encoding stuff
749 my ($language, $encoding) = $self->textcat_get_language_encoding ($filename);
750
751 # create a new document
752 my $doc_obj = new doc ($filename, "indexed_doc");
753 $doc_obj->set_OIDtype ($processor->{'OIDtype'});
754 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Language", $language);
755 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Encoding", $encoding);
756 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
757 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "FileSize", (-s $filename));
758
759 my ($filemeta) = $file =~ /([^\\\/]+)$/;
760 # how do we know what encoding the filename is in?
761 $doc_obj->add_metadata($doc_obj->get_top_section(), "Source", &ghtml::dmsafe($filemeta));
762 if ($self->{'cover_image'}) {
763 $self->associate_cover_image($doc_obj, $filename);
764 }
765
766 # read in file ($text will be in utf8)
767 my $text = "";
768 $self->read_file ($filename, $encoding, $language, \$text);
769
770 if (!length ($text)) {
771 my $plugin_name = ref ($self);
772 if ($gli) {
773 print STDERR "<ProcessingError n='$file' r='File contains no text'>\n";
774 }
775 gsprintf($outhandle, "$plugin_name: {BasPlug.file_has_no_text}\n", $filename) if $self->{'verbosity'};
776
777 my $failhandle = $self->{'failhandle'};
778 gsprintf($failhandle, "$file: " . ref($self) . ": {BasPlug.empty_file}\n");
779 # print $failhandle "$file: " . ref($self) . ": file contains no text\n";
780 $self->{'num_not_processed'} ++;
781
782 return (0,undef); # what should we return here?? error but don't want to pass it on
783 }
784
785 # include any metadata passed in from previous plugins
786 # note that this metadata is associated with the top level section
787
788 $self->extra_metadata ($doc_obj, $doc_obj->get_top_section(), $metadata);
789
790 # do plugin specific processing of doc_obj
791 unless (defined ($self->process (\$text, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli))) {
792 $text = '';
793 undef $text;
794 print STDERR "<ProcessingError n='$file'>\n" if ($gli);
795 return (-1,undef);
796 }
797 $text='';
798 undef $text;
799
800 # do any automatic metadata extraction
801 $self->auto_extract_metadata ($doc_obj);
802
803 # add an OID
804 # see if there is a plugin-specific set_OID function...
805 if (defined ($self->can('set_OID'))) {
806 # it will need $doc_obj to set the Identifier metadata...
807 $self->set_OID($doc_obj);
808 } else {
809 # use the default set_OID() in doc.pm
810 $doc_obj->set_OID();
811 }
812
813 return (1,$doc_obj);
814}
815
816
817# The BasPlug read() function. This function calls read_into_doc_obj()
818# to ensure all the right things to make general options work for a
819# given plugin are done. It then calls the process() function which
820# does all the work specific to a plugin (like the old read functions
821# used to do). Most plugins should define their own process() function
822# and let this read() function keep control.
823#
824# recursive plugins (e.g. RecPlug) and specialized plugins like those
825# capable of processing many documents within a single file (e.g.
826# GMLPlug) might want to implement their own version of read(), but
827# more likely need to implement their own version of read_into_doc_obj()
828#
829# Return number of files processed, undef if can't recognise, -1 if can't
830# process
831
832sub read {
833 my $self = shift (@_);
834 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
835
836 my ($process_status,$doc_obj) = $self->read_into_doc_obj(@_);
837
838 if ((defined $process_status) && ($process_status == 1)) {
839 # process the document
840 $processor->process($doc_obj);
841
842 if(defined($self->{'places_filename'})){
843 &util::rm($self->{'places_filename'});
844 $self->{'places_filename'} = undef;
845 }
846
847 $self->{'num_processed'} ++;
848 undef $doc_obj;
849 }
850
851 # if process_status == 1, then the file has been processed.
852 return $process_status;
853
854}
855
856# returns undef if file is rejected by the plugin
857sub process {
858 my $self = shift (@_);
859 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
860
861 gsprintf(STDERR, "BasPlug::process {common.must_be_implemented}\n") && die "\n";
862 # die "Basplug::process function must be implemented in sub-class\n";
863
864 return undef; # never gets here
865}
866
867# uses the multiread package to read in the entire file pointed to
868# by filename and loads the resulting text into $$textref. Input text
869# may be in any of the encodings handled by multiread, output text
870# will be in utf8
871sub read_file {
872 my $self = shift (@_);
873 my ($filename, $encoding, $language, $textref) = @_;
874
875 if (!-r $filename)
876 {
877 my $outhandle = $self->{'outhandle'};
878 gsprintf($outhandle, "{BasPlug.read_denied}\n", $filename) if $self->{'verbosity'};
879 # print $outhandle "Read permission denied for $filename\n" if $self->{'verbosity'};
880 return;
881 }
882 $$textref = "";
883 if (!open (FILE, $filename)) {
884 gsprintf(STDERR, "BasPlug::read_file {BasPlug.could_not_open_for_reading} ($!)\n", $filename);
885 die "\n";
886 }
887
888 if ($encoding eq "ascii") {
889 undef $/;
890 $$textref = <FILE>;
891 $/ = "\n";
892 } else {
893 my $reader = new multiread();
894 $reader->set_handle ('BasPlug::FILE');
895 $reader->set_encoding ($encoding);
896 $reader->read_file ($textref);
897 #Now segments chinese if the separate_cjk option is set
898 if ($self->{'separate_cjk'}) {
899 # segment the Chinese words
900 $$textref = &cnseg::segment($$textref);
901 }
902 }
903 close FILE;
904}
905
906# write_file -- used by ConvertToPlug, for example in post processing
907#
908sub utf8_write_file {
909 my $self = shift (@_);
910 my ($textref, $filename) = @_;
911
912 if (!open (FILE, ">$filename")) {
913 gsprintf(STDERR, "ConvertToPlug::write_file {ConvertToPlug.could_not_open_for_writing} ($!)\n", $filename);
914 die "\n";
915 }
916 print FILE $$textref;
917
918 close FILE;
919}
920
921
922sub filename_based_title
923{
924 my $self = shift (@_);
925 my ($file) = @_;
926
927 my $file_derived_title = $file;
928 $file_derived_title =~ s/_/ /g;
929 $file_derived_title =~ s/\..*?$//;
930
931 return $file_derived_title;
932}
933
934
935sub title_fallback
936{
937 my $self = shift (@_);
938 my ($doc_obj,$section,$file) = @_;
939
940 if (!defined $doc_obj->get_metadata_element ($section, "Title")) {
941
942 my $file_derived_title = $self->filename_based_title($file);
943 $doc_obj->add_metadata ($section, "Title", $file_derived_title);
944 }
945}
946
947sub textcat_get_language_encoding {
948 my $self = shift (@_);
949 my ($filename) = @_;
950
951
952 my ($language, $encoding, $extracted_encoding);
953 if ($self->{'input_encoding'} eq "auto") {
954 # use textcat to automatically work out the input encoding and language
955 ($language, $encoding) = $self->get_language_encoding ($filename);
956 } elsif ($self->{'extract_language'}) {
957 # use textcat to get language metadata
958 ($language, $extracted_encoding) = $self->get_language_encoding ($filename);
959 $encoding = $self->{'input_encoding'};
960 # don't print this message for english... english in utf8 is identical
961 # to english in iso-8859-1 (except for some punctuation). We don't have
962 # a language model for en_utf8, so textcat always says iso-8859-1!
963 if ($extracted_encoding ne $encoding && $language ne "en"
964 && $self->{'verbosity'}) {
965 my $plugin_name = ref ($self);
966 my $outhandle = $self->{'outhandle'};
967 gsprintf($outhandle, "$plugin_name: {BasPlug.wrong_encoding}\n", $filename, $encoding, $extracted_encoding);
968 # print $outhandle "$plugin_name: WARNING: $filename was read using $encoding encoding but ";
969 # print $outhandle "appears to be encoded as $extracted_encoding.\n";
970 }
971 } else {
972 $language = $self->{'default_language'};
973 $encoding = $self->{'input_encoding'};
974 }
975
976 return ($language, $encoding);
977}
978
979# Uses textcat to work out the encoding and language of the text in
980# $filename. All html tags are removed before processing.
981# returns an array containing "language" and "encoding"
982sub get_language_encoding {
983 my $self = shift (@_);
984 my ($filename) = @_;
985 my $outhandle = $self->{'outhandle'};
986 my $unicode_format = "";
987 # read in file
988 open (FILE, $filename) || (gsprintf(STDERR, "BasPlug::get_language_encoding {BasPlug.could_not_open_for_reading} ($!)\n", $filename) && die "\n"); # die "BasPlug::get_language_encoding could not open $filename for reading ($!)\n";
989 undef $/;
990 my $text = <FILE>;
991 $/ = "\n";
992 close FILE;
993
994 # check if first few bytes have a Byte Order Marker
995 my $bom=substr($text,0,2); # check 16bit unicode
996 if ($bom eq "\xff\xfe") { # little endian 16bit unicode
997 $unicode_format="unicode";
998 } elsif ($bom eq "\xfe\xff") { # big endian 16bit unicode
999 $unicode_format="unicode";
1000 } else {
1001 $bom=substr($text,0,3); # check utf-8
1002 if ($bom eq "\xef\xbb\xbf") { # utf-8 coded FEFF bom
1003 $unicode_format="utf8";
1004# } elsif ($bom eq "\xef\xbf\xbe") { # utf-8 coded FFFE bom. Error!?
1005# $unicode_format="utf8";
1006 }
1007 }
1008
1009
1010 # remove <title>stuff</title> -- as titles tend often to be in English
1011 # for foreign language documents
1012 $text =~ s/<title>(.|\n)*?<\/title>//i;
1013
1014 # remove all HTML tags
1015 # XXX this doesn't match plugins derived from HTMLPlug (except ConvertTo)
1016 if (ref($self) eq 'HTMLPlug' ||
1017 (exists $self->{'converted_to'} && $self->{'converted_to'} eq 'HTML')){
1018 $text =~ s/<[^>]*>//sg;
1019 }
1020
1021 # get the language/encoding
1022 my $results = $self->{'textcat'}->classify(\$text);
1023
1024 # if textcat returns 3 or less possibilities we'll use the
1025 # first one in the list - otherwise use the defaults
1026 if (scalar @$results > 3) {
1027 my $best_encoding="";
1028 if ($unicode_format) { # in case the first had a BOM
1029 $best_encoding=$unicode_format;
1030 } else {
1031 my %guessed_encodings = ();
1032 foreach my $result (@$results) {
1033 $result =~ /([^\-]+)$/;
1034 my $enc=$1;
1035 if (!defined($guessed_encodings{$enc})) {
1036 $guessed_encodings{$enc}=0;
1037 }
1038 $guessed_encodings{$enc}++;
1039 }
1040
1041 $guessed_encodings{""}=-1; # for default best_encoding of ""
1042 foreach my $enc (keys %guessed_encodings) {
1043 if ($guessed_encodings{$enc} >
1044 $guessed_encodings{$best_encoding}){
1045 $best_encoding=$enc;
1046 }
1047 }
1048 }
1049
1050 if ($self->{'input_encoding'} ne 'auto') {
1051 if ($self->{'extract_language'} && ($self->{'verbosity'}>2)) {
1052 gsprintf($outhandle,
1053 "BasPlug: {BasPlug.could_not_extract_language}\n",
1054 $filename, $self->{'default_language'});
1055 }
1056 return ($self->{'default_language'}, $self->{'input_encoding'});
1057
1058 } else {
1059 if ($self->{'verbosity'}>2) {
1060 gsprintf($outhandle,
1061 "BasPlug: {BasPlug.could_not_extract_language}\n",
1062 $filename, $self->{'default_language'});
1063 }
1064 return ($self->{'default_language'}, $best_encoding);
1065 }
1066 }
1067
1068 # format language/encoding
1069 my ($language, $encoding) = $results->[0] =~ /^([^-]*)(?:-(.*))?$/;
1070 if (!defined $language) {
1071 if ($self->{'verbosity'}>2) {
1072 gsprintf($outhandle,
1073 "BasPlug: {BasPlug.could_not_extract_language}\n",
1074 $filename, $self->{'default_language'});
1075 }
1076 $language = $self->{'default_language'};
1077 }
1078 if (!defined $encoding) {
1079 if ($self->{'verbosity'}>2) {
1080 gsprintf($outhandle,
1081 "BasPlug: {BasPlug.could_not_extract_encoding}\n",
1082 $filename, $self->{'default_encoding'});
1083 }
1084 $encoding = $self->{'default_encoding'};
1085 }
1086
1087
1088 # check for equivalents where textcat doesn't have some encodings...
1089 # eg MS versions of standard encodings
1090 if ($encoding =~ /^iso_8859_(\d+)/) {
1091 my $iso = $1; # which variant of the iso standard?
1092 # iso-8859 sets don't use chars 0x80-0x9f, windows codepages do
1093 if ($text =~ /[\x80-\x9f]/) {
1094 # Western Europe
1095 if ($iso == 1 or $iso == 15) { $encoding = 'windows_1252' }
1096 elsif ($iso == 2) { $encoding = 'windows_1250' } # Central Europe
1097 elsif ($iso == 5) { $encoding = 'windows_1251' } # Cyrillic
1098 elsif ($iso == 6) { $encoding = 'windows_1256' } # Arabic
1099 elsif ($iso == 7) { $encoding = 'windows_1253' } # Greek
1100 elsif ($iso == 8) { $encoding = 'windows_1255' } # Hebrew
1101 elsif ($iso == 9) { $encoding = 'windows_1254' } # Turkish
1102 }
1103 }
1104
1105 if ($encoding !~ /^(ascii|utf8|unicode)$/ &&
1106 !defined $encodings::encodings->{$encoding}) {
1107 if ($self->{'verbosity'}) {
1108 gsprintf($outhandle, "BasPlug: {BasPlug.unsupported_encoding}\n",
1109 $filename, $encoding, $self->{'default_encoding'});
1110 }
1111 $encoding = $self->{'default_encoding'};
1112 }
1113
1114 return ($language, $encoding);
1115}
1116
1117# add any extra metadata that's been passed around from one
1118# plugin to another.
1119# extra_metadata uses add_utf8_metadata so it expects metadata values
1120# to already be in utf8
1121sub extra_metadata {
1122 my $self = shift (@_);
1123 my ($doc_obj, $cursection, $metadata) = @_;
1124
1125 foreach my $field (keys(%$metadata)) {
1126 # $metadata->{$field} may be an array reference
1127 if ($field eq "gsdlassocfile_tobe") {
1128 # 'gsdlassocfile_tobe' is artificially introduced metadata
1129 # that is used to signal that certain additional files should
1130 # be tied to this document. Useful in situations where a
1131 # metadata pass in the plugin pipeline works out some files
1132 # need to be associated with a document, but the document hasn't
1133 # been formed yet.
1134
1135 my $equiv_form = "";
1136 foreach my $gaf (@{$metadata->{$field}}) {
1137 my ($full_filename,$mimetype) = ($gaf =~ m/^(.*):(.*):$/);
1138 my ($tail_filename) = ($full_filename =~ /^.*[\/\\](.+?)$/);
1139 my $filename = $full_filename;
1140
1141 $doc_obj->associate_file($full_filename,$tail_filename,$mimetype);
1142
1143 my ($doc_ext) = ($tail_filename =~ m/^.*\.(.*)$/);
1144 my $start_doclink = "<a href=\"_httpcollection_/index/assoc/{Or}{[parent(Top):archivedir],[archivedir]}/$tail_filename\">";
1145 my $srcicon = "_icon".$doc_ext."_";
1146 my $end_doclink = "</a>";
1147
1148 $equiv_form .= " $start_doclink\{If\}{$srcicon,$srcicon,$doc_ext\}$end_doclink";
1149 }
1150 $doc_obj->add_utf8_metadata ($cursection, "equivlink", $equiv_form);
1151 }
1152 elsif (ref ($metadata->{$field}) eq "ARRAY") {
1153 map {
1154 $doc_obj->add_utf8_metadata ($cursection, $field, $_);
1155 } @{$metadata->{$field}};
1156 } else {
1157 $doc_obj->add_utf8_metadata ($cursection, $field, $metadata->{$field});
1158 }
1159 }
1160}
1161
1162# initialise metadata extractors
1163sub initialise_extractors {
1164 my $self = shift (@_);
1165
1166 if ($self->{'extract_acronyms'} || $self->{'markup_acronyms'}) {
1167 &acronym::initialise_acronyms();
1168 }
1169}
1170
1171# finalise metadata extractors
1172sub finalise_extractors {
1173 my $self = shift (@_);
1174
1175 if ($self->{'extract_acronyms'} || $self->{'markup_acronyms'}) {
1176 &acronym::finalise_acronyms();
1177 }
1178}
1179
1180# FIRSTNNN: extract the first NNN characters as metadata
1181sub extract_first_NNNN_characters {
1182 my $self = shift (@_);
1183 my ($textref, $doc_obj, $thissection) = @_;
1184
1185 foreach my $size (split /,/, $self->{'first'}) {
1186 my $tmptext = $$textref;
1187 $tmptext =~ s/^\s+//;
1188 $tmptext =~ s/\s+$//;
1189 $tmptext =~ s/\s+/ /gs;
1190 $tmptext = substr ($tmptext, 0, $size);
1191 $tmptext =~ s/\s\S*$/&#8230;/;
1192 $doc_obj->add_utf8_metadata ($thissection, "First$size", $tmptext);
1193 }
1194}
1195
1196sub extract_email {
1197 my $self = shift (@_);
1198 my ($textref, $doc_obj, $thissection) = @_;
1199 my $outhandle = $self->{'outhandle'};
1200
1201 gsprintf($outhandle, " {BasPlug.extracting_emails}...\n")
1202 if ($self->{'verbosity'} > 2);
1203
1204 my @email = ($$textref =~ m/([-a-z0-9\.@+_=]+@(?:[-a-z0-9]+\.)+(?:com|org|edu|mil|int|net|[a-z][a-z]))/g);
1205 @email = sort @email;
1206
1207# if($self->{"new_extract_email"} == 0)
1208# {
1209# my @email2 = ();
1210# foreach my $address (@email)
1211# {
1212# if (!(join(" ",@email2) =~ m/(^| )$address( |$)/ ))
1213# {
1214# push @email2, $address;
1215# $doc_obj->add_utf8_metadata ($thissection, "emailAddress", $address);
1216# # print $outhandle " extracting $address\n"
1217# &gsprintf($outhandle, " {BasPlug.extracting} $address\n")
1218# if ($self->{'verbosity'} > 3);
1219# }
1220# }
1221# }
1222# else
1223# {
1224 my $hashExistMail = {};
1225 foreach my $address (@email) {
1226 if (!(defined $hashExistMail->{$address}))
1227 {
1228 $hashExistMail->{$address} = 1;
1229 $doc_obj->add_utf8_metadata ($thissection, "emailAddress", $address);
1230 gsprintf($outhandle, " {BasPlug.extracting} $address\n")
1231 if ($self->{'verbosity'} > 3);
1232 }
1233 }
1234 gsprintf($outhandle, " {BasPlug.done_email_extract}\n")
1235 if ($self->{'verbosity'} > 2);
1236}
1237
1238# extract metadata
1239sub auto_extract_metadata {
1240
1241 my $self = shift (@_);
1242 my ($doc_obj) = @_;
1243
1244 if ($self->{'extract_email'}) {
1245 my $thissection = $doc_obj->get_top_section();
1246 while (defined $thissection) {
1247 my $text = $doc_obj->get_text($thissection);
1248 $self->extract_email (\$text, $doc_obj, $thissection) if $text =~ /./;
1249 $thissection = $doc_obj->get_next_section ($thissection);
1250 }
1251 }
1252 if ($self->{'extract_placenames'}) {
1253 my $thissection = $doc_obj->get_top_section();
1254 while (defined $thissection) {
1255 my $text = $doc_obj->get_text($thissection);
1256 $self->extract_placenames (\$text, $doc_obj, $thissection) if $text =~ /./;
1257 $thissection = $doc_obj->get_next_section ($thissection);
1258 }
1259 }
1260
1261 #adding kea keyphrases
1262 if ($self->{'extract_keyphrases'}) {
1263
1264 my $thissection = $doc_obj->get_top_section();
1265 my $text = "";
1266 my $list;
1267
1268 #loop through sections to gather whole doc
1269 while (defined $thissection) {
1270 my $sectiontext = $doc_obj->get_text($thissection);
1271 $text = $text.$sectiontext;
1272 $thissection = $doc_obj->get_next_section ($thissection);
1273 }
1274
1275 if($self->{'extract_keyphrase_options'}) { #if kea options flag is set, call Kea with specified options
1276 $list = &Kea::extract_KeyPhrases ($text, $self->{'extract_keyphrase_options'});
1277 } else { #otherwise call Kea with no options
1278 $list = &Kea::extract_KeyPhrases ($text);
1279 }
1280
1281 if ($list){
1282 # if a list of kea keyphrases was returned (ie not empty)
1283 if ($self->{'verbosity'}) {
1284 gsprintf(STDERR, "{BasPlug.keyphrases}: $list\n");
1285 }
1286
1287 #add metadata to top section
1288 $thissection = $doc_obj->get_top_section();
1289
1290 # add all key phrases as one metadata
1291 $doc_obj->add_metadata($thissection, "Keyphrases", $list);
1292
1293 # add individual key phrases as multiple metadata
1294 foreach my $keyphrase (split(',', $list)) {
1295 $keyphrase =~ s/^\s+|\s+$//g;
1296 $doc_obj->add_metadata($thissection, "Keyphrase", $keyphrase);
1297 }
1298 }
1299 } #end of kea
1300
1301 if ($self->{'first'}) {
1302 my $thissection = $doc_obj->get_top_section();
1303 while (defined $thissection) {
1304 my $text = $doc_obj->get_text($thissection);
1305 $self->extract_first_NNNN_characters (\$text, $doc_obj, $thissection) if $text =~ /./;
1306 $thissection = $doc_obj->get_next_section ($thissection);
1307 }
1308 }
1309
1310 if ($self->{'extract_acronyms'}) {
1311 my $thissection = $doc_obj->get_top_section();
1312 while (defined $thissection) {
1313 my $text = $doc_obj->get_text($thissection);
1314 $self->extract_acronyms (\$text, $doc_obj, $thissection) if $text =~ /./;
1315 $thissection = $doc_obj->get_next_section ($thissection);
1316 }
1317 }
1318
1319 if ($self->{'markup_acronyms'}) {
1320 my $thissection = $doc_obj->get_top_section();
1321 while (defined $thissection) {
1322 my $text = $doc_obj->get_text($thissection);
1323 $text = $self->markup_acronyms ($text, $doc_obj, $thissection);
1324 $doc_obj->delete_text($thissection);
1325 $doc_obj->add_text($thissection, $text);
1326 $thissection = $doc_obj->get_next_section ($thissection);
1327 }
1328 }
1329
1330 if($self->{'extract_historical_years'}) {
1331 my $thissection = $doc_obj->get_top_section();
1332 while (defined $thissection) {
1333
1334 my $text = $doc_obj->get_text($thissection);
1335 &DateExtract::get_date_metadata($text, $doc_obj,
1336 $thissection,
1337 $self->{'no_bibliography'},
1338 $self->{'maximum_year'},
1339 $self->{'maximum_century'});
1340 $thissection = $doc_obj->get_next_section ($thissection);
1341 }
1342 }
1343}
1344
1345# extract acronyms from a section in a document. progress is
1346# reported to outhandle based on the verbosity. both the Acronym
1347# and the AcronymKWIC metadata items are created.
1348
1349sub extract_acronyms {
1350 my $self = shift (@_);
1351 my ($textref, $doc_obj, $thissection) = @_;
1352 my $outhandle = $self->{'outhandle'};
1353
1354 # print $outhandle " extracting acronyms ...\n"
1355 gsprintf($outhandle, " {BasPlug.extracting_acronyms}...\n")
1356 if ($self->{'verbosity'} > 2);
1357
1358 my $acro_array = &acronym::acronyms($textref);
1359
1360 foreach my $acro (@$acro_array) {
1361
1362 #check that this is the first time ...
1363 my $seen_before = "false";
1364 my $previous_data = $doc_obj->get_metadata($thissection, "Acronym");
1365 foreach my $thisAcro (@$previous_data) {
1366 if ($thisAcro eq $acro->to_string()) {
1367 $seen_before = "true";
1368 if ($self->{'verbosity'} >= 4) {
1369 gsprintf($outhandle, " {BasPlug.already_seen} " .
1370 $acro->to_string() . "\n");
1371 }
1372 }
1373 }
1374
1375 if ($seen_before eq "false") {
1376 #write it to the file ...
1377 $acro->write_to_file();
1378
1379 #do the normal acronym
1380 $doc_obj->add_utf8_metadata($thissection, "Acronym", $acro->to_string());
1381 gsprintf($outhandle, " {BasPlug.adding} ".$acro->to_string()."\n")
1382 if ($self->{'verbosity'} > 3);
1383 }
1384 }
1385
1386 gsprintf($outhandle, " {BasPlug.done_acronym_extract}\n")
1387 if ($self->{'verbosity'} > 2);
1388}
1389
1390sub markup_acronyms {
1391 my $self = shift (@_);
1392 my ($text, $doc_obj, $thissection) = @_;
1393 my $outhandle = $self->{'outhandle'};
1394
1395 gsprintf($outhandle, " {BasPlug.marking_up_acronyms}...\n")
1396 if ($self->{'verbosity'} > 2);
1397
1398 #self is passed in to check for verbosity ...
1399 $text = &acronym::markup_acronyms($text, $self);
1400
1401 gsprintf($outhandle, " {BasPlug.done_acronym_markup}\n")
1402 if ($self->{'verbosity'} > 2);
1403
1404 return $text;
1405}
1406
1407sub compile_stats {
1408 my $self = shift(@_);
1409 my ($stats) = @_;
1410
1411 $stats->{'num_processed'} += $self->{'num_processed'};
1412 $stats->{'num_not_processed'} += $self->{'num_not_processed'};
1413 $stats->{'num_archives'} += $self->{'num_archives'};
1414
1415}
1416
1417sub associate_cover_image {
1418 my $self = shift(@_);
1419 my ($doc_obj, $filename) = @_;
1420
1421 my $top_section=$doc_obj->get_top_section();
1422
1423 $filename =~ s/\.[^\\\/\.]+$/\.jpg/;
1424 if (-e $filename) {
1425 $doc_obj->associate_file($filename, "cover.jpg", "image/jpeg");
1426 $doc_obj->add_utf8_metadata($top_section, "hascover", 1);
1427 } else {
1428 $filename =~ s/jpg$/JPG/;
1429 if (-e $filename) {
1430 $doc_obj->associate_file($filename, "cover.jpg", "image/jpeg");
1431 $doc_obj->add_utf8_metadata($top_section, "hascover", 1);
1432 }
1433 }
1434}
1435
14361;
Note: See TracBrowser for help on using the repository browser.