summary refs log tree commit diff
path: root/pkgs/misc/tex/nix/find-includes.pl
blob: 6441f18855b7fae31f227753ec586f2bede0b3da (plain) (blame)
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
use strict;
use File::Basename;

my $src = $ENV{"src"};
my $out = $ENV{"out"};
my $path = $ENV{"searchRelativeTo"};

open OUT, ">$out" or die;
print OUT "[\n";

open FILE, "< $src" or die;

sub addName {
    my ($type, $name) = @_;
    print OUT "{ type = \"$type\"; name = \"$name\"; }\n";
}
    
while (<FILE>) {
    if (/\\input\{(.*)\}/) {
        my $fn2 = $1;
        die "absolute path! $fn2" if substr($fn2, 0, 1) eq "/";
        addName "tex", "$fn2";
    } elsif (/\\input (.*)$/) {
        my $fn2 = $1;
        die "absolute path! $fn2" if substr($fn2, 0, 1) eq "/";
        addName "tex", "$fn2";
    } elsif (/\\RequirePackage(\[.*\])?\{(.*)\}/) {
        my $fn2 = $2;
        die "absolute path! $fn2" if substr($fn2, 0, 1) eq "/";
        addName "misc", "$fn2.sty";
    } elsif (/\\usepackage(\[.*\])?\{(.*)\}/) {
        my $fn2 = $2;
        die "absolute path! $fn2" if substr($fn2, 0, 1) eq "/";
        addName "misc", "$fn2.sty";
    } elsif (/\\documentclass(\[.*\])?\{(.*)\}/) {
        my $fn2 = $2;
        die "absolute path! $fn2" if substr($fn2, 0, 1) eq "/";
        addName "misc", "$fn2.cls";
    } elsif (/\\bibliographystyle\{(.*)\}/) {
        my $fn2 = $1;
        die "absolute path! $fn2" if substr($fn2, 0, 1) eq "/";
        addName "misc", "$fn2.bst";
    } elsif (/\\bibliography\{(.*)\}/) {
        foreach my $bib (split /,/, $1) {
            $bib =~ s/^\s+//; # remove leading / trailing whitespace
            $bib =~ s/\s+$//;
            addName "misc", "$bib.bib";
        }
    } elsif (/\\includegraphics(\[.*\])?\{(.*)\}/) {
        my $fn2 = $2;
        die "absolute path! $fn2" if substr($fn2, 0, 1) eq "/";
        addName "img", "$fn2";
    } elsif (/\\pgfdeclareimage(\[.*\])?\{.*\}\{(.*)\}/) {
        my $fn2 = $2;
        die "absolute path! $fn2" if substr($fn2, 0, 1) eq "/";
        addName "img", "$fn2";
    } elsif (/\\pgfimage(\[.*\])?\{(.*)\}/) {
        my $fn2 = $2;
        die "absolute path! $fn2" if substr($fn2, 0, 1) eq "/";
        addName "img", "$fn2";
    }
    # !!! also support \usepackage
}

close FILE;

print OUT "]\n";
close OUT;