: # -*-Perl-*- # Convert INSPEC to BibTeX # Assumed format from http://www.louisville.edu/cgi-bin/library/inspec.scr # (publications separated by empty lines) eval 'exec perl -w -S $0 ${1+"$@"}' # Portability kludge if 0; # only non-empty items will be put in %db my %keytr = ( "AUTHOR" => "author", "CORP SOURCE" => "authoraddress", "TITLE" => "title", "SOURCE" => "journal", "ISSN" => "", "CODEN" => "", "PLACE OF PUBL" => "address", "LANGUAGE" => "", "YEAR" => "year", "COPYRIGHT NO" => "", "TREATMENT" => "", "ABSTRACT" => "abstract", "IDENTIFIERS" => "keywords", "CONF TITLE" => "booktitle", "CONF LOCATION" => "address", ); my $keyreg = " *([A-Z ]+): "; my $key = ""; my %db = (); # read assocs in while (<>) { chop; if (/$keyreg(.*)/) { $key = $keytr{$1}; if ($key) { $db{$key} = $2; } } elsif (/^\s*$/ && $db{"author"}) { # foreach $key (keys %db) { # print STDERR "$key = \"", $db{$key}, "\"\n"; # } outbib(\%db); $key = ""; %db = (); } else { if ($key) { $db{$key} .= $_; } } } if ($db{"author"}) { outbib(\%db); } exit 0; #=============== sub outbib { my %db = %{$_[0]}; my $def = ""; # clean whitespace foreach $key (keys %db) { $db{$key} =~ s/ +/ /g; $db{$key} =~ s/- /-/g; # join hyphenated # print STDERR "$key = \"", $db{$key}, "\"\n"; } # mangle my @authors = split('; ', $db{"author"}); foreach (@authors) { s/([^,]+), *(.+)/$2 $1/; # put first names first s/([A-Z]\.)(?=[A-Z]\.)/$1 /g; # separate first names } $db{'author'} = join(' and ', @authors); if ($db{'year'} =~ /([-A-Za-z]+) ([0-9]+)/) { $db{'month'} = $1; $db{'year'} = $2; } if ($db{'journal'} =~ /,? *p\. *([0-9]+ +vol\. *\(?[^\)]+\)?, *)?([^, ]+)/) { $db{'pages'} = $2; $db{'journal'} = "$`$'"; } if ($db{'journal'} =~ /,? *vol\. *([^, ]+)/) { $db{'volume'} = $1; $db{'journal'} = "$`$'"; } if ($db{'journal'} =~ /,? *no\. *([^, ]+)/) { $db{'number'} = $1; $db{'journal'} = "$`$'"; } if ($db{'address'} eq 'USA') { $db{'address'} = ''; } # output my @fields = ('author', 'title', 'journal', 'booktitle', 'volume', 'number', 'pages', 'address', 'month', 'year', 'abstract', 'keywords'); # author/year $key = (split(' ', $authors[0]))[-1] . substr($db{'year'}, 2, 2); my $header; if ($db{'booktitle'}) { $header = '@InProceedings{'; $db{'journal'} = ''; } else { $header = '@Article{'; } $header .= $key . ",\n"; # wrap to 72 columns my @lines = (); foreach (@fields) { $def = $db{$_}; if ($def) { my @words = split(' ', $def); my $slines = ""; my $sline = " " . sprintf("%-15s", $_ . ' = ') . '"'; foreach (@words) { if (length($sline) + length($_) < 72) { if (substr($sline, -1) ne '"') { # avoid extra indent $sline .= " " . $_; } else { $sline .= $_; } } else { $slines .= $sline; $sline = "\n" . (" " x 18) . $_; } } $slines .= $sline; push @lines, $slines; } } my $lines = join("\",\n", @lines); print $header; print $lines; print "\"\n}\n\n"; }