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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
|
#! /usr/local/bin/python
# Convert GNU texinfo files into HTML, one file per node.
# Based on Texinfo 2.14.
# Usage: texi2html [-d] [-d] inputfile outputdirectory
# The input file must be a complete texinfo file, e.g. emacs.texi.
# This creates many files (one per info node) in the output directory,
# overwriting existing files of the same name. All files created have
# ".html" as their extension.
# XXX To do:
# - handle @comment*** correctly
# - handle @xref {some words} correctly
# - handle @ftable correctly (items aren't indexed?)
# - handle @itemx properly
# - handle @exdent properly
# - add links directly to the proper line from indices
# - check against the definitive list of @-cmds; we still miss (among others):
# - @set, @clear, @ifset, @ifclear
# - @defindex (hard)
# - @c(omment) in the middle of a line (rarely used)
# - @this* (not really needed, only used in headers anyway)
# - @today{} (ever used outside title page?)
import os
import regex
import regsub
import string
MAGIC = '\\input texinfo'
cmprog = regex.compile('^@\([a-z]+\)\([ \t]\|$\)') # Command (line-oriented)
blprog = regex.compile('^[ \t]*$') # Blank line
kwprog = regex.compile('@[a-z]+') # Keyword (embedded, usually with {} args)
spprog = regex.compile('[\n@{}&<>]') # Special characters in running text
miprog = regex.compile( \
'^\* \([^:]*\):\(:\|[ \t]*\([^\t,\n.]+\)\([^ \t\n]*\)\)[ \t\n]*')
# menu item (Yuck!)
class TexinfoParser:
# Initialize an instance
def __init__(self):
self.unknown = {} # statistics about unknown @-commands
self.debugging = 0 # larger values produce more output
self.nodefp = None # open file we're writing to
self.savetext = None # If not None, save text head instead
self.dirname = 'tmp' # directory where files are created
self.includedir = '.' # directory to search @include files
self.nodename = '' # name of current node
self.topname = '' # name of top node (first node seen)
self.title = '' # title of this whole Texinfo tree
self.resetindex() # Reset all indices
self.contents = [] # Reset table of contents
self.numbering = [] # Reset section numbering counters
self.nofill = 0 # Normal operation: fill paragraphs
# XXX The following should be reset per node?!
self.footnotes = [] # Reset list of footnotes
self.itemarg = None # Reset command used by @item
self.itemnumber = None # Reset number for @item in @enumerate
self.itemindex = None # Reset item index name
# Set (output) directory name
def setdirname(self, dirname):
self.dirname = dirname
# Set include directory name
def setincludedir(self, includedir):
self.includedir = includedir
# Parse the contents of an entire file
def parse(self, fp):
line = fp.readline()
lineno = 1
while line and (line[0] == '%' or blprog.match(line) >= 0):
line = fp.readline()
lineno = lineno + 1
if line[:len(MAGIC)] <> MAGIC:
raise SyntaxError, 'file does not begin with '+`MAGIC`
self.parserest(fp, lineno)
# Parse the contents of a file, not expecting a MAGIC header
def parserest(self, fp, initial_lineno):
lineno = initial_lineno
self.done = 0
self.skip = 0
self.stack = []
accu = []
while not self.done:
line = fp.readline()
if not line:
if accu:
if not self.skip: self.process(accu)
accu = []
if initial_lineno > 0:
print '*** EOF before @bye'
break
lineno = lineno + 1
if cmprog.match(line) >= 0:
a, b = cmprog.regs[1]
cmd = line[a:b]
if cmd in ('noindent', 'refill'):
accu.append(line)
else:
if accu:
if not self.skip:
self.process(accu)
accu = []
self.command(line)
elif blprog.match(line) >= 0:
if accu:
if not self.skip:
self.process(accu)
self.write('<P>\n')
accu = []
else:
# Append the line including trailing \n!
accu.append(line)
#
if self.skip:
print '*** Still skipping at the end'
if self.stack:
print '*** Stack not empty at the end'
print '***', self.stack
# Start saving text in a buffer instead of writing it to a file
def startsaving(self):
if self.savetext <> None:
print '*** Recursively saving text, expect trouble'
self.savetext = ''
# Return the text saved so far and start writing to file again
def collectsavings(self):
savetext = self.savetext
self.savetext = None
return savetext or ''
# Write text to file, or save it in a buffer, or ignore it
def write(self, *args):
text = string.joinfields(args, '')
if self.savetext <> None:
self.savetext = self.savetext + text
elif self.nodefp:
self.nodefp.write(text)
# Complete the current node -- write footnotes and close file
def endnode(self):
if self.savetext <> None:
print '*** Still saving text at end of node'
dummy = self.collectsavings()
if self.footnotes:
self.writefootnotes()
if self.nodefp:
self.nodefp.close()
self.nodefp = None
self.nodename = ''
# Process a list of lines, expanding embedded @-commands
# This mostly distinguishes between menus and normal text
def process(self, accu):
if self.debugging > 1:
print self.skip, self.stack,
if accu: print accu[0][:30],
if accu[0][30:] or accu[1:]: print '...',
print
if self.stack and self.stack[-1] == 'menu':
# XXX should be done differently
for line in accu:
if miprog.match(line) < 0:
line = string.strip(line) + '\n'
self.expand(line)
continue
(bgn, end), (a, b), (c, d), (e, f), (g, h) = \
miprog.regs[:5]
label = line[a:b]
nodename = line[c:d]
if nodename[0] == ':': nodename = label
else: nodename = line[e:f]
punct = line[g:h]
self.write('<DT><A HREF="', \
makefile(nodename), \
'" TYPE=Menu>', nodename, \
'</A>', punct, '\n<DD>')
self.expand(line[end:])
else:
text = string.joinfields(accu, '')
self.expand(text)
# Write a string, expanding embedded @-commands
def expand(self, text):
stack = []
i = 0
n = len(text)
while i < n:
start = i
i = spprog.search(text, i)
if i < 0:
self.write(text[start:])
break
self.write(text[start:i])
c = text[i]
i = i+1
if c == '\n':
if self.nofill > 0:
self.write('<P>\n')
else:
self.write('\n')
continue
if c == '<':
self.write('<')
continue
if c == '>':
self.write('>')
continue
if c == '&':
self.write('&')
continue
if c == '{':
stack.append('')
continue
if c == '}':
if not stack:
print '*** Unmatched }'
self.write('}')
continue
cmd = stack[-1]
del stack[-1]
try:
method = getattr(self, 'close_' + cmd)
except AttributeError:
self.unknown_close(cmd)
continue
method()
continue
if c <> '@':
# Cannot happen unless spprog is changed
raise RuntimeError, 'unexpected funny '+`c`
start = i
while i < n and text[i] in string.letters: i = i+1
if i == start:
# @ plus non-letter: literal next character
i = i+1
c = text[start:i]
if c == ':':
# `@:' means no extra space after
# preceding `.', `?', `!' or `:'
pass
else:
# `@.' means a sentence-ending period;
# `@@', `@{', `@}' quote `@', `{', `}'
self.write(c)
continue
cmd = text[start:i]
if i < n and text[i] == '{':
i = i+1
stack.append(cmd)
try:
method = getattr(self, 'open_' + cmd)
except AttributeError:
self.unknown_open(cmd)
continue
method()
continue
try:
method = getattr(self, 'handle_' + cmd)
except AttributeError:
self.unknown_handle(cmd)
continue
method()
if stack:
print '*** Stack not empty at para:', stack
# --- Handle unknown embedded @-commands ---
def unknown_open(self, cmd):
print '*** No open func for @' + cmd + '{...}'
cmd = cmd + '{'
self.write('@', cmd)
if not self.unknown.has_key(cmd):
self.unknown[cmd] = 1
else:
self.unknown[cmd] = self.unknown[cmd] + 1
def unknown_close(self, cmd):
print '*** No close func for @' + cmd + '{...}'
cmd = '}' + cmd
self.write('}')
if not self.unknown.has_key(cmd):
self.unknown[cmd] = 1
else:
self.unknown[cmd] = self.unknown[cmd] + 1
def unknown_handle(self, cmd):
print '*** No handler for @' + cmd
self.write('@', cmd)
if not self.unknown.has_key(cmd):
self.unknown[cmd] = 1
else:
self.unknown[cmd] = self.unknown[cmd] + 1
# XXX The following sections should be ordered as the texinfo docs
# --- Embedded @-commands without {} argument list --
def handle_noindent(self): pass
def handle_refill(self): pass
# --- Include file handling ---
def do_include(self, args):
file = args
file = os.path.join(self.includedir, file)
try:
fp = open(file, 'r')
except IOError, msg:
print '*** Can\'t open include file', `file`
return
if self.debugging:
print '--> file', `file`
save_done = self.done
save_skip = self.skip
save_stack = self.stack
self.parserest(fp, 0)
fp.close()
self.done = save_done
self.skip = save_skip
self.stack = save_stack
if self.debugging:
print '<-- file', `file`
# --- Special Insertions ---
def open_dmn(self): pass
def close_dmn(self): pass
def open_dots(self): self.write('...')
def close_dots(self): pass
def open_bullet(self): self.write('•')
def close_bullet(self): pass
def open_TeX(self): self.write('TeX')
def close_TeX(self): pass
def open_copyright(self): self.write('(C)')
def close_copyright(self): pass
def open_minus(self): self.write('-')
def close_minus(self): pass
# --- Special Glyphs for Examples ---
def open_result(self): self.write('=>')
def close_result(self): pass
def open_expansion(self): self.write('==>')
def close_expansion(self): pass
def open_print(self): self.write('-|')
def close_print(self): pass
def open_error(self): self.write('error-->')
def close_error(self): pass
def open_equiv(self): self.write('==')
def close_equiv(self): pass
def open_point(self): self.write('-!-')
def close_point(self): pass
# --- Cross References ---
def open_pxref(self):
self.write('see ')
self.startsaving()
def close_pxref(self):
self.makeref()
def open_xref(self):
self.write('See ')
self.startsaving()
def close_xref(self):
self.makeref()
def open_ref(self):
self.startsaving()
def close_ref(self):
self.makeref()
def open_inforef(self):
self.write('See info file ')
self.startsaving()
def close_inforef(self):
text = self.collectsavings()
args = string.splitfields(text, ',')
n = len(args)
for i in range(n):
args[i] = string.strip(args[i])
while len(args) < 3: args.append('')
node = args[0]
file = args[2]
self.write('`', file, '\', node `', node, '\'')
def makeref(self):
text = self.collectsavings()
args = string.splitfields(text, ',')
n = len(args)
for i in range(n):
args[i] = string.strip(args[i])
while len(args) < 5: args.append('')
nodename = label = args[0]
if args[2]: label = args[2]
file = args[3]
title = args[4]
href = makefile(nodename)
if file:
href = '../' + file + '/' + href
self.write('<A HREF="', href, '">', label, '</A>')
# --- Marking Words and Phrases ---
# --- Other @xxx{...} commands ---
def open_(self): pass # Used by {text enclosed in braces}
def close_(self): pass
open_asis = open_
close_asis = close_
def open_cite(self): self.write('<CITE>')
def close_cite(self): self.write('</CITE>')
def open_code(self): self.write('<CODE>')
def close_code(self): self.write('</CODE>')
open_t = open_code
close_t = close_code
def open_dfn(self): self.write('<DFN>')
def close_dfn(self): self.write('</DFN>')
def open_emph(self): self.write('<I>')
def close_emph(self): self.write('</I>')
open_i = open_emph
close_i = close_emph
def open_footnote(self):
if self.savetext <> None:
print '*** Recursive footnote -- expect weirdness'
id = len(self.footnotes) + 1
self.write('<A NAME="footnoteref', `id`, \
'" HREF="#footnotetext', `id`, '">(', `id`, ')</A>')
self.savetext = ''
def close_footnote(self):
id = len(self.footnotes) + 1
self.footnotes.append(`id`, self.savetext)
self.savetext = None
def writefootnotes(self):
self.write('<H2>---------- Footnotes ----------</H2>\n')
for id, text in self.footnotes:
self.write('<A NAME="footnotetext', id, \
'" HREF="#footnoteref', id, '">(', \
id, ')</A>\n', text, '<P>\n')
self.footnotes = []
def open_file(self): self.write('<FILE>')
def close_file(self): self.write('</FILE>')
def open_kbd(self): self.write('<KBD>')
def close_kbd(self): self.write('</KBD>')
def open_key(self): self.write('<KEY>')
def close_key(self): self.write('</KEY>')
def open_r(self): self.write('<R>')
def close_r(self): self.write('</R>')
def open_samp(self): self.write('`<SAMP>')
def close_samp(self): self.write('</SAMP>\'')
def open_sc(self): self.write('<SMALLCAPS>')
def close_sc(self): self.write('</SMALLCAPS>')
def open_strong(self): self.write('<B>')
def close_strong(self): self.write('</B>')
open_b = open_strong
close_b = close_strong
def open_var(self): self.write('<VAR>')
def close_var(self): self.write('</VAR>')
def open_w(self): self.write('<NOBREAK>')
def close_w(self): self.write('</NOBREAK>')
open_titlefont = open_
close_titlefont = close_
def command(self, line):
a, b = cmprog.regs[1]
cmd = line[a:b]
args = string.strip(line[b:])
if self.debugging > 1:
print self.skip, self.stack, '@' + cmd, args
try:
func = getattr(self, 'do_' + cmd)
except AttributeError:
try:
func = getattr(self, 'bgn_' + cmd)
except AttributeError:
self.unknown_cmd(cmd, args)
return
self.stack.append(cmd)
func(args)
return
if not self.skip or cmd == 'end':
func(args)
def unknown_cmd(self, cmd, args):
print '*** unknown', '@' + cmd, args
if not self.unknown.has_key(cmd):
self.unknown[cmd] = 1
else:
self.unknown[cmd] = self.unknown[cmd] + 1
def do_end(self, args):
words = string.split(args)
if not words:
print '*** @end w/o args'
else:
cmd = words[0]
if not self.stack or self.stack[-1] <> cmd:
print '*** @end', cmd, 'unexpected'
else:
del self.stack[-1]
try:
func = getattr(self, 'end_' + cmd)
except AttributeError:
self.unknown_end(cmd)
return
func()
def unknown_end(self, cmd):
cmd = 'end ' + cmd
print '*** unknown', '@' + cmd
if not self.unknown.has_key(cmd):
self.unknown[cmd] = 1
else:
self.unknown[cmd] = self.unknown[cmd] + 1
# --- Comments ---
def do_comment(self, args): pass
do_c = do_comment
# --- Conditional processing ---
def bgn_ifinfo(self, args): pass
def end_ifinfo(self): pass
def bgn_iftex(self, args): self.skip = self.skip + 1
def end_iftex(self): self.skip = self.skip - 1
def bgn_ignore(self, args): self.skip = self.skip + 1
def end_ignore(self): self.skip = self.skip - 1
def bgn_tex(self, args): self.skip = self.skip + 1
def end_tex(self): self.skip = self.skip - 1
# --- Beginning a file ---
do_finalout = do_comment
do_setchapternewpage = do_comment
do_setfilename = do_comment
def do_settitle(self, args):
self.title = args
# --- Ending a file ---
def do_bye(self, args):
self.done = 1
# --- Title page ---
def bgn_titlepage(self, args): self.skip = self.skip + 1
def end_titlepage(self): self.skip = self.skip - 1
def do_center(self, args):
# Actually not used outside title page...
self.write('<H1>', args, '</H1>\n')
do_title = do_center
do_subtitle = do_center
do_author = do_center
do_vskip = do_comment
do_vfill = do_comment
do_smallbook = do_comment
do_paragraphindent = do_comment
do_setchapternewpage = do_comment
do_headings = do_comment
do_footnotestyle = do_comment
do_evenheading = do_comment
do_evenfooting = do_comment
do_oddheading = do_comment
do_oddfooting = do_comment
do_everyheading = do_comment
do_everyfooting = do_comment
# --- Nodes ---
def do_node(self, args):
parts = string.splitfields(args, ',')
while len(parts) < 4: parts.append('')
for i in range(4): parts[i] = string.strip(parts[i])
[name, next, prev, up] = parts[:4]
self.endnode()
file = self.dirname + '/' + makefile(name)
if self.debugging: print '--- writing', file
self.nodefp = open(file, 'w')
self.nodename = name
if not self.topname: self.topname = name
title = name
if self.title: title = title + ' -- ' + self.title
self.write('<TITLE>', title, '</TITLE>\n')
self.link('Next', next)
self.link('Prev', prev)
self.link('Up', up)
if self.nodename <> self.topname:
self.link('Top', self.topname)
def link(self, label, nodename):
if nodename:
if string.lower(nodename) == '(dir)':
addr = '../dir.html'
else:
addr = makefile(nodename)
self.write(label, ': <A HREF="', addr, '" TYPE="', \
label, '">', nodename, '</A> \n')
# --- Sectioning commands ---
def do_chapter(self, args):
self.heading('H1', args, 0)
def do_unnumbered(self, args):
self.heading('H1', args, -1)
def do_appendix(self, args):
self.heading('H1', args, -1)
def do_top(self, args):
self.heading('H1', args, -1)
def do_chapheading(self, args):
self.heading('H1', args, -1)
def do_majorheading(self, args):
self.heading('H1', args, -1)
def do_section(self, args):
self.heading('H1', args, 1)
def do_unnumberedsec(self, args):
self.heading('H1', args, -1)
def do_appendixsec(self, args):
self.heading('H1', args, -1)
do_appendixsection = do_appendixsec
def do_heading(self, args):
self.heading('H1', args, -1)
def do_subsection(self, args):
self.heading('H2', args, 2)
def do_unnumberedsubsec(self, args):
self.heading('H2', args, -1)
def do_appendixsubsec(self, args):
self.heading('H2', args, -1)
def do_subheading(self, args):
self.heading('H2', args, -1)
def do_subsubsection(self, args):
self.heading('H3', args, 3)
def do_unnumberedsubsubsec(self, args):
self.heading('H3', args, -1)
def do_appendixsubsubsec(self, args):
self.heading('H3', args, -1)
def do_subsubheading(self, args):
self.heading('H3', args, -1)
def heading(self, type, args, level):
if level >= 0:
while len(self.numbering) <= level:
self.numbering.append(0)
del self.numbering[level+1:]
self.numbering[level] = self.numbering[level] + 1
x = ''
for i in self.numbering:
x = x + `i` + '.'
args = x + ' ' + args
self.contents.append(level, args, self.nodename)
self.write('<', type, '>')
self.expand(args)
self.write('</', type, '>\n')
if self.debugging:
print '---', args
def do_contents(self, args):
pass
# self.listcontents('Table of Contents', 999)
def do_shortcontents(self, args):
pass
# self.listcontents('Short Contents', 0)
do_summarycontents = do_shortcontents
def listcontents(self, title, maxlevel):
self.write('<H1>', title, '</H1>\n<UL COMPACT>\n')
for level, title, node in self.contents:
if level <= maxlevel:
self.write('<LI>', '. '*level, '<A HREF="', \
makefile(node), '">')
self.expand(title)
self.write('</A> ', node, '\n')
self.write('</UL>\n')
# --- Page lay-out ---
# These commands are only meaningful in printed text
def do_page(self, args): pass
def do_need(self, args): pass
def bgn_group(self, args): pass
def end_group(self): pass
# --- Line lay-out ---
def do_sp(self, args):
# Insert <args> blank lines
if args:
try:
n = string.atoi(args)
except string.atoi_error:
n = 1
else:
n = 1
self.write('<P>\n'*max(n, 0))
# --- Function and variable definitions ---
def bgn_deffn(self, args):
self.write('<DL><DT>')
words = splitwords(args, 2)
[category, name], rest = words[:2], words[2:]
self.expand('@b{' + name + '}')
for word in rest: self.expand(' ' + makevar(word))
self.expand(' -- ' + category)
self.write('<DD>\n')
self.index('fn', name)
def end_deffn(self):
self.write('</DL>\n')
def bgn_defun(self, args): self.bgn_deffn('Function ' + args)
end_defun = end_deffn
def bgn_defmac(self, args): self.bgn_deffn('Macro ' + args)
end_defmac = end_deffn
def bgn_defspec(self, args): self.bgn_deffn('{Special Form} ' + args)
end_defspec = end_deffn
def bgn_defvr(self, args):
self.write('<DL><DT>')
words = splitwords(args, 2)
[category, name], rest = words[:2], words[2:]
self.expand('@code{' + name + '}')
# If there are too many arguments, show them
for word in rest: self.expand(' ' + word)
self.expand(' -- ' + category)
self.write('<DD>\n')
self.index('vr', name)
end_defvr = end_deffn
def bgn_defvar(self, args): self.bgn_defvr('Variable ' + args)
end_defvar = end_defvr
def bgn_defopt(self, args): self.bgn_defvr('{User Option} ' + args)
end_defopt = end_defvr
# --- Ditto for typed languages ---
def bgn_deftypefn(self, args):
self.write('<DL><DT>')
words = splitwords(args, 3)
[category, datatype, name], rest = words[:3], words[3:]
self.expand('@code{' + datatype + '} @b{' + name + '}')
for word in rest: self.expand(' ' + makevar(word))
self.expand(' -- ' + category)
self.write('<DD>\n')
self.index('fn', name)
end_deftypefn = end_deffn
def bgn_deftypefun(self, args): self.bgn_deftypefn('Function ' + args)
end_deftypefun = end_deftypefn
def bgn_deftypevr(self, args):
words = splitwords(args, 3)
[category, datatype, name], rest = words[:3], words[3:]
self.write('<DL><DT>')
self.expand('@code{' + datatype + '} @b{' + name + '}')
# If there are too many arguments, show them
for word in rest: self.expand(' ' + word)
self.expand(' -- ' + category)
self.write('<DD>\n')
self.index('fn', name)
end_deftypevr = end_deftypefn
def bgn_deftypevar(self, args):
self.bgn_deftypevr('Variable ' + args)
end_deftypevar = end_deftypevr
# --- Ditto for object-oriented languages ---
def bgn_defcv(self, args):
words = splitwords(args, 3)
[category, classname, name], rest = words[:3], words[3:]
self.write('<DL><DT>')
self.expand('@b{' + name + '}')
# If there are too many arguments, show them
for word in rest: self.expand(' ' + word)
self.expand(' -- ' + category + ' of ' + classname)
self.write('<DD>\n')
self.index('vr', name + ' @r{of ' + classname + '}')
end_defcv = end_deftypevr
def bgn_defivar(self, args):
self.bgn_defcv('{Instance Variable} ' + args)
end_defivar = end_defcv
def bgn_defop(self, args):
self.write('<DL><DT>')
words = splitwords(args, 3)
[category, classname, name], rest = words[:3], words[3:]
self.expand('@b{' + name + '}')
for word in rest: self.expand(' ' + makevar(word))
self.expand(' -- ' + category + ' on ' + classname)
self.write('<DD>\n')
self.index('fn', name + ' @r{on ' + classname + '}')
end_defop = end_defcv
def bgn_defmethod(self, args):
self.bgn_defop('Method ' + args)
end_defmethod = end_defop
# --- Ditto for data types ---
def bgn_deftp(self, args):
self.write('<DL><DT>')
words = splitwords(args, 2)
[category, name], rest = words[:2], words[2:]
self.expand('@b{' + name + '}')
for word in rest: self.expand(' ' + word)
self.expand(' -- ' + category)
self.write('<DD>\n')
self.index('tp', name)
end_deftp = end_defcv
# --- Making Lists and Tables
def bgn_enumerate(self, args):
if not args: args = '1'
self.itemnumber = args
self.write('<UL>\n')
def end_enumerate(self):
self.itemnumber = None
self.write('</UL>\n')
def bgn_itemize(self, args):
self.itemarg = args
self.write('<UL>\n')
def end_itemize(self):
self.itemarg = None
self.write('</UL>\n')
def bgn_table(self, args):
self.itemarg = args
self.write('<DL>\n')
def end_table(self):
self.itemarg = None
self.write('</DL>\n')
def bgn_ftable(self, args):
self.itemindex = 'fn'
self.bgn_table(args)
def end_ftable(self):
self.itemindex = None
self.end_table()
def do_item(self, args):
if self.itemindex: self.index(self.itemindex, args)
if self.itemarg:
if self.itemarg[0] == '@' and self.itemarg[1:2] and \
self.itemarg[1] in string.letters:
args = self.itemarg + '{' + args + '}'
else:
# some other character, e.g. '-'
args = self.itemarg + ' ' + args
if self.itemnumber <> None:
args = self.itemnumber + '. ' + args
self.itemnumber = increment(self.itemnumber)
if self.stack and self.stack[-1] == 'table':
self.write('<DT>')
self.expand(args)
self.write('<DD>')
else:
self.write('<LI>')
self.expand(args)
self.write(' ')
do_itemx = do_item # XXX Should suppress leading blank line
# --- Enumerations, displays, quotations ---
# XXX Most of these should increase the indentation somehow
def bgn_quotation(self, args): self.write('<P>')
def end_quotation(self): self.write('<P>\n')
def bgn_example(self, args):
self.nofill = self.nofill + 1
self.write('<UL COMPACT><CODE>')
def end_example(self):
self.write('</CODE></UL>\n')
self.nofill = self.nofill - 1
bgn_lisp = bgn_example # Synonym when contents are executable lisp code
end_lisp = end_example
bgn_smallexample = bgn_example # XXX Should use smaller font
end_smallexample = end_example
bgn_smalllisp = bgn_lisp # Ditto
end_smalllisp = end_lisp
def bgn_display(self, args):
self.nofill = self.nofill + 1
self.write('<UL COMPACT>\n')
def end_display(self):
self.write('</UL>\n')
self.nofill = self.nofill - 1
def bgn_format(self, args):
self.nofill = self.nofill + 1
self.write('<UL COMPACT>\n')
def end_format(self):
self.write('</UL>\n')
self.nofill = self.nofill - 1
def do_exdent(self, args): self.expand(args + '\n')
# XXX Should really mess with indentation
def bgn_flushleft(self, args):
self.nofill = self.nofill + 1
self.write('<UL COMPACT>\n')
def end_flushleft(self):
self.write('</UL>\n')
self.nofill = self.nofill - 1
def bgn_flushright(self, args):
self.nofill = self.nofill + 1
self.write('<ADDRESS COMPACT>\n')
def end_flushright(self):
self.write('</ADDRESS>\n')
self.nofill = self.nofill - 1
def bgn_menu(self, args): self.write('<H2>Menu</H2><DL COMPACT>\n')
def end_menu(self): self.write('</DL>\n')
def bgn_cartouche(self, args): pass
def end_cartouche(self): pass
# --- Indices ---
def resetindex(self):
self.noncodeindices = ['cp']
self.indextitle = {}
self.indextitle['cp'] = 'Concept'
self.indextitle['fn'] = 'Function'
self.indextitle['ky'] = 'Keyword'
self.indextitle['pg'] = 'Program'
self.indextitle['tp'] = 'Type'
self.indextitle['vr'] = 'Variable'
#
self.whichindex = {}
for name in self.indextitle.keys():
self.whichindex[name] = []
def user_index(self, name, args):
if self.whichindex.has_key(name):
self.index(name, args)
else:
print '*** No index named', `name`
def do_cindex(self, args): self.index('cp', args)
def do_findex(self, args): self.index('fn', args)
def do_kindex(self, args): self.index('ky', args)
def do_pindex(self, args): self.index('pg', args)
def do_tindex(self, args): self.index('tp', args)
def do_vindex(self, args): self.index('vr', args)
def index(self, name, args):
self.whichindex[name].append(args, self.nodename)
def do_synindex(self, args):
words = string.split(args)
if len(words) <> 2:
print '*** bad @synindex', args
return
[old, new] = words
if not self.whichindex.has_key(old) or \
not self.whichindex.has_key(new):
print '*** bad key(s) in @synindex', args
return
if old <> new and \
self.whichindex[old] is not self.whichindex[new]:
inew = self.whichindex[new]
inew[len(inew):] = self.whichindex[old]
self.whichindex[old] = inew
do_syncodeindex = do_synindex # XXX Should use code font
def do_printindex(self, args):
words = string.split(args)
for name in words:
if self.whichindex.has_key(name):
self.prindex(name)
else:
print '*** No index named', `name`
def prindex(self, name):
iscodeindex = (name not in self.noncodeindices)
index = self.whichindex[name]
if not index: return
if self.debugging:
print '--- Generating', self.indextitle[name], 'index'
# The node already provides a title
index1 = []
junkprog = regex.compile('^\(@[a-z]+\)?{')
for key, node in index:
sortkey = string.lower(key)
# Remove leading `@cmd{' from sort key
# -- don't bother about the matching `}'
oldsortkey = sortkey
while 1:
i = junkprog.match(sortkey)
if i < 0: break
sortkey = sortkey[i:]
index1.append(sortkey, key, node)
del index[:]
index1.sort()
self.write('<DL COMPACT>\n')
for sortkey, key, node in index1:
if self.debugging > 1: print key, ':', node
self.write('<DT>')
if iscodeindex: key = '@code{' + key + '}'
self.expand(key)
self.write('<DD><A HREF="', makefile(node), \
'">', node, '</A>\n')
self.write('</DL>\n')
# --- Final error reports ---
def report(self):
if self.unknown:
print '--- Unrecognized commands ---'
cmds = self.unknown.keys()
cmds.sort()
for cmd in cmds:
print string.ljust(cmd, 20), self.unknown[cmd]
# Put @var{} around alphabetic substrings
def makevar(str):
# XXX This breaks if str contains @word{...}
return regsub.gsub('\([a-zA-Z_][a-zA-Z0-9_]*\)', '@var{\\1}', str)
# Split a string in "words" according to findwordend
def splitwords(str, minlength):
words = []
i = 0
n = len(str)
while i < n:
while i < n and str[i] in ' \t\n': i = i+1
if i >= n: break
start = i
i = findwordend(str, i, n)
words.append(str[start:i])
while len(words) < minlength: words.append('')
return words
# Find the end of a "word", matching braces and interpreting @@ @{ @}
fwprog = regex.compile('[@{} ]')
def findwordend(str, i, n):
level = 0
while i < n:
i = fwprog.search(str, i)
if i < 0: break
c = str[i]; i = i+1
if c == '@': i = i+1 # Next character is not special
elif c == '{': level = level+1
elif c == '}': level = level-1
elif c == ' ' and level <= 0: return i-1
return n
# Convert a node name into a file name
def makefile(nodename):
return string.lower(fixfunnychars(nodename)) + '.html'
# Characters that are perfectly safe in filenames and hyperlinks
goodchars = string.letters + string.digits + '!@-_=+.'
# Replace characters that aren't perfectly safe by underscores
def fixfunnychars(addr):
i = 0
while i < len(addr):
c = addr[i]
if c not in goodchars:
c = '_'
addr = addr[:i] + c + addr[i+1:]
i = i + len(c)
return addr
# Increment a string used as an enumeration
def increment(s):
if not s:
return '1'
for sequence in string.digits, string.lowercase, string.uppercase:
lastc = s[-1]
if lastc in sequence:
i = string.index(sequence, lastc) + 1
if i >= len(sequence):
if len(s) == 1:
s = sequence[0]*2
if s == '00':
s = '10'
else:
s = increment(s[:-1]) + sequence[0]
else:
s = s[:-1] + sequence[i]
return s
return s # Don't increment
def test():
import sys
parser = TexinfoParser()
while sys.argv[1:2] == ['-d']:
parser.debugging = parser.debugging + 1
del sys.argv[1:2]
if len(sys.argv) <> 3:
print 'usage: texi2html [-d] [-d] inputfile outputdirectory'
sys.exit(2)
file = sys.argv[1]
parser.setdirname(sys.argv[2])
if file == '-':
fp = sys.stdin
else:
parser.setincludedir(os.path.dirname(file))
try:
fp = open(file, 'r')
except IOError, msg:
print file, ':', msg
sys.exit(1)
parser.parse(fp)
fp.close()
parser.report()
test()
|