blib/lib/Text/BibTeX/BibSort.pm

filter: Statementstmt 84.6% 55 / 65 Branchbran 61.1% 11 / 18 Conditioncond 62.5% 5 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 80.0% 8 / 10 Podpod 16.6% 1 / 6 totaltotal 72.3% 81 / 112 CC 14 SCAR 17.1
CC
14
Cov
78%
sort_key
10.2
format_alt_fields
4.4
sort_format_names
3.0
CRAP 5.5
« blib/lib/Text/BibTeX/BibFormat.pm ↑ Summary blib/lib/Text/BibTeX/NameFormat.pm »
1 # ----------------------------------------------------------------------
2 # NAME : BibSort.pm
3 # CLASSES : Text::BibTeX::BibSort
4 # RELATIONS : sub-class of StructuredEntry
5 # super-class of BibEntry
6 # DESCRIPTION: Provides methods for generating sort keys of entries
7 # in a BibTeX-style bibliographic database.
8 # CREATED : 1997/11/24, GPW (taken from Bib.pm)
9 # MODIFIED :
10 # VERSION : $Id$
11 # COPYRIGHT : Copyright (c) 1997-2000 by Gregory P. Ward. All rights
12 # reserved.
13 #
14 # This file is part of the Text::BibTeX library. This is free
15 # software; you can redistribute it and/or modify it under the
16 # same terms as Perl itself.
17 # ----------------------------------------------------------------------
18  
19 package Text::BibTeX::BibSort;
20 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use Text::BibTeX::Structure;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 our @ISA = qw(Text::BibTeX::StructuredEntry);
25 1 our $VERSION = '1.00';
26  
27 1 ▶use Text::BibTeX qw(purify_string change_case);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 =head1 NAME
32  
33 Text::BibTeX::BibSort - generate sort keys for bibliographic entries
34  
35 =head1 SYNOPSIS
36  
37 # Assuming $entry comes from a database of the 'Bib' structure
38 # (i.e., that it's blessed into the BibEntry class, which inherits
39 # the sort_key method from BibSort):
40 $sort_key = $entry->sort_key;
41  
42 =head1 DESCRIPTION
43  
44 C<Text::BibTeX::BibSort> is a base class of C<Text::BibTeX::BibEntry>
45 for generating sort keys from bibliography entries. It could in
46 principle (and, someday, might) offer a wide range of highly
47 customizable sort-key generators. Currently, though, it provides only a
48 single method (C<sort_key>) for public use, and that method only pays
49 attention to one structure option, C<sortby>.
50  
51 =head1 METHODS
52  
53 =over 4
54  
55 =item sort_key ()
56  
57 Generates a sort key for a single bibliographic entry. Assumes this
58 entry conforms to the C<Bib> database structure. The nature of this
59 sort key is controlled by the C<sortby> option, which can be either
60 C<"name"> or C<"year">. (The C<namestyle> also has a role, in
61 determining how author/editor names are formatted for inclusion in the
62 sort key.)
63  
64 For by-name sorting (which is how BibTeX's standard styles work), the sort
65 key consists of one of the C<author>, C<editor>, C<organization>, or C<key>
66 fields (depending on the entry type and which fields are actually present),
67 followed by the year and the title. All fields are drastically simplified
68 to produce the sort key: non-English letters are mercilessly anglicized,
69 non-alphabetic characters are stripped, and everything is forced to
70 lowercase. (The first two steps are done by the C<purify_string> routine;
71 see L<Text::BibTeX/purify_string> for all the gory details.)
72  
73 =cut
74  
75 # methods for sorting -- everything here is geared towards generating
76 # a sort key; it's up to external code to actually order entries (since,
77 # of course, a single entry doesn't know anything about any other
78 # entries!)
79  
80 # also, we assume that an entry has been checked and coerced into
81 # shape -- that way we don't need to check for defined-ness of
82 # strings, or check the type, or anything.
83  
84 sub sort_key
85 4 ▶{
Subroutine sort_key: called CC=9 SCAR=23.2
Pod documented
86 4 my $self = shift;
87 4 my ($sortby, $type, $nkey, $skey);
88  
89 4 $sortby = $self->structure->get_options ('sortby');
90 4 ▶ croak ("BibSort::sort_key: sortby option is 'none'")
Branch: 1 branch
BranchTrueFalseTotal
if $sortby eq "none" 0 4 4
91 if $sortby eq 'none';
92 4 ▶ croak ("BibSort::sort_key: unknown sortby option '$sortby'")
Branch: 1 branch
BranchTrueFalseTotal
unless $sortby eq "name" or $sortby eq "year" 0 4 4
Condition66%2/3
$sortby eq "name" or $sortby eq "year"
l!l&&r!l&&!r
220
MC/DC0%0/2
$sortby eq "name" or $sortby eq "year"
$sortby eq "name" $sortby eq "year"
Truth table66%2/3
A or B
A= $sortby eq "name"B= $sortby eq "year"
ABresult
1X1
011
000
93 unless $sortby eq 'name' || $sortby eq 'year';
94  
95 4 $type = $self->type;
96  
97 4 ▶ if ($type eq 'book' || $type eq 'inbook')
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'book' or $type eq 'inbook') { } 2 2 4
elsif ($type eq 'proceedings') { } 0 2 2
elsif ($type eq 'manual') { } 0 2 2
Condition66%2/3
$type eq "book" or $type eq "inbook"
l!l&&r!l&&!r
202
MC/DC50%1/2
$type eq "book" or $type eq "inbook"
$type eq "book" $type eq "inbook"
Truth table66%2/3
A or B
A= $type eq "book"B= $type eq "inbook"
ABresult
1X1
011
000
98 {
99 2 $nkey = $self->format_alt_fields ('author' => 'sort_format_names',
100 'editor' => 'sort_format_names',
101 'key' => 'sortify');
102 }
103 elsif ($type eq 'proceedings')
104 {
105 0 $nkey = $self->format_alt_fields ('editor' => 'sort_format_names',
106 'organization' => 'sort_format_org',
107 'key' => 'sortify');
108 }
109 elsif ($type eq 'manual')
110 {
111 0 $nkey = $self->format_alt_fields ('author' => 'sort_format_names',
112 'organization' => 'sort_format_org',
113 'key' => 'sortify');
114 }
115 else
116 {
117 2 $nkey = $self->format_alt_fields ('author' => 'sort_format_names',
118 'key' => 'sortify');
119 }
120  
121 4 my $ykey = change_case ('l', (purify_string ($self->get ('year'))));
122 4 ▶ $skey = ($sortby eq 'name')
Branch: 1 branch
BranchTrueFalseTotal
$sortby eq 'name' ? : 2 2 4
123 ? $nkey . ' ' . $ykey
124 : $ykey . ' ' . $nkey;
125 4 $skey .= ' ' . $self->sort_format_title ('title');
126 4 return $skey;
127  
128 } # sort_key
129  
130  
131 sub sortify
132 0 ▶{
Subroutine sortify: not called CC=1 SCAR=6.9
Pod undocumented
133 0 my ($self, $field) = @_;
134 0 return lc (purify_string ($self->get ($field)));
135 }
136  
137  
138 sub sort_format_names
139 4 ▶{
Subroutine sort_format_names: called CC=3 SCAR=11.0
Pod undocumented
140 4 require Text::BibTeX::Name;
141 4 require Text::BibTeX::NameFormat;
142  
143 4 my ($self, $field) = @_;
144 4 my ($abbrev, $format, $name);
145  
146 4 $abbrev = ! ($self->structure->get_options ('namestyle') eq 'full');
147 4 $format = Text::BibTeX::NameFormat->new ("vljf", $abbrev);
148 4 $name = Text::BibTeX::Name->new;
149  
150 4 my (@snames, $i, $sname);
151 4 @snames = $self->split ($field);
152 4 for $i (0 .. $#snames)
153 {
154 6 $sname = $snames[$i];
155 6 ▶ if ($sname eq 'others') # hmmm... should we only do this on
Branch: 1 branch
BranchTrueFalseTotal
if ($sname eq 'others') { } 0 6 6
156 { # the final name?
157 0 $sname = 'et al'; # purified version of "et. al."
158 }
159 else
160 {
161 # A spot of ugliness here:
162 # - lc (purify_string (x)) ought to be sortify (x), but I have
163 # already made sortify a method that only operates on a field,
164 # rather than a generic function (as it is in BibTeX)
165
166 6 $name->split ($sname, $self->filename, $self->line ($field), $i+1);
167 6 $sname = $name->format ($format);
168 # print "s_f_n: about to purify >$sname<\n";
169 6 $snames[$i] = lc (purify_string ($sname));
170 }
171 }
172 4 return join (' ', @snames);
173 }
174  
175  
176  
177 # sort_format_org and sort_format_title are suspiciously similar...
178 # could probably have one method to handle both tasks...
179  
180 sub sort_format_org
181 0 ▶{
Subroutine sort_format_org: not called CC=1 SCAR=6.9
Pod undocumented
182 0 my ($self, $field) = @_;
183  
184 0 my $value = $self->get ($field);
185 0 $value =~ s/^the\b\s*//i;
186 0 return lc (purify_string ($value));
187 }
188  
189  
190 sub sort_format_title
191 4 ▶{
Subroutine sort_format_title: called CC=1 SCAR=0.0
Pod undocumented
192 4 my ($self, $field) = @_;
193  
194 4 my $value = $self->get ($field);
195 4 $value =~ s/^(the|an?)\b\s*//i;
196 4 return lc (purify_string ($value));
197 }
198  
199  
200 # Hmm, I suspect format_alt_fields is a little more general purpose --
201 # probably belongs outside of the "generate sort key" methods.
202 # (Or.... does it maybe belong in one of the base classes, StructuredEntry
203 # or even Entry?)
204  
205 sub format_alt_fields
206 4 ▶{
Subroutine format_alt_fields: called CC=4 SCAR=14.8
Pod undocumented
207 4 my $self = shift;
208 4 my ($field, $method);
209  
210 4 ▶ while (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 0 4
211 {
212 4 ($field, $method) = (shift, shift);
213 4 ▶ if ($self->exists ($field))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->exists($field)) 4 0 4
214 {
215 4 ▶ $method = $self->can ($method)
Condition50%1/2
$self->can($method) || croak('unknown method in class ' . ref($self))
l!l
40
MC/DC0%0/1
$self->can($method) || croak('unknown method in class ' . ref($self))
$self->can($method)
Truth table50%1/2
A
A= $self->can($method)
Aresult
11
00
216 || croak ("unknown method in class " . (ref $self));
217 4 return &$method ($self, $field);
218 }
219 }
220  
221 0 return undef; # whoops, none of the alternate fields
222 # were present
223 }
224  
225 1 1;
226  
227 =back
228  
229 =head1 SEE ALSO
230  
231 L<Text::BibTeX::Structure>, L<Text::BibTeX::Bib>,
232 L<Text::BibTeX::BibFormat>
233  
234 =head1 AUTHOR
235  
236 Greg Ward <gward@python.net>
237  
238 =head1 COPYRIGHT
239  
240 Copyright (c) 1997-2000 by Gregory P. Ward. All rights reserved. This file
241 is part of the Text::BibTeX library. This library is free software; you
242 may redistribute it and/or modify it under the same terms as Perl itself.
« blib/lib/Text/BibTeX/BibFormat.pm ↑ Summary blib/lib/Text/BibTeX/NameFormat.pm »