blib/lib/Text/BibTeX/NameFormat.pm

filter: Statementstmt 100% 33 / 33 Branchbran 100% 8 / 8 Conditioncond 46.1% 6 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 88.8% 8 / 9 Podpod 100% 4 / 4 totaltotal 79.7% 59 / 74 CC 11 SCAR 15.0
CC
11
Cov
87%
apply
7.5
new
5.1
BEGIN
1.0
CRAP 4.5
« blib/lib/Text/BibTeX/BibSort.pm ↑ Summary blib/lib/Text/BibTeX/Name.pm »
1 # ----------------------------------------------------------------------
2 # NAME : BibTeX/NameFormat.pm
3 # CLASSES : Text::BibTeX::NameFormat
4 # RELATIONS :
5 # DESCRIPTION: Provides a way to format already-parsed BibTeX-style
6 # author names. (The parsing is done by the
7 # Text::BibTeX:Name 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::NameFormat;
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 =head1 NAME
29  
30 Text::BibTeX::NameFormat - format BibTeX-style author names
31  
32 =head1 SYNOPSIS
33  
34 use Text::BibTeX::NameFormat;
35  
36 $format = Text::BibTeX::NameFormat->($parts, $abbrev_first);
37  
38 $format->set_text ($part,
39 $pre_part, $post_part,
40 $pre_token, $post_token);
41  
42 $format->set_options ($part, $abbrev, $join_tokens, $join_part
43  
44 ## Uses the encoding/binmode and normalization form stored in $name
45 $formatted_name = $format->apply ($name);
46  
47 =head1 DESCRIPTION
48  
49 After splitting a name into its components parts (represented as a
50 C<Text::BibTeX::Name> object), you often want to put it back together
51 again as a single string formatted in a consistent way.
52 C<Text::BibTeX::NameFormat> provides a very flexible way to do this,
53 generally in two stages: first, you create a "name format" which
54 describes how to put the tokens and parts of any name back together, and
55 then you apply the format to a particular name.
56  
57 The "name format" is encapsulated in a C<Text::BibTeX::NameFormat>
58 object. The constructor (C<new>) includes some clever behind-the-scenes
59 trickery that means you can usually get away with calling it alone, and
60 not need to do any customization of the format object. If you do need
61 to customize the format, though, the C<set_text()> and C<set_options()>
62 methods provide that capability.
63  
64 =head1 CONSTANTS
65  
66 Two sets of constants are used with name formats. The "name part"
67 constants name the four "parts" of a BibTeX name: C<BTN_FIRST>,
68 C<BTN_VON>, C<BTN_LAST>, and C<BTN_JR>. The "join method" constants
69 are the ways in which adjacent tokens can be joined together:
70 C<BTJ_MAYTIE>, C<BTJ_SPACE>, C<BTJ_FORCETIE>, and C<BTJ_NOTHING> (see
71 L</"FORMATTING RULES">). Both sets of values may be
72 imported from the C<Text::BibTeX> module, using the import tags
73 C<nameparts> and C<joinmethods>. For instance:
74  
75 use Text::BibTeX qw(:nameparts :joinmethods);
76 use Text::BibTeX::Name;
77 use Text::BibTeX::NameFormat;
78  
79 The "name part" constants are used to specify surrounding text or
80 formatting options on a per-part basis: for instance, you can supply the
81 "pre-token" text, or the "abbreviate" flag, for a single part without
82 affecting other parts. The "join methods" are two of the three
83 formatting options that you can set for a part: you can control how to
84 join the individual tokens of a name (C<"JR Smith">, or C<"J R Smith">,
85 or C<"J~R Smith">, and you can control how the final token of one part
86 is joined to the next part (C<"la Roche"> versus C<"la~Roche">).
87  
88 =head1 FORMATTING RULES
89  
90 A name format controls the following:
91  
92 =over 4
93  
94 =item *
95  
96 which name parts are printed, and in what order (e.g. "first von last
97 jr", or "von last jr first")
98  
99 =item *
100  
101 the text that precedes and follows each part (e.g. if the first name
102 follows the last name, you probably want a comma before the `first'
103 part: "Smith, John" rather than "Smith John")
104  
105 =item *
106  
107 the text that precedes and follows each token (e.g. if the first name is
108 abbreviated, you may want a period after each token: "J. R. Smith"
109 rather than "J R Smith")
110  
111 =item *
112  
113 whether the tokens of each part are abbreviated
114  
115 =item *
116  
117 the method used to join the tokens of each part together
118  
119 =item *
120  
121 the method used to join each part to the following part
122  
123 =back
124  
125 There are I<two> join methods for each part: one between the tokens
126 within the part, and one between the part and the next part. The first
127 allows you, for example, to change C<"J R Smith"> (first name
128 abbreviated with no post-token text but tokens joined by a space) to
129 C<"JR Smith"> (the same, but first-name tokens jammed together). The
130 second is mainly used to ensure that `von' and `last' parts may be
131 joined with a tie: C<"de~Roche"> rather than C<"de Roche">.
132  
133 The join methods are:
134  
135 =over 4
136  
137 =item C<BTJ_MAYTIE>
138  
139 Insert a "discretionary tie" between tokens: either a space or a "tie"
140 is inserted, depending on context. (A tie, otherwise known as an
141 unbreakable space, is always C<"~">, as in TeX.)
142  
143 =item C<BTJ_SPACE>
144  
145 Always insert a space between tokens.
146  
147 =item C<BTJ_FORCETIE>
148  
149 Always insert a tie (C<"~">) between tokens.
150  
151 =item C<BTJ_NOTHING>
152  
153 Insert nothing between tokens---just jam them together.
154  
155 =back
156  
157 With C<BTJ_MAYTIE>, the choice between a space and a tie follows BibTeX.
158 Within a part, a tie is used after the first token if it is less than
159 three characters long, and before the last token. Between parts, a tie
160 is used only if the preceding part consists of a single token less than
161 three characters long. In all other cases, a space is used.
162  
163 =head2 Defaults
164  
165 C<new> sets up the whole format, based on the PARTS and ABBREV_FIRST
166 arguments, so that customization is rarely needed:
167  
168 =over 4
169  
170 =item *
171  
172 The token join method is C<BTJ_MAYTIE> for all parts.
173  
174 =item *
175  
176 The part join method is C<BTJ_SPACE> for the `first', `last', and `jr'
177 parts. If the `von' part immediately precedes the `last' part (which
178 will almost always be the case), C<BTJ_MAYTIE> is used to join `von' to
179 `last'; otherwise `von' also gets C<BTJ_SPACE>.
180  
181 =item *
182  
183 The `von', `last', and `jr' parts are not abbreviated; the `first' part
184 is abbreviated if ABBREV_FIRST is true.
185  
186 =item *
187  
188 All the surrounding text starts out empty. If ABBREV_FIRST is true, the
189 post-token text for the first name is C<".">, which changes
190 C<"J R Smith"> to C<"J. R. Smith">.
191  
192 If `jr' immediately follows `last' (almost always the case), the
193 pre-part text for `jr' is C<", "> and the part join method for `last' is
194 C<BTJ_NOTHING>, so you get C<"John Smith, Jr">. Because the C<", "> is
195 associated with the `jr' part, it is not printed when there is no `jr'
196 part.
197  
198 Likewise, if `first' immediately follows `jr' or `last' (as in
199 "last name first" formats), the pre-part text for `first' is C<", ">
200 and the part join method of the preceding part is C<BTJ_NOTHING>.
201  
202 =back
203  
204 So if you only need C<"fvlj"> and C<"vljf"> part orders, only want to
205 abbreviate the first name, and want periods after abbreviated tokens,
206 ties in the "right" places and commas in the conventional places, you
207 don't need to customize anything. For something out of the
208 ordinary---for instance, abbreviated tokens jammed together with no
209 punctuation, or abbreviated last names---use C<set_text> and
210 C<set_options>.
211  
212 =head1 METHODS
213  
214 =over 4
215  
216 =item new(PARTS, ABBREV_FIRST)
217  
218 Creates a new name format, with the two most common customizations: which
219 parts to include (and in what order), and whether to abbreviate the first
220 name. PARTS should be a string with at most four characters, one representing
221 each part that you want to occur in a formatted name (defaults to C<"fvlj">).
222 For example, C<"fvlj"> means to format names in "first von last jr" order,
223 while C<"vljf"> denotes "von last jr first." ABBREV_FIRST is just a boolean
224 value: false to print out the first name in full, and true to abbreviate it
225 with periods after each token and discretionary ties between tokens (defaults
226 to false). All intra- and inter-token punctuation and spacing is independently
227 controllable with the C<set_text> and C<set_options> methods, although these
228 will rarely be necessary---sensible defaults are chosen for everything, based
229 on the PARTS and ABBREV_FIRST values that you supply. See L</Defaults>
230 for full details of the choices made.
231  
232 =cut
233  
234 sub new
235 44 ▶{
Subroutine new: called CC=5 SCAR=16.4
Pod documented
236 44 my ($class, $parts, $abbrev_first) = @_;
237  
238 44 ▶ $parts ||= "fvlj";
Condition50%1/2
$parts ||= "fvlj"
l!l
440
MC/DC0%0/1
$parts ||= "fvlj"
$parts
Truth table50%1/2
A
A= $parts
Aresult
11
00
239 44 ▶ $abbrev_first = defined($abbrev_first)? $abbrev_first : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $abbrev_first ? : 41 3 44
240  
241 44 ▶ croak "invalid name format \"$parts\"" unless $parts =~ /^[fvlj]{1,4}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $parts =~ /^[fvlj]{1,4}$/u 1 43 44
242  
243 43 ▶ $class = ref ($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0430
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
244 43 my $self = bless {}, $class;
245 43 $self->{_cstruct} = create ($parts, $abbrev_first);
246 43 $self;
247 }
248  
249  
250 sub DESTROY
251 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=0.0
252 # the C structure is freed with its Perl wrapper
253 }
254  
255  
256 =item set_text (PART, PRE_PART, POST_PART, PRE_TOKEN, POST_TOKEN)
257  
258 Allows you to customize some or all of the surrounding text for a single
259 name part. Every name part has four possible chunks of text that go
260 around or within it: before/after the part as a whole, and before/after
261 each token in the part. For instance, if you are abbreviating first
262 names and wish to control the punctuation after each token in the first
263 name, you would set the "post token" text:
264  
265 $format->set_text ('first', undef, undef, undef, '');
266  
267 would set the post-token text to the empty string, resulting in names
268 like C<"J R Smith">. (Normally, abbreviated first names will have a
269 period after each token: C<"J. R. Smith">.) Note that supplying
270 C<undef> for the other three values leaves them unchanged.
271  
272 See L</"FORMATTING RULES"> for full information on formatting names.
273  
274 =cut
275  
276 sub set_text
277 11 ▶{
Subroutine set_text: called CC=1 SCAR=0.0
Pod documented
278 11 my ($self, $part, $pre_part, $post_part, $pre_token, $post_token) = @_;
279  
280 # The C code keeps its own copies of these strings
281 11 _set_text ($self->{'_cstruct'},
282 $part,
283 $pre_part,
284 $post_part,
285 $pre_token,
286 $post_token);
287 11 1;
288 }
289  
290  
291 =item set_options (PART, ABBREV, JOIN_TOKENS, JOIN_PART)
292  
293 Allows further customization of a name format: you can set the
294 abbreviation flag and the two token-join methods. Alas, there is no
295 mechanism for leaving a value unchanged; you must set everything with
296 C<set_options>.
297  
298 For example, let's say that just dropping periods from abbreviated
299 tokens in the first name isn't enough; you I<really> want to save
300 space by jamming the abbreviated tokens together: C<"JR Smith"> rather
301 than C<"J R Smith"> Assuming the two calls in the above example have
302 been done, the following will finish the job:
303  
304 $format->set_options (BTN_FIRST,
305 1, # keep same value for abbrev flag
306 BTJ_NOTHING, # jam tokens together
307 BTJ_SPACE); # space after final token of part
308  
309 Note that we unfortunately had to know (and supply) the current values
310 for the abbreviation flag and post-part join method, even though we were
311 only setting the intra-part join method.
312  
313 =cut
314  
315 sub set_options
316 14 ▶{
Subroutine set_options: called CC=1 SCAR=0.0
Pod documented
317 14 my ($self, $part, $abbrev, $join_tokens, $join_part) = @_;
318  
319 14 _set_options ($self->{'_cstruct'}, $part,
320 $abbrev, $join_tokens, $join_part);
321 14 1;
322 }
323  
324  
325 =item apply (NAME)
326  
327 Once a name format has been created and customized to your heart's
328 content, you can use it to format any number of names using the C<apply>
329 method. NAME must be a C<Text::BibTeX::Name> object (i.e., a pre-split
330 name); C<apply> returns a string containing the parts of the name
331 formatted according to the C<Text::BibTeX::NameFormat> structure it is
332 called on.
333  
334 =cut
335  
336 sub apply
337 66 ▶{
Subroutine apply: called CC=7 SCAR=20.2
Pod documented
338 66 my ($self, $name) = @_;
339  
340 66 ▶ my $name_struct = $name->{'_cstruct'} ||
Condition50%1/2
$name->{'_cstruct'} || croak('invalid Name object: no C structure')
l!l
660
MC/DC0%0/1
$name->{'_cstruct'} || croak('invalid Name object: no C structure')
$name->{'_cstruct'}
Truth table50%1/2
A
A= $name->{'_cstruct'}
Aresult
11
00
341 croak "invalid Name object: no C structure";
342 66 ▶ my $format_struct = $self->{'_cstruct'} ||
Condition50%1/2
$self->{'_cstruct'} || croak('invalid NameFormat object: no C structure')
l!l
660
MC/DC0%0/1
$self->{'_cstruct'} || croak('invalid NameFormat object: no C structure')
$self->{'_cstruct'}
Truth table50%1/2
A
A= $self->{'_cstruct'}
Aresult
11
00
343 croak "invalid NameFormat object: no C structure";
344 66 ▶ my $utf8 = ($name->{binmode} // 'utf-8') eq 'utf-8';
Condition50%1/2
$name->{"binmode"} // "utf-8"
l!l
660
MC/DC0%0/1
$name->{"binmode"} // "utf-8"
$name->{"binmode"}
Truth table50%1/2
A
A= $name->{"binmode"}
Aresult
11
00
345 66 ▶ my $ans = format_name ($name_struct, $format_struct, $utf8 ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$utf8 ? : 64 2 66
346 66 ▶ return $utf8 ? Text::BibTeX::_normalise($name->{normalization} // 'NFC', $ans) : $ans;
Branch: 1 branch
BranchTrueFalseTotal
$utf8 ? : 64 2 66
Condition50%1/2
$name->{"normalization"} // "NFC"
l!l
640
MC/DC0%0/1
$name->{"normalization"} // "NFC"
$name->{"normalization"}
Truth table50%1/2
A
A= $name->{"normalization"}
Aresult
11
00
347 }
348  
349 =back
350  
351 =head1 EXAMPLES
352  
353 Although the process of splitting and formatting names may sound
354 complicated and convoluted from reading the above (along with
355 L<Text::BibTeX::Name>), it's actually quite simple. There are really
356 only three steps to worry about: split the name (create a
357 C<Text::BibTeX::Name> object), create and customize the format
358 (C<Text::BibTeX::NameFormat> object), and apply the format to the name.
359  
360 The first step is covered in L<Text::BibTeX::Name>; here's a brief
361 example:
362  
363 $orig_name = 'Charles Louis Xavier Joseph de la Vall{\'e}e Poussin';
364 $name = Text::BibTeX::Name->new($orig_name);
365  
366 The various parts of the name can now be accessed through
367 C<Text::BibTeX::Name> methods; for instance C<$name-E<gt>part('von')>
368 returns the list C<("de","la")>.
369  
370 Creating the name format is equally simple:
371  
372 $format = Text::BibTeX::NameFormat->new('vljf', 1);
373  
374 creates a format that will print the name in "von last jr first" order,
375 with the first name abbreviated. And for no extra charge, you get the
376 right punctuation at the right place: a comma before any `jr' or `first'
377 tokens, and periods after each `first' token.
378  
379 For instance, we can perform no further customization on this format,
380 and apply it immediately to C<$name>. There are in fact two ways to do
381 this, depending on whether you prefer to think of it in terms of
382 "Applying the format to a name" or "formatting a name". The first is
383 done with C<Text::BibTeX::NameFormat>'s C<apply> method:
384  
385 $formatted_name = $format->apply ($name);
386  
387 while the second uses C<Text::BibTeX::Name>'s C<format> method:
388  
389 $formatted_name = $name->format ($format);
390  
391 which is just a wrapper around C<Text::BibTeX::NameFormat::apply>. In
392 either case, the result with the example name and format shown is
393  
394 de~la Vall{\'e}e~Poussin, C.~L. X.~J.
395  
396 Note the strategic insertion of TeX "ties" (non-breakable spaces) at
397 sensitive spots in the name. (The exact rules for insertion of
398 discretionary ties are given in L</"FORMATTING RULES">.)
399  
400 =head1 SEE ALSO
401  
402 L<Text::BibTeX::Entry>, L<Text::BibTeX::Name>.
403  
404 =head1 AUTHOR
405  
406 Greg Ward <gward@python.net>
407  
408 =head1 COPYRIGHT
409  
410 Copyright (c) 1997-2000 by Gregory P. Ward. All rights reserved. This file
411 is part of the Text::BibTeX library. This library is free software; you
412 may redistribute it and/or modify it under the same terms as Perl itself.
413  
414 =cut
415  
416  
417 18 1;
418  
« blib/lib/Text/BibTeX/BibSort.pm ↑ Summary blib/lib/Text/BibTeX/Name.pm »