1 | # $Id: SiteHierarchy.py,v 1.1 2001/10/11 03:25:54 tavis_rudd Exp $ |
---|
2 | """Create menus and crumbs from a site hierarchy. |
---|
3 | |
---|
4 | You define the site hierarchy as lists/tuples. Each location in the hierarchy |
---|
5 | is a (url, description) tuple. Each list has the base URL/text in the 0 |
---|
6 | position, and all the children coming after it. Any child can be a list, |
---|
7 | representing further depth to the hierarchy. See the end of the file for an |
---|
8 | example hierarchy. |
---|
9 | |
---|
10 | Use Hierarchy(contents, currentURL), where contents is this hierarchy, and |
---|
11 | currentURL is the position you are currently in. The menubar and crumbs methods |
---|
12 | give you the HTML output. |
---|
13 | |
---|
14 | There are methods you can override to customize the HTML output. |
---|
15 | |
---|
16 | Meta-Data |
---|
17 | ================================================================================ |
---|
18 | Author: Ian Bicking <ianb@colorstudy.com> |
---|
19 | Version: $Revision: 1.1 $ |
---|
20 | Start Date: 2001/07/23 |
---|
21 | Last Revision Date: $Date: 2001/10/11 03:25:54 $ |
---|
22 | """ |
---|
23 | __author__ = "Ian Bicking <ianb@colorstudy.com>" |
---|
24 | __version__ = "$Revision: 1.1 $"[11:-2] |
---|
25 | |
---|
26 | ################################################## |
---|
27 | ## DEPENDENCIES |
---|
28 | import string |
---|
29 | try: |
---|
30 | from cStringIO import StringIO |
---|
31 | except ImportError: |
---|
32 | from StringIO import StringIO |
---|
33 | |
---|
34 | |
---|
35 | ################################################## |
---|
36 | ## GLOBALS & CONSTANTS |
---|
37 | |
---|
38 | True, False = (1==1), (0==1) |
---|
39 | |
---|
40 | ################################################## |
---|
41 | ## CLASSES |
---|
42 | |
---|
43 | class Hierarchy: |
---|
44 | def __init__(self, hierarchy, currentURL, prefix='', menuCSSClass=None, |
---|
45 | crumbCSSClass=None): |
---|
46 | """ |
---|
47 | hierarchy is described above, currentURL should be somewhere in |
---|
48 | the hierarchy. prefix will be added before all of the URLs (to |
---|
49 | help mitigate the problems with absolute URLs), and if given, |
---|
50 | cssClass will be used for both links *and* nonlinks. |
---|
51 | """ |
---|
52 | |
---|
53 | self._contents = hierarchy |
---|
54 | self._currentURL = currentURL |
---|
55 | if menuCSSClass: |
---|
56 | self._menuCSSClass = ' class="%s"' % menuCSSClass |
---|
57 | else: |
---|
58 | self._menuCSSClass = '' |
---|
59 | if crumbCSSClass: |
---|
60 | self._crumbCSSClass = ' class="%s"' % crumbCSSClass |
---|
61 | else: |
---|
62 | self._crumbCSSClass = '' |
---|
63 | self._prefix=prefix |
---|
64 | |
---|
65 | |
---|
66 | ## Main output methods |
---|
67 | |
---|
68 | def menuList(self, menuCSSClass=None): |
---|
69 | """An indented menu list""" |
---|
70 | if menuCSSClass: |
---|
71 | self._menuCSSClass = ' class="%s"' % menuCSSClass |
---|
72 | |
---|
73 | stream = StringIO() |
---|
74 | for item in self._contents[1:]: |
---|
75 | self._menubarRecurse(item, 0, stream) |
---|
76 | return stream.getvalue() |
---|
77 | |
---|
78 | def crumbs(self, crumbCSSClass=None): |
---|
79 | """The home>where>you>are crumbs""" |
---|
80 | if crumbCSSClass: |
---|
81 | self._crumbCSSClass = ' class="%s"' % crumbCSSClass |
---|
82 | |
---|
83 | path = [] |
---|
84 | pos = self._contents |
---|
85 | while 1: |
---|
86 | ## This is not the fastest algorithm, I'm afraid. |
---|
87 | ## But it probably won't be for a huge hierarchy anyway. |
---|
88 | foundAny = False |
---|
89 | path.append(pos[0]) |
---|
90 | for item in pos[1:]: |
---|
91 | if self._inContents(item): |
---|
92 | if type(item) is type(()): |
---|
93 | path.append(item) |
---|
94 | break |
---|
95 | else: |
---|
96 | pos = item |
---|
97 | foundAny = True |
---|
98 | break |
---|
99 | if not foundAny: |
---|
100 | break |
---|
101 | if len(path) == 1: |
---|
102 | return self.emptyCrumb() |
---|
103 | return string.join(map(lambda x, self=self: self.crumbLink(x[0], x[1]), |
---|
104 | path), self.crumbSeperator()) + \ |
---|
105 | self.crumbTerminator() |
---|
106 | |
---|
107 | ## Methods to control the Aesthetics |
---|
108 | # - override these methods for your own look |
---|
109 | |
---|
110 | def menuLink(self, url, text, indent): |
---|
111 | if url == self._currentURL or self._prefix + url == self._currentURL: |
---|
112 | return '%s<B%s>%s</B> <BR>\n' % (' '*2*indent, |
---|
113 | self._menuCSSClass, text) |
---|
114 | else: |
---|
115 | return '%s<A HREF="%s%s"%s>%s</A> <BR>\n' % \ |
---|
116 | (' '*2*indent, self._prefix, url, |
---|
117 | self._menuCSSClass, text) |
---|
118 | |
---|
119 | def crumbLink(self, url, text): |
---|
120 | if url == self._currentURL or self._prefix + url == self._currentURL: |
---|
121 | return '<B%s>%s</B>' % (text, self._crumbCSSClass) |
---|
122 | else: |
---|
123 | return '<A HREF="%s%s"%s>%s</A>' % \ |
---|
124 | (self._prefix, url, self._crumbCSSClass, text) |
---|
125 | |
---|
126 | def crumbSeperator(self): |
---|
127 | return ' > ' |
---|
128 | |
---|
129 | def crumbTerminator(self): |
---|
130 | return '' |
---|
131 | |
---|
132 | def emptyCrumb(self): |
---|
133 | """When you are at the homepage""" |
---|
134 | return '' |
---|
135 | |
---|
136 | ## internal methods |
---|
137 | |
---|
138 | def _menubarRecurse(self, contents, indent, stream): |
---|
139 | if type(contents) is type(()): |
---|
140 | url, text = contents |
---|
141 | rest = [] |
---|
142 | else: |
---|
143 | url, text = contents[0] |
---|
144 | rest = contents[1:] |
---|
145 | stream.write(self.menuLink(url, text, indent)) |
---|
146 | if self._inContents(contents): |
---|
147 | for item in rest: |
---|
148 | self._menubarRecurse(item, indent+1, stream) |
---|
149 | |
---|
150 | def _inContents(self, contents): |
---|
151 | if type(contents) is type(()): |
---|
152 | return self._currentURL == contents[0] |
---|
153 | for item in contents: |
---|
154 | if self._inContents(item): |
---|
155 | return True |
---|
156 | return False |
---|
157 | |
---|
158 | ################################################## |
---|
159 | ## from the command line |
---|
160 | |
---|
161 | if __name__ == '__main__': |
---|
162 | hierarchy = [('/', 'home'), |
---|
163 | ('/about', 'About Us'), |
---|
164 | [('/services', 'Services'), |
---|
165 | [('/services/products', 'Products'), |
---|
166 | ('/services/products/widget', 'The Widget'), |
---|
167 | ('/services/products/wedge', 'The Wedge'), |
---|
168 | ('/services/products/thimble', 'The Thimble'), |
---|
169 | ], |
---|
170 | ('/services/prices', 'Prices'), |
---|
171 | ], |
---|
172 | ('/contact', 'Contact Us'), |
---|
173 | ] |
---|
174 | |
---|
175 | for url in ['/', '/services', '/services/products/widget', '/contact']: |
---|
176 | print '<p>', '='*50 |
---|
177 | print '<br> %s: <br>\n' % url |
---|
178 | n = Hierarchy(hierarchy, url, menuCSSClass='menu', crumbCSSClass='crumb', |
---|
179 | prefix='/here') |
---|
180 | print n.menuList() |
---|
181 | print '<p>', '-'*50 |
---|
182 | print n.crumbs() |
---|