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
|
package Text::BibTeX::Validate;
use strict;
use warnings;
# ABSTRACT: validator for BibTeX format
our $VERSION = '0.3.0'; # VERSION
use Algorithm::CheckDigits;
use Data::Validate::Email qw( is_email_rfc822 );
use Data::Validate::URI qw( is_uri );
use Scalar::Util qw( blessed );
use Text::BibTeX::Validate::Warning;
require Exporter;
our @ISA = qw( Exporter );
our @EXPORT_OK = qw(
clean_BibTeX
shorten_DOI
validate_BibTeX
);
my @months = qw(
january
february
march
april
may
june
july
august
september
october
november
december
);
=head1 NAME
Text::BibTeX::Validate - validator for BibTeX format
=head1 SYNOPSIS
use Text::BibTeX;
use Text::BibTeX::Validate qw( validate_BibTeX );
my $bibfile = Text::BibTeX::File->new( 'bibliography.bib' );
while( my $entry = Text::BibTeX::Entry->new( $bibfile ) ) {
for my $warning (validate_BibTeX( $entry )) {
print STDERR "$warning\n";
}
}
=head1 DESCRIPTION
Text::BibTeX::Validate checks the standard fields of BibTeX entries for
their compliance with their format. In particular, value of C<email> is
checked against RFC 822 mandated email address syntax, value of C<doi>
is checked to start with C<10.> and contain at least one C</> and so on.
Some nonstandard fields as C<isbn>, C<issn> and C<url> are also checked.
Failures of checks are returned as instances of
L<Text::BibTeX::Validate::Warning|Text::BibTeX::Validate::Warning>.
=head1 METHODS
=cut
sub shorten_DOI($);
=head2 validate_BibTeX( $what )
Takes plain Perl hash reference containing BibTeX fields and their
values, as well as L<Text::BibTeX::Entry|Text::BibTeX::Entry> instances
and returns an array of validation messages as instances of
L<Text::BibTeX::Validate::Warning|Text::BibTeX::Validate::Warning>.
=cut
sub validate_BibTeX
{
my( $what ) = @_;
my $entry = _convert( $what );
my @warnings;
# Report and remove empty keys
for my $key (sort keys %$entry) {
next if defined $entry->{$key};
push @warnings,
_warn_value( 'undefined value', $entry, $key );
delete $entry->{$key};
}
if( exists $entry->{email} &&
!defined is_email_rfc822 $entry->{email} ) {
push @warnings,
_warn_value( 'value \'%(value)s\' does not look like valid ' .
'email address',
$entry,
'email' );
}
if( exists $entry->{doi} ) {
my $doi = $entry->{doi};
my $doi_now = shorten_DOI $doi;
if( $doi_now !~ m|^10\.[^/]+/| ) {
push @warnings,
_warn_value( 'value \'%(value)s\' does not look like valid DOI',
$entry,
'doi' );
} elsif( $doi ne $doi_now ) {
push @warnings,
_warn_value( 'value \'%(value)s\' is better written as \'%(suggestion)s\'',
$entry,
'doi',
{ suggestion => $doi_now } );
}
}
# Validated according to BibTeX recommendations
if( exists $entry->{month} ) {
if( $entry->{month} =~ /^0?[1-9]|1[12]$/ ) {
push @warnings,
_warn_value( 'value \'%(value)s\' is better written as \'%(suggestion)s\'',
$entry,
'month',
{ suggestion => ucfirst substr( $months[$entry->{month}-1], 0, 3 ) } );
} elsif( grep { lc $entry->{month} eq $_ && length $_ > 3 } @months ) {
push @warnings,
_warn_value( 'value \'%(value)s\' is better written as \'%(suggestion)s\'',
$entry,
'month',
{ suggestion => ucfirst substr( $entry->{month}, 0, 3 ) } );
} elsif( !(grep { lc $entry->{month} eq substr( $_, 0, 3 ) ||
lc $entry->{month} eq substr( $_, 0, 3 ) . '.' } @months) ) {
push @warnings,
_warn_value( 'value \'%(value)s\' does not look like valid month',
$entry,
'month' );
}
}
if( exists $entry->{year} ) {
# Sometimes bibliographies list the next year to show that they
# are going to be published soon.
my @localtime = localtime;
if( $entry->{year} !~ /^[0-9]{4}$/ ) {
push @warnings,
_warn_value( 'value \'%(value)s\' does not look like valid year',
$entry,
'year' );
} elsif( $entry->{year} > $localtime[5] + 1901 ) {
push @warnings,
_warn_value( 'value \'%(value)s\' is too far in the future',
$entry,
'year' );
}
}
# Both keys are nonstandard
for my $key ('isbn', 'issn') {
next if !exists $entry->{$key};
my $check = CheckDigits( $key );
if( $key eq 'isbn' ) {
my $value = $entry->{$key};
$value =~ s/-//g;
if( length $value == 13 ) {
$check = CheckDigits( 'isbn13' );
}
}
next if $check->is_valid( $entry->{$key} );
push @warnings,
_warn_value( 'value \'%(value)s\' does not look like valid %(FIELD)s',
$entry,
$key,
{ FIELD => uc $key } );
}
# Both keys are nonstandard
for my $key ('eprint', 'url') {
next if !exists $entry->{$key};
next if defined is_uri $entry->{$key};
if( $entry->{$key} =~ /^(.*)\n$/ && defined is_uri $1 ) {
# BibTeX converted from YAML (i.e., Debian::DEP12) might
# have trailing newline character attached.
push @warnings,
_warn_value( 'URL has trailing newline character',
$entry,
$key,
{ suggestion => $1 } );
next;
}
push @warnings,
_warn_value( 'value \'%(value)s\' does not look like valid URL',
$entry,
$key );
}
# Nonstandard
if( exists $entry->{pmid} ) {
if( $entry->{pmid} =~ /^PMC[0-9]{7}$/ ) {
push @warnings,
_warn_value( 'PMCID \'%(value)s\' is provided instead of PMID',
$entry,
'pmid' );
} elsif( $entry->{pmid} !~ /^[1-9][0-9]*$/ ) {
push @warnings,
_warn_value( 'value \'%(value)s\' does not look like valid PMID',
$entry,
'pmid' );
}
}
return @warnings;
}
=head2 clean_BibTeX( $what )
Takes the same input as C<validate_BibTeX> and attempts to reconcile
trivial issues like dropping the resolver URL part of DOIs (see
C<shorten_DOI> method) and converting month numbers into three-letter
abbreviations.
=cut
sub clean_BibTeX
{
my( $what ) = @_;
my $entry = _convert( $what );
# Deleting undefined values prior to the validation
for (keys %$entry) {
delete $entry->{$_} if !defined $entry->{$_};
}
my @warnings = validate_BibTeX( $entry );
my @suggestions = grep { $_->{suggestion} } @warnings;
for my $suggestion (@suggestions) {
$entry->{$suggestion->{field}} = $suggestion->{suggestion};
}
return $entry;
}
=head2 shorten_DOI( $doi )
Remove the resolver URL part, as well as C<doi:> prefixes, from DOIs.
=cut
sub shorten_DOI($)
{
my( $doi ) = @_;
return $doi if $doi =~ s|^https?://(dx\.)?doi\.org/||;
return $doi if $doi =~ s|^doi:||;
return $doi;
}
sub _convert
{
my( $what ) = @_;
if( blessed $what && $what->isa( 'Text::BibTeX::Entry' ) ) {
$what = { map { $_ => $what->get($_) } $what->fieldlist };
}
# TODO: check for duplicated keys
return { map { lc $_ => $what->{$_} } keys %$what };
}
sub _warn_value
{
my( $message, $entry, $field, $extra ) = @_;
$extra = {} unless $extra;
return Text::BibTeX::Validate::Warning->new(
$message,
{ field => $field,
value => $entry->{$field},
%$extra } );
}
=head1 AUTHORS
Andrius Merkys, E<lt>merkys@cpan.orgE<gt>
=cut
1;
|