#!/usr/bin/env perl
use strict;
my $RCSID = '$Id: bib2html 20301 2007-12-11 12:24:42Z lefevre $';
my ($proc,$vers,$dt) = $RCSID =~
/^.Id: (\S+) (\d+) (\d{4}-\d\d-\d\d \d\d:\d\d:\d\d)Z/ or die;
my %halserver = ('ensl' => 'prunel.ccsd.cnrs.fr', 'inria' => 'hal.inria.fr');
my %s = (
'jan' => 'January',
'feb' => 'February',
'mar' => 'March',
'apr' => 'April',
'may' => 'May',
'jun' => 'June',
'jul' => 'July',
'aug' => 'August',
'sep' => 'September',
'oct' => 'October',
'nov' => 'November',
'dec' => 'December',
'preprint' => 'Preprint',
'toappear' => 'To appear',
'rr' => 'Research report',
'tr' => 'Technical report',
);
my $date = "{,\n{*month }*year}";
my $amy = "{,\n*address}$date";
my $vol = "{,\nvolume *volume}";
my $pages = "{,\npages *pages}";
my %format = (
'article'
=> "*journal{,\n{*volume}{(*number)}{:*pages}}$date.",
'incollection'
=> "In *booktitle$pages.\n*publisher$amy.",
'inproceedings'
=> "In {*editor, editor,\n}*booktitle$vol$pages$amy.{\n*publisher.}",
'misc'
=> "{*type,\n}{*month }*year.",
'phdthesis'
=> "PhD thesis{,\n*school}$amy.",
'techreport'
=> "*type *number{,\n*institution}$amy.",
);
my (%a,$acronyms,%acr);
# Note: use the html root element instead of dl, though dl should have
# been sufficient. This is necessary in order to use the xhtml namespace,
# as only the html element may have an xmlns attribute (the XHTML DTD is
# designed very poorly IMHO).
print <
V. Lefèvre's Publications
EOF
while (<>)
{
if (/^%a\{(.+)}\s*=\s*(\S+)\s*$/)
{
$a{$1} = $2;
next;
}
if (/^%acronym{([A-Z]+)}\s*=\s*"(.+)"\s*$/)
{
$acr{$1} = $2;
$acronyms = join '|', keys %acr;
next;
}
if (/^(.Id:.*)/)
{
print "\n";
next;
}
my ($type,$key) = /^@(\w+)\{(\w+),\s*$/ or next;
print "- [$key]
\n";
my %entry = %{&getentry($type)};
if ($entry{'type'} eq '' && $entry{'note'} =~ /^preprint$/i)
{
$entry{'type'} = 'Preprint';
delete $entry{'note'};
}
print "- \n";
my @authors;
foreach (split /\s+and\s+/, $entry{'author'})
{
my $url = $a{$_};
s/\. /. /g;
push @authors, defined $url ?
"$_"
: "$_";
}
@authors and print "",
@authors > 1 ? join("\nand\n",
join(",\n", @authors[0..$#authors-1]),
$authors[$#authors])
: $authors[0], ".\n";
$_ = $entry{'title'};
defined $_ and
print defined $entry{'url'} ?
"$_.\n"
: "$_.\n";
my $format = $format{$entry{'A'}};
defined $format or die "$proc: unknown type $entry{A}\n";
my $c = 0;
my (@f,@s);
while ($format ne '')
{
if ($format =~ s/^\{//s)
{
$c++;
$f[$c] = 0;
$s[$c] = '';
next;
}
if ($format =~ s/^\}//s)
{
$f[$c--] and $s[$c] .= $s[$c+1], $f[$c]++;
next;
}
if ($format =~ s/^\*([a-z]+)//s)
{
my $type = $1;
$_ = $entry{$type};
if (defined $_)
{
my $e = $type =~ /title|journal/ ? 'cite' : 'span';
$f[$c]++;
$s[$c] .= "<$e class=\"$type\">$_$e>";
}
else
{
$c or die "$proc: undefined entry $1";
}
next;
}
$format =~ s/^(.)//s or die "$proc: internal error";
$s[$c] .= $1;
}
print "$s[0]\n";
$_ = $entry{'note'};
if (defined $_)
{
substr($_,-1) eq '.' or $_ .= '.';
print "$_\n";
}
my $altlinks;
$altlinks .= &altlink(&halurl($entry{'hal'}),
'hal/ccsd')
if defined $entry{'hal'};
foreach (split / +/, $entry{'other-url'})
{ $altlinks .= &altlink($_); }
$altlinks .= &altlink($entry{'abstract-url'}, 'abstract');
$altlinks .= &altlink($entry{'ri-url'}, 'citeseer');
my $nlinks = $altlinks =~ tr/[//;
$nlinks and print "
\nAlternative link",
($nlinks > 1 ? 's' : ''), ":\n$altlinks";
print " \n\n";
}
print <
EOF
sub getentry
{
my $entry = { 'A' => lc($_[0]) };
my $n = 1;
while (<>)
{
/^\s*\}\s*$/ and return $entry;
$n or die "$proc: missing comma\n";
/^\s*([-\w]+)\s*=\s*(\S.*?)(,?)\s*$/ or next;
my ($k,$v) = (lc($1),$2);
$n = $3;
if ($v !~ s/^\{(.*)\}$/$1/ && $v !~ s/^"(.*)"$/$1/)
{
defined $s{$v} or die "$proc: $v undefined";
$v = $s{$v};
}
$v =~ s/---/—/g;
$v =~ s/--/–/g;
$v =~ s!\{($acronyms)\}!{$1}!g;
$k eq 'title' and substr($v, substr($v,0,1) ne '{')
=~ s/(\{.*?\}|\$.*?\$|.)/lcfirst($1)/eg;
$v =~ s!\\mathbb\{(.*?)\}!$1!g;
$v =~ s!\\texttt\{(.*?)\}!$1!g;
$v =~ s!\^(\{.*?\}|.)!$1!g;
$v =~ tr/{}$//d;
$entry->{$k} = $v;
}
die "$proc: unexpected EOF";
}
sub altlink
{
my ($url,$txt) = @_;
return if $url eq '';
if ($txt eq '')
{
$url =~ /\.pdf$/i and
$txt = 'PDF';
$url =~ /\.ps(\.|$)/i and
$txt = 'postscript';
$txt eq '' and die "$proc: unknown link type for URL $url\n";
}
return <$txt]
EOF
}
sub halurl
{
my $host = ($_[0] =~ /^(\S+)-\d+$/ && $halserver{$1})
|| 'hal.archives-ouvertes.fr';
return "http://$host/$_[0]";
}