blib/lib/Text/BibTeX/BibFormat.pm

filter: Statementstmt 76.9% 90 / 117 Branchbran 43.3% 26 / 60 Conditioncond 58.3% 7 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 86.9% 20 / 23 Podpod 5.8% 1 / 17 totaltotal 61.6% 146 / 237 CC 36 SCAR 18.0
CC
36
Cov
65%
format_number_series
13.0
format_editors
12.0
format_names
10.2
CRAP 6.0
« blib/lib/Text/BibTeX/Error.pm ↑ Summary blib/lib/Text/BibTeX/BibSort.pm »
1 # ----------------------------------------------------------------------
2 # NAME : BibFormat.pm
3 # CLASSES : Text::BibTeX::BibFormat
4 # RELATIONS : sub-class of Text::BibTeX::StructuredEntry
5 # super-class of Text::BibTeX::BibEntry
6 # DESCRIPTION: Provides methods for final output formatting of
7 # bibliographic entries.
8 # CREATED : 1997/11/24, GPW
9 # MODIFIED :
10 # VERSION : $Id$
11 # COPYRIGHT : Copyright (c) 1997 by Gregory P. Ward. All rights reserved.
12 #
13 # This file is part of the Text::BibTeX library. This is free
14 # software; you can redistribute it and/or modify it under the
15 # same terms as Perl itself.
16 # ----------------------------------------------------------------------
17  
18 package Text::BibTeX::BibFormat;
19  
20 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use Text::BibTeX::Name;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Text::BibTeX::NameFormat;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Text::BibTeX::Structure;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 our @ISA = qw(Text::BibTeX::StructuredEntry);
28 1 our $VERSION = '1.00';
29  
30 1 ▶use Text::BibTeX qw(:subs display_list :nameparts :joinmethods);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 =head1 NAME
33  
34 Text::BibTeX::BibFormat - formats bibliography entries
35  
36 =head1 SYNOPSIS
37  
38 # Assuming $entry comes from a database of the 'Bib' structure
39 # (i.e., that it's blessed into the BibEntry class, which inherits
40 # the format method from BibFormat):
41 @blocks = $entry->format;
42  
43 =head1 DESCRIPTION
44  
45 The C<Text::BibTeX::BibFormat> class is a base class of
46 C<Text::BibTeX::BibEntry> for formatting bibliography entries. It thus
47 performs the main job of any program that would hope to supplant BibTeX
48 itself; the other important job (sorting) is handled by its companion
49 class, C<Text::BibTeX::BibSort>.
50  
51 C<BibFormat> (the C<Text::BibTeX> prefix will be dropped for brevity)
52 pays attention to almost all of the structure options described in
53 L<Text::BibTeX::Bib>; it only ignores those that cover sorting,
54 currently just C<sortby>. In particular, all of the "markup" options
55 control what language is generated by C<BibFormat>; if none of those
56 options are set, then it will generate plain, unmarked text.
57  
58 The only method in C<BibFormat>'s documented interface (so far) is
59 C<format>. (The class defines many other methods, but these should not
60 be necessary to outsiders, so they are undocumented and subject to
61 change.)
62  
63 =head1 METHODS
64  
65 =over 4
66  
67 =cut
68  
69 # ----------------------------------------------------------------------
70 # Ordinary subroutines:
71  
72 sub connect_words
73 0 ▶{
Subroutine connect_words: not called CC=2 SCAR=17.9
Pod undocumented
74 0 my ($s1, $s2) = @_;
75  
76 0 ▶ return $s1 . ((length ($s2) < 3) ? '~' : ' ') . $s2;
Branch: 1 branch
BranchTrueFalseTotal
length $s2 < 3 ? : 0 0 0
77 }
78  
79  
80 # ----------------------------------------------------------------------
81 # Utility methods (eg. apply a bit of markup to a string or field)
82  
83 sub markup_field
84 2 ▶{
Subroutine markup_field: called CC=5 SCAR=18.6
Pod undocumented
85 2 my ($self, $markup, $field) = @_;
86  
87 2 ▶ $markup = $self->structure->get_options ("${markup}_mkup")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $markup eq "ARRAY" and @$markup == 2 2 0 2
Condition33%1/3
ref $markup eq "ARRAY" and @$markup == 2
!ll&&!rl&&r
200
MC/DC0%0/2
ref $markup eq "ARRAY" and @$markup == 2
ref $markup eq "ARRAY" @$markup == 2
Truth table33%1/3
A and B
A= ref $markup eq "ARRAY"B= @$markup == 2
ABresult
0X0
100
111
88 unless (ref $markup eq 'ARRAY' && @$markup == 2);
89 2 ▶ croak "${markup}_mkup option not defined"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $markup 0 2 2
90 unless defined $markup;
91  
92 2 ▶ $self->exists ($field)
Branch: 1 branch
BranchTrueFalseTotal
$self->exists($field) ? : 2 0 2
93 ? $markup->[0] . $self->get ($field) . $markup->[1]
94 : '';
95 }
96  
97  
98 sub markup_string
99 23 ▶{
Subroutine markup_string: called CC=4 SCAR=15.6
Pod undocumented
100 23 my ($self, $markup, $string) = @_;
101  
102 23 ▶ $markup = $self->structure->get_options ("${markup}_mkup")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $markup eq "ARRAY" and @$markup == 2 23 0 23
Condition33%1/3
ref $markup eq "ARRAY" and @$markup == 2
!ll&&!rl&&r
2300
MC/DC0%0/2
ref $markup eq "ARRAY" and @$markup == 2
ref $markup eq "ARRAY" @$markup == 2
Truth table33%1/3
A and B
A= ref $markup eq "ARRAY"B= @$markup == 2
ABresult
0X0
100
111
103 unless (ref $markup eq 'ARRAY' && @$markup == 2);
104 23 ▶ croak "${markup}_mkup option not defined"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $markup 0 23 23
105 unless defined $markup;
106  
107 23 $markup->[0] . $string . $markup->[1];
108 }
109  
110  
111 # ----------------------------------------------------------------------
112 # Formatting methods I: utility methods called by the entry-formatters
113  
114 sub format_authors
115 9 ▶{
Subroutine format_authors: called CC=2 SCAR=7.0
Pod undocumented
116 9 my $self = shift;
117  
118 9 ▶ return '' unless $self->exists ('author');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->exists("author") 0 9 9
119 9 my @authors = $self->names ('author');
120 9 $self->format_names (\@authors)
121 }
122  
123  
124 sub format_editors
125 0 ▶{
Subroutine format_editors: not called CC=3 SCAR=24.8
Pod undocumented
126 0 my $self = shift;
127  
128 # The word used to indicate editorship should be customizable --
129 # might want it in another language, or abbreviated, or both.
130 0 ▶ return '' unless $self->exists ('editor');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->exists("editor") 0 0 0
131 0 my @editors = $self->names ('editor');
132 0 ▶ my $tackon = (@editors == 1) ? ', editor' : ', editors';
Branch: 1 branch
BranchTrueFalseTotal
@editors == 1 ? : 0 0 0
133 0 $self->format_names (\@editors) . $tackon;
134 }
135  
136  
137 sub format_names
138 9 ▶{
Subroutine format_names: called CC=10 SCAR=23.2
Pod undocumented
139 9 my ($self, $names) = @_;
140 9 my ($format, $name);
141  
142 9 my ($order, $style) =
143 $self->structure->get_options ('nameorder', 'namestyle');
144 9 ▶ croak "format_names: bad nameorder option \"$order\""
Branch: 1 branch
BranchTrueFalseTotal
unless $order eq "first" or $order eq "last" 0 9 9
Condition66%2/3
$order eq "first" or $order eq "last"
l!l&&r!l&&!r
360
MC/DC0%0/2
$order eq "first" or $order eq "last"
$order eq "first" $order eq "last"
Truth table66%2/3
A or B
A= $order eq "first"B= $order eq "last"
ABresult
1X1
011
000
145 unless $order eq 'first' || $order eq 'last';
146 9 ▶ croak "format_names: bad namestyle option \"$style\""
Branch: 1 branch
BranchTrueFalseTotal
unless $style =~ /^(full|abbrev|nopunct|nospace)$/ 0 9 9
147 unless $style =~ /^(full|abbrev|nopunct|nospace)$/;
148  
149 9 ▶ $order = ($order eq 'first') ? 'fvlj' : 'vljf';
Branch: 1 branch
BranchTrueFalseTotal
$order eq 'first' ? : 3 6 9
150 9 $format = Text::BibTeX::NameFormat->new ($order, ! ($style eq 'full'));
151  
152 9 ▶ $format->set_text (&BTN_FIRST, undef, undef, undef, '')
Branch: 1 branch
BranchTrueFalseTotal
if $style eq "nopunct" or $style eq "nospace" 4 5 9
Condition100%3/3
$style eq "nopunct" or $style eq "nospace"
l!l&&r!l&&!r
135
MC/DC100%2/2
$style eq "nopunct" or $style eq "nospace"
$style eq "nopunct" $style eq "nospace"
Truth table100%3/3
A or B
A= $style eq "nopunct"B= $style eq "nospace"
ABresult
1X1
011
000
153 if $style eq 'nopunct' || $style eq 'nospace';
154 9 ▶ $format->set_options (&BTN_FIRST, 1, &BTJ_NOTHING, &BTJ_SPACE)
Branch: 1 branch
BranchTrueFalseTotal
if $style eq "nospace" 3 6 9
155 if $style eq 'nospace';
156  
157 9 foreach $name (@$names)
158 {
159 16 $name = $name->format ($format);
160 16 ▶ $name = 'et. al.' if $name eq 'others';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "others" 0 16 16
161 }
162  
163 9 return $self->markup_string ('name', display_list($names,0));
164 }
165  
166  
167 sub format_atitle
168 7 ▶{
Subroutine format_atitle: called CC=2 SCAR=6.9
Pod undocumented
169 7 my $self = shift;
170  
171 7 my $lower = $self->structure->get_options ('atitle_lower');
172 7 my $title = $self->get ('title');
173 7 ▶ $title = change_case ('t', $title) if $lower;
Branch: 1 branch
BranchTrueFalseTotal
if $lower 6 1 7
174 7 $self->markup_string ('atitle', $title);
175 # $markup->[0] . $title . $markup->[1];
176 }
177  
178  
179 sub format_btitle
180 2 ▶{
Subroutine format_btitle: called CC=1 SCAR=0.0
Pod undocumented
181 2 my $self = shift;
182  
183 2 $self->markup_field ('btitle', 'title');
184 # my $markup = $self->structure->get_options ('btitle_mkup');
185 # my $title = $self->get ('title');
186 # $markup->[0] . $title . $markup->[1];
187 }
188  
189  
190 # sub format_xref_article
191 # {
192 # my $self = shift;
193  
194 # # N.B. this assumes that the appropriate fields from the cross-
195 # # referenced entry have already been put into the current entry!
196  
197 # # XXX hard-coded LaTeX markup here!!!
198  
199 # my ($key, $journal, $crossref);
200 # $key = $self->get ('key');
201 # $journal = $self->get ('journal');
202 # $crossref = $self->get ('crossref');
203 # if (defined $key)
204 # {
205 # return "In $key \cite{$crossref}";
206 # }
207 # else
208 # {
209 # if (defined $journal)
210 # {
211 # return "In {\em $journal} \cite{$crossref}";
212 # }
213 # else
214 # {
215 # $self->warn ("need key or journal for crossref");
216 # return " \cite{$crossref}";
217 # }
218 # }
219 # }
220  
221  
222 sub format_pages
223 0 ▶{
Subroutine format_pages: not called CC=2 SCAR=17.9
Pod undocumented
224 0 my $self = shift;
225  
226 0 my $pages = $self->get ('pages');
227 0 ▶ if ($pages =~ /[,+-]/) # multiple pages?
Branch: 1 branch
BranchTrueFalseTotal
if ($pages =~ /[,+-]/) { } 0 0 0
228 {
229 0 $pages =~ s/([^-])-([^-])/$1--$2/g;
230 0 return connect_words ("pages", $pages);
231 }
232 else
233 {
234 0 return connect_words ("page", $pages);
235 }
236 }
237  
238  
239 sub format_vol_num_pages
240 7 ▶{
Subroutine format_vol_num_pages: called CC=4 SCAR=14.3
Pod undocumented
241 7 my $self = shift;
242  
243 7 my ($vol, $num, $pages) = $self->get ('volume', 'number', 'pages');
244 7 my $vnp = '';
245 7 ▶ $vnp .= $vol if defined $vol;
Branch: 1 branch
BranchTrueFalseTotal
if defined $vol 7 0 7
246 7 ▶ $vnp .= "($num)" if defined $num;
Branch: 1 branch
BranchTrueFalseTotal
if defined $num 0 7 7
247 7 ▶ $vnp .= ":$pages" if defined $pages;
Branch: 1 branch
BranchTrueFalseTotal
if defined $pages 7 0 7
248 7 return $vnp;
249 }
250  
251  
252 sub format_bvolume
253 2 ▶{
Subroutine format_bvolume: called CC=3 SCAR=15.0
Pod undocumented
254 2 my $self = shift;
255 2 my $volser; # potentially volume and series
256  
257 2 ▶ if ($self->exists ('volume'))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->exists('volume')) { } 0 2 2
258 {
259 0 $volser = connect_words ('volume', $self->get ('volume'));
260 0 ▶ $volser .= ' of ' . $self->markup_field ('btitle', 'series')
Branch: 1 branch
BranchTrueFalseTotal
if $self->exists("series") 0 0 0
261 if $self->exists ('series');
262 0 return $volser;
263 }
264 else
265 {
266 2 return '';
267 }
268 }
269  
270  
271 sub format_number_series
272 2 ▶{
Subroutine format_number_series: called CC=5 SCAR=25.7
Pod undocumented
273 2 my ($self, $mid_sentence) = @_;
274  
275 2 ▶ if ($self->exists ('volume'))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->exists('volume')) { } 0 2 2
276 {
277 # if 'volume' field exists, then format_bvolume took care of
278 # formatting it, so don't do anything here
279 0 return '';
280 }
281 else
282 {
283 2 ▶ if ($self->exists ('number'))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->exists('number')) { } 0 2 2
284 {
285 0 my $numser;
286  
287 0 ▶ $numser = connect_words ($mid_sentence ? 'number' : 'Number',
Branch: 1 branch
BranchTrueFalseTotal
$mid_sentence ? : 0 0 0
288 $self->get ('number'));
289 0 ▶ if ($self->exists ('series'))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->exists('series')) { } 0 0 0
290 {
291 0 $numser .= ' in ' . $self->get ('series');
292 }
293 else
294 {
295 0 $self->warn ("there's a number but no series " .
296 "(is this warning redundant?!?)");
297 }
298 0 return $numser;
299 }
300 else
301 {
302 # No 'number' -- just return the 'series' (or undef if none)
303 2 return $self->get ('series');
304 }
305 } # no 'volume' field
306 } # format_number_series
307  
308  
309 sub format_edition
310 2 ▶{
Subroutine format_edition: called CC=3 SCAR=16.5
Pod undocumented
311 2 my ($self, $mid_sentence) = @_;
312  
313 # XXX more fodder for I18N here: the word 'edition'
314 2 ▶ return '' unless $self->exists ('edition');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->exists("edition") 2 0 2
315 0 ▶ my $case_transform = $mid_sentence ? 'l' : 't';
Branch: 1 branch
BranchTrueFalseTotal
$mid_sentence ? : 0 0 0
316 0 return change_case ($case_transform, $self->get ('edition')) . ' edition';
317  
318 } # format_edition
319  
320  
321 sub format_date
322 9 ▶{
Subroutine format_date: called CC=1 SCAR=0.0
Pod undocumented
323 9 my $self = shift;
324  
325 9 my @date = grep ($_, $self->get ('month', 'year'));
326 9 return join (' ', @date);
327 }
328  
329  
330 # ----------------------------------------------------------------------
331 # The actual entry-formatting methods:
332 # format_article
333 # format_book
334 # format_inbook
335 # ...and so on.
336  
337 # Each of these returns a list of blocks.
338 # A block is a list of sentences.
339 # A sentence is either a string or a list of clauses.
340 # Any clause, sentence, or block in any list may be empty or undefined;
341 # it should be removed before output.
342 # If a sentence consists of a list of clauses, they should be joined
343 # together with ", " to form the sentence-as-string.
344 #
345 # For example, the formatted entry for an article (in the absence of
346 # cross-references) consists of four blocks:
347 # - the name block, which has a single sentence; this sentence
348 # has a single clause (the list of author names), and thus is
349 # represented as a string like "Joe Blow, Fred Jones, and John Smith"
350 # - the title block, which has a single sentence; this sentence
351 # has a single clause, the title of the article, eg. "The mating
352 # habits of foobars"
353 # - the journal block, which consists of a single sentence that has
354 # three clauses: the journal name, the volume/number/pages, and
355 # the date. When the three clauses are joined, we get something like
356 # "Journal of Foo, 4(5):122--130, May 1996" for the single sentence
357 # in the block.
358 # - the note block -- if the entry has no `note' field, this block
359 # will be an undefined value rather than a list of sentences
360 #
361 # These four blocks are returned from `format_article' (and thus from
362 # `format') as a list-of-lists-of-(strings or lists-of-strings. That
363 # is, each format methods returns a list of blocks, each of which is in
364 # turn a list of sentences. (Hence "list of lists of X".) Each
365 # sentence is either a string ("list of lists of strings") or a list of
366 # clauses ("list of lists of lists of strings'). Clear? Hope so!
367 #
368 # [ # enter list of blocks
369 # ["Joe Blow, Fred Jones, and John Smith"] # name block:
370 # # 1 sentence w/ 1 clause
371 # ["The mating habits of foobars"] # title block:
372 # # 1 sentence w/ 1 clause
373 # [["Journal of Foo", # journal block:
374 # "4(5):122--130", # 1 sentence w/ 3 clauses
375 # "May 1996"]]
376 # undef
377 # ]
378 #
379 # A note: the journal name will normally have a bit of markup around it,
380 # say to italicize it -- that's determined by the calling application,
381 # though; the default markups are all empty strings. There could
382 # probably be arbitrary markup for every element of an entry, but I
383 # haven't gone that far yet.
384 #
385 # It is then the responsibility of the calling application to apply the
386 # appropriate punctuation and munge all those lists of strings together
387 # into something worth printing. The canonical application for doing
388 # this is btformat, which supports LaTeX 2.09, LaTeX2e, and HTML markup
389 # and output.
390  
391  
392 sub format_article
393 7 ▶{
Subroutine format_article: called CC=1 SCAR=0.0
Pod undocumented
394 7 my $self = shift;
395  
396  
397 7 my $name_block = [$self->format_authors];
398 7 my $title_block = [$self->format_atitle];
399 7 my $journal_block = [[$self->markup_string('journal', $self->get ('journal')),
400 $self->format_vol_num_pages,
401 $self->format_date]];
402  
403 # if ($self->exists ('crossref'))
404 # {
405 # push (@blocks, [[$self->format_xref_article,
406 # $self->format_pages]]);
407 # }
408 # else
409 # {
410 # }
411  
412 # push (@blocks, [$self->get ('note')]) if $self->exists ('note');
413 # @blocks;
414  
415 7 ($name_block, $title_block, $journal_block, $self->get ('note'));
416 } # format_article
417  
418  
419 sub format_book
420 2 ▶{
Subroutine format_book: called CC=2 SCAR=7.0
Pod undocumented
421 2 my $self = shift;
422  
423 2 ▶ my $name_block = # author(s) or editor(s)
Branch: 1 branch
BranchTrueFalseTotal
$self->exists('author') ? : 2 0 2
424 ($self->exists ('author'))
425 ? [$self->format_authors]
426 : [$self->format_editors];
427 2 my $title_block = # title (and volume)
428 [[$self->format_btitle, $self->format_bvolume]];
429 2 my $from_block = # number/series; publisher, address,
430 [$self->format_number_series (0), # edition, date
431 [$self->get ('publisher'), $self->get ('address'),
432 $self->format_edition (0), $self->format_date]];
433  
434 2 ($name_block, $title_block, $from_block, $self->get('note'));
435  
436 } # format_book
437  
438  
439 # ----------------------------------------------------------------------
440 # Finally, the `format' method -- just calls one of the
441 # type-specific format methods (format_article, etc.)
442  
443 =item format ()
444  
445 Formats a single entry for inclusion in the bibliography of some
446 document. The exact processing performed is highly dependent on the
447 entry type and the fields present; in general, you should be able to
448 join C<format>'s outputs together to create a single paragraph for
449 inclusion in a document of whatever markup language you're working with.
450  
451 Returns a list of "blocks," which can either be jammed together like
452 sentences (for a traditional "tight" bibliography) or printed on
453 separate lines (for an "open" bibliography format). Each block is a
454 reference to a list of sentences; sentences should be joined together
455 with an intervening period. Each sentence is either a single string or
456 a list of clauses; clauses should be joined together with an intervening
457 comma. Each clause is just a simple string.
458  
459 See the source code for C<btformat> for an example of how to use the
460 output of C<format>.
461  
462 =cut
463  
464 sub format
465 9 ▶{
Subroutine format: called CC=2 SCAR=7.3
Pod documented
466 9 my $self = shift;
467  
468 9 my $type = $self->type;
469 9 my $key = $self->key;
470 9 my $method_name = 'format_' . $type;
471 9 my $method = $self->can ($method_name);
472 9 ▶ unless ($method)
Branch: 1 branch
BranchTrueFalseTotal
unless ($method) 0 9 9
473 {
474 0 $self->warn ("can't format entry: " .
475 "no method $method_name (for type $type)");
476 0 return;
477 }
478
479 9 return &$method ($self);
480 }
481  
482 1 1;
483  
484 =back
485  
486 =head1 SEE ALSO
487  
488 L<Text::BibTeX::Structure>, L<Text::BibTeX::Bib>,
489 L<Text::BibTeX::BibSort>
490  
491 =head1 AUTHOR
492  
493 Greg Ward <gward@python.net>
494  
495 =head1 COPYRIGHT
496  
497 Copyright (c) 1997-2000 by Gregory P. Ward. All rights reserved. This file
498 is part of the Text::BibTeX library. This library is free software; you
499 may redistribute it and/or modify it under the same terms as Perl itself.
« blib/lib/Text/BibTeX/Error.pm ↑ Summary blib/lib/Text/BibTeX/BibSort.pm »