-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathads-make-keys
More file actions
executable file
·324 lines (324 loc) · 12.1 KB
/
ads-make-keys
File metadata and controls
executable file
·324 lines (324 loc) · 12.1 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/perl
########################################################################
# Simple perl script to create default format keys for BibTeX entries.
########################################################################
# Create input and output file names -> prefix.ads and prefix.bib
########################################################################
$_ = $ARGV[0];
$ext = ".ads";
if (/$ext$/) {
$last = rindex($_,$ext);
$prefix = substr($_,0,$last);
}
else {
$prefix = $_;
}
$bibfile = $prefix . ".ads";
$outfile = $prefix . ".bib";
$extrafile = $prefix. ".nonads";
########################################################################
# See if we can read from and write to the respective files.
########################################################################
open(INFILE_HANDLE,"<".$bibfile) ||
die "\nCan't open file $bibfile for reading.\n\n";
open(OUTFILE_HANDLE,">".$outfile) ||
die "\nCan't open file $outfile for writing.\n\n";
########################################################################
# Look for @ which falls at start of each BibTeX entry.
# ADS puts year of publication at start of their key, e.g.
# @ARTICLE{1979SciN..115..371.,
# Extract the year and first authors last name to create a new key, in
# the format "first author last name":"year", e.g. Strobel:1998
########################################################################
# Initialize variables.
########################################################################
$TRUE = 1;
$FALSE = 0;
$in_entry = $FALSE;
$in_author = $FALSE;
$in_journal = $FALSE;
$in_title = $FALSE;
$publishstr = ' ';
$editorstr = ' ';
$addressstr = ' ';
$adsnotestr = ' ';
$schoolstr = ' ';
$journalstr = ' ';
@journalext = (' ',' ',' ',' ',' ');
$njourext = 0;
$authorstr = ' ';
@authorext = (' ',' ',' ',' ',' ',' ',' ');
$nauthext = 0;
$titlestr = ' ';
@titleext = (' ',' ',' ',' ',' ');
$ntitlext = 0;
$volumestr = ' ';
$pagesstr = ' ';
$monthstr = ' ';
$yearstr = ' ';
$urlstr = ' ';
$title = ' ';
$year = ' ';
$first = ' ';
$keycount = 0;
########################################################################
# Read from input file until we reach the end.
########################################################################
while (<INFILE_HANDLE>) {
########################################################################
########################################################################
if (/^\s*@/) { # Start of entry - Extract entry type
($type[$keycount],$rest) = split(/{/,$_);
$in_entry = $TRUE;
$in_author = $FALSE;
$in_title = $FALSE;
$in_journal = $FALSE;
########################################################################
########################################################################
}
elsif (/^\s*}/) { # End of entry - print it out
$in_entry = $FALSE;
$in_author = $FALSE;
$in_title = $FALSE;
$in_journal = $FALSE;
$entry[$keycount] = "";
if ($first eq ' ') {
# gsn - no colons
$keyval[$keycount] = "$title:$year:$keycount";
# $keyval[$keycount] = "$title$year$keycount";
}
########################################################################
########################################################################
else {
# gsn - no colons
$keyval[$keycount] = "$first:$year:$keycount";
#$keyval[$keycount] = "$first$year$keycount";
$entry[$keycount] = join("",$entry[$keycount],"$authorstr");
for ($i=0;$i<$nauthext;$i++) {
$entry[$keycount] = join("",$entry[$keycount],"$authorext[$i]");
}
}
if ($titlestr ne ' ') {
$entry[$keycount] = join("",$entry[$keycount],"$titlestr");
}
for ($i=0;$i<$ntitlext;$i++) {
$entry[$keycount] = join("",$entry[$keycount],"$titleext[$i]");
}
if ($schoolstr ne ' ') {
$entry[$keycount] = join("",$entry[$keycount],"$schoolstr");
}
if ($journalstr ne ' ') {
$entry[$keycount] = join("",$entry[$keycount],"$journalstr");
}
for ($i=0;$i<$njourext;$i++) {
$entry[$keycount] = join("",$entry[$keycount],"$journalext[$i]");
}
if ($yearstr ne ' ') {
$entry[$keycount] = join("",$entry[$keycount],"$yearstr");
}
if ($monthstr ne ' ') {
$entry[$keycount] = join("",$entry[$keycount],"$monthstr");
}
if ($volumestr ne ' ') {
$entry[$keycount] = join("",$entry[$keycount],"$volumestr");
}
if ($publishstr ne ' ') {
$entry[$keycount] = join("",$entry[$keycount],"$publishstr");
}
if ($editorstr ne ' ') {
$entry[$keycount] = join("",$entry[$keycount],"$editorstr");
}
if ($addressstr ne ' ') {
$entry[$keycount] = join("",$entry[$keycount],"$addressstr");
}
if ($pagesstr ne ' ') {
$entry[$keycount] = join("",$entry[$keycount],"$pagesstr");
}
# GSN - kill the freaking url field
#if ($urlstr ne ' ') {
# $entry[$keycount] = join("",$entry[$keycount],"$urlstr");
#}
$keycount++;
$publishstr = ' ';
$editorstr = ' ';
$addressstr = ' ';
$adsnotestr = ' ';
$schoolstr = ' ';
$volumestr = ' ';
$pagesstr = ' ';
$monthstr = ' ';
$yearstr = ' ';
$urlstr = ' ';
$title = ' ';
$year = ' ';
$first = ' ';
$journalstr = ' ';
@journalext = (' ',' ',' ',' ',' ');
$njourext = 0;
$authorstr = ' ';
@authorext = (' ',' ',' ',' ',' ',' ',' ');
$nauthext = 0;
$titlestr = ' ';
@titleext = (' ',' ',' ',' ',' ');
$ntitlext = 0;
}
########################################################################
# Match keywords to the appropriate strings.
########################################################################
if (/^\s*booktitle/) {
$journalstr=$_;$journalstr =~ s/ +/ $2/g;
$in_title=$FALSE;$in_author=$FALSE;$in_journal = $TRUE;
}
if (/^\s*journal/ ) {
$journalstr=$_;$journalstr =~ s/ +/ $2/g;
$in_title=$FALSE;$in_author=$FALSE;$in_journal = $TRUE;
}
if (/^\s*publisher/ ) {
$publishstr=$_;$publishstr =~ s/ +/ $2/g;
$in_title=$FALSE;$in_author=$FALSE;$in_journal = $FALSE;
}
if (/^\s*editor/ ) {
$editorstr=$_;$editorstr =~ s/ +/ $2/g;
$in_title=$FALSE;$in_author=$FALSE;$in_journal = $FALSE;
}
if (/^\s*address/ ) {
$addressstr=$_;$addressstr =~ s/ +/ $2/g;
$in_title=$FALSE;$in_author=$FALSE;$in_journal = $FALSE;
}
if (/^\s*adsnote/ ) {
$adsnotestr=$_;$adsnotestr =~ s/ +/ $2/g;
$in_title=$FALSE;$in_author=$FALSE;$in_journal = $FALSE;
}
if (/^\s*volume/ ) {
$volumestr =$_;$volumestr =~ s/ +/ $2/g;
$in_title=$FALSE;$in_author=$FALSE;$in_journal = $FALSE;
}
if (/^\s*school/ ) {
$schoolstr =$_;$schoolstr =~ s/ +/ $2/g;
$in_title=$FALSE;$in_author=$FALSE;$in_journal = $FALSE;
}
if (/^\s*pages/ ) {
$pagesstr =$_;$pagesstr =~ s/ +/ $2/g;
$in_title=$FALSE;$in_author=$FALSE;$in_journal = $FALSE;
}
if (/^\s*month/ ) {
$monthstr =$_;$monthstr =~ s/ +/ $2/g;
$in_title=$FALSE;$in_author=$FALSE;$in_journal = $FALSE;
}
if (/^\s*url/ ) {
$urlstr =$_;$urlstr =~ s/ +/ $2/g;
$in_title=$FALSE;$in_author=$FALSE;$in_journal = $FALSE;
}
########################################################################
# Process keywords which are used to construct the BibTeX keys.
# Extract year of publication
########################################################################
if (/^\s*year/) {
$in_title = $FALSE;
$in_author = $FALSE;
$in_journal = $FALSE;
$yearstr = $_;
$yearstr =~ s/( +)/ $2/g;
($junk,$year,$rest)=split(/=/,$yearstr); # get stuff to right of =
$year =~ s/,//g; # strip commas
for ($year) {s/^\s+//; s/\s+$//;} # strip white space at ends
# Gsn- get rid of 19-- and 20--
$year =~ s/^19//;
$year =~ s/^20//;
}
########################################################################
# Extract first author last name
########################################################################
elsif (/^\s*author/) {
$authorstr = $_;
$authorstr =~ s/( +)/ $2/g;
$in_author = $TRUE;
$in_title = $FALSE;
$in_journal = $FALSE;
($junk,$rest)=split(/=/,$authorstr); # get stuff to right of =
($first,$rest)=split(/\}/,$rest); # get stuff to left of }
$first =~ s/\{//g; # strip outer brackets
$first =~ s/\"//g; # strip outer quotes
for ($first) { # strip all white space
s/\s+//g;
}
$first =~ tr/A-Z/a-z/; # GSN - everyone's lower case
}
########################################################################
# Extract title for use in key when no author info is given.
########################################################################
elsif (/^\s*title/) {
$titlestr = $_;
$titlestr =~ s/ +/ $2/g;
$in_title = $TRUE;
$in_author = $FALSE;
$in_journal = $FALSE;
($junk,$rest)=split(/=/,$titlestr); # get stuff to right of =
($title,$rest)=split(/}/,$rest); # get stuff to left of }
$title =~ s/{//g; # strip outer brackets
for ($title) {s/\s+//g;} # strip all white space
$title =~ s/,//g; # strip commas
$title =~ s/.//g; # strip periods
}
elsif ($in_entry && $in_author && !/\s+=/ && !/^$/){#Additional author entry
$authorext[$nauthext++] = $_;
}
elsif ($in_entry && $in_title && !/\s+=/ && !/^$/) {#Additional title entry
$titleext[$ntitlext++] = $_;
}
elsif ($in_entry && $in_journal && !/\s+=/ && !/^$/) {#Additional jnl entry
$journalext[$njourext++] = $_;
}
}
########################################################################
# Sort keys, adding a,b,c, ... as necessary to ensure uniqueness
########################################################################
@keyval = sort @keyval;
@keyauth = @keyval;
@keyyear = @keyval;
@keypos = @keyval;
@keyextn = @keyval;
for ($i=0;$i<$keycount;$i++) {
($keyauth[$i],$keyyear[$i],$keypos[$i]) = split(/:/,$keyval[$i]);
$keyextn[$i] = "";
}
@extn = ('a','b','c','d','e','f','g','h','i');
$eind = 0;
for ($i=0;$i<$keycount-1;$i++) {
if (($keyauth[$i] eq $keyauth[$i+1]) &&
($keyyear[$i] eq $keyyear[$i+1])) {
$keyextn[$i] = $extn[$eind++];
$keyextn[$i+1] = $extn[$eind];
print STDOUT "Identical Keys - Modifying ...\n";
print STDOUT $keyauth[$i],":",$keyyear[$i],$keyextn[$i],"\n";
print STDOUT $keyauth[$i+1],":",$keyyear[$i+1],$keyextn[$i+1],"\n";
}
else {$eind = 0}
}
########################################################################
# Print out sorted keys and associated BIBTEX entries.
########################################################################
for ($i=0;$i<$keycount;$i++) {
print OUTFILE_HANDLE "$type[$keypos[$i]]\{$keyauth[$i]:";
print OUTFILE_HANDLE "$keyyear[$i]$keyextn[$i],\n";
print OUTFILE_HANDLE "$entry[$keypos[$i]]";
print OUTFILE_HANDLE "\}\n\n";
}
$isextrafile=1;
open(EXTRAFILE_HANDLE,"<".$extrafile) ||
($isextrafile = 0);
print $isextrafile."\n";
if ($isextrafile==1) {
while (<EXTRAFILE_HANDLE>) {
print OUTFILE_HANDLE;
}
}
########################################################################
# Clean Up and Exit
########################################################################
close(INFILE_HANDLE);
close(OUTFILE_HANDLE);
close(EXTRAFILE_HANDLE);
print STDOUT "\n Default BibTeX keys created for file $outfile\n\n";
########################################################################
########################################################################