-
Notifications
You must be signed in to change notification settings - Fork 0
/
ltags
executable file
·78 lines (69 loc) · 2.41 KB
/
ltags
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
#!/usr/bin/perl
# Author: Dimitri Antoniou <[email protected]>
# usage: ltags filename
# handles: \label and \cite{ } with one or more arguments
# fails if arguments of cite spread over more than one line
# also searches in files that are \include or \input in the main file
# get main LaTeX source file from command line:
$mainfile = shift;
# get names of included files and store them in an array
open MAIN, $mainfile or die "$!" ;
@mainfile=<MAIN>;
@allsrcfiles = map{ /^\\(?:input|include){(.*?)}/ } @mainfile;
unshift @allsrcfiles, $mainfile;
# loop over all source files
for $srcfile (@allsrcfiles) {
# if \input{fname} append .tex to fname
unless ( $srcfile =~ m/\.tex/ ) { $srcfile = $srcfile . "\.tex" }
open SRC, $srcfile or die "$!" ;
# store contents of source file in array @texfile
@texfile=<SRC>;
# store lines with \label and \cite (or \citeonline) in arrays
@labelList = grep{ /\\label{/ } @texfile;
@citeList = grep{ /\\(cite|citeonline){/ } @texfile;
# see if we use an external database; if yes, store its name in $bibfile
($dbase) = grep{ /^\\bibliography{/ } @texfile;
if ($dbase) {
$dbase =~ m/\\bibliography{(.*?)}/;
$bibfile = $1;
}
# write \bibitem in tags file
@mrefs=();
@refs=();
@multirefs=();
foreach (@citeList) {
while ( m/\\(?:cite|citeonline){(.*?)}/g ) {
$refs = $1;
# if \cite has more than one argument, split them:
if ($refs =~ /,/) {
@mrefs = split /,/, $refs;
# there might be more than one \cite in a line:
push (@multirefs, @mrefs);
}
else {
@refs = ($refs);
push (@multirefs, @refs);
}
}
# in BibTeX, format is @ARTICLE{Name, }; in source file, \bibitem{Name}
for $ref (@multirefs) {
if ( $dbase ) {
push @unsorttag, "$ref\t$bibfile\t/{$ref,/\n"
}
else {
push @unsorttag, "$ref\t$srcfile\t/bibitem{$ref}/\n"
}
}
}
# write \label in tag file
foreach (@labelList) {
m/\\label{(.*?)}/;
push @unsorttag, "$1\t$srcfile\t/label{$1}/\n";
}
}
# sort tag file; then, eliminate duplicates
@sortedtag = sort @unsorttag;
%seen = ();
@uniqtag = grep { ! $seen{$_} ++ } @sortedtag;
open(TAGS, "> tags");
print TAGS @uniqtag;