Final changes for 4.2.7. Corrected the previous fix for texi2man bug
[xboard.git] / texi2man
1 #!/usr/bin/perl
2 # $Id$
3 # Written by Adrian Mariano, additional features by Eric Backus,
4 # yet more features by Tim Mann.
5
6 # Script to translate a texinfo file into an nroff manual page.
7
8 $version="1.01.tpm1";
9
10 $html=0;
11 $ignore=0;
12 $tex=0;
13 $doman=0;
14 $title=0;
15 $diditem=0;
16 $justdidlp=1;
17 $noman=0;
18 $info=0;
19 $manprefix="";
20 $args=($#ARGV < 0) ? "stdin" : "@ARGV";
21 $itemxcomma=0;
22
23 printf(".\\\"Do not edit this file.  It was created from %s\n", $args);
24 printf(".\\\"using texi2man version %s on %s", $version, `date`);
25 printf(".\\\"If you want a typeset version, you will get better\n");
26 printf(".\\\"results with the original file.\n.\\\"\n");
27
28 sub parse
29 {
30     if (s/\@c man //) { print; next; }
31     if (/\@c noman/) { $noman=1; next; }
32     if (/\@c end noman/) { $noman=0; next; }
33     if ($noman) { next; }
34
35     if (/\@c ifman\s*(.*)/) { $doman=1; $manprefix = $1; next; }
36     if (/\@c end ifman/) { $doman=0; $manprefix = ""; next; }
37
38     if (/^\\input/) { next; }
39     if (/^\*/) { next; }
40     if (/^INFO-DIR-SECTION/) { next; }
41     if (/^START-INFO-DIR-ENTRY/) { next; }
42     if (/^END-INFO-DIR-ENTRY/) { next; }
43
44     if (/\@titlepage/) { $title=1; next; }
45     if (/\@end titlepage/) { $title=0; next; }
46     if (/\@tex/) { $tex=1; next; }
47     if (/\@end tex/) { $tex=0; next; }
48     if (/\@ignore/) { $ignore=1; next; }
49     if (/\@end ignore/) { $ignore=0; next; }
50     if (/\@ifinfo/) { $info=1; next; }
51     if (/\@end ifinfo/) { $info=0; next; }
52     if (/\@ifhtml/) { $html=1; next; }
53     if (/\@end ifhtml/) { $html=0; next; }
54     if (/\@ifnothtml/) { next; }
55     if (/\@end ifhtml/) { next; }
56     if (/\@iftex/) { $tex=1; next; }
57     if (/\@end iftex/) { $tex=0; next; }
58     if (/\@ifnottex/) { next; }
59     if (/\@end iftex/) { next; }
60     if (!$doman && ($ignore || $html || $title || $tex || $info)) { next; }
61
62     s/\\/\\\\/g;
63     s/\@-/\\-/g;
64     s/\@cite\{([^}]*)}/\`$1\'/g;
65     s/\@code\{([^}]*)}/\`$1\'/g;
66     s/\@email\{([^}]*)}/\`$1\'/g;
67     s/\@file\{([^}]*)}/\`$1\'/g;
68     s/\@kbd\{([^}]*)}/\`$1\'/g;
69     s/\@samp\{([^}]*)}/\`$1\'/g;
70     s/\@url\{([^}]*)}/\`$1\'/g;
71     s/\@dfn\{([^}]*)}/\"$1\"/g;
72     s/\@key\{([^}]*)}/<$1>/g;
73     s/\@emph\{([^}]*)}/\\fI$1\\fR/g;
74     s/\@strong\{([^}]*)}/\\fB$1\\fR/g;
75     s/\@var\{([^}]*)}/\U$1\E/g;
76     s/\@sc\{([^}]*)}/\U$1\E/g;
77     s/\@w\{([^}]*)}/$1/g;
78     s/\@pxref\{([^}]*)}/See \\fI$1\\fR/g;
79     s/\@xref\{([^}]*)}/See \\fI$1\\fR/g;
80     s/\@ref\{([^}]*)}/\\fI$1\\fR/g;
81     s/\@footnote\{([^}]*)}/[$1]/g;
82     s/\@minus\{}/-/g;
83     s/\@copyright\{}/(C)/g;
84     s/\@noindent//;
85     s/\@\{/{/g;
86     s/\@}/}/g;
87     s/\@\@/@/g;
88     s/\\'\\'/\"/g;
89     s/\\`\\`/\"/g;
90     s/---/ -- /g;
91
92     s/\@value\{([^\s]+)}/$value{$1}/eg;
93     if (/\@set\s+([^\s]+)\s+(.*)$/) { $value{$1} = $2; next; }
94     if (/\@clear\s+([^\s]+)\s+(.*)$/) { delete $value{$1}; next; }
95
96     if ($itemxcomma) {
97         # If multiple items have the same description, put the items
98         # on one line, separated by commas
99         if (/\@itemx (.*)/) { printf(", $1"); $diditem=1; next; }
100         elsif ($diditem) { printf("\n"); $diditem=0; }
101         if (/\@item (.*)/) {
102             printf("%s.TP\n%s.B $1", $manprefix, $manprefix);
103             $diditem=1;
104             next;
105         }
106     } else {
107         # If multiple items have the same description, put the items
108         # on separate lines
109         if (/\@item (.*)/) {
110             printf("%s.TP\n%s.B $1\n", $manprefix, $manprefix);
111             next;
112         }
113         if (/\@itemx (.*)/) {
114             printf("%s.PD 0\n%s.TP\n%s.B $1\n%s.PD\n",
115                    $manprefix, $manprefix, $manprefix, $manprefix);
116             next;
117         }
118     }
119
120     if (s/\@chapter (.*)/.SH \U$1\E/)
121     {
122         printf("%s%s", $manprefix, $_);
123         $justdidlp=1;
124         next;
125     }
126     if (s/\@unnumbered (.*)/.SH \U$1\E/)
127     {
128         printf("%s%s", $manprefix, $_);
129         $justdidlp=1;
130         next;
131     }
132     if (s/\@section (.*)/$1/)
133     {
134         printf("%s.SS %s", $manprefix, $_);
135         $justdidlp=1;
136         next;
137     }
138
139     if (/\@example/) {
140         printf("%s.nf\n", $manprefix);
141         $manprefix = $manprefix . "    ";
142         $example=1;
143         next;
144     }
145     if (/\@end example/) {
146         $manprefix = substr($manprefix, 0, -4);
147         printf("%s.fi\n", $manprefix);
148         $example=0;
149         next;
150     }
151
152     if (/\@display/) { printf("%s.nf\n", $manprefix); $example=1; next; }
153     if (/\@end display/) { printf("%s.fi\n", $manprefix); $example=0; next; }
154     if (/\@format/) { printf("%s.nf\n", $manprefix); $example=1; next; }
155     if (/\@end format/) { printf("%s.fi\n", $manprefix); $example=0; next; }
156     if (/\@smallexample/) { printf("%s.nf\n", $manprefix); $example=1; next; }
157     if (/\@end smallexample/) { printf("%s.fi\n", $manprefix); $example=0; next; }
158     if (!$example && /^\s*$/ && !$doman)
159     {
160         if ($justdidlp) { next; }
161 #       printf(".PP\n");
162         printf("\n");
163         $justdidlp=1;
164         next;
165     }
166     if (/\@end table/) {
167         printf("%s.PP\n", $manprefix);
168         $justdidlp=1;
169     }
170
171     if (/\@include (.*)/) {
172         my $INCL;
173         open($INCL, $1) or open($INCL, "../$1") or die "$1: $!";
174         while (<$INCL>) {
175             parse();
176         }
177         next;
178     }
179
180     if (/^\@/) { next; }
181
182     printf("%s%s", $manprefix, $_);
183
184     if (!$doman) { $justdidlp=0; }
185 }
186
187 while(<>)
188 {
189     parse();
190 }