Debian Codegraph @

texk/texlive/linked_scripts/multibibliography/multibibliography.pl

raw
1 #!/usr/bin/perl
2 # Usage: perl multibibliography.pl jobname
3 # By Yannis Haralambous and Michael Cohen
4
5 # Changes by Boris Veytsman, $Revision: 1.6 $, $Date: 2013-06-25 03:37:30 $
6
7 # This perl script does the bibtexing part, generating 3 separate .bbl
8 # files with descriptive names derived from source file name, so no
9 # need to separately invoke.
10
11 use strict;
12
13 sub cleanup {
14 local $/=undef;
15 my $file = shift;
16 open IN, $file;
17 my $BIG=<IN>;
18 close IN;
19 $BIG=~s/(\r\n|\r|\n)/ /g;
20 $BIG=~s/\\bibitem/\n\n\\bibitem/g;
21 $BIG=~s/\\newblock/\n\\newblock/g;
22 $BIG=~s/\\end\{thebibliography\}/\n\n\\end{thebibliography}/g;
23 $BIG=~s/[ ]+/ /g;
24 open OUT, ">$file";
25 print OUT $BIG;
26 close OUT;
27 }
28
29 # First, change "apalike" in the aux file to "unsrt" and generate bbl
30 # file, renamed to "fn-sequence.bbl".
31 my $filename=$ARGV[0];
32 $filename=~s/\.(tex|bib|bbl|aux|log)$//;
33 if (-e $filename.".aux") {
34 open IN, $filename.".aux";
35 my $BIG="";
36 while (<IN>) {
37 if (m/\\bibstyle\{apalike\}/) {
38 $BIG .= "\\bibstyle{unsrt}\n";
39 } else {
40 $BIG .= $_;
41 }
42 }
43 close IN;
44 open OUT, "> $filename-sequence.aux";
45 print OUT $BIG;
46 close OUT;
47 system("bibtex $filename-sequence");
48
49 # Now, change "unsrt" in the aux file to "chronological"
50 # and generate bbl file, renamed to "fn-timeline.bbl".
51
52 open IN, "$filename-sequence.aux";
53 $BIG="";
54 while (<IN>) {
55 if (m/\\bibstyle\{unsrt\}/) {
56 $BIG .= "\\bibstyle{chronological}\n";
57 } else {
58 $BIG .= $_;
59 }
60 }
61 close IN;
62 open OUT, "> $filename-timeline.aux";
63 print OUT $BIG;
64 close OUT;
65 system("bibtex $filename-timeline");
66
67 # Lastly, change "chronological" in the aux file to "apalike"
68 # and generate bbl file, retaining name "fn.bbl".
69
70 open IN, "$filename-timeline.aux";
71 $BIG="";
72 while (<IN>) {
73 if (m/\\bibstyle\{chronological\}/) {
74 $BIG .= "\\bibstyle{apalike}\n";
75 } else {
76 $BIG .= $_;
77 }
78 }
79 close IN;
80 open OUT, "> $filename.aux";
81 print OUT $BIG;
82 close OUT;
83 system("bibtex $filename");
84
85 cleanup("$filename.bbl");
86 cleanup("$filename-sequence.bbl");
87 cleanup("$filename-timeline.bbl");
88
89 open IN, "$filename-sequence.bbl";
90 my $counter=1;
91 my %UNSRT;
92 my %DATE;
93 my %NAME;
94 while (<IN>) {
95 if (m/\\bibitem{([^\}]+?)}/) {
96 $UNSRT{$1}=$counter;
97 $counter++;
98 }
99 }
100 close IN;
101
102 open IN, $filename.".bbl";
103 $counter=1;
104 while (<IN>) {
105 if (m/\\bibitem\[([^\]]+)\]{([^}]+?)}/) {
106 my $longdesc=$1;
107 my $label=$2;
108 if ($longdesc =~ /^(.*),\s+(\S+)$/) {
109 $NAME{$label}=$1;
110 $DATE{$label}=$2;
111 }
112 }
113 }
114 close IN;
115
116 open IN, $filename."-sequence.bbl";
117 $counter=1;
118 $BIG="";
119 while (<IN>) {
120 if (s/\\bibitem{(.+?)}/\\bibitem[\\MBlabel{$counter}{$NAME{$1}}{$DATE{$1}}]{$1}\n/) {
121 $counter++;
122 }
123 $BIG .= $_;
124 }
125 close IN;
126 open OUT, "> $filename-sequence.bbl";
127 print OUT $BIG;
128 close OUT;
129
130 open IN, $filename.".bbl";
131 $BIG="";
132 while (<IN>) {
133 s/\\bibitem\[[^\]]+\]{(.+?)}/\\bibitem[\\MBlabel{$UNSRT{$1}}{$NAME{$1}}{$DATE{$1}}]{$1}/;
134 $BIG .= $_;
135 }
136 close IN;
137 open OUT, ">$filename.bbl";
138 print OUT $BIG;
139 close OUT;
140
141 open IN, $filename."-timeline.bbl";
142 $BIG="";
143 while (<IN>) {
144 s/\\bibitem\[[^\]]+\]{(.+?)}/\\bibitem[\\MBlabel{$UNSRT{$1}}{$NAME{$1}}{$DATE{$1}}]{$1}/;
145 $BIG .= $_;
146 }
147 close IN;
148 open OUT, ">$filename-timeline.bbl";
149 print OUT $BIG;
150 close OUT;
151 }
152
153
154