#! /usr/bin/env perl ## 'perl -w' is good, but the PERL on the previous line is substituted, ## by default, by '/usr/bin/env perl', and that doesn't always cope well ## with multiple arguments, so leave the option off. # # This software is copyright, 1999, 2005, Norman Gray. # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # Author: Norman Gray, norman@astro.gla.ac.uk. # Department of Physics and Astronomy, University of Glasgow, UK # # See the file LICENCE for a copy of the GPL. # You can also find an online copy at http://www.gnu.org/copyleft/gpl.html . # # $Id$ # # See # for documentation. use strict; my $version = 'Bibhtml, version 2.0.2, Norman Gray , 2013 September 8'; # configuration... my $rootname = 'bibliography'; my $bibdata = ''; # default bibdata is ./bib.bib my $bibstyle = ''; # default, below, is plainhtml my $bibconfig = 'bibhtml.config'; # configuration file my $html3 = 0; my $citeall = 0; my $verbose = 1; my $mergebib = 0; my $strippis = 0; # if true, then strip PIs from output # configuration variables my $endash; my $nbsp; my $bibfilename; # the name of the .bib file my $bblfilename; # the name of the generated .bbl file my $auxfilename; # the .aux file we generate here # control flags my $phase; my $foundslot; # true when we're in the gap between # # set to 1 to switch on debugging, set > 1 to exit after option parsing my $debug = 0; # normalise progname - part after last slash $0 =~ /([^\/]*)$/; my $progname = $1; if ($#ARGV < $[) { print "Usage: $progname filename ...\n[$version]\n"; exit 1; } # get directory of first arg (this is where we'll search for bibhtml.config) $ARGV[0] =~ /[^\/:]*$/; my $defdir = $`; #print "defdir $defdir\n"; #------ # if -c is present in the argument list, then undefine $bibconfig, # and so don't read it automatically at the beginning for (@ARGV) { if (/^-c/) { undef ($bibconfig); last; }} if (defined ($bibconfig) && (-r $bibconfig)) { readconfig ($bibconfig); } # Option processing while ($_ = $ARGV[0], /^[-+]/) { # print "ARGV[0]=$_\n"; if (/^([-+])3/ ) { $html3= ($1 eq '+') } if (/^-v/) { $verbose = 1 } if (/^-q/) { $verbose = 0 } if (/^-a/) { $citeall = 1 } if (/^-V/) { print STDERR $version, "\n"; exit } if (/^--version/) { print STDERR $version, "\n"; exit } if (/^--merge/) { $mergebib = 1 } if (/^--strip/) { $strippis = 1 } if (/^--help/) { printhelp(); exit } shift; last if ($#ARGV < $[); if (/^-r/) { $rootname = $ARGV[0]; shift } if (/^-b/) { $bibdata = $ARGV[0]; shift } if (/^-s/) { $bibstyle = $ARGV[0]; shift } if (/^-c/) { my $bibconfig = $ARGV[0]; shift; readconfig ($bibconfig); } last if ($#ARGV < $[); } if ($debug) { # debugging arglist... print STDERR "rootname $rootname, bibdata $bibdata\n"; if ($html3) { print STDERR "html3\n"; } if ($verbose) { print STDERR "verbose\n"; } if ($citeall) { print STDERR "citeall\n"; } print STDERR "ARGV @ARGV\n"; exit if ($debug > 1); } if ($mergebib) { # usage is bibhtml file.bbl file.html - do nothing other than # merge a bbl file into an html file if ($#ARGV < 1) { print "Usage: $progname --merge file.bbl file.html\n"; exit; } $phase = +1; $bblfilename = $ARGV[0]; $bibfilename = $ARGV[1]; # Insist that both files have the correct extension # This route will mostly be called by scripts, so this check is # both useful and easily satisfied ($bblfilename =~ /\.bbl$/) && ($bibfilename =~ /\.html$/) || do { print STDERR "Usage: $progname --merge file.bbl file.html\n\tfile extensions required!\n"; exit; }; } else { if ($#ARGV < $[) { print "Usage: $progname filename ...\n"; exit; } # If the command name is bibhtml, then do everything. # If it isn't, then use the name of the (first) argument to work out which # phase to perform. If it's a .bbl file, then merge it with the .html # file, if not, then create the .aux file ready for BibTeX to be # invoked manually. if ($progname eq 'bibhtml') { $phase = 0; } else { if ($ARGV[0] =~ /(.*)\.bbl$/) { $rootname = $1; $phase = +1; } else { $phase = -1; } } } if ($debug) { print STDERR "rootname $rootname\nbibdata $bibdata\nphase $phase, html3 $html3\n"; #exit; } my @date = localtime(time); my @mname = ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); $bblfilename = "$rootname.bbl" unless (defined ($bblfilename)); $bibfilename = "$rootname.html" unless (defined ($bibfilename)); $auxfilename = "$rootname.aux" unless (defined ($auxfilename)); print "bibfilename=$bibfilename\nbibdata=$bibdata\n" if $verbose; ######################################## # # Start the scanning phase if ($phase <= 0) # -1 or 0 { print "Creating $auxfilename...\n" if ($verbose); open (OUT, ">$auxfilename") || die "can't open $auxfilename to write\n"; print OUT "\\relax\n"; # not really necessary, but... if ($citeall) { print "Citing all...\n" if ($verbose); print OUT "\\citation{*}\n"; } else { foreach my $fn (@ARGV) { my $t; # temporary string print "scanning $fn...\n" if ($verbose); open (IN, "$fn") || die "can't open $fn to read\n"; while () { # Match possibly multiple times on one line # Note bibfilename must be present, even if this is the # bibliography itself. foreach my $m (/$bibfilename#([^\"]+)/g) { print OUT "\\citation{$m}\n"; } if (($t) = /<\?bibhtml\s+bibdata\s+([\w,]*)/) { # accumulate bibdata if ($bibdata eq '') { $bibdata = $t; } else { $bibdata .= ',' . $t; } print "bibdata=$bibdata\n" if $verbose; } if (($t) = /<\?bibhtml\s+bibstyle\s+(\w*)/) { # do not override any value specified on the command line $bibstyle = $t if $bibstyle eq ''; print "bibstyle=$bibstyle\n" if $verbose; } } close (IN); } } $bibdata = 'bib' if ($bibdata eq ''); $bibstyle = 'plainhtml' if ($bibstyle eq ''); print OUT "\\bibstyle{$bibstyle}\n"; print OUT "\\bibdata{$bibdata}\n"; close (OUT); } ######################################## # # Call BibTeX if ($phase == 0) { print "Calling BibTeX...\n" if ($verbose); system ("bibtex $rootname")==0 || die "can't bibtex $auxfilename\n"; } ######################################## # # Substitution phase if ($phase >= 0 && -r "$bblfilename") # $phase +1 or 0 { print "Merging $bblfilename into $bibfilename...\n" if ($verbose); # now merge the new bbl file with the old bibfilename, between the # pattern /$bibfilename") ||die "can't open $bibfilename to write"; select (OUT); open (ORIG, "$bibfilename.old")||die "can't open $bibfilename.old to read"; } open (BBL, "$bblfilename") ||die "can't open $bblfilename to read"; $foundslot = 0; # copy the old to the new up to pattern FINDSLOT: while () { if (/<\?bibhtml\s+today/ || /