summaryrefslogtreecommitdiff
path: root/btsort
blob: 803007b2e2247e495ed67965e16fd1dede131ad8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/local/bin/perl -w

#
# btsort
#
# Reads an entire BibTeX file, sorts the entries, and spits them back out
# again.
#
# $Id: btsort 3031 2006-09-21 20:02:34Z ambs $
#

use strict;
use Text::BibTeX (':metatypes');

my ($filename, $structure, @options, $bibfile, $entry, %sortkey, @entries);
die "usage: btcheck file [structure [options]]\n" unless @ARGV >= 1;
($filename, $structure, @options) = @ARGV;
$structure ||= 'Bib';

$bibfile = new Text::BibTeX::File $filename or die "$filename: $!\n";
$bibfile->set_structure ('Bib', @options);

while ($entry = new Text::BibTeX::Entry $bibfile)
{
   next unless $entry->parse_ok && $entry->metatype == BTE_REGULAR;
   $entry->check;
   $sortkey{$entry} = $entry->sort_key;
   push (@entries, $entry);
}
$bibfile->close;

@entries = sort { $sortkey{$a} cmp $sortkey{$b} } @entries;
$entry->print while $entry = shift @entries;