File: NameIndex.py

package info (click to toggle)
synopsis 0.8.0-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 10,112 kB
  • ctags: 12,996
  • sloc: cpp: 34,254; ansic: 33,620; python: 10,975; sh: 7,261; xml: 6,369; makefile: 773; asm: 445
file content (107 lines) | stat: -rw-r--r-- 3,818 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#
# Copyright (C) 2000 Stephen Davies
# Copyright (C) 2000 Stefan Seefeld
# All rights reserved.
# Licensed to the public under the terms of the GNU LGPL (>= 2),
# see the file COPYING for details.
#

from Synopsis.Processor import Parameter
from Synopsis import AST, Type
from Synopsis.Formatters.HTML.View import View
from Synopsis.Formatters.HTML.Tags import *

import os

class NameIndex(View):
   """Creates an index of all names on one view in alphabetical order"""

   columns = Parameter(2, 'the number of columns for the listing')

   def register(self, processor):

      View.register(self, processor)
      self.processor.add_root_view(self.filename(), 'Name Index', 'main', 1)

   def filename(self): return self.processor.file_layout.special('NameIndex')

   def title(self): return 'Synopsis - Name Index'

   def process(self, start):
      """Creates the view. It is created as a list of tables, one for each
      letter. The tables have a number of columns, which is 2 by default.
      _processItem is called for each item in the dictionary."""

      self.start_file()
      self.write(self.processor.navigation_bar(self.filename()))
      self.write(entity('h1', "Name Index"))
      self.write('<i>Hold the mouse over a link to see the scope of each name</i>')

      dict = self.make_dictionary()
      keys = dict.keys()
      keys.sort()
      linker = lambda key: '<a href="#%s">%s</a>'%(ord(key),key)
      self.write(div('nameindex-index', string.join(map(linker, keys))))
      for key in keys:
         self.write('<a name="%s">'%ord(key)+'</a>')
         self.write(entity('h2', key))
         self.write('<table border="0" width="100%" summary="table of names">')
         self.write('<col width="*"/>'*self.columns)
         self.write('<tr>')
         items = dict[key]
         numitems = len(items)
         start = 0
         for column in range(self.columns):
            end = numitems * (column + 1) / self.columns
            self.write('<td valign="top">')
            for item in items[start:end]:
               self._process_item(item)
            self.write('</td>')
            start = end
         self.write('</tr></table>')
	
      self.end_file()

   def make_dictionary(self):
      """Returns a dictionary of items. The keys of the dictionary are the
      headings - the first letter of the name. The values are each a sorted
      list of items with that first letter."""

      decl_filter = lambda type: (isinstance(type, Type.Declared)
                                  and not isinstance(type.declaration(), AST.Builtin))
      def name_cmp(a,b):
         a, b = a.name(), b.name()
         res = cmp(a[-1],b[-1])
         if res == 0: res = cmp(a,b)
         return res
      dict = {}
      def hasher(type, dict=dict):
         name = type.name()
         try: key = name[-1][0]
         except:
            print 'name:',name, 'type:',repr(type)
            raise
         if key >= 'a' and key <= 'z': key = chr(ord(key) - 32)
         if dict.has_key(key): dict[key].append(type)
         else: dict[key] = [type]
      # Fill the dict
      map(hasher, filter(decl_filter, self.processor.ast.types().values()))
      # Now sort the dict
      for items in dict.values():
         items.sort(name_cmp)
      return dict

   def _process_item(self, type):
      """Process the given name for output"""

      name = type.name()
      decl = type.declaration() # non-declared types are filtered out
      if isinstance(decl, AST.Function):
         realname = escape(decl.realname()[-1]) + '()'
      else:
         realname = escape(name[-1])
      self.write('\n')
      title = escape(string.join(name, '::'))
      type = decl.type()
      name = self.reference(name, (), realname, title=title)+' '+type
      self.write(div('nameindex-item', name))