#----------------------------------------------------------- # Copyright Christian Arnault LAL-Orsay CNRS # arnault@lal.in2p3.fr # See the complete license in cmt_license.txt "http://www.cecill.info". #----------------------------------------------------------- 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 '' print '' + self.id + '' print '' + tab + '' + self.title + '' 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 += '' 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 '' print '' ##print '' print '' print '

' + self.name + '
' print '
' + self.title + '

' print '

Version ' + self.version + '
' print '
' + self.author + '
' print '
' + attrs['email'] + '

' self.line += '

' self.line += 'General index' self.line += '

' self.purge () # end def def book_end (self) : print '

Contents

' print '
' print '' for k in range (len (self.sections)) : self.sections[k].show_index_entry () print '' print '' for k in range (len (self.images)) : print '' print '' print '' print '' print '
 

Images

' + repr(k+1) + '' + self.images[k] + '
' print '
' print '
' print '
' print '' + self.author + '' print '
' print '' print '' # end def #
def section_start (self, attrs) : title = attrs['title'] title = title.replace ('>', '>') title = title.replace ('<', '<') id = self.open_section (title) h_level = repr(self.level+1) self.line += '
' self.line += '' self.line += '' + id + ' - ' + title self.line += '' self.purge () self.line = '
' self.purge () # end def def section_end (self) : self.purge () self.line = '
' self.purge () self.close_section () # end def # def code_start (self, attrs) : self.purge () self.in_code += 1 self.line = '
'
  # end def

  def code_end (self) :
    self.in_code -= 1
    print self.line + '
' self.line = '' # end def # def cmtcode_start (self, attrs) : self.purge () self.in_code += 1 self.line = '
'
  # end def

  def cmtcode_end (self) :
    self.in_code -= 1
    print self.line + '
' self.line = '' # end def # def syntax_start (self, attrs) : print '
' print '' print '' if 'rule-width' in attrs : print '' else : print '' if 'name' in attrs : self.ruleref_prefix = 'kw' + attrs['name'] + '-' else : self.ruleref_prefix = 'kw-' print '' print '' print '' # end def def syntax_end (self) : print '
' print '
' # end def # def rule_start (self, attrs) : self.rule_name = attrs['name'] self.rule_started = 0 # end def def rule_end (self) : self.rule_name = '' self.rule_started = 0 # end def # def alt_start (self, attrs) : print '' if self.rule_started == 0 : self.rule_started = 1 print '' + self.rule_name + '' print ':' print '' else : print '' print '|' print '' # end def def alt_end (self) : print '' print '' # end def # def continuation_start (self, attrs) : print '' print '' print '' print '   ' # end def def continuation_end (self) : print '' print '' # end def # def kwd_start (self, attrs) : print '' if 'name' in attrs : name = attrs['name'] else : name = self.rule_name if 'value' in attrs : name += '=' value = '' value += attrs['value'] value += '' else : value = '' print name + value # end def def kwd_end (self) : print '' # end def # def term_start (self, attrs) : print '' print attrs['name'] # end def def term_end (self) : print '' # end def # def ruleref_start (self, attrs) : print '' print '' + attrs['name'] + '' # end def def ruleref_end (self) : print '' # end def #