SGML to Python memory tree
pinard at iro.umontreal.ca
Wed May 17 14:03:21 CEST 2000
Hi, gang. Once started on sharing little pieces of code :-).
For the Translation Project, I have some Python code that reads `nsgmls'
output into a memory tree. It does not process attributes, as I did
not have any in my little application. This code is surprisingly short,
given what it does. (It had to work for Python 1.5.1, that's why it works
around the missing `LIST.pop()').
stack = 
current = 
# Avoid docbk30, which raises some unanalysed interference.
for line in os.popen('SGML_CATALOG_FILES= nsgmls %s' % name).readlines():
if line == '(':
current = [string.lower(line[1:-1])]
if line == ')':
element = tuple(current)
current = stack[-1]
if line == '-':
line = line[1:-1]
line = string.replace(line, '\\n', '\n')
line = string.replace(line, '\\011', '\t')
line = string.rstrip(line)
if line == 'C':
sys.stderr.write(_("SGML in `%s' is not conformant.\n") % name)
François Pinard http://www.iro.umontreal.ca/~pinard
More information about the Python-list