summaryrefslogtreecommitdiff
path: root/info/digests/tugboat/biblio/tugboat.awk
blob: 3d4e693acd4d5d5761d498d2e17fb9bbd063bb9f (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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# /u/sy/beebe/tex/tugboat/tugboat.awk, Fri Jul 15 16:05:42 1994
# Edit by Nelson H. F. Beebe <beebe@ceslab04.math.utah.edu>
# Fix capitalization of TUGboat (old `TUGBoat' is wrong)
# /u/sy/beebe/tex/tugboat/tugboat.awk, Mon May 23 12:56:20 1994
# Edit by Nelson H. F. Beebe <beebe@plot79.math.utah.edu>
# Add small changes to deal with revised tb*.cnt files
# /u/sy/beebe/tex/tugboat/tugboat.awk, Mon Oct 11 09:37:36 1993
# Edit by Nelson H. F. Beebe <beebe@plot79.math.utah.edu>
# Fix comment output (bare <newline> -> %%%<newline>), and add
# extra gsub() for citation tag to reduce en-dashes to hyphens
# /usr/tmp/tugboat/tugboat.awk, Sat Mar 20 07:44:13 1993
# Edit by Nelson H. F. Beebe <beebe@sunrise>
# Add triple percent prefix to leading comments.
# /u/sy/beebe/tex/tugboat/tugboat.awk, Wed Jun  7 15:09:44 1989
# Edit by Nelson H.F. Beebe <beebe@plot79.utah.edu>
# Change unknown author to Anonymous.
# /home/csc-sun/u/sy/beebe/tex/tugboat/tmp/tugboat.awk,
# Mon Oct 31 08:09:28 1988
# Edit by Nelson H.F. Beebe (beebe at plot79.utah.edu)
# ======================================================================
# Filter TUGboat tables of contents files (<tex.tugboat>tb*.cnt)
# to produce BibTeX files.  The new version of AWK (matching the AWK
# book) is required:
#
# Usage:
#	nawk -f tugboat.awk tb*.cnt >tugboat.bib
#
# \def's are collected at written separately on the file tugboat.def
# and a @preamble command is output that will read it.
#
# Known deficiencies:
#
# (1) Reference tags are generated from the last word in the first
# author's name, followed by journal, volume, number and page; these are
# therefore unique, but not as mnemonic as tags that included important
# words from the title.  For automatically-generated tags, it is not
# clear that a better solution can be easily implemented.
#
# (2) Page numbers have only the initial page number; the final page
# number cannot be reliably inferred from the starting number of the
# next article, because an article may end on the same page as the start
# of the following one, or there may be a page break, or advertisements
# between them.
#
# (3) special hacks have been introduced to handle odd cases like
# missing author and non-empty title (set author = editor), names with
# Jr and II or III, Spanish accented i (\'\i), names/tags with
# accented letters
#
# Author:
#	Nelson H. F. Beebe
#	Center for Scientific Computing
#	Department of Mathematics
#	University of Utah
#	Salt Lake City, UT 84112
#	USA
#	Tel: +1 801 581 5254
#	FAX: +1 801 581 4148
#	Email: beebe@math.utah.edu
#
# [15-Jul-1994]
# ======================================================================

BEGIN {
	"echo \"$USER at `hostname`\"" | getline username;
	"date" | getline date;
	printf("%%%%%% -*-bibtex-*-\n"); # Emacs file type string
	printf("%%%%%% ========================================================================\n");
	printf("%%%%%% BibTeX database file for TUGboat created automatically with\n");
	printf("%%%%%%\n");
	printf("%%%%%% \t%s -f tugboat.awk\n",ARGV[0]);
	printf("%%%%%%\n");
	printf("%%%%%% by %s on %s\n",username,date);
	printf("%%%%%%\n");
	printf("%%%%%% Input files:\n");
	for (k = 1; k < ARGC; ++k)
		printf("%%%%%% \t%s\n",ARGV[k]);
	printf("%%%%%%\n");
	printf("%%%%%% ========================================================================\n");
	printf("\n@preamble{\"\\input tugboat.def\"}\n\n");
	printf("@string{TUGboat = \"TUGboat\"}\n\n");
}

# Pattern: "\TUBhead 9, 1, April 1988<"

/^[ \t]*\\TUBhead/	{
	line = trim(strip_comments($0));
	gsub(/{\\SMC\\,\\&\\,}/,"+",line);
	gsub(/,/," ",line);	# remove commas
	split(line,d," ");	# extract fields
	volume = d[2];		# and save them
	number = d[3];
	gsub(/[^0-9]/,"",number)
	if (d[4] == "Supplement")
	{
	    month = substr(d[5],1,3)
	    year = substr(d[6],1,index(d[6],"<")-1)
	}
	else
	{
	    month = substr(d[4],1,3)
	    year = substr(d[5],1,index(d[5],"<")-1)
	}
}

# Pattern: "\subsec Author\\Title\\page<" and "\subquery Author\\Title\\page<"

/^[ \t]*\\sub(sec|query) /	{
	line = trim(strip_comments($0));
#	printf("\n\nDEBUG: [%s]\n",line);
#	printf("DEBUG: [%s]\n",substr(line,length(line),1));
	line = trim(strip_comments($0));
	while (substr(line,length(line),1) != "<")
	{
#		printf("DEBUG: [%s]\n",substr(line,length(line),1));
		getline;
		line = line " " trim(strip_comments($0));
	}
#	printf("DEBUG: [%s]\n",line);
	gsub(/~/," ",line);		# discard ties
	gsub(/\\newline|\\break/," ",line); # and forced line breaks
	gsub(/\.\\thinspace/,". ",line); # and \thinspace after initials only
	line = substr(line,index(line," "));	# discard \subxxx
	line = trim(line);
	gsub(/`\\\\/,"`!!",line);	# protect '\\ from split
	split(line,d,"\\\\\\\\");	# extract fields
	gsub(/`!!/,"`\\\\",d[1]);	# restore `\\ strings
	gsub(/`!!/,"`\\\\",d[2]);
	gsub(/`!!/,"`\\\\",d[3]);
#	printf("DEBUG: d[1] = <%s> d[2] = <%s> d[3] = <%s>\n",d[1],d[2],d[3]);

	author = trim(d[1]);
	gsub(/, *and /," and ",author);
	gsub(/,/," and ",author);	# convert to BibTeX form
	gsub(/\\and/,"and",author);
	gsub(/   */," ",author); 	# reduce to single blanks
	author = fix_name(author," and III",", III");
	author = fix_name(author," and II",", II");
	author = fix_name(author," and Jr.",", Jr.");
	author = fix_name(author," and Jr",", Jr");
	author = fix_name(author," and S.J.",", S.J.");
	gsub(/\\'\\i /,"{\\'{\\i}}",author);# fix D\'\i az to D{\'{\i}}az
	author = fix_accents(author);
	author = fix_initials(author);

	title = trim(d[2]);
	if ((author == "{}") || (author == ""))
	{				# lines like this DO occur
		if ((title == "{}") || (title == ""))
			next;
		printf("File %s:line %d: Empty author field [%s] -- using anonymous\n", \
			FILENAME,FNR,line) >> "/dev/tty";
		author = "Anonymous"; # uncertain authorship
	}

	gsub(/   */," ",title); 	# reduce to single blanks
	page = trim(substr(d[3],1,index(d[3],"<")-1));

	# locate first author's last name in [last_blank+1 .. k-1];
	k = index(author," and");
	if (k == 0)
		k = length(author) + 1;
	n = index(author,",");
	if ((k > n) && (n > 0))
 		k = n;	# drop ", Jr" and ", II" in tags
	last_blank = 0;
	for (n = 1; n < k; ++n)
	{
		if (substr(author,n,1) == " ")
			last_blank = n;
	}
	tag = substr(author,last_blank+1,k-last_blank-1) \
		":TB" volume "-" number
	if (page != "")
	{
		split(page,page_numbers,"-")
		tag = tag "-" page_numbers[1]
	}
	gsub(/ /,"-",tag); 		# change blanks to hyphen
        gsub(/\\[bcdHuvg]{/,"{",tag) # remove accent commands containing letters
	gsub(/[^-+a-zA-Z0-9:]/,"",tag);	# and strip all illegal chars
	gsub(/--/,"-",tag)	# and reduce en-dashes to hyphens

	# There are instances of same author on single page with
	# intervening articles, so we cannot just keep track of the
	# previous tag.  Instead, we use awk's wonderful associative
	# arrays, and the fact that uninitialized values are guaranteed
	# to be 0.
	if (tag_use_count[tag])
	{	# multiple articles by same author on one page
		tag_use_count[tag]++;
		tag = tag "-" tag_use_count[tag];
	}
	else
	{
		tag_use_count[tag] = 1;
	}
	printf("@Article{%s,\n",tag);
	last_tag = tag;
	printf("  author =\t\"%s\",\n",author);

	# NB: the formatting of the title must be done with care.  Two
	# levels of braces are needed.  The outer one protects against
	# letter case conversion, and the inner one removes the
	# possibility of BibTeX interpreting a leading macro name as a
	# `special character', in which case it will do letter case
	# conversion inside the braces anyway.  If the title is empty,
	# we omit the braces, because BibTeX would then view it as
	# non-empty, and would format the bibliography entry
	# incorrectly.
	if (length(title) > 0)
		printf("  title =\t\"{{%s}}\",\n",title);
	else
		printf("  title =\t\"\",\n",title);

	printf("  journal =\tTUGboat,\n");
	printf("  year =\t\"%s\",\n",year);
	printf("  volume =\t\"%s\",\n",volume);
	printf("  number =\t\"%s\",\n",number);
	if (page != "")
		printf("  pages =\t\"%s\",\n",page);
	printf("  month =\t%s,\n",month);
	printf("}\n\n");
}

/^[ \t]*\\def\\/	{
	print $0 >>"tugboat.def"
	bc = brace_count($0,0)
	while (bc > 0)		# collect definition until closing brace found
	{
	    getline;
	    print $0 >>"tugboat.def"
	    bc = brace_count($0,bc)
	}
}

END {}
#=======================================================================
# Utility functions.  NB: awk has no declarations, so to get private
# locals in functions, we declare additional arguments separated by
# space from the main argument(s) that are not supplied when we call
# the function.  If this isn't done, the variables used will be global
# ones, with likely disasterous effects.
#=======================================================================

function brace_count(s,nb_init, k,nb)
{
    # return the brace level in s, starting with an initial level nb_init,
    # and ignoring TeX comments and backslashed braces
    
    nb = nb_init
    for (k = 1; k <= length(s); ++k)
    {
	if (substr(s,k,1) == "{")
	{
	    if (substr(s,k-1,1) != "\\") nb++
	}
	else if (substr(s,k,1) == "}")
	{
	    if (substr(s,k-1,1) != "\\") nb--
	}
	else if ((substr(s,k,1) == "%") && (substr(s,k-1,1) != "\\"))
	    break
    }
#   print "DEBUG: s = [" s "] nbinit = " nbinit " nb = " nb
    return (nb)
}



function fix_accents(t, k,s)	# convert \"x to {\"{x}} for " = ", `, '
{
#	Alas, awk does not allow the replacement pattern to specify
#	\1, \2, etc to identify groups matched by a regular expression,
#	so we handle them manually.

	s = t;

	gsub(/\\'\\i/,"{\\'\\i}",s); # as in Ji\v{r}\'\i{}
	gsub(/\\o( |$)/,"{\\o}",s);	# protect accents
	gsub(/\\oe( |$)/,"{\\oe}",s);
	gsub(/\\ae( |$)/,"{\\ae}",s);
	gsub(/\\aa( |$)/,"{\\aa}",s);
	gsub(/\\ss( |$)/,"{\\ss}",s);
	gsub(/\\l( |$)/,"{\\l}",s);
	gsub(/\\O( |$)/,"{\\O}",s);	# protect accents
	gsub(/\\OE( |$)/,"{\\OE}",s);
	gsub(/\\AE( |$)/,"{\\AE}",s);
	gsub(/\\AA( |$)/,"{\\AA}",s);
	gsub(/\\L( |$)/,"{\\L}",s);

	for (k = 1; k < length(s); ++k)
	{
		if ( ( (substr(s,k,2) == "\\\"") ||
		      (substr(s,k,2) == "\\`") ||
		      (substr(s,k,2) == "\\'") ) &&
		     (substr(s,k+2,1) != "{") )
		{
			t = substr(s,1,k-1);
			t = t "{";
			t = t substr(s,k,2);
			t = t "{";
			t = t substr(s,k+2,1);
			t = t "}}";
			t = t substr(s,k+3);
			s = t;
		}
	}
	gsub(/{{\\'{\\}}i}{}/,"{\\'\\i}",s); # Ji\v{r}{{\'{\}}i}{} -> Ji\v{r}{\'\i}
	gsub(/\\v r/,"\\v{r}",s); # Ji\v r{\'\i} -> Ji\v{r}{\'\i}

	gsub(/\\v{s}/,"{\\v{s}}",s) # Ol\v{s}{\'{a}}k -> Ol{\v{s}}{\'{a}}k

	return (s);
}

function fix_initials(s, k,t)	# change "P.D.Q. Bach" to "P. D. Q. Back"
{
    t = s;
    for (k = 0; k < length(t); ++k)
    {
	if (isupper(substr(t,k,1)) && (substr(t,k+1,1) == ".") &&
	    isupper(substr(t,k+2,1)))
	    t = substr(t,1,k+1) " " substr(t,k+2);
    }
    return (t);
}

# replace old by new, and brace it with preceding last name
function fix_name(s,old,new, k,t)
{
    k = index(s,old);
    if (k == 0)
	return (s);
    t = substr(s,1,k-1) new "}" substr(s,k+length(old));
    for (; (k > 0) && (substr(t,k,1) != " ");--k)
        ;
    t = substr(t,1,k) "{" substr(t,k+1);
    return (t);
}

function isalpha(t)	# return 1 if t is alphabetic, 0 otherwise
{
    if (index("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",t) > 0)
	return 1;
    else
	return 0;
}

function islower(t)	# return 1 if t is lowercase, 0 otherwise
{
    if (index("abcdefghijklmnopqrstuvwxyz",t) > 0)
	return 1;
    else
	return 0;
}

function isupper(t)	# return 1 if t is uppercase, 0 otherwise
{
    if (index("ABCDEFGHIJKLMNOPQRSTUVWXYZ",t) > 0)
	return 1;
    else
	return 0;
}

# NB: The function, protect(), is no longer used, because an extra
# pair of braces around the title protects the entire string from case
# conversion.  However, there may be other circumstances in which it
# is useful, so I'm preserving it here.
#
# Brace upper-case letters for BibTeX protection, but handle macros
# specially.  Macros with no upper-case letters are preserved intact
# (so {\tt foo} works), but macros with mixed case are preserved, so
# \TeX{} becomes {\TeX}{}.   This means that there had better not be
# any macros containing uppercase letters that need arguments.  It
# also suggests that BibTeX should be revised to leave macros alone;
# currently they get case-twiddled.

function protect(t, k,n,s,anyuc)
{
    s = t;
    for (k = 1; k <= length(s); ++k)
    {
	if (isupper(substr(s,k,1)))
	{	# brace consecutive upper-case substring
	    for (n = k+1;\
		 (n <= length(s)) && isupper(substr(s,n,1)); ++n)\
		 ;  # substring k..n-1 is upper-case
	    t = substr(s,1,k-1);
	    t = t "{" substr(s,k,n-k) "}";
	    k = length(t);
	    t = t substr(s,n);
	    s = t;
	}
	else if (substr(s,k,1) == "\\")
	{	# TeX control sequence
	    if (!isalpha(substr(s,k+1,1)))
		n = k+2; # non-alpha control sequence
	    else    # \macro name
	    {
		anyuc = 0;
		for (n = k+1; n <= length(s); ++n)
		{
		    if (!isalpha(substr(s,n,1)))
			break;
		    anyuc = anyuc || isupper(substr(s,n,1));
		}
	    }
#	    printf("DEBUG: %d..%d [%s]\n",k,n,s);
#	    printf("DEBUG: %s{%s}%s\n", \
#		substr(s,1,k-1), substr(s,k,n-k), substr(s,n));
	    if (k > 1)
		t = substr(s,1,k-1);
	    else
		t = "";
	    if (anyuc)
		t = t "{";
	    t = t substr(s,k,n-k);
	    if (anyuc)
		t = t "}";
	    k = length(t);
	    t = t substr(s,n);
	    s = t;
#	    printf("DEBUG:  ----- [%s]\n",s);
	}
    }
    return (s);
}

function strip_comments(s,  k,n,t) # delete trailing comments
{		# recognize \% as not starting a comment
    t = s;
    for (k = 1; k <= length(t); ++k)
    {
	n = k - 1 + index(substr(t,k),"%");
	if (n < k)  # then no comments in t
	    return (t);
	if (substr(t,n-3,3) != "[^\\]\\\\")
	{
#	    printf("  DBG: [%s] -> [%s]\n",s,substr(t,1,n-1));
	    return (substr(t,1,n-1));
	}
	else if (substr(t,n-1,1) != "\\")
	{
#	    printf("  DBG: [%s] -> [%s]\n",s,substr(t,1,n-1));
	    return (substr(t,1,n-1));
	}

	k = n;
    }
#   printf("  DBG: [%s] -> [%s]\n",s,t);
    return (t);
}


function trim(t, s) # trim leading and trailing whitespace, return s
{
    s = t;
    gsub(/^[ \t]*/,"",s);   # trim leading whitespace
    gsub(/[ \t]*$/,"",s);   # trim trailing whitespace
    return (s);
}