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
|
#!/usr/bin/perl
use v5.12;
use warnings;
# Default configuration
my %fmt;
$fmt{f} = '(%A %y)';
$fmt{x} = '(%y)';
$fmt{d} = '%a (%y). {\fI%t\fR}. {\*Q%q\*U}. %n, %d. %c: %p. {Available: %w.}';
# Internal variables
my $author_last = 1; # whether the previous reference field was Ra
my @lines;
my @refs;
# First pass (process formats)
while (<>) {
chomp;
# Format
if (/^\.F([fdx])(\\?) +(.*)/) {
my ($type, $join, $rest) = ($1, $2, $3);
$rest = "\n$rest" if not $join;
while (not eof()) {
last if not $rest =~ s/\\$//;
$rest .= "\n" . <>;
chomp $rest;
}
$rest .= "\n";
$fmt{$type} = $rest;
next;
}
push @lines, $_;
}
# Second pass (process reference definitions and inline references)
my $inlref = 0;
my @r = ();
my $refblock = 0;
my $refi = 0;
for (my $i = $#lines; $i >= 0; $i--) {
$_ = $lines[$i];
chomp;
# Definition
if (/^\.R([acdnpqtwy]) +(.*)/) {
my ($fld, $rest) = ($1, $2);
$refi++ if not $refblock; # new reference
$refblock = 1;
if ($fld eq 'a') {
push @{$refs[$refi]{a}}, $rest;
} else {
$refs[$refi]{$fld} = $rest;
}
next;
} elsif ($refblock) {
# print formatted reference definition
push @r, fmt($fmt{d}, $refi) . "\n";
}
# Inline reference
if (/^\.R([fx]) +(.*)/) {
my ($fld, $def) = ($1, $2, $3);
$inlref = 1;
# find potential prefix/suffix
my ($suffix, $prefix);
if ($def =~ s/ ([.,?!:;\])]) ?([\[(])?$//) {
($suffix, $prefix) = ($1, $2);
}
my @words = split /\s/, $def;
# replace '' with last word on preceding line
for (@words) {
if ($_ eq "''") {
$_ = $lines[$i-1];
chomp;
$_ =~ s/^.*\s(\S+)\s*$/$1/;
}
}
# find matching reference list entry
my $winner = 0;
my @points;
for (my $i = 0; $i < scalar @refs; $i++) {
$points[$i] = 0 if not defined $points[$i];
$points[$i] += 100 if likeness($refs[$i]{a}, @words);
$points[$i] += 50 if likeness($refs[$i]{y}, @words);
$points[$i] += 1 * likeness($refs[$i]{t}, @words);
$points[$i] += 1 * likeness($refs[$i]{q}, @words);
$winner = $i if $points[$i] > $points[$winner];
}
if (not defined $winner) {
print STDERR "Error: Reference '$def' could not be resolved.\n";
exit 1;
}
if ($points[$winner] < 150) {
print STDERR "Warning: Guessing that reference '$def' refers to " . fmt($fmt{f}, $winner) . " (match = $points[$winner]).\n";
}
# format reference
my $fmt;
if ($fld eq 'f') {
$fmt = fmt($fmt{f}, $winner);
} else {
$fmt = fmt($fmt{x}, $winner);
}
# print formatted reference
push @r, "\n" if $fmt =~ s/^\n//;
my $n = index($fmt, "\n");
$prefix = '' if not $prefix;
$suffix = '' if not $suffix;
if ($n != -1) {
push @r, substr($fmt, index($fmt, "\n"));
push @r, $prefix . substr($fmt, 0, index($fmt, "\n")) . "$suffix";
} else {
push @r, "$prefix$fmt$suffix";
}
next;
}
# non-rf line
if (!$inlref) {
push @r, "\n";
}
push @r, $_;
$refblock = 0;
$inlref = 0;
}
# Third pass (print the result)
for (my $i = $#r; $i >= 0; $i--) {
print $r[$i];
}
# Format reference at $i after $fmt
sub fmt {
my ($fmt, $i) = @_;
my %ref = %{$refs[$i]};
for my $fld (split //, 'Aacdnpqtwy') {
if ($ref{lc $fld}) {
no warnings;
my $val = $ref{lc $fld};
$val = join '; ', @$val if lc $fld eq 'a';
$val = fmtl($val) if $fld eq 'A';
$fmt =~ s/\{([^{}%]*)%\Q$fld\E([^{}]*)}/$1$val$2/g;
$fmt =~ s/%\Q$fld\E/$val/g;
} else {
no warnings;
$fmt =~ s/[.([]?\{([^{}%]*)%\Q$fld\E([^{}]*)}[.,?!:;\])]?//g;
$fmt =~ s/[.([]?%\Q$fld\E[.,?!:;\])]?//g;
}
}
$fmt =~ s/ +/ /g;
#$fmt =~ s/([(]) +/$1/;
#$fmt =~ s/ +([.,)])/$1/;
$fmt =~ s/^ *//;
$fmt =~ s/ *$//;
return $fmt;
}
# Remove forenames from a given string of authors
sub fmtl {
my ($a) = @_;
my $r;
for my $name (split /; /, $a) {
if ($name =~ /(.*?),/) {
$r .= ", $1";
} else {
$name =~ s/(.*?)\s/$1/;
return ", $name";
}
}
$r =~ s/^, //;
return $r;
}
# Calculate a basic likeness value a string and a list of strings
sub likeness {
my ($string, @strings) = @_;
my $r = 0;
return 0 if not defined $string;
for (@strings) {
$r += 1 if index($string, $_) != -1;
}
return $r;
}
|