Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
Fred Drake | 0eb7b2a | 1999-05-19 17:37:37 +0000 | [diff] [blame] | 3 | """Generate ESIS events based on a LaTeX source document and |
| 4 | configuration data. |
| 5 | |
| 6 | The conversion is not strong enough to work with arbitrary LaTeX |
| 7 | documents; it has only been designed to work with the highly stylized |
| 8 | markup used in the standard Python documentation. A lot of |
| 9 | information about specific markup is encoded in the control table |
| 10 | passed to the convert() function; changing this table can allow this |
| 11 | tool to support additional LaTeX markups. |
| 12 | |
| 13 | The format of the table is largely undocumented; see the commented |
| 14 | headers where the table is specified in main(). There is no provision |
| 15 | to load an alternate table from an external file. |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 16 | """ |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 17 | |
| 18 | import errno |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 19 | import getopt |
| 20 | import os |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 21 | import re |
| 22 | import string |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 23 | import sys |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 24 | import UserList |
Fred Drake | 691a5a7 | 2000-11-22 17:56:43 +0000 | [diff] [blame] | 25 | import xml.sax.saxutils |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 26 | |
Fred Drake | 54fb7fb | 1999-05-10 19:36:03 +0000 | [diff] [blame] | 27 | from types import ListType, StringType, TupleType |
Fred Drake | aeea981 | 1998-12-01 19:04:12 +0000 | [diff] [blame] | 28 | |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 29 | try: |
| 30 | from xml.parsers.xmllib import XMLParser |
| 31 | except ImportError: |
| 32 | from xmllib import XMLParser |
| 33 | |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 34 | |
Fred Drake | 2262a80 | 2001-03-23 16:53:34 +0000 | [diff] [blame] | 35 | from esistools import encode |
| 36 | |
| 37 | |
Fred Drake | d7acf02 | 1999-01-14 17:38:12 +0000 | [diff] [blame] | 38 | DEBUG = 0 |
| 39 | |
| 40 | |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 41 | class LaTeXFormatError(Exception): |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 42 | pass |
| 43 | |
| 44 | |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 45 | class LaTeXStackError(LaTeXFormatError): |
| 46 | def __init__(self, found, stack): |
| 47 | msg = "environment close for %s doesn't match;\n stack = %s" \ |
| 48 | % (found, stack) |
| 49 | self.found = found |
| 50 | self.stack = stack[:] |
| 51 | LaTeXFormatError.__init__(self, msg) |
| 52 | |
| 53 | |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 54 | _begin_env_rx = re.compile(r"[\\]begin{([^}]*)}") |
| 55 | _end_env_rx = re.compile(r"[\\]end{([^}]*)}") |
Fred Drake | 0eb7b2a | 1999-05-19 17:37:37 +0000 | [diff] [blame] | 56 | _begin_macro_rx = re.compile(r"[\\]([a-zA-Z]+[*]?) ?({|\s*\n?)") |
Fred Drake | 96c00b0 | 1999-05-07 19:59:02 +0000 | [diff] [blame] | 57 | _comment_rx = re.compile("%+ ?(.*)\n[ \t]*") |
Fred Drake | 691a5a7 | 2000-11-22 17:56:43 +0000 | [diff] [blame] | 58 | _text_rx = re.compile(r"[^]~%\\{}]+") |
Fred Drake | b5fc0ab | 2001-07-06 21:01:19 +0000 | [diff] [blame] | 59 | _optional_rx = re.compile(r"\s*[[]([^]]*)[]]", re.MULTILINE) |
Fred Drake | aeea981 | 1998-12-01 19:04:12 +0000 | [diff] [blame] | 60 | # _parameter_rx is this complicated to allow {...} inside a parameter; |
| 61 | # this is useful to match tabular layout specifications like {c|p{24pt}} |
| 62 | _parameter_rx = re.compile("[ \n]*{(([^{}}]|{[^}]*})*)}") |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 63 | _token_rx = re.compile(r"[a-zA-Z][a-zA-Z0-9.-]*$") |
| 64 | _start_group_rx = re.compile("[ \n]*{") |
| 65 | _start_optional_rx = re.compile("[ \n]*[[]") |
| 66 | |
| 67 | |
Fred Drake | 42f5298 | 1998-11-30 14:45:24 +0000 | [diff] [blame] | 68 | ESCAPED_CHARS = "$%#^ {}&~" |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 69 | |
| 70 | |
Fred Drake | f79acbd | 1999-05-07 21:12:21 +0000 | [diff] [blame] | 71 | def dbgmsg(msg): |
Fred Drake | d7acf02 | 1999-01-14 17:38:12 +0000 | [diff] [blame] | 72 | if DEBUG: |
Fred Drake | f79acbd | 1999-05-07 21:12:21 +0000 | [diff] [blame] | 73 | sys.stderr.write(msg + "\n") |
| 74 | |
| 75 | def pushing(name, point, depth): |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 76 | dbgmsg("pushing <%s> at %s" % (name, point)) |
Fred Drake | d7acf02 | 1999-01-14 17:38:12 +0000 | [diff] [blame] | 77 | |
| 78 | def popping(name, point, depth): |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 79 | dbgmsg("popping </%s> at %s" % (name, point)) |
Fred Drake | d7acf02 | 1999-01-14 17:38:12 +0000 | [diff] [blame] | 80 | |
| 81 | |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 82 | class _Stack(UserList.UserList): |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 83 | def append(self, entry): |
Fred Drake | 4fbdf97 | 1999-08-02 14:35:25 +0000 | [diff] [blame] | 84 | if type(entry) is not StringType: |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 85 | raise LaTeXFormatError("cannot push non-string on stack: " |
| 86 | + `entry`) |
Fred Drake | 2262a80 | 2001-03-23 16:53:34 +0000 | [diff] [blame] | 87 | #dbgmsg("%s<%s>" % (" "*len(self.data), entry)) |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 88 | self.data.append(entry) |
| 89 | |
| 90 | def pop(self, index=-1): |
| 91 | entry = self.data[index] |
| 92 | del self.data[index] |
Fred Drake | 2262a80 | 2001-03-23 16:53:34 +0000 | [diff] [blame] | 93 | #dbgmsg("%s</%s>" % (" "*len(self.data), entry)) |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 94 | |
| 95 | def __delitem__(self, index): |
| 96 | entry = self.data[index] |
| 97 | del self.data[index] |
Fred Drake | 2262a80 | 2001-03-23 16:53:34 +0000 | [diff] [blame] | 98 | #dbgmsg("%s</%s>" % (" "*len(self.data), entry)) |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 99 | |
| 100 | |
| 101 | def new_stack(): |
| 102 | if DEBUG: |
| 103 | return _Stack() |
| 104 | return [] |
| 105 | |
| 106 | |
Fred Drake | 4fbdf97 | 1999-08-02 14:35:25 +0000 | [diff] [blame] | 107 | class Conversion: |
| 108 | def __init__(self, ifp, ofp, table): |
| 109 | self.write = ofp.write |
| 110 | self.ofp = ofp |
Fred Drake | 96c00b0 | 1999-05-07 19:59:02 +0000 | [diff] [blame] | 111 | self.table = table |
Fred Drake | 0f9bfd3 | 2001-09-28 16:26:13 +0000 | [diff] [blame] | 112 | self.line = string.join([s.rstrip() for s in ifp.readlines()], "\n") |
Fred Drake | 96c00b0 | 1999-05-07 19:59:02 +0000 | [diff] [blame] | 113 | self.preamble = 1 |
Fred Drake | 96c00b0 | 1999-05-07 19:59:02 +0000 | [diff] [blame] | 114 | |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 115 | def convert(self): |
| 116 | self.subconvert() |
| 117 | |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 118 | def subconvert(self, endchar=None, depth=0): |
| 119 | # |
| 120 | # Parses content, including sub-structures, until the character |
| 121 | # 'endchar' is found (with no open structures), or until the end |
| 122 | # of the input data is endchar is None. |
| 123 | # |
| 124 | stack = new_stack() |
| 125 | line = self.line |
| 126 | while line: |
| 127 | if line[0] == endchar and not stack: |
| 128 | self.line = line |
| 129 | return line |
| 130 | m = _comment_rx.match(line) |
| 131 | if m: |
| 132 | text = m.group(1) |
| 133 | if text: |
| 134 | self.write("(COMMENT\n- %s \n)COMMENT\n-\\n\n" |
| 135 | % encode(text)) |
| 136 | line = line[m.end():] |
| 137 | continue |
| 138 | m = _begin_env_rx.match(line) |
| 139 | if m: |
| 140 | name = m.group(1) |
| 141 | entry = self.get_env_entry(name) |
| 142 | # re-write to use the macro handler |
| 143 | line = r"\%s %s" % (name, line[m.end():]) |
| 144 | continue |
| 145 | m = _end_env_rx.match(line) |
| 146 | if m: |
| 147 | # end of environment |
| 148 | envname = m.group(1) |
| 149 | entry = self.get_entry(envname) |
| 150 | while stack and envname != stack[-1] \ |
| 151 | and stack[-1] in entry.endcloses: |
| 152 | self.write(")%s\n" % stack.pop()) |
| 153 | if stack and envname == stack[-1]: |
| 154 | self.write(")%s\n" % entry.outputname) |
| 155 | del stack[-1] |
| 156 | else: |
| 157 | raise LaTeXStackError(envname, stack) |
| 158 | line = line[m.end():] |
| 159 | continue |
| 160 | m = _begin_macro_rx.match(line) |
| 161 | if m: |
| 162 | # start of macro |
| 163 | macroname = m.group(1) |
Fred Drake | 691a5a7 | 2000-11-22 17:56:43 +0000 | [diff] [blame] | 164 | if macroname == "c": |
| 165 | # Ugh! This is a combining character... |
| 166 | endpos = m.end() |
| 167 | self.combining_char("c", line[endpos]) |
| 168 | line = line[endpos + 1:] |
| 169 | continue |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 170 | entry = self.get_entry(macroname) |
| 171 | if entry.verbatim: |
| 172 | # magic case! |
Fred Drake | 0f9bfd3 | 2001-09-28 16:26:13 +0000 | [diff] [blame] | 173 | pos = line.find("\\end{%s}" % macroname) |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 174 | text = line[m.end(1):pos] |
| 175 | stack.append(entry.name) |
| 176 | self.write("(%s\n" % entry.outputname) |
| 177 | self.write("-%s\n" % encode(text)) |
| 178 | self.write(")%s\n" % entry.outputname) |
| 179 | stack.pop() |
| 180 | line = line[pos + len("\\end{%s}" % macroname):] |
| 181 | continue |
| 182 | while stack and stack[-1] in entry.closes: |
| 183 | top = stack.pop() |
| 184 | topentry = self.get_entry(top) |
| 185 | if topentry.outputname: |
| 186 | self.write(")%s\n-\\n\n" % topentry.outputname) |
| 187 | # |
Fred Drake | 9eda3ae | 2001-09-25 20:57:36 +0000 | [diff] [blame] | 188 | if entry.outputname and entry.empty: |
| 189 | self.write("e\n") |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 190 | # |
Fred Drake | 9eda3ae | 2001-09-25 20:57:36 +0000 | [diff] [blame] | 191 | params, optional, empty = self.start_macro(macroname) |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 192 | # rip off the macroname |
| 193 | if params: |
| 194 | line = line[m.end(1):] |
| 195 | elif empty: |
| 196 | line = line[m.end(1):] |
| 197 | else: |
| 198 | line = line[m.end():] |
| 199 | opened = 0 |
| 200 | implied_content = 0 |
| 201 | |
| 202 | # handle attribute mappings here: |
| 203 | for pentry in params: |
| 204 | if pentry.type == "attribute": |
| 205 | if pentry.optional: |
| 206 | m = _optional_rx.match(line) |
Fred Drake | 4fbdf97 | 1999-08-02 14:35:25 +0000 | [diff] [blame] | 207 | if m and entry.outputname: |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 208 | line = line[m.end():] |
| 209 | self.dump_attr(pentry, m.group(1)) |
Fred Drake | 4fbdf97 | 1999-08-02 14:35:25 +0000 | [diff] [blame] | 210 | elif pentry.text and entry.outputname: |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 211 | # value supplied by conversion spec: |
| 212 | self.dump_attr(pentry, pentry.text) |
| 213 | else: |
| 214 | m = _parameter_rx.match(line) |
| 215 | if not m: |
| 216 | raise LaTeXFormatError( |
| 217 | "could not extract parameter %s for %s: %s" |
| 218 | % (pentry.name, macroname, `line[:100]`)) |
Fred Drake | 4fbdf97 | 1999-08-02 14:35:25 +0000 | [diff] [blame] | 219 | if entry.outputname: |
| 220 | self.dump_attr(pentry, m.group(1)) |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 221 | line = line[m.end():] |
| 222 | elif pentry.type == "child": |
| 223 | if pentry.optional: |
| 224 | m = _optional_rx.match(line) |
| 225 | if m: |
| 226 | line = line[m.end():] |
| 227 | if entry.outputname and not opened: |
| 228 | opened = 1 |
| 229 | self.write("(%s\n" % entry.outputname) |
| 230 | stack.append(macroname) |
| 231 | stack.append(pentry.name) |
| 232 | self.write("(%s\n" % pentry.name) |
| 233 | self.write("-%s\n" % encode(m.group(1))) |
| 234 | self.write(")%s\n" % pentry.name) |
| 235 | stack.pop() |
| 236 | else: |
| 237 | if entry.outputname and not opened: |
| 238 | opened = 1 |
| 239 | self.write("(%s\n" % entry.outputname) |
| 240 | stack.append(entry.name) |
| 241 | self.write("(%s\n" % pentry.name) |
| 242 | stack.append(pentry.name) |
| 243 | self.line = skip_white(line)[1:] |
| 244 | line = self.subconvert( |
| 245 | "}", len(stack) + depth + 1)[1:] |
| 246 | self.write(")%s\n" % stack.pop()) |
| 247 | elif pentry.type == "content": |
| 248 | if pentry.implied: |
| 249 | implied_content = 1 |
| 250 | else: |
| 251 | if entry.outputname and not opened: |
| 252 | opened = 1 |
| 253 | self.write("(%s\n" % entry.outputname) |
| 254 | stack.append(entry.name) |
| 255 | line = skip_white(line) |
| 256 | if line[0] != "{": |
| 257 | raise LaTeXFormatError( |
| 258 | "missing content for " + macroname) |
| 259 | self.line = line[1:] |
| 260 | line = self.subconvert("}", len(stack) + depth + 1) |
| 261 | if line and line[0] == "}": |
| 262 | line = line[1:] |
Fred Drake | 4fbdf97 | 1999-08-02 14:35:25 +0000 | [diff] [blame] | 263 | elif pentry.type == "text" and pentry.text: |
| 264 | if entry.outputname and not opened: |
| 265 | opened = 1 |
| 266 | stack.append(entry.name) |
| 267 | self.write("(%s\n" % entry.outputname) |
Fred Drake | 2262a80 | 2001-03-23 16:53:34 +0000 | [diff] [blame] | 268 | #dbgmsg("--- text: %s" % `pentry.text`) |
Fred Drake | 4fbdf97 | 1999-08-02 14:35:25 +0000 | [diff] [blame] | 269 | self.write("-%s\n" % encode(pentry.text)) |
Fred Drake | f6199ed | 1999-08-26 17:54:16 +0000 | [diff] [blame] | 270 | elif pentry.type == "entityref": |
| 271 | self.write("&%s\n" % pentry.name) |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 272 | if entry.outputname: |
| 273 | if not opened: |
| 274 | self.write("(%s\n" % entry.outputname) |
| 275 | stack.append(entry.name) |
| 276 | if not implied_content: |
| 277 | self.write(")%s\n" % entry.outputname) |
| 278 | stack.pop() |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 279 | continue |
| 280 | if line[0] == endchar and not stack: |
| 281 | self.line = line[1:] |
| 282 | return self.line |
| 283 | if line[0] == "}": |
| 284 | # end of macro or group |
| 285 | macroname = stack[-1] |
| 286 | if macroname: |
Fred Drake | 2262a80 | 2001-03-23 16:53:34 +0000 | [diff] [blame] | 287 | conversion = self.table[macroname] |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 288 | if conversion.outputname: |
| 289 | # otherwise, it was just a bare group |
| 290 | self.write(")%s\n" % conversion.outputname) |
| 291 | del stack[-1] |
| 292 | line = line[1:] |
| 293 | continue |
Fred Drake | 691a5a7 | 2000-11-22 17:56:43 +0000 | [diff] [blame] | 294 | if line[0] == "~": |
| 295 | # don't worry about the "tie" aspect of this command |
| 296 | line = line[1:] |
| 297 | self.write("- \n") |
| 298 | continue |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 299 | if line[0] == "{": |
| 300 | stack.append("") |
| 301 | line = line[1:] |
| 302 | continue |
| 303 | if line[0] == "\\" and line[1] in ESCAPED_CHARS: |
| 304 | self.write("-%s\n" % encode(line[1])) |
| 305 | line = line[2:] |
| 306 | continue |
| 307 | if line[:2] == r"\\": |
| 308 | self.write("(BREAK\n)BREAK\n") |
| 309 | line = line[2:] |
| 310 | continue |
Fred Drake | 691a5a7 | 2000-11-22 17:56:43 +0000 | [diff] [blame] | 311 | if line[:2] == r"\_": |
| 312 | line = "_" + line[2:] |
| 313 | continue |
| 314 | if line[:2] in (r"\'", r'\"'): |
| 315 | # combining characters... |
| 316 | self.combining_char(line[1], line[2]) |
| 317 | line = line[3:] |
| 318 | continue |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 319 | m = _text_rx.match(line) |
| 320 | if m: |
| 321 | text = encode(m.group()) |
| 322 | self.write("-%s\n" % text) |
| 323 | line = line[m.end():] |
| 324 | continue |
| 325 | # special case because of \item[] |
| 326 | # XXX can we axe this??? |
| 327 | if line[0] == "]": |
| 328 | self.write("-]\n") |
| 329 | line = line[1:] |
| 330 | continue |
| 331 | # avoid infinite loops |
| 332 | extra = "" |
| 333 | if len(line) > 100: |
| 334 | extra = "..." |
| 335 | raise LaTeXFormatError("could not identify markup: %s%s" |
| 336 | % (`line[:100]`, extra)) |
| 337 | while stack: |
| 338 | entry = self.get_entry(stack[-1]) |
| 339 | if entry.closes: |
| 340 | self.write(")%s\n-%s\n" % (entry.outputname, encode("\n"))) |
| 341 | del stack[-1] |
| 342 | else: |
| 343 | break |
| 344 | if stack: |
| 345 | raise LaTeXFormatError("elements remain on stack: " |
| 346 | + string.join(stack, ", ")) |
| 347 | # otherwise we just ran out of input here... |
| 348 | |
Fred Drake | 691a5a7 | 2000-11-22 17:56:43 +0000 | [diff] [blame] | 349 | # This is a really limited table of combinations, but it will have |
| 350 | # to do for now. |
| 351 | _combinations = { |
| 352 | ("c", "c"): 0x00E7, |
| 353 | ("'", "e"): 0x00E9, |
| 354 | ('"', "o"): 0x00F6, |
| 355 | } |
| 356 | |
| 357 | def combining_char(self, prefix, char): |
| 358 | ordinal = self._combinations[(prefix, char)] |
| 359 | self.write("-\\%%%d;\n" % ordinal) |
| 360 | |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 361 | def start_macro(self, name): |
| 362 | conversion = self.get_entry(name) |
| 363 | parameters = conversion.parameters |
| 364 | optional = parameters and parameters[0].optional |
Fred Drake | 9eda3ae | 2001-09-25 20:57:36 +0000 | [diff] [blame] | 365 | return parameters, optional, conversion.empty |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 366 | |
| 367 | def get_entry(self, name): |
| 368 | entry = self.table.get(name) |
| 369 | if entry is None: |
Fred Drake | 2262a80 | 2001-03-23 16:53:34 +0000 | [diff] [blame] | 370 | dbgmsg("get_entry(%s) failing; building default entry!" % `name`) |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 371 | # not defined; build a default entry: |
| 372 | entry = TableEntry(name) |
| 373 | entry.has_content = 1 |
| 374 | entry.parameters.append(Parameter("content")) |
| 375 | self.table[name] = entry |
| 376 | return entry |
| 377 | |
| 378 | def get_env_entry(self, name): |
| 379 | entry = self.table.get(name) |
| 380 | if entry is None: |
| 381 | # not defined; build a default entry: |
| 382 | entry = TableEntry(name, 1) |
| 383 | entry.has_content = 1 |
| 384 | entry.parameters.append(Parameter("content")) |
| 385 | entry.parameters[-1].implied = 1 |
| 386 | self.table[name] = entry |
| 387 | elif not entry.environment: |
| 388 | raise LaTeXFormatError( |
| 389 | name + " is defined as a macro; expected environment") |
| 390 | return entry |
| 391 | |
| 392 | def dump_attr(self, pentry, value): |
| 393 | if not (pentry.name and value): |
| 394 | return |
| 395 | if _token_rx.match(value): |
| 396 | dtype = "TOKEN" |
| 397 | else: |
| 398 | dtype = "CDATA" |
| 399 | self.write("A%s %s %s\n" % (pentry.name, dtype, encode(value))) |
| 400 | |
| 401 | |
Fred Drake | eac8abe | 1999-07-29 22:42:27 +0000 | [diff] [blame] | 402 | def convert(ifp, ofp, table): |
| 403 | c = Conversion(ifp, ofp, table) |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 404 | try: |
| 405 | c.convert() |
| 406 | except IOError, (err, msg): |
| 407 | if err != errno.EPIPE: |
| 408 | raise |
| 409 | |
| 410 | |
Fred Drake | d7acf02 | 1999-01-14 17:38:12 +0000 | [diff] [blame] | 411 | def skip_white(line): |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 412 | while line and line[0] in " %\n\t\r": |
Fred Drake | 0f9bfd3 | 2001-09-28 16:26:13 +0000 | [diff] [blame] | 413 | line = line[1:].lstrip() |
Fred Drake | d7acf02 | 1999-01-14 17:38:12 +0000 | [diff] [blame] | 414 | return line |
| 415 | |
| 416 | |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 417 | |
| 418 | class TableEntry: |
| 419 | def __init__(self, name, environment=0): |
| 420 | self.name = name |
| 421 | self.outputname = name |
| 422 | self.environment = environment |
| 423 | self.empty = not environment |
| 424 | self.has_content = 0 |
| 425 | self.verbatim = 0 |
| 426 | self.auto_close = 0 |
| 427 | self.parameters = [] |
| 428 | self.closes = [] |
| 429 | self.endcloses = [] |
| 430 | |
| 431 | class Parameter: |
| 432 | def __init__(self, type, name=None, optional=0): |
| 433 | self.type = type |
| 434 | self.name = name |
| 435 | self.optional = optional |
| 436 | self.text = '' |
| 437 | self.implied = 0 |
| 438 | |
| 439 | |
| 440 | class TableParser(XMLParser): |
Fred Drake | 4fbdf97 | 1999-08-02 14:35:25 +0000 | [diff] [blame] | 441 | def __init__(self, table=None): |
| 442 | if table is None: |
| 443 | table = {} |
| 444 | self.__table = table |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 445 | self.__current = None |
| 446 | self.__buffer = '' |
| 447 | XMLParser.__init__(self) |
| 448 | |
| 449 | def get_table(self): |
| 450 | for entry in self.__table.values(): |
| 451 | if entry.environment and not entry.has_content: |
| 452 | p = Parameter("content") |
| 453 | p.implied = 1 |
| 454 | entry.parameters.append(p) |
| 455 | entry.has_content = 1 |
| 456 | return self.__table |
| 457 | |
| 458 | def start_environment(self, attrs): |
| 459 | name = attrs["name"] |
| 460 | self.__current = TableEntry(name, environment=1) |
| 461 | self.__current.verbatim = attrs.get("verbatim") == "yes" |
| 462 | if attrs.has_key("outputname"): |
| 463 | self.__current.outputname = attrs.get("outputname") |
Fred Drake | 0f9bfd3 | 2001-09-28 16:26:13 +0000 | [diff] [blame] | 464 | self.__current.endcloses = attrs.get("endcloses", "").split() |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 465 | def end_environment(self): |
| 466 | self.end_macro() |
| 467 | |
| 468 | def start_macro(self, attrs): |
| 469 | name = attrs["name"] |
| 470 | self.__current = TableEntry(name) |
Fred Drake | 0f9bfd3 | 2001-09-28 16:26:13 +0000 | [diff] [blame] | 471 | self.__current.closes = attrs.get("closes", "").split() |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 472 | if attrs.has_key("outputname"): |
| 473 | self.__current.outputname = attrs.get("outputname") |
| 474 | def end_macro(self): |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 475 | self.__table[self.__current.name] = self.__current |
| 476 | self.__current = None |
| 477 | |
| 478 | def start_attribute(self, attrs): |
| 479 | name = attrs.get("name") |
| 480 | optional = attrs.get("optional") == "yes" |
| 481 | if name: |
| 482 | p = Parameter("attribute", name, optional=optional) |
| 483 | else: |
| 484 | p = Parameter("attribute", optional=optional) |
| 485 | self.__current.parameters.append(p) |
| 486 | self.__buffer = '' |
| 487 | def end_attribute(self): |
| 488 | self.__current.parameters[-1].text = self.__buffer |
| 489 | |
Fred Drake | f6199ed | 1999-08-26 17:54:16 +0000 | [diff] [blame] | 490 | def start_entityref(self, attrs): |
| 491 | name = attrs["name"] |
| 492 | p = Parameter("entityref", name) |
| 493 | self.__current.parameters.append(p) |
| 494 | |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 495 | def start_child(self, attrs): |
| 496 | name = attrs["name"] |
| 497 | p = Parameter("child", name, attrs.get("optional") == "yes") |
| 498 | self.__current.parameters.append(p) |
| 499 | self.__current.empty = 0 |
| 500 | |
| 501 | def start_content(self, attrs): |
| 502 | p = Parameter("content") |
| 503 | p.implied = attrs.get("implied") == "yes" |
| 504 | if self.__current.environment: |
| 505 | p.implied = 1 |
| 506 | self.__current.parameters.append(p) |
| 507 | self.__current.has_content = 1 |
| 508 | self.__current.empty = 0 |
| 509 | |
| 510 | def start_text(self, attrs): |
Fred Drake | 4fbdf97 | 1999-08-02 14:35:25 +0000 | [diff] [blame] | 511 | self.__current.empty = 0 |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 512 | self.__buffer = '' |
| 513 | def end_text(self): |
| 514 | p = Parameter("text") |
| 515 | p.text = self.__buffer |
| 516 | self.__current.parameters.append(p) |
| 517 | |
| 518 | def handle_data(self, data): |
| 519 | self.__buffer = self.__buffer + data |
| 520 | |
| 521 | |
Fred Drake | 4fbdf97 | 1999-08-02 14:35:25 +0000 | [diff] [blame] | 522 | def load_table(fp, table=None): |
| 523 | parser = TableParser(table=table) |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 524 | parser.feed(fp.read()) |
| 525 | parser.close() |
| 526 | return parser.get_table() |
| 527 | |
| 528 | |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 529 | def main(): |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 530 | global DEBUG |
| 531 | # |
Fred Drake | eac8abe | 1999-07-29 22:42:27 +0000 | [diff] [blame] | 532 | opts, args = getopt.getopt(sys.argv[1:], "D", ["debug"]) |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 533 | for opt, arg in opts: |
Fred Drake | eac8abe | 1999-07-29 22:42:27 +0000 | [diff] [blame] | 534 | if opt in ("-D", "--debug"): |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 535 | DEBUG = DEBUG + 1 |
| 536 | if len(args) == 0: |
| 537 | ifp = sys.stdin |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 538 | ofp = sys.stdout |
Fred Drake | 96e4a06 | 1999-07-29 22:22:13 +0000 | [diff] [blame] | 539 | elif len(args) == 1: |
| 540 | ifp = open(args) |
| 541 | ofp = sys.stdout |
| 542 | elif len(args) == 2: |
| 543 | ifp = open(args[0]) |
| 544 | ofp = open(args[1], "w") |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 545 | else: |
| 546 | usage() |
| 547 | sys.exit(2) |
Fred Drake | eac8abe | 1999-07-29 22:42:27 +0000 | [diff] [blame] | 548 | |
| 549 | table = load_table(open(os.path.join(sys.path[0], 'conversion.xml'))) |
| 550 | convert(ifp, ofp, table) |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 551 | |
| 552 | |
| 553 | if __name__ == "__main__": |
| 554 | main() |