home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl_utl.zip / pod2latex.cmd < prev    next >
OS/2 REXX Batch file  |  1997-11-28  |  23KB  |  665 lines

  1. extproc perl -S
  2. #!f:/perllib/bin/perl
  3.     eval 'exec f:/perllib/bin/perl -S $0 ${1+"$@"}'
  4.     if $running_under_some_shell;
  5. #
  6. # pod2latex, version 1.1
  7. # by Taro Kawagish (kawagish@imslab.co.jp),  Jan 11, 1995.
  8. #
  9. # pod2latex filters Perl pod documents to LaTeX documents.
  10. #
  11. # What pod2latex does:
  12. # 1. Pod file 'perl_doc_entry.pod' is filtered to 'perl_doc_entry.tex'.
  13. # 2. Indented paragraphs are translated into
  14. #    '\begin{verbatim} ... \end{verbatim}'.
  15. # 3. '=head1 heading' command is translated into '\section{heading}'
  16. # 4. '=head2 heading' command is translated into '\subsection*{heading}'
  17. # 5. '=over N' command is translated into
  18. #        '\begin{itemize}'    if following =item starts with *,
  19. #        '\begin{enumerate}'    if following =item starts with 1.,
  20. #        '\begin{description}'    if else.
  21. #      (indentation level N is ignored.)
  22. # 6. '=item * heading' command is translated into '\item heading',
  23. #    '=item 1. heading' command is translated into '\item heading',
  24. #    '=item heading' command(other) is translated into '\item[heading]'.
  25. # 7. '=back' command is translated into
  26. #        '\end{itemize}'    if started with '\begin{itemize}',
  27. #        '\end{enumerate}'    if started with '\begin{enumerate}',
  28. #        '\end{description}'    if started with '\begin{description}'.
  29. # 8. other paragraphs are translated into strings with TeX special characters
  30. #    escaped.
  31. # 9. In heading text, and other paragraphs, the following translation of pod
  32. #    quotes are done, and then TeX special characters are escaped after that.
  33. #      I<text> to {\em text\/},
  34. #      B<text> to {\bf text},
  35. #      S<text> to text1,
  36. #        where text1 is a string with blank characters replaced with ~,
  37. #      C<text> to {\tt text2},
  38. #        where text2 is a string with TeX special characters escaped to
  39. #        obtain a literal printout,
  40. #      E<text> (HTML escape) to TeX escaped string,
  41. #      L<text> to referencing string as is done by pod2man,
  42. #      F<file> to {\em file\/},
  43. #      Z<> to a null string,
  44. # 10. those headings are indexed:
  45. #       '=head1 heading'   =>  \section{heading}\index{heading}
  46. #       '=head2 heading'   =>  \subsection*{heading}\index{heading}
  47. #                 only when heading does not match frequent patterns such as
  48. #                 DESCRIPTION, DIAGNOSTICS,...
  49. #       '=item heading'   =>  \item{heading}\index{heading}
  50. #
  51. # Usage:
  52. #     pod2latex perl_doc_entry.pod
  53. # this will write to a file 'perl_doc_entry.tex'.
  54. #
  55. # To LaTeX:
  56. # The following commands need to be defined in the preamble of the LaTeX
  57. # document:
  58. # \def\C++{{\rm C\kern-.05em\raise.3ex\hbox{\footnotesize ++}}}
  59. # \def\underscore{\leavevmode\kern.04em\vbox{\hrule width 0.4em height 0.3pt}}
  60. # and \parindent should be set zero:
  61. # \setlength{\parindent}{0pt}
  62. #
  63. # Note:
  64. # This script was written modifing pod2man.
  65. #
  66. # Bug:
  67. # If HTML escapes E<text> other than E<amp>,E<lt>,E<gt>,E<quot> are used
  68. # in C<>, translation will produce wrong character strings.
  69. # Translation of HTML escapes of various European accents might be wrong.
  70.  
  71.  
  72. $/ = "";            # record separator is blank lines
  73. # TeX special characters.
  74. ##$tt_ables = "!@*()-=+|;:'\"`,./?<>";
  75. $backslash_escapables = "#\$%&{}_";
  76. $backslash_escapables2 = "#\$%&{}";    # except _
  77. ##$nonverbables = "^\\~";
  78. ##$bracketesc = "[]";
  79. ##@tex_verb_fences = unpack("aaaaaaaaa","|#@!*+?:;");
  80.  
  81. @head1_freq_patterns        # =head1 patterns which need not be index'ed
  82.     = ("AUTHOR","Author","BUGS","DATE","DESCRIPTION","DIAGNOSTICS",
  83.        "ENVIRONMENT","EXAMPLES","FILES","INTRODUCTION","NAME","NOTE",
  84.        "SEE ALSO","SYNOPSIS","WARNING");
  85.  
  86. $indent = 0;
  87.  
  88. # parse the pods, produce LaTeX.
  89.  
  90. open(POD,"<$ARGV[0]") || die "cant open $ARGV[0]";
  91. ($pod=$ARGV[0]) =~ s/\.pod$//;
  92. open(LATEX,">$pod.tex");
  93. &do_hdr();
  94.  
  95. $cutting = 1;
  96. $begun = "";
  97. while (<POD>) {
  98.     if ($cutting) {
  99.     next unless /^=/;
  100.     $cutting = 0;
  101.     }
  102.     if ($begun) {
  103.        if (/^=end\s+$begun/) {
  104.            $begun = "";
  105.        }
  106.        elsif ($begun =~ /^(tex|latex)$/) {
  107.            print LATEX $_;
  108.        }
  109.        next;
  110.     }
  111.     chop;
  112.     length || (print LATEX  "\n") && next;
  113.  
  114.     # translate indented lines as a verabatim paragraph
  115.     if (/^\s/) {
  116.     @lines = split(/\n/);
  117.     print LATEX  "\\begin{verbatim}\n";
  118.     for (@lines) {
  119.         1 while s
  120.         {^( [^\t]* ) \t ( \t* ) }
  121.         { $1 . ' ' x (8 - (length($1)%8) + 8*(length($2))) }ex;
  122.         print LATEX  $_,"\n";
  123.     }
  124.     print LATEX  "\\end{verbatim}\n";
  125.     next;
  126.     }
  127.  
  128.     if (/^=for\s+(\S+)\s*/s) {
  129.     if ($1 eq "tex" or $1 eq "latex") {
  130.         print LATEX $',"\n";
  131.     } else {
  132.         # ignore unknown for
  133.     }
  134.     next;
  135.     }
  136.     elsif (/^=begin\s+(\S+)\s*/s) {
  137.     $begun = $1;
  138.     if ($1 eq "tex" or $1 eq "latex") {
  139.         print LATEX $'."\n";
  140.     }
  141.     next;
  142.     }
  143.  
  144.     # preserve '=item' line with pod quotes as they are.
  145.     if (/^=item/) {
  146.     ($bareitem = $_) =~ s/^=item\s*//;
  147.     }
  148.  
  149.     # check for things that'll hosed our noremap scheme; affects $_
  150.     &init_noremap();
  151.  
  152.     # expand strings "func()" as pod quotes.
  153.     if (!/^=item/) {
  154.     # first hide pod escapes.
  155.     # escaped strings are mapped into the ones with the MSB's on.
  156.     s/([A-Z]<[^<>]*>)/noremap($1)/ge;
  157.  
  158.     # func() is a reference to a perl function
  159.     s{\b([:\w]+\(\))}{I<$1>}g;
  160.     # func(n) is a reference to a man page
  161.     s{(\w+)(\([^\s,\051]+\))}{I<$1>$2}g;
  162.     # convert simple variable references
  163. #    s/([\$\@%][\w:]+)/C<$1>/g;
  164. #    s/\$[\w:]+\[[0-9]+\]/C<$&>/g;
  165.  
  166.     if (m{ ([\-\w]+\([^\051]*?[\@\$,][^\051]*?\))
  167.            }x && $` !~ /([LCI]<[^<>]*|-)$/ && !/^=\w/)
  168.     {
  169.         warn "``$1'' should be a [LCI]<$1> ref";
  170.     }
  171.     while (/(-[a-zA-Z])\b/g && $` !~ /[\w\-]$/) {
  172.         warn "``$1'' should be [CB]<$1> ref";
  173.     }
  174.  
  175.     # put back pod quotes so we get the inside of <> processed;
  176.     $_ = &clear_noremap($_);
  177.     }
  178.  
  179.  
  180.     # process TeX special characters
  181.  
  182.     # First hide HTML quotes E<> since they can be included in C<>.
  183.     s/(E<[^<>]+>)/noremap($1)/ge;
  184.  
  185.     # Then hide C<> type literal quotes.
  186.     # String inside of C<> will later be expanded into {\tt ..} strings
  187.     # with TeX special characters escaped as needed.
  188.     s/(C<[^<>]*>)/&noremap($1)/ge;
  189.  
  190.     # Next escape TeX special characters including other pod quotes B< >,...
  191.     #
  192.     # NOTE: s/re/&func($str)/e evaluates $str just once in perl5.
  193.     # (in perl4 evaluation takes place twice before getting passed to func().)
  194.  
  195.     # - hyphen => ---
  196.     s/(\S+)(\s+)-+(\s+)(\S+)/"$1".&noremap(" --- ")."$4"/ge;
  197.     # '-', '--', "-"  =>  '{\tt -}', '{\tt --}', "{\tt -}"
  198. ##    s/("|')(\s*)(-+)(\s*)\1/&noremap("$1$2\{\\tt $3\}$4$1")/ge;
  199. ## changed Wed Jan 25 15:26:39 JST 1995
  200.     # '-', '--', "-"  =>  '$-$', '$--$', "$-$"
  201.     s/(\s+)(['"])(-+)([^'"\-]*)\2(\s+|[,.])/"$1$2".&noremap("\$$3\$")."$4$2$5"/ge;
  202.     s/(\s+)(['"])([^'"\-]*)(-+)(\s*)\2(\s+|[,.])/"$1$2$3".&noremap("\$$4\$")."$5$2$6"/ge;
  203.     # (--|-)  =>  ($--$|$-$)
  204.     s/(\s+)\((-+)([=@%\$\+\\\|\w]*)(-*)([=@%\$\+\\\|\w]*)\)(\s+|[,.])/"$1\(".&noremap("\$$2\$")."$3".&noremap("\$$4\$")."$5\)$6"/ge;
  205.     # numeral -  =>  $-$
  206.     s/(\(|[0-9]+|\s+)-(\s*\(?\s*[0-9]+)/&noremap("$1\$-\$$2")/ge;
  207.     # -- in quotes  =>  two separate -
  208.     s/B<([^<>]*)--([^<>]*)>/&noremap("B<$1\{\\tt --\}$2>")/ge;
  209.  
  210.     # backslash escapable characters except _.
  211.     s/([$backslash_escapables2])/&noremap("\\$1")/ge;
  212.     s/_/&noremap("\\underscore{}")/ge;        # a litle thicker than \_.
  213.     # quote TeX special characters |, ^, ~, \.
  214.     s/\|/&noremap("\$|\$")/ge;
  215.     s/\^/&noremap("\$\\hat{\\hspace{0.4em}}\$")/ge;
  216.     s/\~/&noremap("\$\\tilde{\\hspace{0.4em}}\$")/ge;
  217.     s/\\/&noremap("\$\\backslash{}\$")/ge;
  218.     # quote [ and ] to be used in \item[]
  219.     s/([\[\]])/&noremap("{\\tt $1}")/ge;
  220.     # characters need to be treated differently in TeX
  221.     # keep * if an item heading
  222.     s/^(=item[ \t]+)[*]((.|\n)*)/"$1" . &noremap("*") . "$2"/ge;
  223.     s/[*]/&noremap("\$\\ast\$")/ge;    # other *
  224.  
  225.     # hide other pod quotes.
  226.     s/([ABD-Z]<[^<>]*>)/&noremap($1)/ge;
  227.  
  228.     # escape < and > as math strings,
  229.     # now that we are done with hiding pod <> quotes.
  230.     s/</&noremap("\$<\$")/ge;
  231.     s/>/&noremap("\$>\$")/ge;
  232.  
  233.     # put it back so we get the <> processed again;
  234.     $_ = &clear_noremap($_);
  235.  
  236.  
  237.     # Expand pod quotes recursively:
  238.     # (1) type face directives [BIFS]<[^<>]*> to appropriate TeX commands,
  239.     # (2) L<[^<>]*> to reference strings,
  240.     # (3) C<[^<>]*> to TeX literal quotes,
  241.     # (4) HTML quotes E<> inside of C<> quotes.
  242.  
  243.     # Hide E<> again since they can be included in C<>.
  244.     s/(E<[^<>]+>)/noremap($1)/ge;
  245.  
  246.     $maxnest = 10;
  247.     while ($maxnest-- && /[A-Z]</) {
  248.  
  249.     # bold and italic quotes
  250.     s/B<([^<>]*)>/"{\\bf $1}"/eg;
  251.     s#I<([^<>]*)>#"{\\em $1\\/}"#eg;
  252.  
  253.     # files and filelike refs in italics
  254.     s#F<([^<>]*)>#"{\\em $1\\/}"#eg;
  255.  
  256.     # no break quote -- usually we want C<> for this
  257.     s/S<([^<>]*)>/&nobreak($1)/eg;
  258.  
  259.     # LREF: a manpage(3f)
  260.     s:L<([a-zA-Z][^\s\/]+)(\([^\)]+\))?>:the {\\em $1\\/}$2 manpage:g;
  261.  
  262.     # LREF: an =item on another manpage
  263.     s{
  264.         L<([^/]+)/([:\w]+(\(\))?)>
  265.     } {the C<$2> entry in the I<$1> manpage}gx;
  266.  
  267.     # LREF: an =item on this manpage
  268.     s{
  269.        ((?:L</([:\w]+(\(\))?)>
  270.         (,?\s+(and\s+)?)?)+)
  271.     } { &internal_lrefs($1) }gex;
  272.  
  273.     # LREF: a =head2 (head1?), maybe on a manpage, maybe right here
  274.     # the "func" can disambiguate
  275.     s{
  276.         L<(?:([a-zA-Z]\S+?) /)?"?(.*?)"?>
  277.     }{
  278.         do {
  279.         $1     # if no $1, assume it means on this page.
  280.             ?  "the section on I<$2> in the I<$1> manpage"
  281.             :  "the section on I<$2>"
  282.         } 
  283.     }gex;
  284.  
  285.     s/Z<>/\\&/g;        # the "don't format me" thing
  286.  
  287.     # comes last because not subject to reprocessing
  288.     s{
  289.         C<([^<>]*)>
  290.     }{
  291.         do {
  292.         ($str = $1) =~ tr/\200-\377/\000-\177/; #normalize hidden stuff
  293.         # expand HTML escapes if any;
  294.         # WARNING: if HTML escapes other than E<amp>,E<lt>,E<gt>,
  295.         # E<quot> are in C<>, they will not be printed correctly.
  296.         $str = &expand_HTML_escapes($str);
  297.         $strverb = &alltt($str);    # Tex verbatim escape of a string.
  298.         &noremap("$strverb");
  299.         }
  300.     }gex;
  301.  
  302. #    if ( /C<([^<>]*)/ ) {
  303. #        $str = $1;
  304. #        if ($str !~ /\|/) {        # if includes |
  305. #        s/C<([^<>]*)>/&noremap("\\verb|$str|")/eg;
  306. #        } else {
  307. #        print STDERR "found \| in C<.*> at paragraph $.\n";
  308. #        # find a character not contained in $str to use it as a
  309. #        # separator of the \verb
  310. #        ($chars = $str) =~ s/(\W)/\\$1/g;
  311. #        ## ($chars = $str) =~ s/([\$<>,\|"'\-^{}()*+?\\])/\\$1/g;
  312. #        @fence = grep(!/[ $chars]/,@tex_verb_fences);
  313. #        s/C<([^<>]*)>/&noremap("\\verb$fence[0]$str$fence[0]")/eg;
  314. #        }
  315. #    }
  316.     }
  317.  
  318.  
  319.     # process each pod command
  320.     if (s/^=//) {                # if a command
  321.     s/\n/ /g;
  322.     ($cmd, $rest) = split(' ', $_, 2);
  323.     $rest =~ s/^\s*//;
  324.     $rest =~ s/\s*$//;
  325.  
  326.     if (defined $rest) {
  327.         &escapes;
  328.     }
  329.  
  330.     $rest = &clear_noremap($rest);
  331.     $rest = &expand_HTML_escapes($rest);
  332.  
  333.     if ($cmd eq 'cut') {
  334.         $cutting = 1;
  335.         $lastcmd = 'cut';
  336.     }
  337.     elsif ($cmd eq 'head1') {    # heading type 1
  338.         $rest =~ s/^\s*//; $rest =~ s/\s*$//;
  339.         print LATEX  "\n\\subsection*{$rest}";
  340.         # put index entry
  341.         ($index = $rest) =~ s/^(An?\s+|The\s+)//i;    # remove 'A' and 'The'
  342.         # index only those heads not matching the frequent patterns.
  343.         foreach $pat (@head1_freq_patterns) {
  344.         if ($index =~ /^$pat/) {
  345.             goto freqpatt;
  346.         }
  347.         }
  348.         print LATEX  "%\n\\index{$index}\n" if ($index);
  349.       freqpatt:
  350.         $lastcmd = 'head1';
  351.     }
  352.     elsif ($cmd eq 'head2') {    # heading type 2
  353.         $rest =~ s/^\s*//; $rest =~ s/\s*$//;
  354.         print LATEX  "\n\\subsubsection*{$rest}";
  355.         # put index entry
  356.         ($index = $rest) =~ s/^(An?\s+|The\s+)//i;    # remove 'A' and 'The'
  357.         $index =~ s/^Example\s*[1-9][0-9]*\s*:\s*//; # remove 'Example :'
  358.         print LATEX  "%\n\\index{$index}\n"  if ($index);
  359.         $lastcmd = 'head2';
  360.     }
  361.     elsif ($cmd eq 'over') {    # 1 level within a listing environment
  362.         push(@indent,$indent);
  363.         $indent = $rest + 0;
  364.         $lastcmd = 'over';
  365.     }
  366.     elsif ($cmd eq 'back') {    # 1 level out of a listing environment
  367.         $indent = pop(@indent);
  368.         warn "Unmatched =back\n" unless defined $indent;
  369.         $listingcmd = pop(@listingcmd);
  370.         print LATEX  "\n\\end{$listingcmd}\n"  if ($listingcmd);
  371.         $lastcmd = 'back';
  372.     }
  373.     elsif ($cmd eq 'item') {    # an item paragraph starts
  374.         if ($lastcmd eq 'over') {    # if we have just entered listing env
  375.         # see what type of list environment we are in.
  376.         if ($rest =~ /^[0-9]\.?/) {    # if numeral heading
  377.             $listingcmd = 'enumerate';
  378.         } elsif ($rest =~ /^\*\s*/) {    # if * heading
  379.             $listingcmd = 'itemize';
  380.         } elsif ($rest =~ /^[^*]/) {    # if other headings
  381.             $listingcmd = 'description';
  382.         } else {
  383.             warn "unknown list type for item $rest";
  384.         }
  385.         print LATEX  "\n\\begin{$listingcmd}\n";
  386.         push(@listingcmd,$listingcmd);
  387.         } elsif ($lastcmd ne 'item') {
  388.         warn "Illegal '=item' command without preceding 'over':";
  389.         warn "=item $bareitem";
  390.         }
  391.  
  392.         if ($listingcmd eq 'enumerate') {
  393.         $rest =~ s/^[0-9]+\.?\s*//;    # remove numeral heading
  394.         print LATEX  "\n\\item";
  395.         print LATEX  "{\\bf $rest}" if $rest;
  396.         } elsif ($listingcmd eq 'itemize') {
  397.         $rest =~ s/^\*\s*//;        # remove * heading
  398.         print LATEX  "\n\\item";
  399.         print LATEX  "{\\bf $rest}" if $rest;
  400.         } else {                # description item
  401.         print LATEX  "\n\\item[$rest]";
  402.         }
  403.         $lastcmd = 'item';
  404.         $rightafter_item = 'yes';
  405.  
  406.         # check if the item heading is short or long.
  407.         ($itemhead = $rest) =~ s/{\\bf (\S*)}/$1/g;
  408.         if (length($itemhead) < 4) {
  409.         $itemshort = "yes";
  410.         } else {
  411.         $itemshort = "no";
  412.         }
  413.         # write index entry
  414.         if ($pod =~ "perldiag") {            # skip 'perldiag.pod'
  415.         goto noindex;
  416.         }
  417.         # strip out the item of pod quotes and get a plain text entry
  418.         $bareitem =~ s/\n/ /g;            # remove newlines
  419.         $bareitem =~ s/\s*$//;            # remove trailing space
  420.         $bareitem =~ s/[A-Z]<([^<>]*)>/$1/g;    # remove <> quotes
  421.         ($index = $bareitem) =~ s/^\*\s+//;        # remove leading '*'
  422.         $index =~ s/^(An?\s+|The\s+)//i;        # remove 'A' and 'The'
  423.         $index =~ s/^\s*[1-9][0-9]*\s*[.]\s*$//; # remove numeral only
  424.         $index =~ s/^\s*\w\s*$//;            # remove 1 char only's
  425.         # quote ", @ and ! with " to be used in makeindex.
  426.         $index =~ s/"/""/g;                # quote "
  427.         $index =~ s/@/"@/g;                # quote @
  428.         $index =~ s/!/"!/g;                # quote !
  429.         ($rest2=$rest) =~ s/^\*\s+//;    # remove *
  430.         $rest2 =~ s/"/""/g;                # quote "
  431.         $rest2 =~ s/@/"@/g;                # quote @
  432.         $rest2 =~ s/!/"!/g;                # quote !
  433.         if ($pod =~ "(perlfunc|perlvar)") {    # when doc is perlfunc,perlvar
  434.         # take only the 1st word of item heading
  435.         $index =~ s/^([^{}\s]*)({.*})?([^{}\s]*)\s+.*/\1\2\3/;
  436.         $rest2 =~ s/^([^{}\s]*)({.*})?([^{}\s]*)\s+.*/\1\2\3/;
  437.         }
  438.         if ($index =~ /[A-Za-z\$@%]/) {
  439.             #  write  \index{plain_text_entry@TeX_string_entry}
  440.         print LATEX  "%\n\\index{$index\@$rest2}%\n";
  441.         }
  442.       noindex:
  443.         ;
  444.     }
  445.     else {
  446.         warn "Unrecognized directive: $cmd\n";
  447.     }
  448.     }
  449.     else {                    # if not command
  450.     &escapes;
  451.     $_ = &clear_noremap($_);
  452.     $_ = &expand_HTML_escapes($_);
  453.  
  454.     # if the present paragraphs follows an =item declaration,
  455.     # put a line break.
  456.     if ($lastcmd eq 'item' &&
  457.         $rightafter_item eq 'yes' && $itemshort eq "no") {
  458.         print LATEX  "\\hfil\\\\";
  459.         $rightafter_item = 'no';
  460.     }
  461.     print LATEX  "\n",$_;
  462.     }
  463. }
  464.  
  465. print LATEX  "\n";
  466. close(POD);
  467. close(LATEX);
  468.  
  469.  
  470. #########################################################################
  471.  
  472. sub do_hdr {
  473.     print LATEX "% LaTeX document produced by pod2latex from \"$pod.pod\".\n";
  474.     print LATEX "% The followings need be defined in the preamble of this document:\n";
  475.     print LATEX "%\\def\\C++{{\\rm C\\kern-.05em\\raise.3ex\\hbox{\\footnotesize ++}}}\n";
  476.     print LATEX "%\\def\\underscore{\\leavevmode\\kern.04em\\vbox{\\hrule width 0.4em height 0.3pt}}\n";
  477.     print LATEX "%\\setlength{\\parindent}{0pt}\n";
  478.     print LATEX "\n";
  479.     $podq = &escape_tex_specials("\U$pod\E");
  480.     print LATEX "\\section{$podq}%\n";
  481.     print LATEX "\\index{$podq}";
  482.     print LATEX "\n";
  483. }
  484.  
  485. sub nobreak {
  486.     my $string = shift;
  487.     $string =~ s/ +/~/g;        # TeX no line break
  488.     $string;
  489. }
  490.  
  491. sub noremap {
  492.     local($thing_to_hide) = shift;
  493.     $thing_to_hide =~ tr/\000-\177/\200-\377/;
  494.     return $thing_to_hide;
  495. }
  496.  
  497. sub init_noremap {
  498.     # escape high bit characters in input stream
  499.     s/([\200-\377])/"E<".ord($1).">"/ge;
  500. }
  501.  
  502. sub clear_noremap {
  503.     local($tmp) = shift;
  504.     $tmp =~ tr/\200-\377/\000-\177/;
  505.     return $tmp;
  506. }
  507.  
  508. sub expand_HTML_escapes {
  509.     local($s) = $_[0];
  510.     $s =~ s { E<((\d+)|([A-Za-z]+))> }
  511.     {
  512.     do {
  513.         defined($2) 
  514.         ? do { chr($2) }
  515.         : 
  516.         exists $HTML_Escapes{$3}
  517.         ? do { $HTML_Escapes{$3} }
  518.         : do {
  519.         warn "Unknown escape: $& in $_";
  520.         "E<$1>";
  521.         }
  522.     }
  523.     }egx;
  524.     return $s;
  525. }
  526.  
  527. sub escapes {
  528.     # make C++ into \C++, which is to be defined as
  529.     # \def\C++{{\rm C\kern-.05em\raise.3ex\hbox{\footnotesize ++}}}
  530.     s/\bC\+\+/\\C++{}/g;
  531. }
  532.  
  533. # Translate a string into a TeX \tt string to obtain a verbatim print out.
  534. # TeX special characters are escaped by \.
  535. # This can be used inside of LaTeX command arguments.
  536. # We don't use LaTeX \verb since it doesn't work inside of command arguments.
  537. sub alltt {
  538.     local($str) = shift;
  539.     # other chars than #,\,$,%,&,{,},_,\,^,~ ([ and ] included).
  540.     $str =~ s/([^${backslash_escapables}\\\^\~]+)/&noremap("$&")/eg;
  541.     # chars #,\,$,%,&,{,}  =>  \# , ...
  542.     $str =~ s/([$backslash_escapables2])/&noremap("\\$&")/eg;
  543.     # chars _,\,^,~  =>  \char`\_ , ...
  544.     $str =~ s/_/&noremap("\\char`\\_")/eg;
  545.     $str =~ s/\\/&noremap("\\char`\\\\")/ge;
  546.     $str =~ s/\^/\\char`\\^/g;
  547.     $str =~ s/\~/\\char`\\~/g;
  548.  
  549.     $str =~ tr/\200-\377/\000-\177/;        # put back
  550.     $str = "{\\tt ".$str."}";            # make it a \tt string
  551.     return $str;
  552. }
  553.  
  554. sub escape_tex_specials {
  555.     local($str) = shift;
  556.     # other chars than #,\,$,%,&,{,},  _,\,^,~ ([ and ] included).
  557.     # backslash escapable characters #,\,$,%,&,{,} except _.
  558.     $str =~ s/([$backslash_escapables2])/&noremap("\\$1")/ge;
  559.     $str =~ s/_/&noremap("\\underscore{}")/ge;    # \_ is too thin.
  560.     # quote TeX special characters |, ^, ~, \.
  561.     $str =~ s/\|/&noremap("\$|\$")/ge;
  562.     $str =~ s/\^/&noremap("\$\\hat{\\hspace{0.4em}}\$")/ge;
  563.     $str =~ s/\~/&noremap("\$\\tilde{\\hspace{0.4em}}\$")/ge;
  564.     $str =~ s/\\/&noremap("\$\\backslash{}\$")/ge;
  565.     # characters need to be treated differently in TeX
  566.     # *
  567.     $str =~ s/[*]/&noremap("\$\\ast\$")/ge;
  568.     # escape < and > as math string,
  569.     $str =~ s/</&noremap("\$<\$")/ge;
  570.     $str =~ s/>/&noremap("\$>\$")/ge;
  571.     $str =~ tr/\200-\377/\000-\177/;        # put back
  572.     return $str;
  573. }
  574.  
  575. sub internal_lrefs {
  576.     local($_) = shift;
  577.  
  578.     s{L</([^>]+)>}{$1}g;
  579.     my(@items) = split( /(?:,?\s+(?:and\s+)?)/ );
  580.     my $retstr = "the ";
  581.     my $i;
  582.     for ($i = 0; $i <= $#items; $i++) {
  583.     $retstr .= "C<$items[$i]>";
  584.     $retstr .= ", " if @items > 2 && $i != $#items;
  585.     $retstr .= " and " if $i+2 == @items;
  586.     }
  587.     $retstr .= " entr" . ( @items > 1  ? "ies" : "y" )
  588.         .  " elsewhere in this document";
  589.  
  590.     return $retstr;
  591. }
  592.  
  593. # map of HTML escapes to TeX escapes.
  594. BEGIN {
  595. %HTML_Escapes = (
  596.     'amp'    =>    '&',    #   ampersand
  597.     'lt'    =>    '<',    #   left chevron, less-than
  598.     'gt'    =>    '>',    #   right chevron, greater-than
  599.     'quot'    =>    '"',    #   double quote
  600.  
  601.     "Aacute"    =>    "\\'{A}",    #   capital A, acute accent
  602.     "aacute"    =>    "\\'{a}",    #   small a, acute accent
  603.     "Acirc"    =>    "\\^{A}",    #   capital A, circumflex accent
  604.     "acirc"    =>    "\\^{a}",    #   small a, circumflex accent
  605.     "AElig"    =>    '\\AE',        #   capital AE diphthong (ligature)
  606.     "aelig"    =>    '\\ae',        #   small ae diphthong (ligature)
  607.     "Agrave"    =>    "\\`{A}",    #   capital A, grave accent
  608.     "agrave"    =>    "\\`{a}",    #   small a, grave accent
  609.     "Aring"    =>    '\\u{A}',    #   capital A, ring
  610.     "aring"    =>    '\\u{a}',    #   small a, ring
  611.     "Atilde"    =>    '\\~{A}',    #   capital A, tilde
  612.     "atilde"    =>    '\\~{a}',    #   small a, tilde
  613.     "Auml"    =>    '\\"{A}',    #   capital A, dieresis or umlaut mark
  614.     "auml"    =>    '\\"{a}',    #   small a, dieresis or umlaut mark
  615.     "Ccedil"    =>    '\\c{C}',    #   capital C, cedilla
  616.     "ccedil"    =>    '\\c{c}',    #   small c, cedilla
  617.     "Eacute"    =>    "\\'{E}",    #   capital E, acute accent
  618.     "eacute"    =>    "\\'{e}",    #   small e, acute accent
  619.     "Ecirc"    =>    "\\^{E}",    #   capital E, circumflex accent
  620.     "ecirc"    =>    "\\^{e}",    #   small e, circumflex accent
  621.     "Egrave"    =>    "\\`{E}",    #   capital E, grave accent
  622.     "egrave"    =>    "\\`{e}",    #   small e, grave accent
  623.     "ETH"    =>    '\\OE',        #   capital Eth, Icelandic
  624.     "eth"    =>    '\\oe',        #   small eth, Icelandic
  625.     "Euml"    =>    '\\"{E}',    #   capital E, dieresis or umlaut mark
  626.     "euml"    =>    '\\"{e}',    #   small e, dieresis or umlaut mark
  627.     "Iacute"    =>    "\\'{I}",    #   capital I, acute accent
  628.     "iacute"    =>    "\\'{i}",    #   small i, acute accent
  629.     "Icirc"    =>    "\\^{I}",    #   capital I, circumflex accent
  630.     "icirc"    =>    "\\^{i}",    #   small i, circumflex accent
  631.     "Igrave"    =>    "\\`{I}",    #   capital I, grave accent
  632.     "igrave"    =>    "\\`{i}",    #   small i, grave accent
  633.     "Iuml"    =>    '\\"{I}',    #   capital I, dieresis or umlaut mark
  634.     "iuml"    =>    '\\"{i}',    #   small i, dieresis or umlaut mark
  635.     "Ntilde"    =>    '\\~{N}',    #   capital N, tilde
  636.     "ntilde"    =>    '\\~{n}',    #   small n, tilde
  637.     "Oacute"    =>    "\\'{O}",    #   capital O, acute accent
  638.     "oacute"    =>    "\\'{o}",    #   small o, acute accent
  639.     "Ocirc"    =>    "\\^{O}",    #   capital O, circumflex accent
  640.     "ocirc"    =>    "\\^{o}",    #   small o, circumflex accent
  641.     "Ograve"    =>    "\\`{O}",    #   capital O, grave accent
  642.     "ograve"    =>    "\\`{o}",    #   small o, grave accent
  643.     "Oslash"    =>    "\\O",        #   capital O, slash
  644.     "oslash"    =>    "\\o",        #   small o, slash
  645.     "Otilde"    =>    "\\~{O}",    #   capital O, tilde
  646.     "otilde"    =>    "\\~{o}",    #   small o, tilde
  647.     "Ouml"    =>    '\\"{O}',    #   capital O, dieresis or umlaut mark
  648.     "ouml"    =>    '\\"{o}',    #   small o, dieresis or umlaut mark
  649.     "szlig"    =>    '\\ss',        #   small sharp s, German (sz ligature)
  650.     "THORN"    =>    '\\L',        #   capital THORN, Icelandic
  651.     "thorn"    =>    '\\l',,        #   small thorn, Icelandic
  652.     "Uacute"    =>    "\\'{U}",    #   capital U, acute accent
  653.     "uacute"    =>    "\\'{u}",    #   small u, acute accent
  654.     "Ucirc"    =>    "\\^{U}",    #   capital U, circumflex accent
  655.     "ucirc"    =>    "\\^{u}",    #   small u, circumflex accent
  656.     "Ugrave"    =>    "\\`{U}",    #   capital U, grave accent
  657.     "ugrave"    =>    "\\`{u}",    #   small u, grave accent
  658.     "Uuml"    =>    '\\"{U}',    #   capital U, dieresis or umlaut mark
  659.     "uuml"    =>    '\\"{u}',    #   small u, dieresis or umlaut mark
  660.     "Yacute"    =>    "\\'{Y}",    #   capital Y, acute accent
  661.     "yacute"    =>    "\\'{y}",    #   small y, acute accent
  662.     "yuml"    =>    '\\"{y}',    #   small y, dieresis or umlaut mark
  663. );
  664. }
  665.