#!/usr/bin/perl -s use Text::BibTeX; our ($o,$debug,$nosubtitle,$m,$dir); my $b=shift or die("usage: bibtex2capas [option] f.bib [key*] options: -o=out.pdf -nosubtitle -debug -m (one output file for each entry) -dir=outputDir (output files are stored in outputDir) -kf=keyfile (file with the keys: one per line) "); die("cant find bibtex file: '$b'\n") unless -f $b; $o ||= "_$b.pdf"; mkdir($dir) if $dir; $o = "$dir/$o" if $dir; my $itemlist; if($kf){ open(F,$kf) or die("cant find Key file '$kf'\n"); while(){chomp;$itemlist->{$_}++}; close(F); } for (@ARGV){ $itemlist->{$_}++ } my $bibfile = new Text::BibTeX::File $b; if($m) { addcapa($bibfile,$itemlist); } else { mkcapas($bibfile,$itemlist); } sub mkcapas{ my $bibfile = shift; my $itemlist = shift; ## all items if not defined; my $tmp = "_biblio2capas$$"; open(O,">$tmp.tex") or die("cant create temporary file '$tmp.tex'\n"); starttex(); while (my $entry = new Text::BibTeX::Entry $bibfile) { next unless $entry->parse_ok; next if $itemlist && !defined($itemlist->{ $entry->key }); ($t,$a,$y,$k,$in,$pdf)=getentry($entry); next unless $t; printentry($t,$a,$y,$k,$in,$pdf); } endtex(); close(O); executa("biblio2tex -expand $tmp.tex _$tmp.tex"); executa("pdflatex _$tmp.tex"); rename("_$tmp.pdf",$o); unlink(<$tmp.*>) unless $debug; unlink(<_$tmp.*>) unless $debug; } sub addcapa{ my $bibfile = shift; my $itemlist = shift; ## all items if not defined; my $tmp = "_biblio2capas$$"; while (my $entry = new Text::BibTeX::Entry $bibfile) { open(O,">$tmp.tex") or die("cant create temporary file '$tmp.tex'\n"); starttex(); next unless $entry->parse_ok; next if $itemlist && !defined($itemlist->{ $entry->key }); ($t,$a,$y,$k,$in,$pdf)=getentry($entry); next unless $t; printentry($t,$a,$y,$k,$in,$pdf); endtex(); close(O); executa("biblio2tex -expand $tmp.tex _$tmp.tex"); executa("pdflatex _$tmp.tex"); my $aux="_$k.pdf"; $aux = "$dir/$aux" if $dir; if($pdf){ executa("gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=$aux -dBATCH _$tmp.pdf $pdf"); } else { rename("_$tmp.pdf",$aux)} unlink(<$tmp.*>) unless $debug; unlink(<_$tmp.*>) unless $debug; } } sub getentry{ my $entry=shift; my $metatype = $entry->metatype; my $k = $entry->key; my $type = $entry->type; my $type = $entry->type; my $in = $entry->get('shortin') || $entry->get('booktitle') || $entry->get('journal') || undef; $in = undef if $nosubtitle; my ($t,$a,$y,$pdf) = $entry->get ('title','author','year','pdf'); $a =~ s/\s+and\s+/ \\\\ /g; return () unless $t; return ($t,$a,$y,$k,$in,$pdf) } sub starttex { print O qq{ \\documentclass[portuges,a4paper]{article} \\RequirePackage[a4paper,top=2.5cm,left=2.5cm,right=2.5cm,bottom=2.5cm,nohead,nofoot]{geometry} \\usepackage{babel} \\usepackage[latin1]{inputenc} %\\usepackage[utf8]{inputenc} %\\usepackage{fancyvrb} \\usepackage{t1enc} \\usepackage{aeguill} \\usepackage{url} \\parindent 0pt \\parskip 20pt \\def\\thepage{} \\begin{document} }; } sub endtex{ print O qq{ \\bibliography{$b} \\bibliographystyle{plain} \\end{document} }; } sub printentry{ my ($t,$a,$y,$k,$in,$pdf)= @_; print O qq{ \\mbox{}\\hrule {\\raggedright\\huge\\hyphenpenalty=10000\\exhyphenpenalty=10000\\relax $t } {\\large{$a}} {\\Large{$y}} {$in}\\mbox{ }\\\\ \\hrule \\vspace{2cm} \\cite{$k} \\newpage ~ \\newpage }; } sub executa { my $cmd = shift; print STDERR "**** $cmd\n" if $debug; if(system ($cmd) == 0){ return 1} else { warn "** ERROR ************ system $cmd failed: $!$?\n"; return 0 } } __END__ =head1 NAME bibtex2capas - makes a PDF with frontages for all doc. in a Bibtex file =head1 SYNOPSIS bibtex2capas file.bib [key*] output is _file.bib.pdf bibtex2capas -o=f.pdf file.bib output is f.pdf =head1 DESCRIPTION For each (selected) item in the bibtex file, C buids a front page. The frontpage has the following aspect: ------------------------------------ TITLE Author1 Author2 Year (ShortIn | journal | booktitle) ------------------------------------ Full Reference.... You can use an extra bibtex field: shortin="TCS´98", =head2 Options -o=file.pdf -debug ( show commands that are being executed, and keep temporary files) -nosubtitle (skip Journal and booktitle in sub-titles) -m (one output file for each entry) -kf=keyfile (file with the keys: one per line) =head2 Dependencies LaTeX BibTeX Text::BibTeX (perl module to parse bibtex files); install it with: sudo cpan Text::BibTeX biblio2tex (to expand the bibtex citation) =head1 AUTHOR J.Joao Almeida, jj@di.uminho.pt =head1 SEE ALSO perl(1). =cut