blob: 74e1dc7e12fb5dbbe41ec22c9a4ca98c5be50fc8 [file] [log] [blame]
Fred Drake30a68c71998-11-23 16:59:39 +00001#! /usr/bin/env python
2
Fred Drake0eb7b2a1999-05-19 17:37:37 +00003"""Generate ESIS events based on a LaTeX source document and
4configuration data.
5
6The conversion is not strong enough to work with arbitrary LaTeX
7documents; it has only been designed to work with the highly stylized
8markup used in the standard Python documentation. A lot of
9information about specific markup is encoded in the control table
10passed to the convert() function; changing this table can allow this
11tool to support additional LaTeX markups.
12
13The format of the table is largely undocumented; see the commented
14headers where the table is specified in main(). There is no provision
15to load an alternate table from an external file.
Fred Drake30a68c71998-11-23 16:59:39 +000016"""
Fred Drake30a68c71998-11-23 16:59:39 +000017
18import errno
Fred Drake96e4a061999-07-29 22:22:13 +000019import getopt
20import os
Fred Drake30a68c71998-11-23 16:59:39 +000021import re
22import string
Fred Drake30a68c71998-11-23 16:59:39 +000023import sys
Fred Drake96e4a061999-07-29 22:22:13 +000024import UserList
Fred Drake691a5a72000-11-22 17:56:43 +000025import xml.sax.saxutils
Fred Drake30a68c71998-11-23 16:59:39 +000026
Fred Drake54fb7fb1999-05-10 19:36:03 +000027from types import ListType, StringType, TupleType
Fred Drakeaeea9811998-12-01 19:04:12 +000028
Fred Drake96e4a061999-07-29 22:22:13 +000029try:
30 from xml.parsers.xmllib import XMLParser
31except ImportError:
32 from xmllib import XMLParser
33
Fred Drake30a68c71998-11-23 16:59:39 +000034
Fred Drake2262a802001-03-23 16:53:34 +000035from esistools import encode
36
37
Fred Draked7acf021999-01-14 17:38:12 +000038DEBUG = 0
39
40
Fred Drake96e4a061999-07-29 22:22:13 +000041class LaTeXFormatError(Exception):
Fred Drake30a68c71998-11-23 16:59:39 +000042 pass
43
44
Fred Drake96e4a061999-07-29 22:22:13 +000045class 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 Drake30a68c71998-11-23 16:59:39 +000054_begin_env_rx = re.compile(r"[\\]begin{([^}]*)}")
55_end_env_rx = re.compile(r"[\\]end{([^}]*)}")
Fred Drake0eb7b2a1999-05-19 17:37:37 +000056_begin_macro_rx = re.compile(r"[\\]([a-zA-Z]+[*]?) ?({|\s*\n?)")
Fred Drake96c00b01999-05-07 19:59:02 +000057_comment_rx = re.compile("%+ ?(.*)\n[ \t]*")
Fred Drake691a5a72000-11-22 17:56:43 +000058_text_rx = re.compile(r"[^]~%\\{}]+")
Fred Drake30a68c71998-11-23 16:59:39 +000059_optional_rx = re.compile(r"\s*[[]([^]]*)[]]")
Fred Drakeaeea9811998-12-01 19:04:12 +000060# _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 Drake30a68c71998-11-23 16:59:39 +000063_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 Drake42f52981998-11-30 14:45:24 +000068ESCAPED_CHARS = "$%#^ {}&~"
Fred Drake30a68c71998-11-23 16:59:39 +000069
70
Fred Drakef79acbd1999-05-07 21:12:21 +000071def dbgmsg(msg):
Fred Draked7acf021999-01-14 17:38:12 +000072 if DEBUG:
Fred Drakef79acbd1999-05-07 21:12:21 +000073 sys.stderr.write(msg + "\n")
74
75def pushing(name, point, depth):
Fred Drake96e4a061999-07-29 22:22:13 +000076 dbgmsg("pushing <%s> at %s" % (name, point))
Fred Draked7acf021999-01-14 17:38:12 +000077
78def popping(name, point, depth):
Fred Drake96e4a061999-07-29 22:22:13 +000079 dbgmsg("popping </%s> at %s" % (name, point))
Fred Draked7acf021999-01-14 17:38:12 +000080
81
Fred Drake96e4a061999-07-29 22:22:13 +000082class _Stack(UserList.UserList):
Fred Drake96e4a061999-07-29 22:22:13 +000083 def append(self, entry):
Fred Drake4fbdf971999-08-02 14:35:25 +000084 if type(entry) is not StringType:
Fred Drake96e4a061999-07-29 22:22:13 +000085 raise LaTeXFormatError("cannot push non-string on stack: "
86 + `entry`)
Fred Drake2262a802001-03-23 16:53:34 +000087 #dbgmsg("%s<%s>" % (" "*len(self.data), entry))
Fred Drake96e4a061999-07-29 22:22:13 +000088 self.data.append(entry)
89
90 def pop(self, index=-1):
91 entry = self.data[index]
92 del self.data[index]
Fred Drake2262a802001-03-23 16:53:34 +000093 #dbgmsg("%s</%s>" % (" "*len(self.data), entry))
Fred Drake96e4a061999-07-29 22:22:13 +000094
95 def __delitem__(self, index):
96 entry = self.data[index]
97 del self.data[index]
Fred Drake2262a802001-03-23 16:53:34 +000098 #dbgmsg("%s</%s>" % (" "*len(self.data), entry))
Fred Drake96e4a061999-07-29 22:22:13 +000099
100
101def new_stack():
102 if DEBUG:
103 return _Stack()
104 return []
105
106
Fred Drake4fbdf971999-08-02 14:35:25 +0000107class Conversion:
108 def __init__(self, ifp, ofp, table):
109 self.write = ofp.write
110 self.ofp = ofp
Fred Drake96c00b01999-05-07 19:59:02 +0000111 self.table = table
Fred Drake96c00b01999-05-07 19:59:02 +0000112 self.line = string.join(map(string.rstrip, ifp.readlines()), "\n")
Fred Drake96c00b01999-05-07 19:59:02 +0000113 self.preamble = 1
Fred Drake96c00b01999-05-07 19:59:02 +0000114
Fred Drake96e4a061999-07-29 22:22:13 +0000115 def convert(self):
116 self.subconvert()
117
Fred Drake96e4a061999-07-29 22:22:13 +0000118 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 Drake691a5a72000-11-22 17:56:43 +0000164 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 Drake96e4a061999-07-29 22:22:13 +0000170 entry = self.get_entry(macroname)
171 if entry.verbatim:
172 # magic case!
173 pos = string.find(line, "\\end{%s}" % macroname)
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 #
188 if entry.outputname:
189 if entry.empty:
190 self.write("e\n")
Fred Drake96e4a061999-07-29 22:22:13 +0000191 #
192 params, optional, empty, environ = self.start_macro(macroname)
193 # rip off the macroname
194 if params:
195 line = line[m.end(1):]
196 elif empty:
197 line = line[m.end(1):]
198 else:
199 line = line[m.end():]
200 opened = 0
201 implied_content = 0
202
203 # handle attribute mappings here:
204 for pentry in params:
205 if pentry.type == "attribute":
206 if pentry.optional:
207 m = _optional_rx.match(line)
Fred Drake4fbdf971999-08-02 14:35:25 +0000208 if m and entry.outputname:
Fred Drake96e4a061999-07-29 22:22:13 +0000209 line = line[m.end():]
210 self.dump_attr(pentry, m.group(1))
Fred Drake4fbdf971999-08-02 14:35:25 +0000211 elif pentry.text and entry.outputname:
Fred Drake96e4a061999-07-29 22:22:13 +0000212 # value supplied by conversion spec:
213 self.dump_attr(pentry, pentry.text)
214 else:
215 m = _parameter_rx.match(line)
216 if not m:
217 raise LaTeXFormatError(
218 "could not extract parameter %s for %s: %s"
219 % (pentry.name, macroname, `line[:100]`))
Fred Drake4fbdf971999-08-02 14:35:25 +0000220 if entry.outputname:
221 self.dump_attr(pentry, m.group(1))
Fred Drake96e4a061999-07-29 22:22:13 +0000222 line = line[m.end():]
223 elif pentry.type == "child":
224 if pentry.optional:
225 m = _optional_rx.match(line)
226 if m:
227 line = line[m.end():]
228 if entry.outputname and not opened:
229 opened = 1
230 self.write("(%s\n" % entry.outputname)
231 stack.append(macroname)
232 stack.append(pentry.name)
233 self.write("(%s\n" % pentry.name)
234 self.write("-%s\n" % encode(m.group(1)))
235 self.write(")%s\n" % pentry.name)
236 stack.pop()
237 else:
238 if entry.outputname and not opened:
239 opened = 1
240 self.write("(%s\n" % entry.outputname)
241 stack.append(entry.name)
242 self.write("(%s\n" % pentry.name)
243 stack.append(pentry.name)
244 self.line = skip_white(line)[1:]
245 line = self.subconvert(
246 "}", len(stack) + depth + 1)[1:]
247 self.write(")%s\n" % stack.pop())
248 elif pentry.type == "content":
249 if pentry.implied:
250 implied_content = 1
251 else:
252 if entry.outputname and not opened:
253 opened = 1
254 self.write("(%s\n" % entry.outputname)
255 stack.append(entry.name)
256 line = skip_white(line)
257 if line[0] != "{":
258 raise LaTeXFormatError(
259 "missing content for " + macroname)
260 self.line = line[1:]
261 line = self.subconvert("}", len(stack) + depth + 1)
262 if line and line[0] == "}":
263 line = line[1:]
Fred Drake4fbdf971999-08-02 14:35:25 +0000264 elif pentry.type == "text" and pentry.text:
265 if entry.outputname and not opened:
266 opened = 1
267 stack.append(entry.name)
268 self.write("(%s\n" % entry.outputname)
Fred Drake2262a802001-03-23 16:53:34 +0000269 #dbgmsg("--- text: %s" % `pentry.text`)
Fred Drake4fbdf971999-08-02 14:35:25 +0000270 self.write("-%s\n" % encode(pentry.text))
Fred Drakef6199ed1999-08-26 17:54:16 +0000271 elif pentry.type == "entityref":
272 self.write("&%s\n" % pentry.name)
Fred Drake96e4a061999-07-29 22:22:13 +0000273 if entry.outputname:
274 if not opened:
275 self.write("(%s\n" % entry.outputname)
276 stack.append(entry.name)
277 if not implied_content:
278 self.write(")%s\n" % entry.outputname)
279 stack.pop()
Fred Drake96e4a061999-07-29 22:22:13 +0000280 continue
281 if line[0] == endchar and not stack:
282 self.line = line[1:]
283 return self.line
284 if line[0] == "}":
285 # end of macro or group
286 macroname = stack[-1]
287 if macroname:
Fred Drake2262a802001-03-23 16:53:34 +0000288 conversion = self.table[macroname]
Fred Drake96e4a061999-07-29 22:22:13 +0000289 if conversion.outputname:
290 # otherwise, it was just a bare group
291 self.write(")%s\n" % conversion.outputname)
292 del stack[-1]
293 line = line[1:]
294 continue
Fred Drake691a5a72000-11-22 17:56:43 +0000295 if line[0] == "~":
296 # don't worry about the "tie" aspect of this command
297 line = line[1:]
298 self.write("- \n")
299 continue
Fred Drake96e4a061999-07-29 22:22:13 +0000300 if line[0] == "{":
301 stack.append("")
302 line = line[1:]
303 continue
304 if line[0] == "\\" and line[1] in ESCAPED_CHARS:
305 self.write("-%s\n" % encode(line[1]))
306 line = line[2:]
307 continue
308 if line[:2] == r"\\":
309 self.write("(BREAK\n)BREAK\n")
310 line = line[2:]
311 continue
Fred Drake691a5a72000-11-22 17:56:43 +0000312 if line[:2] == r"\_":
313 line = "_" + line[2:]
314 continue
315 if line[:2] in (r"\'", r'\"'):
316 # combining characters...
317 self.combining_char(line[1], line[2])
318 line = line[3:]
319 continue
Fred Drake96e4a061999-07-29 22:22:13 +0000320 m = _text_rx.match(line)
321 if m:
322 text = encode(m.group())
323 self.write("-%s\n" % text)
324 line = line[m.end():]
325 continue
326 # special case because of \item[]
327 # XXX can we axe this???
328 if line[0] == "]":
329 self.write("-]\n")
330 line = line[1:]
331 continue
332 # avoid infinite loops
333 extra = ""
334 if len(line) > 100:
335 extra = "..."
336 raise LaTeXFormatError("could not identify markup: %s%s"
337 % (`line[:100]`, extra))
338 while stack:
339 entry = self.get_entry(stack[-1])
340 if entry.closes:
341 self.write(")%s\n-%s\n" % (entry.outputname, encode("\n")))
342 del stack[-1]
343 else:
344 break
345 if stack:
346 raise LaTeXFormatError("elements remain on stack: "
347 + string.join(stack, ", "))
348 # otherwise we just ran out of input here...
349
Fred Drake691a5a72000-11-22 17:56:43 +0000350 # This is a really limited table of combinations, but it will have
351 # to do for now.
352 _combinations = {
353 ("c", "c"): 0x00E7,
354 ("'", "e"): 0x00E9,
355 ('"', "o"): 0x00F6,
356 }
357
358 def combining_char(self, prefix, char):
359 ordinal = self._combinations[(prefix, char)]
360 self.write("-\\%%%d;\n" % ordinal)
361
Fred Drake96e4a061999-07-29 22:22:13 +0000362 def start_macro(self, name):
363 conversion = self.get_entry(name)
364 parameters = conversion.parameters
365 optional = parameters and parameters[0].optional
Fred Drake96e4a061999-07-29 22:22:13 +0000366 return parameters, optional, conversion.empty, conversion.environment
367
368 def get_entry(self, name):
369 entry = self.table.get(name)
370 if entry is None:
Fred Drake2262a802001-03-23 16:53:34 +0000371 dbgmsg("get_entry(%s) failing; building default entry!" % `name`)
Fred Drake96e4a061999-07-29 22:22:13 +0000372 # not defined; build a default entry:
373 entry = TableEntry(name)
374 entry.has_content = 1
375 entry.parameters.append(Parameter("content"))
376 self.table[name] = entry
377 return entry
378
379 def get_env_entry(self, name):
380 entry = self.table.get(name)
381 if entry is None:
382 # not defined; build a default entry:
383 entry = TableEntry(name, 1)
384 entry.has_content = 1
385 entry.parameters.append(Parameter("content"))
386 entry.parameters[-1].implied = 1
387 self.table[name] = entry
388 elif not entry.environment:
389 raise LaTeXFormatError(
390 name + " is defined as a macro; expected environment")
391 return entry
392
393 def dump_attr(self, pentry, value):
394 if not (pentry.name and value):
395 return
396 if _token_rx.match(value):
397 dtype = "TOKEN"
398 else:
399 dtype = "CDATA"
400 self.write("A%s %s %s\n" % (pentry.name, dtype, encode(value)))
401
402
Fred Drakeeac8abe1999-07-29 22:42:27 +0000403def convert(ifp, ofp, table):
404 c = Conversion(ifp, ofp, table)
Fred Drake96e4a061999-07-29 22:22:13 +0000405 try:
406 c.convert()
407 except IOError, (err, msg):
408 if err != errno.EPIPE:
409 raise
410
411
Fred Draked7acf021999-01-14 17:38:12 +0000412def skip_white(line):
Fred Drake96e4a061999-07-29 22:22:13 +0000413 while line and line[0] in " %\n\t\r":
Fred Draked7acf021999-01-14 17:38:12 +0000414 line = string.lstrip(line[1:])
415 return line
416
417
Fred Drake96e4a061999-07-29 22:22:13 +0000418
419class TableEntry:
420 def __init__(self, name, environment=0):
421 self.name = name
422 self.outputname = name
423 self.environment = environment
424 self.empty = not environment
425 self.has_content = 0
426 self.verbatim = 0
427 self.auto_close = 0
428 self.parameters = []
429 self.closes = []
430 self.endcloses = []
431
432class Parameter:
433 def __init__(self, type, name=None, optional=0):
434 self.type = type
435 self.name = name
436 self.optional = optional
437 self.text = ''
438 self.implied = 0
439
440
441class TableParser(XMLParser):
Fred Drake4fbdf971999-08-02 14:35:25 +0000442 def __init__(self, table=None):
443 if table is None:
444 table = {}
445 self.__table = table
Fred Drake96e4a061999-07-29 22:22:13 +0000446 self.__current = None
447 self.__buffer = ''
448 XMLParser.__init__(self)
449
450 def get_table(self):
451 for entry in self.__table.values():
452 if entry.environment and not entry.has_content:
453 p = Parameter("content")
454 p.implied = 1
455 entry.parameters.append(p)
456 entry.has_content = 1
457 return self.__table
458
459 def start_environment(self, attrs):
460 name = attrs["name"]
461 self.__current = TableEntry(name, environment=1)
462 self.__current.verbatim = attrs.get("verbatim") == "yes"
463 if attrs.has_key("outputname"):
464 self.__current.outputname = attrs.get("outputname")
465 self.__current.endcloses = string.split(attrs.get("endcloses", ""))
466 def end_environment(self):
467 self.end_macro()
468
469 def start_macro(self, attrs):
470 name = attrs["name"]
471 self.__current = TableEntry(name)
472 self.__current.closes = string.split(attrs.get("closes", ""))
473 if attrs.has_key("outputname"):
474 self.__current.outputname = attrs.get("outputname")
475 def end_macro(self):
Fred Drake96e4a061999-07-29 22:22:13 +0000476 self.__table[self.__current.name] = self.__current
477 self.__current = None
478
479 def start_attribute(self, attrs):
480 name = attrs.get("name")
481 optional = attrs.get("optional") == "yes"
482 if name:
483 p = Parameter("attribute", name, optional=optional)
484 else:
485 p = Parameter("attribute", optional=optional)
486 self.__current.parameters.append(p)
487 self.__buffer = ''
488 def end_attribute(self):
489 self.__current.parameters[-1].text = self.__buffer
490
Fred Drakef6199ed1999-08-26 17:54:16 +0000491 def start_entityref(self, attrs):
492 name = attrs["name"]
493 p = Parameter("entityref", name)
494 self.__current.parameters.append(p)
495
Fred Drake96e4a061999-07-29 22:22:13 +0000496 def start_child(self, attrs):
497 name = attrs["name"]
498 p = Parameter("child", name, attrs.get("optional") == "yes")
499 self.__current.parameters.append(p)
500 self.__current.empty = 0
501
502 def start_content(self, attrs):
503 p = Parameter("content")
504 p.implied = attrs.get("implied") == "yes"
505 if self.__current.environment:
506 p.implied = 1
507 self.__current.parameters.append(p)
508 self.__current.has_content = 1
509 self.__current.empty = 0
510
511 def start_text(self, attrs):
Fred Drake4fbdf971999-08-02 14:35:25 +0000512 self.__current.empty = 0
Fred Drake96e4a061999-07-29 22:22:13 +0000513 self.__buffer = ''
514 def end_text(self):
515 p = Parameter("text")
516 p.text = self.__buffer
517 self.__current.parameters.append(p)
518
519 def handle_data(self, data):
520 self.__buffer = self.__buffer + data
521
522
Fred Drake4fbdf971999-08-02 14:35:25 +0000523def load_table(fp, table=None):
524 parser = TableParser(table=table)
Fred Drake96e4a061999-07-29 22:22:13 +0000525 parser.feed(fp.read())
526 parser.close()
527 return parser.get_table()
528
529
Fred Drake30a68c71998-11-23 16:59:39 +0000530def main():
Fred Drake96e4a061999-07-29 22:22:13 +0000531 global DEBUG
532 #
Fred Drakeeac8abe1999-07-29 22:42:27 +0000533 opts, args = getopt.getopt(sys.argv[1:], "D", ["debug"])
Fred Drake96e4a061999-07-29 22:22:13 +0000534 for opt, arg in opts:
Fred Drakeeac8abe1999-07-29 22:42:27 +0000535 if opt in ("-D", "--debug"):
Fred Drake96e4a061999-07-29 22:22:13 +0000536 DEBUG = DEBUG + 1
537 if len(args) == 0:
538 ifp = sys.stdin
Fred Drake30a68c71998-11-23 16:59:39 +0000539 ofp = sys.stdout
Fred Drake96e4a061999-07-29 22:22:13 +0000540 elif len(args) == 1:
541 ifp = open(args)
542 ofp = sys.stdout
543 elif len(args) == 2:
544 ifp = open(args[0])
545 ofp = open(args[1], "w")
Fred Drake30a68c71998-11-23 16:59:39 +0000546 else:
547 usage()
548 sys.exit(2)
Fred Drakeeac8abe1999-07-29 22:42:27 +0000549
550 table = load_table(open(os.path.join(sys.path[0], 'conversion.xml')))
551 convert(ifp, ofp, table)
Fred Drake30a68c71998-11-23 16:59:39 +0000552
553
554if __name__ == "__main__":
555 main()