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