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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
package BibTeX;
use Symbol 'qualify_to_ref';
%bibtex_prototypes = ('string' => 'p', 'preamble' => 'v', '_' => 'kp*');
sub parse_bibtex_key ($) {
my($fh) = @_;
$_ = <$fh> while ((/^\s+$/s || /^\s+%/) && !eof $fh);
if (/^\s*([^"#%'(),={}\s]+)(.*)/s) {
$_ = $2;
lc($1);
} else {
print STDERR "no key at line $.\n";
"";
}
}
sub parse_bibtex_value ($$) {
my($fh, $strings) = @_;
my($data) = "";
my($bracelevel, $line);
# loop over concatenation
while (1) {
# loop over lines
$_ = <$fh> while ((/^\s+$/s || /^\s+%/) && !eof $fh);
s/^\s+//;
if (eof $fh) {
print STDERR "unexpected end of file\n";
return $data;
}
# check type of thing
if (/^\"(.*)/s) {
$_ = $1;
$bracelevel = 0;
$line = $.;
while (1) {
if (!$bracelevel && /^([^{}\"]*)\"(.*)/s) {
$data .= $1;
$_ = $2;
last;
} elsif ($bracelevel && /^([^{}]*\})(.*)/s) {
$data .= $1;
$_ = $2;
$bracelevel--;
} elsif (/^([^{}]*\{)(.*)/s) {
$data .= $1;
$_ = $2;
$bracelevel++;
} else {
$data .= $_;
die "end of file within quotes started at line $line" if eof $fh;
$_ = <$fh>;
}
}
} elsif (/^\{(.*)/s) {
$_ = $1;
$bracelevel = 1;
$line = $.;
while ($bracelevel) {
if (/^([^{}]*)\}(.*)/s) {
$data .= $1;
$data .= "}" if $bracelevel > 1;
$_ = $2;
$bracelevel--;
} elsif (/^([^{}]*\{)(.*)/s) {
$data .= $1;
$_ = $2;
$bracelevel++;
} else {
$data .= $_;
die "end of file within braces started at line $line" if eof $fh;
$_ = <$fh>;
}
}
} elsif (/^\#/) {
# do nothing
print STDERR "warning: odd concatenation at line $.\n";
} elsif (/^[\},]/) {
print STDERR "no data after field at line $.\n" if $data eq '';
return $data;
} elsif (/^([^\s\},]+)(.*)/s) {
if ($strings->{lc($1)}) {
$data .= $strings->{lc($1)};
} else {
$data .= $1;
}
$_ = $2;
}
# got a single string, check for concatenation
$_ = <$fh> while ((/^\s+$/s || /^\s+%/) && !eof $fh);
s/^\s+//;
if (/^\#(.*)/s) {
$_ = $1;
} else {
return $data;
}
}
}
sub parse_bibtex_entry ($$$$) {
# uses caller's $_
my($fh, $name, $strings, $entries) = @_;
my($entryline) = $.;
$_ = <$fh> while /^\s+$/ && !eof $fh;
if (/^\s*\{(.*)/s) {
$_ = $1;
} else {
print STDERR "no open brace after \@$name starting at line $entryline\n";
return [];
}
# get prototype
my($prototype) = $bibtex_prototypes{$name};
$prototype = $bibtex_prototypes{'_'} if !defined $prototype;
# parse entry into `@v'
my(@v, $a, $b);
while (!eof $fh) {
$_ = <$fh> while /^\s*$/ && !eof $fh;
if (/^\s*\}(.*)/s) {
$_ = $1;
last;
} elsif ($prototype =~ /^k/) {
push @v, parse_bibtex_key($fh);
} elsif ($prototype =~ /^v/) {
push @v, parse_bibtex_value($fh, $strings);
} elsif ($prototype =~ /^p/) {
push @v, parse_bibtex_key($fh);
$_ = <$fh> while /^\s+$/ && !eof $fh;
s/^\s+\=?//;
push @v, parse_bibtex_value($fh, $strings);
}
$_ = <$fh> while /^\s*$/ && !eof $fh;
s/^\s*,?//;
$prototype = substr($prototype, 1)
if $prototype && $prototype !~ /^.\*/;
}
print STDERR "missing args to \@$name at line $.\n"
if $prototype && $prototype !~ /^.\*/;
# do something with entry
if ($name eq 'string') {
$strings->{$v[0]} = $v[1];
} elsif ($name eq 'preamble') {
# do nothing
} else {
my($key) = shift @v;
$entries->{$key} = {@v};
$entries->{$key}->{'_type'} = $name;
$entries->{$key}->{'_key'} = $key;
push @{$entries->{'_'}}, $key;
}
}
sub parse (*;\%) {
my($fh) = qualify_to_ref(shift, caller);
my($initial_strings) = @_;
my($strings) = $initial_strings;
my($curname, $garbage, %entries);
local($_) = '';
while (<$fh>) {
if (/^\s*[%\#]/ || /^\s*$/) {
# comment
} elsif (/^\s*\@([^\s\"\#%\'(),={}]+)(.*)/s) {
$curname = lc($1);
$_ = $2;
parse_bibtex_entry($fh, $curname, $strings, \%entries);
} else {
print STDERR "garbage at line $.\n" if !defined $garbage;
$garbage = 1;
}
}
\%entries;
}
sub expand ($$) {
my($e, $key) = @_;
my(%d) = %{$e->{$key}};
while ($d{'crossref'}) {
my($v) = $d{'crossref'};
delete $d{'crossref'};
%d = (%{$e->{$v}}, %d);
}
\%d;
}
sub split_von ($$$@) {
my($f, $v, $l, @x) = @_;
my(@pre, $t, $in_von, $tt);
while (@x) {
$t = $tt = shift @x;
if ($tt =~ /^\{\\/) {
$tt =~ s/\\[A-Za-z@]+//g;
$tt =~ s/\\.//g;
$tt =~ tr/{}//d;
}
if ($tt =~ /^[a-z]/) {
push @$v, $t;
$in_von = 1;
} elsif ($in_von || !ref($f)) {
push @$l, $t, @x;
return;
} else {
push @$f, $t;
}
}
if (!$in_von) {
push @$l, (pop @$f);
}
}
sub parse_author ($) {
local($_) = $_[0];
my(@x) = ();
my($pos, $pos0, $t, $bracelevel);
$bracelevel = 0;
# move text into @x
while (!/^\s*$/) {
s/^\s+//;
$pos = 0;
while ($pos < length) {
$t = substr($_, $pos, 1);
if ($t eq '{') {
$bracelevel++;
} elsif ($t eq '}') {
$bracelevel--;
} elsif ($bracelevel <= 0) {
last if ($t =~ /[\s,]/);
}
$pos++;
}
push @x, substr($_, 0, $pos);
if ($t eq ',') {
push @x, ',';
$pos++;
}
$_ = substr($_, $pos);
}
# split @x into arrays based on `and'
my(@aa) = ([]);
foreach $t (@x) {
if ($t eq 'and') {
push @aa, [] if @{$aa[-1]} > 0;
} else {
push @{$aa[-1]}, $t;
}
}
# massage each subarray into four parts: first, von, last, jr
my(@aaa) = ();
foreach $t (@aa) {
my(@fvl, @vl, @v, @l, @f, @j, $cur, $commas);
$cur = \@fvl; $commas = 0;
# split into subarrays if possible
foreach $x (@$t) {
if ($x eq ',') {
if ($commas == 0) {
@vl = @fvl;
@fvl = ();
$cur = \@f;
} else {
push @j, @f;
@f = ();
}
$commas++;
} else {
push @$cur, $x;
}
}
# split out the `von' part
if ($commas == 0) {
split_von(\@f, \@v, \@l, @fvl);
} else {
split_von(0, \@v, \@l, @vl);
}
# store as an array of arrays
push @aaa, [[@f], [@v], [@l], [@j]];
}
@aaa;
}
1;
|