Initial Commit
[packages] / xemacs-packages / auctex / doc / preview-dtxdoc.pl
1 #!/usr/bin/perl
2
3 # Autoconverter from preview.dtx to preview-dtxdoc.texi
4
5 # Author: Jan-Åke Larsson <jalar@mai.liu.se>
6 # Maintainer: auctex-devel@gnu.org
7
8 # Copyright (C) 2002, 2005 Free Software Foundation, Inc.
9
10 # This file is part of AUCTeX.
11
12 # AUCTeX is free software; you can redistribute it and/or modify it
13 # under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 3, or (at your option)
15 # any later version.
16
17 # AUCTeX is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 # General Public License for more details.
21
22 # You should have received a copy of the GNU General Public License
23 # along with AUCTeX; see the file COPYING.  If not, write to the Free
24 # Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 # MA 02110-1301, USA.
26
27 # Commentary:
28
29 # Simpleminded autoconverter from preview.dtx to preview-dtxdoc.texi
30 # run as 'perl preview-dtxdoc.pl ../latex/preview.dtx preview-dtxdoc.texi'
31
32 die "Usage: perl preview-dtxdoc.pl infile outfile" unless ($#ARGV == 1);
33 open(STDIN, $ARGV[0]) || die "Can't open $ARGV[0] for reading";
34 open(STDOUT, "> $ARGV[1]") || die "Can't open $ARGV[1] for writing";
35
36
37 # Eat header 
38 MUNGE: while (<STDIN>) {
39     last MUNGE if /^% *.section/;
40 }
41
42 # Fish out possible CR characters.
43 /(\r*)$/;
44 $cr = $1;
45
46
47 # Noindent is used sometimes after \end{quote} (see below) 
48 $noindent="";
49 # Quote environments is translated into @example _without_
50 # @code{..} inside (see below) 
51 $quote="";
52 MAIN: while (<STDIN>) {
53     s/^%//;
54     s/\\%/%/g;
55
56     # Text-substitution macros
57     s/\@/\@\@/g;
58     s/\\#/#/g;
59     s/AUC~?\\TeX[\\ ]?/\@AUCTeX{}/g;
60     s/\\LaTeX[\\ ]?/\@LaTeX{}/g;
61     s/\\TeX[\\ ]?/\@TeX{}/g;
62     s/\\previewlatex[\\ ]?/\@previewlatex{}/g;
63     s/EPS/\@acronym{EPS}/g;
64     s/DVI/\@acronym{DVI}/g;
65     s/~/\@w{ }/g;
66     s/^ *//;
67     # Environments
68     if (s/\\begin\{quote\}/$cr\n\@example/) { 
69         $quote="yes" }
70     if (/^\w/) { 
71         print $noindent } 
72     $noindent = "";
73     if (s/\\end\{quote\}/\@end example$cr\n/) { 
74         $quote=""; 
75         $noindent="\@noindent$cr\n"  }
76     s/\\begin\{description\}/$cr\n\@table \@w/;
77     # Convoluted pattern: handle 
78     # \item[|...|], \item[\meta{..}] and \item[{|[]|}]
79     s/\\item\[\{?(.+?[\|\}])\}?\] ?/\@item $1$cr\n/;
80     s/\\end\{description\}/\@end table$cr\n/;
81     s/\\begin\{enumerate\}/$cr\n\@enumerate/;
82     s/\\item /\@item /;
83     s/\\end\{enumerate\}/\@end enumerate$cr\n/;
84
85     # Formatting (\cmd is special within {quote})
86     s/\\texttt/\@option/g;
87     s/\\marg\{([^}]+)\}/\@{\@var{$1}\@}/g;
88     s/\\meta/\@var/g;
89     s/\\emph/\@emph/g;
90     s/\\cmd(\\[\(\)\w]+)/|$1|/g;
91     s/\\cmd\{(.*?)\}/|$1|/g;
92     s/\\oarg\{([^}]+?)\}/\[\@var{$1}\]/g;
93     s/\\char.//g;
94     s/\\raggedright$cr\n//g;
95     s/\\DescribeEnv\{(.*?)\} /\@item \\begin\@{$1\@}\@dots{}\\end\@{$1\@}$cr\n/;
96     if (s/\\DescribeMacro\{(.*?)\}( |$cr\n)/\@item $1$cr\n/) {
97         # Index entries for two important macros
98         if (/(\\Preview(Macro|Environment))( |$cr\n)/) {
99             $_ .= "\@findex $1$cr\n";
100         }
101     }
102
103     # ||||||| Hell... I hate bars
104     # Braces WITHIN bars should be escaped like so: @{ @}
105     # and |..| translates to @code{..} or @file{..} depending on content
106     # and to .. if in {quote}
107     @chunks = split /\|/;
108     $odd=0;
109     COMMAND: foreach (@chunks) {
110         if ($odd==0) {
111             $odd=1;
112         } else {
113             s/\{/\@\{/g;
114             s/\}/\@\}/g;
115             if (! $quote) {
116                 if (/[.\/]/) {
117                     $_="\@file\{".$_."\}";
118                 } else {
119                     $_="\@code\{".$_."\}";
120                 }
121             }
122             $odd=0;
123         }
124     }
125     $_=join("",@chunks);
126     # Argh! mixed types occurs in @code{...}@var{..}@file{..}
127     # Should be @file{...@var{..}..}
128     s/\@code(\S*?)\}(\S*)\@file\{/\@file$1$2/g;
129
130     # Texinfo @node-ification
131     if (s/\\section\{(.*)\}/\@subsection $1/) {
132         if (s/[Oo]ptions/options/) {
133             $_="\@menu$cr\n" .
134 "* Package options::$cr\n" .
135 "* Provided commands::$cr\n" .
136 "\@end menu$cr\n$cr\n" .
137 "\@node Package options, Provided commands, The LaTeX style file, The LaTeX style file$cr\n" . $_;
138         } elsif (s/[Cc]ommands/commands/) {
139         # \Describe... needs @table
140             $_= "\@node Provided commands, ,Package options, The LaTeX style file$cr\n" . 
141                 $_ . "$cr\n\@table \@code$cr\n";
142         }
143     }
144         
145     # Stop here
146     # \Describe.... needs @end table
147     if (/^.StopEventually/) {
148         print "\@end table$cr\n";
149         last MAIN;
150     }
151     print $_;
152 }