def test_tag_multiplication_with_macro(): """tag multiplication including macro""" url_data = [ {'url': 'http://www.google.com', 'label': 'Google', 'class': 'link'}, {'url': 'http://www.yahoo.com', 'label': 'Yahoo!', 'class': 'link'}, {'url': 'http://www.amazon.com', 'label': 'Amazon', 'class': 'link'} ] template = ( macro('test_macro', lambda url: T.a(href=url)["$label"] ), T.html[ T.head[T.title[my_name()]], T.body[ T.ul[ T.li(class_="$class")[test_macro("$url")] * url_data # @UndefinedVariable ] ] ] ) output = flatten(template) assert output == ('<html><head><title>test_tag_multiplication_with_macro</title></head>' '<body><ul><li class="link"><a href="http://www.google.com">Google</a></li>' '<li class="link"><a href="http://www.yahoo.com">Yahoo!</a></li>' '<li class="link"><a href="http://www.amazon.com">Amazon</a></li></ul>' '</body></html>')
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 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 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 _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]