Parser/Entry.pm

filter: Statementstmt 81.8% 131 / 160 Branchbran 78.5% 44 / 56 Conditioncond 100% 8 / 8 MC/DCmcdc 100% 5 / 5 Subroutinesub 91.3% 21 / 23 Podpod 100% 16 / 16 totaltotal 83.9% 225 / 268 CC 36 SCAR 21.5
CC
36
Cov
82%
_handle_author_editor
26.3
to_string
14.0
_sanitize_field
6.0
CRAP 8.6
« Parser.pm ↑ Summary
1 package BibTeX::Parser::Entry;
2 {
3 20 $BibTeX::Parser::Entry::VERSION = '1.95';
4 }
5  
6 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 20 require BibTeX::Parser::Author; # mutual dependencies
10 20 require BibTeX::Parser;
11  
12 140 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
13 140 my ($class, $type, $key, $parse_ok, $fieldsref) = @_;
14  
15 140 ▶ my %fields = defined $fieldsref ? %$fieldsref : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $fieldsref ? : 93 47 140
16 140 my $i=0;
17 140 foreach my $field (keys %fields) {
18 93 ▶ if ($field !~ /^_/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($field =~ /^_/) 93 0 93
19 93 $fields{_fieldnums}->{$field}=$i;
20 93 $i++;
21 }
22 }
23 140 ▶ if (defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) 93 47 140
24 93 $fields{_type} = uc($type);
25 }
26 140 $fields{_key} = $key;
27 140 $fields{_parse_ok} = $parse_ok;
28 140 $fields{_raw} = '';
29 140 return bless \%fields, $class;
30 }
31  
32  
33  
34 74 ▶sub parse_ok {
Subroutine parse_ok: called CC=2 SCAR=6.9
Pod documented
35 74 my $self = shift;
36 74 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 45 29 74
37 45 $self->{_parse_ok} = shift;
38 }
39 74 $self->{_parse_ok};
40 }
41  
42  
43 4 ▶sub error {
Subroutine error: called CC=3 SCAR=11.3
Pod documented
44 4 my $self = shift;
45 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 0 4
46 4 $self->{_error} = shift;
47 4 $self->parse_ok(0);
48 }
49 4 ▶ return $self->parse_ok ? undef : $self->{_error};
Branch: 1 branch
BranchTrueFalseTotal
$self->parse_ok ? : 0 4 4
50 }
51  
52  
53 75 ▶sub type {
Subroutine type: called CC=2 SCAR=6.9
Pod documented
54 75 ▶ if (scalar @_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) { } 29 46 75
55 # get
56 29 my $self = shift;
57 29 return $self->{_type};
58 } else {
59 # set
60 46 my ($self, $newval) = @_;
61 46 $self->{_type} = uc($newval);
62 }
63 }
64  
65  
66 108 ▶sub key {
Subroutine key: called CC=2 SCAR=6.9
Pod documented
67 108 ▶ if (scalar @_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) { } 68 40 108
68 # get
69 68 my $self = shift;
70 68 return $self->{_key};
71 } else {
72 # set
73 40 my ($self, $newval) = @_;
74 40 $self->{_key} = $newval;
75 }
76  
77 }
78  
79  
80 372 ▶sub field {
Subroutine field: called CC=3 SCAR=11.0
Pod documented
81 372 ▶ if (scalar @_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 2) { } 199 173 372
82 # get
83 199 my ($self, $field) = @_;
84 199 return $self->{ lc( $field ) };
85 } else {
86 173 my ($self, $key, $value) = @_;
87 173 my $field = lc ($key);
88 173 $self->{$field} = $value; #_sanitize_field($value);
89 173 ▶ if (!exists($self->{_fieldnums}->{$field})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{"_fieldnums"}{$field}) 172 1 173
90 172 my $num = scalar keys %{$self->{_fieldnums}};
91 172 $self->{_fieldnums}->{$field} = $num;
92 }
93 }
94  
95 }
96  
97 20 ▶use LaTeX::ToUnicode qw( convert );
Subroutine BEGIN: called CC=1 SCAR=0.0
98  
99  
100 91 ▶sub cleaned_field {
Subroutine cleaned_field: called CC=2 SCAR=7.6
Pod documented
101 91 my ( $self, $field, @options ) = @_;
102 91 ▶ if ( $field =~ /author|editor/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field =~ /author|editor/i) { } 0 91 91
103 0 return $self->field( $field );
104 } else {
105 91 return convert( $self->field( lc $field ), @options );
106 }
107 }
108  
109  
110 1 ▶sub cleaned_author {
Subroutine cleaned_author: called CC=1 SCAR=0.0
Pod documented
111 1 my $self = shift;
112 1 $self->_handle_cleaned_author_editor( [ $self->author ], @_ );
113 }
114  
115  
116 0 ▶sub cleaned_editor {
Subroutine cleaned_editor: not called CC=1 SCAR=6.9
Pod documented
117 0 my $self = shift;
118 0 $self->_handle_cleaned_author_editor( [ $self->editor ], @_ );
119 }
120  
121 1 ▶sub _handle_cleaned_author_editor {
Subroutine _handle_cleaned_author_editor: called CC=1 SCAR=0.0
122 1 my ( $self, $authors, @options ) = @_;
123 map {
124 1 my $author = $_;
125 2 my $new_author = BibTeX::Parser::Author->new;
126 map {
127 4 $new_author->$_( convert( $author->$_, @options ) )
128 2 } grep { defined $author->$_ } qw( first von last jr );
129 2 $new_author;
130 } @$authors;
131 }
132  
133 20 ▶no LaTeX::ToUnicode;
Subroutine BEGIN: called CC=1 SCAR=0.0
134  
135 34 ▶sub _handle_author_editor {
Subroutine _handle_author_editor: called CC=8 SCAR=32.7
136 34 my $type = shift;
137 34 my $self = shift;
138 34 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 34 34
139 0 ▶ if (@_ == 1) { #single string
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
140 # my @names = split /\s+and\s+/i, $_[0];
141 0 $_[0] =~ s/^\s*//;
142 0 $_[0] =~ s/\s*$//;
143 0 my @names = BibTeX::Parser::_split_braced_string($_[0],
144 '\s+and\s+');
145 0 ▶ if (!scalar @names) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @names) 0 0 0
146 0 $self->error('Bad names in author/editor field');
147 0 return;
148 }
149 0 $self->{"_$type"} = [map {new BibTeX::Parser::Author $_} @names];
150 0 $self->field($type, join " and ", @{$self->{"_$type"}});
151 } else {
152 0 $self->{"_$type"} = [];
153 0 foreach my $param (@_) {
154 0 ▶ if (ref $param eq "BibTeX::Author") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $param eq 'BibTeX::Author') { } 0 0 0
155 0 push @{$self->{"_$type"}}, $param;
156 } else {
157 0 push @{$self->{"_$type"}}, new BibTeX::Parser::Author $param;
158 }
159
160 0 $self->field($type, join " and ", @{$self->{"_$type"}});
161 }
162 }
163 } else {
164 34 ▶ unless ( defined $self->{"_$type"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$self{"_$type"}) 18 16 34
165 18 ▶ my @names = BibTeX::Parser::_split_braced_string($self->{$type} || "", '\s+and\s+' );
Condition100%2/2
$self->{$type} || ''
l!l
171
MC/DC100%1/1
$self->{$type} || ''
$self->{$type}
Truth table100%2/2
A
A= $self->{$type}
Aresult
11
00
166 18 $self->{"_$type"} = [map {new BibTeX::Parser::Author $_} @names];
167 }
168 34 return @{$self->{"_$type"}};
169 }
170 }
171  
172  
173  
174 25 ▶sub author {
Subroutine author: called CC=1 SCAR=0.0
Pod documented
175 25 _handle_author_editor('author', @_);
176 }
177  
178  
179 9 ▶sub editor {
Subroutine editor: called CC=1 SCAR=0.0
Pod documented
180 9 _handle_author_editor('editor', @_);
181 }
182  
183  
184 1 ▶sub fieldlist {
Subroutine fieldlist: called CC=1 SCAR=0.0
Pod documented
185 1 my $self = shift;
186
187 1 return grep {!/^_/} keys %$self;
188 }
189  
190  
191 3 ▶sub has {
Subroutine has: called CC=1 SCAR=0.0
Pod documented
192 3 my ($self, $field) = @_;
193  
194 3 return defined $self->{$field};
195 }
196  
197 0 ▶sub _sanitize_field {
Subroutine _sanitize_field: not called CC=2 SCAR=17.9
198 0 my $value = shift;
199 0 for ($value) {
200 0 tr/\{\}//d;
201 0 s/\\(?!=[ \\])//g;
202 0 s/\\\\/\\/g;
203 }
204 0 return $value;
205 }
206  
207  
208  
209 45 ▶sub raw_bibtex {
Subroutine raw_bibtex: called CC=2 SCAR=7.0
Pod documented
210 45 my $self = shift;
211 45 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 45 0 45
212 45 $self->{_raw} = shift;
213 }
214 45 return $self->{_raw};
215 }
216  
217 48 ▶sub pre {
Subroutine pre: called CC=2 SCAR=6.9
Pod documented
218 48 my $self = shift;
219 48 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 45 3 48
220 45 $self->{_pre} = shift;
221 }
222 48 return $self->{_pre};
223 }
224  
225  
226 16 ▶sub to_string {
Subroutine to_string: called CC=14 SCAR=26.4
Pod documented
227 16 my $self = shift;
228 16 my %options=@_;
229 16 ▶ if (!exists($options{canonize_names})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $options{"canonize_names"}) 15 1 16
230 15 $options{canonize_names}=1;
231 }
232 16 my @fields = grep {!/^_/} keys %$self;
233 @fields = sort {
234 16 $self->{_fieldnums}->{$a} <=>
235 103 $self->{_fieldnums}->{$b}} @fields;
236 16 my $result = '';
237 16 ▶ if ($options{print_pre}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"print_pre"}) 3 13 16
238 3 $result .= $self->pre()."\n";
239 }
240 16 my $type = $self->type;
241 16 ▶ if (exists($options{type_capitalization})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{"type_capitalization"}) 3 13 16
242 3 ▶ if ($options{type_capitalization} eq 'Lowercase') {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"type_capitalization"} eq "Lowercase") 1 2 3
243 1 $type = lc $type;
244 }
245 3 ▶ if ($options{type_capitalization} eq 'Titlecase') {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"type_capitalization"} eq "Titlecase") 1 2 3
246 1 $type = ucfirst lc $type;
247 }
248 }
249 16 print STDERR $self->key, "\n";
250 16 $result .= '@'.$type."{".$self->key.",\n";
251 16 foreach my $field (@fields) {
252 77 my $value = $self->field($field);
253 77 ▶ if ($field eq 'author' && $options{canonize_names}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq "author" and $options{"canonize_names"}) 15 62 77
Condition100%3/3
$field eq "author" and $options{"canonize_names"}
!ll&&!rl&&r
61115
MC/DC100%2/2
$field eq "author" and $options{"canonize_names"}
$field eq "author" $options{"canonize_names"}
Truth table100%3/3
A and B
A= $field eq "author"B= $options{"canonize_names"}
ABresult
0X0
100
111
254 15 my @names = ($self->author);
255 15 $value = join(' and ', @names);
256 }
257 77 ▶ if ($field eq 'editor' && $options{canonize_names}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq "editor" and $options{"canonize_names"}) 8 69 77
Condition100%3/3
$field eq "editor" and $options{"canonize_names"}
!ll&&!rl&&r
6818
MC/DC100%2/2
$field eq "editor" and $options{"canonize_names"}
$field eq "editor" $options{"canonize_names"}
Truth table100%3/3
A and B
A= $field eq "editor"B= $options{"canonize_names"}
ABresult
0X0
100
111
258 8 my @names = ($self->editor);
259 8 $value = join(' and ', @names);
260 }
261 77 ▶ if (exists($options{field_capitalization})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{"field_capitalization"}) 15 62 77
262 15 ▶ if ($options{field_capitalization} eq 'Uppercase') {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"field_capitalization"} eq "Uppercase") 5 10 15
263 5 $field = uc $field;
264 }
265 15 ▶ if ($options{field_capitalization} eq 'Titlecase') {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"field_capitalization"} eq "Titlecase") 5 10 15
266 5 $field = ucfirst $field;
267 }
268 }
269 77 $result .= " $field = {"."$value"."},\n";
270 }
271 16 $result .= "}";
272 16 return $result;
273 }
274  
275 20 1; # End of BibTeX::Entry
276  
277 __END__
« Parser.pm ↑ Summary