#!/usr/bin/perl -w
use strict;
use POSIX qw<ctime>;
use Getopt::Std;
my %opts;
getopts('a:h:p?', \%opts);
if ($opts{'?'}) {
print <<EOT;
Usage: $0 [-a author] [-h num] [-p] [files ...]
Switches:
-a author - specifies the name of the author (for header 2)
-h num - specifies the header style to use (see below)
-p - Keep the TeX & PDF files without printing them
Header styles:
0 - no header; footer: page number
1 - header: filename, date; footer: page number (default)
2 - header: author, filename; footer: page number
3 - header: filename, page number; no footer
4 - header: filename; footer: page number
EOT
exit 0;
}
@ARGV = ('-') unless @ARGV;
$opts{a} = 'Japheth A. Packer-Herl' unless exists $opts{a};
$opts{h} = 1 unless exists $opts{h};
sub headers {
(my $filename = shift) =~ s/([_&{}^~])/\\$1/g;
my $head = '\headline={\tt\vbox{\hbox to\hsize{%s\hfil %s}\smallskip\hrule}}';
my $foot = '\footline={\tt\hfil- \folio{} -\hfil}';
if ($opts{h} == 0) { return $foot }
elsif ($opts{h} == 1) {
return sprintf "$head\n$foot", $filename, substr ctime(time), 0, -1
} elsif ($opts{h} == 2) { return sprintf "$head\n$foot", $opts{a}, $filename }
elsif ($opts{h} == 3) {
return sprintf "$head\n\\footline={}", $filename, '\folio'
} elsif ($opts{h} == 4) { return sprintf "$head\n$foot", $filename, '' }
}
foreach my $f (@ARGV) {
open INFILE, '<', $f or print STDERR "Error reading $_: $!\n" and next;
if (-e "$f.tex") {
print "Overwrite file $f.tex? (y/n) ";
print "Well, then, we have a problem.\n" and next if <STDIN> !~ /^y/i;
}
open TEX, '>', "$f.tex" or print STDERR "Error writing to $f.tex: $!\n" and next;
select TEX;
print <<'EOC', headers($f), <<'EOT';
% The macros used here are taken from *TeX for the Impatient* by Paul W.
% Abrahams, Kathryn A. Hargreaves, and Karl Berry, which can be found at
% <
ftp://tug.org/tex/impatient>.
EOC
\tt
\def\deactivate{%
\catcode`\\=12 \catcode`\{=12 \catcode`\}=12
\catcode`\$=12 \catcode`\&=12 \catcode`\#=12
\catcode`\~=12 \catcode`\^=12 \catcode`\_=12
}
\def\makeactive#1{\catcode`#1=\active\ignorespaces}
{\makeactive\^^M \gdef\obeywhitespace{%
\makeactive\^^M \let^^M=\newline\aftergroup\removebox\obeyspaces\frenchspacing
}}
\def\~{\char126}\def\newline{\par\indent}\def\removebox{\setbox0=\lastbox}
\def\verbatim{\begingroup\deactivate\obeywhitespace\catcode`\~=0}
\verbatim%
EOT
while (<INFILE>) {
chomp; if ($_ eq '') {print "\n"; next; }
1 while s/\t+/' ' x (length($&)*8 - length($`)%8)/e;
while ((my $sub = substr $_, 0, 80, '') ne '') {
$sub =~ s/~( ?)/'~~' . ($1 ? '~ ' : '')/ge;
$sub =~ s/%/~%/g;
print $sub, "\n";
}
}
print "~endgroup\n\\bye\n";
select STDOUT;
system "pdftex $f.tex";
unlink "$f.log";
unless ($opts{p}) {
system "lpr $f.pdf";
print "Delete TeX & PDF files for $f now? (y/n) ";
unlink "$f.tex", "$f.pdf" if <STDIN> =~ /^y/i;
}
}