blob: 508428425793a2d87979dde8a0692914946d3b67 [file] [log] [blame]
Guido van Rossum26a9d371995-03-15 11:26:05 +00001#! /usr/local/bin/python
2
3# Convert GNU texinfo files into HTML, one file per node.
4# Based on Texinfo 2.14.
Guido van Rossum06f42891995-08-28 03:01:00 +00005# Usage: texi2html [-d] [-d] [-c] inputfile outputdirectory
Guido van Rossum26a9d371995-03-15 11:26:05 +00006# The input file must be a complete texinfo file, e.g. emacs.texi.
7# This creates many files (one per info node) in the output directory,
8# overwriting existing files of the same name. All files created have
9# ".html" as their extension.
10
11
12# XXX To do:
13# - handle @comment*** correctly
14# - handle @xref {some words} correctly
15# - handle @ftable correctly (items aren't indexed?)
16# - handle @itemx properly
17# - handle @exdent properly
18# - add links directly to the proper line from indices
19# - check against the definitive list of @-cmds; we still miss (among others):
Guido van Rossum26a9d371995-03-15 11:26:05 +000020# - @defindex (hard)
21# - @c(omment) in the middle of a line (rarely used)
22# - @this* (not really needed, only used in headers anyway)
23# - @today{} (ever used outside title page?)
24
Guido van Rossum06f42891995-08-28 03:01:00 +000025# More consistent handling of chapters/sections/etc.
26# Lots of documentation
27# Many more options:
28# -top designate top node
29# -links customize which types of links are included
30# -split split at chapters or sections instead of nodes
31# -name Allow different types of filename handling. Non unix systems
32# will have problems with long node names
33# ...
34# Support the most recent texinfo version and take a good look at HTML 3.0
35# More debugging output (customizable) and more fexible error handling
36# How about icons ?
Guido van Rossum26a9d371995-03-15 11:26:05 +000037
38import os
39import regex
40import regsub
41import string
42
43MAGIC = '\\input texinfo'
44
45cmprog = regex.compile('^@\([a-z]+\)\([ \t]\|$\)') # Command (line-oriented)
46blprog = regex.compile('^[ \t]*$') # Blank line
47kwprog = regex.compile('@[a-z]+') # Keyword (embedded, usually with {} args)
48spprog = regex.compile('[\n@{}&<>]') # Special characters in running text
49miprog = regex.compile( \
50 '^\* \([^:]*\):\(:\|[ \t]*\([^\t,\n.]+\)\([^ \t\n]*\)\)[ \t\n]*')
51 # menu item (Yuck!)
52
Guido van Rossum06f42891995-08-28 03:01:00 +000053
54class Node:
55 __doc__ = """
56 Some of the parser's functionality is separated into this class.
57
58 A Node accumulates its contents, takes care of links to other Nodes
59 and saves itself when it is finished and all links are resolved. """
60
61 def __init__ (self, dir, name, topname, title, next, prev, up):
62 self.dirname = dir
63 self.name = name
64 if topname:
65 self.topname = topname
66 else:
67 self.topname = name
68 self.title = title
69 self.next = next
70 self.prev = prev
71 self.up = up
72 self.lines = []
73 self.type = 0
74 self.cont = ''
75
76 def write (self, *lines):
Guido van Rossum29901ff1996-08-09 21:46:34 +000077 map(self.lines.append, lines)
Guido van Rossum06f42891995-08-28 03:01:00 +000078
79 def flush (self):
80 fp = open (self.dirname + '/' + makefile(self.name), 'w')
81 fp.write (self.prologue)
82 fp.write (self.text)
83 fp.write (self.epilogue)
84 fp.close ()
85
86
87 def link(self, label, nodename):
88 if nodename:
89 if string.lower(nodename) == '(dir)':
90 addr = '../dir.html'
91 else:
92 addr = makefile(nodename)
93 self.write(label, ': <A HREF="', addr, '" TYPE="', \
94 label, '">', nodename, '</A> \n')
95
96
97 def finalize(self):
98 length = len (self.lines)
99 self.text = string.joinfields (self.lines, '')
100 self.lines = []
Guido van Rossum29901ff1996-08-09 21:46:34 +0000101 self.write ('<HR>\n')
Guido van Rossum06f42891995-08-28 03:01:00 +0000102 if self.cont != self.next:
103 self.link('Cont', self.cont)
104 self.link('Next', self.next)
105 self.link('Prev', self.prev)
106 self.link('Up', self.up)
107 if self.name <> self.topname:
108 self.link('Top', self.topname)
Guido van Rossum29901ff1996-08-09 21:46:34 +0000109 self.write ('<HR>\n')
110 links = string.joinfields(self.lines, '')
Guido van Rossum06f42891995-08-28 03:01:00 +0000111 self.lines = []
112
Guido van Rossum29901ff1996-08-09 21:46:34 +0000113 self.prologue = ('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n'
114 '<!-- Converted with texi2html and Python -->\n'
115 '<HEAD>\n'
116 ' <TITLE>' + self.title + '</TITLE>\n'
117 '</HEAD><BODY>\n' + \
118 links)
Guido van Rossum06f42891995-08-28 03:01:00 +0000119
120 if length > 20:
Guido van Rossum29901ff1996-08-09 21:46:34 +0000121 self.epilogue = '<P>\n%s</BODY>\n' % links
Guido van Rossum06f42891995-08-28 03:01:00 +0000122 else:
123 self.epilogue = '</BODY>\n'
124
125
Guido van Rossum26a9d371995-03-15 11:26:05 +0000126class TexinfoParser:
127
128 # Initialize an instance
129 def __init__(self):
130 self.unknown = {} # statistics about unknown @-commands
Guido van Rossuma12bbff1995-05-03 14:17:36 +0000131 self.filenames = {} # Check for identical filenames
Guido van Rossum26a9d371995-03-15 11:26:05 +0000132 self.debugging = 0 # larger values produce more output
133 self.nodefp = None # open file we're writing to
Guido van Rossuma12bbff1995-05-03 14:17:36 +0000134 self.nodelineno = 0 # Linenumber relative to node
135 self.links = None # Links from current node
Guido van Rossum26a9d371995-03-15 11:26:05 +0000136 self.savetext = None # If not None, save text head instead
Guido van Rossum06f42891995-08-28 03:01:00 +0000137 self.savestack = [] # If not None, save text head instead
Guido van Rossum26a9d371995-03-15 11:26:05 +0000138 self.dirname = 'tmp' # directory where files are created
139 self.includedir = '.' # directory to search @include files
140 self.nodename = '' # name of current node
141 self.topname = '' # name of top node (first node seen)
142 self.title = '' # title of this whole Texinfo tree
143 self.resetindex() # Reset all indices
144 self.contents = [] # Reset table of contents
145 self.numbering = [] # Reset section numbering counters
146 self.nofill = 0 # Normal operation: fill paragraphs
Guido van Rossum06f42891995-08-28 03:01:00 +0000147 self.values={'html': 1} # Names that should be parsed in ifset
Guido van Rossuma12bbff1995-05-03 14:17:36 +0000148 self.stackinfo={} # Keep track of state in the stack
Guido van Rossum26a9d371995-03-15 11:26:05 +0000149 # XXX The following should be reset per node?!
150 self.footnotes = [] # Reset list of footnotes
151 self.itemarg = None # Reset command used by @item
152 self.itemnumber = None # Reset number for @item in @enumerate
153 self.itemindex = None # Reset item index name
Guido van Rossum06f42891995-08-28 03:01:00 +0000154 self.node = None
155 self.nodestack = []
156 self.cont = 0
157 self.includedepth = 0
Guido van Rossum26a9d371995-03-15 11:26:05 +0000158 # Set (output) directory name
159 def setdirname(self, dirname):
160 self.dirname = dirname
161
162 # Set include directory name
163 def setincludedir(self, includedir):
164 self.includedir = includedir
165
166 # Parse the contents of an entire file
167 def parse(self, fp):
168 line = fp.readline()
169 lineno = 1
170 while line and (line[0] == '%' or blprog.match(line) >= 0):
171 line = fp.readline()
172 lineno = lineno + 1
173 if line[:len(MAGIC)] <> MAGIC:
174 raise SyntaxError, 'file does not begin with '+`MAGIC`
175 self.parserest(fp, lineno)
176
177 # Parse the contents of a file, not expecting a MAGIC header
178 def parserest(self, fp, initial_lineno):
179 lineno = initial_lineno
180 self.done = 0
181 self.skip = 0
182 self.stack = []
183 accu = []
184 while not self.done:
185 line = fp.readline()
Guido van Rossuma12bbff1995-05-03 14:17:36 +0000186 self.nodelineno = self.nodelineno + 1
Guido van Rossum26a9d371995-03-15 11:26:05 +0000187 if not line:
188 if accu:
189 if not self.skip: self.process(accu)
190 accu = []
191 if initial_lineno > 0:
192 print '*** EOF before @bye'
193 break
194 lineno = lineno + 1
195 if cmprog.match(line) >= 0:
196 a, b = cmprog.regs[1]
197 cmd = line[a:b]
198 if cmd in ('noindent', 'refill'):
199 accu.append(line)
200 else:
201 if accu:
202 if not self.skip:
203 self.process(accu)
204 accu = []
205 self.command(line)
Guido van Rossuma12bbff1995-05-03 14:17:36 +0000206 elif blprog.match(line) >= 0 and \
207 'format' not in self.stack and \
208 'example' not in self.stack:
Guido van Rossum26a9d371995-03-15 11:26:05 +0000209 if accu:
210 if not self.skip:
211 self.process(accu)
212 self.write('<P>\n')
Guido van Rossuma12bbff1995-05-03 14:17:36 +0000213 accu = []
Guido van Rossum26a9d371995-03-15 11:26:05 +0000214 else:
215 # Append the line including trailing \n!
216 accu.append(line)
217 #
218 if self.skip:
219 print '*** Still skipping at the end'
220 if self.stack:
221 print '*** Stack not empty at the end'
222 print '***', self.stack
Guido van Rossum06f42891995-08-28 03:01:00 +0000223 if self.includedepth == 0:
224 while self.nodestack:
225 self.nodestack[-1].finalize ()
226 self.nodestack[-1].flush ()
227 del self.nodestack [-1]
Guido van Rossum26a9d371995-03-15 11:26:05 +0000228
229 # Start saving text in a buffer instead of writing it to a file
230 def startsaving(self):
231 if self.savetext <> None:
Guido van Rossum06f42891995-08-28 03:01:00 +0000232 self.savestack.append (self.savetext)
233 # print '*** Recursively saving text, expect trouble'
Guido van Rossum26a9d371995-03-15 11:26:05 +0000234 self.savetext = ''
235
236 # Return the text saved so far and start writing to file again
237 def collectsavings(self):
238 savetext = self.savetext
Guido van Rossum06f42891995-08-28 03:01:00 +0000239 if len (self.savestack) > 0:
240 self.savetext = self.savestack[-1]
241 del self.savestack[-1]
242 else:
243 self.savetext = None
Guido van Rossum26a9d371995-03-15 11:26:05 +0000244 return savetext or ''
245
246 # Write text to file, or save it in a buffer, or ignore it
247 def write(self, *args):
Guido van Rossum06f42891995-08-28 03:01:00 +0000248 try:
249 text = string.joinfields(args, '')
250 except:
251 print args
252 raise TypeError
Guido van Rossum26a9d371995-03-15 11:26:05 +0000253 if self.savetext <> None:
254 self.savetext = self.savetext + text
255 elif self.nodefp:
256 self.nodefp.write(text)
Guido van Rossum06f42891995-08-28 03:01:00 +0000257 elif self.node:
258 self.node.write (text)
Guido van Rossum26a9d371995-03-15 11:26:05 +0000259 # Complete the current node -- write footnotes and close file
260 def endnode(self):
261 if self.savetext <> None:
262 print '*** Still saving text at end of node'
263 dummy = self.collectsavings()
264 if self.footnotes:
265 self.writefootnotes()
266 if self.nodefp:
Guido van Rossuma12bbff1995-05-03 14:17:36 +0000267 if self.nodelineno > 20:
268 self.write ('<HR>\n')
269 [name, next, prev, up] = self.nodelinks[:4]
270 self.link('Next', next)
271 self.link('Prev', prev)
272 self.link('Up', up)
273 if self.nodename <> self.topname:
274 self.link('Top', self.topname)
275 self.write ('<HR>\n')
276 self.write('</BODY>\n')
Guido van Rossum26a9d371995-03-15 11:26:05 +0000277 self.nodefp.close()
Guido van Rossum06f42891995-08-28 03:01:00 +0000278 self.nodefp = None
279 elif self.node:
280 if not self.cont and \
281 (not self.node.type or \
282 (self.node.next and self.node.prev and self.node.up)):
283 self.node.finalize ()
284 self.node.flush ()
285 else:
286 self.nodestack.append (self.node)
287 self.node = None
Guido van Rossum26a9d371995-03-15 11:26:05 +0000288 self.nodename = ''
289
290 # Process a list of lines, expanding embedded @-commands
291 # This mostly distinguishes between menus and normal text
292 def process(self, accu):
293 if self.debugging > 1:
294 print self.skip, self.stack,
295 if accu: print accu[0][:30],
296 if accu[0][30:] or accu[1:]: print '...',
297 print
298 if self.stack and self.stack[-1] == 'menu':
299 # XXX should be done differently
300 for line in accu:
301 if miprog.match(line) < 0:
302 line = string.strip(line) + '\n'
303 self.expand(line)
304 continue
305 (bgn, end), (a, b), (c, d), (e, f), (g, h) = \
306 miprog.regs[:5]
307 label = line[a:b]
308 nodename = line[c:d]
309 if nodename[0] == ':': nodename = label
310 else: nodename = line[e:f]
311 punct = line[g:h]
Guido van Rossum29901ff1996-08-09 21:46:34 +0000312 self.write('<LI><A HREF="',
313 makefile(nodename),
314 '">', nodename,
315 '</A>', punct, '\n')
Guido van Rossum26a9d371995-03-15 11:26:05 +0000316 self.expand(line[end:])
317 else:
318 text = string.joinfields(accu, '')
319 self.expand(text)
320
321 # Write a string, expanding embedded @-commands
322 def expand(self, text):
323 stack = []
324 i = 0
325 n = len(text)
326 while i < n:
327 start = i
328 i = spprog.search(text, i)
329 if i < 0:
330 self.write(text[start:])
331 break
332 self.write(text[start:i])
333 c = text[i]
334 i = i+1
335 if c == '\n':
336 if self.nofill > 0:
337 self.write('<P>\n')
338 else:
339 self.write('\n')
340 continue
341 if c == '<':
342 self.write('&lt;')
343 continue
344 if c == '>':
345 self.write('&gt;')
346 continue
347 if c == '&':
348 self.write('&amp;')
349 continue
350 if c == '{':
351 stack.append('')
352 continue
353 if c == '}':
354 if not stack:
355 print '*** Unmatched }'
356 self.write('}')
357 continue
358 cmd = stack[-1]
359 del stack[-1]
360 try:
361 method = getattr(self, 'close_' + cmd)
362 except AttributeError:
363 self.unknown_close(cmd)
364 continue
365 method()
366 continue
367 if c <> '@':
368 # Cannot happen unless spprog is changed
369 raise RuntimeError, 'unexpected funny '+`c`
370 start = i
371 while i < n and text[i] in string.letters: i = i+1
372 if i == start:
373 # @ plus non-letter: literal next character
374 i = i+1
375 c = text[start:i]
376 if c == ':':
377 # `@:' means no extra space after
378 # preceding `.', `?', `!' or `:'
379 pass
380 else:
381 # `@.' means a sentence-ending period;
382 # `@@', `@{', `@}' quote `@', `{', `}'
383 self.write(c)
384 continue
385 cmd = text[start:i]
386 if i < n and text[i] == '{':
387 i = i+1
388 stack.append(cmd)
389 try:
390 method = getattr(self, 'open_' + cmd)
391 except AttributeError:
392 self.unknown_open(cmd)
393 continue
394 method()
395 continue
396 try:
397 method = getattr(self, 'handle_' + cmd)
398 except AttributeError:
399 self.unknown_handle(cmd)
400 continue
401 method()
402 if stack:
403 print '*** Stack not empty at para:', stack
404
405 # --- Handle unknown embedded @-commands ---
406
407 def unknown_open(self, cmd):
408 print '*** No open func for @' + cmd + '{...}'
409 cmd = cmd + '{'
410 self.write('@', cmd)
411 if not self.unknown.has_key(cmd):
412 self.unknown[cmd] = 1
413 else:
414 self.unknown[cmd] = self.unknown[cmd] + 1
415
416 def unknown_close(self, cmd):
417 print '*** No close func for @' + cmd + '{...}'
418 cmd = '}' + cmd
419 self.write('}')
420 if not self.unknown.has_key(cmd):
421 self.unknown[cmd] = 1
422 else:
423 self.unknown[cmd] = self.unknown[cmd] + 1
424
425 def unknown_handle(self, cmd):
426 print '*** No handler for @' + cmd
427 self.write('@', cmd)
428 if not self.unknown.has_key(cmd):
429 self.unknown[cmd] = 1
430 else:
431 self.unknown[cmd] = self.unknown[cmd] + 1
432
433 # XXX The following sections should be ordered as the texinfo docs
434
435 # --- Embedded @-commands without {} argument list --
436
437 def handle_noindent(self): pass
438
439 def handle_refill(self): pass
440
441 # --- Include file handling ---
442
443 def do_include(self, args):
444 file = args
445 file = os.path.join(self.includedir, file)
446 try:
447 fp = open(file, 'r')
448 except IOError, msg:
449 print '*** Can\'t open include file', `file`
450 return
451 if self.debugging:
452 print '--> file', `file`
453 save_done = self.done
454 save_skip = self.skip
455 save_stack = self.stack
Guido van Rossum06f42891995-08-28 03:01:00 +0000456 self.includedepth = self.includedepth + 1
Guido van Rossum26a9d371995-03-15 11:26:05 +0000457 self.parserest(fp, 0)
Guido van Rossum06f42891995-08-28 03:01:00 +0000458 self.includedepth = self.includedepth - 1
Guido van Rossum26a9d371995-03-15 11:26:05 +0000459 fp.close()
460 self.done = save_done
461 self.skip = save_skip
462 self.stack = save_stack
463 if self.debugging:
464 print '<-- file', `file`
465
466 # --- Special Insertions ---
467
468 def open_dmn(self): pass
469 def close_dmn(self): pass
470
471 def open_dots(self): self.write('...')
472 def close_dots(self): pass
473
Guido van Rossuma12bbff1995-05-03 14:17:36 +0000474 def open_bullet(self): pass
Guido van Rossum26a9d371995-03-15 11:26:05 +0000475 def close_bullet(self): pass
476
477 def open_TeX(self): self.write('TeX')
478 def close_TeX(self): pass
479
Guido van Rossuma12bbff1995-05-03 14:17:36 +0000480 def handle_copyright(self): self.write('(C)')
Guido van Rossum06f42891995-08-28 03:01:00 +0000481 def open_copyright(self): self.write('(C)')
482 def close_copyright(self): pass
483
Guido van Rossum26a9d371995-03-15 11:26:05 +0000484 def open_minus(self): self.write('-')
485 def close_minus(self): pass
486
487 # --- Special Glyphs for Examples ---
488
489 def open_result(self): self.write('=&gt;')
490 def close_result(self): pass
491
492 def open_expansion(self): self.write('==&gt;')
493 def close_expansion(self): pass
494
495 def open_print(self): self.write('-|')
496 def close_print(self): pass
497
498 def open_error(self): self.write('error--&gt;')
499 def close_error(self): pass
500
501 def open_equiv(self): self.write('==')
502 def close_equiv(self): pass
503
504 def open_point(self): self.write('-!-')
505 def close_point(self): pass
506
507 # --- Cross References ---
508
509 def open_pxref(self):
510 self.write('see ')
511 self.startsaving()
512 def close_pxref(self):
513 self.makeref()
514
515 def open_xref(self):
516 self.write('See ')
517 self.startsaving()
518 def close_xref(self):
519 self.makeref()
520
521 def open_ref(self):
522 self.startsaving()
523 def close_ref(self):
524 self.makeref()
525
526 def open_inforef(self):
527 self.write('See info file ')
528 self.startsaving()
529 def close_inforef(self):
530 text = self.collectsavings()
531 args = string.splitfields(text, ',')
532 n = len(args)
533 for i in range(n):
534 args[i] = string.strip(args[i])
535 while len(args) < 3: args.append('')
536 node = args[0]
537 file = args[2]
538 self.write('`', file, '\', node `', node, '\'')
539
540 def makeref(self):
541 text = self.collectsavings()
542 args = string.splitfields(text, ',')
543 n = len(args)
544 for i in range(n):
545 args[i] = string.strip(args[i])
546 while len(args) < 5: args.append('')
547 nodename = label = args[0]
548 if args[2]: label = args[2]
549 file = args[3]
550 title = args[4]
551 href = makefile(nodename)
552 if file:
553 href = '../' + file + '/' + href
554 self.write('<A HREF="', href, '">', label, '</A>')
555
556 # --- Marking Words and Phrases ---
557
558 # --- Other @xxx{...} commands ---
559
560 def open_(self): pass # Used by {text enclosed in braces}
561 def close_(self): pass
562
563 open_asis = open_
564 close_asis = close_
565
566 def open_cite(self): self.write('<CITE>')
567 def close_cite(self): self.write('</CITE>')
568
569 def open_code(self): self.write('<CODE>')
570 def close_code(self): self.write('</CODE>')
571
572 open_t = open_code
573 close_t = close_code
574
575 def open_dfn(self): self.write('<DFN>')
576 def close_dfn(self): self.write('</DFN>')
577
Guido van Rossum29901ff1996-08-09 21:46:34 +0000578 def open_emph(self): self.write('<EM>')
579 def close_emph(self): self.write('</EM>')
Guido van Rossum26a9d371995-03-15 11:26:05 +0000580
581 open_i = open_emph
582 close_i = close_emph
583
584 def open_footnote(self):
Guido van Rossum06f42891995-08-28 03:01:00 +0000585 # if self.savetext <> None:
586 # print '*** Recursive footnote -- expect weirdness'
Guido van Rossum26a9d371995-03-15 11:26:05 +0000587 id = len(self.footnotes) + 1
588 self.write('<A NAME="footnoteref', `id`, \
589 '" HREF="#footnotetext', `id`, '">(', `id`, ')</A>')
Guido van Rossum06f42891995-08-28 03:01:00 +0000590 # self.savetext = ''
591 self.startsaving ()
Guido van Rossum26a9d371995-03-15 11:26:05 +0000592
593 def close_footnote(self):
594 id = len(self.footnotes) + 1
Guido van Rossum06f42891995-08-28 03:01:00 +0000595 # self.footnotes.append(`id`, self.savetext)
596 self.footnotes.append(`id`, self.collectsavings())
597 # self.savetext = None
Guido van Rossum26a9d371995-03-15 11:26:05 +0000598
599 def writefootnotes(self):
Guido van Rossum29901ff1996-08-09 21:46:34 +0000600 self.write('\n<HR NOSHADE SIZE=1 WIDTH=200>\n'
601 '<STRONG><EM>Footnotes</EM></STRONG>\n<P>')
Guido van Rossum26a9d371995-03-15 11:26:05 +0000602 for id, text in self.footnotes:
603 self.write('<A NAME="footnotetext', id, \
604 '" HREF="#footnoteref', id, '">(', \
605 id, ')</A>\n', text, '<P>\n')
606 self.footnotes = []
607
Guido van Rossum29901ff1996-08-09 21:46:34 +0000608 def open_file(self): self.write('<CODE>')
609 def close_file(self): self.write('</CODE>')
Guido van Rossum26a9d371995-03-15 11:26:05 +0000610
611 def open_kbd(self): self.write('<KBD>')
612 def close_kbd(self): self.write('</KBD>')
613
614 def open_key(self): self.write('<KEY>')
615 def close_key(self): self.write('</KEY>')
616
617 def open_r(self): self.write('<R>')
618 def close_r(self): self.write('</R>')
619
620 def open_samp(self): self.write('`<SAMP>')
621 def close_samp(self): self.write('</SAMP>\'')
622
623 def open_sc(self): self.write('<SMALLCAPS>')
624 def close_sc(self): self.write('</SMALLCAPS>')
625
626 def open_strong(self): self.write('<B>')
627 def close_strong(self): self.write('</B>')
628
629 open_b = open_strong
630 close_b = close_strong
631
632 def open_var(self): self.write('<VAR>')
633 def close_var(self): self.write('</VAR>')
634
635 def open_w(self): self.write('<NOBREAK>')
636 def close_w(self): self.write('</NOBREAK>')
637
638 open_titlefont = open_
639 close_titlefont = close_
640
Guido van Rossuma12bbff1995-05-03 14:17:36 +0000641 def open_small(self): pass
642 def close_small(self): pass
643
Guido van Rossum26a9d371995-03-15 11:26:05 +0000644 def command(self, line):
645 a, b = cmprog.regs[1]
646 cmd = line[a:b]
647 args = string.strip(line[b:])
648 if self.debugging > 1:
649 print self.skip, self.stack, '@' + cmd, args
650 try:
651 func = getattr(self, 'do_' + cmd)
652 except AttributeError:
653 try:
654 func = getattr(self, 'bgn_' + cmd)
655 except AttributeError:
Guido van Rossum06f42891995-08-28 03:01:00 +0000656 # don't complain if we are skipping anyway
657 if not self.skip:
658 self.unknown_cmd(cmd, args)
Guido van Rossum26a9d371995-03-15 11:26:05 +0000659 return
660 self.stack.append(cmd)
661 func(args)
662 return
663 if not self.skip or cmd == 'end':
664 func(args)
665
666 def unknown_cmd(self, cmd, args):
667 print '*** unknown', '@' + cmd, args
668 if not self.unknown.has_key(cmd):
669 self.unknown[cmd] = 1
670 else:
671 self.unknown[cmd] = self.unknown[cmd] + 1
672
673 def do_end(self, args):
674 words = string.split(args)
675 if not words:
676 print '*** @end w/o args'
677 else:
678 cmd = words[0]
679 if not self.stack or self.stack[-1] <> cmd:
680 print '*** @end', cmd, 'unexpected'
681 else:
682 del self.stack[-1]
683 try:
684 func = getattr(self, 'end_' + cmd)
685 except AttributeError:
686 self.unknown_end(cmd)
687 return
688 func()
689
690 def unknown_end(self, cmd):
691 cmd = 'end ' + cmd
692 print '*** unknown', '@' + cmd
693 if not self.unknown.has_key(cmd):
694 self.unknown[cmd] = 1
695 else:
696 self.unknown[cmd] = self.unknown[cmd] + 1
697
698 # --- Comments ---
699
700 def do_comment(self, args): pass
701 do_c = do_comment
702
703 # --- Conditional processing ---
704
705 def bgn_ifinfo(self, args): pass
706 def end_ifinfo(self): pass
707
708 def bgn_iftex(self, args): self.skip = self.skip + 1
709 def end_iftex(self): self.skip = self.skip - 1
710
711 def bgn_ignore(self, args): self.skip = self.skip + 1
712 def end_ignore(self): self.skip = self.skip - 1
713
714 def bgn_tex(self, args): self.skip = self.skip + 1
715 def end_tex(self): self.skip = self.skip - 1
716
Guido van Rossum06f42891995-08-28 03:01:00 +0000717 def do_set(self, args):
718 fields = string.splitfields (args, ' ')
719 key = fields[0]
720 if len(fields) == 1:
721 value = 1
722 else:
723 value = string.joinfields (fields[1:], ' ')
724 self.values[key]=value
725 print self.values
Guido van Rossuma12bbff1995-05-03 14:17:36 +0000726
Guido van Rossum06f42891995-08-28 03:01:00 +0000727 def do_clear(self, args):
728 self.values[args] = None
Guido van Rossuma12bbff1995-05-03 14:17:36 +0000729
730 def bgn_ifset(self, args):
Guido van Rossum06f42891995-08-28 03:01:00 +0000731 if args not in self.values.keys() \
732 or self.values[args] is None:
Guido van Rossuma12bbff1995-05-03 14:17:36 +0000733 self.skip = self.skip + 1
734 self.stackinfo[len(self.stack)] = 1
735 else:
736 self.stackinfo[len(self.stack)] = 0
737 def end_ifset(self):
738 print self.stack
739 print self.stackinfo
740 if self.stackinfo[len(self.stack) + 1]:
741 self.skip = self.skip - 1
742 del self.stackinfo[len(self.stack) + 1]
743
744 def bgn_ifclear(self, args):
Guido van Rossum06f42891995-08-28 03:01:00 +0000745 if args in self.values.keys() \
746 and self.values[args] is not None:
Guido van Rossuma12bbff1995-05-03 14:17:36 +0000747 self.skip = self.skip + 1
748 self.stackinfo[len(self.stack)] = 1
749 else:
750 self.stackinfo[len(self.stack)] = 0
751
752 end_ifclear = end_ifset
Guido van Rossum06f42891995-08-28 03:01:00 +0000753
754 def open_value(self):
755 self.startsaving()
756
757 def close_value(self):
758 key = self.collectsavings ()
759 if key in self.values.keys():
760 self.write (self.values[key])
761 else:
762 print '*** Undefined value: ', key
763
Guido van Rossum26a9d371995-03-15 11:26:05 +0000764 # --- Beginning a file ---
765
766 do_finalout = do_comment
767 do_setchapternewpage = do_comment
768 do_setfilename = do_comment
769
770 def do_settitle(self, args):
Guido van Rossum06f42891995-08-28 03:01:00 +0000771 print args
772 self.startsaving()
773 self.expand (args)
774 self.title = self.collectsavings ()
775 print self.title
Guido van Rossuma12bbff1995-05-03 14:17:36 +0000776 def do_parskip(self, args): pass
777
Guido van Rossum26a9d371995-03-15 11:26:05 +0000778 # --- Ending a file ---
779
780 def do_bye(self, args):
Guido van Rossum06f42891995-08-28 03:01:00 +0000781 self.endnode ()
Guido van Rossum26a9d371995-03-15 11:26:05 +0000782 self.done = 1
783
784 # --- Title page ---
785
786 def bgn_titlepage(self, args): self.skip = self.skip + 1
787 def end_titlepage(self): self.skip = self.skip - 1
Guido van Rossum06f42891995-08-28 03:01:00 +0000788 def do_shorttitlepage(self, args): pass
789
Guido van Rossum26a9d371995-03-15 11:26:05 +0000790 def do_center(self, args):
791 # Actually not used outside title page...
Guido van Rossuma12bbff1995-05-03 14:17:36 +0000792 self.write('<H1>')
793 self.expand (args)
794 self.write ('</H1>\n')
Guido van Rossum26a9d371995-03-15 11:26:05 +0000795 do_title = do_center
796 do_subtitle = do_center
797 do_author = do_center
798
799 do_vskip = do_comment
800 do_vfill = do_comment
801 do_smallbook = do_comment
802
803 do_paragraphindent = do_comment
804 do_setchapternewpage = do_comment
805 do_headings = do_comment
806 do_footnotestyle = do_comment
807
808 do_evenheading = do_comment
809 do_evenfooting = do_comment
810 do_oddheading = do_comment
811 do_oddfooting = do_comment
812 do_everyheading = do_comment
813 do_everyfooting = do_comment
814
815 # --- Nodes ---
816
817 def do_node(self, args):
Guido van Rossuma12bbff1995-05-03 14:17:36 +0000818 self.endnode()
819 self.nodelineno = 0
Guido van Rossum26a9d371995-03-15 11:26:05 +0000820 parts = string.splitfields(args, ',')
821 while len(parts) < 4: parts.append('')
822 for i in range(4): parts[i] = string.strip(parts[i])
Guido van Rossuma12bbff1995-05-03 14:17:36 +0000823 self.nodelinks = parts
Guido van Rossum26a9d371995-03-15 11:26:05 +0000824 [name, next, prev, up] = parts[:4]
Guido van Rossum26a9d371995-03-15 11:26:05 +0000825 file = self.dirname + '/' + makefile(name)
Guido van Rossuma12bbff1995-05-03 14:17:36 +0000826 if self.filenames.has_key(file):
827 print '*** Filename already in use: ', file
828 else:
829 if self.debugging: print '--- writing', file
830 self.filenames[file] = 1
Guido van Rossum06f42891995-08-28 03:01:00 +0000831 # self.nodefp = open(file, 'w')
Guido van Rossum26a9d371995-03-15 11:26:05 +0000832 self.nodename = name
Guido van Rossum06f42891995-08-28 03:01:00 +0000833 if self.cont and self.nodestack:
834 self.nodestack[-1].cont = self.nodename
Guido van Rossum26a9d371995-03-15 11:26:05 +0000835 if not self.topname: self.topname = name
836 title = name
837 if self.title: title = title + ' -- ' + self.title
Guido van Rossum06f42891995-08-28 03:01:00 +0000838 self.node = Node (self.dirname, self.nodename, self.topname, \
839 title, next, prev, up)
840
Guido van Rossum26a9d371995-03-15 11:26:05 +0000841 def link(self, label, nodename):
842 if nodename:
843 if string.lower(nodename) == '(dir)':
844 addr = '../dir.html'
845 else:
846 addr = makefile(nodename)
847 self.write(label, ': <A HREF="', addr, '" TYPE="', \
848 label, '">', nodename, '</A> \n')
849
850 # --- Sectioning commands ---
851
Guido van Rossum06f42891995-08-28 03:01:00 +0000852 def popstack (self, type):
853 if (self.node):
854 self.node.type = type
855 while self.nodestack:
856 if self.nodestack[-1].type > type:
857 self.nodestack[-1].finalize ()
858 self.nodestack[-1].flush ()
859 del self.nodestack [-1]
860 elif self.nodestack[-1].type == type:
861 if not self.nodestack[-1].next:
862 self.nodestack[-1].next = self.node.name
863 if not self.node.prev:
864 self.node.prev = self.nodestack[-1].name
865 self.nodestack[-1].finalize ()
866 self.nodestack[-1].flush ()
867 del self.nodestack [-1]
868 else:
869 if type > 1 and not self.node.up:
870 self.node.up = self.nodestack[-1].name
871 break
872
Guido van Rossum26a9d371995-03-15 11:26:05 +0000873 def do_chapter(self, args):
874 self.heading('H1', args, 0)
Guido van Rossum06f42891995-08-28 03:01:00 +0000875 self.popstack (1)
876
Guido van Rossum26a9d371995-03-15 11:26:05 +0000877 def do_unnumbered(self, args):
878 self.heading('H1', args, -1)
Guido van Rossum06f42891995-08-28 03:01:00 +0000879 self.popstack (1)
Guido van Rossum26a9d371995-03-15 11:26:05 +0000880 def do_appendix(self, args):
881 self.heading('H1', args, -1)
Guido van Rossum06f42891995-08-28 03:01:00 +0000882 self.popstack (1)
Guido van Rossum26a9d371995-03-15 11:26:05 +0000883 def do_top(self, args):
884 self.heading('H1', args, -1)
885 def do_chapheading(self, args):
886 self.heading('H1', args, -1)
887 def do_majorheading(self, args):
888 self.heading('H1', args, -1)
889
890 def do_section(self, args):
891 self.heading('H1', args, 1)
Guido van Rossum06f42891995-08-28 03:01:00 +0000892 self.popstack (2)
893
Guido van Rossum26a9d371995-03-15 11:26:05 +0000894 def do_unnumberedsec(self, args):
895 self.heading('H1', args, -1)
Guido van Rossum06f42891995-08-28 03:01:00 +0000896 self.popstack (2)
Guido van Rossum26a9d371995-03-15 11:26:05 +0000897 def do_appendixsec(self, args):
898 self.heading('H1', args, -1)
Guido van Rossum06f42891995-08-28 03:01:00 +0000899 self.popstack (2)
Guido van Rossum26a9d371995-03-15 11:26:05 +0000900 do_appendixsection = do_appendixsec
901 def do_heading(self, args):
902 self.heading('H1', args, -1)
903
904 def do_subsection(self, args):
905 self.heading('H2', args, 2)
Guido van Rossum06f42891995-08-28 03:01:00 +0000906 self.popstack (3)
Guido van Rossum26a9d371995-03-15 11:26:05 +0000907 def do_unnumberedsubsec(self, args):
908 self.heading('H2', args, -1)
Guido van Rossum06f42891995-08-28 03:01:00 +0000909 self.popstack (3)
Guido van Rossum26a9d371995-03-15 11:26:05 +0000910 def do_appendixsubsec(self, args):
911 self.heading('H2', args, -1)
Guido van Rossum06f42891995-08-28 03:01:00 +0000912 self.popstack (3)
Guido van Rossum26a9d371995-03-15 11:26:05 +0000913 def do_subheading(self, args):
914 self.heading('H2', args, -1)
915
916 def do_subsubsection(self, args):
917 self.heading('H3', args, 3)
Guido van Rossum06f42891995-08-28 03:01:00 +0000918 self.popstack (4)
Guido van Rossum26a9d371995-03-15 11:26:05 +0000919 def do_unnumberedsubsubsec(self, args):
920 self.heading('H3', args, -1)
Guido van Rossum06f42891995-08-28 03:01:00 +0000921 self.popstack (4)
Guido van Rossum26a9d371995-03-15 11:26:05 +0000922 def do_appendixsubsubsec(self, args):
923 self.heading('H3', args, -1)
Guido van Rossum06f42891995-08-28 03:01:00 +0000924 self.popstack (4)
Guido van Rossum26a9d371995-03-15 11:26:05 +0000925 def do_subsubheading(self, args):
926 self.heading('H3', args, -1)
927
928 def heading(self, type, args, level):
929 if level >= 0:
930 while len(self.numbering) <= level:
931 self.numbering.append(0)
932 del self.numbering[level+1:]
933 self.numbering[level] = self.numbering[level] + 1
934 x = ''
935 for i in self.numbering:
936 x = x + `i` + '.'
937 args = x + ' ' + args
938 self.contents.append(level, args, self.nodename)
939 self.write('<', type, '>')
940 self.expand(args)
941 self.write('</', type, '>\n')
942 if self.debugging:
943 print '---', args
944
945 def do_contents(self, args):
Guido van Rossum06f42891995-08-28 03:01:00 +0000946 # pass
947 self.listcontents('Table of Contents', 999)
Guido van Rossum26a9d371995-03-15 11:26:05 +0000948
949 def do_shortcontents(self, args):
950 pass
951 # self.listcontents('Short Contents', 0)
952 do_summarycontents = do_shortcontents
953
954 def listcontents(self, title, maxlevel):
Guido van Rossum29901ff1996-08-09 21:46:34 +0000955 self.write('<H1>', title, '</H1>\n<UL COMPACT PLAIN>\n')
956 prevlevels = [0]
Guido van Rossum26a9d371995-03-15 11:26:05 +0000957 for level, title, node in self.contents:
Guido van Rossum29901ff1996-08-09 21:46:34 +0000958 if level > maxlevel:
959 continue
960 if level > prevlevels[-1]:
961 # can only advance one level at a time
962 self.write(' '*prevlevels[-1], '<UL PLAIN>\n')
963 prevlevels.append(level)
964 elif level < prevlevels[-1]:
965 # might drop back multiple levels
966 while level < prevlevels[-1]:
967 del prevlevels[-1]
968 self.write(' '*prevlevels[-1],
969 '</UL>\n')
970 self.write(' '*level, '<LI> <A HREF="',
971 makefile(node), '">')
972 self.expand(title)
973 self.write('</A>\n')
974 self.write('</UL>\n' * len(prevlevels))
Guido van Rossum26a9d371995-03-15 11:26:05 +0000975
976 # --- Page lay-out ---
977
978 # These commands are only meaningful in printed text
979
980 def do_page(self, args): pass
981
982 def do_need(self, args): pass
983
984 def bgn_group(self, args): pass
985 def end_group(self): pass
986
987 # --- Line lay-out ---
988
989 def do_sp(self, args):
990 # Insert <args> blank lines
991 if args:
992 try:
993 n = string.atoi(args)
994 except string.atoi_error:
995 n = 1
996 else:
997 n = 1
998 self.write('<P>\n'*max(n, 0))
999
Guido van Rossuma12bbff1995-05-03 14:17:36 +00001000 def do_hline(self, args):
1001 self.write ('<HR>')
1002
Guido van Rossum26a9d371995-03-15 11:26:05 +00001003 # --- Function and variable definitions ---
1004
1005 def bgn_deffn(self, args):
Guido van Rossum06f42891995-08-28 03:01:00 +00001006 self.write('<DL>')
1007 self.do_deffnx (args)
Guido van Rossum26a9d371995-03-15 11:26:05 +00001008
1009 def end_deffn(self):
1010 self.write('</DL>\n')
1011
Guido van Rossuma12bbff1995-05-03 14:17:36 +00001012 def do_deffnx(self, args):
1013 self.write('<DT>')
1014 words = splitwords(args, 2)
1015 [category, name], rest = words[:2], words[2:]
1016 self.expand('@b{' + name + '}')
1017 for word in rest: self.expand(' ' + makevar(word))
1018 self.expand(' -- ' + category)
1019 self.write('<DD>\n')
1020 self.index('fn', name)
1021
Guido van Rossum26a9d371995-03-15 11:26:05 +00001022 def bgn_defun(self, args): self.bgn_deffn('Function ' + args)
1023 end_defun = end_deffn
Guido van Rossum06f42891995-08-28 03:01:00 +00001024 def do_defunx(self, args): self.do_deffnx('Function ' + args)
Guido van Rossum26a9d371995-03-15 11:26:05 +00001025
1026 def bgn_defmac(self, args): self.bgn_deffn('Macro ' + args)
1027 end_defmac = end_deffn
Guido van Rossum06f42891995-08-28 03:01:00 +00001028 def do_defmacx(self, args): self.do_deffnx('Macro ' + args)
Guido van Rossum26a9d371995-03-15 11:26:05 +00001029
1030 def bgn_defspec(self, args): self.bgn_deffn('{Special Form} ' + args)
1031 end_defspec = end_deffn
Guido van Rossum06f42891995-08-28 03:01:00 +00001032 def do_defspecx(self, args): self.do_deffnx('{Special Form} ' + args)
Guido van Rossum26a9d371995-03-15 11:26:05 +00001033
1034 def bgn_defvr(self, args):
Guido van Rossum06f42891995-08-28 03:01:00 +00001035 self.write('<DL>')
1036 self.do_defvrx (args)
1037
1038 end_defvr = end_deffn
1039
1040 def do_defvrx(self, args):
1041 self.write('<DT>')
Guido van Rossum26a9d371995-03-15 11:26:05 +00001042 words = splitwords(args, 2)
1043 [category, name], rest = words[:2], words[2:]
1044 self.expand('@code{' + name + '}')
1045 # If there are too many arguments, show them
1046 for word in rest: self.expand(' ' + word)
1047 self.expand(' -- ' + category)
1048 self.write('<DD>\n')
1049 self.index('vr', name)
1050
Guido van Rossum26a9d371995-03-15 11:26:05 +00001051 def bgn_defvar(self, args): self.bgn_defvr('Variable ' + args)
1052 end_defvar = end_defvr
Guido van Rossum06f42891995-08-28 03:01:00 +00001053 def do_defvarx(self, args): self.do_defvrx('Variable ' + args)
Guido van Rossum26a9d371995-03-15 11:26:05 +00001054
1055 def bgn_defopt(self, args): self.bgn_defvr('{User Option} ' + args)
1056 end_defopt = end_defvr
Guido van Rossum06f42891995-08-28 03:01:00 +00001057 def do_defoptx(self, args): self.do_defvrx('{User Option} ' + args)
Guido van Rossum26a9d371995-03-15 11:26:05 +00001058
1059 # --- Ditto for typed languages ---
1060
1061 def bgn_deftypefn(self, args):
Guido van Rossum06f42891995-08-28 03:01:00 +00001062 self.write('<DL>')
1063 self.do_deftypefnx (args)
1064
1065 end_deftypefn = end_deffn
1066
1067 def do_deftypefnx(self, args):
1068 self.write('<DT>')
Guido van Rossum26a9d371995-03-15 11:26:05 +00001069 words = splitwords(args, 3)
1070 [category, datatype, name], rest = words[:3], words[3:]
1071 self.expand('@code{' + datatype + '} @b{' + name + '}')
1072 for word in rest: self.expand(' ' + makevar(word))
1073 self.expand(' -- ' + category)
1074 self.write('<DD>\n')
1075 self.index('fn', name)
1076
Guido van Rossum26a9d371995-03-15 11:26:05 +00001077
1078 def bgn_deftypefun(self, args): self.bgn_deftypefn('Function ' + args)
1079 end_deftypefun = end_deftypefn
Guido van Rossum06f42891995-08-28 03:01:00 +00001080 def do_deftypefunx(self, args): self.do_deftypefnx('Function ' + args)
Guido van Rossum26a9d371995-03-15 11:26:05 +00001081
1082 def bgn_deftypevr(self, args):
Guido van Rossum06f42891995-08-28 03:01:00 +00001083 self.write('<DL>')
1084 self.do_deftypevrx (args)
1085
1086 end_deftypevr = end_deftypefn
1087
1088 def do_deftypevrx(self, args):
1089 self.write('<DT>')
Guido van Rossum26a9d371995-03-15 11:26:05 +00001090 words = splitwords(args, 3)
1091 [category, datatype, name], rest = words[:3], words[3:]
Guido van Rossum26a9d371995-03-15 11:26:05 +00001092 self.expand('@code{' + datatype + '} @b{' + name + '}')
1093 # If there are too many arguments, show them
1094 for word in rest: self.expand(' ' + word)
1095 self.expand(' -- ' + category)
1096 self.write('<DD>\n')
1097 self.index('fn', name)
1098
Guido van Rossum26a9d371995-03-15 11:26:05 +00001099 def bgn_deftypevar(self, args):
1100 self.bgn_deftypevr('Variable ' + args)
1101 end_deftypevar = end_deftypevr
Guido van Rossum06f42891995-08-28 03:01:00 +00001102 def do_deftypevarx(self, args):
1103 self.do_deftypevrx('Variable ' + args)
Guido van Rossum26a9d371995-03-15 11:26:05 +00001104
1105 # --- Ditto for object-oriented languages ---
1106
1107 def bgn_defcv(self, args):
Guido van Rossum06f42891995-08-28 03:01:00 +00001108 self.write('<DL>')
1109 self.do_defcvx(args)
1110
1111 end_defcv = end_deftypevr
1112
1113 def do_defcvx(self, args):
1114 self.write('<DT>')
Guido van Rossum26a9d371995-03-15 11:26:05 +00001115 words = splitwords(args, 3)
1116 [category, classname, name], rest = words[:3], words[3:]
Guido van Rossum26a9d371995-03-15 11:26:05 +00001117 self.expand('@b{' + name + '}')
1118 # If there are too many arguments, show them
1119 for word in rest: self.expand(' ' + word)
1120 self.expand(' -- ' + category + ' of ' + classname)
1121 self.write('<DD>\n')
1122 self.index('vr', name + ' @r{of ' + classname + '}')
1123
Guido van Rossum26a9d371995-03-15 11:26:05 +00001124 def bgn_defivar(self, args):
1125 self.bgn_defcv('{Instance Variable} ' + args)
1126 end_defivar = end_defcv
Guido van Rossum06f42891995-08-28 03:01:00 +00001127 def do_defivarx(self, args):
1128 self.do_defcvx('{Instance Variable} ' + args)
Guido van Rossum26a9d371995-03-15 11:26:05 +00001129
1130 def bgn_defop(self, args):
Guido van Rossum06f42891995-08-28 03:01:00 +00001131 self.write('<DL>')
1132 self.do_defopx (args)
1133
1134 end_defop = end_defcv
1135
1136 def do_defopx(self, args):
1137 self.write('<DT>')
Guido van Rossum26a9d371995-03-15 11:26:05 +00001138 words = splitwords(args, 3)
1139 [category, classname, name], rest = words[:3], words[3:]
1140 self.expand('@b{' + name + '}')
1141 for word in rest: self.expand(' ' + makevar(word))
1142 self.expand(' -- ' + category + ' on ' + classname)
1143 self.write('<DD>\n')
1144 self.index('fn', name + ' @r{on ' + classname + '}')
1145
Guido van Rossum26a9d371995-03-15 11:26:05 +00001146 def bgn_defmethod(self, args):
1147 self.bgn_defop('Method ' + args)
1148 end_defmethod = end_defop
Guido van Rossum06f42891995-08-28 03:01:00 +00001149 def do_defmethodx(self, args):
1150 self.do_defopx('Method ' + args)
Guido van Rossum26a9d371995-03-15 11:26:05 +00001151
1152 # --- Ditto for data types ---
1153
1154 def bgn_deftp(self, args):
Guido van Rossum06f42891995-08-28 03:01:00 +00001155 self.write('<DL>')
1156 self.do_deftpx(args)
1157
1158 end_deftp = end_defcv
1159
1160 def do_deftpx(self, args):
1161 self.write('<DT>')
Guido van Rossum26a9d371995-03-15 11:26:05 +00001162 words = splitwords(args, 2)
1163 [category, name], rest = words[:2], words[2:]
1164 self.expand('@b{' + name + '}')
1165 for word in rest: self.expand(' ' + word)
1166 self.expand(' -- ' + category)
1167 self.write('<DD>\n')
1168 self.index('tp', name)
1169
Guido van Rossum26a9d371995-03-15 11:26:05 +00001170 # --- Making Lists and Tables
1171
1172 def bgn_enumerate(self, args):
Guido van Rossuma12bbff1995-05-03 14:17:36 +00001173 if not args:
1174 self.write('<OL>\n')
1175 self.stackinfo[len(self.stack)] = '</OL>\n'
1176 else:
1177 self.itemnumber = args
1178 self.write('<UL>\n')
1179 self.stackinfo[len(self.stack)] = '</UL>\n'
Guido van Rossum26a9d371995-03-15 11:26:05 +00001180 def end_enumerate(self):
1181 self.itemnumber = None
Guido van Rossuma12bbff1995-05-03 14:17:36 +00001182 self.write(self.stackinfo[len(self.stack) + 1])
1183 del self.stackinfo[len(self.stack) + 1]
Guido van Rossum26a9d371995-03-15 11:26:05 +00001184
1185 def bgn_itemize(self, args):
1186 self.itemarg = args
1187 self.write('<UL>\n')
1188 def end_itemize(self):
1189 self.itemarg = None
1190 self.write('</UL>\n')
1191
1192 def bgn_table(self, args):
1193 self.itemarg = args
1194 self.write('<DL>\n')
1195 def end_table(self):
1196 self.itemarg = None
1197 self.write('</DL>\n')
1198
1199 def bgn_ftable(self, args):
1200 self.itemindex = 'fn'
1201 self.bgn_table(args)
1202 def end_ftable(self):
1203 self.itemindex = None
1204 self.end_table()
1205
1206 def do_item(self, args):
1207 if self.itemindex: self.index(self.itemindex, args)
1208 if self.itemarg:
1209 if self.itemarg[0] == '@' and self.itemarg[1:2] and \
1210 self.itemarg[1] in string.letters:
1211 args = self.itemarg + '{' + args + '}'
1212 else:
1213 # some other character, e.g. '-'
1214 args = self.itemarg + ' ' + args
1215 if self.itemnumber <> None:
1216 args = self.itemnumber + '. ' + args
1217 self.itemnumber = increment(self.itemnumber)
1218 if self.stack and self.stack[-1] == 'table':
1219 self.write('<DT>')
1220 self.expand(args)
1221 self.write('<DD>')
1222 else:
1223 self.write('<LI>')
1224 self.expand(args)
1225 self.write(' ')
1226 do_itemx = do_item # XXX Should suppress leading blank line
1227
1228 # --- Enumerations, displays, quotations ---
1229 # XXX Most of these should increase the indentation somehow
1230
Guido van Rossum29901ff1996-08-09 21:46:34 +00001231 def bgn_quotation(self, args): self.write('<BLOCKQUOTE>')
1232 def end_quotation(self): self.write('</BLOCKQUOTE>\n')
Guido van Rossum26a9d371995-03-15 11:26:05 +00001233
1234 def bgn_example(self, args):
1235 self.nofill = self.nofill + 1
Guido van Rossum29901ff1996-08-09 21:46:34 +00001236 self.write('<PRE>')
Guido van Rossum26a9d371995-03-15 11:26:05 +00001237 def end_example(self):
Guido van Rossum29901ff1996-08-09 21:46:34 +00001238 self.write('</PRE>')
Guido van Rossum26a9d371995-03-15 11:26:05 +00001239 self.nofill = self.nofill - 1
1240
1241 bgn_lisp = bgn_example # Synonym when contents are executable lisp code
1242 end_lisp = end_example
1243
1244 bgn_smallexample = bgn_example # XXX Should use smaller font
1245 end_smallexample = end_example
1246
1247 bgn_smalllisp = bgn_lisp # Ditto
1248 end_smalllisp = end_lisp
1249
Guido van Rossum29901ff1996-08-09 21:46:34 +00001250 bgn_display = bgn_example
1251 end_display = end_example
Guido van Rossum26a9d371995-03-15 11:26:05 +00001252
Guido van Rossum29901ff1996-08-09 21:46:34 +00001253 bgn_format = bgn_display
1254 end_format = end_display
Guido van Rossum26a9d371995-03-15 11:26:05 +00001255
1256 def do_exdent(self, args): self.expand(args + '\n')
1257 # XXX Should really mess with indentation
1258
1259 def bgn_flushleft(self, args):
1260 self.nofill = self.nofill + 1
Guido van Rossuma12bbff1995-05-03 14:17:36 +00001261 self.write('<PRE>\n')
Guido van Rossum26a9d371995-03-15 11:26:05 +00001262 def end_flushleft(self):
Guido van Rossuma12bbff1995-05-03 14:17:36 +00001263 self.write('</PRE>\n')
Guido van Rossum26a9d371995-03-15 11:26:05 +00001264 self.nofill = self.nofill - 1
1265
1266 def bgn_flushright(self, args):
1267 self.nofill = self.nofill + 1
1268 self.write('<ADDRESS COMPACT>\n')
1269 def end_flushright(self):
1270 self.write('</ADDRESS>\n')
1271 self.nofill = self.nofill - 1
1272
Guido van Rossum29901ff1996-08-09 21:46:34 +00001273 def bgn_menu(self, args):
1274 self.write('<DIR>\n')
1275 self.write(' <STRONG><EM>Menu</EM></STRONG><P>\n')
1276 def end_menu(self): self.write('</DIR>\n')
Guido van Rossum26a9d371995-03-15 11:26:05 +00001277
1278 def bgn_cartouche(self, args): pass
1279 def end_cartouche(self): pass
1280
1281 # --- Indices ---
1282
1283 def resetindex(self):
1284 self.noncodeindices = ['cp']
1285 self.indextitle = {}
1286 self.indextitle['cp'] = 'Concept'
1287 self.indextitle['fn'] = 'Function'
1288 self.indextitle['ky'] = 'Keyword'
1289 self.indextitle['pg'] = 'Program'
1290 self.indextitle['tp'] = 'Type'
1291 self.indextitle['vr'] = 'Variable'
1292 #
1293 self.whichindex = {}
1294 for name in self.indextitle.keys():
1295 self.whichindex[name] = []
1296
1297 def user_index(self, name, args):
1298 if self.whichindex.has_key(name):
1299 self.index(name, args)
1300 else:
1301 print '*** No index named', `name`
1302
1303 def do_cindex(self, args): self.index('cp', args)
1304 def do_findex(self, args): self.index('fn', args)
1305 def do_kindex(self, args): self.index('ky', args)
1306 def do_pindex(self, args): self.index('pg', args)
1307 def do_tindex(self, args): self.index('tp', args)
1308 def do_vindex(self, args): self.index('vr', args)
1309
1310 def index(self, name, args):
1311 self.whichindex[name].append(args, self.nodename)
1312
1313 def do_synindex(self, args):
1314 words = string.split(args)
1315 if len(words) <> 2:
1316 print '*** bad @synindex', args
1317 return
1318 [old, new] = words
1319 if not self.whichindex.has_key(old) or \
1320 not self.whichindex.has_key(new):
1321 print '*** bad key(s) in @synindex', args
1322 return
1323 if old <> new and \
1324 self.whichindex[old] is not self.whichindex[new]:
1325 inew = self.whichindex[new]
1326 inew[len(inew):] = self.whichindex[old]
1327 self.whichindex[old] = inew
1328 do_syncodeindex = do_synindex # XXX Should use code font
1329
1330 def do_printindex(self, args):
1331 words = string.split(args)
1332 for name in words:
1333 if self.whichindex.has_key(name):
1334 self.prindex(name)
1335 else:
1336 print '*** No index named', `name`
1337
1338 def prindex(self, name):
1339 iscodeindex = (name not in self.noncodeindices)
1340 index = self.whichindex[name]
1341 if not index: return
1342 if self.debugging:
1343 print '--- Generating', self.indextitle[name], 'index'
1344 # The node already provides a title
1345 index1 = []
1346 junkprog = regex.compile('^\(@[a-z]+\)?{')
1347 for key, node in index:
1348 sortkey = string.lower(key)
1349 # Remove leading `@cmd{' from sort key
1350 # -- don't bother about the matching `}'
1351 oldsortkey = sortkey
1352 while 1:
1353 i = junkprog.match(sortkey)
1354 if i < 0: break
1355 sortkey = sortkey[i:]
1356 index1.append(sortkey, key, node)
1357 del index[:]
1358 index1.sort()
1359 self.write('<DL COMPACT>\n')
Guido van Rossum29901ff1996-08-09 21:46:34 +00001360 prevkey = prevnode = None
Guido van Rossum26a9d371995-03-15 11:26:05 +00001361 for sortkey, key, node in index1:
Guido van Rossum29901ff1996-08-09 21:46:34 +00001362 if (key, node) == (prevkey, prevnode):
1363 continue
Guido van Rossum26a9d371995-03-15 11:26:05 +00001364 if self.debugging > 1: print key, ':', node
1365 self.write('<DT>')
1366 if iscodeindex: key = '@code{' + key + '}'
Guido van Rossum29901ff1996-08-09 21:46:34 +00001367 if key != prevkey:
1368 self.expand(key)
Guido van Rossum26a9d371995-03-15 11:26:05 +00001369 self.write('<DD><A HREF="', makefile(node), \
1370 '">', node, '</A>\n')
Guido van Rossum29901ff1996-08-09 21:46:34 +00001371 prevkey, prevnode = key, node
Guido van Rossum26a9d371995-03-15 11:26:05 +00001372 self.write('</DL>\n')
1373
1374 # --- Final error reports ---
1375
1376 def report(self):
1377 if self.unknown:
1378 print '--- Unrecognized commands ---'
1379 cmds = self.unknown.keys()
1380 cmds.sort()
1381 for cmd in cmds:
1382 print string.ljust(cmd, 20), self.unknown[cmd]
1383
1384
1385# Put @var{} around alphabetic substrings
1386def makevar(str):
Guido van Rossuma12bbff1995-05-03 14:17:36 +00001387 return '@var{'+str+'}'
Guido van Rossum26a9d371995-03-15 11:26:05 +00001388
1389
1390# Split a string in "words" according to findwordend
1391def splitwords(str, minlength):
1392 words = []
1393 i = 0
1394 n = len(str)
1395 while i < n:
1396 while i < n and str[i] in ' \t\n': i = i+1
1397 if i >= n: break
1398 start = i
1399 i = findwordend(str, i, n)
1400 words.append(str[start:i])
1401 while len(words) < minlength: words.append('')
1402 return words
1403
1404
1405# Find the end of a "word", matching braces and interpreting @@ @{ @}
1406fwprog = regex.compile('[@{} ]')
1407def findwordend(str, i, n):
1408 level = 0
1409 while i < n:
1410 i = fwprog.search(str, i)
1411 if i < 0: break
1412 c = str[i]; i = i+1
1413 if c == '@': i = i+1 # Next character is not special
1414 elif c == '{': level = level+1
1415 elif c == '}': level = level-1
1416 elif c == ' ' and level <= 0: return i-1
1417 return n
1418
1419
1420# Convert a node name into a file name
1421def makefile(nodename):
Guido van Rossuma12bbff1995-05-03 14:17:36 +00001422 return fixfunnychars(nodename) + '.html'
Guido van Rossum26a9d371995-03-15 11:26:05 +00001423
1424
1425# Characters that are perfectly safe in filenames and hyperlinks
Guido van Rossum06f42891995-08-28 03:01:00 +00001426goodchars = string.letters + string.digits + '!@-=+.'
Guido van Rossum26a9d371995-03-15 11:26:05 +00001427
Guido van Rossum06f42891995-08-28 03:01:00 +00001428# Replace characters that aren't perfectly safe by dashes
1429# Underscores are bad since Cern HTTPD treats them as delimiters for
1430# encoding times, so you get mismatches if you compress your files:
1431# a.html.gz will map to a_b.html.gz
Guido van Rossum26a9d371995-03-15 11:26:05 +00001432def fixfunnychars(addr):
1433 i = 0
1434 while i < len(addr):
1435 c = addr[i]
1436 if c not in goodchars:
Guido van Rossum06f42891995-08-28 03:01:00 +00001437 c = '-'
Guido van Rossum26a9d371995-03-15 11:26:05 +00001438 addr = addr[:i] + c + addr[i+1:]
1439 i = i + len(c)
1440 return addr
1441
1442
1443# Increment a string used as an enumeration
1444def increment(s):
1445 if not s:
1446 return '1'
1447 for sequence in string.digits, string.lowercase, string.uppercase:
1448 lastc = s[-1]
1449 if lastc in sequence:
1450 i = string.index(sequence, lastc) + 1
1451 if i >= len(sequence):
1452 if len(s) == 1:
1453 s = sequence[0]*2
1454 if s == '00':
1455 s = '10'
1456 else:
1457 s = increment(s[:-1]) + sequence[0]
1458 else:
1459 s = s[:-1] + sequence[i]
1460 return s
1461 return s # Don't increment
1462
1463
1464def test():
1465 import sys
1466 parser = TexinfoParser()
1467 while sys.argv[1:2] == ['-d']:
1468 parser.debugging = parser.debugging + 1
1469 del sys.argv[1:2]
Guido van Rossum06f42891995-08-28 03:01:00 +00001470 if sys.argv[1] == '-c':
1471 parser.cont = 1
1472 del sys.argv[1]
Guido van Rossum26a9d371995-03-15 11:26:05 +00001473 if len(sys.argv) <> 3:
Guido van Rossum06f42891995-08-28 03:01:00 +00001474 print 'usage: texi2html [-d] [-d] [-c] inputfile outputdirectory'
Guido van Rossum26a9d371995-03-15 11:26:05 +00001475 sys.exit(2)
1476 file = sys.argv[1]
1477 parser.setdirname(sys.argv[2])
1478 if file == '-':
1479 fp = sys.stdin
1480 else:
1481 parser.setincludedir(os.path.dirname(file))
1482 try:
1483 fp = open(file, 'r')
1484 except IOError, msg:
1485 print file, ':', msg
1486 sys.exit(1)
1487 parser.parse(fp)
1488 fp.close()
1489 parser.report()
1490
1491
1492test()
Guido van Rossum06f42891995-08-28 03:01:00 +00001493
1494# Emacs local variables
1495#
1496# Local Variables:
1497# py-indent-offset: 8
1498# End: