-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexunexpand
executable file
·166 lines (154 loc) · 5.5 KB
/
texunexpand
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
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/perl
#
#
# Explodes a LaTeX file obtained from texexpand -x, and then compares
# the resulting files with the files existing on the current machines,
# removing the unmodified ones.
#
# Useful when collaborating with a remote author that is not particularly
# familiar with version control and/or basic tools like tar.
#
# (c) Roberto Di Cosmo <[email protected]> 2003-2011
# Distributend under the GPL Licence, V2 or later
#
# SUBROUTINES
#
# get the file name from a path
#
sub fnFromPath { local($path) = @_;
$path=~s-[./]*.*[/]--;
return $path;
}
#
# get the long path name from $_
#
sub getOutPath{
$_=~s/^.*% [*]*FULLNAME://;
$OUTPATH=$_;
}
#
# switch output file
#
sub getOutpathSwitchOutFile{
# get the new filename
&getOutPath();
# do the actual switch
&switchOutFile();
}
#
# switch output file without getting the new file name ...
#
sub switchOutFile{local($ouname);
# save the current output file on the stack
$filestack[$filetop++] = $OUT;
# open the new output file
$OUT=&fnFromPath($OUTPATH);
$outname=$OUT; chop $outname;
if (-f $outname) {print STDERR "Warning: $outname already exists... copying to .teximport\n";
if (!(-e "./.teximport")) {system("/bin/mkdir .teximport");};
$OUT = "./.teximport/$OUT";
};
open($OUT,">".$OUT);
print STDERR "x $OUT";
# also update the filename/filepath cross reference table
$paths{$OUT}=$OUTPATH;
}
#
# get the CWD of the original archive here ...
#
$_=<>;
if (!($_=~/% [*]*CWD is:/)) {die "not in texexpand -x format!";};
$_=~ s/% [*]*CWD is://;
chop;
$origcwd=$_;
#
# check the TOPLEVEL directive, and if it is a stdin, then output on stdout
#
$_=<>;
if (!($_=~/% [*]*TOPLEVEL/)) {die "not in texexpand -x format!";};
if (/STDIN/) {print STDERR "Warning: this file was generated from stdin... output to stdin.tex\n\n\n";
$OUT="stdin.tex\n"; $OUTPATH="stdin.tex\n";
&switchOutFile();}
else {
#
# open the main file corresponding to the first FULLNAME directive ...
#
$_=<>;
if (!($_=~/% [*]*FULLNAME:/)) {die "not in texexpand -x format!";};
$filetop=0;
&getOutpathSwitchOutFile();
};
#
# start the proper explode loop
#
while (<>) {
if (/% [*]*FULLNAME:/){
# restore characters preceding this directive
$tmp=$_;
$tmp=~s/% [*]*FULLNAME:.*$//; chop $tmp; print $OUT $tmp;
# get the new short file name
&getOutPath();
# restore the original input/include line before switching output file
$_=<>;
if (!(/% [*]*EXPORTBEGS:/)) {die "Not in texexpand -x format!";};
~s/% [*]*EXPORTBEGS: //; chop; if (!(/\\bibliography/)) {print $OUT $_;}; #do not mess with bibliography ...
# now switch otput file ...
&switchOutFile();
next;
}
if (/% [*]*EXPORTENDS:/){#close current output file and resume previous one ...
$OLDOUT=$OUT;
close($OUT);
$OUT=$filestack[--$filetop];
#print "Closing $OLDOUT because of $_, popping to $OUT\n";
next;
}
if (/% [*]*TOPLEVEL/){#close current output file and resume previous one ...
close($OUT);
if ($filetop> 1) {print STDERR "Warning: improper nesting at line \n";};
$filetop=0;
$_=<>;
if (!($_=~/% [*]*FULLNAME:/)) {die "not in texexpand -x format!";};
# now switch otput file ...
print STDERR "---------\n";
&getOutpathSwitchOutFile();
next;
}
if (/% [*]*EXPANDEDINCLUDEONLY/){ print $OUT "\\includeonly{"; next;};
if (/% [*]*ENDEXPANDEDINCLUDEONLY/){ print $OUT "}"; next;};
if (/% [*]*INCLUDEONLYFILE:/){$_=~s/% [*]*INCLUDEONLYFILE://; chop; print $OUT $_,","; next;};
if (/% [*]*EXPANDEDBIBSTYLE: /){ ~s/% [*]*EXPANDEDBIBSTYLE: //; print $OUT $_; next;};
if (/% [*]*EXPANDEDBIBFILE: /){
~s/% [*]*EXPANDEDBIBFILE: //; print $OUT $_; # restore \bibliography line
# restore characters preceding this directive
$tmp=$_;
$tmp=~s/% [*]*EXPANDEDBIBFILE:.*$//; chop $tmp; print $OUT $tmp;
# restore the original input/include line before switching output file
$_=<>;
if (!(/% [*]*EXPORTBEGS:/)) {die "Not in texexpand -x format!";};
~s/% [*]*EXPORTBEGS: \\bibliography{([^}]*)}/\1/; $OUTPATH=$_; # extract the name of the .bbl file
# now switch otput file ...
&switchOutFile();
next;};
#
# Otherwise, output the current line
#
print $OUT $_;
};
#
# close the main file
#
close($OUT);
#
# Now check for modifications ...
#
open(MLOG,"> .mlog");
foreach (keys %paths){$original = $paths{$_}; $new=$_; chop $new; chop $original;
$original=~s/^[ ]*[.]/$origcwd/;
if ($new eq $original) {print "U ",$new," *not* erased: it is the current file\n";next;};
system("diff $new $original > /dev/null 2>&1"); $result = $?;
if ($result == 0) {print "U ",$new," erased.\n"; unlink($new);}
elsif ($result == 256) {print "M ", $new,"\n"; print MLOG $new, " - ", $original,"\n";}
else {print STDERR "? ", $new, $original," is not available!\n";};
};
close(MLOG);