File: texinfo2HTML

package info (click to toggle)
gri 2.12.26-1
  • links: PTS
  • area: main
  • in suites: buster, sid
  • size: 5,952 kB
  • ctags: 2,396
  • sloc: cpp: 36,598; sh: 4,196; lisp: 3,764; perl: 1,362; ansic: 1,046; makefile: 606
file content (485 lines) | stat: -rwxr-xr-x 12,680 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#!/usr/bin/perl -w

#$example_indent = "    ";
$example_indent = "";

#$ex_color = "#179C4A";		# dark green -- too light
#$ex_color = "#14843F";		# dark green -- still too light
#$ex_color = "#056819";		# dark green -- "
$ex_color = "#82140F";		# dark red

$usage = "
texinfo2HTML [-h] [filename]

Convert texinfo doc to HTML format, through processing of 
texinfo comments as follows:

1. Pass single line to HTML but not to other forms of manual
------------------------------------------------------------

    \@c HTML some-stuff


2. Commented-out portions
-------------------------

    \@c HTML <!--
    This line won't appear in the html
    \@c HTML -->


3. Break file here
------------------

    \@c HTML <!-- newfile filename other_words_for_title -->
	    If it occurs on a line all by itself, causes 
	    this perlscript to chop files here.  The filename
	    will be as specified.  The other_words 
	    will be used as the title.  If neither the
	    filename nor the other_words are present, then
	    this script makes up filenames using numbers, e.g.,
	    gri1.html, gri2.html.  If the filename is \".\",
	    then this same naming scheme is used, but the titles
	    are used.

4. Insert latex command
-----------------------

    \@c HTML <!-- latex: some-stuff -->
	    Plants a comment to be stripped out as a latex-only command.

BUGS:
	Does not search for closing braces properly, so that lines like
		blah \@code{blah blah
		the end is here} and more blah
	will not get the \@code{} item converted correctly.
";
#require "ctime.pl";
use POSIX qw(ctime);
use Getopt::Std;
&getopts('h');
if ($opt_h) {
    print "$usage";
    exit 0;
}
$debug = 0;

#$date = &ctime(time);
$date = &ctime(time);
chop($date);

$CH   = 0;			# chapter 
$SEC1 = 1;			# section
$SEC2 = 1;			# subsection
$SEC3 = 1;			# subsubsection

$allow_P = 0;
$in_example = 0;
$last_was_node = 0;
print "<!-- Created automatically from texinfo source -->\n";
print "<!-- $date -->\n";
sub get_a_line() {
    if ($_=<>) {
	s/\@noindent//;
	if (/^\@c\s*HTML\s*<!--\s*$/) {
	    while(<>) {
		#print STDERR "SKIPPING $_";
		if (/^\@c\s*HTML\s*-->\s*$/) {
		    $_ = <>;
		    return $_;
		}
	    }
	}
	return $_;
    } else {
	return "";
    }
}
while(get_a_line()) {
    $line++ if $debug;
    print STDERR "$0 at line: $line\n" if ($debug && ($line == 2000 * int($line / 2000)));
    next if /\@cindex/;
    # Prevent unclosed braces
    if (!$in_example) {
	while (/{[^}]*$/o) {
	    $a = $_;
	    $a =~ s/\n/ /o;
	    $_ = get_a_line();
	    $_ = $a . $_;
	}
    } else {
	$_ = "$example_indent$_";
    }
    # Handle HTML inserts
    if (/^\@c HTML (.*)/o) {
	$_ = "$1 \n";
	&sub_refs;
	s,\@value\{(.*)\},$value{$1},g; # set/value pair
	print;
	next;
    }
    # Process image commands (NOT...it's there already) 
    if (/\@image\{(.*)\}/) {
	#print "<img src=\"$1.gif\" border=\"1\">\n";
	next;
    }
    next if /^\@top\s/;
    next if /^\@include\s*/;
    # Ignore all other comments
    next if /^\@c\s/;
    next if /^\@comment\s*/;
    # Ignore unnumbered, since special case (see Makefile)
    next if /\@unnumbered/;
    # Ignore special-cases text
    next if /^Copyright/;
    # Ignore texinfo command to input files
    next if /\\input/;
    # Ignore some tex-type commands
    next if /\@summarycontents/;
    next if /\@contents/;
    next if /\@bye/;
    next if /\@message/;
    next if /\@global/;
    next if /\@setfilename/;
    next if /\@setchapternewpage/;
    next if /\@settitle/;
    next if /\@page/;
    next if /\@space/;
    next if /\@sp/;
    next if /\@defindex/;
    next if /\@vskip/;
    s/\@noindent//;
    # Pass contents of ifhtml blocks
    next if /\@ifhtml/;
    next if /\@end ifhtml/;
    # Pass contents of ifinfo blocks
    next if /\@ifinfo/;
    next if /\@end ifinfo/;
    # Pass contents of ifnotinfo blocks
    next if /\@ifnotinfo/;
    next if /\@end ifnotinfo/;
    # Ignore contents of @dircategory and @direntry block
    next if /\@dircategory/;
    if (/^\@direntry/o) {
	while(get_a_line()) {
	    if (/^\@end direntry/o) {
		$_ = "";
		last;
	    }
	}
    }
    # Ignore contents of tex and iftex blocks
    if (/^\@tex/o) {
	while(get_a_line()) {
	    if (/^\@end tex/o) {
		$_ = "";
		last;
	    }
	}
    }
    if (/^\@iftex/o) {
	while(get_a_line()) {
	    if (/^\@end iftex/o) {
		$_ = "";
		last;
	    }
	}
    }
    # Ignore indices (for now)
    next if /\@defindex/;
    if (/\@[cfv]index\s*(.*)/o) {
	print "<!-- latex: \\index{$1} -->\n";
	next;
    }
    next if /\@EGindex/;
    next if /\@printindex/;
    #
    # Save set/value pair.
    if (/\@set\s*(\S*)\s*(\S*)/o) {
	$value{$1} = $2;
	next;
    }
    s,\@value\{([^}]*)\},$value{$1},g; # Substitute set/value pair
    #
    # Fix some texinfo escapes
    s,&,&amp;,og;
    s,>,&gt;,og;
    s,<,&lt;,og;
    # Special tweak to make 'Gri' look cooler.
	#    s, Gri , G<FONT SIZE=-1>RI</FONT> ,og;
    s,\@code\{\@\@\},\@code{TEXINFO2HTML-AT-AT},og;
    s,\@},TEXINFO2HTML-CLOSE-BRACE,og; # retain inside e.g. @code{}
    s,\@\@,TEXINFO2HTML-AT-AT,og;
    s,\@\{,{,og;
    s,\@TeX\{\},TeX,og;
    s,\@dots\{\},...,og;
    # Put in place-holders for some accents.  I should check for all
    # of them, but for now, I'm just kludging in a couple, 
    # to solve an immediate problem and to serve as a place-holder
    # for better coding.  A good reference on accents is
    #    http://www.w3.org/MarkUp/html-spec/html-spec_13.html
    # which I found on 2002-feb-24.
    s,\@'a,TEXINFO2HTML-ACCENT-ACUTE-a,og;
    s,\@'e,TEXINFO2HTML-ACCENT-ACUTE-e,og;
    s,\@'o,TEXINFO2HTML-ACCENT-ACUTE-o,og;
    if (/^\@titlepage/o) {
	while(get_a_line()) {
	    s,\@value\{(.*)\},$value{$1},g; # set/value pair
	    if (/\@author\s*(.*)/o) {
		$author = $1;
	        $author =~ s/\@\@/\@/;
	    } else {
		if (/^\@end titlepage/o) {
		    last;
		}
	    }
	}
	next;
    }
    while (/\@url\{([^}]*)\}/) {
	$the_url = $1;
        s:\@url\{[^}]*\}:<a href="$the_url">\@code{$the_url}</a>:;
    }
    while (/\@uref\{([^}]*)\}/) {
	@items = split(/,/, $1);
        if ($#items == 0) {
            s:\@uref\{[^}]*\}:<a href="$items[0]">$items[0]</a>:;
        } elsif ($#items == 1) {
            s:\@uref\{[^}]*\}:<a href="$items[0]">$items[1]</a>:;
        } elsif ($#items == 2) {
            s:\@uref\{[^}]*\}:\@code{$items[2]}:;
        } else {
            die "Cannot have more than 3 items in a 'uref' at \"$_\"";
        }
    }
    #
    # Convert cross-references
    if (/^\@node\s*([^,]*), ([^,]*), ([^,]*), ([^\n]*)/o) {
	$next_node = $2;
	$previous_node = $3;
	$parent_node = $4;
        # Start anchor, and set flag so end put after next heading.
	# Need this flag because xmosaic 1.0 does not accept
	# <A NAME="foo"></a>; it requires <A NAME="foo"><h1>heading</h1></a>
	# or it will not properly search the reference.  I think this
	# is a bug, and reported it to the author in an email:
	#   From kelley Wed Jun 30 16:31:13 1993
	#   To: marca\@ncsa.uiuc.edu
	chop;	print "<!-- $_ -->\n";
	print "<a name=\"$1\" ></a>\n";
	$last_was_node = 1;
	next;
    }
    &sub_refs;
    #
    # Convert headings; put the end anchor if needed
    if (/\@chapter/o
	|| /\@section/o
	|| /\@subsection/o
	|| /\@subsubsection/o
	|| /\@subsubsubsection/o
	|| /\@appendixsubsec/o
	|| /\@unnumbered/o
	|| /\@appendixsubsec/o
	|| /\@appendixsec/o
	|| /\@appendix/o) {
        &sub_headings;
	&sub_emphasis;
	print;
	#
	# Last was a node, so put the end anchor in
	if ($last_was_node) {
	    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";
	    $last_was_node = 0;
	}
	# 
	# Done with this line
	next;
    }
    &sub_emphasis;
    #
    # Convert quotations
    s,\@quotation,<BLOCKQUOTE>,;
    s,\@end quotation,</BLOCKQUOTE>,;
    &process_examples();
    # Convert Menus to UL (was DL before)
    if (/\@menu/o) {
	print "<UL>\n";
	while(get_a_line()) {
	    if (/^\@end menu/o) {
		$_ = "";
		last;
	    }
	    &sub_headings;
	    &sub_emphasis;
	    if (/\*\s([^:]*)::\s*(.*)/o) {
		print "<LI><a href=\"#$1\">$1</a>: $2\n";
	    } 
	}
	print "</UL>\n";
    }
    if (/\@table\s*(.*)/o) {
	if ($1 eq "\@code") {
	    $start_item = "<tt>"; $end_item = "</tt>"
	}
	if ($1 eq "\@emph") {
	    $start_item = "<i>"; $end_item = "</i>"
	}
	print "<dl>\n";
	while(get_a_line()) {
	    next if /\@sp/;
	    next if /\@cindex/;
            s|\@anchor\{([^}]*)\}|<a name=\"$1\"></a>|g;
	    &process_examples();
	    # Handle HTML inserts
	    if (/^\@c HTML (.*)/o) {
		$_ = "$1 \n";
		&sub_refs;
		s,\@value\{(.*)\},$value{$1},g; # set/value pair
		print;
		next;
	    }
	    if (/^\s*\@end\s*table/o) {
		$_ = "";
		last;
	    }
	    if (/\s*\@item\s*(.*)/o) {
                $the_item = $1;
		$the_item =~ s:\@\{:{:og;
		$the_item =~ s:\@\}:}:og;
		$the_item =~ s:\@\@:\@:og;
		$the_item =~ s:\@code\{([^}]*)\}:`<font color="$ex_color"><code>$1</code></font>':og;
		print "<dt> $start_item$the_item$end_item\n<dd>";
	    } else {
		print "<p>" if (/^$/o);
	        &sub_refs;
	        &sub_headings;
		&sub_emphasis;
		print;
            }
	}
	print "</dl>\n";
    }
    #
    # Convert lists
    s,\@itemize.*,<ul>,;
    s,\@end\s*itemize,</ul>,;
    s,\@enumerate,<ol>,;
    s,\@end\s*enumerate,</ol>,;
    s,\@itemx,<li>,;
    s,\@item,<li>,;
    s,\@bullet,,;		# HTML does not use
    #
    # Multiple blanks lines are paragraph separators in texinfo
    if (/^$/o) {
	if ($allow_P) {
	    print "<p>";
	}
    	$allow_P = 0;
    } elsif (/<H/o) {
    	$allow_P = 0;
    } else {
    	$allow_P = 1;
    }
    #
    # Finally, print modified line
    print;
} 
#print "____________________________________________________________________\n";
#print "<ADDRESS> $title / $author </ADDRESS>\n";

print "</BODY>\n";
print "</HTML>\n";

sub process_examples() {
    if (/\@example/o) {
	s,\s*\@example,<TABLE SUMMARY="Example" BORDER="0" BGCOLOR="#efefef" WIDTH="100%">\n<TR>\n<TD>\n<PRE>\n<font color="$ex_color">,;
	$in_example = 1;
    } elsif (/^\s*\@end example/o) {
	s,\s*\@end example,</font></PRE>\n</TD>\n</TR>\n</TABLE>,;
	$in_example = 0;
    } else {
        s,\@value\{([^}]*)\},$value{$1},g; # Substitute set/value pair
        s,\@},},og;
        s,\@\{,{,og;
    }
}


sub sub_refs {
    die "line $. of file: cannot have multiple refs on one line" if (/\@[px]*ref\{(.*)\}(.*)\@[px]*ref\{(.*)\}/);
    # anchors
    s|\@anchor\{([^}]*)\}|<a name=\"$1\"></a>|g;
    # Change e.g. 
    #     @xref{Viewing}
    # into
    #     <a href="#Viewing">see Viewing</a>

    #if (/\@ref/){print "AAA[$_]AAA\n";
    s|\@ref\{([^}]*)\}|see <a href="#$1">$1</a>|g;
    #print "BBB[$_]BBB\n";}

    s|\@xref\{([^}]*)\}|see <a href="#$1">$1</a>|g;
    s|\@pxref\{([^}]*)\}|see <a href="#$1">$1.</a>|g;
    while (/\@url\{([^}]*)\}/) {
	$the_url = $1;
        s:\@url\{[^}]*\}:<a href="$the_url">\@code{$the_url}</a>:;
    }
    while (/\@uref\{([^}]*)\}/) {
	@items = split(/,/, $1);
        if ($#items == 0) {
            s:\@uref\{[^}]*\}:<a href="$items[0]">$items[0]</a>:;
        } elsif ($#items == 1) {
            s:\@uref\{[^}]*\}:<a href="$items[0]">$items[1]</a>:;
        } elsif ($#items == 2) {
            s:\@uref\{[^}]*\}:\@code{$items[2]}:;
        } else {
            die "Cannot have more than 3 items in a 'uref' at \"$_\"";
        }
    }
}

sub sub_headings {
    if (/\@chapter/) {
	$CH++;
	$SEC1 = 0;
	s,\@chapter\s*(.*),<h1>$CH: $1</h1>,;
    } elsif (/\@unnumbered/) {
	$CH++;
	$SEC1 = 0;
	s,\@unnumbered\s*(.*),<h1>$CH: $1</h1>,;
    } elsif (/\@section/) {
	$SEC1++;
	$SEC2 = 0;
	s,\@section\s*(.*),<h2>$CH.$SEC1: $1</h2>,;
    } elsif (/\@subsection/) {
	$SEC2++;
	$SEC3 = 0;
	s,\@subsection\s*(.*),<h3>$CH.$SEC1.$SEC2: $1</h3>,;
    } elsif (/\@subsubsection/) { 
	$SEC3++;
	s,\@subsubsection\s*(.*),<h4>$CH.$SEC1.$SEC2.$SEC3: $1</h4>,;
    }
}

sub sub_emphasis {
    s,<<,&lt&lt,g;
    s,>>,&gt&gt,g;
    s,\@emph\{([^}]*)\},<em>$1</em>,g;
    s,\@strong\{([^}]*)\},<b>$1</b>,g;
    s,\@footnote\{([^}]*)\}, [$1],g;
    s,\@b\{([^}]*)\},<b>$1</b>,g;

    s,\@code\{([^}]*)\},`<font color="$ex_color"><code>$1</code></font>',g;
    s,\@\@,\@,g;
    s,\@samp\{([^}]*)\},`<font color="$ex_color"><samp>$1</samp></font>',g;
    s,\@key\{([^}]*)\},`<font color="$ex_color"><kbd>$1</kbd></font>',g;
    s,\@kbd\{([^}]*)\},`<font color="$ex_color"><kbd>$1</kbd></font>',g;
    s,\@file\{([^}]*)\},`<font color="$ex_color"><samp>$1</samp></font>',g;
    s,TEXINFO2HTML-CLOSE-BRACE,},g;
    s,TEXINFO2HTML-AT-AT,\@,g;
    s,TEXINFO2HTML-ACCENT-ACUTE-a,&#225,g;
    s,TEXINFO2HTML-ACCENT-ACUTE-e,&#233,g;
    s,TEXINFO2HTML-ACCENT-ACUTE-o,&#243,g;
}