source: CMT/v1r18p20041201/doc/gendoc.py @ 1

Last change on this file since 1 was 1, checked in by arnault, 19 years ago

Import all tags

File size: 17.6 KB
Line 
1#-----------------------------------------------------------
2# Copyright Christian Arnault LAL-Orsay CNRS
3# arnault@lal.in2p3.fr
4# See the complete license in cmt_license.txt "http://www.cecill.info".
5#-----------------------------------------------------------
6
7import sys, os, string, getopt, types
8import xml.parsers.expat
9from datetime import date
10
11#------------------------------------------------------------
12# A Section holds
13#   an  id     of the form n.m.p
14#   a   title
15#   the level
16#
17class Section :
18  def __init__ (self, id, title, level) :
19    self.id = id
20    self.title = title
21    self.level = level
22  # end def
23
24  # Format the index entry for this section
25  def show_index_entry (self) :
26    tab = string.rjust ("", 4*self.level)
27    tab = tab.replace (" ", " ")
28    print '<tr>'
29    print '<td width="100">' + self.id + '</td>'
30    print '<td>' + tab + '<a href="#' + self.title + '">' + self.title + '</a></td>'
31    print '</tr>'
32  # end def
33
34#------------------------------------------------------------
35# A Book
36#  It holds the history of sections and the definitions of internal entities
37#
38class Book :
39  # Format one level of a section id as %2.2d
40  def format_id (self, id) :
41    r = repr (id)
42    if id < 10 :
43      r = '&nbsp;' + r
44    return r
45  # end def
46
47  # Format a complete section id, with all individual level ids
48  #  Currently the implementation is somewhat ugly. This is due
49  #  to the strange behaviour of the ideal form (using a for-loop
50  #  over the elements of self.level_ids). Once we can understand
51  #  the reason of this unexpected behaviour we can improve it.
52  def build_id (self) :
53    id = '<tt>'
54    n = len (self.level_ids)
55    if n >= 1 :
56      id += self.format_id (self.level_ids[0])
57    if n >= 2 :
58      id += '.' + self.format_id (self.level_ids[1])
59    if n >= 3 :
60      id += '.' + self.format_id (self.level_ids[2])
61    if n >= 4 :
62      id += '.' + self.format_id (self.level_ids[3])
63    id += '</tt>'
64    return id
65  # end def
66
67  # Create a new Section object
68  #  Register the history of sections (with level hierarchy)
69  def open_section (self, name) :
70    self.level_ids [self.level] += 1
71    i = self.level_ids [self.level]
72    id = self.build_id ()
73    self.sections.append (Section (id, name, self.level))
74    self.level += 1
75    self.level_ids.append (0)
76    return id
77  # end def
78
79  # Pop one level in the level hierarchy
80  def close_section (self) :
81    self.level_ids.pop ()
82    self.level -= 1
83  # end def
84
85  # Register the definition of an internal entity in the entity dictionary
86  def entity_decl (self, name, is_parameter, value, base, systemId, publicId, notation) :
87    if value :
88      self.entities['&' + name + ';'] = value
89  # end def
90
91  # Generic parser
92  def generic_parse (self, p, name) :
93    p.StartElementHandler = self.start_element
94    p.EndElementHandler = self.end_element
95    p.CharacterDataHandler = self.char_data
96    p.ExternalEntityRefHandler = self.external_entity
97    p.DefaultHandler = self.dummy
98    p.EntityDeclHandler = self.entity_decl
99    #file_name = os.path.join (sys.path[0], name)
100    f = open (name)
101    p.ParseFile (f)
102    f.close ()
103  # end def
104
105  # Top level parser
106  def parse (self, name) :
107    self.p = xml.parsers.expat.ParserCreate ()
108    self.generic_parse (self.p, name)
109  # end def
110
111  # Sub-parser for external entities
112  def external_entity (self, context, base, system_id, public_id):
113    #print "external entity " + repr(context) + " " + repr(system_id) + ' ' + sys.path[0]
114    ep = self.p.ExternalEntityParserCreate (context)
115    self.generic_parse (ep, system_id)
116  # end def
117
118  # Format a tabulation according to the stored tabulation level
119  def tab (self) :
120    return (string.rjust ("", self.tabulation))
121  # end def
122
123  # Flush the internal line buffer and display it using the tabulation
124  def purge (self) :
125    if self.line != '':
126      if self.in_code == 0 :
127        print self.tab () + self.line
128        self.line = ''
129  # end def
130
131
132  #----------------------------------------------------------
133  #
134  #  All XML element handlers
135  #
136  #----------------------------------------------------------
137
138
139  # any element : simply reproduce the element with its attributes
140  #  (ie assume it is a true HTML element)
141  def default_start (self, name, attrs) :
142    self.line += '<' + name
143    if len (attrs) > 0 :
144      i = len (attrs)
145      for k, v in attrs.items() :
146        self.line += ' ' + k + '="' + v + '"'
147        i -= 1
148        if i <= 0 : break
149    self.line += '>'
150    #####  self.purge ()
151    self.tabulation += 2
152  # end def
153
154
155  def default_end (self, name) :
156    self.tabulation -= 2
157    self.line += '</' + name + '>'
158    self.purge ()
159  # end def
160
161
162  # <book>
163  def book_start (self, attrs) :
164    self.name = attrs['name']
165    self.title = attrs['title']
166    self.version = attrs['version']
167    self.author = attrs['author']
168    print '<!doctype html public "-//w3c//dtd html 4.0 transitional//en"> '
169    print '<html>'
170    print '<title>' + self.name + '</title>'
171    print '<head>'
172    print ' <style type="text/css">'
173    print '   tt {color:#006600; font-weight: normal; %background-color: #eeffee}'
174    print '   tt.cmt {color:#00AA00; font-weight: normal; %background-color: #eeeeee}'
175    print '   pre {color:#FF0000; background-color: #eeeeee; border-style: solid; border-width: 1; border-color: black; padding: 4}'
176    print '   pre.cmt {color:#00AA00; background-color: #eeeeee; border-style: solid; border-width: 1; border-color: black; padding: 4}'
177    print '   h2 {color:#0000FF}'
178    print '   h3 {color:#00AA00}'
179    print '   h4 {color:#997700}'
180    print '   a {color:#0000FF; background-color: LightGoldenRodYellow; text-decoration: none}'
181    print '   b {font-family: courier; color:#006600; font-weight: normal; %background-color: #eeffee}'
182    print '   td.rule {padding-top: 10}'
183    print ' </style>'
184    print '</head>'
185    print '<body bgcolor="#ffffff" link="#550088" alink="#007777" alink="#007777">'
186    ##print '<font face="Arial, Helvetica" color="#000000">'
187    print '<font face="Arial, Helvetica, Comic Sans MS, Times" color="#000000">'
188    print '<h1><center>' + self.name + '</center>'
189    print '<center>' + self.title + '</center></h1>'
190    print '<h2><center>Version ' + self.version + '</center>'
191    print '<center>' + self.author + '</center>'
192    print '<center><tt>' + attrs['email'] + '</tt></center></h2>'
193    self.line += '<center><i>Document revision date : ' + date.today().isoformat() + '</i></center>'
194    self.line += '<hr><h2>'
195    self.line += '<a href="#index">General index</a>'
196    self.line += '</h2>'
197    self.purge ()
198  # end def
199
200  def book_end (self) :
201    print '<hr><h1><A NAME="index"></A>Contents</h1>'
202    print '<blockquote>'
203    print '<table cols="2">'
204    for k in range (len (self.sections)) :
205      self.sections[k].show_index_entry ()
206    print '<tr><td colspan="2">&nbsp;</td></tr>'
207    print '<tr><td colspan="2"><h2>Images</h2></td></tr>'
208    for k in range (len (self.images)) :
209      print '<tr>'
210      print '<td width="100"><tt>' + repr(k+1) + '</tt></td>'
211      print '<td><a href="#' + self.images[k] + '">' + self.images[k] + '</a></td>'
212      print '</tr>'
213    print '</table>'
214    print '</blockquote>'
215    print '</font>'
216    print '<address>'
217    print '<i>' + self.author + '</i>'
218    print '</address>'
219    print '</body>'
220    print '</html>'
221  # end def
222
223
224  # <section>
225  def section_start (self, attrs) :
226    title = attrs['title']
227    title = title.replace ('>', '&gt;')
228    title = title.replace ('<', '&lt;')
229    id = self.open_section (title)
230    h_level = repr(self.level+1)
231    self.line += '<hr><h' + h_level + '>'
232    self.line += '<a name="' + title + '"></a>'
233    self.line += '<a href="#index">' + id + '</a> - ' + title
234    self.line += '</h' + h_level + '>'
235    self.purge ()
236    self.line = '<blockquote>'
237    self.purge ()
238  # end def
239
240  def section_end (self) :
241    self.purge ()
242    self.line = '</blockquote>'
243    self.purge ()
244    self.close_section ()
245  # end def
246
247
248  # <code>
249  def code_start (self, attrs) :
250    self.purge ()
251    self.in_code += 1
252    self.line = '<pre>'
253  # end def
254
255  def code_end (self) :
256    self.in_code -= 1
257    print self.line + '</pre>'
258    self.line = ''
259  # end def
260
261
262  # <cmtcode>
263  def cmtcode_start (self, attrs) :
264    self.purge ()
265    self.in_code += 1
266    self.line = '<pre class="cmt">'
267  # end def
268
269  def cmtcode_end (self) :
270    self.in_code -= 1
271    print self.line + '</pre>'
272    self.line = ''
273  # end def
274
275
276  # <syntax>
277  def syntax_start (self, attrs) :
278    print '<center>'
279    print '<table cols="3">'
280    print '<tr>'
281    if 'rule-width' in attrs :
282      print '<td width="' + attrs['rule-width'] + '"></td>'
283    else :
284      print '<td></td>'
285    if 'name' in attrs :
286      self.ruleref_prefix = 'kw' + attrs['name'] + '-'
287    else :
288      self.ruleref_prefix = 'kw-'
289    print '<td width="10"></td>'
290    print '<td></td>'
291    print '</tr>' 
292  # end def
293
294  def syntax_end (self) :
295    print '</table>'
296    print '</center>'
297  # end def
298
299
300  # <rule>
301  def rule_start (self, attrs) :
302    self.rule_name = attrs['name']
303    self.rule_started = 0
304  # end def
305
306  def rule_end (self) :
307    self.rule_name = ''
308    self.rule_started = 0
309  # end def
310
311
312  # <alt>
313  def alt_start (self, attrs) :
314    print '<tr>'
315    if self.rule_started == 0 :
316      self.rule_started = 1
317      print '<td class="rule"><font face="courier new, courier" COLOR="#770000"><i><a name="' + self.ruleref_prefix + self.rule_name + '"></a>' + self.rule_name + '</i></font></td>'
318      print '<td class="rule">:</td>'
319      print '<td class="rule">'
320    else :
321      print '<td></td>'
322      print '<td>|</td>'
323      print '<td>'
324  # end def
325     
326  def alt_end (self) :
327    print '</td>'
328    print '</tr>'
329  # end def
330
331
332  # <continuation>
333  def continuation_start (self, attrs) :
334    print '<tr>'
335    print '<td></td>'
336    print '<td></td>'
337    print '<td>&nbsp;&nbsp;&nbsp;'
338  # end def
339     
340  def continuation_end (self) :
341    print '</td>'
342    print '</tr>'
343  # end def
344
345
346  # <kwd>
347  def kwd_start (self, attrs) :
348    print '<font face="courier new, courier" COLOR="#FF0000">'
349    if 'name' in attrs :
350      name = attrs['name']
351    else :
352      name = self.rule_name
353    if 'value' in attrs :
354      name += '='
355      value = '<font face="courier new, courier" COLOR="#770000"><i>'
356      value += attrs['value']
357      value += '</i></font>'
358    else :
359      value = ''
360    print name + value
361  # end def
362
363  def kwd_end (self) :
364    print '</font>'
365  # end def
366
367
368
369  # <term>
370  def term_start (self, attrs) :
371    print '<font face="courier new, courier" COLOR="#770000"><i>'
372    print attrs['name']
373  # end def
374
375  def term_end (self) :
376    print '</i></font>'
377  # end def
378
379
380
381  # <ruleref>
382  def ruleref_start (self, attrs) :
383    print '<font face="courier new, courier" COLOR="#770000"><i>'
384    print '<a href="#' + self.ruleref_prefix + attrs['name'] + '">' + attrs['name'] + '</a>'
385  # end def
386
387  def ruleref_end (self) :
388    print '</i></font>'
389  # end def
390
391
392  # <option>
393  def option_start (self, attrs) :
394    print '[&nbsp;'
395  # end def
396
397  def option_end (self) :
398    print '&nbsp;]&nbsp;'
399  # end def
400
401
402  # <seq>
403  def seq_start (self, attrs) :
404    i=0
405  # end def
406
407  def seq_end (self) :
408    print '&nbsp;...&nbsp;'
409  # end def
410
411
412  # <optionseq>
413  def optionseq_start (self, attrs) :
414    print '[&nbsp;'
415  # end def
416
417  def optionseq_end (self) :
418    print '&nbsp;...&nbsp;'
419    print '&nbsp;]&nbsp;'
420  # end def
421
422
423
424  # <image>
425  def image_start (self, attrs) :
426    caption = attrs['caption']
427    self.images.append (caption)
428    n = len (self.images)
429    print '<center>'
430    print '<a name="' + caption + '"></a>'
431    print '<img src="' + attrs['src'] + '"/>'
432    print '</center>'
433    print '<center>'
434    print '<tt>' + repr(n) + '</tt> - <i>' + caption + '</i>'
435  # end def
436
437  def image_end (self) :
438    print '</center>'
439  # end def
440
441  def blockquote_start (self, attrs) :
442    print '<blockquote><hr>'
443  # end def
444
445  def blockquote_end (self) :
446    print '<hr></blockquote>'
447  # end def
448
449
450  # Basic element parsing handler
451  def start_element (self, name, attrs):
452    #print 'start_element: name=' + repr(name)
453    try :
454      if self.start_handlers.has_key (name) :
455        self.start_handlers [name] (attrs)
456      else :
457        self.default_start (name, attrs)
458    except TypeError:
459      print 'start_element: (error) name=' + repr(name)
460      self.default_start (name, attrs)
461  # end def
462
463  def end_element (self, name) :
464    #print 'end_element: name=' + repr(name)
465    try :
466      if self.end_handlers.has_key (name) :
467        self.end_handlers [name] ()
468      else :
469        self.default_end (name)
470    except TypeError:
471      #print 'end_element: (error) name=' + repr(name)
472      if name == u'cmt:book' :
473        self.book_end ()
474      else :
475        self.default_end (name)
476  # end def
477
478  # Unhandled elements will be trapped here
479  def dummy (self, data) :
480    if self.entities.has_key (data) :
481      self.char_data (self.entities[data])
482    #print "dummy:[" + repr (data) + "]" + repr (type(data))
483  # end def
484
485  # CDATA handling inside code sections
486  def code_char_data (self, data) :
487    if data == u'\n' :
488      self.line += data
489    else :
490      n = len (data)
491      #
492      if n > 0 :
493        if data == u'<':
494          self.line += '&lt;'
495        elif data == u'>' :
496          self.line += '&gt;'
497        else :
498          self.line += data
499  # end def
500
501  # CDATA handling outside code sections
502  def plain_char_data (self, data) :
503    if data == u'\n' :
504      self.purge ()
505    else :
506      n = len (string.strip (data))
507      #
508      if n > 0 :
509        if data == u'<':
510          self.line += '&lt;'
511        elif data == u'>' :
512          self.line += '&gt;'
513        else :
514  #        self.line += string.strip (data)
515          self.line += data
516  # end def
517
518  # CDATA handling
519  def char_data (self, data) :
520    #print '[' + repr(data) + ']' + repr (type (data)) + ' ' + repr(len (string.strip (data)))
521    if self.in_code > 0 :
522      self.code_char_data (data)
523    else :
524      self.plain_char_data (data)
525  # end def
526
527  def __init__ (self) :
528    self.line = ''
529    self.tabulation = 0
530    self.in_code = 0
531    self.sections = []
532    self.level_ids = [0]
533    self.level = 0
534    self.images = []
535    self.entities = {}
536    #
537    self.start_handlers = {}
538    self.start_handlers['cmt:code'] = self.code_start
539    self.start_handlers['cmt:cmtcode'] = self.cmtcode_start
540    self.start_handlers['cmt:section'] = self.section_start
541    self.start_handlers['cmt:book'] = self.book_start
542    self.start_handlers['cmt:syntax'] = self.syntax_start
543    self.start_handlers['cmt:rule'] = self.rule_start
544    self.start_handlers['cmt:alt'] = self.alt_start
545    self.start_handlers['cmt:continuation'] = self.continuation_start
546    self.start_handlers['cmt:kwd'] = self.kwd_start
547    self.start_handlers['cmt:term'] = self.term_start
548    self.start_handlers['cmt:ruleref'] = self.ruleref_start
549    self.start_handlers['cmt:option'] = self.option_start
550    self.start_handlers['cmt:seq'] = self.seq_start
551    self.start_handlers['cmt:optionseq'] = self.optionseq_start
552    self.start_handlers['cmt:image'] = self.image_start
553    self.start_handlers['cmt:blockquote'] = self.blockquote_start
554    #
555    self.end_handlers = {}
556    self.end_handlers['cmt:code'] = self.code_end
557    self.end_handlers['cmt:cmtcode'] = self.cmtcode_end
558    self.end_handlers['cmt:section'] = self.section_end
559    self.end_handlers['cmt:book'] = self.book_end
560    self.end_handlers['cmt:syntax'] = self.syntax_end
561    self.end_handlers['cmt:rule'] = self.rule_end
562    self.end_handlers['cmt:alt'] = self.alt_end
563    self.end_handlers['cmt:continuation'] = self.continuation_end
564    self.end_handlers['cmt:kwd'] = self.kwd_end
565    self.end_handlers['cmt:term'] = self.term_end
566    self.end_handlers['cmt:ruleref'] = self.ruleref_end
567    self.end_handlers['cmt:option'] = self.option_end
568    self.end_handlers['cmt:seq'] = self.seq_end
569    self.end_handlers['cmt:optionseq'] = self.optionseq_end
570    self.end_handlers['cmt:image'] = self.image_end
571    self.end_handlers['cmt:blockquote'] = self.blockquote_end
572  # end def
573
574
575
576#----------------------------------------------------------------------------------
577
578#----------------------------------------------------------------------------------
579#
580#  Various top level functions
581#
582#----------------------------------------------------------------------------------
583def usage() :
584  print 'Usage:'
585  print '  gendoc'
586  print 'Try "gendoc --help" for more information.'
587  sys.exit()
588# end def
589
590#----------------------------------------------------------------------------------
591def help() :
592  print "Generates the HTML documentation from Xml"
593# end def
594
595#----------------------------------------------------------------------------------
596def main() :
597  file_name = ''
598  options = []
599  for a in sys.argv[1:] :
600    if a[0] == '-' :
601      options = sys.argv[sys.argv.index(a):]
602      break
603    else :
604      file_name = a
605  try:
606    opts, args = getopt.getopt(options, 'h', ['help'])
607  except getopt.GetoptError:
608    usage()
609    sys.exit(2)
610  for o, a in opts:
611    if o in ('-h', '--help'):
612      help()
613      sys.exit()
614  book = Book ()
615  book.parse (file_name)
616# end def
617
618#---------------------------------------------------------------------
619#print '__name__ = ' + __name__
620if __name__ == "__main__":
621  main()
622# end def
623
Note: See TracBrowser for help on using the repository browser.