Debian Codegraph @

texmf-dist/scripts/texdirflatten/texdirflatten

raw
1 #! /usr/bin/env perl
2
3 # Use:
4 # $ perldoc texdirflatten
5 # to see embedded documentation. Alternatively, you can create manual
6 # or HTML pages using pod2man and pod2html.
7
8 =pod
9
10 =head1 NAME
11
12 B<texdirflatten> - Collects all components of a (La)TeX file in a
13 single output directory -- i.e., flattens its hierarchy.
14
15 =head1 SYNOPSIS
16
17 texdirflatten [-1|--onetex] [-f|--file input.tex] [-o outputdir]
18 [--imgexts .ext1,.ext2,...] [--debug] [-V|--version] [-?|--help]
19
20 =head1 DESCRIPTION
21
22 This Perl script parses a LaTeX file recursively, scanning all child
23 files, and collects details of any included and other data files, such
24 as graphics and BiBTeX bibliography files. These component files, are
25 then all put into a single directory (thus "flattening" the document's
26 directory tree). This is useful in distributing manuscripts to
27 collaborators or in submitting to journals.
28
29 =for comment
30 Run without parameters to see usage.
31
32 =head1 OPTIONS
33
34 =over
35
36 =item B<--file>, B<-f> I<input.tex>
37
38 Specifies input (La)TeX file.
39
40 =item B<--onetex>, B<-1>
41
42 If specified, produces a single TeX file by expanding all \input and
43 \include commands in place.
44
45 =item B<--output>, B<-o> I<outputdir>
46
47 Directory to collect all files. B<texdirflatten> will copy each source
48 file, graphics and bibliography file to this directory. It will be
49 created if it is unexistent. If unspecified, it defaults to C<flat/>.
50
51 =item B<--imgexts> I<.ext1,.ext2[,...]>
52
53 Prepends to the prioritized list of image extensions to search when
54 trying to find the image to copy to output folder. The first file with
55 the extension found will be copied and search stopped. Default order
56 is C<"", ".eps", ".pdf", ".pstex"> (note that it includes files with
57 no extension). If using pdflatex, one may want to add PDF before EPS
58 with C<--imgexts .pdf>. Don't forget the dot before the extension!
59
60 =item B<--debug>
61
62 Enables copious amounts of debugging output - useful if something is going wrong.
63
64 =item B<--version>, B<-V>
65
66 Displays the current version number and the usage and exits.
67
68 =item B<--help>, B<-?>
69
70 Show this manual page.
71
72 =back
73
74 =head1 EXAMPLES
75
76 The following example scans C<manuscript.tex> in the current directory
77 and gathers it and all its components in the C<submit_01/> directory:
78
79 $ texdirflatten -f manuscript.tex -o submit_01
80
81 =head1 CAVEATS
82
83 Please take backups before running this command. No warranties
84 whatsoever provided.
85
86 You may need to run C<epstopdf> on EPS files if you are using C<pdflatex>:
87
88 $ for i in *.eps; do epstopdf $i; done
89
90 =head1 BUGS
91
92 Bug reports and patches are welcome.
93
94 =head1 AUTHOR
95
96 Cengiz Gunay <cengique<AT>users.sf.net>
97
98 =head1 COPYRIGHT AND LICENSE
99
100 Copyleft 2003-2017, Cengiz Gunay
101
102 This library is free software; you may redistribute it and/or modify
103 it under the same terms as Perl itself.
104
105 =cut
106
107 # TODO:
108 # - parse BIBINPUTS environment variable and search figures and such
109 # there as well.
110 # - also copy bibtex style file
111
112 package texdirflatten;
113
114 use Getopt::Long qw(GetOptions);
115 use Pod::Usage;
116 use strict;
117 use warnings;
118 use re 'eval';
119
120 # Global
121 my $outputdir = "flat"; # Output directory
122 my $file = ""; # File to read
123 my @imgexts = ("", ".eps", ".pdf", ".pstex"); # Ordered list of image extensions to search and copy
124 my $imgexts_pre; # Prepend to @imgexts
125 my $onetex = 0; # Indicates all input and include commands be expanded
126 my $version = "1.3"; # 2017-02-02
127 my $DEBUG = 0; # Enable debugging output
128 my $help;
129
130 # parse helpers
131 my %texfiles;
132
133 # GetOpt::Long::
134 my $result = GetOptions ("output|o=s" => \$outputdir,
135 "onetex|1" => \$onetex,
136 "file|f=s" => \$file,
137 "imgexts=s" => \$imgexts_pre,
138 "debug" => \$DEBUG,
139 "version|V" => sub { print "texdirflatten version $version\n";
140 pod2usage( -section => "SYNOPSIS"); },
141 "help|?" => \$help );
142
143 # regexp for none or an even number of backslashes (\):
144 my $unescaped =
145 '(?<! \\\\)(?(?= \\\\) (?: \\\\\\\\)* )?';
146
147 # counter for files translated (like LyX does)
148 $::filecount = 0;
149
150 pod2usage( -verbose => 2 ) if ($help);
151
152 pod2usage( -section => "SYNOPSIS") if ($file eq "");
153
154 if ($imgexts_pre) {
155 unshift(@imgexts, split(/,/, $imgexts_pre));
156 print "imgexts: ". join(",", @imgexts) . "\n" if $DEBUG;
157 }
158
159 system "mkdir $outputdir" if (not -d $outputdir);
160
161 # start recursing
162 parseTeX($file, "", popfile($file), 0);
163
164 sub parseTeX {
165 my $file = shift;
166 my $inputdir = shift;
167 my $outfile = "$outputdir/" . shift;
168 my $level = shift;
169
170 # Add a proper TeX suffix if it's missing
171 $outfile .= '.tex' if ! ($outfile =~ /\.tex\s*$/i );
172
173 my @flats;
174 my @longs;
175 my $popped;
176
177 open my $FILE, $file or die "Cannot find file to read $file\n";
178
179 # Read to whole file first, and then scan for regexps
180 my $contents = "";
181 while(<$FILE>) {
182 $contents = $contents . $_;
183 }
184 close $FILE;
185
186 ## First, remove comments so that input commands inside are ignored.
187 # Also remove trailing newline and all whitespace at the beginning
188 # of the following line like TeX does it. Only for %'s following
189 # the proper backslash pattern.
190 $contents =~ s/( $unescaped )\%.*?\n[ \t]*/$1/gx;
191
192 # Look for graphics include statement
193 if ($contents =~ /\\input\@path\{\{([^}]*)\}\}/) {
194 $inputdir = $1;
195 print "Found input directory: $inputdir\n" if $DEBUG;
196 }
197
198 print "Parsing '$file' in directory '$inputdir'\n";
199
200 # Default value
201 #$inputdir = './' if (-z $inputdir);
202
203 # three cases: graphics, inputs and bibs
204
205 # an \includegraphics statement
206 @::flats = ();
207 @::longs = ();
208 #$::popped = "";
209
210 $contents =~
211 s/\\includegraphics(\[[^\]]*\])?\{([^}]*)\}
212 (?{ $::popped = flattenfilename($2);
213 push @::longs, $2; push @::flats, $::popped })
214 /\\includegraphics$1\{$::popped\}/gmx;
215
216 if ($#::flats > -1) {
217 my ($long, $flat);
218
219 foreach $long (@::longs) {
220 $flat = shift @::flats;
221
222 # convert LyX directory dots
223 $long =~ s/\\lyxdot /./g;
224 $flat =~ s/\\lyxdot /./g;
225
226 print "Looking for graphics: '$long'\n" if $DEBUG;
227
228 my @dirs = ("", "$inputdir", $inputdir . '../figures/');
229
230 seekfile($long, $flat, \@imgexts,
231 ["", "$inputdir", $inputdir . '../figures/']);
232
233 }
234 }
235
236 # Must do bibs before input commands because, when expanded, they
237 # add new bibs inside.
238 replacebibs(\$contents, $inputdir);
239
240 # an \input or \include statement
241 @flats = ();
242 @longs = ();
243 $contents =~
244 s/\\(input|include)\{([^}]*)\}/"\\${1}\{" .
245 ($popped = flattenfilename($2) and push @longs, $2 and push @flats,$popped and $popped)
246 . "\}"/egm;
247
248 if ($#flats > -1) {
249 #print "Found " . scalar @longs . " items on line: '@flats'.\n";
250
251 my ($long, $flat);
252
253 foreach $long (@longs) {
254 $flat = shift @flats;
255 print "Found Input/Include: '$long'\n" if $DEBUG;
256 # recurse to parse that tex file unless it's done already
257
258 if (not exists $texfiles{$flat}) {
259 if (! -r $long ) {
260 if (-r $inputdir . $long) {
261 $long = $inputdir . $long;
262 } elsif (-r "$long.tex") {
263 $long .= '.tex';
264 }
265 }
266 $texfiles{$flat} = $long;
267 my $newcontents = parseTeX($long, basedir($long), $flat, $level + 1); #later
268 if ( $onetex ) {
269 # If specified, expand inclusion directives
270 die "Failed to find input/include to expand for '$flat'!\n"
271 if (! ( $contents =~
272 s/\\(input|include) \{ \Q$flat\E \}
273 (?{ print "===Found \\$1\{$flat\} to expand.\n" if $DEBUG; })
274 / ($1 eq 'include' ? "\\clearpage\n" : '') . "$newcontents"/gxem ) );
275 }
276 }
277 }
278 }
279
280 #print "Writing to \"$outfile\"\n" .
281 # "----------------------------------------\n" .
282 # "$contents\n" .
283 # "----------------------------------------\n";
284
285 if (! $onetex || $level == 0) { # do output at zero recurse level
286 # open flat output file
287 open my $COPY, ">$outfile" or die "Cannot write file $outfile\n";
288
289 # write to flat copy
290 print $COPY $contents;
291
292 close $COPY;
293 } else {
294 return $contents;
295 }
296 }
297
298 # Look for file to copy
299 sub seekfile {
300 my ($file, $dest, $exts, $dirs) = @_;
301 my @exts = @$exts;
302 my @dirs = @$dirs;
303
304 OUT: foreach my $dir (@dirs) {
305 foreach my $ext (@exts) {
306 my $tryfile = "$dir$file$ext";
307 if (-r $tryfile) {
308 # add the extension to $dest only if it hasn't have any dots
309 #(! ($dest =~ /\./)) ? $ext : ""; => better not to have this
310 my $dext = $ext;
311
312 if (system("cp $tryfile $outputdir/$dest$dext") != 0) {
313 die "Cannot copy $tryfile!\n";
314 }
315 # If we found it, then get out of the loop
316 print "Copied: '$tryfile' (ext: $ext) -> '$dest$dext'\n" if $DEBUG;
317 last OUT;
318 }
319 }
320 }
321 }
322
323 # Return everything after the last /
324 sub popfile {
325 my $file = shift (@_);
326
327 my @a = split /\//, $file;
328
329 #print "Popping $file... split: @a ($#a), pop: " . pop(@a) . "\n";
330 pop @a;
331 }
332
333 # Make a new file by flattening directory names
334 sub flattenfilename {
335 local $_ = shift;
336
337 # convert slashes to pluses, dots to Xs
338 tr|/|+|;
339 tr|\.|X|;
340
341 # add a unique number at the beginning so files don't start with a
342 # dot
343 $_ = sprintf("%03d_", ++$::filecount) . "$_";
344
345 print "===Flattened file to '$_'\n" if $DEBUG;
346
347 return $_;
348 }
349
350 sub basedir {
351 my $file = shift (@_);
352
353 my @a = split /\//, $file;
354
355 #print "Popping $file... split: @a ($#a), pop: " . pop(@a) . "\n";
356 pop @a; # remove file part
357 my $basedir = join('/', @a); # combine the rest with /'s
358 $basedir .= '/' if (! $basedir =~ /^$/); # add a trailing / if not empty
359 $basedir;
360 }
361
362 # get the bibliography files
363 sub replacebibs {
364 my ($contentsref, $inputdir) = @_;
365 #my $contents = $$contentsref;
366 my $popped;
367
368 #print "bib contents: $contents\n";
369
370 local @::flats = ();
371 local @::longs = ();
372 local @::refs;
373
374 $" = ','; # set list item separator
375 # search for \bibliography statements
376 $$contentsref =~
377 s/\\bibliography(\[[^\]]*\])? {
378 (?{ @::refs = (); #print "Found bibliography!\n"; #initialize ref list
379 }) (?: \s* ([^}, ]+)(?=[,}]),?
380 (?{ # for each word do:
381 #local (@_flats, @_longs, @_refs);
382 #print "Word: \"$+\", ";
383 $popped = flattenfilename($+); push @::longs, $+;
384 push @::flats,$popped; push @::refs, $popped;
385 }) )+ \s* }
386 (?{ #print "\n+++ $#::flats; $#::refs; $#::longs\n";
387 #push @::flats, @_flats; push @longs, @_longs; push @refs, @_refs;
388 #print "Refs: @::refs\n";
389 })/"\\bibliography" . ($1 || "") . "{@::refs}"/egmx;# &&
390 #print "bib contents: $$contentsref\n";
391 #print "\n*** $#::flats; $#::refs; $#::longs\n";
392 $" = ' '; # restore list item separator
393
394 if ($#::flats > -1) {
395 #print "Found " . scalar @::longs . " items on line: '@::flats'.\n";
396
397 my ($long, $flat);
398
399 foreach $long (@::longs) {
400 $flat = shift @::flats;
401 print "Found bib. file: '$long'\n" if $DEBUG;
402 # recurse to parse that tex file unless it's done already
403
404 # convert LyX directory dots
405 $long =~ s/\\lyxdot /./g;
406
407 seekfile($long, "$flat", [".bib"],
408 ["", "$inputdir"]);
409 }
410 }
411 }