home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / perl-5.003-bin.lha / bin / pod2latex (.txt) < prev    next >
LaTeX Document  |  1996-10-12  |  22KB  |  529 lines

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