open(OUT, '>'.$outFilename);
print OUT $out;
close(OUT);
system('pdflatex',$outFilename);
EOF
#
# I'll assume most distos have psfrag and leave this out for now...
#
# use Cwd;
#$cwd = cwd();
#$ENV{TEXINPUTS} .= ":$cwd/psfrag";
#$ENV{DVIPSHEADERS} .= ":$cwd/psfrag";
######################
# MAIN
######################
### 1) if it doesn't end with a newline, add one
if (substr($in,-1) ne "\n") {$in = $in . "\n";}
### 2) convert DOS-style newlines to UNIX-style (strip linefeeds)
while ($in =~ s/\r$//g) {}
### 3) store verbatims
$in = storeVerbatims($in);
## first, make a copy of the input
open(OUT, '>'.$outFilename);
print OUT $in;
close(OUT);
## now, run the transforms on it
foreach $transform (@TRANSFORM_PATHS) {
if (-x $transform)
{
system("$transform", "$outFilename", "$EASYLATEX_DIR", @TRANSFORM_ARGS);
open(OUT, '>'.$outFilename);
print OUT $out;
close(OUT);
## do post-processing if requested
postProcess($out);
######################
# end of MAIN
######################
sub init
{
findEasylatexDir();
initListOfTransforms();
if ($PARTIAL_DOCUMENT) {
push(@TRANSFORM_ARGS, '--partial');
}
$TRANSFORM_ARGS = join(' ', @TRANSFORM_ARGS);
#print "DBG: #$TRANSFORM_ARGS#\n";
}
sub postProcess {
my ($out) = @_;
open(FILE, $outFilename);
local undef $/;
my $file = <FILE>;
close(FILE);
#TODO: may not work with pdflatex
if ($file =~ /\\bibliography{(.*)\.bib}/) {
system('latex',$outFilename);
system('bibtex',$1);
}
system('latex',$outFilename);
if ($POSTSCRIPT_OUTPUT)
{
eval $PS_OUTPUT_COMMANDS;
}
if ($PDF_OUTPUT)
{
eval $PDF_OUTPUT_COMMANDS;
}
if ($PDFLATEX_OUTPUT)
{
eval $PDFLATEX_OUTPUT_COMMANDS;
}
}
###########################################################################
# findEasylatexDir
# looks through a searchpath for the easylatex directory.
# die if it can't find it.
###########################################################################
sub findEasylatexDir {
local @easylatex_dir_searchpath;
####################################################
# errorCheckEasylatexDir
# if the easylatex or the transform directories
# don't exist or aren't directories, then die
####################################################
sub errorCheckEasylatexDir {
if (! $EASYLATEX_DIR)
{
die "Can't find easylatex directory. The program probably isn't installed correctly; please see documentation or run quickInstall. Aborting (looked for the directory in " . join(', ', @easylatex_dir_searchpath) . ")";
}
if (! ( -e "$EASYLATEX_DIR/transforms" && -d "$EASYLATEX_DIR/transforms"))
{
die "I found the easylatex directory at $EASYLATEX_DIR, but didn't find the directory $EASYLATEX_DIR/transforms. The program probably isn't installed correctly; please see documentation. Aborting.";
}
# TODO: add to docs
}
###########################################################################
# initListOfTransforms
# makes a list of the paths of the transforms to run
# (1) lists the files in the transform directory,
# (2) filters out files starting with '.' or '#' or ending with '~',
# (3) sorts the resulting list,
# (4) prepends the path to each file,
# (5) leaves the result in the global variable @TRANSFORM_PATHS
###########################################################################
sub initListOfTransforms {
my %fullPath;
##### list the files in the transform directory
opendir(TRANSFORMDIR, "$EASYLATEX_DIR/transforms") || die "can't opendir $EASYLATEX_DIR/transforms. The program probably isn't installed correctly; please see documentation. Aborting.";
@TRANSFORM_PATHS = readdir(TRANSFORMDIR);
closedir(TRANSFORMDIR);
$file =~ s/(\\begin{verbatim}.*?\\end{verbatim})/store($1)/egs;
$file =~ s/\\begin{latex}(.*?)\\end{latex}/store($1)/egs;
$file =~ s/(\\newcommand.*)/store($1)/eg;
$file =~ s/(\\includegraphics(?:\[[^\]]*\])?{[^}]+})/store($1)/egs;
# TODO: there's a bug in the last line (i think, maybe)
$file =~ s/(%%.*)/store($1)/eg;
return $file;
}
sub unstoreVerbatims {
my ($file) = @_;
# print STDERR "UNSTORE:\n\n".$file;
while ($file =~ s/ ?${EASYLATEXSTORE_MAIN_RESERVED_WORD}-(\d+)-${EASYLATEXSTORE_MAIN_RESERVED_WORD} ?/$Stored{$1}/egs) {}