blib/lib/Text/BibTeX/Name.pm

filter: Statementstmt 100% 36 / 36 Branchbran 92.8% 13 / 14 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 90.0% 9 / 10 Podpod 100% 4 / 4 totaltotal 86.6% 65 / 75 CC 11 SCAR 11.8
CC
11
Cov
91%
split
5.2
new
4.0
part
4.0
CRAP 3.3
« blib/lib/Text/BibTeX/NameFormat.pm ↑ Summary blib/lib/Text/BibTeX/Bib.pm »
1 # ----------------------------------------------------------------------
2 # NAME : BibTeX/Name.pm
3 # CLASSES : Text::BibTeX::Name
4 # RELATIONS :
5 # DESCRIPTION: Provides an object-oriented interface to the BibTeX-
6 # style author names (parsing them, that is; formatting
7 # them is done by the Text::BibTeX::NameFormat class).
8 # CREATED : Nov 1997, Greg Ward
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
15 # library is free software; you may redistribute it and/or
16 # modify it under the same terms as Perl itself.
17 # ----------------------------------------------------------------------
18  
19 package Text::BibTeX::Name;
20  
21 18 ▶use 5.026;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 18 our $VERSION = '1.00';
27  
28 18 ▶use Text::BibTeX;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 =encoding UTF-8
31  
32 =head1 NAME
33  
34 Text::BibTeX::Name - interface to BibTeX-style author names
35  
36 =head1 SYNOPSIS
37  
38 use Text::BibTeX::Name;
39  
40 $name = Text::BibTeX::Name->new();
41 $name->split('J. Random Hacker');
42 # or:
43 $name = Text::BibTeX::Name->new('J. Random Hacker');
44  
45 @firstname_tokens = $name->part ('first');
46 $lastname = join (' ', $name->part ('last'));
47  
48 $format = Text::BibTeX::NameFormat->new();
49 # ...customize $format...
50 $formatted = $name->format ($format);
51  
52 =head1 DESCRIPTION
53  
54 C<Text::BibTeX::Name> provides an abstraction for BibTeX-style names and
55 some basic operations on them. A name, in the BibTeX world, consists of
56 a list of I<tokens> which are divided amongst four I<parts>: `first',
57 `von', `last', and `jr'.
58  
59 Tokens are separated by whitespace or commas at brace-level zero. Thus
60 the name
61  
62 van der Graaf, Horace Q.
63  
64 has five tokens, whereas the name
65  
66 {Foo, Bar, and Sons}
67  
68 consists of a single token. Skip down to L<"EXAMPLES"> for more examples, or
69 read on if you want to know the exact details of how names are split into
70 tokens and parts.
71  
72 How tokens are divided into parts depends on the form of the name. If
73 the name has no commas at brace-level zero (as in the second example),
74 then it is assumed to be in either "first last" or "first von last"
75 form. If there are no tokens that start with a lower-case letter, then
76 "first last" form is assumed: the final token is the last name, and all
77 other tokens form the first name. Otherwise, the earliest contiguous
78 sequence of tokens with initial lower-case letters is taken as the `von'
79 part; if this sequence includes the final token, then a warning is
80 printed and the final token is forced to be the `last' part.
81  
82 If a name has a single comma, then it is assumed to be in "von last,
83 first" form. A leading sequence of tokens with initial lower-case
84 letters, if any, forms the `von' part; tokens between the `von' and the
85 comma form the `last' part; tokens following the comma form the `first'
86 part. Again, if there are no tokens following a leading sequence of
87 lowercase tokens, a warning is printed and the token immediately
88 preceding the comma is taken to be the `last' part.
89  
90 A token "starts with a lower-case letter" if its first character is a
91 lowercase letter in the Unicode sense (general category C<Ll>), so
92 C<"van">, C<"de"> and C<"ван"> are all lowercase tokens. A token that
93 starts with a brace is never lowercase: this is how to keep a
94 lowercase token out of the `von' part (C<"{de} Gaulle">), or to make a
95 group of words a single token (C<"{Barnes and Noble}">). TeX special
96 characters at the start of a token (C<"{\'e}tienne">) are treated like
97 any other brace group.
98  
99 If a name has more than two commas, a warning is printed and the name is
100 treated as though only the first two commas were present. Consecutive
101 commas (C<"Smith,, John">) count as one comma.
102  
103 Finally, if a name has two commas, it is assumed to be in "von last, jr,
104 first" form. (This is the only way to represent a name with a `jr'
105 part.) The parsing of the name is the same as for a one-comma name,
106 except that tokens between the two commas are taken to be the `jr' part.
107  
108 Note that BibTeX has no idea that C<"Jr."> is special: C<"Henry Ford
109 Jr."> splits into first name C<"Henry Ford"> and last name C<"Jr.">.
110 Write it as C<"Ford, Jr., Henry"> instead, or, if you don't want the
111 `jr' part to be separate, as C<"Henry {Ford Jr.}">.
112  
113 A name with no last name at all (as in C<", John">) keeps its other
114 parts, and a warning is printed.
115  
116 =head1 CAVEAT
117  
118 The code that does the actual work of splitting up names takes a shortcut
119 and makes few assumptions about whitespace. In particular, there must be
120 no leading whitespace, no trailing whitespace, no consecutive whitespace
121 characters in the string, and no whitespace characters other than space.
122 In other words, all whitespace must consist of lone internal spaces.
123 (Field values from parsed entries are always like this. Other strings
124 are split without error, but the result may not be what you expect.)
125  
126 =head1 EXAMPLES
127  
128 The strings C<"John Smith"> and C<"Smith, John"> are different
129 representations of the same name, so split into parts and tokens the
130 same way, namely as:
131  
132 first => ('John')
133 von => ()
134 last => ('Smith')
135 jr => ()
136  
137 Note that every part is a list of tokens, even if there is only one
138 token in that part; empty parts get empty token lists. Every token is
139 just a string. Writing this example in actual code is simple:
140  
141 $name = Text::BibTeX::Name->new("John Smith"); # or "Smith, John"
142 $name->part ('first'); # returns list ("John")
143 $name->part ('last'); # returns list ("Smith")
144 $name->part ('von'); # returns list ()
145 $name->part ('jr'); # returns list ()
146  
147 (We'll omit the empty parts in the rest of the examples: just assume
148 that any unmentioned part is an empty list.) If more than two tokens
149 are included and there's no comma, they'll go to the first name: thus
150 C<"John Q. Smith"> splits into
151  
152 first => ("John", "Q."))
153 last => ("Smith")
154  
155 and C<"J. R. R. Tolkein"> into
156  
157 first => ("J.", "R.", "R.")
158 last => ("Tolkein")
159  
160 The ambiguous name C<"Kevin Philips Bong"> splits into
161  
162 first => ("Kevin", "Philips")
163 last => ("Bong")
164  
165 which may or may not be the right thing, depending on the particular
166 person. There's no way to know though, so if this fellow's last name is
167 "Philips Bong" and not "Bong", the string representation of his name
168 must disambiguate. One possibility is C<"Philips Bong, Kevin"> which
169 splits into
170  
171 first => ("Kevin")
172 last => ("Philips", "Bong")
173  
174 Alternately, C<"Kevin {Philips Bong}"> takes advantage of the fact that
175 tokes are only split on whitespace I<at brace-level zero>, and becomes
176  
177 first => ("Kevin")
178 last => ("{Philips Bong}")
179  
180 which is fine if your names are destined to be processed by TeX, but
181 might be problematic in other contexts. Similarly, C<"St John-Mollusc,
182 Oliver"> becomes
183  
184 first => ("Oliver")
185 last => ("St", "John-Mollusc")
186  
187 which can also be written as C<"Oliver {St John-Mollusc}">:
188  
189 first => ("Oliver")
190 last => ("{St John-Mollusc}")
191  
192 Since tokens are separated purely by whitespace, hyphenated names will
193 work either way: both C<"Nigel Incubator-Jones"> and C<"Incubator-Jones,
194 Nigel"> come out as
195  
196 first => ("Nigel")
197 last => ("Incubator-Jones")
198  
199 Multi-token last names with lowercase components -- the "von part" --
200 work fine: both C<"Ludwig van Beethoven"> and C<"van Beethoven, Ludwig">
201 parse (correctly) into
202  
203 first => ("Ludwig")
204 von => ("van")
205 last => ("Beethoven")
206  
207 This allows these European aristocratic names to sort properly,
208 i.e. I<van Beethoven> under I<B> rather than I<v>. Speaking of
209 aristocratic European names, C<"Charles Louis Xavier Joseph de la
210 Vall{\'e}e Poussin"> is handled just fine, and splits into
211  
212 first => ("Charles", "Louis", "Xavier", "Joseph")
213 von => ("de", "la")
214 last => ("Vall{\'e}e", "Poussin")
215  
216 so could be sorted under I<V> rather than I<d>. (Note that the sorting
217 algorithm in L<Text::BibTeX::BibSort> is a slavish imitiation of BibTeX
218 0.99, and therefore does the wrong thing with these names: the sort key
219 starts with the "von" part.)
220  
221 However, capitalized "von parts" don't work so well: C<"R. J. Van de
222 Graaff"> splits into
223  
224 first => ("R.", "J.", "Van")
225 von => ("de")
226 last => ("Graaff")
227  
228 which is clearly wrong. This name should be represented as C<"Van de
229 Graaff, R. J.">
230  
231 first => ("R.", "J.")
232 last => ("Van", "de", "Graaff")
233  
234 which is probably right. (This particular Van de Graaff was an
235 American, so he probably belongs under I<V> -- which is where my
236 (British) dictionary puts him. Other Van de Graaff's mileages may
237 vary.)
238  
239 Finally, many names include a suffix: "Jr.", "III", "fils", and so
240 forth. These are handled, but with some limitations. If there's a
241 comma before the suffix (the usual U.S. convention for "Jr."), then the
242 name should be in I<last, jr, first> form, e.g. C<"Doe, Jr., John">
243 comes out (correctly) as
244  
245 first => ("John")
246 last => ("Doe")
247 jr => ("Jr.")
248  
249 but C<"John Doe, Jr."> is ambiguous and is parsed as
250  
251 first => ("Jr.")
252 last => ("John", "Doe")
253  
254 (so don't do it that way). If there's no comma before the suffix -- the
255 usual for Roman numerals, and occasionally seen with "Jr." -- then
256 you're stuck and have to make the suffix part of the last name. Thus,
257 C<"Gates III, William H."> comes out
258  
259 first => ("William", "H.")
260 last => ("Gates", "III")
261  
262 but C<"William H. Gates III"> is ambiguous, and becomes
263  
264 first => ("William", "H.", "Gates")
265 last => ("III")
266  
267 -- not what you want. Again, the curly-brace trick comes in handy, so
268 C<"William H. {Gates III}"> splits into
269  
270 first => ("William", "H.")
271 last => ("{Gates III}")
272  
273 There is no way to make a comma-less suffix the C<jr> part. (This is an
274 unfortunate consequence of slavishly imitating BibTeX 0.99.)
275  
276 Finally, names that aren't really names of people but rather are
277 organization or company names should be forced into a single token by
278 wrapping them in curly braces. For example, "Foo, Bar and Sons" should
279 be written C<"{Foo, Bar and Sons}">, which will split as
280  
281 last => ("{Foo, Bar and Sons}")
282  
283 Of course, if this is one name in a BibTeX C<authors> or C<editors>
284 list, this name has to be wrapped in braces anyways (because of the C<"
285 and ">), but that's another story.
286  
287 =head1 FORMATTING NAMES
288  
289 Putting a split-up name back together again in a flexible, customizable
290 way is the job of another module: see L<Text::BibTeX::NameFormat>.
291  
292 =head1 METHODS
293  
294 =over 4
295  
296 =item new([ [OPTS,] NAME [, FILENAME, LINE, NAME_NUM]])
297  
298 Creates a new C<Text::BibTeX::Name> object. If NAME is supplied, it
299 must be a string containing a single name, and it will be be passed to
300 the C<split> method for further processing. FILENAME, LINE, and
301 NAME_NUM, if present, are all also passed to C<split> to allow better
302 error messages.
303  
304 If the first argument is a hash reference, it is used to define
305 configuration values. At the moment the available values are:
306  
307 =over 4
308  
309 =item BINMODE
310  
311 Set the way Text::BibTeX deals with strings: C<'utf-8'> (the default)
312 to get Perl character strings, or C<'bytes'> for byte strings. See
313 L<Text::BibTeX/"STRINGS AND UNICODE">.
314  
315 =item NORMALIZATION
316  
317 The Unicode normalization form for strings in C<'utf-8'> mode: C<'NFC'>
318 (the default), C<'NFD'>, C<'NFKC'>, C<'NFKD'> or C<'none'>.
319  
320 Text::BibTeX::Name->new(
321 { binmode => 'utf-8', normalization => 'NFD' },
322 "Alberto Simões");
323  
324 =back
325  
326 =cut
327  
328 83 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
329 83 my $class = shift;
330 83 ▶ my $opts = ref $_[0] eq 'HASH' ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 45 38 83
331 83 my ( $name, $filename, $line, $name_num ) = @_;
332  
333 83 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0830
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
334 83 my $self = bless { }, $class;
335 83 @{$self}{qw(binmode normalization)} = Text::BibTeX::_options($opts);
336 83 ▶ $self->split( $name, $filename, $line, $name_num, 1 )
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 79 4 83
337 if ( defined $name );
338 83 $self;
339 }
340  
341  
342 sub DESTROY
343 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=0.0
344 # the C structure kept by `split' is freed with its Perl wrapper
345 }
346  
347  
348 =item split (NAME [, FILENAME, LINE, NAME_NUM])
349  
350 Splits NAME (a string containing a single name) into tokens and
351 subsequently into the four parts of a BibTeX-style name (first, von,
352 last, and jr). (Each part is a list of tokens, and tokens are separated
353 by whitespace or commas at brace-depth zero. See above for full details
354 on how a name is split into its component parts.)
355  
356 The token-lists that make up each part of the name are then stored in
357 the C<Text::BibTeX::Name> object for later retrieval or formatting with
358 the C<part> and C<format> methods.
359  
360 =cut
361  
362 sub split
363 85 ▶{
Subroutine split: called CC=5 SCAR=16.5
Pod documented
364 85 my ($self, $name, $filename, $line, $name_num) = @_;
365  
366 # Call the XSUB with default values if necessary
367 $self->_split (Text::BibTeX::_to_c($self->{binmode} // 'utf-8',
368 85 ▶ $self->{normalization} // 'NFC', $name),
Branch: 2 branches
BranchTrueFalseTotal
defined $line ? : 40 45 85
defined $name_num ? : 40 45 85
Condition50%2/4
$self->{"binmode"} // "utf-8"
l!l
850
$self->{"normalization"} // "NFC"
l!l
850
MC/DC0%0/1
$self->{"binmode"} // "utf-8"
$self->{"binmode"}
MC/DC0%0/1
$self->{"normalization"} // "NFC"
$self->{"normalization"}
Truth table50%1/2
A
A= $self->{"binmode"}
Aresult
11
00
Truth table50%1/2
A
A= $self->{"normalization"}
Aresult
11
00
369 $filename,
370 defined $line ? $line : -1,
371 defined $name_num ? $name_num : -1,
372 1);
373 }
374  
375  
376 =item part (PARTNAME)
377  
378 Returns the list of tokens in part PARTNAME of a name previously split with
379 C<split>. For example, suppose a C<Text::BibTeX::Name> object is created and
380 initialized like this:
381  
382 $name = Text::BibTeX::Name->new();
383 $name->split ('Charles Louis Xavier Joseph de la Vall{\'e}e Poussin');
384  
385 Then this code:
386  
387 $name->part ('von');
388  
389 would return the list C<('de','la')>.
390  
391 =cut
392  
393 61 ▶sub part {
Subroutine part: called CC=4 SCAR=13.9
Pod documented
394 61 my ( $self, $partname ) = @_;
395  
396 61 ▶ croak "unknown name part"
Branch: 1 branch
BranchTrueFalseTotal
unless $partname =~ /^(first|von|last|jr)$/u 0 61 61
397 unless $partname =~ /^(first|von|last|jr)$/;
398 61 ▶ if ( exists $self->{$partname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$partname}) 43 18 61
399 43 my @x = @{ $self->{$partname} };
400 43 ▶ return @x > 1 ? @x : $x[0];
Branch: 1 branch
BranchTrueFalseTotal
@x > 1 ? : 8 35 43
401 }
402 18 return undef;
403 }
404  
405  
406 =item format (FORMAT)
407  
408 Formats a name according to the specifications encoded in FORMAT, which
409 should be a C<Text::BibTeX::NameFormat> (or descendant) object. (In short,
410 it must supply a method C<apply> which takes a C<Text::BibTeX::NameFormat>
411 object as its only argument.) Returns the formatted name as a string.
412  
413 See L<Text::BibTeX::NameFormat> for full details on formatting names.
414  
415 =cut
416  
417 sub format
418 42 ▶{
Subroutine format: called CC=1 SCAR=0.0
Pod documented
419 42 my ($self, $format) = @_;
420  
421 42 $format->apply ($self);
422 }
423  
424 18 1;
425  
426 =back
427  
428 =head1 SEE ALSO
429  
430 L<Text::BibTeX::Entry>, L<Text::BibTeX::NameFormat>,
431 L<Text::BibTeX/split_list>.
432  
433 =head1 AUTHOR
434  
435 Greg Ward <gward@python.net>
436  
437 =head1 COPYRIGHT
438  
439 Copyright (c) 1997-2000 by Gregory P. Ward. All rights reserved. This file
440 is part of the Text::BibTeX library. This library is free software; you
441 may redistribute it and/or modify it under the same terms as Perl itself.
« blib/lib/Text/BibTeX/NameFormat.pm ↑ Summary blib/lib/Text/BibTeX/Bib.pm »