1 eval '(exit $?0)' && eval 'exec perl -wST "$0" ${1+"$@"}'
2 & eval 'exec perl -wST "$0" $argv:q'
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;
42 return "$prefix\{$suffix\}";
50 $prefix =~ s/\\_/\\_\\-/g;
52 return "\\funcref\{$prefix\}\{$suffix\}";
60 $suffix =~ s/\,/\}\{/g;
62 return "\\showfunc$prefix\{$suffix\}";
70 return "\\showfuncdesc\{$suffix\}";
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;
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";
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);
105 while ($line = <FILE>) {
109 #print "%menu: $line";
110 if ($line =~ m/\@end /) {
115 } elsif ($mode == ITEMIZE) {
116 #print "%itemize: $line";
117 if ($line =~ s/\@end itemize/\\end{itemize}/g) {
120 $line =~ s/\@item (.+)/\\item $1/g;
121 $line =~ s/\@item/\\item/g;
123 } elsif ($mode == ENUMERATE) {
124 if ($line =~ s/\@end itemize/\\end{enumerate}/g) {
127 if ($line =~ s/\@end enumerate/\\end{enumerate}/g) {
130 $line =~ s/\@item(.*)/\\item $1/g;
131 $line =~ s/\@item/\\item/g;
133 } elsif ($mode == FLOAT) {
134 if ($line =~ s/\@end float/\\label\{$label\}\n\\end{figure}/g) {
137 $line =~ s/\@image\{(\w)\,($spacematch)\}/\\includegraphics\[width=$2\]\{$1\}/g;
138 $line =~ s/\@caption\{/\\caption\{/g;
140 } elsif ($mode == FLOAT_TABLE) {
141 if ($line =~ s/\@end float/\\label\{$label\}\n\\end{table}/g) {
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;
157 } elsif ($mode == TABLE_ITEMIZE) {
158 if ($line =~ s/\@end table/\n\\end{itemize}/g) {
167 $line =~ s/\@item (.+)/\\item $1 /g;
168 $line =~ s/\@itemx (.+)/\\item $1 /g;
171 } elsif ($mode == MULTITABLE) {
173 if ($line =~ s/\@end multitable/\\\\\n\\hline\n\\end{tabular}/g) {
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;
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;
197 } elsif ($mode == VERBATIM) {
198 if ($line =~ s/\@end verbatim/\\end{verbatim}/g) {
203 } elsif ($mode == QUOTE) {
204 if ($line =~ s/\@end quotation/\\end{quote}/g) {
208 } elsif ($mode == EXAMPLE) {
209 if ($line =~ s/\@end example/\\end{example}/g) {
217 } elsif ($mode == SMALL_EXAMPLE) {
218 if ($line =~ s/\@end smallexample/\\end{smallexample}/g) {
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) {
235 $line .= "\\label{$label}\n";
239 if ($line =~ s/\@subsubsection (.+)/\\subsubsection{$1}/g) {
241 $line .= "\\label{$label}\n";
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) {
251 $line .= "\\label{$label}\n";
255 if ($line =~ s/\@chapter (.+)/\\chapter{$1}/g) {
257 $line .= "\\label{$label}\n";
260 if ($line =~ s/\@appendix (.+)/\\chapter{$1}/g) {
262 $line .= "\\label{$label}\n";
266 if ($line =~ m/\@node (.+)/) {
272 if ($line =~ s/\@menu//g) {
274 push(@stack, NORMAL);
277 if ($line =~ s/\@ignore//g) {
279 push(@stack, NORMAL);
282 if ($line =~ s/\@ifnottex//g) {
284 push(@stack, NORMAL);
287 if ($line =~ s/\@itemize \@bullet/\\begin{itemize}/g) {
289 push(@stack, NORMAL);
291 if ($line =~ s/\@itemize/\\begin{itemize}/g) {
293 push(@stack, NORMAL);
295 if ($line =~ s/\@float Figure\,(.*)/\\begin{figure}[htbp]\n\\centering/g) {
297 push(@stack, NORMAL);
300 if ($line =~ s/\@float Table\,(.*)/\\begin{table}[htbp]\n\\centering/g) {
302 push(@stack, NORMAL);
305 if ($line =~ s/\@enumerate/\\begin{enumerate}/g) {
306 push(@stack, NORMAL);
309 if ($line =~ s/\@table .*/\n\\begin{itemize}/g) {
310 push(@stack, NORMAL);
311 $mode = TABLE_ITEMIZE;
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);
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);
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);
325 if ($line =~ s/\@example$/\\begin{example}/g) {
326 push(@stack, NORMAL);
329 if ($line =~ s/\@smallexample/\\begin{smallexample}/g) {
330 push(@stack, NORMAL);
331 $mode = SMALL_EXAMPLE;
333 if ($line =~ s/\@verbatim$/\\begin{verbatim}/g) {
334 push(@stack, NORMAL);
337 if ($line =~ s/\@quotation$/\\begin{quote}/g) {
338 push(@stack, NORMAL);
343 if ($verbatim == 0) {
344 $line =~ s/\</\$<\$/g;
345 $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;
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;
393 #when a myref{} contains underscores remove them
394 $line =~ s/(\\myref)\{($underscorematch+)\}/$punescape->($1,$2)/ge;
396 $line =~ s/\@\@/\@/g;