summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/multibibliography/multibibliography.pl
blob: 16374700359915bd8635a1e0047f27984ae498fa (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
#!/usr/bin/perl
# Usage: perl multibibliography.pl jobname
# By Yannis Haralambous and Michael Cohen

# Changes by Boris Veytsman, $Revision: 1.6 $, $Date: 2013-06-25 03:37:30 $

# This perl script does the bibtexing part, generating 3 separate .bbl
# files with descriptive names derived from source file name, so no
# need to separately invoke.

use strict;

sub cleanup {
    local $/=undef;
    my $file = shift;
    open IN, $file;
    my $BIG=<IN>;
    close IN;
    $BIG=~s/(\r\n|\r|\n)/ /g;
    $BIG=~s/\\bibitem/\n\n\\bibitem/g;
    $BIG=~s/\\newblock/\n\\newblock/g;
    $BIG=~s/\\end\{thebibliography\}/\n\n\\end{thebibliography}/g;
    $BIG=~s/[ ]+/ /g;
    open OUT, ">$file";
    print OUT $BIG;
    close OUT;
}

# First, change "apalike" in the aux file to "unsrt" and generate bbl
# file, renamed to "fn-sequence.bbl".
my $filename=$ARGV[0];
$filename=~s/\.(tex|bib|bbl|aux|log)$//;
if (-e $filename.".aux") {
    open IN, $filename.".aux";
    my $BIG="";
    while (<IN>) {
	if (m/\\bibstyle\{apalike\}/) {
	    $BIG .= "\\bibstyle{unsrt}\n";
	} else {
	    $BIG .= $_;
	}
    }
    close IN;
    open OUT, "> $filename-sequence.aux";
    print OUT $BIG;
    close OUT;
    system("bibtex $filename-sequence");
   
    # Now, change "unsrt" in the aux file to "chronological" 
    # and generate bbl file, renamed to "fn-timeline.bbl".

    open IN, "$filename-sequence.aux";
    $BIG="";
    while (<IN>) {
	if (m/\\bibstyle\{unsrt\}/) {
	    $BIG .= "\\bibstyle{chronological}\n";
	} else {
	    $BIG .= $_;
	}
    }
    close IN;
    open OUT, "> $filename-timeline.aux";
    print OUT $BIG;
    close OUT;
    system("bibtex $filename-timeline");
    
    # Lastly, change "chronological" in the aux file to "apalike" 
    # and generate bbl file, retaining name "fn.bbl".
    
    open IN, "$filename-timeline.aux";
    $BIG="";
    while (<IN>) {
	if (m/\\bibstyle\{chronological\}/) {
	    $BIG .= "\\bibstyle{apalike}\n";
	} else {
	    $BIG .= $_;
	}
    }
    close IN;
    open OUT, "> $filename.aux";
    print OUT $BIG;
    close OUT;
    system("bibtex $filename");
    
    cleanup("$filename.bbl");
    cleanup("$filename-sequence.bbl");
    cleanup("$filename-timeline.bbl");
    
    open IN, "$filename-sequence.bbl";
    my $counter=1;
    my %UNSRT;
    my %DATE;
    my %NAME;
    while (<IN>) {
	if (m/\\bibitem{([^\}]+?)}/) {
	    $UNSRT{$1}=$counter;
	    $counter++;
	}
    }
    close IN;
    
    open IN, $filename.".bbl";
    $counter=1;
    while (<IN>) {
	if (m/\\bibitem\[([^\]]+)\]{([^}]+?)}/) {
	   my $longdesc=$1;
	   my $label=$2;
	   if ($longdesc =~ /^(.*),\s+(\S+)$/) {
	       $NAME{$label}=$1;
	       $DATE{$label}=$2;
	   }
       }
    }
    close IN;
    
    open IN, $filename."-sequence.bbl";
    $counter=1;
    $BIG="";
    while (<IN>) {
	if (s/\\bibitem{(.+?)}/\\bibitem[\\MBlabel{$counter}{$NAME{$1}}{$DATE{$1}}]{$1}\n/) {
	    $counter++;
	}
	$BIG .= $_;
    }
    close IN;
    open OUT, "> $filename-sequence.bbl";
    print OUT $BIG;
    close OUT;
    
    open IN, $filename.".bbl";
    $BIG="";
    while (<IN>) {
	s/\\bibitem\[[^\]]+\]{(.+?)}/\\bibitem[\\MBlabel{$UNSRT{$1}}{$NAME{$1}}{$DATE{$1}}]{$1}/;
	$BIG .= $_;
    }
    close IN;
    open OUT, ">$filename.bbl";
    print OUT $BIG;
    close OUT;
    
    open IN, $filename."-timeline.bbl";
    $BIG="";
    while (<IN>) {
		s/\\bibitem\[[^\]]+\]{(.+?)}/\\bibitem[\\MBlabel{$UNSRT{$1}}{$NAME{$1}}{$DATE{$1}}]{$1}/;
	$BIG .= $_;
    }
    close IN;
    open OUT, ">$filename-timeline.bbl";
    print OUT $BIG;
    close OUT;
}