mirror of
https://github.com/python/cpython.git
synced 2025-08-03 00:23:06 +00:00
Mostly minor changes.
Removed some obsolete commented-out code. Merged BaseConversion into Conversion since there's only one conversion process now. Remove push/pop of output stream; it's no longer needed.
This commit is contained in:
parent
d158b85896
commit
4fbdf9779c
1 changed files with 23 additions and 52 deletions
|
@ -80,10 +80,8 @@ def popping(name, point, depth):
|
||||||
|
|
||||||
|
|
||||||
class _Stack(UserList.UserList):
|
class _Stack(UserList.UserList):
|
||||||
StringType = type('')
|
|
||||||
|
|
||||||
def append(self, entry):
|
def append(self, entry):
|
||||||
if type(entry) is not self.StringType:
|
if type(entry) is not StringType:
|
||||||
raise LaTeXFormatError("cannot push non-string on stack: "
|
raise LaTeXFormatError("cannot push non-string on stack: "
|
||||||
+ `entry`)
|
+ `entry`)
|
||||||
sys.stderr.write("%s<%s>\n" % (" "*len(self.data), entry))
|
sys.stderr.write("%s<%s>\n" % (" "*len(self.data), entry))
|
||||||
|
@ -106,25 +104,13 @@ def new_stack():
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
class BaseConversion:
|
class Conversion:
|
||||||
def __init__(self, ifp, ofp, table={}, discards=(), autoclosing=()):
|
def __init__(self, ifp, ofp, table):
|
||||||
self.ofp_stack = [ofp]
|
self.write = ofp.write
|
||||||
self.pop_output()
|
self.ofp = ofp
|
||||||
self.table = table
|
self.table = table
|
||||||
self.discards = discards
|
|
||||||
self.autoclosing = autoclosing
|
|
||||||
self.line = string.join(map(string.rstrip, ifp.readlines()), "\n")
|
self.line = string.join(map(string.rstrip, ifp.readlines()), "\n")
|
||||||
self.preamble = 1
|
self.preamble = 1
|
||||||
self.stack = new_stack()
|
|
||||||
|
|
||||||
def push_output(self, ofp):
|
|
||||||
self.ofp_stack.append(self.ofp)
|
|
||||||
self.ofp = ofp
|
|
||||||
self.write = ofp.write
|
|
||||||
|
|
||||||
def pop_output(self):
|
|
||||||
self.ofp = self.ofp_stack.pop()
|
|
||||||
self.write = self.ofp.write
|
|
||||||
|
|
||||||
def err_write(self, msg):
|
def err_write(self, msg):
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
|
@ -133,12 +119,6 @@ class BaseConversion:
|
||||||
def convert(self):
|
def convert(self):
|
||||||
self.subconvert()
|
self.subconvert()
|
||||||
|
|
||||||
|
|
||||||
class Conversion(BaseConversion):
|
|
||||||
def __init__(self, ifp, ofp, table={}):
|
|
||||||
BaseConversion.__init__(self, ifp, ofp, table)
|
|
||||||
self.discards = []
|
|
||||||
|
|
||||||
def subconvert(self, endchar=None, depth=0):
|
def subconvert(self, endchar=None, depth=0):
|
||||||
#
|
#
|
||||||
# Parses content, including sub-structures, until the character
|
# Parses content, including sub-structures, until the character
|
||||||
|
@ -206,9 +186,6 @@ class Conversion(BaseConversion):
|
||||||
if entry.outputname:
|
if entry.outputname:
|
||||||
if entry.empty:
|
if entry.empty:
|
||||||
self.write("e\n")
|
self.write("e\n")
|
||||||
self.push_output(self.ofp)
|
|
||||||
else:
|
|
||||||
self.push_output(StringIO.StringIO())
|
|
||||||
#
|
#
|
||||||
params, optional, empty, environ = self.start_macro(macroname)
|
params, optional, empty, environ = self.start_macro(macroname)
|
||||||
# rip off the macroname
|
# rip off the macroname
|
||||||
|
@ -226,10 +203,10 @@ class Conversion(BaseConversion):
|
||||||
if pentry.type == "attribute":
|
if pentry.type == "attribute":
|
||||||
if pentry.optional:
|
if pentry.optional:
|
||||||
m = _optional_rx.match(line)
|
m = _optional_rx.match(line)
|
||||||
if m:
|
if m and entry.outputname:
|
||||||
line = line[m.end():]
|
line = line[m.end():]
|
||||||
self.dump_attr(pentry, m.group(1))
|
self.dump_attr(pentry, m.group(1))
|
||||||
elif pentry.text:
|
elif pentry.text and entry.outputname:
|
||||||
# value supplied by conversion spec:
|
# value supplied by conversion spec:
|
||||||
self.dump_attr(pentry, pentry.text)
|
self.dump_attr(pentry, pentry.text)
|
||||||
else:
|
else:
|
||||||
|
@ -238,9 +215,8 @@ class Conversion(BaseConversion):
|
||||||
raise LaTeXFormatError(
|
raise LaTeXFormatError(
|
||||||
"could not extract parameter %s for %s: %s"
|
"could not extract parameter %s for %s: %s"
|
||||||
% (pentry.name, macroname, `line[:100]`))
|
% (pentry.name, macroname, `line[:100]`))
|
||||||
self.dump_attr(pentry, m.group(1))
|
if entry.outputname:
|
||||||
## if entry.name == "label":
|
self.dump_attr(pentry, m.group(1))
|
||||||
## sys.stderr.write("[%s]" % m.group(1))
|
|
||||||
line = line[m.end():]
|
line = line[m.end():]
|
||||||
elif pentry.type == "child":
|
elif pentry.type == "child":
|
||||||
if pentry.optional:
|
if pentry.optional:
|
||||||
|
@ -283,13 +259,13 @@ class Conversion(BaseConversion):
|
||||||
line = self.subconvert("}", len(stack) + depth + 1)
|
line = self.subconvert("}", len(stack) + depth + 1)
|
||||||
if line and line[0] == "}":
|
if line and line[0] == "}":
|
||||||
line = line[1:]
|
line = line[1:]
|
||||||
elif pentry.type == "text":
|
elif pentry.type == "text" and pentry.text:
|
||||||
if pentry.text:
|
if entry.outputname and not opened:
|
||||||
if entry.outputname and not opened:
|
opened = 1
|
||||||
opened = 1
|
stack.append(entry.name)
|
||||||
stack.append(entry.name)
|
self.write("(%s\n" % entry.outputname)
|
||||||
self.write("(%s\n" % entry.outputname)
|
self.err_write("--- text: %s\n" % `pentry.text`)
|
||||||
self.write("-%s\n" % encode(pentry.text))
|
self.write("-%s\n" % encode(pentry.text))
|
||||||
if entry.outputname:
|
if entry.outputname:
|
||||||
if not opened:
|
if not opened:
|
||||||
self.write("(%s\n" % entry.outputname)
|
self.write("(%s\n" % entry.outputname)
|
||||||
|
@ -297,7 +273,6 @@ class Conversion(BaseConversion):
|
||||||
if not implied_content:
|
if not implied_content:
|
||||||
self.write(")%s\n" % entry.outputname)
|
self.write(")%s\n" % entry.outputname)
|
||||||
stack.pop()
|
stack.pop()
|
||||||
self.pop_output()
|
|
||||||
continue
|
continue
|
||||||
if line[0] == endchar and not stack:
|
if line[0] == endchar and not stack:
|
||||||
self.line = line[1:]
|
self.line = line[1:]
|
||||||
|
@ -359,11 +334,6 @@ class Conversion(BaseConversion):
|
||||||
conversion = self.get_entry(name)
|
conversion = self.get_entry(name)
|
||||||
parameters = conversion.parameters
|
parameters = conversion.parameters
|
||||||
optional = parameters and parameters[0].optional
|
optional = parameters and parameters[0].optional
|
||||||
## empty = not len(parameters)
|
|
||||||
## if empty:
|
|
||||||
## self.write("e\n")
|
|
||||||
## elif conversion.empty:
|
|
||||||
## empty = 1
|
|
||||||
return parameters, optional, conversion.empty, conversion.environment
|
return parameters, optional, conversion.empty, conversion.environment
|
||||||
|
|
||||||
def get_entry(self, name):
|
def get_entry(self, name):
|
||||||
|
@ -441,8 +411,10 @@ class Parameter:
|
||||||
|
|
||||||
|
|
||||||
class TableParser(XMLParser):
|
class TableParser(XMLParser):
|
||||||
def __init__(self):
|
def __init__(self, table=None):
|
||||||
self.__table = {}
|
if table is None:
|
||||||
|
table = {}
|
||||||
|
self.__table = table
|
||||||
self.__current = None
|
self.__current = None
|
||||||
self.__buffer = ''
|
self.__buffer = ''
|
||||||
XMLParser.__init__(self)
|
XMLParser.__init__(self)
|
||||||
|
@ -473,8 +445,6 @@ class TableParser(XMLParser):
|
||||||
if attrs.has_key("outputname"):
|
if attrs.has_key("outputname"):
|
||||||
self.__current.outputname = attrs.get("outputname")
|
self.__current.outputname = attrs.get("outputname")
|
||||||
def end_macro(self):
|
def end_macro(self):
|
||||||
## if self.__current.parameters and not self.__current.outputname:
|
|
||||||
## raise ValueError, "markup with parameters must have an output name"
|
|
||||||
self.__table[self.__current.name] = self.__current
|
self.__table[self.__current.name] = self.__current
|
||||||
self.__current = None
|
self.__current = None
|
||||||
|
|
||||||
|
@ -506,6 +476,7 @@ class TableParser(XMLParser):
|
||||||
self.__current.empty = 0
|
self.__current.empty = 0
|
||||||
|
|
||||||
def start_text(self, attrs):
|
def start_text(self, attrs):
|
||||||
|
self.__current.empty = 0
|
||||||
self.__buffer = ''
|
self.__buffer = ''
|
||||||
def end_text(self):
|
def end_text(self):
|
||||||
p = Parameter("text")
|
p = Parameter("text")
|
||||||
|
@ -516,8 +487,8 @@ class TableParser(XMLParser):
|
||||||
self.__buffer = self.__buffer + data
|
self.__buffer = self.__buffer + data
|
||||||
|
|
||||||
|
|
||||||
def load_table(fp):
|
def load_table(fp, table=None):
|
||||||
parser = TableParser()
|
parser = TableParser(table=table)
|
||||||
parser.feed(fp.read())
|
parser.feed(fp.read())
|
||||||
parser.close()
|
parser.close()
|
||||||
return parser.get_table()
|
return parser.get_table()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue