def test_autolist_macro ( self ): '''test autolist macro''' data = [ "Item %s" % _i for _i in range ( 1,9 ) ] template = ( macro ( 'AutoList', lambda data: data and ( T.ul ( class_='autolist' ) [ [ T.li [ _i ] for _i in data ] ] ) or '' ), T.html [ T.head [ T.title [ my_name ( ) ] ], T.body [ AutoList ( data ) ] ] ) output = flatten ( template ) self.assertEqual ( output, ( u'<html><head><title>test_autolist_macro</title></head>' u'<body><ul class="autolist"><li>Item 1</li><li>Item 2</li>' u'<li>Item 3</li><li>Item 4</li><li>Item 5</li><li>Item 6</li>' u'<li>Item 7</li><li>Item 8</li></ul></body></html>' ) )
def build_tree(facet, root_url, category_path): ul_by_path = {} root = T.ul() ul_by_path[''] = root cats = list(facet['category']) cats.sort(lambda x, y: cmp(len(x['path'].split('.')), len(y['path'].split('.')))) for c in cats: u = T.ul() ul_by_path[c['path']] = u if c['path'] == category_path: class_ = 'selected' else: class_ = '' link = T.a(href=root_url.child(c['path']))[c['data']['label']] link.attrs['class'] = class_ li_link = T.li()[link] parent = get_parent(c['path']) ul_by_path[ parent ][ li_link, u ] return flatten(root)
def build_tree(facet, root_url, category_path): ul_by_path = {} root = T.ul() ul_by_path[''] = root cats = list(facet['category']) cats.sort( lambda x, y: cmp(len(x['path'].split('.')), len(y['path'].split('.')))) for c in cats: u = T.ul() ul_by_path[c['path']] = u if c['path'] == category_path: class_ = 'selected' else: class_ = '' link = T.a(href=root_url.child(c['path']))[c['data']['label']] link.attrs['class'] = class_ li_link = T.li()[link] parent = get_parent(c['path']) ul_by_path[parent][li_link, u] return flatten(root)
def test_autolist_macro ( self ): '''test autolist macro''' sublist1 = [ "List 1:%s" % _i for _i in range ( 3 ) ] sublist2 = [ "List 2:%s" % _i for _i in range ( 3, 6 ) ] sublist3 = [ "List 3:%s" % _i for _i in range ( 6, 9 ) ] sublist3.append ( sublist2 ) data = [ 'Item A', 'Item B', 'Item C', sublist1, 'Item D', 'Item E', 'Item F', sublist3, ] template = ( macro ( 'AutoList', lambda data, level=0: data and ( T.ul ( class_='autolist level-%s' % level ) [ [ T.li [ [ lambda _i, _j: _i, AutoList ] [ isinstance ( _i, list ) ]( _i, level + 1 ) ] for _i in data ] ] ) or '' ), T.html [ T.head [ T.title [ my_name ( ) ] ], T.body [ AutoList ( data ) ] ] ) output = flatten ( template ) self.assertEqual ( output, ( u'<html><head><title>test_autolist_macro</title></head>' u'<body><ul class="autolist level-0"><li>Item A</li>' u'<li>Item B</li><li>Item C</li><li><ul class="autolist level-1">' u'<li>List 1:0</li><li>List 1:1</li><li>List 1:2</li></ul></li>' u'<li>Item D</li><li>Item E</li><li>Item F</li><li>' u'<ul class="autolist level-1"><li>List 3:6</li>' u'<li>List 3:7</li><li>List 3:8</li><li><ul class="autolist level-2">' u'<li>List 2:3</li><li>List 2:4</li><li>List 2:5</li></ul>' u'</li></ul></li></ul></body></html>' ) )
def renderModuleNav(self, tag, data): """ :type data: (werkzeug.routing.MapAdapter, None) """ (url, unused) = data currentEndpoint = url.match()[0] currentModule = [x for x in self.modules if currentEndpoint in x.endpoints][0] html = ( T.ul(class_='nav nav-pills nav-stacked')[ [ T.li(class_='active' if currentEndpoint == page.endpoint else None)[ T.a(href=page.url)[page.name] ] for page in currentModule.publicPages ] ] ) return tag[html]
def test_autolist_macro1(): """test autolist macro""" data = ["Item %s" % _i for _i in range(1, 9)] template = ( macro('AutoList', lambda data: data and ( T.ul(class_='autolist')[ [T.li[_i] for _i in data] ] ) or '' ), T.html[ T.head[T.title[my_name()]], T.body[AutoList(data)] # @UndefinedVariable ] ) output = flatten(template) assert output == ('<html><head><title>test_autolist_macro1</title></head>' '<body><ul class="autolist"><li>Item 1</li><li>Item 2</li>' '<li>Item 3</li><li>Item 4</li><li>Item 5</li><li>Item 6</li>' '<li>Item 7</li><li>Item 8</li></ul></body></html>')
def _add_child_menus(tag, node, urlpath): nodepath = node.path.split('.') nodedepth = len(nodepath) label = node.label # If we're not showing submenus (apart from the selected item) # then loop through our urlpath and check that the node matches each segment. # If it doesn't then return our result so far if not self.openall: for n, segment in enumerate(urlpath[:self.openallbelow]): if n+1>=nodedepth or segment != nodepath[n+1]: return t = T.li()[T.a(href=self.urlfactory(node))[label]] tag[t] # Mark selected item if request_path == nodepath: add_class(t, 'selected') elif request_path[:nodedepth] == nodepath[:nodedepth]: add_class(t, 'selectedpath') if self.item_id == 'name': t.attrs['id'] = 'nav-%s'%node.name # only show up to a set depth if self.maxdepth is not None and nodedepth > self.maxdepth: return # If we have more children and we're showing them all or we're considered to be in the right location - then add them if node.children and (self.openall or force_url_path[:nodedepth] == nodepath[:nodedepth]): s = T.ul() t[s] _add_children(s, node, urlpath,is_root=False)
def renderNavbar(self, tag, data): """ :type data: (werkzeug.routing.MapAdapter, None) """ (url, unused) = data currentEndpoint = url.match()[0] burgerToggle = lambda targetId: ( T.button(class_='navbar-toggle', **{'data-toggle': 'collapse', 'data-target': '#%s' % targetId})[ T.span(class_='sr-only')['Toggle navigation'], T.span(class_='icon-bar'), T.span(class_='icon-bar'), T.span(class_='icon-bar'), ] ) mainNavigation = lambda: ( forEach(self.modules, lambda mod: ( T.li(class_='active' if currentEndpoint in mod.endpoints else None)[ T.a(href=url.build(mod.pages[0].endpoint))[mod.name] ] )) ) html = ( T.nav(class_='navbar navbar-default', role='navigation')[ T.div(class_='container-fluid')[ T.div(class_='navbar-header')[ burgerToggle('mainNavCollapse'), T.span(class_='navbar-brand')['Rudykocur Maxwell'] ], T.div(class_='collapse navbar-collapse', id='mainNavCollapse')[ T.ul(class_="nav navbar-nav")[ mainNavigation() ], T.ul(class_="nav navbar-nav navbar-right")[ T.li(class_='dropdown')[ T.a(href='#', class_='dropdown-toggle', **{'data-toggle': 'dropdown'})[ 'Characters', ' ', T.span(class_='caret') ], T.ul(class_='dropdown-menu', role='menu')[ T.li(role='presentation', class_='dropdown-header')['Account: Rudykocur'], T.li[T.a(href='#')['Rudykocur Maxwell']], T.li[T.a(href='#')['Imaginary Profile']], T.li(role='presentation', class_='divider'), T.li(role='presentation', class_='dropdown-header')['Account: Generic'], T.li[T.a(href='#')['All Skills V']], ], ], T.li[ T.a(href=url.build('logout'))['Logout'] ] ] ] ] ] ) return tag[html]
def build_menu(self, sitemap, request): # Highlighting of navigation is driven by the request. request_path = self.current_path_segments(request) # Filter out empty segments request_path = [ r for r in request_path if r ] if self.force_url_path is not None: force_url_path = [ r for r in self.force_url_path if r ] else: force_url_path = request_path def _add_root_menu(tag): nodepath = node.path.split('.') nodedepth = len(nodepath) label = node.label t = T.li()[T.a(href=self.urlfactory(node))[label]] tag[t] if request_path == nodepath: add_class(t, 'selected') if self.item_id == 'name': t.attrs['id'] = 'nav-%s'%node.name def _add_child_menus(tag, node, urlpath): nodepath = node.path.split('.') nodedepth = len(nodepath) label = node.label # If we're not showing submenus (apart from the selected item) # then loop through our urlpath and check that the node matches each segment. # If it doesn't then return our result so far if not self.openall: for n, segment in enumerate(urlpath[:self.openallbelow]): if n+1>=nodedepth or segment != nodepath[n+1]: return t = T.li()[T.a(href=self.urlfactory(node))[label]] tag[t] # Mark selected item if request_path == nodepath: add_class(t, 'selected') elif request_path[:nodedepth] == nodepath[:nodedepth]: add_class(t, 'selectedpath') if self.item_id == 'name': t.attrs['id'] = 'nav-%s'%node.name # only show up to a set depth if self.maxdepth is not None and nodedepth > self.maxdepth: return # If we have more children and we're showing them all or we're considered to be in the right location - then add them if node.children and (self.openall or force_url_path[:nodedepth] == nodepath[:nodedepth]): s = T.ul() t[s] _add_children(s, node, urlpath,is_root=False) def _add_children(tag, node, urlpath, is_root=True): """ The root node is the top level segment (e.g. for an absolute url /a/b/c, root node is 'a' node is the sitemap root node (with path 'root') """ # if this is the root node of our tree, and we have 'showroot' set # then add this as a top level list item if node is not None and is_root is True and self.showroot is True: _add_root_menu(tag) if node is None or node.children is None: return tag # for each child of the node, (i.e. for each top level menu item) for child in node.children: # as long as a group is defined, otherwise continue if self.navigation_type is not None and child.group != self.navigation_type: continue _add_child_menus(tag, child, urlpath) return tag def _menu_built(tag): if 'firstlast' in self.item_class: try: add_class(tag.children[0], 'first-child') add_class(tag.children[-1], 'last-child') except IndexError: pass if 'number' in self.item_class: for n,child in enumerate(tag.children): add_class(tag.children[n], 'item-%s'%(n+1)) return tag # Render from the root of the sitemap by default node = sitemap # Adjust the root node for the start depth. urlpath = request.url.path_segments if urlpath == ['']: urlpath = [] if len(urlpath) < self.startdepth: # We've not reached this section of the site yet. node = None else: # Traverse the sitemap to find the root node at the given start depth. for segment in urlpath[:self.startdepth]: node = node.child_by_name(segment) # We always start with a menu tag = T.ul() if self.css_class is not None: add_class(tag, self.css_class) # Take the rootnode and add children given the navigation group as # context (navgroup is primary secondary but coded as integers) tag = _add_children(tag, node, urlpath) return _menu_built(tag)