diff options
author | Karl Berry <karl@freefriends.org> | 2017-04-16 22:25:15 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2017-04-16 22:25:15 +0000 |
commit | 75c789c980df299fd7e82e2a4f1e40e496bf3e3e (patch) | |
tree | a1d570e5bccdf4d18955f2b59f2e4df859cac685 /Master/texmf-dist/scripts/crossrefware | |
parent | ca335820d525cec5e0c68237c97333b952e61778 (diff) |
crossrefware (14apr17)
git-svn-id: svn://tug.org/texlive/trunk@43866 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/scripts/crossrefware')
-rwxr-xr-x | Master/texmf-dist/scripts/crossrefware/bbl2bib.pl | 259 | ||||
-rwxr-xr-x | Master/texmf-dist/scripts/crossrefware/bibdoiadd.pl | 33 | ||||
-rwxr-xr-x | Master/texmf-dist/scripts/crossrefware/bibmradd.pl | 210 | ||||
-rwxr-xr-x | Master/texmf-dist/scripts/crossrefware/bibzbladd.pl | 58 |
4 files changed, 538 insertions, 22 deletions
diff --git a/Master/texmf-dist/scripts/crossrefware/bbl2bib.pl b/Master/texmf-dist/scripts/crossrefware/bbl2bib.pl new file mode 100755 index 00000000000..7a05297a0fd --- /dev/null +++ b/Master/texmf-dist/scripts/crossrefware/bbl2bib.pl @@ -0,0 +1,259 @@ +#!/usr/bin/env perl + +=pod + +=head1 NAME + +bbl2bib.pl - convert thebibliography environment to a bib file + +=head1 SYNOPSIS + +bbl2bib.pl [B<-o> I<output>] I<file> + +=head1 OPTIONS + +=over 4 + + + +=item B<-o> I<output> + +Output file. If this option is not used, the name for the +output file is formed by changing the extension to C<.bib> + + +=back + +=head1 DESCRIPTION + +The script tries to reconstruct a C<bib> file from the corresponding +C<thebibliography> environment. One can argue that this operation is +akin to reconstructing a cow from the steak. The way the script does +it is searching for the entry in the MR database, and +creating the corresponding BibTeX fields. + +The script reads a TeX or Bbl file and extracts from it the +C<thebibliography> environment. For each bibitem it creates a plain +text bibliography entry, and then tries to match it in +the database. +=head1 INPUT FILE + +We assume some structure of the input file: + +=over 4 + +=item 1. + +The bibliography is contained between the lines + + \begin{thebibliography}... + +and + + \end{thebibliography} + +=item 2. + +Each bibliography item starts from the line + + \bibitem[...]{....} + +=back + + + +=head1 EXAMPLES + + bbl2bib -o - file.tex > result.bib + bbl2bib -o result.bib file.bbl + bbl2bib file.tex + +=head1 AUTHOR + +Boris Veytsman + +=head1 COPYRIGHT AND LICENSE + +Copyright (C) 2014-2017 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=SELFAUTOPARENT`); # TL root + if (length($TLMaster)) { + unshift @INC, "$TLMaster/texmf-dist/scripts/bibtexperllibs"; + } +} +use IO::File; +use BibTeX::Parser; +use FileHandle; +use LaTeX::ToUnicode qw (convert); +use Getopt::Std; +use URI::Escape; +use LWP::Simple; + +my $USAGE="USAGE: $0 [-o output] file\n"; +my $VERSION = <<END; +bbl2bib v2.1 +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('c:o:s: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/\.([^\.]*)$/.bib/; + +if (exists $opts{o}) { + $outputfile = $opts{o}; +} + + + +my $input= IO::File->new($inputfile) or + die "Cannot find Bbl or TeX file $inputfile\n$USAGE\n"; +my $output = IO::File->new("> $outputfile") or + die "Cannot write to $outputfile\n$USAGE\n"; + +my $userAgent = LWP::UserAgent->new; + + +# Bibitem is a hash with the entries 'key', 'text', 'mr', +# 'zbl' +my $bibitem; + +while (<$input>) { + if (!(/\\begin\{thebibliography\}/../\\end\{thebibliography\}/) || + /\\begin\{thebibliography\}/ || /\\end\{thebibliography\}/) { + next; + } + if (/\\bibitem(\[[^\]]*\])?\{([^\}]*)\}/) { + ProcessBibitem($bibitem); + $bibitem = undef; + $bibitem->{key}=$2; + $bibitem->{text}=""; + } + if (!/^\s*$/) { + $bibitem -> {text} .= $_; + } +} +ProcessBibitem($bibitem); + + +exit 0; + +sub ProcessBibitem { + my $bibitem = shift; + my $key = $bibitem->{key}; + my $text=$bibitem->{text}; + if (!length($text) || $text =~ /^\s+$/s) { + return; + } + + my $printtext = $text; + $printtext =~ s/^(.)/% $1/mg; + print $output "$printtext"; + $text =~ s/\n/ /mg; + $text =~ s/\\bibitem(\[[^\]]*\])?\{[^\}]*\}//; + + # Arxiv entry? + if ($text =~ s/\\arxiv\{([^\}]+)\}\.?//) { + $bibitem->{arxiv}=$1; + } + + # Mr number exists? + if ($text =~ s/\\mr\{([^\}]+)\}\.?//) { + $bibitem->{mr}=$1; + } + + # zbl number exists? + if ($text =~ s/\\zbl\{([^\}]+)\}\.?//) { + $bibitem->{zbl}=$1; + } + + # doi number exists? + if ($text =~ s/\\doi\{([^\}]+)\}\.?//) { + $bibitem->{doi}=$1; + } + + $bibitem->{bib} = SearchMref($bibitem); + PrintBibitem($bibitem); + return; +} + + +sub SearchMref { + my $bibitem = shift; + my $mirror = "http://www.ams.org/mathscinet-mref"; + my $string=uri_escape_utf8($bibitem->{text}); + my $response = $userAgent->get("$mirror?ref=$string&dataType=bibtex") -> + decoded_content(); + if ($response =~ /<pre>(.*)<\/pre>/s) { + my $bib= $1; + my $fh = new FileHandle; + open $fh, "<", \$bib; + my $parser = new BibTeX::Parser($fh); + my $entry = $parser->next; + if ($entry->parse_ok()) { + $entry->key($bibitem->{key}); + return ($entry); + } + } +} + + + + +sub PrintBibitem { + print "\n"; + my $bibitem = shift; + if (!ref($bibitem->{bib})) { + return; + } + my $entry=$bibitem->{bib}; + if ($bibitem->{mr} && ! $entry->field('mrnumber')) { + $entry->field('mrnumber', $bibitem->{mr}); + } + if ($entry->field('mrnumber')) { + my $mr=$entry->field('mrnumber'); + while (length($mr)<7) { + $mr = "0$mr"; + } + $mr=$entry->field('mrnumber', $mr); + } + if ($bibitem->{zbl} && ! $entry->field('zblnumber')) { + $entry->field('zblnumber', $bibitem->{zbl}); + } + if ($bibitem->{doi} && ! $entry->field('doi')) { + $entry->field('doi', $bibitem->{doi}); + } + if ($bibitem->{arxiv} && ! $entry->field('arxiv')) { + $entry->field('arxiv', $bibitem->{arxiv}); + } + + print $entry->to_string(), "\n\n"; +} + + diff --git a/Master/texmf-dist/scripts/crossrefware/bibdoiadd.pl b/Master/texmf-dist/scripts/crossrefware/bibdoiadd.pl index 8937729a3ba..bf7bc1a2856 100755 --- a/Master/texmf-dist/scripts/crossrefware/bibdoiadd.pl +++ b/Master/texmf-dist/scripts/crossrefware/bibdoiadd.pl @@ -8,7 +8,7 @@ bibdoiadd.pl - add DOI numbers to papers in a given bib file =head1 SYNOPSIS -bibdoiadd [B<-c> I<config_file>] [B<-f>] [B<-o> I<output>] I<bib_file> +bibdoiadd [B<-c> I<config_file>] [B<-e> 1|0] [B<-f>] [B<-o> I<output>] I<bib_file> =head1 OPTIONS @@ -19,6 +19,12 @@ bibdoiadd [B<-c> I<config_file>] [B<-f>] [B<-o> I<output>] I<bib_file> Configuration file. If this file is absent, some defaults are used. See below for its format. +=item B<-e> + +If 1 (default), add empty doi if a doi cannot be found. This prevents +repeated searches for the same entries if you add new entries to the +file. Calling C<-e 0> suppresses this behavior. + =item B<-f> Force checking doi number even if one is present @@ -59,15 +65,15 @@ The configuration file is mostly self-explanatory: it has comments $field = value ; -The important parameters are C<$mode> (C<'free'> or C<'paid'>, +The important parameters are C<$mode> (C<'free'> or C<'paid'>), C<$email> (for free users) and C<$username> & C<$password> for paid members. =head1 EXAMPLES - bibdoiadd -c bibdoiadd.cfg citations.bib > result.bib - bibdoiadd -c bibdoiadd.cfg citations.bib -o result.bib + bibdoiadd -c bibdoiadd.cfg -o - citations.bib > result.bib + bibdoiadd -c bibdoiadd.cfg -o result.bib citations.bib =head1 AUTHOR @@ -75,7 +81,7 @@ Boris Veytsman =head1 COPYRIGHT AND LICENSE -Copyright (C) 2014-2016 Boris Veytsman +Copyright (C) 2014-2017 Boris Veytsman This is free software. You may redistribute copies of it under the terms of the GNU General Public License @@ -99,17 +105,17 @@ use Getopt::Std; use URI::Escape; use LWP::Simple; -my $USAGE="USAGE: $0 [-c config] [-f] [-o output] file\n"; +my $USAGE="USAGE: $0 [-c config] [-e 1|0] [-f] [-o output] file\n"; my $VERSION = <<END; -bibdoiadd v2.0 +bibdoiadd v2.1 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 -my %opts; -getopts('fc:o:hV',\%opts) or die $USAGE; +our %opts; +getopts('fe:c:o:hV',\%opts) or die $USAGE; if ($opts{h} || $opts{V}){ print $VERSION; @@ -126,12 +132,15 @@ my $outputfile = $inputfile; $outputfile =~ s/\.([^\.]*)$/_doi.$1/; -if ($opts{o}) { +if (exists $opts{o}) { $outputfile = $opts{o}; } my $forceSearch=$opts{f}; - +my $forceEmpty = 1; +if (exists $opts{e}) { + $forceEmpty = $opts{e}; +} our $mode='free'; our $email; @@ -196,7 +205,7 @@ while (my $entry = $parser->next) { my $doi = GetDoi($prefix, $entry); - if (length($doi)) { + if (length($doi) || $forceEmpty) { $entry->field('doi',$doi); } print $output $entry->to_string(), "\n\n"; diff --git a/Master/texmf-dist/scripts/crossrefware/bibmradd.pl b/Master/texmf-dist/scripts/crossrefware/bibmradd.pl new file mode 100755 index 00000000000..8b1c9938a56 --- /dev/null +++ b/Master/texmf-dist/scripts/crossrefware/bibmradd.pl @@ -0,0 +1,210 @@ +#!/usr/bin/env perl + +=pod + +=head1 NAME + +bibmradd.pl - add MR numbers to papers in a given bib file + +=head1 SYNOPSIS + +bibmradd [-d] [B<-f>] [B<-e> 1|0] [B<-o> I<output>] I<bib_file> + +=head1 OPTIONS + +=over 4 + +=item B<-d> + +Debug mode + +=item B<-e> + +If 1 (default), add an empty mrnumber if a mr cannot be found. This +prevents repeated searches for the same entries if you add new entries +to the file. Calling C<-e 0> suppresses this behavior. + + +=item B<-f> + +Force searching for MR numbers even if the entry already has one. + +=item B<-o> I<output> + +Output file. If this option is not used, the name for the +output file is formed by adding C<_mr> to the input file + +=back + +=head1 DESCRIPTION + +The script reads a BibTeX file. It checks whether the entries have +mrnumberss. If not, tries to contact internet to get the numbers. The +result is a BibTeX file with the fields +C<mrnumber=...> added. + +The name of the output file is either set by the B<-o> option or +is derived by adding the suffix C<_mr> to the output file. + +=head1 AUTHOR + +Boris Veytsman + +=head1 COPYRIGHT AND LICENSE + +Copyright (C) 2014-2017 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=SELFAUTOPARENT`); # TL root + if (length($TLMaster)) { + unshift @INC, "$TLMaster/texmf-dist/scripts/bibtexperllibs"; + } +} +use IO::File; +use BibTeX::Parser; +use Getopt::Std; +use URI::Escape; +use LWP::UserAgent; +$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0; + +my $USAGE="USAGE: $0 [-d] [-e 1|0] [-f] [-o output] file\n"; +my $VERSION = <<END; +bibmradd v2.1 +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 +my %opts; +getopts('de:fo: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/\.([^\.]*)$/_mr.$1/; + +if ($opts{o}) { + $outputfile = $opts{o}; +} + +my $forceSearch=$opts{f}; + +my $forceEmpty = 1; +if (exists $opts{e}) { + $forceEmpty = $opts{e}; +} + +my $debug = $opts{d}; + +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); + + + +# Creating the HTTP parameters +my $mirror = + "http://www.ams.org/mathscinet-mref"; +my $userAgent = LWP::UserAgent->new; + +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('mrnumber') && !$forceSearch) { + print $output $entry->raw_bibtex(), "\n\n"; + if ($debug) { + print STDERR "DEBUG: entry ", $entry->key(), + " has mrnumber ", $entry->field('mrnumber'), + " and no forced search is requested\n"; + } + next; + } + + + # Now we have an entry with no MR. Let us get to work. + if ($debug) { + print STDERR "DEBUG: Searching for mr number for entry ", + $entry->key, "\n"; + } + my $mr = GetMr($entry, $userAgent, $mirror); + if (length($mr) || $forceEmpty) { + $entry->field('mrnumber', $mr); + } + print $output $entry->to_string(), "\n\n"; + +} + +$input->close(); +$output->close(); +exit 0; + +############################################################### +# Getting one MR +############################################################### + +sub GetMr { + my $entry=shift; + my $userAgent=shift; + my $mirror=shift; + + my @query; + + my $string=uri_escape_utf8($entry->to_string()); + + if ($debug) { + print STDERR "DEBUG: query: $mirror?ref=$string&dataType=bibtex\n" ; + } + + + my $response = $userAgent->get("$mirror?ref=$string&dataType=bibtex"); + if ($debug) { + print STDERR "DEBUG: response: ", + $response->decoded_content, "\n"; + } + + if ($response->decoded_content =~ /MRNUMBER\s*=\s*{(.*)}/m) { + my $mr=$1; + # Somehow mref deletes leading zeros. They are needed! + while (length($mr)<7) { + $mr = "0$mr"; + } + if ($debug) { + print STDERR "DEBUG: got MR: $mr\n", + } + return $mr; + } else { + if ($debug) { + print STDERR "DEBUG: Did not get MR\n", + } + return (""); + } + +} + diff --git a/Master/texmf-dist/scripts/crossrefware/bibzbladd.pl b/Master/texmf-dist/scripts/crossrefware/bibzbladd.pl index f348cd4b946..b946766fdf9 100755 --- a/Master/texmf-dist/scripts/crossrefware/bibzbladd.pl +++ b/Master/texmf-dist/scripts/crossrefware/bibzbladd.pl @@ -8,12 +8,23 @@ bibzbladd.pl - add Zbl numbers to papers in a given bib file =head1 SYNOPSIS -bibzbladd [B<-f>] [B<-o> I<output>] I<bib_file> +bibzbladd [-d] [B<-f>] [B<-e> 1|0] [B<-o> I<output>] I<bib_file> =head1 OPTIONS =over 4 +=item B<-d> + +Debug mode + +=item B<-e> + +If 1 (default), add an empty zblnumber if a zbl cannot be found. This +prevents repeated searches for the same entries if you add new entries +to the file. Calling C<-e 0> suppresses this behavior. + + =item B<-f> Force searching for Zbl numbers even if the entry already has one. @@ -41,7 +52,7 @@ Boris Veytsman =head1 COPYRIGHT AND LICENSE -Copyright (C) 2014-2016 Boris Veytsman +Copyright (C) 2014-2017 Boris Veytsman This is free software. You may redistribute copies of it under the terms of the GNU General Public License @@ -63,10 +74,11 @@ use BibTeX::Parser; use Getopt::Std; use URI::Escape; use LWP::UserAgent; +$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0; -my $USAGE="USAGE: $0 [-f] [-o output] file\n"; +my $USAGE="USAGE: $0 [-d] [-e 1|0] [-f] [-o output] file\n"; my $VERSION = <<END; -bibzbladd v2.0 +bibzbladd v2.1 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 @@ -74,7 +86,7 @@ extent permitted by law. $USAGE END my %opts; -getopts('fo:hV',\%opts) or die $USAGE; +getopts('de:fo:hV',\%opts) or die $USAGE; if ($opts{h} || $opts{V}){ print $VERSION; @@ -97,6 +109,13 @@ if ($opts{o}) { my $forceSearch=$opts{f}; +my $forceEmpty = 1; +if (exists $opts{e}) { + $forceEmpty = $opts{e}; +} + +my $debug = $opts{d}; + my $input= IO::File->new($inputfile) or die "Cannot find BibTeX file $inputfile\n$USAGE\n"; my $output = IO::File->new("> $outputfile") or @@ -118,19 +137,24 @@ while (my $entry = $parser->next ) { print STDERR "Skipping this entry\n"; next; } - if (!($entry->type() eq 'ARTICLE')) { - print $output $entry->raw_bibtex(), "\n\n"; - next; - } if ($entry->has('zblnumber') && !$forceSearch) { print $output $entry->raw_bibtex(), "\n\n"; + if ($debug) { + print STDERR "DEBUG: entry ", $entry->key(), + " has zblnumber ", $entry->field('zblnumber'), + " and no forced search is requested\n"; + } next; } # Now we have an entry with no Zbl. Let us get to work. + if ($debug) { + print STDERR "DEBUG: Searching for zbl number for entry ", + $entry->key, "\n"; + } my $zbl = GetZbl($entry, $userAgent, $mirror); - if (length($zbl)) { + if (length($zbl) || $forceEmpty) { $entry->field('zblnumber',$zbl); } print $output $entry->to_string(), "\n\n"; @@ -154,12 +178,26 @@ sub GetZbl { my $string=uri_escape_utf8($entry->to_string()); + if ($debug) { + print STDERR "DEBUG: query: $mirror?bibtex=$string\n" ; + } my $response = $userAgent->get("$mirror?bibtex=$string"); + if ($debug) { + print STDERR "DEBUG: response: ", + $response->decoded_content, "\n"; + } + if ($response->decoded_content =~ /^\s*"zbl_id":\s*"(.*)",\s*$/m) { + if ($debug) { + print STDERR "DEBUG: got zbl: $1\n", + } return $1; } else { + if ($debug) { + print STDERR "DEBUG: Did not get zbl\n", + } return (""); } |