mirror of
https://github.com/python/cpython.git
synced 2025-08-03 00:23:06 +00:00
Adapted unweave to Matthias' fixes. It's still off-by-one-space in some cases.
This commit is contained in:
parent
5401996638
commit
0eea16633a
1 changed files with 53 additions and 15 deletions
|
@ -11,8 +11,17 @@ BEGINDEFINITION=re.compile("^<<(?P<name>.*)>>=\s*")
|
||||||
USEDEFINITION=re.compile("^(?P<pre>.*)<<(?P<name>.*)>>(?P<post>[^=].*)")
|
USEDEFINITION=re.compile("^(?P<pre>.*)<<(?P<name>.*)>>(?P<post>[^=].*)")
|
||||||
ENDDEFINITION=re.compile("^@")
|
ENDDEFINITION=re.compile("^@")
|
||||||
|
|
||||||
|
DEFAULT_CONFIG="""
|
||||||
|
config = [
|
||||||
|
("^.*\.cp$", ":unweave-src"),
|
||||||
|
("^.*\.h$", ":unweave-include"),
|
||||||
|
]
|
||||||
|
genlinedirectives = 0
|
||||||
|
gencomments = 1
|
||||||
|
"""
|
||||||
|
|
||||||
class Processor:
|
class Processor:
|
||||||
def __init__(self, filename):
|
def __init__(self, filename, config={}):
|
||||||
self.items = {}
|
self.items = {}
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
self.fp = open(filename)
|
self.fp = open(filename)
|
||||||
|
@ -20,6 +29,15 @@ class Processor:
|
||||||
self.resolving = {}
|
self.resolving = {}
|
||||||
self.resolved = {}
|
self.resolved = {}
|
||||||
self.pushback = None
|
self.pushback = None
|
||||||
|
# Options
|
||||||
|
if config.has_key("genlinedirectives"):
|
||||||
|
self.genlinedirectives = config["genlinedirectives"]
|
||||||
|
else:
|
||||||
|
self.genlinedirectives = 1
|
||||||
|
if config.has_key("gencomments"):
|
||||||
|
self.gencomments = config["gencomments"]
|
||||||
|
else:
|
||||||
|
self.gencomments = 0
|
||||||
|
|
||||||
def _readline(self):
|
def _readline(self):
|
||||||
"""Read a line. Allow for pushback"""
|
"""Read a line. Allow for pushback"""
|
||||||
|
@ -50,10 +68,9 @@ class Processor:
|
||||||
if mo:
|
if mo:
|
||||||
pre = mo.group('pre')
|
pre = mo.group('pre')
|
||||||
if pre:
|
if pre:
|
||||||
rv.append((lineno, pre+'\n'))
|
## rv.append((lineno, pre+'\n'))
|
||||||
|
rv.append((lineno, pre))
|
||||||
rv.append((lineno, line))
|
rv.append((lineno, line))
|
||||||
# For simplicity we add #line directives now, if
|
|
||||||
# needed.
|
|
||||||
if mo:
|
if mo:
|
||||||
post = mo.group('post')
|
post = mo.group('post')
|
||||||
if post and post != '\n':
|
if post and post != '\n':
|
||||||
|
@ -69,6 +86,7 @@ class Processor:
|
||||||
|
|
||||||
def read(self):
|
def read(self):
|
||||||
"""Read the source file and store all definitions"""
|
"""Read the source file and store all definitions"""
|
||||||
|
savedcomment = []
|
||||||
while 1:
|
while 1:
|
||||||
lineno, line = self._readline()
|
lineno, line = self._readline()
|
||||||
if not line: break
|
if not line: break
|
||||||
|
@ -76,10 +94,33 @@ class Processor:
|
||||||
if mo:
|
if mo:
|
||||||
name = mo.group('name')
|
name = mo.group('name')
|
||||||
value = self._readitem()
|
value = self._readitem()
|
||||||
|
if self.gencomments:
|
||||||
|
defline = [(lineno, '// <%s>=\n'%name)]
|
||||||
|
if savedcomment:
|
||||||
|
savedcomment = savedcomment + [(lineno, '//\n')] + defline
|
||||||
|
else:
|
||||||
|
savedcomment = defline
|
||||||
|
savedcomment = self._extendlines(savedcomment)
|
||||||
|
value = savedcomment + value
|
||||||
|
savedcomment = []
|
||||||
self._define(name, value)
|
self._define(name, value)
|
||||||
else:
|
else:
|
||||||
pass # We could output the TeX code but we don't bother.
|
if self.gencomments:
|
||||||
|
# It seems initial blank lines are ignored:-(
|
||||||
|
if savedcomment or line.strip():
|
||||||
|
savedcomment.append((lineno, '// '+line))
|
||||||
|
|
||||||
|
def _extendlines(self, comment):
|
||||||
|
# This routine mimicks some artefact of Matthias' code.
|
||||||
|
rv = []
|
||||||
|
for lineno, line in comment:
|
||||||
|
line = line[:-1]
|
||||||
|
if len(line) < 75:
|
||||||
|
line = line + (75-len(line))*' '
|
||||||
|
line = line + '\n'
|
||||||
|
rv.append((lineno, line))
|
||||||
|
return rv
|
||||||
|
|
||||||
def resolve(self):
|
def resolve(self):
|
||||||
"""Resolve all references"""
|
"""Resolve all references"""
|
||||||
for name in self.items.keys():
|
for name in self.items.keys():
|
||||||
|
@ -134,7 +175,7 @@ class Processor:
|
||||||
rv = []
|
rv = []
|
||||||
for lineno, line in data:
|
for lineno, line in data:
|
||||||
curlineno = curlineno + 1
|
curlineno = curlineno + 1
|
||||||
if line and line != '\n' and lineno != curlineno:
|
if self.genlinedirectives and line and line != '\n' and lineno != curlineno:
|
||||||
rv.append(self._linedirective(lineno))
|
rv.append(self._linedirective(lineno))
|
||||||
curlineno = lineno
|
curlineno = lineno
|
||||||
rv.append(line)
|
rv.append(line)
|
||||||
|
@ -150,27 +191,24 @@ class Processor:
|
||||||
fp = open(pathname, "w").writelines(data)
|
fp = open(pathname, "w").writelines(data)
|
||||||
|
|
||||||
def process(file, config):
|
def process(file, config):
|
||||||
pr = Processor(file)
|
pr = Processor(file, config)
|
||||||
pr.read()
|
pr.read()
|
||||||
pr.resolve()
|
pr.resolve()
|
||||||
for pattern, folder in config:
|
for pattern, folder in config['config']:
|
||||||
pr.save(folder, pattern)
|
pr.save(folder, pattern)
|
||||||
|
|
||||||
def readconfig():
|
def readconfig():
|
||||||
"""Read a configuration file, if it doesn't exist create it."""
|
"""Read a configuration file, if it doesn't exist create it."""
|
||||||
configname = sys.argv[0] + '.config'
|
configname = sys.argv[0] + '.config'
|
||||||
if not os.path.exists(configname):
|
if not os.path.exists(configname):
|
||||||
confstr = """config = [
|
confstr = DEFAULT_CONFIG
|
||||||
("^.*\.cp$", ":unweave-src"),
|
|
||||||
("^.*\.h$", ":unweave-include"),
|
|
||||||
]"""
|
|
||||||
open(configname, "w").write(confstr)
|
open(configname, "w").write(confstr)
|
||||||
print "Created config file", configname
|
print "Created config file", configname
|
||||||
## print "Please check and adapt (if needed)"
|
## print "Please check and adapt (if needed)"
|
||||||
## sys.exit(0)
|
## sys.exit(0)
|
||||||
namespace = {}
|
namespace = {}
|
||||||
execfile(configname, namespace)
|
execfile(configname, namespace)
|
||||||
return namespace['config']
|
return namespace
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
config = readconfig()
|
config = readconfig()
|
||||||
|
@ -185,7 +223,7 @@ def main():
|
||||||
fss, ok = macfs.PromptGetFile("Select .nw source file", "TEXT")
|
fss, ok = macfs.PromptGetFile("Select .nw source file", "TEXT")
|
||||||
if not ok:
|
if not ok:
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
process(fss.as_pathname())
|
process(fss.as_pathname(), config)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue