-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbib-fix.pl
executable file
·80 lines (67 loc) · 1.5 KB
/
bib-fix.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
#!/usr/bin/perl -w
use strict;
# open(INPUT,"refs.bib");
my($bib_head, $bib_body, $bib_isbn, $bib_url, $bib_year, $bib_urldate);
sub reset_bib {
$bib_body = $bib_head = $bib_url = $bib_isbn = $bib_year = $bib_urldate = "";
}
reset_bib();
while(defined(my $line=<>)) {
chomp($line);
if($line =~ /^@/){
$line =~ s/,\s*//g;
$bib_head = $line;
$bib_body = $bib_head;
next;
}
if($bib_head eq "") { next; }
if($line =~ /^\s*year = \s*{(.*)}/) {
$bib_year = $1;
next;
}
if($line =~ /^\s*urldate = \s*{(.*)}/) {
$bib_urldate = $1;
$bib_urldate =~ s/-.*//g;
next;
}
if($line =~/^}$/) {
print $bib_body;
my $bib_suffix = "";
# note (URLS + ISBN)
my $bib_note = $bib_url;
if($bib_isbn ne "") {
if($bib_note ne "") {
$bib_note .= ". ".$bib_isbn;
} else {
$bib_note = $bib_isbn;
}
}
if($bib_note ne "") {$bib_suffix = " note = {$bib_note},\n"; }
# year
if($bib_year eq "") { $bib_year = $bib_urldate; }
if($bib_year ne "") { $bib_suffix .= " year = {$bib_year}\n"; }
if($bib_suffix ne "") {
print ",\n".$bib_suffix;
}
print "}\n";
reset_bib();
next;
}
if($line =~ /^\s*note = /) {
if(!(($line =~ /URL~/) or ($line =~ /ISBN~/))) {
next;
}
}
if($line =~ /^\s*url = \s*{(.*)}/) {
$bib_url = $1;
$bib_url = "URL~: \\url{$bib_url}";
# next;
}
if($line =~ /^\s*isbn = \s*{(.*)}/) {
$bib_isbn = $1;
$bib_isbn = "ISBN~: $bib_isbn";
# next;
}
$line =~ s/\s*,\s*$//g;
$bib_body .= ",\n".$line;
}