Debian Codegraph @

texk/texlive/linked_scripts/crossrefware/bbl2bib.pl

raw
1 #!/usr/bin/env perl
2
3 =pod
4
5 =head1 NAME
6
7 bbl2bib.pl - convert thebibliography environment to a bib file
8
9 =head1 SYNOPSIS
10
11 bbl2bib.pl [-d] [-u] [B<-o> I<output>] I<file>
12
13 =head1 OPTIONS
14
15 =over 4
16
17 =item [-d]
18
19 Send debugging output to stdout
20
21 =item B<-o> I<output>
22
23 Output file. If this option is not used, the name for the
24 output file is formed by changing the extension to C<.bib>
25
26
27 =item B<-u>
28
29 Do not clean URL fields.
30
31 Normally C<bbl2bib> recognizes URL fields of the kind
32 C<http://dx.doi.org> and their variants and converts them to DOI
33 fields (see also L<biburl2doi(1)> script). The switch B<-u>
34 suppresses this cleanup.
35
36 =back
37
38 =head1 DESCRIPTION
39
40 The script tries to reconstruct a C<bib> file from the corresponding
41 C<thebibliography> environment. One can argue that this operation is
42 akin to reconstructing a cow from the steak. The way the script does
43 it is searching for the entry in the MR database, and
44 creating the corresponding BibTeX fields.
45
46 The script reads a TeX or Bbl file and extracts from it the
47 C<thebibliography> environment. For each bibitem it creates a plain
48 text bibliography entry, and then tries to match it in
49 the database.
50
51 =head1 INPUT FILE
52
53 We assume some structure of the input file:
54
55 =over 4
56
57 =item 1.
58
59 The bibliography is contained between the lines
60
61 \begin{thebibliography}...
62
63 and
64
65 \end{thebibliography}
66
67 =item 2.
68
69 Each bibliography item starts from the line
70
71 \bibitem[...]{....}
72
73 =back
74
75
76
77 =head1 EXAMPLES
78
79 bbl2bib -o - file.tex > result.bib
80 bbl2bib -o result.bib file.bbl
81 bbl2bib file.tex
82
83 =head1 AUTHOR
84
85 Boris Veytsman
86
87 =head1 COPYRIGHT AND LICENSE
88
89 Copyright (C) 2014-2021 Boris Veytsman
90
91 This is free software. You may redistribute copies of it under the
92 terms of the GNU General Public License
93 L<http://www.gnu.org/licenses/gpl.html>. There is NO WARRANTY, to the
94 extent permitted by law.
95
96 =cut
97
98 use strict;
99 BEGIN {
100 # find files relative to our installed location within TeX Live
101 chomp(my $TLMaster = `kpsewhich -var-value=TEXMFROOT`); # TL root
102 if (length($TLMaster)) {
103 unshift @INC, "$TLMaster/texmf-dist/scripts/bibtexperllibs";
104 }
105 }
106 use IO::File;
107 use BibTeX::Parser;
108 use FileHandle;
109 use LaTeX::ToUnicode qw (convert);
110 use Getopt::Std;
111 use URI::Escape;
112 use LWP::Simple;
113 # Sometimes AMS forgets to update certificates
114 $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;
115
116
117 my $USAGE="USAGE: $0 [-d] [-u] [-o output] file\n";
118 my $VERSION = <<END;
119 bbl2bib v2.3
120 This is free software. You may redistribute copies of it under the
121 terms of the GNU General Public License
122 http://www.gnu.org/licenses/gpl.html. There is NO WARRANTY, to the
123 extent permitted by law.
124 $USAGE
125 END
126 our %opts;
127 getopts('do:huV',\%opts) or die $USAGE;
128
129 if ($opts{h} || $opts{V}){
130 print $VERSION;
131 exit 0;
132 }
133
134 ################################################################
135 # Defaults and parameters
136 ################################################################
137
138 my $inputfile = shift;
139
140 my $outputfile = $inputfile;
141
142 $outputfile =~ s/\.([^\.]*)$/.bib/;
143
144 if (exists $opts{o}) {
145 $outputfile = $opts{o};
146 }
147
148 my $debug=0;
149 if ($opts{d}) {
150 $debug=1;
151 }
152
153 my $cleanUrls = 1;
154 if ($opts{u}) {
155 $cleanUrls = 0;
156 }
157
158 my $input= IO::File->new($inputfile) or
159 die "Cannot find Bbl or TeX file $inputfile\n$USAGE\n";
160 my $output = IO::File->new("> $outputfile") or
161 die "Cannot write to $outputfile\n$USAGE\n";
162
163 my $userAgent = LWP::UserAgent->new;
164
165
166 # Bibitem is a hash with the entries 'key', 'text', 'mr',
167 # 'zbl'
168 my $bibitem;
169
170 while (<$input>) {
171 if (!(/\\begin\{thebibliography\}/../\\end\{thebibliography\}/) ||
172 /\\begin\{thebibliography\}/ || /\\end\{thebibliography\}/) {
173 next;
174 }
175 if (/\\bibitem\s*(\[[^\]]*\])?\{([^\}]*)\}/) {
176 ProcessBibitem($bibitem);
177 $bibitem = undef;
178 $bibitem->{key}=$2;
179 $bibitem->{text}="";
180 }
181 if (!/^\s*$/) {
182 $bibitem -> {text} .= $_;
183 }
184 }
185 ProcessBibitem($bibitem);
186
187
188 exit 0;
189
190 sub ProcessBibitem {
191 my $bibitem = shift;
192 my $key = $bibitem->{key};
193 my $text=$bibitem->{text};
194
195 if ($debug) {
196 print STDOUT "DEBUG: Processing item $key\n";
197 }
198
199 if (!length($text) || $text =~ /^\s+$/s) {
200 if ($debug) {
201 print STDOUT "DEBUG: No text found\n";
202 }
203 return;
204 }
205
206 my $printtext = $text;
207 $printtext =~ s/^(.)/% $1/mg;
208 print $output "$printtext";
209 $text =~ s/\n/ /mg;
210 $text =~ s/\\bibitem(\[[^\]]*\])?\{[^\}]*\}//;
211
212 # Arxiv entry?
213 if ($text =~ s/\\arxiv\{([^\}]+)\}\.?//) {
214 if ($debug) {
215 print STDOUT "DEBUG: Found arXiv number $1\n";
216 }
217 $bibitem->{arxiv}=$1;
218 }
219
220 # Mr number exists?
221 if ($text =~ s/\\mr\{([^\}]+)\}\.?//) {
222 if ($debug) {
223 print STDOUT "DEBUG: Found mr number $1\n";
224 }
225 $bibitem->{mr}=$1;
226 }
227
228 # zbl number exists?
229 if ($text =~ s/\\zbl\{([^\}]+)\}\.?//) {
230 if ($debug) {
231 print STDOUT "DEBUG: Found zbl number $1\n";
232 }
233 $bibitem->{zbl}=$1;
234 }
235
236 # doi number exists?
237 if ($text =~ s/\\doi\{([^\}]+)\}\.?//) {
238 if ($debug) {
239 print STDOUT "DEBUG: Found doi $1\n";
240 }
241 $bibitem->{doi}=$1;
242 }
243
244 $bibitem->{bib} = SearchMref($bibitem);
245
246 if ($cleanUrls) {
247 $bibitem->{bib} = CleanUrl ($bibitem->{bib});
248 }
249
250 PrintBibitem($bibitem);
251 return;
252 }
253
254
255 sub SearchMref {
256 my $bibitem = shift;
257 my $mirror = "https://mathscinet.ams.org/mathscinet-mref";
258 my $string=uri_escape_utf8($bibitem->{text});
259 if ($debug) {
260 print STDOUT "Sending $mirror?ref=$string".'&'."dataType=bibtex\n"
261 }
262 my $response = $userAgent->get("$mirror?ref=$string&dataType=bibtex") ->
263 decoded_content();
264 if ($debug) {
265 print STDOUT "DEBUG: Response $response\n";
266 }
267 if ($response =~ /<pre>(.*)<\/pre>/s) {
268 my $bib= $1;
269 # Looks like Mathscinet sometimes fails to unaccent text.
270 # For the time being we just delete the offending characters.
271 # Should probably write LaTeX::FromUnicode instead
272 $bib =~ s/[^\x00-\x7f]//g;
273 if ($debug) {
274 print STDOUT "DEBUG: got $bib\n";
275 }
276 my $fh = new FileHandle;
277 open $fh, "<", \$bib;
278 my $parser = new BibTeX::Parser($fh);
279 my $entry = $parser->next;
280 if (ref($entry) && $entry->parse_ok()) {
281 $entry->key($bibitem->{key});
282 return ($entry);
283 } else {
284 if ($debug) {
285 if (!ref($entry)) {
286 print STDERR "DEBUG: not a reference\n";
287 } else{
288 print STDERR "DEBUG: parsing $entry->parse_ok\n";
289 }
290 }
291 }
292 } else {
293 if ($debug) {
294 print STDOUT "DEBUG: did not get an entry\n";
295 }
296 }
297 }
298
299 sub CleanUrl {
300 my $entry = shift;
301 if (!ref($entry)) {
302 return $entry;
303 }
304
305 if ($entry->has('doi')) {
306 return $entry;
307 }
308 if (!$entry->has('url')) {
309 return $entry;
310 }
311 if ($entry->field('url') =~ m|^http(?:s)?://(?:dx\.)?doi\.org/(.*)$|) {
312 $entry->field('doi', $1);
313 delete $entry->{'url'};
314 }
315 return $entry;
316
317 }
318
319
320 sub PrintBibitem {
321 print $output "\n";
322 my $bibitem = shift;
323 if (!ref($bibitem->{bib})) {
324 return;
325 }
326 my $entry=$bibitem->{bib};
327 if ($bibitem->{mr} && ! $entry->field('mrnumber')) {
328 $entry->field('mrnumber', $bibitem->{mr});
329 }
330 if ($entry->field('mrnumber')) {
331 my $mr=$entry->field('mrnumber');
332 while (length($mr)<7) {
333 $mr = "0$mr";
334 }
335 $mr=$entry->field('mrnumber', $mr);
336 }
337 if ($bibitem->{zbl} && ! $entry->field('zblnumber')) {
338 $entry->field('zblnumber', $bibitem->{zbl});
339 }
340 if ($bibitem->{doi} && ! $entry->field('doi')) {
341 $entry->field('doi', $bibitem->{doi});
342 }
343 if ($bibitem->{arxiv} && ! $entry->field('arxiv')) {
344 $entry->field('arxiv', $bibitem->{arxiv});
345 }
346
347 print $output $entry->to_string(), "\n\n";
348 }
349
350