Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1 | #! /usr/local/bin/python |
| 2 | |
| 3 | # Convert GNU texinfo files into HTML, one file per node. |
| 4 | # Based on Texinfo 2.14. |
Guido van Rossum | 06f4289 | 1995-08-28 03:01:00 +0000 | [diff] [blame] | 5 | # Usage: texi2html [-d] [-d] [-c] inputfile outputdirectory |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 6 | # 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 Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 20 | # - @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 Rossum | 06f4289 | 1995-08-28 03:01:00 +0000 | [diff] [blame] | 25 | # 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 Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 37 | |
| 38 | import os |
| 39 | import regex |
| 40 | import regsub |
| 41 | import string |
| 42 | |
| 43 | MAGIC = '\\input texinfo' |
| 44 | |
| 45 | cmprog = regex.compile('^@\([a-z]+\)\([ \t]\|$\)') # Command (line-oriented) |
| 46 | blprog = regex.compile('^[ \t]*$') # Blank line |
| 47 | kwprog = regex.compile('@[a-z]+') # Keyword (embedded, usually with {} args) |
| 48 | spprog = regex.compile('[\n@{}&<>]') # Special characters in running text |
| 49 | miprog = regex.compile( \ |
| 50 | '^\* \([^:]*\):\(:\|[ \t]*\([^\t,\n.]+\)\([^ \t\n]*\)\)[ \t\n]*') |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 51 | # menu item (Yuck!) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | 06f4289 | 1995-08-28 03:01:00 +0000 | [diff] [blame] | 53 | |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 54 | class HTMLNode: |
| 55 | """Some of the parser's functionality is separated into this class. |
Guido van Rossum | 06f4289 | 1995-08-28 03:01:00 +0000 | [diff] [blame] | 56 | |
| 57 | A Node accumulates its contents, takes care of links to other Nodes |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 58 | and saves itself when it is finished and all links are resolved. |
| 59 | """ |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 60 | |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 61 | DOCTYPE = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">' |
| 62 | |
| 63 | type = 0 |
| 64 | cont = '' |
| 65 | epilogue = '</BODY></HTML>\n' |
| 66 | |
| 67 | def __init__(self, dir, name, topname, title, next, prev, up): |
Guido van Rossum | 06f4289 | 1995-08-28 03:01:00 +0000 | [diff] [blame] | 68 | self.dirname = dir |
| 69 | self.name = name |
| 70 | if topname: |
| 71 | self.topname = topname |
| 72 | else: |
| 73 | self.topname = name |
| 74 | self.title = title |
| 75 | self.next = next |
| 76 | self.prev = prev |
| 77 | self.up = up |
| 78 | self.lines = [] |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 79 | |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 80 | def write(self, *lines): |
Guido van Rossum | 29901ff | 1996-08-09 21:46:34 +0000 | [diff] [blame] | 81 | map(self.lines.append, lines) |
Guido van Rossum | 06f4289 | 1995-08-28 03:01:00 +0000 | [diff] [blame] | 82 | |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 83 | def flush(self): |
| 84 | fp = open(self.dirname + '/' + makefile(self.name), 'w') |
| 85 | fp.write(self.prologue) |
| 86 | fp.write(self.text) |
| 87 | fp.write(self.epilogue) |
| 88 | fp.close() |
Guido van Rossum | 06f4289 | 1995-08-28 03:01:00 +0000 | [diff] [blame] | 89 | |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 90 | def link(self, label, nodename, rel=None, rev=None): |
Guido van Rossum | 06f4289 | 1995-08-28 03:01:00 +0000 | [diff] [blame] | 91 | if nodename: |
| 92 | if string.lower(nodename) == '(dir)': |
| 93 | addr = '../dir.html' |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 94 | title = '' |
Guido van Rossum | 06f4289 | 1995-08-28 03:01:00 +0000 | [diff] [blame] | 95 | else: |
| 96 | addr = makefile(nodename) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 97 | title = ' TITLE="%s"' % nodename |
| 98 | self.write(label, ': <A HREF="', addr, '"', \ |
| 99 | rel and (' REL=' + rel) or "", \ |
| 100 | rev and (' REV=' + rev) or "", \ |
| 101 | title, '>', nodename, '</A> \n') |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 102 | |
Guido van Rossum | 06f4289 | 1995-08-28 03:01:00 +0000 | [diff] [blame] | 103 | def finalize(self): |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 104 | length = len(self.lines) |
| 105 | self.text = string.joinfields(self.lines, '') |
Guido van Rossum | 06f4289 | 1995-08-28 03:01:00 +0000 | [diff] [blame] | 106 | self.lines = [] |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 107 | self.open_links() |
| 108 | self.output_links() |
| 109 | self.close_links() |
Guido van Rossum | 29901ff | 1996-08-09 21:46:34 +0000 | [diff] [blame] | 110 | links = string.joinfields(self.lines, '') |
Guido van Rossum | 06f4289 | 1995-08-28 03:01:00 +0000 | [diff] [blame] | 111 | self.lines = [] |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 112 | |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 113 | self.prologue = ( |
| 114 | self.DOCTYPE + |
| 115 | '\n<HTML><HEAD>\n' |
| 116 | ' <!-- Converted with texi2html and Python -->\n' |
| 117 | ' <TITLE>' + self.title + '</TITLE>\n' |
| 118 | ' <LINK REL=Next HREF="' |
| 119 | + makefile(self.next) + '" TITLE="' + self.next + '">\n' |
| 120 | ' <LINK REL=Previous HREF="' |
| 121 | + makefile(self.prev) + '" TITLE="' + self.prev + '">\n' |
| 122 | ' <LINK REL=Up HREF="' |
| 123 | + makefile(self.up) + '" TITLE="' + self.up + '">\n' |
| 124 | '</HEAD><BODY>\n' + |
| 125 | links) |
Guido van Rossum | 06f4289 | 1995-08-28 03:01:00 +0000 | [diff] [blame] | 126 | if length > 20: |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 127 | self.epilogue = '<P>\n%s</BODY></HTML>\n' % links |
| 128 | |
| 129 | def open_links(self): |
| 130 | self.write('<HR>\n') |
| 131 | |
| 132 | def close_links(self): |
| 133 | self.write('<HR>\n') |
| 134 | |
| 135 | def output_links(self): |
| 136 | if self.cont != self.next: |
| 137 | self.link(' Cont', self.cont) |
| 138 | self.link(' Next', self.next, rel='Next') |
| 139 | self.link(' Prev', self.prev, rel='Previous') |
| 140 | self.link(' Up', self.up, rel='Up') |
| 141 | if self.name <> self.topname: |
| 142 | self.link(' Top', self.topname) |
| 143 | |
| 144 | |
| 145 | class HTML3Node(HTMLNode): |
| 146 | |
| 147 | DOCTYPE = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML Level 3//EN//3.0">' |
| 148 | |
| 149 | def open_links(self): |
Fred Drake | 22f7f9e | 1996-10-11 16:54:00 +0000 | [diff] [blame] | 150 | self.write('<DIV CLASS=Navigation>\n <HR>\n') |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 151 | |
| 152 | def close_links(self): |
| 153 | self.write(' <HR>\n</DIV>\n') |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 154 | |
Guido van Rossum | 06f4289 | 1995-08-28 03:01:00 +0000 | [diff] [blame] | 155 | |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 156 | class TexinfoParser: |
| 157 | |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 158 | COPYRIGHT_SYMBOL = "©" |
| 159 | FN_ID_PATTERN = "(%(id)s)" |
| 160 | FN_SOURCE_PATTERN = '<A NAME=footnoteref%(id)s' \ |
| 161 | ' HREF="#footnotetext%(id)s">' \ |
| 162 | + FN_ID_PATTERN + '</A>' |
| 163 | FN_TARGET_PATTERN = '<A NAME=footnotetext%(id)s' \ |
| 164 | ' HREF="#footnoteref%(id)s">' \ |
| 165 | + FN_ID_PATTERN + '</A>\n%(text)s<P>\n' |
Fred Drake | 22f7f9e | 1996-10-11 16:54:00 +0000 | [diff] [blame] | 166 | FN_HEADER = '\n<P>\n<HR NOSHADE SIZE=1 WIDTH=200>\n' \ |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 167 | '<STRONG><EM>Footnotes</EM></STRONG>\n<P>' |
| 168 | |
| 169 | |
| 170 | Node = HTMLNode |
| 171 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 172 | # Initialize an instance |
| 173 | def __init__(self): |
| 174 | self.unknown = {} # statistics about unknown @-commands |
| 175 | self.filenames = {} # Check for identical filenames |
| 176 | self.debugging = 0 # larger values produce more output |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 177 | self.print_headers = 0 # always print headers? |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 178 | self.nodefp = None # open file we're writing to |
| 179 | self.nodelineno = 0 # Linenumber relative to node |
| 180 | self.links = None # Links from current node |
| 181 | self.savetext = None # If not None, save text head instead |
| 182 | self.savestack = [] # If not None, save text head instead |
| 183 | self.dirname = 'tmp' # directory where files are created |
| 184 | self.includedir = '.' # directory to search @include files |
| 185 | self.nodename = '' # name of current node |
| 186 | self.topname = '' # name of top node (first node seen) |
| 187 | self.title = '' # title of this whole Texinfo tree |
| 188 | self.resetindex() # Reset all indices |
| 189 | self.contents = [] # Reset table of contents |
| 190 | self.numbering = [] # Reset section numbering counters |
| 191 | self.nofill = 0 # Normal operation: fill paragraphs |
| 192 | self.values={'html': 1} # Names that should be parsed in ifset |
| 193 | self.stackinfo={} # Keep track of state in the stack |
| 194 | # XXX The following should be reset per node?! |
| 195 | self.footnotes = [] # Reset list of footnotes |
| 196 | self.itemarg = None # Reset command used by @item |
| 197 | self.itemnumber = None # Reset number for @item in @enumerate |
| 198 | self.itemindex = None # Reset item index name |
| 199 | self.node = None |
| 200 | self.nodestack = [] |
| 201 | self.cont = 0 |
| 202 | self.includedepth = 0 |
| 203 | # Set (output) directory name |
| 204 | def setdirname(self, dirname): |
| 205 | self.dirname = dirname |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 206 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 207 | # Set include directory name |
| 208 | def setincludedir(self, includedir): |
| 209 | self.includedir = includedir |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 210 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 211 | # Parse the contents of an entire file |
| 212 | def parse(self, fp): |
| 213 | line = fp.readline() |
| 214 | lineno = 1 |
| 215 | while line and (line[0] == '%' or blprog.match(line) >= 0): |
| 216 | line = fp.readline() |
| 217 | lineno = lineno + 1 |
| 218 | if line[:len(MAGIC)] <> MAGIC: |
| 219 | raise SyntaxError, 'file does not begin with '+`MAGIC` |
| 220 | self.parserest(fp, lineno) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 221 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 222 | # Parse the contents of a file, not expecting a MAGIC header |
| 223 | def parserest(self, fp, initial_lineno): |
| 224 | lineno = initial_lineno |
| 225 | self.done = 0 |
| 226 | self.skip = 0 |
| 227 | self.stack = [] |
| 228 | accu = [] |
| 229 | while not self.done: |
| 230 | line = fp.readline() |
| 231 | self.nodelineno = self.nodelineno + 1 |
| 232 | if not line: |
| 233 | if accu: |
| 234 | if not self.skip: self.process(accu) |
| 235 | accu = [] |
| 236 | if initial_lineno > 0: |
| 237 | print '*** EOF before @bye' |
| 238 | break |
| 239 | lineno = lineno + 1 |
| 240 | if cmprog.match(line) >= 0: |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 241 | a, b = cmprog.regs[1] |
| 242 | cmd = line[a:b] |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 243 | if cmd in ('noindent', 'refill'): |
| 244 | accu.append(line) |
| 245 | else: |
| 246 | if accu: |
| 247 | if not self.skip: |
| 248 | self.process(accu) |
| 249 | accu = [] |
| 250 | self.command(line) |
| 251 | elif blprog.match(line) >= 0 and \ |
| 252 | 'format' not in self.stack and \ |
| 253 | 'example' not in self.stack: |
| 254 | if accu: |
| 255 | if not self.skip: |
| 256 | self.process(accu) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 257 | if self.nofill: |
| 258 | self.write('\n') |
| 259 | else: |
| 260 | self.write('<P>\n') |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 261 | accu = [] |
| 262 | else: |
| 263 | # Append the line including trailing \n! |
| 264 | accu.append(line) |
| 265 | # |
| 266 | if self.skip: |
| 267 | print '*** Still skipping at the end' |
| 268 | if self.stack: |
| 269 | print '*** Stack not empty at the end' |
| 270 | print '***', self.stack |
| 271 | if self.includedepth == 0: |
| 272 | while self.nodestack: |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 273 | self.nodestack[-1].finalize() |
| 274 | self.nodestack[-1].flush() |
| 275 | del self.nodestack[-1] |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 276 | |
| 277 | # Start saving text in a buffer instead of writing it to a file |
| 278 | def startsaving(self): |
| 279 | if self.savetext <> None: |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 280 | self.savestack.append(self.savetext) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 281 | # print '*** Recursively saving text, expect trouble' |
| 282 | self.savetext = '' |
| 283 | |
| 284 | # Return the text saved so far and start writing to file again |
| 285 | def collectsavings(self): |
| 286 | savetext = self.savetext |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 287 | if len(self.savestack) > 0: |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 288 | self.savetext = self.savestack[-1] |
| 289 | del self.savestack[-1] |
| 290 | else: |
| 291 | self.savetext = None |
| 292 | return savetext or '' |
| 293 | |
| 294 | # Write text to file, or save it in a buffer, or ignore it |
| 295 | def write(self, *args): |
| 296 | try: |
| 297 | text = string.joinfields(args, '') |
| 298 | except: |
| 299 | print args |
| 300 | raise TypeError |
| 301 | if self.savetext <> None: |
| 302 | self.savetext = self.savetext + text |
| 303 | elif self.nodefp: |
| 304 | self.nodefp.write(text) |
| 305 | elif self.node: |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 306 | self.node.write(text) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 307 | # Complete the current node -- write footnotes and close file |
| 308 | def endnode(self): |
| 309 | if self.savetext <> None: |
| 310 | print '*** Still saving text at end of node' |
| 311 | dummy = self.collectsavings() |
| 312 | if self.footnotes: |
| 313 | self.writefootnotes() |
| 314 | if self.nodefp: |
| 315 | if self.nodelineno > 20: |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 316 | self.write('<HR>\n') |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 317 | [name, next, prev, up] = self.nodelinks[:4] |
| 318 | self.link('Next', next) |
| 319 | self.link('Prev', prev) |
| 320 | self.link('Up', up) |
| 321 | if self.nodename <> self.topname: |
| 322 | self.link('Top', self.topname) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 323 | self.write('<HR>\n') |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 324 | self.write('</BODY>\n') |
| 325 | self.nodefp.close() |
| 326 | self.nodefp = None |
| 327 | elif self.node: |
| 328 | if not self.cont and \ |
| 329 | (not self.node.type or \ |
| 330 | (self.node.next and self.node.prev and self.node.up)): |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 331 | self.node.finalize() |
| 332 | self.node.flush() |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 333 | else: |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 334 | self.nodestack.append(self.node) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 335 | self.node = None |
| 336 | self.nodename = '' |
| 337 | |
| 338 | # Process a list of lines, expanding embedded @-commands |
| 339 | # This mostly distinguishes between menus and normal text |
| 340 | def process(self, accu): |
| 341 | if self.debugging > 1: |
| 342 | print self.skip, self.stack, |
| 343 | if accu: print accu[0][:30], |
| 344 | if accu[0][30:] or accu[1:]: print '...', |
| 345 | print |
| 346 | if self.stack and self.stack[-1] == 'menu': |
| 347 | # XXX should be done differently |
| 348 | for line in accu: |
| 349 | if miprog.match(line) < 0: |
| 350 | line = string.strip(line) + '\n' |
| 351 | self.expand(line) |
| 352 | continue |
| 353 | (bgn, end), (a, b), (c, d), (e, f), (g, h) = \ |
| 354 | miprog.regs[:5] |
| 355 | label = line[a:b] |
| 356 | nodename = line[c:d] |
| 357 | if nodename[0] == ':': nodename = label |
| 358 | else: nodename = line[e:f] |
| 359 | punct = line[g:h] |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 360 | self.write(' <LI><A HREF="', |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 361 | makefile(nodename), |
| 362 | '">', nodename, |
| 363 | '</A>', punct, '\n') |
| 364 | self.expand(line[end:]) |
| 365 | else: |
| 366 | text = string.joinfields(accu, '') |
| 367 | self.expand(text) |
| 368 | |
| 369 | # Write a string, expanding embedded @-commands |
| 370 | def expand(self, text): |
| 371 | stack = [] |
| 372 | i = 0 |
| 373 | n = len(text) |
| 374 | while i < n: |
| 375 | start = i |
| 376 | i = spprog.search(text, i) |
| 377 | if i < 0: |
| 378 | self.write(text[start:]) |
| 379 | break |
| 380 | self.write(text[start:i]) |
| 381 | c = text[i] |
| 382 | i = i+1 |
| 383 | if c == '\n': |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 384 | self.write('\n') |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 385 | continue |
| 386 | if c == '<': |
| 387 | self.write('<') |
| 388 | continue |
| 389 | if c == '>': |
| 390 | self.write('>') |
| 391 | continue |
| 392 | if c == '&': |
| 393 | self.write('&') |
| 394 | continue |
| 395 | if c == '{': |
| 396 | stack.append('') |
| 397 | continue |
| 398 | if c == '}': |
| 399 | if not stack: |
| 400 | print '*** Unmatched }' |
| 401 | self.write('}') |
| 402 | continue |
| 403 | cmd = stack[-1] |
| 404 | del stack[-1] |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 405 | try: |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 406 | method = getattr(self, 'close_' + cmd) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 407 | except AttributeError: |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 408 | self.unknown_close(cmd) |
| 409 | continue |
| 410 | method() |
| 411 | continue |
| 412 | if c <> '@': |
| 413 | # Cannot happen unless spprog is changed |
| 414 | raise RuntimeError, 'unexpected funny '+`c` |
| 415 | start = i |
| 416 | while i < n and text[i] in string.letters: i = i+1 |
| 417 | if i == start: |
| 418 | # @ plus non-letter: literal next character |
| 419 | i = i+1 |
| 420 | c = text[start:i] |
| 421 | if c == ':': |
| 422 | # `@:' means no extra space after |
| 423 | # preceding `.', `?', `!' or `:' |
| 424 | pass |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 425 | else: |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 426 | # `@.' means a sentence-ending period; |
| 427 | # `@@', `@{', `@}' quote `@', `{', `}' |
| 428 | self.write(c) |
| 429 | continue |
| 430 | cmd = text[start:i] |
| 431 | if i < n and text[i] == '{': |
| 432 | i = i+1 |
| 433 | stack.append(cmd) |
| 434 | try: |
| 435 | method = getattr(self, 'open_' + cmd) |
| 436 | except AttributeError: |
| 437 | self.unknown_open(cmd) |
| 438 | continue |
| 439 | method() |
| 440 | continue |
| 441 | try: |
| 442 | method = getattr(self, 'handle_' + cmd) |
| 443 | except AttributeError: |
| 444 | self.unknown_handle(cmd) |
| 445 | continue |
| 446 | method() |
| 447 | if stack: |
| 448 | print '*** Stack not empty at para:', stack |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 449 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 450 | # --- Handle unknown embedded @-commands --- |
| 451 | |
| 452 | def unknown_open(self, cmd): |
| 453 | print '*** No open func for @' + cmd + '{...}' |
| 454 | cmd = cmd + '{' |
| 455 | self.write('@', cmd) |
| 456 | if not self.unknown.has_key(cmd): |
| 457 | self.unknown[cmd] = 1 |
| 458 | else: |
| 459 | self.unknown[cmd] = self.unknown[cmd] + 1 |
| 460 | |
| 461 | def unknown_close(self, cmd): |
| 462 | print '*** No close func for @' + cmd + '{...}' |
| 463 | cmd = '}' + cmd |
| 464 | self.write('}') |
| 465 | if not self.unknown.has_key(cmd): |
| 466 | self.unknown[cmd] = 1 |
| 467 | else: |
| 468 | self.unknown[cmd] = self.unknown[cmd] + 1 |
| 469 | |
| 470 | def unknown_handle(self, cmd): |
| 471 | print '*** No handler for @' + cmd |
| 472 | self.write('@', cmd) |
| 473 | if not self.unknown.has_key(cmd): |
| 474 | self.unknown[cmd] = 1 |
| 475 | else: |
| 476 | self.unknown[cmd] = self.unknown[cmd] + 1 |
| 477 | |
| 478 | # XXX The following sections should be ordered as the texinfo docs |
| 479 | |
| 480 | # --- Embedded @-commands without {} argument list -- |
| 481 | |
| 482 | def handle_noindent(self): pass |
| 483 | |
| 484 | def handle_refill(self): pass |
| 485 | |
| 486 | # --- Include file handling --- |
| 487 | |
| 488 | def do_include(self, args): |
| 489 | file = args |
| 490 | file = os.path.join(self.includedir, file) |
| 491 | try: |
| 492 | fp = open(file, 'r') |
| 493 | except IOError, msg: |
| 494 | print '*** Can\'t open include file', `file` |
| 495 | return |
| 496 | if self.debugging: |
| 497 | print '--> file', `file` |
| 498 | save_done = self.done |
| 499 | save_skip = self.skip |
| 500 | save_stack = self.stack |
| 501 | self.includedepth = self.includedepth + 1 |
| 502 | self.parserest(fp, 0) |
| 503 | self.includedepth = self.includedepth - 1 |
| 504 | fp.close() |
| 505 | self.done = save_done |
| 506 | self.skip = save_skip |
| 507 | self.stack = save_stack |
| 508 | if self.debugging: |
| 509 | print '<-- file', `file` |
| 510 | |
| 511 | # --- Special Insertions --- |
| 512 | |
| 513 | def open_dmn(self): pass |
| 514 | def close_dmn(self): pass |
| 515 | |
| 516 | def open_dots(self): self.write('...') |
| 517 | def close_dots(self): pass |
| 518 | |
| 519 | def open_bullet(self): pass |
| 520 | def close_bullet(self): pass |
| 521 | |
| 522 | def open_TeX(self): self.write('TeX') |
| 523 | def close_TeX(self): pass |
| 524 | |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 525 | def handle_copyright(self): self.write(self.COPYRIGHT_SYMBOL) |
| 526 | def open_copyright(self): self.write(self.COPYRIGHT_SYMBOL) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 527 | def close_copyright(self): pass |
| 528 | |
| 529 | def open_minus(self): self.write('-') |
| 530 | def close_minus(self): pass |
| 531 | |
| 532 | # --- Special Glyphs for Examples --- |
| 533 | |
| 534 | def open_result(self): self.write('=>') |
| 535 | def close_result(self): pass |
| 536 | |
| 537 | def open_expansion(self): self.write('==>') |
| 538 | def close_expansion(self): pass |
| 539 | |
| 540 | def open_print(self): self.write('-|') |
| 541 | def close_print(self): pass |
| 542 | |
| 543 | def open_error(self): self.write('error-->') |
| 544 | def close_error(self): pass |
| 545 | |
| 546 | def open_equiv(self): self.write('==') |
| 547 | def close_equiv(self): pass |
| 548 | |
| 549 | def open_point(self): self.write('-!-') |
| 550 | def close_point(self): pass |
| 551 | |
| 552 | # --- Cross References --- |
| 553 | |
| 554 | def open_pxref(self): |
| 555 | self.write('see ') |
| 556 | self.startsaving() |
| 557 | def close_pxref(self): |
| 558 | self.makeref() |
| 559 | |
| 560 | def open_xref(self): |
| 561 | self.write('See ') |
| 562 | self.startsaving() |
| 563 | def close_xref(self): |
| 564 | self.makeref() |
| 565 | |
| 566 | def open_ref(self): |
| 567 | self.startsaving() |
| 568 | def close_ref(self): |
| 569 | self.makeref() |
| 570 | |
| 571 | def open_inforef(self): |
| 572 | self.write('See info file ') |
| 573 | self.startsaving() |
| 574 | def close_inforef(self): |
| 575 | text = self.collectsavings() |
| 576 | args = string.splitfields(text, ',') |
| 577 | n = len(args) |
| 578 | for i in range(n): |
| 579 | args[i] = string.strip(args[i]) |
| 580 | while len(args) < 3: args.append('') |
| 581 | node = args[0] |
| 582 | file = args[2] |
| 583 | self.write('`', file, '\', node `', node, '\'') |
| 584 | |
| 585 | def makeref(self): |
| 586 | text = self.collectsavings() |
| 587 | args = string.splitfields(text, ',') |
| 588 | n = len(args) |
| 589 | for i in range(n): |
| 590 | args[i] = string.strip(args[i]) |
| 591 | while len(args) < 5: args.append('') |
| 592 | nodename = label = args[0] |
| 593 | if args[2]: label = args[2] |
| 594 | file = args[3] |
| 595 | title = args[4] |
| 596 | href = makefile(nodename) |
| 597 | if file: |
| 598 | href = '../' + file + '/' + href |
| 599 | self.write('<A HREF="', href, '">', label, '</A>') |
| 600 | |
| 601 | # --- Marking Words and Phrases --- |
| 602 | |
| 603 | # --- Other @xxx{...} commands --- |
| 604 | |
| 605 | def open_(self): pass # Used by {text enclosed in braces} |
| 606 | def close_(self): pass |
| 607 | |
| 608 | open_asis = open_ |
| 609 | close_asis = close_ |
| 610 | |
| 611 | def open_cite(self): self.write('<CITE>') |
| 612 | def close_cite(self): self.write('</CITE>') |
| 613 | |
| 614 | def open_code(self): self.write('<CODE>') |
| 615 | def close_code(self): self.write('</CODE>') |
| 616 | |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 617 | def open_t(self): self.write('<TT>') |
| 618 | def close_t(self): self.write('</TT>') |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 619 | |
| 620 | def open_dfn(self): self.write('<DFN>') |
| 621 | def close_dfn(self): self.write('</DFN>') |
| 622 | |
| 623 | def open_emph(self): self.write('<EM>') |
| 624 | def close_emph(self): self.write('</EM>') |
| 625 | |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 626 | def open_i(self): self.write('<I>') |
| 627 | def close_i(self): self.write('</I>') |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 628 | |
| 629 | def open_footnote(self): |
| 630 | # if self.savetext <> None: |
| 631 | # print '*** Recursive footnote -- expect weirdness' |
| 632 | id = len(self.footnotes) + 1 |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 633 | self.write(self.FN_SOURCE_PATTERN % {'id': `id`}) |
| 634 | self.startsaving() |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 635 | |
| 636 | def close_footnote(self): |
| 637 | id = len(self.footnotes) + 1 |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 638 | self.footnotes.append(id, self.collectsavings()) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 639 | |
| 640 | def writefootnotes(self): |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 641 | self.write(self.FN_HEADER) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 642 | for id, text in self.footnotes: |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 643 | self.write(self.FN_TARGET_PATTERN |
| 644 | % {'id': `id`, 'text': text}) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 645 | self.footnotes = [] |
| 646 | |
| 647 | def open_file(self): self.write('<CODE>') |
| 648 | def close_file(self): self.write('</CODE>') |
| 649 | |
| 650 | def open_kbd(self): self.write('<KBD>') |
| 651 | def close_kbd(self): self.write('</KBD>') |
| 652 | |
| 653 | def open_key(self): self.write('<KEY>') |
| 654 | def close_key(self): self.write('</KEY>') |
| 655 | |
| 656 | def open_r(self): self.write('<R>') |
| 657 | def close_r(self): self.write('</R>') |
| 658 | |
| 659 | def open_samp(self): self.write('`<SAMP>') |
| 660 | def close_samp(self): self.write('</SAMP>\'') |
| 661 | |
| 662 | def open_sc(self): self.write('<SMALLCAPS>') |
| 663 | def close_sc(self): self.write('</SMALLCAPS>') |
| 664 | |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 665 | def open_strong(self): self.write('<STRONG>') |
| 666 | def close_strong(self): self.write('</STRONG>') |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 667 | |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 668 | def open_b(self): self.write('<B>') |
| 669 | def close_b(self): self.write('</B>') |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 670 | |
| 671 | def open_var(self): self.write('<VAR>') |
| 672 | def close_var(self): self.write('</VAR>') |
| 673 | |
| 674 | def open_w(self): self.write('<NOBREAK>') |
| 675 | def close_w(self): self.write('</NOBREAK>') |
| 676 | |
| 677 | open_titlefont = open_ |
| 678 | close_titlefont = close_ |
| 679 | |
| 680 | def open_small(self): pass |
| 681 | def close_small(self): pass |
| 682 | |
| 683 | def command(self, line): |
| 684 | a, b = cmprog.regs[1] |
| 685 | cmd = line[a:b] |
| 686 | args = string.strip(line[b:]) |
| 687 | if self.debugging > 1: |
| 688 | print self.skip, self.stack, '@' + cmd, args |
| 689 | try: |
| 690 | func = getattr(self, 'do_' + cmd) |
| 691 | except AttributeError: |
| 692 | try: |
| 693 | func = getattr(self, 'bgn_' + cmd) |
| 694 | except AttributeError: |
| 695 | # don't complain if we are skipping anyway |
| 696 | if not self.skip: |
| 697 | self.unknown_cmd(cmd, args) |
| 698 | return |
| 699 | self.stack.append(cmd) |
| 700 | func(args) |
| 701 | return |
| 702 | if not self.skip or cmd == 'end': |
| 703 | func(args) |
| 704 | |
| 705 | def unknown_cmd(self, cmd, args): |
| 706 | print '*** unknown', '@' + cmd, args |
| 707 | if not self.unknown.has_key(cmd): |
| 708 | self.unknown[cmd] = 1 |
| 709 | else: |
| 710 | self.unknown[cmd] = self.unknown[cmd] + 1 |
| 711 | |
| 712 | def do_end(self, args): |
| 713 | words = string.split(args) |
| 714 | if not words: |
| 715 | print '*** @end w/o args' |
| 716 | else: |
| 717 | cmd = words[0] |
| 718 | if not self.stack or self.stack[-1] <> cmd: |
| 719 | print '*** @end', cmd, 'unexpected' |
| 720 | else: |
| 721 | del self.stack[-1] |
| 722 | try: |
| 723 | func = getattr(self, 'end_' + cmd) |
| 724 | except AttributeError: |
| 725 | self.unknown_end(cmd) |
| 726 | return |
| 727 | func() |
| 728 | |
| 729 | def unknown_end(self, cmd): |
| 730 | cmd = 'end ' + cmd |
| 731 | print '*** unknown', '@' + cmd |
| 732 | if not self.unknown.has_key(cmd): |
| 733 | self.unknown[cmd] = 1 |
| 734 | else: |
| 735 | self.unknown[cmd] = self.unknown[cmd] + 1 |
| 736 | |
| 737 | # --- Comments --- |
| 738 | |
| 739 | def do_comment(self, args): pass |
| 740 | do_c = do_comment |
| 741 | |
| 742 | # --- Conditional processing --- |
| 743 | |
| 744 | def bgn_ifinfo(self, args): pass |
| 745 | def end_ifinfo(self): pass |
| 746 | |
| 747 | def bgn_iftex(self, args): self.skip = self.skip + 1 |
| 748 | def end_iftex(self): self.skip = self.skip - 1 |
| 749 | |
| 750 | def bgn_ignore(self, args): self.skip = self.skip + 1 |
| 751 | def end_ignore(self): self.skip = self.skip - 1 |
| 752 | |
| 753 | def bgn_tex(self, args): self.skip = self.skip + 1 |
| 754 | def end_tex(self): self.skip = self.skip - 1 |
| 755 | |
| 756 | def do_set(self, args): |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 757 | fields = string.splitfields(args, ' ') |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 758 | key = fields[0] |
| 759 | if len(fields) == 1: |
| 760 | value = 1 |
| 761 | else: |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 762 | value = string.joinfields(fields[1:], ' ') |
| 763 | self.values[key] = value |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 764 | print self.values |
| 765 | |
| 766 | def do_clear(self, args): |
| 767 | self.values[args] = None |
| 768 | |
| 769 | def bgn_ifset(self, args): |
| 770 | if args not in self.values.keys() \ |
| 771 | or self.values[args] is None: |
| 772 | self.skip = self.skip + 1 |
| 773 | self.stackinfo[len(self.stack)] = 1 |
| 774 | else: |
| 775 | self.stackinfo[len(self.stack)] = 0 |
| 776 | def end_ifset(self): |
| 777 | print self.stack |
| 778 | print self.stackinfo |
| 779 | if self.stackinfo[len(self.stack) + 1]: |
| 780 | self.skip = self.skip - 1 |
| 781 | del self.stackinfo[len(self.stack) + 1] |
| 782 | |
| 783 | def bgn_ifclear(self, args): |
| 784 | if args in self.values.keys() \ |
| 785 | and self.values[args] is not None: |
| 786 | self.skip = self.skip + 1 |
| 787 | self.stackinfo[len(self.stack)] = 1 |
| 788 | else: |
| 789 | self.stackinfo[len(self.stack)] = 0 |
| 790 | |
| 791 | end_ifclear = end_ifset |
| 792 | |
| 793 | def open_value(self): |
| 794 | self.startsaving() |
| 795 | |
| 796 | def close_value(self): |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 797 | key = self.collectsavings() |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 798 | if key in self.values.keys(): |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 799 | self.write(self.values[key]) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 800 | else: |
| 801 | print '*** Undefined value: ', key |
| 802 | |
| 803 | # --- Beginning a file --- |
| 804 | |
| 805 | do_finalout = do_comment |
| 806 | do_setchapternewpage = do_comment |
| 807 | do_setfilename = do_comment |
| 808 | |
| 809 | def do_settitle(self, args): |
| 810 | print args |
| 811 | self.startsaving() |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 812 | self.expand(args) |
| 813 | self.title = self.collectsavings() |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 814 | print self.title |
| 815 | def do_parskip(self, args): pass |
| 816 | |
| 817 | # --- Ending a file --- |
| 818 | |
| 819 | def do_bye(self, args): |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 820 | self.endnode() |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 821 | self.done = 1 |
| 822 | |
| 823 | # --- Title page --- |
| 824 | |
| 825 | def bgn_titlepage(self, args): self.skip = self.skip + 1 |
| 826 | def end_titlepage(self): self.skip = self.skip - 1 |
| 827 | def do_shorttitlepage(self, args): pass |
| 828 | |
| 829 | def do_center(self, args): |
| 830 | # Actually not used outside title page... |
| 831 | self.write('<H1>') |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 832 | self.expand(args) |
| 833 | self.write('</H1>\n') |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 834 | do_title = do_center |
| 835 | do_subtitle = do_center |
| 836 | do_author = do_center |
| 837 | |
| 838 | do_vskip = do_comment |
| 839 | do_vfill = do_comment |
| 840 | do_smallbook = do_comment |
| 841 | |
| 842 | do_paragraphindent = do_comment |
| 843 | do_setchapternewpage = do_comment |
| 844 | do_headings = do_comment |
| 845 | do_footnotestyle = do_comment |
| 846 | |
| 847 | do_evenheading = do_comment |
| 848 | do_evenfooting = do_comment |
| 849 | do_oddheading = do_comment |
| 850 | do_oddfooting = do_comment |
| 851 | do_everyheading = do_comment |
| 852 | do_everyfooting = do_comment |
| 853 | |
| 854 | # --- Nodes --- |
| 855 | |
| 856 | def do_node(self, args): |
| 857 | self.endnode() |
| 858 | self.nodelineno = 0 |
| 859 | parts = string.splitfields(args, ',') |
| 860 | while len(parts) < 4: parts.append('') |
| 861 | for i in range(4): parts[i] = string.strip(parts[i]) |
| 862 | self.nodelinks = parts |
| 863 | [name, next, prev, up] = parts[:4] |
| 864 | file = self.dirname + '/' + makefile(name) |
| 865 | if self.filenames.has_key(file): |
| 866 | print '*** Filename already in use: ', file |
| 867 | else: |
| 868 | if self.debugging: print '--- writing', file |
| 869 | self.filenames[file] = 1 |
| 870 | # self.nodefp = open(file, 'w') |
| 871 | self.nodename = name |
| 872 | if self.cont and self.nodestack: |
| 873 | self.nodestack[-1].cont = self.nodename |
| 874 | if not self.topname: self.topname = name |
| 875 | title = name |
| 876 | if self.title: title = title + ' -- ' + self.title |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 877 | self.node = self.Node(self.dirname, self.nodename, self.topname, |
| 878 | title, next, prev, up) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 879 | |
| 880 | def link(self, label, nodename): |
| 881 | if nodename: |
| 882 | if string.lower(nodename) == '(dir)': |
| 883 | addr = '../dir.html' |
| 884 | else: |
| 885 | addr = makefile(nodename) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 886 | self.write(label, ': <A HREF="', addr, '" TYPE="', |
| 887 | label, '">', nodename, '</A> \n') |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 888 | |
| 889 | # --- Sectioning commands --- |
| 890 | |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 891 | def popstack(self, type): |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 892 | if (self.node): |
| 893 | self.node.type = type |
| 894 | while self.nodestack: |
| 895 | if self.nodestack[-1].type > type: |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 896 | self.nodestack[-1].finalize() |
| 897 | self.nodestack[-1].flush() |
| 898 | del self.nodestack[-1] |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 899 | elif self.nodestack[-1].type == type: |
| 900 | if not self.nodestack[-1].next: |
| 901 | self.nodestack[-1].next = self.node.name |
| 902 | if not self.node.prev: |
| 903 | self.node.prev = self.nodestack[-1].name |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 904 | self.nodestack[-1].finalize() |
| 905 | self.nodestack[-1].flush() |
| 906 | del self.nodestack[-1] |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 907 | else: |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 908 | if type > 1 and not self.node.up: |
| 909 | self.node.up = self.nodestack[-1].name |
| 910 | break |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 911 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 912 | def do_chapter(self, args): |
| 913 | self.heading('H1', args, 0) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 914 | self.popstack(1) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 915 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 916 | def do_unnumbered(self, args): |
| 917 | self.heading('H1', args, -1) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 918 | self.popstack(1) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 919 | def do_appendix(self, args): |
| 920 | self.heading('H1', args, -1) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 921 | self.popstack(1) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 922 | def do_top(self, args): |
| 923 | self.heading('H1', args, -1) |
| 924 | def do_chapheading(self, args): |
| 925 | self.heading('H1', args, -1) |
| 926 | def do_majorheading(self, args): |
| 927 | self.heading('H1', args, -1) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 928 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 929 | def do_section(self, args): |
| 930 | self.heading('H1', args, 1) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 931 | self.popstack(2) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 932 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 933 | def do_unnumberedsec(self, args): |
| 934 | self.heading('H1', args, -1) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 935 | self.popstack(2) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 936 | def do_appendixsec(self, args): |
| 937 | self.heading('H1', args, -1) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 938 | self.popstack(2) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 939 | do_appendixsection = do_appendixsec |
| 940 | def do_heading(self, args): |
| 941 | self.heading('H1', args, -1) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 942 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 943 | def do_subsection(self, args): |
| 944 | self.heading('H2', args, 2) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 945 | self.popstack(3) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 946 | def do_unnumberedsubsec(self, args): |
| 947 | self.heading('H2', args, -1) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 948 | self.popstack(3) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 949 | def do_appendixsubsec(self, args): |
| 950 | self.heading('H2', args, -1) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 951 | self.popstack(3) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 952 | def do_subheading(self, args): |
| 953 | self.heading('H2', args, -1) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 954 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 955 | def do_subsubsection(self, args): |
| 956 | self.heading('H3', args, 3) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 957 | self.popstack(4) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 958 | def do_unnumberedsubsubsec(self, args): |
| 959 | self.heading('H3', args, -1) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 960 | self.popstack(4) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 961 | def do_appendixsubsubsec(self, args): |
| 962 | self.heading('H3', args, -1) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 963 | self.popstack(4) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 964 | def do_subsubheading(self, args): |
| 965 | self.heading('H3', args, -1) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 966 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 967 | def heading(self, type, args, level): |
| 968 | if level >= 0: |
| 969 | while len(self.numbering) <= level: |
| 970 | self.numbering.append(0) |
| 971 | del self.numbering[level+1:] |
| 972 | self.numbering[level] = self.numbering[level] + 1 |
| 973 | x = '' |
| 974 | for i in self.numbering: |
| 975 | x = x + `i` + '.' |
| 976 | args = x + ' ' + args |
| 977 | self.contents.append(level, args, self.nodename) |
| 978 | self.write('<', type, '>') |
| 979 | self.expand(args) |
| 980 | self.write('</', type, '>\n') |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 981 | if self.debugging or self.print_headers: |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 982 | print '---', args |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 983 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 984 | def do_contents(self, args): |
| 985 | # pass |
| 986 | self.listcontents('Table of Contents', 999) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 987 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 988 | def do_shortcontents(self, args): |
| 989 | pass |
| 990 | # self.listcontents('Short Contents', 0) |
| 991 | do_summarycontents = do_shortcontents |
Guido van Rossum | a12bbff | 1995-05-03 14:17:36 +0000 | [diff] [blame] | 992 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 993 | def listcontents(self, title, maxlevel): |
| 994 | self.write('<H1>', title, '</H1>\n<UL COMPACT PLAIN>\n') |
| 995 | prevlevels = [0] |
| 996 | for level, title, node in self.contents: |
| 997 | if level > maxlevel: |
| 998 | continue |
| 999 | if level > prevlevels[-1]: |
| 1000 | # can only advance one level at a time |
| 1001 | self.write(' '*prevlevels[-1], '<UL PLAIN>\n') |
| 1002 | prevlevels.append(level) |
| 1003 | elif level < prevlevels[-1]: |
| 1004 | # might drop back multiple levels |
| 1005 | while level < prevlevels[-1]: |
| 1006 | del prevlevels[-1] |
| 1007 | self.write(' '*prevlevels[-1], |
| 1008 | '</UL>\n') |
| 1009 | self.write(' '*level, '<LI> <A HREF="', |
| 1010 | makefile(node), '">') |
| 1011 | self.expand(title) |
| 1012 | self.write('</A>\n') |
| 1013 | self.write('</UL>\n' * len(prevlevels)) |
Guido van Rossum | 06f4289 | 1995-08-28 03:01:00 +0000 | [diff] [blame] | 1014 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1015 | # --- Page lay-out --- |
Guido van Rossum | 06f4289 | 1995-08-28 03:01:00 +0000 | [diff] [blame] | 1016 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1017 | # These commands are only meaningful in printed text |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1018 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1019 | def do_page(self, args): pass |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1020 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1021 | def do_need(self, args): pass |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1022 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1023 | def bgn_group(self, args): pass |
| 1024 | def end_group(self): pass |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1025 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1026 | # --- Line lay-out --- |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1027 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1028 | def do_sp(self, args): |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1029 | if self.nofill: |
| 1030 | self.write('\n') |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1031 | else: |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1032 | self.write('<P>\n') |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1033 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1034 | def do_hline(self, args): |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1035 | self.write('<HR>') |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1036 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1037 | # --- Function and variable definitions --- |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1038 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1039 | def bgn_deffn(self, args): |
| 1040 | self.write('<DL>') |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1041 | self.do_deffnx(args) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1042 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1043 | def end_deffn(self): |
| 1044 | self.write('</DL>\n') |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1045 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1046 | def do_deffnx(self, args): |
| 1047 | self.write('<DT>') |
| 1048 | words = splitwords(args, 2) |
| 1049 | [category, name], rest = words[:2], words[2:] |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1050 | self.expand('@b{%s}' % name) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1051 | for word in rest: self.expand(' ' + makevar(word)) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1052 | #self.expand(' -- ' + category) |
| 1053 | self.write('\n<DD>') |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1054 | self.index('fn', name) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1055 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1056 | def bgn_defun(self, args): self.bgn_deffn('Function ' + args) |
| 1057 | end_defun = end_deffn |
| 1058 | def do_defunx(self, args): self.do_deffnx('Function ' + args) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1059 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1060 | def bgn_defmac(self, args): self.bgn_deffn('Macro ' + args) |
| 1061 | end_defmac = end_deffn |
| 1062 | def do_defmacx(self, args): self.do_deffnx('Macro ' + args) |
Guido van Rossum | 06f4289 | 1995-08-28 03:01:00 +0000 | [diff] [blame] | 1063 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1064 | def bgn_defspec(self, args): self.bgn_deffn('{Special Form} ' + args) |
| 1065 | end_defspec = end_deffn |
| 1066 | def do_defspecx(self, args): self.do_deffnx('{Special Form} ' + args) |
Guido van Rossum | 06f4289 | 1995-08-28 03:01:00 +0000 | [diff] [blame] | 1067 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1068 | def bgn_defvr(self, args): |
| 1069 | self.write('<DL>') |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1070 | self.do_defvrx(args) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1071 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1072 | end_defvr = end_deffn |
Guido van Rossum | 06f4289 | 1995-08-28 03:01:00 +0000 | [diff] [blame] | 1073 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1074 | def do_defvrx(self, args): |
| 1075 | self.write('<DT>') |
| 1076 | words = splitwords(args, 2) |
| 1077 | [category, name], rest = words[:2], words[2:] |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1078 | self.expand('@code{%s}' % name) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1079 | # If there are too many arguments, show them |
| 1080 | for word in rest: self.expand(' ' + word) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1081 | #self.expand(' -- ' + category) |
| 1082 | self.write('\n<DD>') |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1083 | self.index('vr', name) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1084 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1085 | def bgn_defvar(self, args): self.bgn_defvr('Variable ' + args) |
| 1086 | end_defvar = end_defvr |
| 1087 | def do_defvarx(self, args): self.do_defvrx('Variable ' + args) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1088 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1089 | def bgn_defopt(self, args): self.bgn_defvr('{User Option} ' + args) |
| 1090 | end_defopt = end_defvr |
| 1091 | def do_defoptx(self, args): self.do_defvrx('{User Option} ' + args) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1092 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1093 | # --- Ditto for typed languages --- |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1094 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1095 | def bgn_deftypefn(self, args): |
| 1096 | self.write('<DL>') |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1097 | self.do_deftypefnx(args) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1098 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1099 | end_deftypefn = end_deffn |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1100 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1101 | def do_deftypefnx(self, args): |
| 1102 | self.write('<DT>') |
| 1103 | words = splitwords(args, 3) |
| 1104 | [category, datatype, name], rest = words[:3], words[3:] |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1105 | self.expand('@code{%s} @b{%s}' % (datatype, name)) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1106 | for word in rest: self.expand(' ' + makevar(word)) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1107 | #self.expand(' -- ' + category) |
| 1108 | self.write('\n<DD>') |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1109 | self.index('fn', name) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1110 | |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1111 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1112 | def bgn_deftypefun(self, args): self.bgn_deftypefn('Function ' + args) |
| 1113 | end_deftypefun = end_deftypefn |
| 1114 | def do_deftypefunx(self, args): self.do_deftypefnx('Function ' + args) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1115 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1116 | def bgn_deftypevr(self, args): |
| 1117 | self.write('<DL>') |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1118 | self.do_deftypevrx(args) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1119 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1120 | end_deftypevr = end_deftypefn |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1121 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1122 | def do_deftypevrx(self, args): |
| 1123 | self.write('<DT>') |
| 1124 | words = splitwords(args, 3) |
| 1125 | [category, datatype, name], rest = words[:3], words[3:] |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1126 | self.expand('@code{%s} @b{%s}' % (datatype, name)) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1127 | # If there are too many arguments, show them |
| 1128 | for word in rest: self.expand(' ' + word) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1129 | #self.expand(' -- ' + category) |
| 1130 | self.write('\n<DD>') |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1131 | self.index('fn', name) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1132 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1133 | def bgn_deftypevar(self, args): |
| 1134 | self.bgn_deftypevr('Variable ' + args) |
| 1135 | end_deftypevar = end_deftypevr |
| 1136 | def do_deftypevarx(self, args): |
| 1137 | self.do_deftypevrx('Variable ' + args) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1138 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1139 | # --- Ditto for object-oriented languages --- |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1140 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1141 | def bgn_defcv(self, args): |
| 1142 | self.write('<DL>') |
| 1143 | self.do_defcvx(args) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1144 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1145 | end_defcv = end_deftypevr |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1146 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1147 | def do_defcvx(self, args): |
| 1148 | self.write('<DT>') |
| 1149 | words = splitwords(args, 3) |
| 1150 | [category, classname, name], rest = words[:3], words[3:] |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1151 | self.expand('@b{%s}' % name) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1152 | # If there are too many arguments, show them |
| 1153 | for word in rest: self.expand(' ' + word) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1154 | #self.expand(' -- %s of @code{%s}' % (category, classname)) |
| 1155 | self.write('\n<DD>') |
| 1156 | self.index('vr', '%s @r{on %s}' % (name, classname)) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1157 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1158 | def bgn_defivar(self, args): |
| 1159 | self.bgn_defcv('{Instance Variable} ' + args) |
| 1160 | end_defivar = end_defcv |
| 1161 | def do_defivarx(self, args): |
| 1162 | self.do_defcvx('{Instance Variable} ' + args) |
Guido van Rossum | a12bbff | 1995-05-03 14:17:36 +0000 | [diff] [blame] | 1163 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1164 | def bgn_defop(self, args): |
| 1165 | self.write('<DL>') |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1166 | self.do_defopx(args) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1167 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1168 | end_defop = end_defcv |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1169 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1170 | def do_defopx(self, args): |
| 1171 | self.write('<DT>') |
| 1172 | words = splitwords(args, 3) |
| 1173 | [category, classname, name], rest = words[:3], words[3:] |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1174 | self.expand('@b{%s}' % name) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1175 | for word in rest: self.expand(' ' + makevar(word)) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1176 | #self.expand(' -- %s of @code{%s}' % (category, classname)) |
| 1177 | self.write('\n<DD>') |
| 1178 | self.index('fn', '%s @r{on %s}' % (name, classname)) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1179 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1180 | def bgn_defmethod(self, args): |
| 1181 | self.bgn_defop('Method ' + args) |
| 1182 | end_defmethod = end_defop |
| 1183 | def do_defmethodx(self, args): |
| 1184 | self.do_defopx('Method ' + args) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1185 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1186 | # --- Ditto for data types --- |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1187 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1188 | def bgn_deftp(self, args): |
| 1189 | self.write('<DL>') |
| 1190 | self.do_deftpx(args) |
Guido van Rossum | 06f4289 | 1995-08-28 03:01:00 +0000 | [diff] [blame] | 1191 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1192 | end_deftp = end_defcv |
Guido van Rossum | 06f4289 | 1995-08-28 03:01:00 +0000 | [diff] [blame] | 1193 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1194 | def do_deftpx(self, args): |
| 1195 | self.write('<DT>') |
| 1196 | words = splitwords(args, 2) |
| 1197 | [category, name], rest = words[:2], words[2:] |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1198 | self.expand('@b{%s}' % name) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1199 | for word in rest: self.expand(' ' + word) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1200 | #self.expand(' -- ' + category) |
| 1201 | self.write('\n<DD>') |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1202 | self.index('tp', name) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1203 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1204 | # --- Making Lists and Tables |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1205 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1206 | def bgn_enumerate(self, args): |
| 1207 | if not args: |
| 1208 | self.write('<OL>\n') |
| 1209 | self.stackinfo[len(self.stack)] = '</OL>\n' |
| 1210 | else: |
| 1211 | self.itemnumber = args |
| 1212 | self.write('<UL>\n') |
| 1213 | self.stackinfo[len(self.stack)] = '</UL>\n' |
| 1214 | def end_enumerate(self): |
| 1215 | self.itemnumber = None |
| 1216 | self.write(self.stackinfo[len(self.stack) + 1]) |
| 1217 | del self.stackinfo[len(self.stack) + 1] |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1218 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1219 | def bgn_itemize(self, args): |
| 1220 | self.itemarg = args |
| 1221 | self.write('<UL>\n') |
| 1222 | def end_itemize(self): |
| 1223 | self.itemarg = None |
| 1224 | self.write('</UL>\n') |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1225 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1226 | def bgn_table(self, args): |
| 1227 | self.itemarg = args |
| 1228 | self.write('<DL>\n') |
| 1229 | def end_table(self): |
| 1230 | self.itemarg = None |
| 1231 | self.write('</DL>\n') |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1232 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1233 | def bgn_ftable(self, args): |
| 1234 | self.itemindex = 'fn' |
| 1235 | self.bgn_table(args) |
| 1236 | def end_ftable(self): |
| 1237 | self.itemindex = None |
| 1238 | self.end_table() |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1239 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1240 | def do_item(self, args): |
| 1241 | if self.itemindex: self.index(self.itemindex, args) |
| 1242 | if self.itemarg: |
| 1243 | if self.itemarg[0] == '@' and self.itemarg[1:2] and \ |
| 1244 | self.itemarg[1] in string.letters: |
| 1245 | args = self.itemarg + '{' + args + '}' |
| 1246 | else: |
| 1247 | # some other character, e.g. '-' |
| 1248 | args = self.itemarg + ' ' + args |
| 1249 | if self.itemnumber <> None: |
| 1250 | args = self.itemnumber + '. ' + args |
| 1251 | self.itemnumber = increment(self.itemnumber) |
| 1252 | if self.stack and self.stack[-1] == 'table': |
| 1253 | self.write('<DT>') |
| 1254 | self.expand(args) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1255 | self.write('\n<DD>') |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1256 | else: |
| 1257 | self.write('<LI>') |
| 1258 | self.expand(args) |
| 1259 | self.write(' ') |
| 1260 | do_itemx = do_item # XXX Should suppress leading blank line |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1261 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1262 | # --- Enumerations, displays, quotations --- |
| 1263 | # XXX Most of these should increase the indentation somehow |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1264 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1265 | def bgn_quotation(self, args): self.write('<BLOCKQUOTE>') |
| 1266 | def end_quotation(self): self.write('</BLOCKQUOTE>\n') |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1267 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1268 | def bgn_example(self, args): |
| 1269 | self.nofill = self.nofill + 1 |
| 1270 | self.write('<PRE>') |
| 1271 | def end_example(self): |
| 1272 | self.write('</PRE>') |
| 1273 | self.nofill = self.nofill - 1 |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1274 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1275 | bgn_lisp = bgn_example # Synonym when contents are executable lisp code |
| 1276 | end_lisp = end_example |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1277 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1278 | bgn_smallexample = bgn_example # XXX Should use smaller font |
| 1279 | end_smallexample = end_example |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1280 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1281 | bgn_smalllisp = bgn_lisp # Ditto |
| 1282 | end_smalllisp = end_lisp |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1283 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1284 | bgn_display = bgn_example |
| 1285 | end_display = end_example |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1286 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1287 | bgn_format = bgn_display |
| 1288 | end_format = end_display |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1289 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1290 | def do_exdent(self, args): self.expand(args + '\n') |
| 1291 | # XXX Should really mess with indentation |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1292 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1293 | def bgn_flushleft(self, args): |
| 1294 | self.nofill = self.nofill + 1 |
| 1295 | self.write('<PRE>\n') |
| 1296 | def end_flushleft(self): |
| 1297 | self.write('</PRE>\n') |
| 1298 | self.nofill = self.nofill - 1 |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1299 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1300 | def bgn_flushright(self, args): |
| 1301 | self.nofill = self.nofill + 1 |
| 1302 | self.write('<ADDRESS COMPACT>\n') |
| 1303 | def end_flushright(self): |
| 1304 | self.write('</ADDRESS>\n') |
| 1305 | self.nofill = self.nofill - 1 |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1306 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1307 | def bgn_menu(self, args): |
| 1308 | self.write('<DIR>\n') |
| 1309 | self.write(' <STRONG><EM>Menu</EM></STRONG><P>\n') |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1310 | def end_menu(self): |
| 1311 | self.write('</DIR>\n') |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1312 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1313 | def bgn_cartouche(self, args): pass |
| 1314 | def end_cartouche(self): pass |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1315 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1316 | # --- Indices --- |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1317 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1318 | def resetindex(self): |
| 1319 | self.noncodeindices = ['cp'] |
| 1320 | self.indextitle = {} |
| 1321 | self.indextitle['cp'] = 'Concept' |
| 1322 | self.indextitle['fn'] = 'Function' |
| 1323 | self.indextitle['ky'] = 'Keyword' |
| 1324 | self.indextitle['pg'] = 'Program' |
| 1325 | self.indextitle['tp'] = 'Type' |
| 1326 | self.indextitle['vr'] = 'Variable' |
| 1327 | # |
| 1328 | self.whichindex = {} |
| 1329 | for name in self.indextitle.keys(): |
| 1330 | self.whichindex[name] = [] |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1331 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1332 | def user_index(self, name, args): |
| 1333 | if self.whichindex.has_key(name): |
| 1334 | self.index(name, args) |
| 1335 | else: |
| 1336 | print '*** No index named', `name` |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1337 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1338 | def do_cindex(self, args): self.index('cp', args) |
| 1339 | def do_findex(self, args): self.index('fn', args) |
| 1340 | def do_kindex(self, args): self.index('ky', args) |
| 1341 | def do_pindex(self, args): self.index('pg', args) |
| 1342 | def do_tindex(self, args): self.index('tp', args) |
| 1343 | def do_vindex(self, args): self.index('vr', args) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1344 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1345 | def index(self, name, args): |
| 1346 | self.whichindex[name].append(args, self.nodename) |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1347 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1348 | def do_synindex(self, args): |
| 1349 | words = string.split(args) |
| 1350 | if len(words) <> 2: |
| 1351 | print '*** bad @synindex', args |
| 1352 | return |
| 1353 | [old, new] = words |
| 1354 | if not self.whichindex.has_key(old) or \ |
| 1355 | not self.whichindex.has_key(new): |
| 1356 | print '*** bad key(s) in @synindex', args |
| 1357 | return |
| 1358 | if old <> new and \ |
| 1359 | self.whichindex[old] is not self.whichindex[new]: |
| 1360 | inew = self.whichindex[new] |
| 1361 | inew[len(inew):] = self.whichindex[old] |
| 1362 | self.whichindex[old] = inew |
| 1363 | do_syncodeindex = do_synindex # XXX Should use code font |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1364 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1365 | def do_printindex(self, args): |
| 1366 | words = string.split(args) |
| 1367 | for name in words: |
| 1368 | if self.whichindex.has_key(name): |
| 1369 | self.prindex(name) |
| 1370 | else: |
| 1371 | print '*** No index named', `name` |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1372 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1373 | def prindex(self, name): |
| 1374 | iscodeindex = (name not in self.noncodeindices) |
| 1375 | index = self.whichindex[name] |
| 1376 | if not index: return |
| 1377 | if self.debugging: |
| 1378 | print '--- Generating', self.indextitle[name], 'index' |
| 1379 | # The node already provides a title |
| 1380 | index1 = [] |
| 1381 | junkprog = regex.compile('^\(@[a-z]+\)?{') |
| 1382 | for key, node in index: |
| 1383 | sortkey = string.lower(key) |
| 1384 | # Remove leading `@cmd{' from sort key |
| 1385 | # -- don't bother about the matching `}' |
| 1386 | oldsortkey = sortkey |
| 1387 | while 1: |
| 1388 | i = junkprog.match(sortkey) |
| 1389 | if i < 0: break |
| 1390 | sortkey = sortkey[i:] |
| 1391 | index1.append(sortkey, key, node) |
| 1392 | del index[:] |
| 1393 | index1.sort() |
| 1394 | self.write('<DL COMPACT>\n') |
| 1395 | prevkey = prevnode = None |
| 1396 | for sortkey, key, node in index1: |
| 1397 | if (key, node) == (prevkey, prevnode): |
| 1398 | continue |
| 1399 | if self.debugging > 1: print key, ':', node |
| 1400 | self.write('<DT>') |
| 1401 | if iscodeindex: key = '@code{' + key + '}' |
| 1402 | if key != prevkey: |
| 1403 | self.expand(key) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1404 | self.write('\n<DD><A HREF="%s">%s</A>\n' % (makefile(node), node)) |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1405 | prevkey, prevnode = key, node |
| 1406 | self.write('</DL>\n') |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1407 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1408 | # --- Final error reports --- |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1409 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1410 | def report(self): |
| 1411 | if self.unknown: |
| 1412 | print '--- Unrecognized commands ---' |
| 1413 | cmds = self.unknown.keys() |
| 1414 | cmds.sort() |
| 1415 | for cmd in cmds: |
| 1416 | print string.ljust(cmd, 20), self.unknown[cmd] |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1417 | |
| 1418 | |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1419 | class TexinfoParserHTML3(TexinfoParser): |
| 1420 | |
| 1421 | COPYRIGHT_SYMBOL = "©" |
| 1422 | FN_ID_PATTERN = "[%(id)s]" |
| 1423 | FN_SOURCE_PATTERN = '<A ID=footnoteref%(id)s ' \ |
| 1424 | 'HREF="#footnotetext%(id)s">' + FN_ID_PATTERN + '</A>' |
| 1425 | FN_TARGET_PATTERN = '<FN ID=footnotetext%(id)s>\n' \ |
| 1426 | '<P><A HREF="#footnoteref%(id)s">' + FN_ID_PATTERN \ |
| 1427 | + '</A>\n%(text)s</P></FN>\n' |
| 1428 | FN_HEADER = '<DIV CLASS=footnotes>\n <HR NOSHADE WIDTH=200>\n' \ |
| 1429 | ' <STRONG><EM>Footnotes</EM></STRONG>\n <P>\n' |
| 1430 | |
| 1431 | Node = HTML3Node |
| 1432 | |
| 1433 | def bgn_quotation(self, args): self.write('<BQ>') |
| 1434 | def end_quotation(self): self.write('</BQ>\n') |
| 1435 | |
| 1436 | def bgn_example(self, args): |
| 1437 | self.nofill = self.nofill + 1 |
| 1438 | self.write('<PRE CLASS=example>') |
| 1439 | |
| 1440 | def bgn_flushleft(self, args): |
| 1441 | self.nofill = self.nofill + 1 |
| 1442 | self.write('<PRE CLASS=flushleft>\n') |
| 1443 | |
| 1444 | def bgn_flushright(self, args): |
| 1445 | self.nofill = self.nofill + 1 |
| 1446 | self.write('<DIV ALIGN=right CLASS=flushright><ADDRESS COMPACT>\n') |
| 1447 | def end_flushright(self): |
| 1448 | self.write('</ADDRESS></DIV>\n') |
| 1449 | self.nofill = self.nofill - 1 |
| 1450 | |
| 1451 | def bgn_menu(self, args): |
| 1452 | self.write('<UL PLAIN CLASS=menu>\n') |
| 1453 | self.write(' <LH>Menu</LH>\n') |
| 1454 | def end_menu(self): |
| 1455 | self.write('</UL>\n') |
| 1456 | |
| 1457 | |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1458 | # Put @var{} around alphabetic substrings |
| 1459 | def makevar(str): |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1460 | return '@var{'+str+'}' |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1461 | |
| 1462 | |
| 1463 | # Split a string in "words" according to findwordend |
| 1464 | def splitwords(str, minlength): |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1465 | words = [] |
| 1466 | i = 0 |
| 1467 | n = len(str) |
| 1468 | while i < n: |
| 1469 | while i < n and str[i] in ' \t\n': i = i+1 |
| 1470 | if i >= n: break |
| 1471 | start = i |
| 1472 | i = findwordend(str, i, n) |
| 1473 | words.append(str[start:i]) |
| 1474 | while len(words) < minlength: words.append('') |
| 1475 | return words |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1476 | |
| 1477 | |
| 1478 | # Find the end of a "word", matching braces and interpreting @@ @{ @} |
| 1479 | fwprog = regex.compile('[@{} ]') |
| 1480 | def findwordend(str, i, n): |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1481 | level = 0 |
| 1482 | while i < n: |
| 1483 | i = fwprog.search(str, i) |
| 1484 | if i < 0: break |
| 1485 | c = str[i]; i = i+1 |
| 1486 | if c == '@': i = i+1 # Next character is not special |
| 1487 | elif c == '{': level = level+1 |
| 1488 | elif c == '}': level = level-1 |
| 1489 | elif c == ' ' and level <= 0: return i-1 |
| 1490 | return n |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1491 | |
| 1492 | |
| 1493 | # Convert a node name into a file name |
| 1494 | def makefile(nodename): |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1495 | return fixfunnychars(nodename) + '.html' |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1496 | |
| 1497 | |
| 1498 | # Characters that are perfectly safe in filenames and hyperlinks |
Guido van Rossum | 06f4289 | 1995-08-28 03:01:00 +0000 | [diff] [blame] | 1499 | goodchars = string.letters + string.digits + '!@-=+.' |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1500 | |
Guido van Rossum | 06f4289 | 1995-08-28 03:01:00 +0000 | [diff] [blame] | 1501 | # Replace characters that aren't perfectly safe by dashes |
| 1502 | # Underscores are bad since Cern HTTPD treats them as delimiters for |
| 1503 | # encoding times, so you get mismatches if you compress your files: |
| 1504 | # a.html.gz will map to a_b.html.gz |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1505 | def fixfunnychars(addr): |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1506 | i = 0 |
| 1507 | while i < len(addr): |
| 1508 | c = addr[i] |
| 1509 | if c not in goodchars: |
| 1510 | c = '-' |
| 1511 | addr = addr[:i] + c + addr[i+1:] |
| 1512 | i = i + len(c) |
| 1513 | return addr |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1514 | |
| 1515 | |
| 1516 | # Increment a string used as an enumeration |
| 1517 | def increment(s): |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1518 | if not s: |
| 1519 | return '1' |
| 1520 | for sequence in string.digits, string.lowercase, string.uppercase: |
| 1521 | lastc = s[-1] |
| 1522 | if lastc in sequence: |
| 1523 | i = string.index(sequence, lastc) + 1 |
| 1524 | if i >= len(sequence): |
| 1525 | if len(s) == 1: |
| 1526 | s = sequence[0]*2 |
| 1527 | if s == '00': |
| 1528 | s = '10' |
| 1529 | else: |
| 1530 | s = increment(s[:-1]) + sequence[0] |
| 1531 | else: |
| 1532 | s = s[:-1] + sequence[i] |
| 1533 | return s |
| 1534 | return s # Don't increment |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1535 | |
| 1536 | |
| 1537 | def test(): |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1538 | import sys |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1539 | debugging = 0 |
| 1540 | print_headers = 0 |
| 1541 | cont = 0 |
| 1542 | html3 = 0 |
| 1543 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1544 | while sys.argv[1:2] == ['-d']: |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1545 | debugging = debugging + 1 |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1546 | del sys.argv[1:2] |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1547 | if sys.argv[1] == '-p': |
| 1548 | print_headers = 1 |
| 1549 | del sys.argv[1] |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1550 | if sys.argv[1] == '-c': |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1551 | cont = 1 |
| 1552 | del sys.argv[1] |
| 1553 | if sys.argv[1] == '-3': |
| 1554 | html3 = 1 |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1555 | del sys.argv[1] |
| 1556 | if len(sys.argv) <> 3: |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1557 | print 'usage: texi2html [-d [-d]] [-p] [-c] inputfile outputdirectory' |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1558 | sys.exit(2) |
Fred Drake | 0282726 | 1996-10-09 19:05:12 +0000 | [diff] [blame] | 1559 | |
| 1560 | if html3: |
| 1561 | parser = TexinfoParserHTML3() |
| 1562 | else: |
| 1563 | parser = TexinfoParser() |
| 1564 | parser.cont = cont |
| 1565 | parser.debugging = debugging |
| 1566 | parser.print_headers = print_headers |
| 1567 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1568 | file = sys.argv[1] |
| 1569 | parser.setdirname(sys.argv[2]) |
| 1570 | if file == '-': |
| 1571 | fp = sys.stdin |
| 1572 | else: |
| 1573 | parser.setincludedir(os.path.dirname(file)) |
| 1574 | try: |
| 1575 | fp = open(file, 'r') |
| 1576 | except IOError, msg: |
| 1577 | print file, ':', msg |
| 1578 | sys.exit(1) |
| 1579 | parser.parse(fp) |
| 1580 | fp.close() |
| 1581 | parser.report() |
Guido van Rossum | 26a9d37 | 1995-03-15 11:26:05 +0000 | [diff] [blame] | 1582 | |
| 1583 | |
Fred Drake | a39a25e | 1996-09-13 14:44:34 +0000 | [diff] [blame] | 1584 | if __name__ == "__main__": |
| 1585 | test() |