File Coverage

blib/lib/Biblio/Util.pm
Criterion Covered Total %
statement 84 132 63.6
branch 34 86 39.5
condition 5 15 33.3
subroutine 16 20 80.0
pod 6 12 50.0
total 145 265 54.7


line stmt bran cond sub pod time code
1             # --*-Perl-*--
2             # $Id: Util.pm 13 2004-11-27 08:58:44Z tandler $
3             #
4            
5             =head1 NAME
6            
7             Biblio::Util - Package Frontend for bp_util (Perl Bibliography Package)
8            
9             =head1 SYNOPSIS
10            
11             use Biblio::Util;
12            
13             =head1 DESCRIPTION
14            
15             well, I guess it\'s better if you check the source or the original docs
16             for now .... sorry ... ;-)
17            
18             =cut
19            
20             package Biblio::Util;
21 2     2   51 use 5.006;
  2         8  
  2         89  
22 2     2   13 no strict; # for strange AUTOLOAD method call ...
  2         3  
  2         75  
23 2     2   11 use warnings;
  2         11  
  2         234  
24             #use English;
25            
26             # for debug:
27 2     2   12 use Data::Dumper;
  2         1346  
  2         144  
28            
29             BEGIN {
30 2     2   11 use vars qw($Revision $VERSION);
  2         5  
  2         260  
31 2 50   2   3 my $major = 1; q$Revision: 13 $ =~ /: (\d+)/; my ($minor) = ($1); $VERSION = "$major." . ($minor<10 ? '0' : '') . $minor;
  2         17  
  2         7  
  2         57  
32             }
33            
34             # superclass
35             #use YYYY;
36             #use vars qw(@ISA);
37             #@ISA = qw(YYYY);
38            
39             # used modules
40             #use FileHandle;
41             #use File::Basename;
42            
43             # used own modules
44 2     2   11 use Biblio::BP;
  2         4  
  2         4232  
45            
46             =head1 METHODS
47            
48             =over
49            
50             =cut
51            
52             #
53             #
54             # some additional helper functions
55             #
56             #
57            
58             =item $bool = ordnumber($text)
59            
60             Convert ordinal number in text representatin to integer value.
61             Return undef if $text is no ordinal number.
62            
63            
64             =cut
65            
66             our %ordNumbers = qw/
67             first 1
68             second 2
69             third 3
70             fourth 4
71             fifth 5
72             sixth 6
73             seventh 7
74             eighth 8
75             ninth 9
76             /;
77 2     2 1 4 sub ordnumber { my ($text) = @_;
78             # return true (the number), if $text is an ordinal number
79             # check for number (with optional trailing "." or "st" etc.
80 2 50       13 if( $text =~ /^(\d+)(?:\.|st|nd|th)?$/ )
81 0         0 { return $1 }
82             # check for numbers as text (not very elaborated ....)
83 2         9 return $ordNumbers{lc($text)};
84             }
85            
86             =item $defaultCiteKey = defaultCiteKey($rec)
87            
88             Generate Default CiteKey for record in pbib format.
89            
90             =cut
91            
92             sub defaultCiteKey {
93 0     0 1 0 my ($rec, $title_words) = @_;
94 0         0 my @key;
95            
96 0         0 my $project = $rec->{'Project'};
97 0 0       0 if( $project ) {
98 0         0 push @key, $project;
99             } else {
100             # get author
101 0         0 my ($author, $type) = getAuthors($rec);
102             # print STDERR "author: $author ($type) ";
103 0         0 my @authors = split_names($author, $type);
104             # print Dumper \@authors;
105 0 0       0 push @key, last_name($authors[0]) if( @authors );
106             # print STDERR "-> $author\n";
107             }
108            
109             # get year
110 0         0 my $year = $rec->{'Year'};
111 0 0       0 push @key, $year if defined $year;
112            
113             # get title
114 0         0 my $title = $rec->{'Title'};
115 0         0 $title =~ s/\-//ig; # join compond words
116 0         0 $title =~ s/[^a-zäöüÄÖÜßáéíóúàèìòùâêîôû]/ /ig;
117 0         0 my @words = split(/\s+/, $title);
118 0   0     0 while( @words &&
      0        
119             (
120             ! defined $words[0] ||
121             lc($words[0]) =~ /^(the|in(side|to)?|on(to)?|for|from|of+|with(out)?|an?|over)$/ ||
122             length($words[0]) < 4
123             )
124             ) {
125 0         0 shift @words;
126             }
127 0 0       0 $title_words = 1 unless defined $title_words;
128 0         0 for( my $i = $title_words; $i > 0; $i--) {
129 0 0       0 push @key, ucfirst(shift @words) if( @words );
130             }
131            
132             # combine everything ...
133 0         0 return join("-", @key);
134             }
135            
136             =item ($author, $author_type) = getAuthors($rec)
137            
138             =cut
139            
140             sub getAuthors {
141 0     0 1 0 my ($rec) = @_;
142             # based on bp's genkey
143 0 0       0 defined $rec->{'Authors'} &&
144             return ($rec->{'Authors'}, 'names');
145 0 0       0 defined $rec->{'CorpAuthor'} &&
146             return ($rec->{'CorpAuthor'}, 'org');
147 0 0       0 defined $rec->{'Editors'} &&
148             return ($rec->{'Editors'}, 'names');
149 0 0       0 defined $rec->{'Publisher'} &&
150             return ($rec->{'Publisher'}, 'org');
151 0 0       0 defined $rec->{'Organization'} &&
152             return ($rec->{'Organization'}, 'org');
153 0         0 return ("Anonymous", 'text');
154             }
155            
156             =item split_names($names_string, $type)
157            
158             $type = 'names': the string contains names
159             $type = 'org': the string contains a company
160             $type = 'xname': return the advanced name array
161            
162             =cut
163            
164             sub split_names {
165 348     348 1 492 my ($names, $type) = @_;
166 348 100       578 return () unless defined($names);
167 346 50 66     1010 return ($names) if defined($type) && $type !~ /name/i;
168 346 100 66     839 my $xname_flag = 1 if defined($type) && $type eq 'xname';
169            
170 346         308 my $etal;
171 346 50       1436 if( $names =~ s/\s+et.?\s+al.*\s*$//i ) {
172 0         0 $etal = 1;
173             }
174             # support for bibtex "and others"
175 346 50       1030 if( $names =~ s/\s+and\s+others\s*$//i ) {
176 0         0 $etal = 1;
177             }
178            
179             ##### ToDo: remove ' and ', or ', and ' etc. -- does this work now?
180             # treat all ";" as "," ... in the future, I could think about a more sophisticated
181             # treatment of names that, e.g., allows to explicitely separate
182             # the parts of a name
183             # as well similar to bibliographix or bp
184 346         390 $names =~ s/;/,/g;
185             # replace " and " / ", and " / etc. with plain ","
186 346         3937 $names =~ s/(?:\s+|\s*,\s*)and\s+/,/ig;
187             # strip spaces around ","
188 346         1646 $names =~ s/\s*,\s*/,/g;
189 346         1765 my @n_arr = split(/\s*,\s*/, $names);
190             # print Dumper @n_arr;
191 346         823 @n_arr = map(split_nameparts($_, $xname_flag), @n_arr);
192 346 50       659 push @n_arr, "et al." if $etal;
193 346         1208 return @n_arr;
194             }
195            
196             =item @parts = split_nameparts($name_string, $xname_flag)
197            
198             e.g.
199             /John/von/Jones/Jr./
200             /Ed/Krol/
201             /Ludwig/von/Beethoven/
202             /Frederick P.//Brooks/Jr./
203             /Sandra/Da Campo/ -- a space within the last name
204             /Dan R.//Olsen/Jr./ -- middle initial / several firstnames
205             i.e.
206             1 -> "/Company/"
207             2 -> "/Firstnames/Lastname/"
208             3 -> hm ... "/Firstnames/von/Lastname/"
209             4 -> "/Firstnames/von/Lastname/Jr/"
210            
211             if $xname_flag is undef or 0
212             return: [firstnames .... "von last, Jr."]
213             --> no separate handling of "von" and "Jr." possible.
214             --> ["one-name-only"] = company
215             --> "et al." = and others
216            
217             if $xname_flag is set
218             return: [firstnames, [von, last, jr]] in case 4
219             or [firstnames, [von, last]] in case 3
220             or [firstnames, [last]] in case 2
221             or [company] in case 1
222            
223             =cut
224            
225             sub split_nameparts {
226 892     892 1 1020 my ($name, $xname_flag) = @_;
227            
228             # etal handling
229 892 50       1499 return "et al." if $name eq "others"; #nobody can be named "others" :-)
230 892 50       2613 return "et al." if $name =~ /"^et\s+al\.?$"/;
231            
232             ### ToDo: use some bibtex heuristic to look for
233             ### "jr.", "von", "da" etc.
234 892 100       4603 return [split(/\s+/, $name)] if $name !~ /^\/.+\/$/;
235            
236             # parse the formatted name string
237 6         27 my @parts = split(/\//, $name);
238 6         13 shift @parts; # the first one is always empty.
239             # print STDERR Dumper([@parts]);
240 6 50       19 return [] unless @parts; # error, no name. that's strange ...
241 6         9 my $first = shift(@parts);
242 6 50       15 return [$first] unless @parts; # 1: compary name
243 6 50       37 my $von = shift(@parts) if scalar(@parts) > 1; # 3,4: von
244 6 50       22 my $jr = pop(@parts) if scalar(@parts) > 1; # 4: Jr
245 6         13 my $last = pop(@parts); # 2,3,4: last
246            
247             # print STDERR "$name -> ";
248             # print STDERR "von: <$von> " if $von;
249             # print STDERR "last: <$last> " if $last;
250             # print STDERR "jr: <$jr> " if $jr;
251             # print STDERR "first(s): <$firsts> ";
252            
253 6         17 my @firsts = split(/\s+/, $first);
254            
255 6 100       14 if( $xname_flag ) {
256 2         13 return [@firsts, [$von, $last, $jr]];
257             }
258            
259             # now assemble the name array
260 4 50       13 $last = "$von $last" if $von;
261 4 50       13 $last = "$last, $jr" if $jr;
262             # print STDERR "-> last: <$last>\n";
263            
264 4         17 return [@firsts, $last];
265             }
266            
267             =item @parts = split_namepartsold($name_string)
268            
269             /Jones/von/John/Jr./,/Krol/Ed/,/Beethoven/von/Ludwig/
270             i.e.
271             2 "/" -> "/Company/"
272             3 "/" -> "/Lastname/Firstnames/"
273             4 "/" -> hm ... "/Lastname/von/Firstnames/"
274             5 "/" -> "/Lastname/von/Firstnames/Jr/"
275            
276             currently return: [firstnames .... "von last, Jr."]
277             --> no separate handling of "von" and "Jr." possible.
278             --> ["one-name-only"] = company
279             --> ["et al."] = and others
280            
281             =cut
282            
283             sub split_namepartsold {
284 0     0 1 0 my ($name) = @_;
285 0 0       0 return [split(/\s+/, $name)] if $name !~ /^\/.+\/$/;
286            
287             # parse the formatted name string
288 0         0 my @parts = split(/\//, $name);
289 0         0 shift @parts; # the first one is always empty.
290             # print STDERR Dumper([@parts]);
291 0 0       0 return [] unless @parts; # error, no name. that's strange ...
292 0         0 my $last = shift(@parts);
293 0 0       0 return [$last] unless @parts; # compary name
294 0 0       0 my $von = shift(@parts) if scalar(@parts) > 1;
295 0 0       0 my $jr = pop(@parts) if scalar(@parts) > 1;
296            
297             # print STDERR "$name -> ";
298             # print STDERR "von: <$von> " if $von;
299             # print STDERR "last: <$last> " if $last;
300             # print STDERR "jr: <$jr> " if $jr;
301             # print STDERR "first(s): <@parts> ";
302            
303             # now assemble the name array
304 0 0       0 $last = "$von $last" if $von;
305 0 0       0 $last = "$last, $jr" if $jr;
306             # print STDERR "-> last: <$last>\n";
307            
308 0         0 return [split(/\s+/, $parts[0]), $last];
309             }
310            
311 8     8 0 12 sub num_names { my ($names) = @_;
312 8         18 return scalar(split_names($names));
313             }
314 304     304 0 293 sub first_names { my ($name_array) = @_;
315 304 50       616 return undef if $name_array eq "et al.";
316 304         295 my @names = @{$name_array};
  304         642  
317 304         339 pop @names;
318 304         709 return @names;
319             }
320 304     304 0 347 sub first_initials { my ($name_array, $initials_space) = @_;
321             # $initials_space if true add a space between initials,
322             # otherwise they are directly concatenated
323 304         500 my @first = first_names($name_array);
324 304 100       586 return undef unless @first;
325             # use each first character as initial and add dot:
326 302         1134 @first = map( substr($_, 0, 1) . '.', @first);
327 302 50       618 return @first if wantarray;
328 302 50       1084 return join($initials_space ? ' ' : '', @first);
329             }
330            
331 726     726 0 769 sub last_name { my ($name_array) = @_;
332             # my $num_names = scalar(@{$name_array});
333             # return $name_array->[$num_names - 1];
334 726 50       1544 return "et al." if $name_array eq "et al.";
335 726         934 my $last = $name_array->[-1];
336 726 100       2971 return $last unless ref($last) eq 'ARRAY';
337 2         9 return $last->[1];
338             }
339            
340             sub join_and_list {
341 178     178 0 246 my $n = scalar(@_);
342 178 100       541 return "@_" if $n < 2;
343 112 100       363 return "$_[0] and $_[1]" if $n == 2;
344 56         62 my $last = pop(@_);
345 56         420 return join(", ", @_) . ", and $last";
346             }
347            
348 64     64 0 81 sub multi_page_check { my ($pages) = @_;
349             # return true, if more then one page
350 64         316 my @pp = split(/[,;:-]/, $pages);
351 64   33     397 return scalar(@pp) > 1 || $pages =~ /f$/;
352             }
353            
354             =back
355            
356             =cut
357            
358            
359             #
360             #
361             # bp'util methods
362             #
363             #
364            
365             our $AUTOLOAD;
366             sub AUTOLOAD {
367             # my $self = shift;
368 0     0     my ($method) = $AUTOLOAD;
369 0           my (@parameters) = @_;
370 0           $method =~ s/.*:://;
371 0           $method = "bp_util'$method";
372 0           &bib'debugs("call method $method", 2);
373             #print "self = $self call <$method> args: <@parameters>\n";
374 0           &$method(@parameters);
375             }
376            
377             1;
378            
379            
380             __END__