home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-bin / bin / pod2latex < prev    next >
Encoding:
Text File  |  1996-10-12  |  21.2 KB  |  636 lines

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