Debian Codegraph @

doc/texinfo2HTML

raw
1 #!/usr/bin/perl -w
2
3 #$example_indent = " ";
4 $example_indent = "";
5
6 #$ex_color = "#179C4A"; # dark green -- too light
7 #$ex_color = "#14843F"; # dark green -- still too light
8 #$ex_color = "#056819"; # dark green -- "
9 $ex_color = "#82140F"; # dark red
10
11 $usage = "
12 texinfo2HTML [-h] [filename]
13
14 Convert texinfo doc to HTML format, through processing of
15 texinfo comments as follows:
16
17 1. Pass single line to HTML but not to other forms of manual
18 ------------------------------------------------------------
19
20 \@c HTML some-stuff
21
22
23 2. Commented-out portions
24 -------------------------
25
26 \@c HTML <!--
27 This line won't appear in the html
28 \@c HTML -->
29
30
31 3. Break file here
32 ------------------
33
34 \@c HTML <!-- newfile filename other_words_for_title -->
35 If it occurs on a line all by itself, causes
36 this perlscript to chop files here. The filename
37 will be as specified. The other_words
38 will be used as the title. If neither the
39 filename nor the other_words are present, then
40 this script makes up filenames using numbers, e.g.,
41 gri1.html, gri2.html. If the filename is \".\",
42 then this same naming scheme is used, but the titles
43 are used.
44
45 4. Insert latex command
46 -----------------------
47
48 \@c HTML <!-- latex: some-stuff -->
49 Plants a comment to be stripped out as a latex-only command.
50
51 BUGS:
52 Does not search for closing braces properly, so that lines like
53 blah \@code{blah blah
54 the end is here} and more blah
55 will not get the \@code{} item converted correctly.
56 ";
57 #require "ctime.pl";
58 use POSIX qw(ctime);
59 use Getopt::Std;
60 &getopts('h');
61 if ($opt_h) {
62 print "$usage";
63 exit 0;
64 }
65 $debug = 0;
66
67 #$date = &ctime(time);
68 $date = &ctime(time);
69 chop($date);
70
71 $CH = 0; # chapter
72 $SEC1 = 1; # section
73 $SEC2 = 1; # subsection
74 $SEC3 = 1; # subsubsection
75
76 $allow_P = 0;
77 $in_example = 0;
78 $last_was_node = 0;
79 print "<!-- Created automatically from texinfo source -->\n";
80 print "<!-- $date -->\n";
81 sub get_a_line() {
82 if ($_=<>) {
83 s/\@noindent//;
84 if (/^\@c\s*HTML\s*<!--\s*$/) {
85 while(<>) {
86 #print STDERR "SKIPPING $_";
87 if (/^\@c\s*HTML\s*-->\s*$/) {
88 $_ = <>;
89 return $_;
90 }
91 }
92 }
93 return $_;
94 } else {
95 return "";
96 }
97 }
98 while(get_a_line()) {
99 $line++ if $debug;
100 print STDERR "$0 at line: $line\n" if ($debug && ($line == 2000 * int($line / 2000)));
101 next if /\@cindex/;
102 # Prevent unclosed braces
103 if (!$in_example) {
104 while (/{[^}]*$/o) {
105 $a = $_;
106 $a =~ s/\n/ /o;
107 $_ = get_a_line();
108 $_ = $a . $_;
109 }
110 } else {
111 $_ = "$example_indent$_";
112 }
113 # Handle HTML inserts
114 if (/^\@c HTML (.*)/o) {
115 $_ = "$1 \n";
116 &sub_refs;
117 s,\@value\{(.*)\},$value{$1},g; # set/value pair
118 print;
119 next;
120 }
121 # Process image commands (NOT...it's there already)
122 if (/\@image\{(.*)\}/) {
123 #print "<img src=\"$1.gif\" border=\"1\">\n";
124 next;
125 }
126 next if /^\@top\s/;
127 next if /^\@include\s*/;
128 # Ignore all other comments
129 next if /^\@c\s/;
130 next if /^\@comment\s*/;
131 # Ignore unnumbered, since special case (see Makefile)
132 next if /\@unnumbered/;
133 # Ignore special-cases text
134 next if /^Copyright/;
135 # Ignore texinfo command to input files
136 next if /\\input/;
137 # Ignore some tex-type commands
138 next if /\@summarycontents/;
139 next if /\@contents/;
140 next if /\@bye/;
141 next if /\@message/;
142 next if /\@global/;
143 next if /\@setfilename/;
144 next if /\@setchapternewpage/;
145 next if /\@settitle/;
146 next if /\@page/;
147 next if /\@space/;
148 next if /\@sp/;
149 next if /\@defindex/;
150 next if /\@vskip/;
151 s/\@noindent//;
152 # Pass contents of ifhtml blocks
153 next if /\@ifhtml/;
154 next if /\@end ifhtml/;
155 # Pass contents of ifinfo blocks
156 next if /\@ifinfo/;
157 next if /\@end ifinfo/;
158 # Pass contents of ifnotinfo blocks
159 next if /\@ifnotinfo/;
160 next if /\@end ifnotinfo/;
161 # Ignore contents of @dircategory and @direntry block
162 next if /\@dircategory/;
163 if (/^\@direntry/o) {
164 while(get_a_line()) {
165 if (/^\@end direntry/o) {
166 $_ = "";
167 last;
168 }
169 }
170 }
171 # Ignore contents of tex and iftex blocks
172 if (/^\@tex/o) {
173 while(get_a_line()) {
174 if (/^\@end tex/o) {
175 $_ = "";
176 last;
177 }
178 }
179 }
180 if (/^\@iftex/o) {
181 while(get_a_line()) {
182 if (/^\@end iftex/o) {
183 $_ = "";
184 last;
185 }
186 }
187 }
188 # Ignore indices (for now)
189 next if /\@defindex/;
190 if (/\@[cfv]index\s*(.*)/o) {
191 print "<!-- latex: \\index{$1} -->\n";
192 next;
193 }
194 next if /\@EGindex/;
195 next if /\@printindex/;
196 #
197 # Save set/value pair.
198 if (/\@set\s*(\S*)\s*(\S*)/o) {
199 $value{$1} = $2;
200 next;
201 }
202 s,\@value\{([^}]*)\},$value{$1},g; # Substitute set/value pair
203 #
204 # Fix some texinfo escapes
205 s,&,&amp;,og;
206 s,>,&gt;,og;
207 s,<,&lt;,og;
208 # Special tweak to make 'Gri' look cooler.
209 # s, Gri , G<FONT SIZE=-1>RI</FONT> ,og;
210 s,\@code\{\@\@\},\@code{TEXINFO2HTML-AT-AT},og;
211 s,\@},TEXINFO2HTML-CLOSE-BRACE,og; # retain inside e.g. @code{}
212 s,\@\@,TEXINFO2HTML-AT-AT,og;
213 s,\@\{,{,og;
214 s,\@TeX\{\},TeX,og;
215 s,\@dots\{\},...,og;
216 # Put in place-holders for some accents. I should check for all
217 # of them, but for now, I'm just kludging in a couple,
218 # to solve an immediate problem and to serve as a place-holder
219 # for better coding. A good reference on accents is
220 # http://www.w3.org/MarkUp/html-spec/html-spec_13.html
221 # which I found on 2002-feb-24.
222 s,\@'a,TEXINFO2HTML-ACCENT-ACUTE-a,og;
223 s,\@'e,TEXINFO2HTML-ACCENT-ACUTE-e,og;
224 s,\@'o,TEXINFO2HTML-ACCENT-ACUTE-o,og;
225 if (/^\@titlepage/o) {
226 while(get_a_line()) {
227 s,\@value\{(.*)\},$value{$1},g; # set/value pair
228 if (/\@author\s*(.*)/o) {
229 $author = $1;
230 $author =~ s/\@\@/\@/;
231 } else {
232 if (/^\@end titlepage/o) {
233 last;
234 }
235 }
236 }
237 next;
238 }
239 while (/\@url\{([^}]*)\}/) {
240 $the_url = $1;
241 s:\@url\{[^}]*\}:<a href="$the_url">\@code{$the_url}</a>:;
242 }
243 while (/\@uref\{([^}]*)\}/) {
244 @items = split(/,/, $1);
245 if ($#items == 0) {
246 s:\@uref\{[^}]*\}:<a href="$items[0]">$items[0]</a>:;
247 } elsif ($#items == 1) {
248 s:\@uref\{[^}]*\}:<a href="$items[0]">$items[1]</a>:;
249 } elsif ($#items == 2) {
250 s:\@uref\{[^}]*\}:\@code{$items[2]}:;
251 } else {
252 die "Cannot have more than 3 items in a 'uref' at \"$_\"";
253 }
254 }
255 #
256 # Convert cross-references
257 if (/^\@node\s*([^,]*), ([^,]*), ([^,]*), ([^\n]*)/o) {
258 $next_node = $2;
259 $previous_node = $3;
260 $parent_node = $4;
261 # Start anchor, and set flag so end put after next heading.
262 # Need this flag because xmosaic 1.0 does not accept
263 # <A NAME="foo"></a>; it requires <A NAME="foo"><h1>heading</h1></a>
264 # or it will not properly search the reference. I think this
265 # is a bug, and reported it to the author in an email:
266 # From kelley Wed Jun 30 16:31:13 1993
267 # To: marca\@ncsa.uiuc.edu
268 chop; print "<!-- $_ -->\n";
269 print "<a name=\"$1\" ></a>\n";
270 $last_was_node = 1;
271 next;
272 }
273 &sub_refs;
274 #
275 # Convert headings; put the end anchor if needed
276 if (/\@chapter/o
277 || /\@section/o
278 || /\@subsection/o
279 || /\@subsubsection/o
280 || /\@subsubsubsection/o
281 || /\@appendixsubsec/o
282 || /\@unnumbered/o
283 || /\@appendixsubsec/o
284 || /\@appendixsec/o
285 || /\@appendix/o) {
286 &sub_headings;
287 &sub_emphasis;
288 print;
289 #
290 # Last was a node, so put the end anchor in
291 if ($last_was_node) {
292 print "\n<b>Navigation</b>:\n<a href=\"#$next_node\">next</a>,\n<a href=\"#$previous_node\">previous</a>,\n<a href=\"#$parent_node\">parent</a>.\n";
293 $last_was_node = 0;
294 }
295 #
296 # Done with this line
297 next;
298 }
299 &sub_emphasis;
300 #
301 # Convert quotations
302 s,\@quotation,<BLOCKQUOTE>,;
303 s,\@end quotation,</BLOCKQUOTE>,;
304 &process_examples();
305 # Convert Menus to UL (was DL before)
306 if (/\@menu/o) {
307 print "<UL>\n";
308 while(get_a_line()) {
309 if (/^\@end menu/o) {
310 $_ = "";
311 last;
312 }
313 &sub_headings;
314 &sub_emphasis;
315 if (/\*\s([^:]*)::\s*(.*)/o) {
316 print "<LI><a href=\"#$1\">$1</a>: $2\n";
317 }
318 }
319 print "</UL>\n";
320 }
321 if (/\@table\s*(.*)/o) {
322 if ($1 eq "\@code") {
323 $start_item = "<tt>"; $end_item = "</tt>"
324 }
325 if ($1 eq "\@emph") {
326 $start_item = "<i>"; $end_item = "</i>"
327 }
328 print "<dl>\n";
329 while(get_a_line()) {
330 next if /\@sp/;
331 next if /\@cindex/;
332 s|\@anchor\{([^}]*)\}|<a name=\"$1\"></a>|g;
333 &process_examples();
334 # Handle HTML inserts
335 if (/^\@c HTML (.*)/o) {
336 $_ = "$1 \n";
337 &sub_refs;
338 s,\@value\{(.*)\},$value{$1},g; # set/value pair
339 print;
340 next;
341 }
342 if (/^\s*\@end\s*table/o) {
343 $_ = "";
344 last;
345 }
346 if (/\s*\@item\s*(.*)/o) {
347 $the_item = $1;
348 $the_item =~ s:\@\{:{:og;
349 $the_item =~ s:\@\}:}:og;
350 $the_item =~ s:\@\@:\@:og;
351 $the_item =~ s:\@code\{([^}]*)\}:`<font color="$ex_color"><code>$1</code></font>':og;
352 print "<dt> $start_item$the_item$end_item\n<dd>";
353 } else {
354 print "<p>" if (/^$/o);
355 &sub_refs;
356 &sub_headings;
357 &sub_emphasis;
358 print;
359 }
360 }
361 print "</dl>\n";
362 }
363 #
364 # Convert lists
365 s,\@itemize.*,<ul>,;
366 s,\@end\s*itemize,</ul>,;
367 s,\@enumerate,<ol>,;
368 s,\@end\s*enumerate,</ol>,;
369 s,\@itemx,<li>,;
370 s,\@item,<li>,;
371 s,\@bullet,,; # HTML does not use
372 #
373 # Multiple blanks lines are paragraph separators in texinfo
374 if (/^$/o) {
375 if ($allow_P) {
376 print "<p>";
377 }
378 $allow_P = 0;
379 } elsif (/<H/o) {
380 $allow_P = 0;
381 } else {
382 $allow_P = 1;
383 }
384 #
385 # Finally, print modified line
386 print;
387 }
388 #print "____________________________________________________________________\n";
389 #print "<ADDRESS> $title / $author </ADDRESS>\n";
390
391 print "</BODY>\n";
392 print "</HTML>\n";
393
394 sub process_examples() {
395 if (/\@example/o) {
396 s,\s*\@example,<TABLE SUMMARY="Example" BORDER="0" BGCOLOR="#efefef" WIDTH="100%">\n<TR>\n<TD>\n<PRE>\n<font color="$ex_color">,;
397 $in_example = 1;
398 } elsif (/^\s*\@end example/o) {
399 s,\s*\@end example,</font></PRE>\n</TD>\n</TR>\n</TABLE>,;
400 $in_example = 0;
401 } else {
402 s,\@value\{([^}]*)\},$value{$1},g; # Substitute set/value pair
403 s,\@},},og;
404 s,\@\{,{,og;
405 }
406 }
407
408
409 sub sub_refs {
410 die "line $. of file: cannot have multiple refs on one line" if (/\@[px]*ref\{(.*)\}(.*)\@[px]*ref\{(.*)\}/);
411 # anchors
412 s|\@anchor\{([^}]*)\}|<a name=\"$1\"></a>|g;
413 # Change e.g.
414 # @xref{Viewing}
415 # into
416 # <a href="#Viewing">see Viewing</a>
417
418 #if (/\@ref/){print "AAA[$_]AAA\n";
419 s|\@ref\{([^}]*)\}|see <a href="#$1">$1</a>|g;
420 #print "BBB[$_]BBB\n";}
421
422 s|\@xref\{([^}]*)\}|see <a href="#$1">$1</a>|g;
423 s|\@pxref\{([^}]*)\}|see <a href="#$1">$1.</a>|g;
424 while (/\@url\{([^}]*)\}/) {
425 $the_url = $1;
426 s:\@url\{[^}]*\}:<a href="$the_url">\@code{$the_url}</a>:;
427 }
428 while (/\@uref\{([^}]*)\}/) {
429 @items = split(/,/, $1);
430 if ($#items == 0) {
431 s:\@uref\{[^}]*\}:<a href="$items[0]">$items[0]</a>:;
432 } elsif ($#items == 1) {
433 s:\@uref\{[^}]*\}:<a href="$items[0]">$items[1]</a>:;
434 } elsif ($#items == 2) {
435 s:\@uref\{[^}]*\}:\@code{$items[2]}:;
436 } else {
437 die "Cannot have more than 3 items in a 'uref' at \"$_\"";
438 }
439 }
440 }
441
442 sub sub_headings {
443 if (/\@chapter/) {
444 $CH++;
445 $SEC1 = 0;
446 s,\@chapter\s*(.*),<h1>$CH: $1</h1>,;
447 } elsif (/\@unnumbered/) {
448 $CH++;
449 $SEC1 = 0;
450 s,\@unnumbered\s*(.*),<h1>$CH: $1</h1>,;
451 } elsif (/\@section/) {
452 $SEC1++;
453 $SEC2 = 0;
454 s,\@section\s*(.*),<h2>$CH.$SEC1: $1</h2>,;
455 } elsif (/\@subsection/) {
456 $SEC2++;
457 $SEC3 = 0;
458 s,\@subsection\s*(.*),<h3>$CH.$SEC1.$SEC2: $1</h3>,;
459 } elsif (/\@subsubsection/) {
460 $SEC3++;
461 s,\@subsubsection\s*(.*),<h4>$CH.$SEC1.$SEC2.$SEC3: $1</h4>,;
462 }
463 }
464
465 sub sub_emphasis {
466 s,<<,&lt&lt,g;
467 s,>>,&gt&gt,g;
468 s,\@emph\{([^}]*)\},<em>$1</em>,g;
469 s,\@strong\{([^}]*)\},<b>$1</b>,g;
470 s,\@footnote\{([^}]*)\}, [$1],g;
471 s,\@b\{([^}]*)\},<b>$1</b>,g;
472
473 s,\@code\{([^}]*)\},`<font color="$ex_color"><code>$1</code></font>',g;
474 s,\@\@,\@,g;
475 s,\@samp\{([^}]*)\},`<font color="$ex_color"><samp>$1</samp></font>',g;
476 s,\@key\{([^}]*)\},`<font color="$ex_color"><kbd>$1</kbd></font>',g;
477 s,\@kbd\{([^}]*)\},`<font color="$ex_color"><kbd>$1</kbd></font>',g;
478 s,\@file\{([^}]*)\},`<font color="$ex_color"><samp>$1</samp></font>',g;
479 s,TEXINFO2HTML-CLOSE-BRACE,},g;
480 s,TEXINFO2HTML-AT-AT,\@,g;
481 s,TEXINFO2HTML-ACCENT-ACUTE-a,&#225,g;
482 s,TEXINFO2HTML-ACCENT-ACUTE-e,&#233,g;
483 s,TEXINFO2HTML-ACCENT-ACUTE-o,&#243,g;
484 }
485