#! /usr/local/bin/python # Convert GNU texinfo files into HTML, one file per node. # Based on Texinfo 2.14. # Usage: texi2html [-d] [-d] [-c] 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): # - @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?) # More consistent handling of chapters/sections/etc. # Lots of documentation # Many more options: # -top designate top node # -links customize which types of links are included # -split split at chapters or sections instead of nodes # -name Allow different types of filename handling. Non unix systems # will have problems with long node names # ... # Support the most recent texinfo version and take a good look at HTML 3.0 # More debugging output (customizable) and more fexible error handling # How about icons ? 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 Node: __doc__ = """ Some of the parser's functionality is separated into this class. A Node accumulates its contents, takes care of links to other Nodes and saves itself when it is finished and all links are resolved. """ def __init__ (self, dir, name, topname, title, next, prev, up): self.dirname = dir self.name = name if topname: self.topname = topname else: self.topname = name self.title = title self.next = next self.prev = prev self.up = up self.lines = [] self.type = 0 self.cont = '' def write (self, *lines): map(self.lines.append, lines) def flush (self): fp = open (self.dirname + '/' + makefile(self.name), 'w') fp.write (self.prologue) fp.write (self.text) fp.write (self.epilogue) fp.close () def link(self, label, nodename): if nodename: if string.lower(nodename) == '(dir)': addr = '../dir.html' else: addr = makefile(nodename) self.write(label, ': ', nodename, ' \n') def finalize(self): length = len (self.lines) self.text = string.joinfields (self.lines, '') self.lines = [] self.write ('
\n%s\n' % links else: self.epilogue = '