changes in asynchronous documentation
[gnutls:gnutls.git] / doc / scripts / mytexi2latex
1 eval '(exit $?0)' && eval 'exec perl -wST "$0" ${1+"$@"}'
2   & eval 'exec perl -wST "$0" $argv:q'
3     if 0;
4
5 # Copyright (C) 2011-2012 Free Software Foundation, Inc.
6 #
7 # This file is part of GnuTLS.
8 #
9 # This file is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This file is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this file; if not, write to the Free Software Foundation,
21 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22
23 use constant NORMAL => 0;
24 use constant SKIP => 1;
25 use constant ITEMIZE => 2;
26 use constant VERBATIM => 3;
27 use constant ENUMERATE => 4;
28 use constant TABLE_ITEMIZE => 5;
29 use constant MULTITABLE => 6;
30 use constant EXAMPLE => 7;
31 use constant SMALL_EXAMPLE => 8;
32 use constant QUOTE => 9;
33 use constant FLOAT => 10;
34 use constant FLOAT_TABLE => 11;
35 use constant MULTITABLE_INIT => 12;
36
37 sub unescape()
38 {
39 my $prefix=$_[0];
40 my $suffix=$_[1];
41         $suffix =~ s/\\//g;
42         return "$prefix\{$suffix\}";
43 }
44
45 sub funcref()
46 {
47 my $prefix = $_[0];
48 my $suffix=$_[0];
49         $suffix =~ s/\\//g;
50         $prefix =~ s/\\_/\\_\\-/g;
51
52         return "\\funcref\{$prefix\}\{$suffix\}";
53 }
54
55 sub showfunc()
56 {
57 my $prefix = $_[0];
58 my $suffix = $_[1];
59         $suffix =~ s/\\//g;
60         $suffix =~ s/\,/\}\{/g;
61
62         return "\\showfunc$prefix\{$suffix\}";
63 }
64
65 sub showfuncdesc()
66 {
67 my $suffix = $_[0];
68         $suffix =~ s/\\//g;
69
70         return "\\showfuncdesc\{$suffix\}";
71 }
72
73 sub showenumdesc()
74 {
75 my $prefix = $_[0];
76 my $suffix = $_[1];
77         $prefix =~ s/\\//g;
78         return "\\showenumdesc\{$prefix\}\{$suffix\}";
79 }
80
81 my $punescape = \&unescape;
82 my $pfuncref = \&funcref;
83 my $pshowfunc = \&showfunc;
84 my $pshowfuncdesc = \&showfuncdesc;
85 my $pshowenumdesc = \&showenumdesc;
86 my $mode;
87 my $num_args = $#ARGV + 1;
88
89 if ($num_args != 1 || $ARGV[0] eq "-h" || $ARGV[0] eq "--help") {
90         print "Usage: " . "texi2latex infile\n";
91         exit 0;
92 }
93 open (FILE, "< $ARGV[0]") or die "Cannot open $ARGV[0]";
94
95 my $match = "[\\w\\d-\\.\\/\\@\\:\_\\\\\#]";
96 my $spacematch = "[\\s\\w\\d-\\.\\/\\@\\#\\:]";
97 my $mathmatch = "[\\s\\w\\d-\\.\\/\\:\\(\\)\\+\\/\\^\\'\\=\{\}\\\\\\,]";
98 my $underscorematch = "[\\s\\w\\d-\\.\\/\\@\\_\\\\\:\\~]";
99 my $codematch = "[\\s\\w\\d-\\.\\/\\@\\_\\\\\:\\-\\\"\+\\%\\#\\,]";
100 my $extcodematch = "[\\s\\w\\d-\\.\\/\\@\\_\\\\\:\\-\\\"\+\\%\\,\\{\\}]";
101 my ($line, $prev_mode);
102 my ($verbatim, $label);
103 my @stack = ();
104
105 while ($line = <FILE>) {
106         $verbatim = 0;
107
108         if ($mode == SKIP) {
109 #print "%menu: $line";
110                 if ($line =~ m/\@end /) {
111                         $mode = pop(@stack);
112                 }
113                 $prev_mode = $mode;
114                 next;
115         } elsif ($mode == ITEMIZE) {
116 #print "%itemize: $line";
117                 if ($line =~ s/\@end itemize/\\end{itemize}/g) {
118                         $mode = pop(@stack);
119                 }
120                 $line =~ s/\@item (.+)/\\item $1/g;
121                 $line =~ s/\@item/\\item/g;
122                 $prev_mode = $mode;
123         } elsif ($mode == ENUMERATE) {
124                 if ($line =~ s/\@end itemize/\\end{enumerate}/g) {
125                         $mode = pop(@stack);
126                 }
127                 if ($line =~ s/\@end enumerate/\\end{enumerate}/g) {
128                         $mode = pop(@stack);
129                 }
130                 $line =~ s/\@item(.*)/\\item $1/g;
131                 $line =~ s/\@item/\\item/g;
132                 $prev_mode = $mode;
133         } elsif ($mode == FLOAT) {
134                 if ($line =~ s/\@end float/\\label\{$label\}\n\\end{figure}/g) {
135                         $mode = pop(@stack);
136                 }
137                 $line =~ s/\@image\{(\w)\,($spacematch)\}/\\includegraphics\[width=$2\]\{$1\}/g;
138                 $line =~ s/\@caption\{/\\caption\{/g;
139                 $prev_mode = $mode;
140         } elsif ($mode == FLOAT_TABLE) {
141                 if ($line =~ s/\@end float/\\label\{$label\}\n\\end{table}/g) {
142                         $mode = pop(@stack);
143                 } else {
144                         $line =~ s/\@caption\{/\\caption\{/g;
145
146                         if ($line =~ m/\@multitable/) {
147                                 push(@stack, FLOAT_TABLE);
148                                 $line =~ s/\@multitable \@columnfractions ([\.\d]+) ([\.\d]+)$/\n\\begin{tabular}{|p{$1\\linewidth}|p{$2\\linewidth}|}\n\\hline\n/g;
149                                 $line =~ s/\@multitable \@columnfractions ([\.\d]+) ([\.\d]+) ([\.\d]+)$/\n\\begin{tabular}{|p{$1\\linewidth}|p{$2\\linewidth}|p{$3\\linewidth}|}\n\\hline\n/g;
150                                 $line =~ s/\@multitable \@columnfractions ([\.\d]+) ([\.\d]+) ([\.\d]+) ([\.\d]+)$/\n\\begin{tabular}{|p{$1\\linewidth}|p{$2\\linewidth}|p{$3\\linewidth}|p{$4\\linewidth}|}\n\\hline\n/g;
151                                 $line =~ s/\@multitable \@columnfractions ([\.\d]+) ([\.\d]+) ([\.\d]+) ([\.\d]+) ([\.\d]+)$/\n\\begin{tabular}{|p{$1\\linewidth}|p{$2\\linewidth}|p{$3\\linewidth}|p{$4\\linewidth}|p{$5\\linewidth}|}\n\\hline\n/g;
152                         }
153                         
154                         goto multitable;
155                 }
156                 $prev_mode = $mode;
157         } elsif ($mode == TABLE_ITEMIZE) {
158                 if ($line =~ s/\@end table/\n\\end{itemize}/g) {
159                         $mode = pop(@stack);
160                 }
161                 chomp $line;
162                 if ($line eq "") {
163                         next;
164                 }
165                 $line .= "\n";
166
167                 $line =~ s/\@item (.+)/\\item $1 /g;
168                 $line =~ s/\@itemx (.+)/\\item $1 /g;
169
170                 $prev_mode = $mode;
171         } elsif ($mode == MULTITABLE) {
172 multitable:
173                 if ($line =~ s/\@end multitable/\\\\\n\\hline\n\\end{tabular}/g) {
174                         $mode = pop(@stack);
175                 } else {
176                         chomp $line;
177                         if ($line eq "") {
178                                 next;
179                         }
180                         $line .= "\n";
181                         $line =~ s/\@tab/\&/g;
182                         if ($line =~ m/\@headitem/) {
183                                 $line =~ s/\@headitem (.+)/$1\\\\\n\\hline\n\\hline/g;
184                                 $prev_mode = MULTITABLE_INIT;
185                         } else {
186                                 if ($prev_mode == MULTITABLE_INIT) {
187                                         $line =~ s/\@item (.+)/$1/g;
188                                         $line =~ s/\@itemx (.+)/$1/g;
189                                         $prev_mode = MULTITABLE;
190                                 } elsif ($prev_mode == MULTITABLE) {
191                                         $line =~ s/\@item (.+)/\\\\\n\\hline\n$1/g;
192                                         $line =~ s/\@itemx (.+)/\\\\\n\\hline\n$1/g;
193                                 }
194                         }
195                 }
196
197         } elsif ($mode == VERBATIM) {
198                 if ($line =~ s/\@end verbatim/\\end{verbatim}/g) {
199                         $mode = pop(@stack);
200                 }
201                 $verbatim = 1;
202                 $prev_mode = $mode;
203         } elsif ($mode == QUOTE) {
204                 if ($line =~ s/\@end quotation/\\end{quote}/g) {
205                         $mode = pop(@stack);
206                 }
207                 $prev_mode = $mode;
208         } elsif ($mode == EXAMPLE) {
209                 if ($line =~ s/\@end example/\\end{example}/g) {
210                         $mode = pop(@stack);
211                 }
212                 $line =~ s/\@{/{/g;
213                 $line =~ s/\@}/}/g;
214                 $verbatim = 1;
215
216                 $prev_mode = $mode;
217         } elsif ($mode == SMALL_EXAMPLE) {
218                 if ($line =~ s/\@end smallexample/\\end{smallexample}/g) {
219                         $mode = pop(@stack);
220                 }
221                 $line =~ s/\@{/\{/g;
222                 $line =~ s/\@}/\}/g;
223                 $verbatim = 1;
224
225                 $prev_mode = $mode;
226         } else {
227                 $prev_mode = $mode;
228
229                 $line =~ s/\@iftex/%c /g;
230                 $line =~ s/\@end iftex/%c /g;
231                 $line =~ s/\@anchor (.+)/\\label{$1}/g;
232                 $line =~ s/\@anchor\{($spacematch+)\}/\\label{$1}/g;
233                 if ($line =~ s/\@subsection (.+)/\\subsection{$1}/g) {
234                         if ($label ne '') {
235                                 $line .= "\\label{$label}\n";
236                         }
237                 }
238
239                 if ($line =~ s/\@subsubsection (.+)/\\subsubsection{$1}/g) {
240                         if ($label ne '') {
241                                 $line .= "\\label{$label}\n";
242                         }
243                 }
244
245                 $line =~ s/\@subsubheading (.+)/\\subsubsection\*{$1}/g;
246                 $line =~ s/\@subheading (.+)/\\subsection\*{$1}/g;
247
248                 if ($line =~ s/\@section (.+)/\\section{$1}/g) {
249                         if ($label ne '') {
250                                 $line .= "\\label{$label}\n";
251                         }
252                 }
253
254                 if ($line =~ s/\@chapter (.+)/\\chapter{$1}/g) {
255                         if ($label ne '') {
256                                 $line .= "\\label{$label}\n";
257                         }
258                 }
259                 if ($line =~ s/\@appendix (.+)/\\chapter{$1}/g) {
260                         if ($label ne '') {
261                                 $line .= "\\label{$label}\n";
262                         }
263                 }
264
265                 if ($line =~ m/\@node (.+)/) {
266                         $label = $1;
267                         next;
268                 } else {
269                         $label = '';
270                 }
271                 if ($line =~ s/\@menu//g) {
272                         $mode = SKIP;
273                         push(@stack, NORMAL);
274                         next;
275                 }
276                 if ($line =~ s/\@ignore//g) {
277                         $mode = SKIP;
278                         push(@stack, NORMAL);
279                         next;
280                 }
281                 if ($line =~ s/\@ifnottex//g) {
282                         $mode = SKIP;
283                         push(@stack, NORMAL);
284                         next;
285                 }
286                 if ($line =~ s/\@itemize \@bullet/\\begin{itemize}/g) {
287                         $mode = ITEMIZE;
288                         push(@stack, NORMAL);
289                 }
290                 if ($line =~ s/\@itemize/\\begin{itemize}/g) {
291                         $mode = ITEMIZE;
292                         push(@stack, NORMAL);
293                 }
294                 if ($line =~ s/\@float Figure\,(.*)/\\begin{figure}[htbp]\n\\centering/g) {
295                         $label = $1;
296                         push(@stack, NORMAL);
297                         $mode = FLOAT;
298                 }
299                 if ($line =~ s/\@float Table\,(.*)/\\begin{table}[htbp]\n\\centering/g) {
300                         $label = $1;
301                         push(@stack, NORMAL);
302                         $mode = FLOAT_TABLE;
303                 }
304                 if ($line =~ s/\@enumerate/\\begin{enumerate}/g) {
305                         push(@stack, NORMAL);
306                         $mode = ENUMERATE;
307                 }
308                 if ($line =~ s/\@table .*/\n\\begin{itemize}/g) {
309                         push(@stack, NORMAL);
310                         $mode = TABLE_ITEMIZE;
311                 }
312                 if ($line =~ s/\@multitable \@columnfractions ([\.\d]+) ([\.\d]+)$/\n\\begin{tabular}{|p{$1\\linewidth}|p{$2\\linewidth}|}\n\\hline\n/g) {
313                         push(@stack, NORMAL);
314                         $mode = MULTITABLE;
315                 }
316                 if ($line =~ s/\@multitable \@columnfractions ([\.\d]+) ([\.\d]+) ([\.\d]+)$/\n\\begin{tabular}{|p{$1\\linewidth}|p{$2\\linewidth}|p{$3\\linewidth}|}\n\\hline\n/g) {
317                         push(@stack, NORMAL);
318                         $mode = MULTITABLE;
319                 }
320                 if ($line =~ s/\@multitable \@columnfractions ([\.\d]+) ([\.\d]+) ([\.\d]+) ([\.\d]+) ([\.\d]+)$/\n\\begin{tabular}{|p{$1\\linewidth}|p{$2\\linewidth}|p{$3\\linewidth}|p{$4\\linewidth}|p{$5\\linewidth}|}\n\\hline\n/g) {
321                         push(@stack, NORMAL);
322                         $mode = MULTITABLE;
323                 }
324                 if ($line =~ s/\@example$/\\begin{example}/g) {
325                         push(@stack, NORMAL);
326                         $mode = EXAMPLE;
327                 }
328                 if ($line =~ s/\@smallexample/\\begin{smallexample}/g) {
329                         push(@stack, NORMAL);
330                         $mode = SMALL_EXAMPLE;
331                 }
332                 if ($line =~ s/\@verbatim$/\\begin{verbatim}/g) {
333                         push(@stack, NORMAL);
334                         $mode = VERBATIM;
335                 }
336                 if ($line =~ s/\@quotation$/\\begin{quote}/g) {
337                         push(@stack, NORMAL);
338                         $mode = QUOTE;
339                 }
340         }
341
342         if ($verbatim == 0) {
343                 $line =~ s/\_/\\_/g;
344                 $line =~ s/\~/\\~/g;
345                 $line =~ s/\%(?!c)/\\%/g;
346                 $line =~ s/\#/\\\#/g;
347                 $line =~ s/\@-/\\-/g;
348                 $line =~ s/\@verbatiminclude (.*)/\\examplefile{\.\.\/$1}/g;
349                 $line =~ s/\@image\{($match+)\,($match+)\}/\\includegraphics\[width\=$2\]\{\.\.\/$1\}/g;
350                 $line =~ s/\@samp\{($spacematch+)\}/$1/g;
351                 $line =~ s/\@strong\{/\{\\bf /g;
352                 $line =~ s/\@exampleindent.*//g;
353                 $line =~ s/\@c (.*)/\% $1/g;
354                 $line =~ s/\@math\{($mathmatch+)\}/\$$1\$/g;
355                 $line =~ s/\@acronym\{($spacematch+)\}/\\acronym{$1}/g;
356                 $line =~ s/\s*\@xcite\{($match+)\}/~\\cite{$1}/g;
357                 $line =~ s/\s*\@xcite\{($match+)\,($match+)\}/~\\cite{$1,$2}/g;
358                 $line =~ s/\@footnote\{/\\footnote{/g;
359                 $line =~ s/\@tindex (.+)/\\index{$1}/g;
360                 if ($line =~ s/\@include (.+)/\\input{$1}/g) {
361                         $line =~ s/\.texi/\.tex/g;
362                         $line =~ s/(\\input)\{($underscorematch+)\}/$punescape->($1,$2)/ge;
363                 }
364                 $line =~ s/\@cindex (.+)/\\index{$1}/g;
365                 $line =~ s/\@pindex (.+)/\\index{$1}/g;
366                 $line =~ s/\@url\{($underscorematch+)\}/\\url{$1}/g;
367                 #$line =~ s/\@euro/\\euro/g;
368                 $line =~ s/\s*\@euro\{\}/\~\\textgreek\{\\euro\}/g;
369                 $line =~ s/\@page/\\newpage/g;
370                 $line =~ s/\@file\{($spacematch+)\}/\\file{$1}/g;
371                 $line =~ s/\@code\{/\\code{/g;
372                 $line =~ s/\@option\{($codematch+)\}/\\command{$1}/g;
373                 $line =~ s/\@command\{($codematch+)\}/\\command{$1}/g;
374                 $line =~ s/\@ref\{/\\myref\{/g;
375                 $line =~ s/\@emph\{($spacematch+)\}/\\emph{$1}/g;
376                 $line =~ s/\@xref\{/\\myref\{/g;
377                 $line =~ s/\@funcref\{($codematch+)\}/$pfuncref->($1)/ge;
378                 $line =~ s/\@funcintref\{($codematch+)\}/$pfuncref->($1)/ge;
379                 $line =~ s/\@showfunc([A-Z])\{($codematch+)\}/$pshowfunc->($1,$2)/ge;
380                 $line =~ s/\@showfuncdesc\{($codematch+)\}/$pshowfuncdesc->($1)/ge;
381                 $line =~ s/\@showenumdesc\{($codematch+),($extcodematch+)\}/$pshowenumdesc->($1,$2)/ge;
382                 $line =~s/\@pxref\{($spacematch+),($spacematch+)\}/\\myref\{$1\}/g;
383                 $line =~ s/\@pxref\{/\\myref\{/g;
384                 $line =~ s/\@center (.*)/\\begin{center}\n$1\n\\end{center}/g;
385                 if ($line =~ m/\@email/) {
386                         $line =~ s/\@email\{(.+)\}/$1/g;
387                 }
388                 $line =~ s/\@\@/@/g;
389
390                 #when a myref{} contains underscores remove them
391                 $line =~ s/(\\myref)\{($underscorematch+)\}/$punescape->($1,$2)/ge;
392         }
393         $line =~ s/\@\@/\@/g;
394
395         print $line;
396 }
397 close (FILE);
398
399 exit 0;