From 75c789c980df299fd7e82e2a4f1e40e496bf3e3e Mon Sep 17 00:00:00 2001 From: Karl Berry Date: Sun, 16 Apr 2017 22:25:15 +0000 Subject: crossrefware (14apr17) git-svn-id: svn://tug.org/texlive/trunk@43866 c570f23f-e606-0410-a88d-b1316a301751 --- .../source/texk/texlive/linked_scripts/Makefile.am | 2 + .../source/texk/texlive/linked_scripts/Makefile.in | 2 + .../texlive/linked_scripts/crossrefware/bbl2bib.pl | 259 +++++++++++++++++++++ .../linked_scripts/crossrefware/bibdoiadd.pl | 33 ++- .../linked_scripts/crossrefware/bibmradd.pl | 210 +++++++++++++++++ .../linked_scripts/crossrefware/bibzbladd.pl | 58 ++++- .../source/texk/texlive/linked_scripts/scripts.lst | 2 + 7 files changed, 544 insertions(+), 22 deletions(-) create mode 100755 Build/source/texk/texlive/linked_scripts/crossrefware/bbl2bib.pl create mode 100755 Build/source/texk/texlive/linked_scripts/crossrefware/bibmradd.pl (limited to 'Build') diff --git a/Build/source/texk/texlive/linked_scripts/Makefile.am b/Build/source/texk/texlive/linked_scripts/Makefile.am index 09c2f3b6896..ca2e26bb171 100644 --- a/Build/source/texk/texlive/linked_scripts/Makefile.am +++ b/Build/source/texk/texlive/linked_scripts/Makefile.am @@ -97,7 +97,9 @@ texmf_other_scripts = \ cjk-gs-integrate/cjk-gs-integrate.pl \ context/perl/mptopdf.pl \ convbkmk/convbkmk.rb \ + crossrefware/bbl2bib.pl \ crossrefware/bibdoiadd.pl \ + crossrefware/bibmradd.pl \ crossrefware/bibzbladd.pl \ crossrefware/ltx2crossrefxml.pl \ ctanify/ctanify \ diff --git a/Build/source/texk/texlive/linked_scripts/Makefile.in b/Build/source/texk/texlive/linked_scripts/Makefile.in index 50e0d82452f..51fd0db035a 100644 --- a/Build/source/texk/texlive/linked_scripts/Makefile.in +++ b/Build/source/texk/texlive/linked_scripts/Makefile.in @@ -312,7 +312,9 @@ texmf_other_scripts = \ cjk-gs-integrate/cjk-gs-integrate.pl \ context/perl/mptopdf.pl \ convbkmk/convbkmk.rb \ + crossrefware/bbl2bib.pl \ crossrefware/bibdoiadd.pl \ + crossrefware/bibmradd.pl \ crossrefware/bibzbladd.pl \ crossrefware/ltx2crossrefxml.pl \ ctanify/ctanify \ diff --git a/Build/source/texk/texlive/linked_scripts/crossrefware/bbl2bib.pl b/Build/source/texk/texlive/linked_scripts/crossrefware/bbl2bib.pl new file mode 100755 index 00000000000..7a05297a0fd --- /dev/null +++ b/Build/source/texk/texlive/linked_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] I + +=head1 OPTIONS + +=over 4 + + + +=item B<-o> I + +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 file from the corresponding +C 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 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. 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 = <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>/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/Build/source/texk/texlive/linked_scripts/crossrefware/bibdoiadd.pl b/Build/source/texk/texlive/linked_scripts/crossrefware/bibdoiadd.pl
index 8937729a3ba..bf7bc1a2856 100755
--- a/Build/source/texk/texlive/linked_scripts/crossrefware/bibdoiadd.pl
+++ b/Build/source/texk/texlive/linked_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] [B<-f>] [B<-o> I] I
+bibdoiadd [B<-c> I] [B<-e> 1|0] [B<-f>] [B<-o> I] I
 
 =head1 OPTIONS
 
@@ -19,6 +19,12 @@ bibdoiadd [B<-c> I] [B<-f>] [B<-o> I] I
 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 = <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/Build/source/texk/texlive/linked_scripts/crossrefware/bibmradd.pl b/Build/source/texk/texlive/linked_scripts/crossrefware/bibmradd.pl
new file mode 100755
index 00000000000..8b1c9938a56
--- /dev/null
+++ b/Build/source/texk/texlive/linked_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] I
+
+=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 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 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.  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 = <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/Build/source/texk/texlive/linked_scripts/crossrefware/bibzbladd.pl b/Build/source/texk/texlive/linked_scripts/crossrefware/bibzbladd.pl
index f348cd4b946..b946766fdf9 100755
--- a/Build/source/texk/texlive/linked_scripts/crossrefware/bibzbladd.pl
+++ b/Build/source/texk/texlive/linked_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] I
+bibzbladd  [-d] [B<-f>] [B<-e> 1|0] [B<-o> I] I
 
 =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 = <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 ("");
     }
 
diff --git a/Build/source/texk/texlive/linked_scripts/scripts.lst b/Build/source/texk/texlive/linked_scripts/scripts.lst
index dd52f5545e1..ddf9ceaaefa 100644
--- a/Build/source/texk/texlive/linked_scripts/scripts.lst
+++ b/Build/source/texk/texlive/linked_scripts/scripts.lst
@@ -42,7 +42,9 @@ checkcites/checkcites.lua
 cjk-gs-integrate/cjk-gs-integrate.pl
 context/perl/mptopdf.pl
 convbkmk/convbkmk.rb
+crossrefware/bbl2bib.pl
 crossrefware/bibdoiadd.pl
+crossrefware/bibmradd.pl
 crossrefware/bibzbladd.pl
 crossrefware/ltx2crossrefxml.pl
 ctanify/ctanify
-- 
cgit v1.2.3