1
  N   ˆl–ßi~” ”Ð?Je@‚ãn\ªbÑ¿�þEr,Ü£�’>Ñ[”€þÒ„B ÿÑa–ßi~” T¾R( 	Å õÁ=q–ÔÅ£Ð +]    #!/usr/bin/perl
# Debian required shebang above
# Original shebang below
# #! /usr/bin/env perl

# Use:
# $ perldoc texdirflatten
# to see embedded documentation. Alternatively, you can create manual
# or HTML pages using pod2man and pod2html.

=pod

=head1 NAME

B<texdirflatten> - Collects all components of a (La)TeX file in a
single output directory -- i.e., flattens its hierarchy.

=head1 SYNOPSIS

texdirflatten [-1|--onetex] [-f|--file input.tex] [-o outputdir] 
    	      [--imgexts .ext1,.ext2,...] [--debug] [-V|--version] [-?|--help]

=head1 DESCRIPTION

This Perl script parses a LaTeX file recursively, scanning all child
files, and collects details of any included and other data files, such
as graphics and BiBTeX bibliography files. These component files, are
then all put into a single directory (thus "flattening" the document's
directory tree). This is useful in distributing manuscripts to
collaborators or in submitting to journals.

=for comment
Run without parameters to see usage.

=head1 OPTIONS

=over

=item B<--file>, B<-f> I<input.tex>

Specifies input (La)TeX file.

=item B<--onetex>, B<-1>

If specified, produces a single TeX file by expanding all \input and
\include commands in place.

=item B<--output>, B<-o> I<outputdir>

Directory to collect all files. B<texdirflatten> will copy each source
file, graphics and bibliography file to this directory. It will be
created if it is unexistent. If unspecified, it defaults to C<flat/>.

=item B<--imgexts> I<.ext1,.ext2[,...]>

Prepends to the prioritized list of image extensions to search when
trying to find the image to copy to output folder. The first file with
the extension found will be copied and search stopped. Default order
is C<"", ".eps", ".pdf", ".pstex"> (note that it includes files with
no extension). If using pdflatex, one may want to add PDF before EPS
with C<--imgexts .pdf>. Don't forget the dot before the extension!

=item B<--debug>

Enables copious amounts of debugging output - useful if something is going wrong.

=item B<--version>, B<-V>

Displays the current version number and the usage and exits.

=item B<--help>, B<-?>

Show this manual page.

=back

=head1 EXAMPLES

The following example scans C<manuscript.tex> in the current directory
and gathers it and all its components in the C<submit_01/> directory:

 $ texdirflatten -f manuscript.tex -o submit_01

=head1 CAVEATS

Please take backups before running this command. No warranties
whatsoever provided.

You may need to run C<epstopdf> on EPS files if you are using C<pdflatex>:

 $ for i in *.eps; do epstopdf $i; done

=head1 BUGS

Bug reports and patches are welcome.

=head1 AUTHOR

Cengiz Gunay <cengique<AT>users.sf.net>

=head1 COPYRIGHT AND LICENSE

Copyleft 2003-2017, Cengiz Gunay

This library is free software; you may redistribute it and/or modify
it under the same terms as Perl itself.

=cut

# TODO:
# - parse BIBINPUTS environment variable and search figures and such
# there as well.
# - also copy bibtex style file

package texdirflatten;

use Getopt::Long qw(GetOptions);
use Pod::Usage;
use strict;
use warnings;
use re 'eval';

# Global
my $outputdir   = "flat";	# Output directory
my $file = "";			# File to read
my @imgexts = ("", ".eps", ".pdf", ".pstex"); # Ordered list of image extensions to search and copy
my $imgexts_pre;			    # Prepend to @imgexts
my $onetex = 0;			# Indicates all input and include commands be expanded
my $version = "1.3";		# 2017-02-02
my $DEBUG = 0;			# Enable debugging output
my $help;

# parse helpers
my %texfiles;

# GetOpt::Long::
my $result = GetOptions ("output|o=s" => \$outputdir,
			 "onetex|1" => \$onetex,
			 "file|f=s" => \$file,
			 "imgexts=s" => \$imgexts_pre,
			 "debug" => \$DEBUG,
			 "version|V" => sub { print "texdirflatten version $version\n"; 
					      pod2usage( -section => "SYNOPSIS"); },
			 "help|?" => \$help );

# regexp for none or an even number of backslashes (\):
my $unescaped =
      '(?<! \\\\)(?(?= \\\\) (?: \\\\\\\\)* )?';