import sys, os, string, getopt, types
import xml.parsers.expat
#------------------------------------------------------------
# A Section holds
# an id of the form n.m.p
# a title
# the level
#
class Section :
def __init__ (self, id, title, level) :
self.id = id
self.title = title
self.level = level
# end def
# Format the index entry for this section
def show_index_entry (self) :
tab = string.rjust ("", 4*self.level)
tab = tab.replace (" ", " ")
print '
'
# end def
#------------------------------------------------------------
# A Book
# It holds the history of sections and the definitions of internal entities
#
class Book :
# Format one level of a section id as %2.2d
def format_id (self, id) :
r = repr (id)
if id < 10 :
r = ' ' + r
return r
# end def
# Format a complete section id, with all individual level ids
# Currently the implementation is somewhat ugly. This is due
# to the strange behaviour of the ideal form (using a for-loop
# over the elements of self.level_ids). Once we can understand
# the reason of this unexpected behaviour we can improve it.
def build_id (self) :
id = ''
n = len (self.level_ids)
if n >= 1 :
id += self.format_id (self.level_ids[0])
if n >= 2 :
id += '.' + self.format_id (self.level_ids[1])
if n >= 3 :
id += '.' + self.format_id (self.level_ids[2])
if n >= 4 :
id += '.' + self.format_id (self.level_ids[3])
id += ''
return id
# end def
# Create a new Section object
# Register the history of sections (with level hierarchy)
def open_section (self, name) :
self.level_ids [self.level] += 1
i = self.level_ids [self.level]
id = self.build_id ()
self.sections.append (Section (id, name, self.level))
self.level += 1
self.level_ids.append (0)
return id
# end def
# Pop one level in the level hierarchy
def close_section (self) :
self.level_ids.pop ()
self.level -= 1
# end def
# Register the definition of an internal entity in the entity dictionary
def entity_decl (self, name, is_parameter, value, base, systemId, publicId, notation) :
if value :
self.entities['&' + name + ';'] = value
# end def
# Generic parser
def generic_parse (self, p, name) :
p.StartElementHandler = self.start_element
p.EndElementHandler = self.end_element
p.CharacterDataHandler = self.char_data
p.ExternalEntityRefHandler = self.external_entity
p.DefaultHandler = self.dummy
p.EntityDeclHandler = self.entity_decl
#file_name = os.path.join (sys.path[0], name)
f = open (name)
p.ParseFile (f)
f.close ()
# end def
# Top level parser
def parse (self, name) :
self.p = xml.parsers.expat.ParserCreate ()
self.generic_parse (self.p, name)
# end def
# Sub-parser for external entities
def external_entity (self, context, base, system_id, public_id):
#print "external entity " + repr(context) + " " + repr(system_id) + ' ' + sys.path[0]
ep = self.p.ExternalEntityParserCreate (context)
self.generic_parse (ep, system_id)
# end def
# Format a tabulation according to the stored tabulation level
def tab (self) :
return (string.rjust ("", self.tabulation))
# end def
# Flush the internal line buffer and display it using the tabulation
def purge (self) :
if self.line != '':
if self.in_code == 0 :
print self.tab () + self.line
self.line = ''
# end def
#----------------------------------------------------------
#
# All XML element handlers
#
#----------------------------------------------------------
# any element : simply reproduce the element with its attributes
# (ie assume it is a true HTML element)
def default_start (self, name, attrs) :
self.line += '<' + name
if len (attrs) > 0 :
i = len (attrs)
for k, v in attrs.items() :
self.line += ' ' + k + '="' + v + '"'
i -= 1
if i <= 0 : break
self.line += '>'
self.purge ()
self.tabulation += 2
# end def
def default_end (self, name) :
self.tabulation -= 2
self.line += '' + name + '>'
self.purge ()
# end def
#
def book_start (self, attrs) :
self.name = attrs['name']
self.title = attrs['title']
self.version = attrs['version']
self.author = attrs['author']
print ' '
print ''
print '' + self.name + ''
print ''
print ''
print '