#! /usr/bin/perl # DO NOT EDIT # This file created by configure from the "sowing" package # -*- Mode:Perl; -*- # # Minimal processing of a BibTeX database file to make it html # Usage: # bib2html filename > output.html # bib2html -group gfilename filename > output.html # where gfilename is a file containing regular expressions for # author's names and matching URLs. See mpich.group for an example. # -ignore value specifies a field to ignore (e.g., -ignore annote eliminates # annote fields from the output. # # Add header/tail (with link to this code) # Convert comments to %...
# Convert @{ or ( to @ # # To customize for yourself, change the following three lines. $UserName = "William Gropp"; $userURL = "http://www.mcs.anl.gov/~gropp"; # This is W. or William (the W(\.|illiam)), optionally followed by D. (the # ( D\.)?), followed by Gropp. $UserREName = "W(\.|illiam)( D\.)? Gropp"; # For names that have accents, e.g., \'e, use the HTML entity: # J(\.|orge) Mor&eaccute; # for Jorge Mor\'e # # Using styles # You can define # # and then use # $EntryStart = "

\n"; # $EntryEnd = "

\n"; # to group an entry. # Other style elements that can be used are # font-family # font-weight # text-align # color # width # margin-left # margin-right # $GroupEmphStart = ""; $GroupEmphEnd = ""; # $announce_oldstyle = 0; $groupsize = 0; $ignoresize = 0; $outlist = 0; $debug = 0; $special_entity = 1; $check_valid_fields = 0; # # Default Formats for outlist # Book # title, author, publisher, year. %htmlformatsBook = ( 'starttitle' => '', 'endtitle' => '' ); @fieldlistBook = (title, author, publisher, note, year); @fieldlistoptionalBook = (address, pages, isbn, lccn, series, keywords, editor, annote, pubaddress, key ); # # Article # title, author, journal, volume, number, pages, year. %htmlformatsArticle = ( 'starttitle' => '', 'endtitle' => '' ); @fieldlistArticle = (title, author, journal, volume, number, pages, note, year); @fieldlistoptionalArticle = (month, annote, url, acknowledgement, bibdate, issn, coden, keywords, classification, conflocation, corpsource, conftitle, sponsororg, treatment, key, affiliation, thesaurus, key, journalabr ); # InBook # title, author, publisher, year. %htmlformatsInBook = ( 'starttitle' => '', 'endtitle' => '' ); # chapter is the chapter title. @fieldlistInBook = (title, chapter, author, booktitle, publisher, note, year); @fieldlistoptionalInBook = (address, pages, isbn, lccn, series, keywords, editor, annote, pubaddress, key ); # # InProceedings # title, author, booktitle, editors, pages, year %htmlformatsInProc = ( 'starttitle' => '', 'endtitle' => '' ); @fieldlistInProc = (title, author, booktitle, editor, pages, note, year); @fieldlistoptionalInProc = (number, month, series, publisher, address, volume, keywords, acknowledgement, treatment, corpsource, conftitle, classification, isbn, organization, confdate, meetingdate, meetingdate2, sponsor, sponsororg, thesaurus, affiliation, bibdate, annote, url, issn, confloc, eariler, owner, conflocation, crossref, editors, conference, meetingaddress, earlier, coden, key, pubaddress, category, place, affiliationaddress, journalabr ); %htmlformatsInColl = %htmlformatsInProc; @fieldlistInColl = @fieldlistInProc; @fieldlistoptionalInColl = @fieldlistoptionalInProc; # # Techreport # title, author, institution, number, year %htmlformatsTech = ( 'starttitle' => '', 'endtitle' => '' ); @fieldlistTech = (title, author, institution, organization, number, note, year); @fieldlistoptionalTech = (month, keywords, url, language, notes, pages, bibdate, later, scope, type, abstract-url, annote, address, key, location, category ); # # Misc # title, author, institution, number, year %htmlformatsMisc = ( 'starttitle' => '', 'endtitle' => '' ); @fieldlistMisc = (title, author, institution, organization, number, note, year); @fieldlistoptionalMisc = ( month, annote, key, address, type ); # Manual, Unpublished come from Misc for now. We don't need to define these # because Misc is the default #%htmlformatsManual = %htmlformatsMisc; #@fieldlistManual = @fieldlistMisc; #%htmlformatsUnpublished = %htmlformatsUnpublished; #@fieldlistUnpublished = @fieldlistMisc; # Bibtex entry type mappings %shortentrynames = ( 'Book' => 'Book', 'Article' => 'Article', 'Inproceedings' => 'InProc', 'Incollection' => 'InColl', 'Techreport' => 'Tech', 'Misc' => 'Misc', 'Unpublished' => 'Misc', 'Manual' => 'Misc' ); # Example Header entries for a listfile configuration file. # $HeaderRE = "Section:"; # $HeaderStart = "

"; # $HeaderEnd = "

"; # while ($_ = $ARGV[0]) { shift; if ($_ eq "-group") { # Read from group file $ARGV[0] # Format is RE!URL # (RE is regular expression for matching the name) $groupname = $ARGV[0]; shift; open( GROUP, "<$groupname" ) || die "Could not open group $groupname!\n"; while () { chop; if ($_ ne "") { # Do we want !/^\s*$/? ($GroupREName[$groupsize],$GroupURL[$groupsize]) = split /!/; $groupsize ++; } } } elsif ($_ eq "-ignore") { # Read name from cmdline $IgnoreFields[$ignoresize] = lc( $ARGV[0] ); $ignoresize++; shift; } elsif ($_ eq "-listfile") { $outlist = 1; $listfile = $ARGV[0]; shift; } elsif ($_ eq "-listconfig") { $listconfig = $ARGV[0]; shift; } elsif ($_ eq "-title") { $TitleString = $ARGV[0]; shift; } elsif ($_ eq "-debug") { $debug = 1; } elsif ($_ eq "-nospecials") { $special_entity = 0; } elsif ($_ eq "-nooldstyle") { $announce_oldstyle = 1; } elsif ($_ eq "-check") { $check_valid_fields = 1; } elsif ($_ eq "-help") { print "bib2html [ -listfile name ] [ -listconfig name ] \ [ -group name ] [ -ignore name ] [ -title name ] [ -nospecials ]\ [ -nooldstyle ] [ -check ] filename \ -listfile name - output a bibliography listing on file name\ -listconfig name - Use file name to control the format of\ the list file. This is a Perl script defining\ \$ListHeader - HTML file header (to BODY)\ \$ListFooter - HTML file footer (from /BODY)\ \$BibFileName - Name of bib file (used for links)\ \$HeaderRE - Regular expression that will be matched in TeX comments\ \$HeaderStart - HTML inserted in listfile before header\ \$HeaderEnd - HTML inserted in listfile after header\ \$GroupEmphStart - HTML inserted before names found in the\ group file\ \$GroupEmphEnd - HTML inserted after names found in the\ group file\ \%htmlformatsBook - A Perl has containing HTML commands for\ formatting a Book entry\ \@fieldlistBook - A Perl array containing the BibTeX fields\ to use when formatting a book\ (Similarly for Article, Techreport, InProc (proceedings)\ and Misc)\ A typical listconfig file might contain:\ \ \$ListHeader = \"Bib for me\\\ \ \\n\";\ \$ListFooter = \"\\n\";\ \$BibFileName = \"mpich2-bib.htm\";\ \$EntryStart = \"\";\ \$EntryEnd = \"

\\n\";\ \$HeaderRE = \"Section:\";\ \$HeaderStart = \"

\";\ \$HeaderEnd = \"

\";\ \$GroupEmphStart = \"\";\ \$GroupEmphEnd = \"\";\ 1;\n\ -group name - File containing author names and associated URLs\ -ignore name - Ignore field \"name\" in each bib entry\ -title name - Name to use in the title of the BiBTeX page\ -nospecials - Do not use certain valid HTML character entities\ such as endash that some older browers do not \ recognize.\ -nooldstyle - Print uses of oldstyle LaTeX to stderr.\ -check - Warn about unrecogonized BibTeX fields names.\ \n"; exit 0; } else { $bibfile=$_; } } open( BIB, "<$bibfile" ) || die "Could not open $bibfile!\n"; if ($outlist) { open( LISTFILE, ">$listfile" ) || die "Could not open $listfile\n"; if ($listconfig ne "") { require $listconfig; print LISTFILE $ListHeader; } } if ($TitleString eq "") { $TitleString = "Bibliography for $UserName"; } print "\n\n"; print "$TitleString\n"; print "\n"; print "\n"; print "\n"; # # Still to do: # Process @string(name=value); replace all uses of @name with # value in formatted (listfile) version. # Allow multiple HeaderRE's # Allow fields in the entryinfo to modify the formatting. For example, # we might let @field@ be replaced by the value of entryinfo{'field'}. # %bibstrings = (); $inrow = ""; $intable = 0; $dooutput = 1; $line_count = 0; $MakeBold = ""; # Format as table while () { $line = $_; $line_count++; # Look for a new bib entry starting a line if (/^\s*\@[Ss][Tt][Rr][Ii][Nn][Gg]\s*[\(\{]\s*([^=\s]*)\s*=\s*(.*)\s*[\)\}]/) { # Found @string name = value $bibstrings{$1} = $2; next; } elsif (/^\s*@([a-zA-Z]*)\s*[({]/) { # Found the beginning of a BibTeX entry (@....) $entry_type = lc( $1 ); if ($intable == 1) { # Finish off the previous table print "\n"; } #$line = $entryinfo{$fieldname}; #$line =~ s%[^=]*=%%; #$entryinfo{$fieldname} = $line; %entryinfo = (); # Clear entry info $entryinfo{"entry_type"} = $entry_type; $inrow = ""; $intable = 1; $dooutput = 1; s%@([a-zA-Z]*)([({])\s*(.*)\s*,\s*$%@$1$2$3,\n%g; $entryinfo{'key'} = $3; # Setup the list of expected field names based on the entry_type %knownfields = (); $entryname = ucfirst $entry_type; if (defined($shortentrynames{$entryname})) { $entryname = $shortentrynames{$entryname}; } $fieldref = "fieldlist$entryname"; # @$fieldref is the array @fieldlist$entryname #$fieldref->[i] accesses the ith element of the fieldlist %knownfields = (); for ($k=0; $k <= $#$fieldref; $k++) { $knownfields{$fieldref->[$k]} = 1; } $fieldoptref = "fieldlistoptional$entryname"; for ($k=0; $k <= $#$fieldoptref; $k++) { $knownfields{$fieldoptref->[$k]} = 1; } $knownfields{'abstract'} = 1; } elsif (/^\s*%\s*($HeaderRE)(.*)$/) { # Found a special comment that we want modified and added to the # LISTFILE. $headertext = $2; if ($outlist && ($HeaderRE ne "" || $HeaderEnd ne "" || $HeaderStart ne "")) { print LISTFILE "$HeaderStart $headertext $HeaderEnd\n"; } } else { # Look for a new item entry in a bib entry # We may want to skip some items # One thing to be careful of is multi-line items. # Ignore lines that are NOT field asignments, such as # abstract entries that include an = sign. # (This isn't quite right, because a line within an entry that # is = will still match if (/^\s*\w+\s*=/) { # Found field = .... if ($intable == 0) { print STDERR "Missing bibtex entry type for line #$line_count:$_\n"; print "
\n"; $intable = 1; } # Finish off the previous line print "$MakeBold$inrow\n" ; $inrow = ""; # Check to see if this line is one that we wish to # suppress # ---- not completely implemented /^\s*(\w+)\s*=/; print "matched $1 in $_" if $debug; $fieldname = lc( $1 ); if ($fieldname eq "") { print STDERR "Matched empty field in $_"; } for ($i = 0; $i <= $#IgnoreFields; $i++) { if ($IgnoreFields[$i] eq $fieldname) { $dooutput=0; next; } } if ($check_valid_fields && !defined($knownfields{$fieldname})) { print STDERR "Field $fieldname not known for $entryname on line $line_count\n"; } $entryinfo{$fieldname} = ""; $line =~ s/^\s*[^=%]*\s*=//; # ---- # Make the "title" text bold, others regular print "Found $fieldname\n" if $debug; if ($fieldname eq "title" || $fieldname eq "chapter") { $MakeBold=""; } else { $MakeBold = ""; } # Output this line s%^([^=]*)[ ]*=\s*(.*)\s*$%
$1 = $MakeBold$2%; if ($MakeBold ne "") { $MakeBold = ""; } } # Check of the end of entry (must be alone on a separate line) # fix emacs broken matching mode })} else { if (/^[ ]*[})][ ]*$/) { if ($dooutput == 1) { #({ s%^[ ]*([})])[ ]*$%
\n$1%; } else { s%^[ ]*([})])[ ]*$%\n$1%; } if ($outlist && defined($entryinfo{"entry_type"})) { &PrintEntry( %entryinfo ); } elsif ($debug && $outlist) { print %entryinfo; } %entryinfo = (); # Clear entry info $fieldname = ""; $intable = 0; $dooutput = 1; } } } print "Current line = \"$line\"\n" if $debug; $original_line = $line; # Fix HTML reserved characters $line =~ s%[\\]+&%&%; $line =~ s%[\\]+<%<%; $line =~ s%[\\]+>%>%; # Convert various TeX commands # $...$ $line =~ s%\$([^\$]*)\$%$1%; # ~ (but not \~) $line =~ s%([^\\]{1})~%$1 %; # \textrm{} (currently only strips out the command) $line =~ s%\\textrm{([^}]*)}%$1%; # \textem{} (set to em, won't work if nested) $line =~ s%\\textem{([^}]*)}%$1%; # \texttt{} (set to tt, won't work if nested) $line =~ s%\\texttt{([^}]*)}%$1%; # \url{} $line =~ s%\\url{([^}]*)}%$1%; # {\tt ... } if ($announce_oldstyle && /\\tt /) { print STDERR "line $line_count: \\tt in $original_line"; } $line =~ s%{\\tt([^}]*)}%$1%; # {\it ... } if ($announce_oldstyle && /\\it /) { print STDERR "line $line_count: \\it in $original_line"; } $line =~ s%{\\it([^}]*)}%$1%; # \~{ } $line =~ s%\\~\{\s*\}%~%g; # \# $line =~ s%\\#%#%g; # \o $line =~ s%\\o([^A-Za-z]{1})%ø$1%; # \O $line =~ s%\\O([^A-Za-z]{1})%Ø$1%; # _{} -> ... # ^{} -> ... # \_ -> _ # \{ -> { # \} -> } # \(.*\) -> ... # \'{letter} {} is optional $line =~ s%\\'\{([AEIOUYaeiouy]{1})\}%&$1acute;%; $line =~ s%\\'([AEIOUYaeiouy]{1})%&$1acute;%; # \~ $line =~ s%\\~\{([ANOano]{1})\}%&$1tilde;%; $line =~ s%\\~([ANOano]{1})%&$1tilde;%; # \" $line =~ s%\\"\{([AEIOUaeiou]{1})\}%&$1uml;%; $line =~ s%\\"([AEIOUYaeiouy]{1})%&$1uml;%; # \` $line =~ s%\\`\{([AEIOUaeiou]{1})\}%&$1grave;%; $line =~ s%\\`([AEIOUaeiou]{1})%&$1grave;%; # # \. $line =~ s%\\`\{([AEIOUaeiou]{1})\}%&$1circ;%; $line =~ s%\\`([AEIOUaeiou]{1})%&$1circ;%; # --- -> — $line =~ s%---%—% if $special_entity; # -- -> – $line =~ s%--%–% if $special_entity; print "Adding field $fieldname with text $line" if $debug; $entryinfo{ $fieldname } .= $line; # Turn URLs into links in $_ s%(http:[^ }"]*)%$1%g; s%(ftp:[^ }"]*)%$1%g; # Comment the following to not use links for author names # This also makes the author name bold s%($UserREName)%$GroupEmphStart$1$GroupEmphEnd%g; # For a group's bibliography, an array of UserREs and the corresponding # URLs could be used (preloaded as an option to this command) for ($i = 1; $i <= $#GroupURL; $i++) { s%($GroupREName[$i])%$GroupEmphStart$1$GroupEmphEnd%g; } # Add a BR to preserve formating outside of tables if ($intable == 0) { s/$/
/; } print $_ if $dooutput == 1; } $curtime = localtime; print "Generated on $curtime with bib2html\n"; print "\n"; print "\n"; if ($listconfig ne "") { print LISTFILE $ListFooter; } # # This routine replaces @xxx@ with the value of xxx in the entry info. # This allows you to use bibtex fields to control formatting by using # the class=name style directive. sub ReplaceParms { $textline = $_[0]; $default = $_[1]; if ($textline =~ /@([a-z]*)@/) { $token = $1; $replacement = $entryinfo{$token}; $replacement =~ s/^\s*//; if ($replacement eq "") { $replacement = $default; } $textline =~ s/\@$token@/$replacement/; } return $textline; } # Print out an entry sub PrintEntry { $entry_type = $entryinfo{"entry_type"}; print "Printing $entry_type\n" if $debug; # First, clean up values foreach $key (keys %entryinfo) { $value = $entryinfo{$key}; $value =~ s/,\s*$//s; $value =~ s/^\s*(\"|\{){1}(.*)(\"|\}){1}/$2/s; chomp $value; $entryinfo{$key} = $value; } if ($debug) { print %entryinfo ; } if (defined($entryinfo{'key'})) { my $keyname = $entryinfo{'key'}; print LISTFILE "\n"; } if ($EntryStart ne "") { $textline = &ReplaceParms( $EntryStart ); print LISTFILE $textline; } if ($entry_type eq "book") { &PrintBook; } elsif ($entry_type eq "article") { &PrintArticle; } elsif ($entry_type eq "inproceedings" || $entry_type eq "incollection") { &PrintInProceedings; } elsif ($entry_type eq "techreport") { &PrintTechreport; } else { # Works for manual, unpublished, misc, etc &PrintMisc; } if ($EntryEnd ne "") { $textline = &ReplaceParms( $EntryEnd ); print LISTFILE $textline; } } sub TitleCase { # Convert to title case, but don't change case inside of {}. } sub AuthorList { my $fieldname = $_[0]; # Convert all but last "and" into , $author = $entryinfo{$fieldname}; @authors = split /\s\s*and\s*\s/, $author; print @authors if $debug; print "n = $#authors\n" if $debug; if ($#authors > 1) { $author = ""; for ($i=0; $i<$#authors; $i++) { $author .= $authors[$i]; $author .= ", "; } $author .= "and $authors[$#authors]"; } # Comment the following to not use links for author names # This also makes the author name bold $author =~ s%($UserREName)%$GroupEmphStart$1$GroupEmphEnd%g; # For a group's bibliography, an array of UserREs and the corresponding # URLs could be used (preloaded as an option to this command) for ($i = 1; $i <= $#GroupURL; $i++) { $author =~ s%($GroupREName[$i])%$GroupEmphStart$1$GroupEmphEnd%g; } $entryinfo{$fieldname} = $author; } sub PrintBook { # title, author, publisher, year. %htmlformats = %htmlformatsBook; # We should really check for author; if no author but an editor, # use the editor field instead. @fieldlist = @fieldlistBook; if (!defined($entryinfo{'author'}) && defined($entryinfo{'editor'})) { for ($i=0; $i<$#fieldlist; $i++) { if ($fieldlist[$i] eq 'author') { $fieldlist[$i] = 'editor'; last; } } } &PrintGeneral; } sub PrintArticle { # title, author, journal, volume, number, pages, year. %htmlformats = %htmlformatsArticle; @fieldlist = @fieldlistArticle; &PrintGeneral; } sub PrintInProceedings { # title, author, booktitle, editors, pages, year %htmlformats = %htmlformatsInProc; @fieldlist = @fieldlistInProc; &PrintGeneral; } sub PrintTechreport { # title, author, institution, number, year %htmlformats = %htmlformatsTech; @fieldlist = @fieldlistTech; &PrintGeneral; } sub PrintMisc { # title, author, institution, number, year %htmlformats = %htmlformatsMisc; @fieldlist = @fieldlistMisc; &PrintGeneral; } sub CleanString { $value = $_[0]; # Remove extra {} pairs $value =~ s/{([^{}]*)}/$1/gs; return $value; } sub PrintGeneral { &AuthorList( 'author' ); &AuthorList( 'editor' ); $firstitem = 1; foreach $key (@fieldlist) { if (!defined($entryinfo{$key})) { next; } $value = $entryinfo{$key}; if ($value eq "") { next; } if (!$firstitem) { print LISTFILE ", ";} else { $firstitem = 0; } $keyname = "start$key"; print LISTFILE "$htmlformats{$keyname}"; $value = &CleanString( $value ); # # Here is where we look for @name and then look up name # in bibstrings. Or we could just keep a list of strings and # replacements (just like the GroupREName and GroupURL) if ($key eq 'title' && defined($entryinfo{'url'})) { $url = $entryinfo{'url'}; print LISTFILE "$value"; } else { print LISTFILE "$value"; } $keyname = "end$key"; print LISTFILE "$htmlformats{$keyname}"; } print LISTFILE ".\n"; print LISTFILE "BibTeX\n"; }