Validate.pm

filter: Statementstmt 90.9% 90 / 99 Branchbran 75.9% 41 / 54 Conditioncond 60.0% 9 / 15 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 82.3% 159 / 193 CC 38 SCAR 32.9
CC
38
Cov
83%
validate_BibTeX
42.1
clean_BibTeX
4.0
shorten_DOI
3.0
CRAP 26.7
↑ Summary Validate/Warning.pm »
1 package Text::BibTeX::Validate;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: validator for BibTeX format
7 3 our $VERSION = '0.3.0'; # VERSION
8  
9 3 ▶use Algorithm::CheckDigits;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Data::Validate::Email qw( is_email_rfc822 );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Data::Validate::URI qw( is_uri );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Text::BibTeX::Validate::Warning;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 require Exporter;
16 3 our @ISA = qw( Exporter );
17 3 our @EXPORT_OK = qw(
18 clean_BibTeX
19 shorten_DOI
20 validate_BibTeX
21 );
22  
23 3 my @months = qw(
24 january
25 february
26 march
27 april
28 may
29 june
30 july
31 august
32 september
33 october
34 november
35 december
36 );
37  
38 =head1 NAME
39  
40 Text::BibTeX::Validate - validator for BibTeX format
41  
42 =head1 SYNOPSIS
43  
44 use Text::BibTeX;
45 use Text::BibTeX::Validate qw( validate_BibTeX );
46  
47 my $bibfile = Text::BibTeX::File->new( 'bibliography.bib' );
48 while( my $entry = Text::BibTeX::Entry->new( $bibfile ) ) {
49 for my $warning (validate_BibTeX( $entry )) {
50 print STDERR "$warning\n";
51 }
52 }
53  
54 =head1 DESCRIPTION
55  
56 Text::BibTeX::Validate checks the standard fields of BibTeX entries for
57 their compliance with their format. In particular, value of C<email> is
58 checked against RFC 822 mandated email address syntax, value of C<doi>
59 is checked to start with C<10.> and contain at least one C</> and so on.
60 Some nonstandard fields as C<isbn>, C<issn> and C<url> are also checked.
61 Failures of checks are returned as instances of
62 L<Text::BibTeX::Validate::Warning|Text::BibTeX::Validate::Warning>.
63  
64 =head1 METHODS
65  
66 =cut
67  
68 sub shorten_DOI($);
69  
70 =head2 validate_BibTeX( $what )
71  
72 Takes plain Perl hash reference containing BibTeX fields and their
73 values, as well as L<Text::BibTeX::Entry|Text::BibTeX::Entry> instances
74 and returns an array of validation messages as instances of
75 L<Text::BibTeX::Validate::Warning|Text::BibTeX::Validate::Warning>.
76  
77 =cut
78  
79 sub validate_BibTeX
80 20 ▶{
Subroutine validate_BibTeX: called CC=30 SCAR=37.4
Pod documented
81 20 my( $what ) = @_;
82 20 my $entry = _convert( $what );
83  
84 20 my @warnings;
85  
86 # Report and remove empty keys
87 20 for my $key (sort keys %$entry) {
88 38 ▶ next if defined $entry->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $entry->{$key} 38 0 38
89 0 push @warnings,
90 _warn_value( 'undefined value', $entry, $key );
91 0 delete $entry->{$key};
92 }
93  
94 20 ▶ if( exists $entry->{email} &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $entry->{"email"} and not defined is_email_rfc822($entry->{"email"})) 0 20 20
Condition33%1/3
exists $entry->{"email"} and not defined is_email_rfc822($entry->{"email"})
!ll&&!rl&&r
2000
MC/DC0%0/2
exists $entry->{"email"} and not defined is_email_rfc822($entry->{"email"})
exists $entry->{"email"} not defined is_email_rfc822($entry->{"email"})
Truth table33%1/3
A and not(B)
A= exists $entry->{"email"}B= not defined is_email_rfc822($entry->{"email"})
ABresult
0X0
100
111
95 !defined is_email_rfc822 $entry->{email} ) {
96 0 push @warnings,
97 _warn_value( 'value \'%(value)s\' does not look like valid ' .
98 'email address',
99 $entry,
100 'email' );
101 }
102  
103 20 ▶ if( exists $entry->{doi} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $entry->{"doi"}) 6 14 20
104 6 my $doi = $entry->{doi};
105 6 my $doi_now = shorten_DOI $doi;
106  
107 6 ▶ if( $doi_now !~ m|^10\.[^/]+/| ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $doi_now =~ m[^10\.[^/]+/]) { } 3 3 6
elsif ($doi ne $doi_now) { } 2 1 3
108 3 push @warnings,
109 _warn_value( 'value \'%(value)s\' does not look like valid DOI',
110 $entry,
111 'doi' );
112 } elsif( $doi ne $doi_now ) {
113 2 push @warnings,
114 _warn_value( 'value \'%(value)s\' is better written as \'%(suggestion)s\'',
115 $entry,
116 'doi',
117 { suggestion => $doi_now } );
118 }
119 }
120  
121 # Validated according to BibTeX recommendations
122 20 ▶ if( exists $entry->{month} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $entry->{"month"}) 8 12 20
123 8 ▶ if( $entry->{month} =~ /^0?[1-9]|1[12]$/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($entry->{'month'} =~ /^0?[1-9]|1[12]$/) { } 2 6 8
elsif (grep {lc $entry->{'month'} eq $_ and length $_ > 3;} @months) { } 2 4 6
elsif (not grep({lc $entry->{'month'} eq substr($_, 0, 3) or lc $entry->{'month'} eq substr($_, 0, 3) . '.';} @months)) { } 0 4 4
124 push @warnings,
125 _warn_value( 'value \'%(value)s\' is better written as \'%(suggestion)s\'',
126 $entry,
127 'month',
128 2 { suggestion => ucfirst substr( $months[$entry->{month}-1], 0, 3 ) } );
129 72 ▶ } elsif( grep { lc $entry->{month} eq $_ && length $_ > 3 } @months ) {
Condition100%3/3
lc $entry->{'month'} eq $_ && length $_ > 3
!ll&&!rl&&r
6822
MC/DC100%2/2
lc $entry->{'month'} eq $_ && length $_ > 3
lc $entry->{'month'} eq $_ length $_ > 3
Truth table100%3/3
A && B
A= lc $entry->{'month'} eq $_B= length $_ > 3
ABresult
0X0
100
111
130 push @warnings,
131 _warn_value( 'value \'%(value)s\' is better written as \'%(suggestion)s\'',
132 $entry,
133 'month',
134 2 { suggestion => ucfirst substr( $entry->{month}, 0, 3 ) } );
135 } elsif( !(grep { lc $entry->{month} eq substr( $_, 0, 3 ) ||
136 48 ▶ lc $entry->{month} eq substr( $_, 0, 3 ) . '.' } @months) ) {
Condition66%2/3
lc $entry->{'month'} eq substr($_, 0, 3) || lc $entry->{'month'} eq substr($_, 0, 3) . '.'
l!l&&r!l&&!r
4044
MC/DC50%1/2
lc $entry->{'month'} eq substr($_, 0, 3) || lc $entry->{'month'} eq substr($_, 0, 3) . '.'
lc $entry->{'month'} eq substr($_, 0, 3) lc $entry->{'month'} eq substr($_, 0, 3) . '.'
Truth table66%2/3
A || B
A= lc $entry->{'month'} eq substr($_, 0, 3)B= lc $entry->{'month'} eq substr($_, 0, 3) . '.'
ABresult
1X1
011
000
137 0 push @warnings,
138 _warn_value( 'value \'%(value)s\' does not look like valid month',
139 $entry,
140 'month' );
141 }
142 }
143  
144 20 ▶ if( exists $entry->{year} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $entry->{"year"}) 2 18 20
145 # Sometimes bibliographies list the next year to show that they
146 # are going to be published soon.
147 2 my @localtime = localtime;
148 2 ▶ if( $entry->{year} !~ /^[0-9]{4}$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $entry->{'year'} =~ /^[0-9]{4}$/) { } 0 2 2
elsif ($entry->{'year'} > $localtime[5] + 1901) { } 0 2 2
149 0 push @warnings,
150 _warn_value( 'value \'%(value)s\' does not look like valid year',
151 $entry,
152 'year' );
153 } elsif( $entry->{year} > $localtime[5] + 1901 ) {
154 0 push @warnings,
155 _warn_value( 'value \'%(value)s\' is too far in the future',
156 $entry,
157 'year' );
158 }
159 }
160  
161 # Both keys are nonstandard
162 20 for my $key ('isbn', 'issn') {
163 40 ▶ next if !exists $entry->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $entry->{$key} 36 4 40
164 4 my $check = CheckDigits( $key );
165 4 ▶ if( $key eq 'isbn' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "isbn") 4 0 4
166 4 my $value = $entry->{$key};
167 4 $value =~ s/-//g;
168 4 ▶ if( length $value == 13 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $value == 13) 0 4 4
169 0 $check = CheckDigits( 'isbn13' );
170 }
171 }
172 4 ▶ next if $check->is_valid( $entry->{$key} );
Branch: 1 branch
BranchTrueFalseTotal
if $check->is_valid($entry->{$key}) 2 2 4
173 2 push @warnings,
174 _warn_value( 'value \'%(value)s\' does not look like valid %(FIELD)s',
175 $entry,
176 $key,
177 { FIELD => uc $key } );
178 }
179  
180 # Both keys are nonstandard
181 20 for my $key ('eprint', 'url') {
182 40 ▶ next if !exists $entry->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $entry->{$key} 36 4 40
183 4 ▶ next if defined is_uri $entry->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined is_uri($entry->{$key}) 2 2 4
184  
185 2 ▶ if( $entry->{$key} =~ /^(.*)\n$/ && defined is_uri $1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{$key} =~ /^(.*)\n$/ and defined is_uri($1)) 2 0 2
Condition33%1/3
$entry->{$key} =~ /^(.*)\n$/ and defined is_uri($1)
!ll&&!rl&&r
002
MC/DC0%0/2
$entry->{$key} =~ /^(.*)\n$/ and defined is_uri($1)
$entry->{$key} =~ /^(.*)\n$/ defined is_uri($1)
Truth table33%1/3
A and B
A= $entry->{$key} =~ /^(.*)\n$/B= defined is_uri($1)
ABresult
0X0
100
111
186 # BibTeX converted from YAML (i.e., Debian::DEP12) might
187 # have trailing newline character attached.
188 2 push @warnings,
189 _warn_value( 'URL has trailing newline character',
190 $entry,
191 $key,
192 { suggestion => $1 } );
193 2 next;
194 }
195  
196 0 push @warnings,
197 _warn_value( 'value \'%(value)s\' does not look like valid URL',
198 $entry,
199 $key );
200 }
201  
202 # Nonstandard
203 20 ▶ if( exists $entry->{pmid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $entry->{"pmid"}) 2 18 20
204 2 ▶ if( $entry->{pmid} =~ /^PMC[0-9]{7}$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($entry->{'pmid'} =~ /^PMC[0-9]{7}$/) { } 2 0 2
elsif (not $entry->{'pmid'} =~ /^[1-9][0-9]*$/) { } 0 0 0
205 2 push @warnings,
206 _warn_value( 'PMCID \'%(value)s\' is provided instead of PMID',
207 $entry,
208 'pmid' );
209 } elsif( $entry->{pmid} !~ /^[1-9][0-9]*$/ ) {
210 0 push @warnings,
211 _warn_value( 'value \'%(value)s\' does not look like valid PMID',
212 $entry,
213 'pmid' );
214 }
215 }
216  
217 20 return @warnings;
218 }
219  
220 =head2 clean_BibTeX( $what )
221  
222 Takes the same input as C<validate_BibTeX> and attempts to reconcile
223 trivial issues like dropping the resolver URL part of DOIs (see
224 C<shorten_DOI> method) and converting month numbers into three-letter
225 abbreviations.
226  
227 =cut
228  
229 sub clean_BibTeX
230 9 ▶{
Subroutine clean_BibTeX: called CC=4 SCAR=13.9
Pod documented
231 9 my( $what ) = @_;
232 9 my $entry = _convert( $what );
233  
234 # Deleting undefined values prior to the validation
235 9 for (keys %$entry) {
236 9 ▶ delete $entry->{$_} if !defined $entry->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry->{$_} 0 9 9
237 }
238  
239 9 my @warnings = validate_BibTeX( $entry );
240 9 my @suggestions = grep { $_->{suggestion} } @warnings;
241  
242 9 for my $suggestion (@suggestions) {
243 4 $entry->{$suggestion->{field}} = $suggestion->{suggestion};
244 }
245  
246 9 return $entry;
247 }
248  
249 =head2 shorten_DOI( $doi )
250  
251 Remove the resolver URL part, as well as C<doi:> prefixes, from DOIs.
252  
253 =cut
254  
255 sub shorten_DOI($)
256 6 ▶{
Subroutine shorten_DOI: called CC=3 SCAR=11.0
Pod documented
257 6 my( $doi ) = @_;
258  
259 6 ▶ return $doi if $doi =~ s|^https?://(dx\.)?doi\.org/||;
Branch: 1 branch
BranchTrueFalseTotal
if $doi =~ s[^https?://(dx\.)?doi\.org/][] 2 4 6
260 4 ▶ return $doi if $doi =~ s|^doi:||;
Branch: 1 branch
BranchTrueFalseTotal
if $doi =~ s/^doi:// 0 4 4
261 4 return $doi;
262 }
263  
264 sub _convert
265 29 ▶{
Subroutine _convert: called CC=3 SCAR=11.0
266 29 my( $what ) = @_;
267  
268 29 ▶ if( blessed $what && $what->isa( 'Text::BibTeX::Entry' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($what) and $what->isa("Text::BibTeX::Entry")) 2 27 29
Condition66%2/3
builtin::blessed($what) and $what->isa("Text::BibTeX::Entry")
!ll&&!rl&&r
2702
MC/DC50%1/2
builtin::blessed($what) and $what->isa("Text::BibTeX::Entry")
builtin::blessed($what) $what->isa("Text::BibTeX::Entry")
Truth table66%2/3
A and B
A= builtin::blessed($what)B= $what->isa("Text::BibTeX::Entry")
ABresult
0X0
100
111
269 2 $what = { map { $_ => $what->get($_) } $what->fieldlist };
270 }
271  
272 # TODO: check for duplicated keys
273 29 return { map { lc $_ => $what->{$_} } keys %$what };
274 }
275  
276 sub _warn_value
277 15 ▶{
Subroutine _warn_value: called CC=2 SCAR=6.9
278 15 my( $message, $entry, $field, $extra ) = @_;
279 15 ▶ $extra = {} unless $extra;
Branch: 1 branch
BranchTrueFalseTotal
unless $extra 5 10 15
280 return Text::BibTeX::Validate::Warning->new(
281 $message,
282 { field => $field,
283 15 value => $entry->{$field},
284 %$extra } );
285 }
286  
287 =head1 AUTHORS
288  
289 Andrius Merkys, E<lt>merkys@cpan.orgE<gt>
290  
291 =cut
292  
293 3 1;
↑ Summary Validate/Warning.pm »