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

Last change on this file since 10446 was 10446, checked in by chi, 19 years ago

Modifications for converting windows-1252 to windows_1252.

  • Property svn:keywords set to Author Date Id Revision
File size: 41.9 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 # VB scripting generated Word to HTML file
1010 if ($text =~ /charset=(windows.*)[\"]/ig){
1011 my $vbhtml_encoding = $1;
1012 $vbhtml_encoding =~ s/-+/_/g;
1013 $self->{'input_encoding'} = $vbhtml_encoding;
1014 }
1015
1016 # remove <title>stuff</title> -- as titles tend often to be in English
1017 # for foreign language documents
1018 $text =~ s/<title>(.|\n)*?<\/title>//i;
1019
1020 # remove all HTML tags
1021 # XXX this doesn't match plugins derived from HTMLPlug (except ConvertTo)
1022 if (ref($self) eq 'HTMLPlug' ||
1023 (exists $self->{'converted_to'} && $self->{'converted_to'} eq 'HTML')){
1024 $text =~ s/<[^>]*>//sg;
1025 }
1026
1027 # get the language/encoding
1028 my $results = $self->{'textcat'}->classify(\$text);
1029
1030 # if textcat returns 3 or less possibilities we'll use the
1031 # first one in the list - otherwise use the defaults
1032 if (scalar @$results > 3) {
1033 my $best_encoding="";
1034 if ($unicode_format) { # in case the first had a BOM
1035 $best_encoding=$unicode_format;
1036 } else {
1037 my %guessed_encodings = ();
1038 foreach my $result (@$results) {
1039 $result =~ /([^\-]+)$/;
1040 my $enc=$1;
1041 if (!defined($guessed_encodings{$enc})) {
1042 $guessed_encodings{$enc}=0;
1043 }
1044 $guessed_encodings{$enc}++;
1045 }
1046
1047 $guessed_encodings{""}=-1; # for default best_encoding of ""
1048 foreach my $enc (keys %guessed_encodings) {
1049 if ($guessed_encodings{$enc} >
1050 $guessed_encodings{$best_encoding}){
1051 $best_encoding=$enc;
1052 }
1053 }
1054 }
1055
1056 if ($self->{'input_encoding'} ne 'auto') {
1057 if ($self->{'extract_language'} && ($self->{'verbosity'}>2)) {
1058 gsprintf($outhandle,
1059 "BasPlug: {BasPlug.could_not_extract_language}\n",
1060 $filename, $self->{'default_language'});
1061 }
1062 return ($self->{'default_language'}, $self->{'input_encoding'});
1063
1064 } else {
1065 if ($self->{'verbosity'}>2) {
1066 gsprintf($outhandle,
1067 "BasPlug: {BasPlug.could_not_extract_language}\n",
1068 $filename, $self->{'default_language'});
1069 }
1070 return ($self->{'default_language'}, $best_encoding);
1071 }
1072 }
1073
1074 # format language/encoding
1075 my ($language, $encoding) = $results->[0] =~ /^([^-]*)(?:-(.*))?$/;
1076 if (!defined $language) {
1077 if ($self->{'verbosity'}>2) {
1078 gsprintf($outhandle,
1079 "BasPlug: {BasPlug.could_not_extract_language}\n",
1080 $filename, $self->{'default_language'});
1081 }
1082 $language = $self->{'default_language'};
1083 }
1084 if (!defined $encoding) {
1085 if ($self->{'verbosity'}>2) {
1086 gsprintf($outhandle,
1087 "BasPlug: {BasPlug.could_not_extract_encoding}\n",
1088 $filename, $self->{'default_encoding'});
1089 }
1090 $encoding = $self->{'default_encoding'};
1091 }
1092
1093
1094 # check for equivalents where textcat doesn't have some encodings...
1095 # eg MS versions of standard encodings
1096 if ($encoding =~ /^iso_8859_(\d+)/) {
1097 my $iso = $1; # which variant of the iso standard?
1098 # iso-8859 sets don't use chars 0x80-0x9f, windows codepages do
1099 if ($text =~ /[\x80-\x9f]/) {
1100 # Western Europe
1101 if ($iso == 1 or $iso == 15) { $encoding = 'windows_1252' }
1102 elsif ($iso == 2) { $encoding = 'windows_1250' } # Central Europe
1103 elsif ($iso == 5) { $encoding = 'windows_1251' } # Cyrillic
1104 elsif ($iso == 6) { $encoding = 'windows_1256' } # Arabic
1105 elsif ($iso == 7) { $encoding = 'windows_1253' } # Greek
1106 elsif ($iso == 8) { $encoding = 'windows_1255' } # Hebrew
1107 elsif ($iso == 9) { $encoding = 'windows_1254' } # Turkish
1108 }
1109 }
1110
1111 if ($encoding !~ /^(ascii|utf8|unicode)$/ &&
1112 !defined $encodings::encodings->{$encoding}) {
1113 if ($self->{'verbosity'}) {
1114 gsprintf($outhandle, "BasPlug: {BasPlug.unsupported_encoding}\n",
1115 $filename, $encoding, $self->{'default_encoding'});
1116 }
1117 $encoding = $self->{'default_encoding'};
1118 }
1119
1120 return ($language, $encoding);
1121}
1122
1123# add any extra metadata that's been passed around from one
1124# plugin to another.
1125# extra_metadata uses add_utf8_metadata so it expects metadata values
1126# to already be in utf8
1127sub extra_metadata {
1128 my $self = shift (@_);
1129 my ($doc_obj, $cursection, $metadata) = @_;
1130
1131 foreach my $field (keys(%$metadata)) {
1132 # $metadata->{$field} may be an array reference
1133 if ($field eq "gsdlassocfile_tobe") {
1134 # 'gsdlassocfile_tobe' is artificially introduced metadata
1135 # that is used to signal that certain additional files should
1136 # be tied to this document. Useful in situations where a
1137 # metadata pass in the plugin pipeline works out some files
1138 # need to be associated with a document, but the document hasn't
1139 # been formed yet.
1140
1141 my $equiv_form = "";
1142 foreach my $gaf (@{$metadata->{$field}}) {
1143 my ($full_filename,$mimetype) = ($gaf =~ m/^(.*):(.*):$/);
1144 my ($tail_filename) = ($full_filename =~ /^.*[\/\\](.+?)$/);
1145 my $filename = $full_filename;
1146
1147 $doc_obj->associate_file($full_filename,$tail_filename,$mimetype);
1148
1149 my ($doc_ext) = ($tail_filename =~ m/^.*\.(.*)$/);
1150 my $start_doclink = "<a href=\"_httpcollection_/index/assoc/{Or}{[parent(Top):archivedir],[archivedir]}/$tail_filename\">";
1151 my $srcicon = "_icon".$doc_ext."_";
1152 my $end_doclink = "</a>";
1153
1154 $equiv_form .= " $start_doclink\{If\}{$srcicon,$srcicon,$doc_ext\}$end_doclink";
1155 }
1156 $doc_obj->add_utf8_metadata ($cursection, "equivlink", $equiv_form);
1157 }
1158 elsif (ref ($metadata->{$field}) eq "ARRAY") {
1159 map {
1160 $doc_obj->add_utf8_metadata ($cursection, $field, $_);
1161 } @{$metadata->{$field}};
1162 } else {
1163 $doc_obj->add_utf8_metadata ($cursection, $field, $metadata->{$field});
1164 }
1165 }
1166}
1167
1168# initialise metadata extractors
1169sub initialise_extractors {
1170 my $self = shift (@_);
1171
1172 if ($self->{'extract_acronyms'} || $self->{'markup_acronyms'}) {
1173 &acronym::initialise_acronyms();
1174 }
1175}
1176
1177# finalise metadata extractors
1178sub finalise_extractors {
1179 my $self = shift (@_);
1180
1181 if ($self->{'extract_acronyms'} || $self->{'markup_acronyms'}) {
1182 &acronym::finalise_acronyms();
1183 }
1184}
1185
1186# FIRSTNNN: extract the first NNN characters as metadata
1187sub extract_first_NNNN_characters {
1188 my $self = shift (@_);
1189 my ($textref, $doc_obj, $thissection) = @_;
1190
1191 foreach my $size (split /,/, $self->{'first'}) {
1192 my $tmptext = $$textref;
1193 $tmptext =~ s/^\s+//;
1194 $tmptext =~ s/\s+$//;
1195 $tmptext =~ s/\s+/ /gs;
1196 $tmptext = substr ($tmptext, 0, $size);
1197 $tmptext =~ s/\s\S*$/&#8230;/;
1198 $doc_obj->add_utf8_metadata ($thissection, "First$size", $tmptext);
1199 }
1200}
1201
1202sub extract_email {
1203 my $self = shift (@_);
1204 my ($textref, $doc_obj, $thissection) = @_;
1205 my $outhandle = $self->{'outhandle'};
1206
1207 gsprintf($outhandle, " {BasPlug.extracting_emails}...\n")
1208 if ($self->{'verbosity'} > 2);
1209
1210 my @email = ($$textref =~ m/([-a-z0-9\.@+_=]+@(?:[-a-z0-9]+\.)+(?:com|org|edu|mil|int|net|[a-z][a-z]))/g);
1211 @email = sort @email;
1212
1213# if($self->{"new_extract_email"} == 0)
1214# {
1215# my @email2 = ();
1216# foreach my $address (@email)
1217# {
1218# if (!(join(" ",@email2) =~ m/(^| )$address( |$)/ ))
1219# {
1220# push @email2, $address;
1221# $doc_obj->add_utf8_metadata ($thissection, "emailAddress", $address);
1222# # print $outhandle " extracting $address\n"
1223# &gsprintf($outhandle, " {BasPlug.extracting} $address\n")
1224# if ($self->{'verbosity'} > 3);
1225# }
1226# }
1227# }
1228# else
1229# {
1230 my $hashExistMail = {};
1231 foreach my $address (@email) {
1232 if (!(defined $hashExistMail->{$address}))
1233 {
1234 $hashExistMail->{$address} = 1;
1235 $doc_obj->add_utf8_metadata ($thissection, "emailAddress", $address);
1236 gsprintf($outhandle, " {BasPlug.extracting} $address\n")
1237 if ($self->{'verbosity'} > 3);
1238 }
1239 }
1240 gsprintf($outhandle, " {BasPlug.done_email_extract}\n")
1241 if ($self->{'verbosity'} > 2);
1242}
1243
1244# extract metadata
1245sub auto_extract_metadata {
1246
1247 my $self = shift (@_);
1248 my ($doc_obj) = @_;
1249
1250 if ($self->{'extract_email'}) {
1251 my $thissection = $doc_obj->get_top_section();
1252 while (defined $thissection) {
1253 my $text = $doc_obj->get_text($thissection);
1254 $self->extract_email (\$text, $doc_obj, $thissection) if $text =~ /./;
1255 $thissection = $doc_obj->get_next_section ($thissection);
1256 }
1257 }
1258 if ($self->{'extract_placenames'}) {
1259 my $thissection = $doc_obj->get_top_section();
1260 while (defined $thissection) {
1261 my $text = $doc_obj->get_text($thissection);
1262 $self->extract_placenames (\$text, $doc_obj, $thissection) if $text =~ /./;
1263 $thissection = $doc_obj->get_next_section ($thissection);
1264 }
1265 }
1266
1267 #adding kea keyphrases
1268 if ($self->{'extract_keyphrases'}) {
1269
1270 my $thissection = $doc_obj->get_top_section();
1271 my $text = "";
1272 my $list;
1273
1274 #loop through sections to gather whole doc
1275 while (defined $thissection) {
1276 my $sectiontext = $doc_obj->get_text($thissection);
1277 $text = $text.$sectiontext;
1278 $thissection = $doc_obj->get_next_section ($thissection);
1279 }
1280
1281 if($self->{'extract_keyphrase_options'}) { #if kea options flag is set, call Kea with specified options
1282 $list = &Kea::extract_KeyPhrases ($text, $self->{'extract_keyphrase_options'});
1283 } else { #otherwise call Kea with no options
1284 $list = &Kea::extract_KeyPhrases ($text);
1285 }
1286
1287 if ($list){
1288 # if a list of kea keyphrases was returned (ie not empty)
1289 if ($self->{'verbosity'}) {
1290 gsprintf(STDERR, "{BasPlug.keyphrases}: $list\n");
1291 }
1292
1293 #add metadata to top section
1294 $thissection = $doc_obj->get_top_section();
1295
1296 # add all key phrases as one metadata
1297 $doc_obj->add_metadata($thissection, "Keyphrases", $list);
1298
1299 # add individual key phrases as multiple metadata
1300 foreach my $keyphrase (split(',', $list)) {
1301 $keyphrase =~ s/^\s+|\s+$//g;
1302 $doc_obj->add_metadata($thissection, "Keyphrase", $keyphrase);
1303 }
1304 }
1305 } #end of kea
1306
1307 if ($self->{'first'}) {
1308 my $thissection = $doc_obj->get_top_section();
1309 while (defined $thissection) {
1310 my $text = $doc_obj->get_text($thissection);
1311 $self->extract_first_NNNN_characters (\$text, $doc_obj, $thissection) if $text =~ /./;
1312 $thissection = $doc_obj->get_next_section ($thissection);
1313 }
1314 }
1315
1316 if ($self->{'extract_acronyms'}) {
1317 my $thissection = $doc_obj->get_top_section();
1318 while (defined $thissection) {
1319 my $text = $doc_obj->get_text($thissection);
1320 $self->extract_acronyms (\$text, $doc_obj, $thissection) if $text =~ /./;
1321 $thissection = $doc_obj->get_next_section ($thissection);
1322 }
1323 }
1324
1325 if ($self->{'markup_acronyms'}) {
1326 my $thissection = $doc_obj->get_top_section();
1327 while (defined $thissection) {
1328 my $text = $doc_obj->get_text($thissection);
1329 $text = $self->markup_acronyms ($text, $doc_obj, $thissection);
1330 $doc_obj->delete_text($thissection);
1331 $doc_obj->add_text($thissection, $text);
1332 $thissection = $doc_obj->get_next_section ($thissection);
1333 }
1334 }
1335
1336 if($self->{'extract_historical_years'}) {
1337 my $thissection = $doc_obj->get_top_section();
1338 while (defined $thissection) {
1339
1340 my $text = $doc_obj->get_text($thissection);
1341 &DateExtract::get_date_metadata($text, $doc_obj,
1342 $thissection,
1343 $self->{'no_bibliography'},
1344 $self->{'maximum_year'},
1345 $self->{'maximum_century'});
1346 $thissection = $doc_obj->get_next_section ($thissection);
1347 }
1348 }
1349}
1350
1351# extract acronyms from a section in a document. progress is
1352# reported to outhandle based on the verbosity. both the Acronym
1353# and the AcronymKWIC metadata items are created.
1354
1355sub extract_acronyms {
1356 my $self = shift (@_);
1357 my ($textref, $doc_obj, $thissection) = @_;
1358 my $outhandle = $self->{'outhandle'};
1359
1360 # print $outhandle " extracting acronyms ...\n"
1361 gsprintf($outhandle, " {BasPlug.extracting_acronyms}...\n")
1362 if ($self->{'verbosity'} > 2);
1363
1364 my $acro_array = &acronym::acronyms($textref);
1365
1366 foreach my $acro (@$acro_array) {
1367
1368 #check that this is the first time ...
1369 my $seen_before = "false";
1370 my $previous_data = $doc_obj->get_metadata($thissection, "Acronym");
1371 foreach my $thisAcro (@$previous_data) {
1372 if ($thisAcro eq $acro->to_string()) {
1373 $seen_before = "true";
1374 if ($self->{'verbosity'} >= 4) {
1375 gsprintf($outhandle, " {BasPlug.already_seen} " .
1376 $acro->to_string() . "\n");
1377 }
1378 }
1379 }
1380
1381 if ($seen_before eq "false") {
1382 #write it to the file ...
1383 $acro->write_to_file();
1384
1385 #do the normal acronym
1386 $doc_obj->add_utf8_metadata($thissection, "Acronym", $acro->to_string());
1387 gsprintf($outhandle, " {BasPlug.adding} ".$acro->to_string()."\n")
1388 if ($self->{'verbosity'} > 3);
1389 }
1390 }
1391
1392 gsprintf($outhandle, " {BasPlug.done_acronym_extract}\n")
1393 if ($self->{'verbosity'} > 2);
1394}
1395
1396sub markup_acronyms {
1397 my $self = shift (@_);
1398 my ($text, $doc_obj, $thissection) = @_;
1399 my $outhandle = $self->{'outhandle'};
1400
1401 gsprintf($outhandle, " {BasPlug.marking_up_acronyms}...\n")
1402 if ($self->{'verbosity'} > 2);
1403
1404 #self is passed in to check for verbosity ...
1405 $text = &acronym::markup_acronyms($text, $self);
1406
1407 gsprintf($outhandle, " {BasPlug.done_acronym_markup}\n")
1408 if ($self->{'verbosity'} > 2);
1409
1410 return $text;
1411}
1412
1413sub compile_stats {
1414 my $self = shift(@_);
1415 my ($stats) = @_;
1416
1417 $stats->{'num_processed'} += $self->{'num_processed'};
1418 $stats->{'num_not_processed'} += $self->{'num_not_processed'};
1419 $stats->{'num_archives'} += $self->{'num_archives'};
1420
1421}
1422
1423sub associate_cover_image {
1424 my $self = shift(@_);
1425 my ($doc_obj, $filename) = @_;
1426
1427 my $top_section=$doc_obj->get_top_section();
1428
1429 $filename =~ s/\.[^\\\/\.]+$/\.jpg/;
1430 if (-e $filename) {
1431 $doc_obj->associate_file($filename, "cover.jpg", "image/jpeg");
1432 $doc_obj->add_utf8_metadata($top_section, "hascover", 1);
1433 } else {
1434 $filename =~ s/jpg$/JPG/;
1435 if (-e $filename) {
1436 $doc_obj->associate_file($filename, "cover.jpg", "image/jpeg");
1437 $doc_obj->add_utf8_metadata($top_section, "hascover", 1);
1438 }
1439 }
1440}
1441
14421;
Note: See TracBrowser for help on using the repository browser.