#!/usr/bin/perl -w # Generate a text summary of a bunch of BibTeX files. # Adam Sampson use strict; use lib "$ENV{HOME}/Code/misccode"; use Bibliography qw(key_from_date); use Getopt::Long; sub usage () { print STDERR "Usage: bibsummary [OPTIONS] FILE ...\n\n"; print STDERR " --years Show year headings\n"; print STDERR " --sections Sort by sections\n"; exit 0; } my $with_years = 0; my $with_sections = 0; GetOptions( 'years' => \$with_years, 'sections' => \$with_sections, ) or usage(); sub get_section ($) { my ($entry) = @_; if ($with_sections) { my $type = $entry->type(); if ($type eq 'misc') { return 'Other'; } elsif ($type eq 'phdthesis' or $type eq 'proceedings' or $type eq 'book') { return 'Book'; } else { return 'Main'; } } else { return ''; } } my $bib = Bibliography->new; $bib->load(@ARGV); $bib->resolve_crossrefs(); # Place entries into sections in date order. my %sections = (); foreach my $entry (reverse $bib->sorted_entries(\&key_from_date)) { my $section = get_section($entry); push @{$sections{$section}}, $entry; } # Use BibTeX to format each entry. foreach my $section (keys %sections) { if ($section ne '') { print "\n== $section ==\n"; } my $prev_year = -1; foreach my $entry (@{$sections{$section}}) { my $key = $entry->key(); if ($with_years) { my $year = int($entry->get('year')); if ($year != $prev_year) { print "\n$year\n"; $prev_year = $year; } } print $bib->harvard_entry($entry) . "\n"; } }