-
Notifications
You must be signed in to change notification settings - Fork 9
/
bib-abbreviate.pl
executable file
·162 lines (146 loc) · 5.1 KB
/
bib-abbreviate.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/perl -wps
# bib-abbreviate: Maintain parallel bibliography abbreviation (bibstring) files
# Michael Ernst <[email protected]>
# Sometimes, I want a verbose bibliography; other times I want a terser
# one (usually so it doesn't take up so much space against a page limit).
# This program maintains two sets of bibliography abbreviation files, which
# lets you easily switch between the two versions.
# Invoke like:
# bib-abbreviate bibstring-master.bib > bibstring-unabbrev.bib
# bib-abbreviate -abbrev bibstring-master.bib > bibstring-abbrev.bib
# Then you can switch among the two versions in LaTeX by choosing between
# \bibliography{bibstring-abbrev,inv-icse}
# \bibliography{bibstring-unabbrev,inv-icse}
# This program converts lines like this in the bibstring-master.bib file:
# @string{name = unabbrev = abbrev}
# into either
# @string{name = unabbrev}
# or
# @string{name = abbrev}
# depending on whether the "-abbrev" argument is supplied to bib-abbreviate.
#
# For example, here are some lines from my bibstring-master.bib file (which
# is also distributed with this program):
#
# @string{ICSE99 = "Proceedings of the 21st International Conference on
# Software Engineering" = "ICSE"}
# @string{ICSE99addr = "Los Angeles, CA, USA" = ""}
# @string{ICSE99date = may # "~19--21," = may}
# I maintain my bibstring-unabbrev.bib and bibstring-abbrev.bib with a
# Makefile that contains the following entries. The Emacs code at the end
# of bibstring-master.bib causes "make" to be run every time that the file
# is saved, so I never need to run "make" manually.
#
# all: bibstring-unabbrev.bib bibstring-abbrev.bib
#
# bibstring-unabbrev.bib: bibstring-master.bib
# rm -f $@
# bib-abbreviate $< > $@
# chmod -w $@
#
# bibstring-abbrev.bib: bibstring-master.bib
# rm -f $@
# bib-abbreviate -abbrev $< > $@
# chmod -w $@
# Another way to save space in your bibliography is to reduce the
# indentation of bibliography entries (by default, it is the width of the
# longest bibliography label) and to use a smaller font for the bibliography.
# The following TeX code does both things.
#
# %% Make bibliography items less indented, to save space; also use small font.
# \makeatletter
# \renewenvironment{thebibliography}[1]
# {\section*{\refname
# \@mkboth{\MakeUppercase\refname}{\MakeUppercase\refname}}%
# \vspace{5pt}
# \begingroup
# % \begin{footnotesize}
# \begin{small}
# \list{\@biblabel{\@arabic\c@enumiv}}%
# {%\settowidth\labelwidth{\@biblabel{#1}}%
# \settowidth\labelwidth{~}%
# % \itemsep 0pt \parskip 0pt
# \itemsep 0pt \parskip -2pt
# \leftmargin\labelwidth
# \advance\leftmargin\labelsep
# \@openbib@code
# \usecounter{enumiv}%
# \let\p@enumiv\@empty
# \renewcommand\theenumiv{\@arabic\c@enumiv}}%
# \sloppy\clubpenalty4000\widowpenalty4000%
# \sfcode`\.\@m}
# {\def\@noitemerr
# {\@latex@warning{Empty `thebibliography' environment}}%
# \endlist
# % \end{footnotesize}
# \end{small}
# \endgroup}
# \makeatother
###########################################################################
BEGIN {
$debug = 1;
$debug = 0; # to debug, comment out this line
}
# "$line" is the line that has been read so far. (??)
# Gratuitous use to avoid Perl warning.
# This variable is set by supplying "-abbrev" on the command line.
if (!defined($abbrev))
{ $abbrev = 0; }
if (defined($line) && ($_ =~ /^\@string\{/)) {
print $line;
$line = "";
}
if (defined($line) && (($_ =~ /^$/) || ($_ =~ /^@/))) {
print $line;
$line = "";
}
if (defined($line) && ($line ne "")) {
if ($debug) {
print "appending to: $line";
print "appendee: $_";
}
$line .= $_;
$_ = "";
} elsif (/^\@string\{/i) {
$line = $_;
$_ = "";
}
if ($debug && defined($line)) {
print STDERR "line: $line";
}
if (defined($line)) {
if ($line =~ /^\@string\{
([-_a-z0-9A-Z]+\s*=\s*)
# A string without embedded quotes: \"[^\"]+\"
# A string with embedded quotes (ignores possible errors if string
# ends with "\\"; assume that doesn't happen):
# An entry chunk is an abbrev or string: (?:[-_a-z0-9A-Z]+|\"(?:[^\"]|\\\")+\").
# Permit any number of them, separated by " # ".
((?:[-_a-z0-9A-Z]+|\"(?:[^\"]|\\\")+\")
(?:\s*\#\s*(?:[-_a-z0-9A-Z]+|\"(?:[^\"]|\\\")+\"))*)
# An empty string "" is permitted in the main part of this regexp.
(\s*=\s*
((?:[-_a-z0-9A-Z]+|\"(?:[^\"]|\\\")*\")
(?:\s*\#\s*(?:[-_a-z0-9A-Z]+|\"(?:[^\"]|\\\")+\"))*)
)?
\s*\}\s*$/ix) {
if ($debug) { print STDERR "match: $line"; }
my $name = $1;
my $long = $2;
my $short = $4;
if (defined($names{$name})) {
print STDERR "WARNING: Duplicate in bibliography abbreviation file: $name\n";
}
$names{$name} = 1;
if ($abbrev and defined($short)) {
$text = $short;
} else {
$text = $long;
}
$line = "";
print "\@string{$name$text}\n";
$_ = "";
} else {
if ($debug) { print STDERR "no match: $line"; }
}
}