Debian Codegraph @

texk/texlive/linked_scripts/crossrefware/biburl2doi.pl

raw
1 #!/usr/bin/env perl
2
3 =pod
4
5 =head1 NAME
6
7 biburl2doi.pl - convert URLs pointing to doi.org to DOIs
8
9 =head1 SYNOPSIS
10
11 biburl2doi [B<-D>] [B<-o> I<output>] I<bib_file>
12
13 =head1 OPTIONS
14
15 =over 4
16
17 =item B<-D>
18
19 Do not delete URLs converted to DOIs
20
21
22 =item B<-o> I<output>
23
24 Output file. If this option is not used, the name for the
25 output file is formed by adding C<_cleaned> to the input file
26
27 =back
28
29 =head1 DESCRIPTION
30
31 The script recognizes URL fields of the kind
32 C<http://dx.doi.org> and their variants and converts them to DOI
33 fields.
34
35
36 =head1 AUTHOR
37
38 Boris Veytsman
39
40 =head1 COPYRIGHT AND LICENSE
41
42 Copyright (C) 2021 Boris Veytsman
43
44 This is free software. You may redistribute copies of it under the
45 terms of the GNU General Public License
46 L<http://www.gnu.org/licenses/gpl.html>. There is NO WARRANTY, to the
47 extent permitted by law.
48
49 =cut
50
51 use strict;
52 BEGIN {
53 # find files relative to our installed location within TeX Live
54 chomp(my $TLMaster = `kpsewhich -var-value=TEXMFROOT`); # TL root
55 if (length($TLMaster)) {
56 unshift @INC, "$TLMaster/texmf-dist/scripts/bibtexperllibs";
57 }
58 }
59 use IO::File;
60 use BibTeX::Parser;
61 use LaTeX::ToUnicode qw (convert);
62 use Getopt::Std;
63
64 my $USAGE="USAGE: $0 [-D] [-o output] file\n";
65 my $VERSION = <<END;
66 biburl2doi 1.0
67 This is free software. You may redistribute copies of it under the
68 terms of the GNU General Public License
69 http://www.gnu.org/licenses/gpl.html. There is NO WARRANTY, to the
70 extent permitted by law.
71 $USAGE
72 END
73 our %opts;
74 getopts('Do:hV',\%opts) or die $USAGE;
75
76 if ($opts{h} || $opts{V}){
77 print $VERSION;
78 exit 0;
79 }
80
81 ################################################################
82 # Defaults and parameters
83 ################################################################
84
85 my $inputfile = shift;
86
87 my $outputfile = $inputfile;
88
89 $outputfile =~ s/\.([^\.]*)$/_cleaned.$1/;
90
91 if (exists $opts{o}) {
92 $outputfile = $opts{o};
93 }
94
95 my $deleteUrl = 1;
96 if (exists $opts{D}) {
97 $deleteUrl = 0;
98 }
99
100
101 my $input= IO::File->new($inputfile) or
102 die "Cannot find BibTeX file $inputfile\n$USAGE\n";
103 my $output = IO::File->new("> $outputfile") or
104 die "Cannot write to $outputfile\n$USAGE\n";
105
106 my $parser=new BibTeX::Parser($input);
107
108
109
110 # Processing the input
111 while (my $entry = $parser->next) {
112 if (!$entry->parse_ok()) {
113 print STDERR "Cannot understand entry: ";
114 $entry->print(*STDERR);
115 print STDERR "Skipping this entry\n";
116 next;
117 }
118
119 if ($entry->has('doi') || !$entry->has('url')) {
120 print $output $entry->raw_bibtex(), "\n\n";
121 next;
122 }
123
124 if ($entry->field('url') =~ m|^http(?:s)?://(?:dx\.)?doi\.org/(.*)$|) {
125 $entry->field('doi', $1);
126 if ($deleteUrl) {
127 delete $entry->{'url'};
128 }
129 print $output $entry->to_string(), "\n\n";
130 } else {
131 print $output $entry->raw_bibtex(), "\n\n";
132 }
133 }
134
135
136