summaryrefslogtreecommitdiff
path: root/support/orderer/Orderer.pl
blob: 2d529848f99f4e158b18b006b4c10a7c10c4ef5c (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
#!/usr/bin/perl
# The script is designated to order the references in the external .tex-file.
# It is assumed that all the references are introduced using standard \bibitem{key}. M.S.
if(!$ARGV[0]) 
{print "Enter the external .tex-file name in which You want to order references:";
$file=<STDIN>;chomp($file);$fileout="$file.tex";
print "By default the output will be placed to the file $fileout\nYou can specify the output using option -o in the command line call: ./Orderer filename -o OutputName\n";}
else {$file=$ARGV[0];if(($ARGV[1] eq '-o')and($ARGV[2])){$fileout=$ARGV[2]}else{$fileout="$file.tex"}};
print "Opening file $file\n";
open(SM,$file);@lines=<SM>;close(SM);
# Now drop all the lines which start with %(comment)
foreach $line(@lines){if($line!~/^[ ]*\%/){push(@Lines,"$line")}};
foreach $line(@Lines){if($line=~ /\\cite\{[a-zA-Z:0-9\,\']+\}/g){&AddCite($&);
while($'=~ /\\cite\{[a-zA-Z:0-9\,\']+\}/g) # Multiple citations on the same page
 {&AddCite($&)}}};
print "$#Cite citations found\n"; 
foreach $line(@Lines){$x=$line;if($x=~s/(\\bibitem\{)([a-zA-Z:0-9\,\']+)(\}\s)$/\2/g){$hash=$x;chomp($hash)};
if($hash){if($line!~/\\end/){@Y=("$BibItem{$hash}","$line");$BibItem{$hash}="@Y";}
  else{push(@LastLines,$line)}}else{push(@FirstLines,$line)};};
print "Printing out ordered list into file $fileout:\n";
open(SM,">$fileout");print SM "@FirstLines";
foreach $citation(@Cite){print SM $BibItem{$citation}};
print SM "@LastLines";close(SM);&Check; 
sub AddCite
# The subroutine extracts the citation and adds it to the specified array
# Format of the call: (String)
{$string=$_[0];$string=~ s/^(\\cite\{)([a-zA-Z:0-9\,\']+)(\})$/\2/g;@Values=split(/\,/,$string);
foreach $value(@Values){if("@Cite"!~/$value/){push(@Cite,$value)};}};
sub Check
{@BibItemsList=keys %BibItem;
foreach $citation(@Cite){if("@BibItemsList"!~/$citation/){print "Warning::Citation $citation is not found !\n "}};
foreach $bib(@BibItemsList){if("@Cite"!~/$bib/){print "Warning::Bibitem", $BibItem{$bib}, " is not used !\n "}};};