home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BURKS 2
/
BURKS_AUG97.ISO
/
SLAKWARE
/
D12
/
PERL1.TGZ
/
perl1.tar
/
usr
/
bin
/
pod2latex
(
.txt
)
< prev
next >
Wrap
LaTeX Document
|
1996-06-28
|
22KB
|
529 lines
#!/usr/bin/perl
eval 'exec perl -S $0 "$@"'
if 0;
# pod2latex, version 1.1
# by Taro Kawagish (kawagish@imslab.co.jp), Jan 11, 1995.
# pod2latex filters Perl pod documents to LaTeX documents.
# What pod2latex does:
# 1. Pod file 'perl_doc_entry.pod' is filtered to 'perl_doc_entry.tex'.
# 2. Indented paragraphs are translated into
# '\begin{verbatim} ... \end{verbatim}'.
# 3. '=head1 heading' command is translated into '\section{heading}'
# 4. '=head2 heading' command is translated into '\subsection*{heading}'
# 5. '=over N' command is translated into
# '\begin{itemize}' if following =item starts with *,
# '\begin{enumerate}' if following =item starts with 1.,
# '\begin{description}' if else.
# (indentation level N is ignored.)
# 6. '=item * heading' command is translated into '\item heading',
# '=item 1. heading' command is translated into '\item heading',
# '=item heading' command(other) is translated into '\item[heading]'.
# 7. '=back' command is translated into
# '\end{itemize}' if started with '\begin{itemize}',
# '\end{enumerate}' if started with '\begin{enumerate}',
# '\end{description}' if started with '\begin{description}'.
# 8. other paragraphs are translated into strings with TeX special characters
# escaped.
# 9. In heading text, and other paragraphs, the following translation of pod
# quotes are done, and then TeX special characters are escaped after that.
# I<text> to {\em text\/},
# B<text> to {\bf text},
# S<text> to text1,
# where text1 is a string with blank characters replaced with ~,
# C<text> to {\tt text2},
# where text2 is a string with TeX special characters escaped to
# obtain a literal printout,
# E<text> (HTML escape) to TeX escaped string,
# L<text> to referencing string as is done by pod2man,
# F<file> to {\em file\/},
# Z<> to a null string,
# 10. those headings are indexed:
# '=head1 heading' => \section{heading}\index{heading}
# '=head2 heading' => \subsection*{heading}\index{heading}
# only when heading does not match frequent patterns such as
# DESCRIPTION, DIAGNOSTICS,...
# '=item heading' => \item{heading}\index{heading}
# Usage:
# pod2latex perl_doc_entry.pod
# this will write to a file 'perl_doc_entry.tex'.
# To LaTeX:
# The following commands need to be defined in the preamble of the LaTeX
# document:
# \def\C++{{\rm C\kern-.05em\raise.3ex\hbox{\footnotesize ++}}}
# \def\underscore{\leavevmode\kern.04em\vbox{\hrule width 0.4em height 0.3pt}}
# and \parindent should be set zero:
# \setlength{\parindent}{0pt}
# Note:
# This script was written modifing pod2man.
# Bug:
# If HTML escapes E<text> other than E<amp>,E<lt>,E<gt>,E<quot> are used
# in C<>, translation will produce wrong character strings.
# Translation of HTML escapes of various European accents might be wrong.
$/ = ""; # record separator is blank lines
# TeX special characters.
##$tt_ables = "!@*()-=+|;:'\"`,./?<>";
$backslash_escapables = "#\$%&{}_";
$backslash_escapables2 = "#\$%&{}"; # except _
##$nonverbables = "^\\~";
##$bracketesc = "[]";
##@tex_verb_fences = unpack("aaaaaaaaa","|#@!*+?:;");
@head1_freq_patterns # =head1 patterns which need not be index'ed
= ("AUTHOR","Author","BUGS","DATE","DESCRIPTION","DIAGNOSTICS",
"ENVIRONMENT","EXAMPLES","FILES","INTRODUCTION","NAME","NOTE",
"SEE ALSO","SYNOPSIS","WARNING");
$indent = 0;
# parse the pods, produce LaTeX.
open(POD,"<$ARGV[0]") || die "cant open $ARGV[0]";
($pod=$ARGV[0]) =~ s/\.pod$//;
open(LATEX,">$pod.tex");
&do_hdr();
$cutting = 1;
while (<POD>) {
if ($cutting) {
next unless /^=/;
$cutting = 0;
}
chop;
length || (print LATEX "\n") && next;
# translate indented lines as a verabatim paragraph
if (/^\s/) {
@lines = split(/\n/);
print LATEX "\\begin{verbatim}\n";
for (@lines) {
1 while s
{^( [^\t]* ) \t ( \t* ) }
{ $1 . ' ' x (8 - (length($1)%8) + 8*(length($2))) }ex;
print LATEX $_,"\n";
print LATEX "\\end{verbatim}\n";
next;
}
# preserve '=item' line with pod quotes as they are.
if (/^=item/) {
($bareitem = $_) =~ s/^=item\s*//;
}
# check for things that'll hosed our noremap scheme; affects $_
&init_noremap();
# expand strings "func()" as pod quotes.
if (!/^=item/) {
# first hide pod escapes.
# escaped strings are mapped into the ones with the MSB's on.
s/([A-Z]<[^<>]*>)/noremap($1)/ge;
# func() is a reference to a perl function
s{\b([:\w]+\(\))}{I<$1>}g;
# func(n) is a reference to a man page
s{(\w+)(\([^\s,\051]+\))}{I<$1>$2}g;
# convert simple variable references
# s/([\$\@%][\w:]+)/C<$1>/g;
# s/\$[\w:]+\[[0-9]+\]/C<$&>/g;
if (m{ ([\-\w]+\([^\051]*?[\@\$,][^\051]*?\))
}x && $` !~ /([LCI]<[^<>]*|-)$/ && !/^=\w/)
warn "``$1'' should be a [LCI]<$1> ref";
while (/(-[a-zA-Z])\b/g && $` !~ /[\w\-]$/) {
warn "``$1'' should be [CB]<$1> ref";
# put back pod quotes so we get the inside of <> processed;
$_ = &clear_noremap($_);
}
# process TeX special characters
# First hide HTML quotes E<> since they can be included in C<>.
s/(E<[^<>]+>)/noremap($1)/ge;
# Then hide C<> type literal quotes.
# String inside of C<> will later be expanded into {\tt ..} strings
# with TeX special characters escaped as needed.
s/(C<[^<>]*>)/&noremap($1)/ge;
# Next escape TeX special characters including other pod quotes B< >,...
#
# NOTE: s/re/&func($str)/e evaluates $str just once in perl5.
# (in perl4 evaluation takes place twice before getting passed to func().)
# - hyphen => ---
s/(\S+)(\s+)-+(\s+)(\S+)/"$1".&noremap(" --- ")."$4"/ge;
# '-', '--', "-" => '{\tt -}', '{\tt --}', "{\tt -}"
## s/("|')(\s*)(-+)(\s*)\1/&noremap("$1$2\{\\tt $3\}$4$1")/ge;
## changed Wed Jan 25 15:26:39 JST 1995
# '-', '--', "-" => '$-$', '$--$', "$-$"
s/(\s+)(['"])(-+)([^'"\-]*)\2(\s+|[,.])/"$1$2".&noremap("\$$3\$")."$4$2$5"/ge;
s/(\s+)(['"])([^'"\-]*)(-+)(\s*)\2(\s+|[,.])/"$1$2$3".&noremap("\$$4\$")."$5$2$6"/ge;
# (--|-) => ($--$|$-$)
s/(\s+)\((-+)([=@%\$\+\\\|\w]*)(-*)([=@%\$\+\\\|\w]*)\)(\s+|[,.])/"$1\(".&noremap("\$$2\$")."$3".&noremap("\$$4\$")."$5\)$6"/ge;
# numeral - => $-$
s/(\(|[0-9]+|\s+)-(\s*\(?\s*[0-9]+)/&noremap("$1\$-\$$2")/ge;
# -- in quotes => two separate -
s/B<([^<>]*)--([^<>]*)>/&noremap("B<$1\{\\tt --\}$2>")/ge;
# backslash escapable characters except _.
s/([$backslash_escapables2])/&noremap("\\$1")/ge;
s/_/&noremap("\\underscore{}")/ge; # a litle thicker than \_.
# quote TeX special characters |, ^, ~, \.
s/\|/&noremap("\$|\$")/ge;
s/\^/&noremap("\$\\hat{\\hspace{0.4em}}\$")/ge;
s/\~/&noremap("\$\\tilde{\\hspace{0.4em}}\$")/ge;
s/\\/&noremap("\$\\backslash{}\$")/ge;
# quote [ and ] to be used in \item[]
s/([\[\]])/&noremap("{\\tt $1}")/ge;
# characters need to be treated differently in TeX
# keep * if an item heading
s/^(=item[ \t]+)[*]((.|\n)*)/"$1" . &noremap("*") . "$2"/ge;
s/[*]/&noremap("\$\\ast\$")/ge; # other *
# hide other pod quotes.
s/([ABD-Z]<[^<>]*>)/&noremap($1)/ge;
# escape < and > as math strings,
# now that we are done with hiding pod <> quotes.
s/</&noremap("\$<\$")/ge;
s/>/&noremap("\$>\$")/ge;
# put it back so we get the <> processed again;
$_ = &clear_noremap($_);
# Expand pod quotes recursively:
# (1) type face directives [BIFS]<[^<>]*> to appropriate TeX commands,
# (2) L<[^<>]*> to reference strings,
# (3) C<[^<>]*> to TeX literal quotes,
# (4) HTML quotes E<> inside of C<> quotes.
# Hide E<> again since they can be included in C<>.
s/(E<[^<>]+>)/noremap($1)/ge;
$maxnest = 10;
while ($maxnest-- && /[A-Z]</) {
# bold and italic quotes
s/B<([^<>]*)>/"{\\bf $1}"/eg;
s#I<([^<>]*)>#"{\\em $1\\/}"#eg;
# files and filelike refs in italics
s#F<([^<>]*)>#"{\\em $1\\/}"#eg;
# no break quote -- usually we want C<> for this
s/S<([^<>]*)>/&nobreak($1)/eg;
# LREF: a manpage(3f)
s:L<([a-zA-Z][^\s\/]+)(\([^\)]+\))?>:the {\\em $1\\/}$2 manpage:g;