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