Documentation updates
[gnutls.git] / doc / scripts / mytexi2latex
blobfe9176c52ea1838fcc9ad6d6be77fbde31dec6b9
1 eval '(exit $?0)' && eval 'exec perl -wST "$0" ${1+"$@"}'
2   & eval 'exec perl -wST "$0" $argv:q'
3     if 0;
5 # Copyright (C) 2011-2012 Free Software Foundation, Inc.
7 # This file is part of GnuTLS.
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.
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.
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.
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;
37 sub unescape()
39 my $prefix=$_[0];
40 my $suffix=$_[1];
41         $suffix =~ s/\\//g;
42         return "$prefix\{$suffix\}";
45 sub funcref()
47 my $prefix = $_[0];
48 my $suffix=$_[0];
49         $suffix =~ s/\\//g;
50         $prefix =~ s/\\_/\\_\\-/g;
52         return "\\funcref\{$prefix\}\{$suffix\}";
55 sub showfunc()
57 my $prefix = $_[0];
58 my $suffix = $_[1];
59         $suffix =~ s/\\//g;
60         $suffix =~ s/\,/\}\{/g;
62         return "\\showfunc$prefix\{$suffix\}";
65 sub showfuncdesc()
67 my $suffix = $_[0];
68         $suffix =~ s/\\//g;
70         return "\\showfuncdesc\{$suffix\}";
73 sub showenumdesc()
75 my $prefix = $_[0];
76 my $suffix = $_[1];
77         $prefix =~ s/\\//g;
78         return "\\showenumdesc\{$prefix\}\{$suffix\}";
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;
89 if ($num_args != 1 || $ARGV[0] eq "-h" || $ARGV[0] eq "--help") {
90         print "Usage: " . "texi2latex infile\n";
91         exit 0;
93 open (FILE, "< $ARGV[0]") or die "Cannot open $ARGV[0]";
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 = ();
105 while ($line = <FILE>) {
106         $verbatim = 0;
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;
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";
167                 $line =~ s/\@item (.+)/\\item $1 /g;
168                 $line =~ s/\@itemx (.+)/\\item $1 /g;
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                 }
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;
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;
225                 $prev_mode = $mode;
226         } else {
227                 $prev_mode = $mode;
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                 }
239                 if ($line =~ s/\@subsubsection (.+)/\\subsubsection{$1}/g) {
240                         if ($label ne '') {
241                                 $line .= "\\label{$label}\n";
242                         }
243                 }
245                 $line =~ s/\@subsubheading (.+)/\\subsubsection\*{$1}/g;
246                 $line =~ s/\@subheading (.+)/\\subsection\*{$1}/g;
247                 $line =~ s/\@heading (.+)/\\section\*{$1}/g;
249                 if ($line =~ s/\@section (.+)/\\section{$1}/g) {
250                         if ($label ne '') {
251                                 $line .= "\\label{$label}\n";
252                         }
253                 }
255                 if ($line =~ s/\@chapter (.+)/\\chapter{$1}/g) {
256                         if ($label ne '') {
257                                 $line .= "\\label{$label}\n";
258                         }
259                 }
260                 if ($line =~ s/\@appendix (.+)/\\chapter{$1}/g) {
261                         if ($label ne '') {
262                                 $line .= "\\label{$label}\n";
263                         }
264                 }
266                 if ($line =~ m/\@node (.+)/) {
267                         $label = $1;
268                         next;
269                 } else {
270                         $label = '';
271                 }
272                 if ($line =~ s/\@menu//g) {
273                         $mode = SKIP;
274                         push(@stack, NORMAL);
275                         next;
276                 }
277                 if ($line =~ s/\@ignore//g) {
278                         $mode = SKIP;
279                         push(@stack, NORMAL);
280                         next;
281                 }
282                 if ($line =~ s/\@ifnottex//g) {
283                         $mode = SKIP;
284                         push(@stack, NORMAL);
285                         next;
286                 }
287                 if ($line =~ s/\@itemize \@bullet/\\begin{itemize}/g) {
288                         $mode = ITEMIZE;
289                         push(@stack, NORMAL);
290                 }
291                 if ($line =~ s/\@itemize/\\begin{itemize}/g) {
292                         $mode = ITEMIZE;
293                         push(@stack, NORMAL);
294                 }
295                 if ($line =~ s/\@float Figure\,(.*)/\\begin{figure}[htbp]\n\\centering/g) {
296                         $label = $1;
297                         push(@stack, NORMAL);
298                         $mode = FLOAT;
299                 }
300                 if ($line =~ s/\@float Table\,(.*)/\\begin{table}[htbp]\n\\centering/g) {
301                         $label = $1;
302                         push(@stack, NORMAL);
303                         $mode = FLOAT_TABLE;
304                 }
305                 if ($line =~ s/\@enumerate/\\begin{enumerate}/g) {
306                         push(@stack, NORMAL);
307                         $mode = ENUMERATE;
308                 }
309                 if ($line =~ s/\@table .*/\n\\begin{itemize}/g) {
310                         push(@stack, NORMAL);
311                         $mode = TABLE_ITEMIZE;
312                 }
313                 if ($line =~ s/\@multitable \@columnfractions ([\.\d]+) ([\.\d]+)$/\n\\begin{tabular}{|p{$1\\linewidth}|p{$2\\linewidth}|}\n\\hline\n/g) {
314                         push(@stack, NORMAL);
315                         $mode = MULTITABLE;
316                 }
317                 if ($line =~ s/\@multitable \@columnfractions ([\.\d]+) ([\.\d]+) ([\.\d]+)$/\n\\begin{tabular}{|p{$1\\linewidth}|p{$2\\linewidth}|p{$3\\linewidth}|}\n\\hline\n/g) {
318                         push(@stack, NORMAL);
319                         $mode = MULTITABLE;
320                 }
321                 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) {
322                         push(@stack, NORMAL);
323                         $mode = MULTITABLE;
324                 }
325                 if ($line =~ s/\@example$/\\begin{example}/g) {
326                         push(@stack, NORMAL);
327                         $mode = EXAMPLE;
328                 }
329                 if ($line =~ s/\@smallexample/\\begin{smallexample}/g) {
330                         push(@stack, NORMAL);
331                         $mode = SMALL_EXAMPLE;
332                 }
333                 if ($line =~ s/\@verbatim$/\\begin{verbatim}/g) {
334                         push(@stack, NORMAL);
335                         $mode = VERBATIM;
336                 }
337                 if ($line =~ s/\@quotation$/\\begin{quote}/g) {
338                         push(@stack, NORMAL);
339                         $mode = QUOTE;
340                 }
341         }
343         if ($verbatim == 0) {
344                 $line =~ s/\</\$<\$/g;
345                 $line =~ s/\>/\$>\$/g;
346                 $line =~ s/\_/\\_/g;
347                 $line =~ s/\~/\\~/g;
348                 $line =~ s/\%(?!c)/\\%/g;
349                 $line =~ s/\#/\\\#/g;
350                 $line =~ s/\@-/\\-/g;
351                 $line =~ s/\@verbatiminclude (.*)/\\examplefile{\.\.\/$1}/g;
352                 $line =~ s/\@image\{($match+)\,($match+)\}/\\includegraphics\[width\=$2\]\{\.\.\/$1\}/g;
353                 $line =~ s/\@samp\{($spacematch+)\}/$1/g;
354                 $line =~ s/\@strong\{/\{\\bf /g;
355                 $line =~ s/\@noindent.*//g;
356                 $line =~ s/\@exampleindent.*//g;
357                 $line =~ s/\@c (.*)/\% $1/g;
358                 $line =~ s/\@math\{($mathmatch+)\}/\$$1\$/g;
359                 $line =~ s/\@acronym\{($spacematch+)\}/\\acronym{$1}/g;
360                 $line =~ s/\s*\@xcite\{($match+)\}/~\\cite{$1}/g;
361                 $line =~ s/\s*\@xcite\{($match+)\,($match+)\}/~\\cite{$1,$2}/g;
362                 $line =~ s/\@footnote\{/\\footnote{/g;
363                 $line =~ s/\@tindex (.+)/\\index{$1}/g;
364                 if ($line =~ s/\@include (.+)/\\input{$1}/g) {
365                         $line =~ s/\.texi/\.tex/g;
366                         $line =~ s/(\\input)\{($underscorematch+)\}/$punescape->($1,$2)/ge;
367                 }
368                 $line =~ s/\@cindex (.+)/\\index{$1}/g;
369                 $line =~ s/\@pindex (.+)/\\index{$1}/g;
370                 $line =~ s/\@url\{($underscorematch+)\}/\\url{$1}/g;
371                 #$line =~ s/\s*\@euro\{\}/\~\\textgreek\{\\euro\}/g;
372                 $line =~ s/\@page/\\newpage/g;
373                 $line =~ s/\@file\{($spacematch+)\}/\\file{$1}/g;
374                 $line =~ s/\@code\{/\\code{/g;
375                 $line =~ s/\@option\{($codematch+)\}/\\command{$1}/g;
376                 $line =~ s/\@command\{($codematch+)\}/\\command{$1}/g;
377                 $line =~ s/\@ref\{/\\myref\{/g;
378                 $line =~ s/\@emph\{($spacematch+)\}/\\emph{$1}/g;
379                 $line =~ s/\@xref\{/\\myref\{/g;
380                 $line =~ s/\@funcref\{($codematch+)\}/$pfuncref->($1)/ge;
381                 $line =~ s/\@funcintref\{($codematch+)\}/$pfuncref->($1)/ge;
382                 $line =~ s/\@showfunc([A-Z])\{($codematch+)\}/$pshowfunc->($1,$2)/ge;
383                 $line =~ s/\@showfuncdesc\{($codematch+)\}/$pshowfuncdesc->($1)/ge;
384                 $line =~ s/\@showenumdesc\{($codematch+),($extcodematch+)\}/$pshowenumdesc->($1,$2)/ge;
385                 $line =~s/\@pxref\{($spacematch+),($spacematch+)\}/\\myref\{$1\}/g;
386                 $line =~ s/\@pxref\{/\\myref\{/g;
387                 $line =~ s/\@center (.*)/\\begin{center}\n$1\n\\end{center}/g;
388                 if ($line =~ m/\@email/) {
389                         $line =~ s/\@email\{(.+)\}/$1/g;
390                 }
391                 $line =~ s/\@\@/@/g;
393                 #when a myref{} contains underscores remove them
394                 $line =~ s/(\\myref)\{($underscorematch+)\}/$punescape->($1,$2)/ge;
395         }
396         $line =~ s/\@\@/\@/g;
398         print $line;
400 close (FILE);
402 exit 0;