# Perl script to convert citations from SPIE bib download results to bibtex # To use this, download SPIE bibtex file into a file called "spie.txt" # Then run spie.pl to convert it to spie.bib # If you want to use an alternative name, you can specify it as an argument # To enable right-clicking, you can create an association with a .TXT file called "SPIE bib" having # an action "...\perl\bin\perl.exe" "...\spie.pl" "%1" [where the ... are system dependent] # could do "keywords" field for journals #% Copyright (C) Mike Brookes 2006 #% Version: $Id: spie.pl,v 1.3 2006/01/11 20:13:02 dmb Exp $ #% #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% #% 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 can obtain a copy of the GNU General Public License from #% ftp://prep.ai.mit.edu/pub/gnu/COPYING-2.0 or by writing to #% Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA. #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% $logging = 0; # set to 1 to enable logging if (@ARGV) { $infile = $ARGV[0]; } # only use the first argument for now else { $infile = "spie.txt"; } if ( $infile =~ /(.*)\.(.*)/ ) { $2 ne "bib" || die "Cannot convert a .bib file"; $inbase = $1; $inext = $2; } else { $inbase = $infile; $inext = ""; } $outfile = $inbase . ".bib"; open( INFILE, $infile ) || die "Can't open $infile\a\n"; open( OUTFILE, ">$outfile" ) || die "Can't open $outfile\a\n"; $logging and $logfile = $inbase . ".log"; $logging and open( LOGFILE, ">$logfile" ) || die "Can't open $logfile\a\n"; @gmt = localtime(time); $timestamp = sprintf( '%d.%02d.%02d', 1900 + $gmt[5], $gmt[4] + 1, $gmt[3] ); $inentry = 0; # inside an entry while () { s/^\s+//; # trim leading whitespace s/\s+$//; # and training whitespace tr/‘’/`'/; # fix bad quote marks s/“/``/g; s/"/``/; # fix leading double quote mark s/["”]/''/g; # and trailing ones $logging and print LOGFILE "$inentry: $_\n"; if ($inentry) { if ( $inentry = !/^}/ ) { if (/^([^\s]+)\s*=\s*\{(.*)\}/) { $fields{$1} = $2; $logging and print LOGFILE "*** $1 = $2\n"; } } else { # now print the entry # find the first author name $fields{"author"} =~ /.*?([^\s]+)\s*(and|,|$)/; $basekey = $1 . $fields{"year"}; if ( exists( $basecnt{$basekey} ) ) { # check if we have had a previous reference with this base key $fullkey = "$basekey$basecnt{$basekey}" ; # add on a sufffix: "a", "b", ... $basecnt{$basekey}++; } else { $basecnt{$basekey} = "a"; $fullkey = "$basekey"; # omit the sufffix if it is "a" } $logging and print LOGFILE "Printing entry: $fullkey\n"; if ($conf) { print OUTFILE "\@INPROCEEDINGS{$fullkey,\n"; print OUTFILE "author = {$fields{'author'}},\n"; print OUTFILE "title = {{$fields{'title'}}},\n"; print OUTFILE "booktitle = {{Proc SPIE Conf on $fields{'journal'}}},\n"; print OUTFILE "year = {$fields{'year'}},\n"; print OUTFILE "volume = {$fields{'volume'}},\n"; print OUTFILE "pages = {$fields{'pages'}},\n"; ( $fields{'location'} =~ /^([^\s,]+)/ ) && print OUTFILE "address = {$1},\n"; length( $fields{'url'} ) && print OUTFILE "url = {$fields{'url'}},\n"; print OUTFILE "timestamp = {$timestamp},\n"; print OUTFILE "}\n\n"; } else { print OUTFILE "\@ARTICLE{$fullkey,\n"; print OUTFILE "author = {$fields{'author'}},\n"; print OUTFILE "title = {{$fields{'title'}}},\n"; print OUTFILE "journal = {{$fields{'journal'}}},\n"; print OUTFILE "year = {$fields{'year'}},\n"; print OUTFILE "volume = {$fields{'volume'}},\n"; print OUTFILE "number = {$fields{'number'}},\n"; # print OUTFILE "pages = {$fields{'pages'}},\n"; if ( ( $fields{'pages'} =~ /-/ ) || !$fields{'numpages'} ) { print OUTFILE "pages = {$fields{'pages'}},\n"; } else { if ( $fields{'numpages'} > 1 ) { print OUTFILE "pages = {$fields{'pages'}.1-$fields{'numpages'}},\n"; } else { print OUTFILE "pages = {$fields{'pages'}.1},\n"; } } length( $fields{'url'} ) && print OUTFILE "url = {$fields{'url'}},\n"; print OUTFILE "timestamp = {$timestamp},\n"; print OUTFILE "}\n\n"; } } } else { if ( $inentry = /^@/ ) { $conf = /conference/; undef %fields; } } } # end while (< >) close INFILE; close OUTFILE; $logging and close LOGFILE;