#!/bin/sh exec perl -S -x -- "$0" "$@" #!perl -w # $Id: update-html-bib,v 1.17 2017/09/25 15:16:25 jbw Exp $ use English; use strict; use User::pwent; my $JBW_Home; BEGIN { my $pw = getpwnam('jbw') || getpwnam('user') || die "Can't find user jbw or user: $OS_ERROR"; $JBW_Home = $pw->dir; push (@INC, "$JBW_Home/share/share/perl"); push (@INC, "$JBW_Home/church/lib/perl"); } use JBW::Utils qw(&with_file_contents &max &min); use Getopt::Long; use User::pwent; # This program uses bib2html to update the HTML bibliography in the # target file. my $Short_Prog_Name = $PROGRAM_NAME; $Short_Prog_Name =~ s(.*/)(); sub debug_print { #print STDERR "$Short_Prog_Name: @_\n"; } # Where bib2html is installed on local system. my ($Bib2HTMLDir) = "$JBW_Home/bib2html-1.33"; my ($Bib2HTMLProg) = "$Bib2HTMLDir/bib2html"; my ($HTMLBibFile, $AuxFile, $CiteFile, $BibDelim, $Bib2HTMLOpts, $ReverseNumericOrder); Getopt::Long::GetOptions ( 'html-bib=s' => \$HTMLBibFile, 'latex-aux=s' => \$AuxFile, 'html-cite-file=s' => \$CiteFile, 'bib-delim=s' => \$BibDelim, 'bib2html-opts=s' => \$Bib2HTMLOpts, 'reverse-numeric-order' => \$ReverseNumericOrder, ); if (! defined $HTMLBibFile) { die "must specify HTML bib file"; } if (! -f $HTMLBibFile) { die "Bad HTML bib file: $HTMLBibFile"; } if (! defined $AuxFile) { die "must specify LaTeX .aux file"; } if (! -f $AuxFile) { die "Bad LaTeX .aux file: $AuxFile"; } # RegenerateHTMLBib (HTML_BIB_FILE, AUX_FILE, BIB_NAME) # # (Re)generates an HTML formatted bibliography in a (possibly already # existing) HTML file. The bibliography contains entries specified in # the file given by the AUX_FILE argument. # # Arguments: # # HTML_BIB_FILE: target HTML file, may already exist # AUX_FILE: source file, .aux output of LaTeX # BIB_NAME: name of a bib2html section in target file # sub RegenerateHTMLBib { my ($HTMLBibFile, $AuxFile, $BibName) = @_; # bib2html uses special style files. However, it is too stupid to # set the environment variable to tell bibtex this. So we do it # here. $ENV{'BSTINPUTS'} = $Bib2HTMLDir; # bib2html options: # -a include abstracts # -c chronological sorting # -r reverse sorting order # -d XXX use XXX as part of delimeter marker in HTML file # -t FILE get \bibcite commands from FILE # -s STYLE use STYLE list labels # # (Dis)Advantages of various values for STYLE: # empty: # bullet lists --- hard to see crossrefs visually. # plain: # numbered list --- easy to see crossrefs visually, but # hard to read them directly. # alpha: # labelled list with "KW94" style labels --- somewhat # easy to see crossrefs visually, can be read directly # somewhat easily, but ugly. # named: # labelled list with "Kfoury and Wells, 1994" style # labels --- easy to see crossrefs visually, can be read # directly, fairly good looking, but sometimes omits # "Wells" due to author lists longer than 2 names. # Fixing requires hacking html-btxbst.doc, ugh. my $command = ("$Bib2HTMLProg -s plain " . ((defined $Bib2HTMLOpts) ? "$Bib2HTMLOpts " : "") . (((defined $BibName) && ($BibName ne '')) ? "-d $BibName " : "") . "-t $AuxFile $HTMLBibFile"); #debug_print "command: $command"; (system ($command) == 0) || die "system failed"; } RegenerateHTMLBib ($HTMLBibFile, $AuxFile, $BibDelim); # my (%Name_to_URL) = # ( # 'Jeffrey Considine' => 'http://cs-people.bu.edu/jconsidi/', # 'Allyn Dimock' => 'http://www.das.harvard.edu/users/students/Allyn_Dimock/Allyn_Dimock.html', # 'Fairouz Kamareddine' => 'http://www.cee.hw.ac.uk/~fairouz/', # 'A. J. Kfoury' => 'http://www.cs.bu.edu/~kfoury/', # 'Assaf J. Kfoury' => 'http://www.cs.bu.edu/~kfoury/', # # 'Harry G. Mairson' => 'http://www.cs.brandeis.edu/~mairson/', # 'Harry G. Mairson' => 'http://cs-people.bu.edu/mairson/', # # 'Robert Muller' => 'http://www.cs.bc.edu/~muller/', # 'Robert Muller' => 'http://oak.bc.edu/~muller/', # 'Alejandro Ríos' => 'http://www.dc.uba.ar/academic/profesor.htm', # # 'Franklyn Turbak' => 'http://www.wellesley.edu/CS/fturbak.html', # # 'Franklyn A. Turbak' => 'http://www.wellesley.edu/CS/fturbak.html', # 'Franklyn Turbak' => 'http://www-swiss.ai.mit.edu/~lyn/lyn.html', # 'Franklyn A. Turbak' => 'http://www-swiss.ai.mit.edu/~lyn/lyn.html', # 'René Vestergaard' => 'http://www.cee.hw.ac.uk/~jrvest/', # 'J. B. Wells' => 'http://www.cee.hw.ac.uk/~jbw/', # 'Ian Westmacott' => 'http://cs-people.bu.edu/ianw/', # ); # # # Convert the NAME->URL mapping into NAME_PATTERN->REPLACEMENT_TEXT format. # my (%NamePatt_to_LinkedName); # { # my ($Name); # foreach $Name (keys %Name_to_URL) { # #print "Name: [$Name]\n"; # my ($ReplacementName) = $Name; # while (($ReplacementName =~ s/ / /g) >= 2) { # $ReplacementName =~ s/ / /; } # my ($NamePatt) = $ReplacementName; # $NamePatt =~ s/ /\\s+/; # #print "NamePatt [$NamePatt]\n"; # my ($URL) = $Name_to_URL{$Name}; # my ($Replacement) = "$ReplacementName"; # #print "Replacement [$Replacement]\n"; # $NamePatt_to_LinkedName{$NamePatt} = $Replacement; }} # # # Rewrite file using the mapping %NamePatt_to_LinkedName. # with_file_contents $HTMLBibFile, sub { # my ($PreNamePatt) = '
|,\s+|\s+and\s+'; # my ($PostNamePatt) = '.|,|\s+and\s+'; # my ($NamePatt); # foreach $NamePatt (keys %NamePatt_to_LinkedName) { # my ($Replacement) = $NamePatt_to_LinkedName{$NamePatt}; # s/($PreNamePatt)$NamePatt($PostNamePatt)/$1$Replacement$2/g; }}; my (%CiteKey_to_Number); sub FixCites { s{( \[? ) (\d*) ( \]? )} { do { my $key = $3; my $old = $1 . $5 . $6; my $new; if (defined $CiteKey_to_Number{$key}) { $new = $1 . $CiteKey_to_Number{$key} . $6; } else { $new = $old; } #print STDERR "key: [$key]; CiteKey_to_Number{key}: [$CiteKey_to_Number{$key}]; old: [$old]; new: [$new]\n"; $new; } }eigx; } # Generate a mapping from cite keys to their numbers in the # bibliography listing. This only works assuming numeric style. Also # reverse the numeric labels if asked. This depends greatly on the # implementation of bib2html. with_file_contents $HTMLBibFile, sub { my ($max, $min); while (m|\[(\d+)\]|g) { #print "cite key: [$1]\n"; #print "number: [$2]\n"; $CiteKey_to_Number{$1} = $2; $max = max($max, $2); $min = min($min, $2); } if ($ReverseNumericOrder) { foreach my $key (keys %CiteKey_to_Number) { $CiteKey_to_Number{$key} = $max - $CiteKey_to_Number{$key} + $min; } s{(\[)(\d+)(\])}{$1 . $2 . $3 . $CiteKey_to_Number{$2} . $5}ge; #print STDERR "about to call FixCites on $HTMLBibFile\n"; FixCites(); #print STDERR "just called FixCites on $HTMLBibFile\n"; } }; # $CiteFile may contain citations like this: # # 123 # # If "ABC" is a label for a bibliography entry listed in $HTMLBibFile, # then we presume that "..." is a path that reaches $HTMLBibFile (in # general, we have no good way to decide this) and that "123" is (was) # the number for bibliography entry "ABC" in $HTMLBibFile. Because # these numbers can change when $HTMLBibFile is regenerated, we update # the number "123" to the current correct number for "ABC" in # $HTMLBibFile. # if ((defined $CiteFile) && (-f $CiteFile)) { # my ($HTMLBibFileBaseName) = $HTMLBibFile; # $HTMLBibFileBaseName =~ s|.*/||; with_file_contents $CiteFile, \&FixCites; } # Local variables: # mode: perl # end: