Synopsis - Synopsis/Formatters/HTML/Views/ModuleListing.py

File: Synopsis/Formatters/HTML/Views/ModuleListing.py

     1#
     2# Copyright (C) 2000 Stephen Davies
     3# Copyright (C) 2000 Stefan Seefeld
     4# All rights reserved.
     5# Licensed to the public under the terms of the GNU LGPL (>= 2),
     6# see the file COPYING for details.
     7#
     8
     9from Synopsis.Processor import Parameter
    10from Synopsis import ASG
    11from Synopsis.Formatters.HTML.View import View
    12from Synopsis.Formatters.HTML.Tags import *
    13
    14class ModuleListing(View):
    15   """Create an index of all modules."""
    16
    17   short_title = Parameter('Modules', 'short title')
    18   child_types = Parameter(None, 'the types of children to include')
    19
    20   def register(self, frame):
    21
    22      super(ModuleListing, self).register(frame)
    23      self._children_cache = {}
    24
    25   def filename(self):
    26
    27      return self.directory_layout.special('ModuleListing')
    28
    29   def title(self):
    30
    31      return self.short_title
    32
    33   def root(self):
    34
    35      return self.filename(), self.title()
    36
    37   def process(self):
    38      """Create a view with an index of all modules."""
    39
    40      # Create the file
    41      self.start_file()
    42      self.write_navigation_bar()
    43      self.write('<ul class="tree">')
    44      # FIXME: see HTML.Formatter
    45      module = self.processor.ir.asg.declarations[0]
    46      self.index_module(module, module.name)
    47      self.write('</ul>')
    48      self.end_file()
    49
    50   def _link_href(self, module):
    51      """Returns the link to the given declaration"""
    52
    53      return rel(self.filename(),
    54                 self.directory_layout.module_index(module.name))
    55
    56   def get_children(self, decl):
    57      """Returns the children of the given declaration"""
    58
    59      try: return self._children_cache[decl]
    60      except KeyError: pass
    61      children = [c for c in decl.declarations
    62                  if isinstance(c, ASG.Module) and
    63                  (not self.child_types or child.type in self.child_types)]
    64      self._children_cache[decl] = children
    65      return children
    66
    67   def index_module(self, module, rel_scope):
    68      "Write a link for this module and recursively visit child modules."
    69
    70      my_scope = module.name
    71      # Find children, and sort so that compound children (packages) go first
    72      children = self.get_children(module)
    73      children.sort(lambda a,b,g=self.get_children:
    74                    cmp(len(g(b)),len(g(a))))
    75      # Print link to this module
    76      name = str(rel_scope.prune(my_scope)) or 'Global %s'%module.type.capitalize()
    77      link = self._link_href(module)
    78      text = href(link, name, target='detail')
    79      if not len(children):
    80         self.write('<li>%s</li>'%text)
    81      else:
    82         self.write('<li>%s<ul class="tree">'%text)
    83         # Add children
    84         for child in children:
    85            self.index_module(child, my_scope)
    86         self.write('</ul></li>')
    87
    88

Generated on Thu Apr 16 16:27:15 2009 by
synopsis (version devel)