summaryrefslogtreecommitdiff
path: root/support/bibtexperllibs/BibTeX-Parser/lib/BibTeX/Parser/Entry.pm
blob: 2b8fe76220aaeb9cabbaa39924a3af78bbfa77b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package BibTeX::Parser::Entry;
{
  $BibTeX::Parser::Entry::VERSION = '1.02';
}

use warnings;
use strict;

use BibTeX::Parser;
use BibTeX::Parser::Author;



sub new {
	my ($class, $type, $key, $parse_ok, $fieldsref) = @_;

	my %fields = defined $fieldsref ? %$fieldsref : ();
	my $i=0;
	foreach my $field (keys %fields) {
	    if ($field !~ /^_/) {
		$fields{_fieldnums}->{$field}=$i;
		$i++;
	    }
	}
        if (defined $type) {
            $fields{_type} = uc($type);
        }
	$fields{_key}      = $key;
	$fields{_parse_ok} = $parse_ok;
        $fields{_raw}      = '';
	return bless \%fields, $class;
}



sub parse_ok {
	my $self = shift;
	if (@_) {
		$self->{_parse_ok} = shift;
	}
	$self->{_parse_ok};
}


sub error {
	my $self = shift;
	if (@_) {
		$self->{_error} = shift;
		$self->parse_ok(0);
	}
	return $self->parse_ok ? undef : $self->{_error};
}


sub type {
	if (scalar @_ == 1) {
		# get
		my $self = shift;
		return $self->{_type};
	} else {
		# set
		my ($self, $newval) = @_;
		$self->{_type} = uc($newval);
	}
}


sub key {
	if (scalar @_ == 1) {
		# get
		my $self = shift;
		return $self->{_key};
	} else {
		# set
		my ($self, $newval) = @_;
		$self->{_key} = $newval;
	}

}


sub field {
	if (scalar @_ == 2) {
		# get
		my ($self, $field) = @_;
		return $self->{ lc( $field ) };
	} else {
		my ($self, $key, $value) = @_;
		my $field = lc ($key);
		$self->{$field} = $value; #_sanitize_field($value);
		if (!exists($self->{_fieldnums}->{$field})) {
		    my $num = scalar keys %{$self->{_fieldnums}};
		    $self->{_fieldnums}->{$field} = $num;
		}
	}

}

use LaTeX::ToUnicode qw( convert );


sub cleaned_field {
        my ( $self, $field, @options ) = @_;
        if ( $field =~ /author|editor/i ) {
            return $self->field( $field );
        } else {
            return convert( $self->field( lc $field ), @options );
        }
}


sub cleaned_author {
    my $self = shift;
    $self->_handle_cleaned_author_editor( [ $self->author ], @_ );
}


sub cleaned_editor {
    my $self = shift;
    $self->_handle_cleaned_author_editor( [ $self->editor ], @_ );
}

sub _handle_cleaned_author_editor {
    my ( $self, $authors, @options ) = @_;
    map {
        my $author = $_;
        my $new_author = BibTeX::Parser::Author->new;
        map {
            $new_author->$_( convert( $author->$_, @options ) )
        } grep { defined $author->$_ } qw( first von last jr );
        $new_author;
    } @$authors;
}

no LaTeX::ToUnicode;

sub _handle_author_editor {
    my $type = shift;
    my $self = shift;
    if (@_) {
	if (@_ == 1) { #single string
	    # my @names = split /\s+and\s+/i, $_[0];
	    $_[0] =~ s/^\s*//; 
	    $_[0] =~ s/\s*$//; 
	    my @names = BibTeX::Parser::_split_braced_string($_[0], 
							     '\s+and\s+');
	    if (!scalar @names) {
		$self->error('Bad names in author/editor field');
		return;
	    }
	    $self->{"_$type"} = [map {new BibTeX::Parser::Author $_} @names];
	    $self->field($type, join " and ", @{$self->{"_$type"}});
	} else {
	    $self->{"_$type"} = [];
	    foreach my $param (@_) {
		if (ref $param eq "BibTeX::Author") {
		    push @{$self->{"_$type"}}, $param;
		} else {
		    push @{$self->{"_$type"}}, new BibTeX::Parser::Author $param;
		}
		
		$self->field($type, join " and ", @{$self->{"_$type"}});
	    }
	}
    } else {
	unless ( defined $self->{"_$type"}) {
	    my @names = BibTeX::Parser::_split_braced_string($self->{$type} || "", '\s+and\s+' );
	    $self->{"_$type"} = [map {new BibTeX::Parser::Author $_} @names];
	}
	return @{$self->{"_$type"}};
    }
}



sub author {
	_handle_author_editor('author', @_);
}


sub editor {
	_handle_author_editor('editor', @_);
}


sub fieldlist {
	my $self = shift;
	
	return grep {!/^_/} keys %$self;	
}


sub has {
	my ($self, $field) = @_;

	return defined $self->{$field};
}

sub _sanitize_field {
	my $value = shift;	
	for ($value) {
		tr/\{\}//d;
		s/\\(?!=[ \\])//g;
		s/\\\\/\\/g;
	}
	return $value;
}



sub raw_bibtex {
	my $self = shift;
	if (@_) {
		$self->{_raw} = shift;
	}
	return $self->{_raw};
}

sub pre {
	my $self = shift;
	if (@_) {
		$self->{_pre} = shift;
	}
	return $self->{_pre};
}


sub to_string {
    my $self = shift;
    my %options=@_;
    if (!exists($options{canonize_names})) {
	$options{canonize_names}=1;
    }
    my @fields = grep {!/^_/} keys %$self;
    @fields = sort {
	$self->{_fieldnums}->{$a} <=> 
	    $self->{_fieldnums}->{$b}} @fields;
    my $result = '';
    if ($options{print_pre}) {
	$result .= $self->pre()."\n";
    }
    my $type = $self->type;
    if (exists($options{type_capitalization})) {
	if ($options{type_capitalization} eq 'Lowercase') {
	    $type = lc $type;
	}
	if ($options{type_capitalization} eq 'Titlecase') {
	    $type = ucfirst lc $type;
	}
    }
    $result .= '@'.$type."{".$self->key.",\n";    
    foreach my $field (@fields) {
	my $value = $self->field($field);
	if ($field eq 'author' && $options{canonize_names}) {
	    my @names = ($self->author);
	    $value = join(' and ', @names);
	}
	if ($field eq 'editor' && $options{canonize_names}) {
	    my @names = ($self->editor);
	    $value = join(' and ', @names);
	}
	if (exists($options{field_capitalization})) {
	    if ($options{field_capitalization} eq 'Uppercase') {
		$field = uc $field;
	    }
	    if ($options{field_capitalization} eq 'Titlecase') {
		$field = ucfirst  $field;
	    }
	}
	$result .= "    $field = {"."$value"."},\n";	
    }
    $result .= "}";
    return $result;
}

1; # End of BibTeX::Entry

__END__
=pod

=head1 NAME

BibTeX::Parser::Entry - Contains a single entry of a BibTeX document.

=head1 SYNOPSIS

This class ist a wrapper for a single BibTeX entry. It is usually created
by a BibTeX::Parser.

    use BibTeX::Parser::Entry;

    my $entry = BibTeX::Parser::Entry->new($type, $key, $parse_ok, \%fields);
    
    if ($entry->parse_ok) {
	    my $type    = $entry->type;
	    my $key     = $enty->key;
	    print $entry->field("title");
	    my @authors = $entry->author;
	    my @editors = $entry->editor;

	    ...

	    print $entry->to_string;
    }

   



=head1 FUNCTIONS

=head2 new

Create new entry.

=head2 parse_ok

If the entry was correctly parsed, this method returns a true value, false otherwise.

=head2 error

Return the error message, if the entry could not be parsed or undef otherwise.

=head2 type

Get or set the type of the entry, eg. 'ARTICLE' or 'BOOK'. Return value is 
always uppercase.

=head2 key

Get or set the reference key of the entry.

=head2 field($name [, $value])

Get or set the contents of a field. The first parameter is the name of the
field, the second (optional) value is the new value.

=head2 cleaned_field($name)

Retrieve the contents of a field in a format that is cleaned of TeX markup.

=head2 cleaned_author

Get an array of L<BibTeX::Parser::Author> objects for the authors of this
entry. Each name has been cleaned of accents and braces.

=head2 cleaned_editor

Get an array of L<BibTeX::Parser::Author> objects for the editors of this
entry. Each name has been cleaned of accents and braces.

=head2 author([@authors])

Get or set the authors. Returns an array of L<BibTeX::Author|BibTeX::Author> 
objects. The parameters can either be L<BibTeX::Author|BibTeX::Author> objects
or strings.

Note: You can also change the authors with $entry->field('author', $authors_string)

=head2 editor([@editors])

Get or set the editors. Returns an array of L<BibTeX::Author|BibTeX::Author> 
objects. The parameters can either be L<BibTeX::Author|BibTeX::Author> objects
or strings.

Note: You can also change the authors with $entry->field('editor', $editors_string)

=head2 fieldlist ()

Returns a list of all the fields used in this entry.

=head2 has($fieldname)

Returns a true value if this entry has a value for $fieldname.

=head2 pre ()

Return the text in BibTeX file before the entry

=head2 raw_bibtex ()

Return raw BibTeX entry (if available).

=head2 to_string ([options])

Returns a text of the BibTeX entry in BibTeX format.  Options are
a hash.  

=over 4

=item C<canonize_names>

If true (the default), authors' and editors' 
names are translated into canonical bibtex form.  The command 
C<$entry-E<gt>to_string(canonize_names=E<gt>0)> overrides this behavior.

=item C<field_capitalization>

Capitalization of the field names.  
Can take values 'Uppercase', 'Lowercase' (the default) or 'Titlecase'

=item C<print_pre>

False by default.  If true, the text in the Bib file before the
entry is printed.  Note that at present we assume the text 
before the entry NEVER has the @ symbol inside

=item C<type_capitalization>

Capitalization of the type names.  
Can take values 'Uppercase' (the default), 'Lowercase' or 'Titlecase'


=back

=head1 VERSION

version 1.02

=head1 AUTHOR

Gerhard Gossen <gerhard.gossen@googlemail.com> and
Boris Veytsman <boris@varphi.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2013-2016 by Gerhard Gossen and Boris Veytsman

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut