summaryrefslogtreecommitdiff
path: root/support/crossrefware/biburl2doi.pl
blob: 9482ba2931569645e6a05bb59d695b1259e93870 (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
#!/usr/bin/env perl

=pod

=head1 NAME

biburl2doi.pl - convert URLs pointing to doi.org to DOIs

=head1 SYNOPSIS

biburl2doi  [B<-D>] [B<-o> I<output>] I<bib_file>

=head1 OPTIONS

=over 4

=item B<-D> 

Do not delete URLs converted to DOIs


=item B<-o> I<output>

Output file.  If this option is not used, the name for the 
output file is formed by adding C<_cleaned> to the input file

=back

=head1 DESCRIPTION

The script recognizes URL fields of the kind
C<http://dx.doi.org> and their variants and converts them to DOI
fields.  


=head1 AUTHOR

Boris Veytsman

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2022  Boris Veytsman

This is free software.  You may redistribute copies of it under the
terms of the GNU General Public License
L<http://www.gnu.org/licenses/gpl.html>.  There is NO WARRANTY, to the
extent permitted by law.

=cut

use strict;
BEGIN {
    # find files relative to our installed location within TeX Live
    chomp(my $TLMaster = `kpsewhich -var-value=TEXMFROOT`); # TL root
    if (length($TLMaster)) {
	unshift @INC, "$TLMaster/texmf-dist/scripts/bibtexperllibs";
    }
}
use IO::File;
use BibTeX::Parser;
use LaTeX::ToUnicode qw (convert);
use Getopt::Std;

my $USAGE="USAGE: $0 [-D] [-o output] file\n";
my $VERSION = <<END;
biburl2doi 1.0
This is free software.  You may redistribute copies of it under the
terms of the GNU General Public License
http://www.gnu.org/licenses/gpl.html.  There is NO WARRANTY, to the
extent permitted by law.
$USAGE
END
our %opts;
getopts('Do:hV',\%opts) or die $USAGE;

if ($opts{h} || $opts{V}){
    print $VERSION;
    exit 0;
}

################################################################
# Defaults and parameters
################################################################

my $inputfile = shift;

my $outputfile = $inputfile;

$outputfile =~ s/\.([^\.]*)$/_cleaned.$1/;

if (exists $opts{o}) {
    $outputfile = $opts{o};
}

my $deleteUrl = 1;
if (exists $opts{D}) {
    $deleteUrl = 0;
}		


my $input= IO::File->new($inputfile) or 
    die "Cannot find BibTeX file $inputfile\n$USAGE\n";
my $output = IO::File->new("> $outputfile") or 
    die "Cannot write to $outputfile\n$USAGE\n";

my $parser=new BibTeX::Parser($input);



# Processing the input
while (my $entry = $parser->next) {
    if (!$entry->parse_ok()) {
	print STDERR "Cannot understand entry: ";
	$entry->print(*STDERR);
	print STDERR "Skipping this entry\n";
	next;
    }

    if ($entry->has('doi') || !$entry->has('url')) {
	print $output $entry->raw_bibtex(), "\n\n";
	next;
    }

    if ($entry->field('url') =~ m|^http(?:s)?://(?:dx\.)?doi\.org/(.*)$|) {
	$entry->field('doi', $1);
	if ($deleteUrl) {
	    delete $entry->{'url'};
	}
	print $output $entry->to_string(), "\n\n";
    } else {
	print $output $entry->raw_bibtex(), "\n\n";
    }
}