blib/lib/Text/BibTeX/Bib.pm

filter: Statementstmt 100% 37 / 37 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 97.8% 45 / 46 CC 2 SCAR 2.4
CC
2
Cov
97%
default_option
2.1
BEGIN
1.0
known_option
1.0
CRAP 1.3
« blib/lib/Text/BibTeX/Name.pm ↑ Summary src/error.c »
1 # ----------------------------------------------------------------------
2 # NAME : BibTeX/Bib.pm
3 # CLASSES : Text::BibTeX::BibStructure, Text::BibTeX::BibEntry;
4 # loads Text::BibTeX::BibSort and Text::BibTeX::BibFormat
5 # for use by BibEntry
6 # RELATIONS : BibStructure inherits from Structure
7 # BibEntry inherits from BibSort and BibFormat, which
8 # both inherit from StructuredEntry
9 # DESCRIPTION: Implements the "Bib" structure, which provides the
10 # same functionality -- though in a completely different
11 # context, and much more customizably -- as the standard
12 # style files of BibTeX 0.99.
13 # CREATED : 1997/09/21, Greg Ward
14 # MODIFIED :
15 # VERSION : $Id$
16 # COPYRIGHT : Copyright (c) 1997-2000 by Gregory P. Ward. All rights
17 # reserved.
18 #
19 # This file is part of the Text::BibTeX library. This
20 # library is free software; you may redistribute it and/or
21 # modify it under the same terms as Perl itself.
22 # ----------------------------------------------------------------------
23  
24 =head1 NAME
25  
26 Text::BibTeX::Bib - defines the "Bib" database structure
27  
28 =head1 SYNOPSIS
29  
30 $bibfile = Text::BibTeX::File $filename->new;
31 $bibfile->set_structure ('Bib',
32 # Default option values:
33 sortby => 'name',
34 namestyle => 'full'
35 nameorder => 'first',
36 atitle => 1,
37 labels => 'numeric');
38  
39 # Alternate option values:
40 $bibfile->set_option (sortby => 'year');
41 $bibfile->set_option (namestyle => 'nopunct');
42 $bibfile->set_option (namestyle => 'nospace');
43 $bibfile->set_option (nameorder => 'last');
44 $bibfile->set_option (atitle => 0);
45 $bibfile->set_option (labels => 'alpha'); # not implemented yet!
46  
47 # parse entry from $bibfile and automatically make it a BibEntry
48 $entry = Text::BibTeX::Entry->new($bibfile);
49  
50 # or get an entry from somewhere else which is hard-coded to be
51 # a BibEntry
52 $entry = Text::BibTeX::BibEntry->new(...);
53  
54 $sortkey = $entry->sort_key;
55 @blocks = $entry->format;
56  
57 =head1 DESCRIPTION
58  
59 (B<NOTE!> Do not believe everything you read in this document. The
60 classes described here are unfinished and only lightly tested. The
61 current implementation is a proof-of-principle, to convince myself (and
62 anyone who might be interested) that it really is possible to
63 reimplement BibTeX 0.99 in Perl using the core C<Text::BibTeX> classes;
64 this principle is vaguely demonstrated by the current C<Bib*> modules,
65 but not really proved. Many important features needed to reimplement
66 the standard styles of BibTeX 0.99 are missing, even though this
67 document may brashly assert otherwise. If you are interested in using
68 these classes, you should start by reading and grokking the code, and
69 contributing the missing bits and pieces that you need.)
70  
71 C<Text::BibTeX::Bib> implements the database structure for
72 bibliographies as defined by the standard styles of BibTeX 0.99. It
73 does this by providing two classes, C<BibStructure> and C<BibEntry> (the
74 leading C<Text::BibTeX> is implied, and will be omitted for the rest of
75 this document). These two classes, being specific to bibliographic
76 data, are outside of the core C<Text::BibTeX> class hierarchy, but are
77 distributed along with it as they provide a canonical example of a
78 specific database structure using classes derived from the core
79 hierarchy.
80  
81 C<BibStructure>, which derives from the C<Structure> class, deals with
82 the structure as a whole: it handles structure options and describes all
83 the types and fields that make up the database structure. If you're
84 interested in writing your own database structure modules, the standard
85 interface for both of these is described in L<Text::BibTeX::Structure>;
86 if you're just interested in finding out the exact database structure or
87 the options supported by the C<Bib> structure, you've come to the right
88 place. (However, you may have to wade through a bit of excess verbiage
89 due to this module's dual purpose: first, to reimplement the standard
90 styles of BibTeX 0.99, and second, to provide an example for other
91 programmers wishing to implement new or derived database structure
92 modules.)
93  
94 C<BibEntry> derives from the C<StructuredEntry> class and provides
95 methods that operate on individual entries presumed to come from a
96 database conforming to the structure defined by the C<BibStructure>
97 class. (Actually, to be completely accurate, C<BibEntry> inherits from
98 two intermediate classes, C<BibSort> and C<BibFormat>. These two
99 classes just exist to reduce the amount of code in the C<Bib> module,
100 and thanks to the magic of inheritance, their existence is usually
101 irrelevant. But you might want to consult those two classes if you're
102 interested in the gory details of sorting and formatting entries from
103 BibTeX 0.99-style bibliography databases.)
104  
105 =cut
106  
107  
108 # first, the "structure class" (inherits from Text::BibTeX::Structure)
109  
110 package Text::BibTeX::BibStructure;
111 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
112 1 our @ISA = qw(Text::BibTeX::Structure);
113 1 our $VERSION = '1.00';
114  
115 =head1 STRUCTURE OPTIONS
116  
117 C<BibStructure> handles several user-supplied "structure options" and
118 methods for dealing with them. The options currently supported by the
119 C<Bib> database structure, and the values allowed for them, are:
120  
121 =over 4
122  
123 =item C<sortby>
124  
125 How to sort entries. Valid values: C<name> (sort on author names, year,
126 and title), C<year> (sort on year, author names, and title). Sorting on
127 "author names" is a bit more complicated than just using the C<author>
128 field; see L<Text::BibTeX::BibSort> for details. Default value: C<name>.
129  
130 =item C<namestyle>
131  
132 How to print author (and editor) names: C<full> for unabbreviated first
133 names, C<abbrev> for first names abbreviated with periods, C<nopunct>
134 for first names abbreviated with space but no periods, and C<nospace> to
135 abbreviate without space or periods. Default value: C<full>.
136  
137 =item C<nameorder>
138  
139 The order in which to print names: C<first> for "first von last jr"
140 order, and C<last> for "von last jr first" order. Default value:
141 C<first>.
142  
143 =item C<atitle_lower>
144  
145 A boolean option: if true, non-book titles will be changed to "sentence
146 capitalization:" words following colons and sentence-ending punctuation
147 will be capitalized, and everything else at brace-depth zero will be
148 changed to lowercase. Default value: true.
149  
150 =item C<labels>
151  
152 The type of bibliographic labels to generate: C<numeric> or C<alpha>.
153 (Alphabetic labels are not yet implemented, so this option is currently
154 ignored.) Default value: C<numeric>.
155  
156 =back
157  
158 Also, several "markup options" are supported. Markup options are
159 distinct because they don't change how text is extracted from the
160 database entries and subsequently mangled; rather, they supply bits of
161 markup that go around the database-derived text. Markup options are
162 always two-element lists: the first to "turn on" some feature of the
163 markup language, and the second to turn it off. For example, if your
164 target language is LaTeX2e and you want journal names emphasized, you
165 would supply a list reference C<['\emph{','}']> for the C<journal_mkup>
166 option. If you were instead generating HTML, you might supply
167 C<['E<lt>emphE<gt>','E<lt>/emphE<gt>']>. To keep the structure module
168 general with respect to markup languages, all markup options are empty
169 by default. (Or, rather, they are all references to lists consisting of
170 two empty strings.)
171  
172 =over 4
173  
174 =item C<name_mkup>
175  
176 Markup to add around the list of author names.
177  
178 =item C<atitle_mkup>
179  
180 Markup to add around non-book (article) titles.
181  
182 =item C<btitle_mkup>
183  
184 Markup to add around book titles.
185  
186 =item C<journal_mkup>
187  
188 Markup to add around journal names.
189  
190 =back
191  
192 =cut
193  
194 1 my %default_options =
195 (sortby => 'name', # or 'year', 'none'
196 namestyle => 'full', # or 'abbrev', 'nopunct', 'nospace'
197 nameorder => 'first', # or 'last'
198 atitle_lower=> 1, # mangle case of non-book titles?
199 labels => 'numeric', # or 'alpha' (not yet supported!)
200 name_mkup => ['', ''],
201 atitle_mkup => ['', ''],
202 btitle_mkup => ['', ''],
203 journal_mkup=> ['', ''],
204 );
205  
206  
207 =head2 Option methods
208  
209 As required by the C<Text::BibTeX::Structure> module,
210 C<Text::BibTeX::Bib> provides two methods for handling options:
211 C<known_option> and C<default_option>. (The other two option methods,
212 C<set_options> and C<get_options>, are just inherited from
213 C<Text::BibTeX::Structure>.)
214  
215 =over 4
216  
217 =item known_option (OPTION)
218  
219 Returns true if OPTION is one of the options on the above list.
220  
221 =item default_option (OPTION)
222  
223 Returns the default value of OPTION, or C<croak>s if OPTION is not a
224 supported option.
225  
226 =back
227  
228 =cut
229  
230 sub known_option
231 10 ▶{
Subroutine known_option: called CC=1 SCAR=0.0
232 10 my ($self, $option) = @_;
233 10 return exists $default_options{$option};
234 }
235  
236  
237 sub default_option
238 36 ▶{
Subroutine default_option: called CC=2 SCAR=7.2
239 36 my ($self, $option) = @_;
240 return exists $default_options{$option}
241 36 ▶ ? $default_options{$option}
Branch: 1 branch
BranchTrueFalseTotal
exists $default_options{$option} ? : 36 0 36
242 : $self->SUPER::default_option ($option);
243 }
244  
245  
246 # The field lists in the following documentation are automatically
247 # generated by my `doc_structure' program -- I run it and read the
248 # output into this file. Wouldn't it be cool if the module could just
249 # document itself? Ah well, dreaming again...
250  
251 =head1 DATABASE STRUCTURE
252  
253 The other purpose of a structure class is to provide a method,
254 C<describe_entry>, that lists the allowed entry types and the known
255 fields for the structure. Programmers wishing to write their own
256 database structure module should consult L<Text::BibTeX::Structure> for
257 the conventions and requirements of this method; the purpose of the
258 present document is to describe the C<Bib> database structure.
259  
260 The allowed entry types, and the fields recognized for them, are:
261  
262 =over 4
263  
264 =item C<article>
265  
266 Required fields: C<author>, C<title>, C<journal>, C<year>.
267 Optional fields: C<volume>, C<number>, C<pages>, C<month>, C<note>.
268  
269 =item C<book>
270  
271 Required fields: C<title>, C<publisher>, C<year>.
272 Optional fields: C<series>, C<address>, C<edition>, C<month>, C<note>.
273 Constrained fields: exactly one of C<author>, C<editor>; at most one of C<volume>, C<number>.
274  
275 =item C<booklet>
276  
277 Required fields: C<title>.
278 Optional fields: C<author>, C<howpublished>, C<address>, C<month>, C<year>, C<note>.
279  
280 =item C<inbook>
281  
282 Required fields: C<publisher>, C<year>.
283 Optional fields: C<series>, C<type>, C<address>, C<edition>, C<month>, C<note>.
284 Constrained fields: exactly one of C<author>, C<editor>; at least one of C<chapter>, C<pages>; at most one of C<volume>, C<number>.
285  
286 =item C<incollection>
287  
288 Required fields: C<author>, C<title>, C<booktitle>, C<publisher>, C<year>.
289 Optional fields: C<editor>, C<series>, C<type>, C<chapter>, C<pages>, C<address>, C<edition>, C<month>, C<note>.
290 Constrained fields: at most one of C<volume>, C<number>.
291  
292 =item C<inproceedings>
293  
294 =item C<conference>
295  
296 Required fields: C<author>, C<title>, C<booktitle>, C<year>.
297 Optional fields: C<editor>, C<series>, C<pages>, C<address>, C<month>, C<organization>, C<publisher>, C<note>.
298 Constrained fields: at most one of C<volume>, C<number>.
299  
300 =item C<manual>
301  
302 Required fields: C<title>.
303 Optional fields: C<author>, C<organization>, C<address>, C<edition>, C<month>, C<year>, C<note>.
304  
305 =item C<mastersthesis>
306  
307 Required fields: C<author>, C<title>, C<school>, C<year>.
308 Optional fields: C<type>, C<address>, C<month>, C<note>.
309  
310 =item C<misc>
311  
312 Required fields: none.
313 Optional fields: C<author>, C<title>, C<howpublished>, C<month>, C<year>, C<note>.
314  
315 =item C<phdthesis>
316  
317 Required fields: C<author>, C<title>, C<school>, C<year>.
318 Optional fields: C<type>, C<address>, C<month>, C<note>.
319  
320 =item C<proceedings>
321  
322 Required fields: C<title>, C<year>.
323 Optional fields: C<editor>, C<series>, C<address>, C<month>, C<organization>, C<publisher>, C<note>.
324 Constrained fields: at most one of C<volume>, C<number>.
325  
326 =item C<techreport>
327  
328 Required fields: C<author>, C<title>, C<institution>, C<year>.
329 Optional fields: C<type>, C<number>, C<address>, C<month>, C<note>.
330  
331 =item C<unpublished>
332  
333 Required fields: C<author>, C<title>, C<note>.
334 Optional fields: C<month>, C<year>.
335  
336 =back
337  
338 =cut
339  
340 sub describe_entry
341 1 ▶{
Subroutine describe_entry: called CC=1 SCAR=0.0
342 1 my $self = shift;
343  
344 # Advantages of the current scheme (set all fields for a particular
345 # entry type together):
346 # - groups fields more naturally (by entry type)
347 # - might lend itself to structuring things by 'type' in the object
348 # as well, making it easier to determine if a type is valid
349 # - prevents accidentally giving a type optional fields but no
350 # required fields -- currently this mistake would make the type
351 # 'unknown'
352 #
353 # Requirement of any scheme:
354 # - must be easy for derived classes to override/augment the field
355 # lists defined here! (ie. they should be able just to inherit
356 # describe_entry; or explicitly call SUPER::describe_entry and then
357 # undo/change some of its definitions
358  
359 # Things that I don't think are handled by this scheme, but that
360 # bibtex does look out for:
361 # * warns if month but no year
362 # * crossref stuff:
363 # - article can xref article; xref'd entry must have key or journal
364 # - book or inboox can xref book; xref'd entry must have editor,
365 # key, or series
366 # - incollection can xref a book and inproceedings can xref a
367 # proceedings; xref'd entry must have editor, key, or booktitle
368  
369 1 $self->set_fields ('article',
370 [qw(author title journal year)],
371 [qw(volume number pages month note)]);
372 1 $self->set_fields ('book',
373 [qw(title publisher year)],
374 [qw(series address edition month note)],
375 [1, 1, [qw(author editor)]],
376 [0, 1, [qw(volume number)]]);
377 1 $self->set_fields ('booklet',
378 [qw(title)],
379 [qw(author howpublished address month year note)]);
380 1 $self->set_fields ('inbook',
381 [qw(publisher year)],
382 [qw(series type address edition month note)],
383 [1, 1, [qw(author editor)]],
384 [1, 2, [qw(chapter pages)]],
385 [0, 1, [qw(volume number)]]);
386 1 $self->set_fields ('incollection',
387 [qw(author title booktitle publisher year)],
388 [qw(editor series type chapter pages address
389 edition month note)],
390 [0, 1, [qw(volume number)]]);
391 1 $self->set_fields ('inproceedings',
392 [qw(author title booktitle year)],
393 [qw(editor series pages address month
394 organization publisher note)],
395 [0, 1, [qw(volume number)]]);
396 1 $self->set_fields ('conference',
397 [qw(author title booktitle year)],
398 [qw(editor series pages address month
399 organization publisher note)],
400 [0, 1, [qw(volume number)]]);
401 1 $self->set_fields ('manual',
402 [qw(title)],
403 [qw(author organization address edition
404 month year note)]);
405 1 $self->set_fields ('mastersthesis',
406 [qw(author title school year)],
407 [qw(type address month note)]);
408 1 $self->set_fields ('misc',
409 [],
410 [qw(author title howpublished month year note)]);
411 1 $self->set_fields ('phdthesis',
412 [qw(author title school year)],
413 [qw(type address month note)]);
414 1 $self->set_fields ('proceedings',
415 [qw(title year)],
416 [qw(editor series address month
417 organization publisher note)],
418 [0, 1, [qw(volume number)]]);
419 1 $self->set_fields ('techreport',
420 [qw(author title institution year)],
421 [qw(type number address month note)]);
422 1 $self->set_fields ('unpublished',
423 [qw(author title note)],
424 [qw(month year)]);
425  
426 } # describe_entry
427  
428  
429 =head1 STRUCTURED ENTRY CLASS
430  
431 The second class provided by the C<Text::BibTeX::Bib> module is
432 C<BibEntry> (again, a leading C<Text::BibTeX> is implied). This being a
433 structured entry class, it derives from C<StructuredEntry>. The
434 conventions and requirements for such a class are documented in
435 L<Text::BibTeX::Structure> for the benefit of programmers implementing
436 their own structure modules.
437  
438 If you wish to write utilities making use of the C<Bib> database
439 structure, then you should call one of the "officially supported"
440 methods provided by the C<BibEntry> class. Currently, there are only
441 two of these: C<sort_key> and C<format>. These are actually implemented
442 in the C<BibSort> and C<BibFormat> classes, respectively, which are base
443 classes of C<BibEntry>. Thus, see L<Text::BibTeX::BibSort> and
444 L<Text::BibTeX::BibFormat> for details on these two methods.
445  
446 =cut
447  
448 package Text::BibTeX::BibEntry;
449 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
450  
451 1 our $VERSION = '1.00';
452  
453 1 ▶use Text::BibTeX::BibSort;
Subroutine BEGIN: called CC=1 SCAR=0.0
454 1 ▶use Text::BibTeX::BibFormat;
Subroutine BEGIN: called CC=1 SCAR=0.0
455  
456 1 our @ISA = qw(Text::BibTeX::BibSort Text::BibTeX::BibFormat);
457
458  
459 1 1;
460  
461 =head1 SEE ALSO
462  
463 L<Text::BibTeX::Structure>, L<Text::BibTeX::BibSort>,
464 L<Text::BibTeX::BibFormat>.
465  
466 =head1 AUTHOR
467  
468 Greg Ward <gward@python.net>
469  
470 =head1 COPYRIGHT
471  
472 Copyright (c) 1997-2000 by Gregory P. Ward. All rights reserved. This file
473 is part of the Text::BibTeX library. This library is free software; you
474 may redistribute it and/or modify it under the same terms as Perl itself.
« blib/lib/Text/BibTeX/Name.pm ↑ Summary src/error.c »