-
Notifications
You must be signed in to change notification settings - Fork 0
/
bibextract.pl
147 lines (107 loc) · 3.84 KB
/
bibextract.pl
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/perl
########################################################
#
# This script is part of the biblatex-archaeology
# package by Ingram Braun. It sole purpose is to
# extract publishable databases from the author's
# private (and unpublished) bib file.
#
# call: see bibtex.bat
#
########################################################
use strict;
no warnings;
use FileHandle;
use BibTeX::Parser;
#use Data::Dumper;
my $project = shift @ARGV;
$project .= ".aux";
my $dtx = shift @ARGV;
my $driver = shift @ARGV;
my @inbib = @ARGV;
my %db; # entries of outfile
my %buffer; # other entries, needed for crossref check
# CTAN demands UNIX line feeds
my $newline = "\012";
# get keys from aux.file
my $aux = FileHandle->new("< $project");
if (defined $aux) {
binmode($aux, ":utf8");
print "READ $project\n";
while(<$aux>) { $db{$1}{count}++ if $_ =~ /\\abx\@aux\@cite\{(.+?)\}/; }
undef $aux; # automatically closes the file
}
else { die "Could not open $project: $1" }
# read databases
foreach my $inbib (@inbib) {
print "READ $inbib\n";
my $fh = FileHandle->new("< $inbib");
binmode($fh, ":utf8");
# Create parser object ...
my $parser = BibTeX::Parser->new($fh);
my $entry;
# ... and iterate over entries
while ( $entry = $parser->next ) {
my %entries;
if ( $entry->parse_ok ) {
$entries{$entry->key}{entry} = $entry->field("_raw");
$entries{$entry->key}{type} = $entry->type;
push( @{$entries{$entry->key}{crossref}}, $entry->field("crossref") );
push( @{$entries{$entry->key}{crossref}}, $entry->field("xref") );
push( @{$entries{$entry->key}{crossref}}, split( /,\s*/, $entry->field("entryset") ) );
push( @{$entries{$entry->key}{crossref}}, split( /,\s*/, $entry->field("related") ) );
push( @{$entries{$entry->key}{crossref}}, split( /,\s*/, $entry->field("xdata") ) );
if ( exists( $db{ $entry->key } ) ) {
$db{ $entry->key } = $entries{ $entry->key };
}
else {
$buffer{ $entry->key } = $entries{ $entry->key };
}
}
else {
die "Error parsing file: " . $entry->error;
}
}
$fh->close();
}
# crossref check
my $found = 1;
unless ( !$found ) {
undef $found;
foreach my $selected ( keys %db ) {
foreach my $crossref ( @{$db{$selected}{crossref}} ) {
if ( length $crossref > 0 && !exists( $db{$crossref} ) ) {
if ( !exists( $buffer{$crossref} ) ) {
warn "Referenced key '$crossref' does not exist in database!\n";
}
else {
$found = 1;
$db{$crossref} = $buffer{$crossref};
}
}
}
}
}
# generate macrocode environment of ltxdoc class
my $bib = "$newline% \\begin{macrocode}$newline";
grep { $bib .= $db{$_}{entry} . "$newline" if $db{$_}{type} =~ /^article|^in|^supp|^review/i } sort keys %db;
grep { $bib .= $db{$_}{entry} . "$newline" if $db{$_}{type} !~ /^article|^in|^supp|^review/i } sort keys %db;
$bib .= "% \\end{macrocode}$newline";
$bib =~ s/\n\s+(\w+)\s?=/$newline \U\1\E =/gs;
$bib =~ s/\n/$newline/gs;
# copy database into dtx file
my $source = "";
my $out = FileHandle->new($dtx, O_RDONLY);
if (defined $out) {
binmode($out, ":utf8");
while ( <$out> ) { $source .= $_ }
$source =~ s/(<\*\Q$driver\E>\n%\s?\\fi\n).*?(\n%\s\\iffalse\n%<\/\Q$driver\E>)/\1$bib%\2/s;
undef $out; # automatically closes the file
print "\nPATTERN: ", $&. "\n\n";
}
$out = FileHandle->new($dtx, O_WRONLY|O_TRUNC);
if (defined $out) {
binmode($out, ":utf8");
print $out $source;
undef $out; # automatically closes the file
}