def sidebar_menu(context): def traverse(tree, depth=1): ul = htmlutils.Tag('ul', classes={ 'nav', 'nav-pills', 'nav-stacked', 'depth-' + str(depth), }) yield ul.start() for page, children in tree: is_active = page.pk == calling_page.pk li = htmlutils.Tag('li') li.toggle_class('active', is_active) yield li.start() if is_active: yield htmlutils.tag('a', page.title, href='#') else: yield htmlutils.tag('a', page.title, href=page.url) if children: yield from traverse(children, depth=depth + 1) yield li.end() yield ul.end() if 'self' in context: calling_page = context['self'] top_menu_page = nav.get_top_menu_page(calling_page) if top_menu_page: descendants = top_menu_page.get_descendants().live().in_menu() tree = treeutils.grow_tree(descendants) return '\n'.join(traverse(treeutils.transform(tree))) return ''
def test_endless_recursion(self): root = get_default_site().root_page # Create a tree with recursive loop. tree = [Page(slug='recursive')] tree.append(tree) # Try to create that tree. treeutils.create_tree(root, tree) # See the results (hopefully without falling into endless recursion). result = treeutils.grow_tree(root.get_descendants(), slug_getter) self.assertEqual(result, ['recursive'])
def test_get_top_menu_page(self): home = get_default_site().root_page treeutils.create_tree(home, [ node('p1'), [ node('p2'), [ node('p3', show_in_menus=False), [ node('p4', show_in_menus=False), ], ], ], ]) p1, (p2, (p3, (p4,))) = treeutils.grow_tree(home.get_descendants()) self.assertEqual(nav.get_top_menu_page(None), None) self.assertEqual(nav.get_top_menu_page(home), None) self.assertEqual(nav.get_top_menu_page(p1).title, 'p1') self.assertEqual(nav.get_top_menu_page(p2).title, 'p1') self.assertEqual(nav.get_top_menu_page(p3).title, 'p1') self.assertEqual(nav.get_top_menu_page(p4).title, 'p1')
def test_get_top_menu_page(self): home = get_default_site().root_page treeutils.create_tree(home, [ node('p1'), [ node('p2'), [ node('p3', show_in_menus=False), [ node('p4', show_in_menus=False), ], ], ], ]) p1, (p2, (p3, (p4, ))) = treeutils.grow_tree(home.get_descendants()) self.assertEqual(nav.get_top_menu_page(None), None) self.assertEqual(nav.get_top_menu_page(home), None) self.assertEqual(nav.get_top_menu_page(p1).title, 'p1') self.assertEqual(nav.get_top_menu_page(p2).title, 'p1') self.assertEqual(nav.get_top_menu_page(p3).title, 'p1') self.assertEqual(nav.get_top_menu_page(p4).title, 'p1')
def sidebar_menu(context): # pylint: disable=invalid-name def traverse(tree, depth=1): ul = htmlutils.Tag('ul', classes={ 'nav', 'nav-pills', 'nav-stacked', 'depth-' + str(depth), }) yield ul.start() for page, children in tree: is_active = page.pk == calling_page.pk li = htmlutils.Tag('li') li.toggle_class('active', is_active) yield li.start() if is_active: yield htmlutils.tag('a', page.title, href='#') else: yield htmlutils.tag('a', page.title, href=page.url) if children: yield from traverse(children, depth=depth + 1) yield li.end() yield ul.end() if 'self' in context: calling_page = context['self'] top_menu_page = nav.get_top_menu_page(calling_page) if top_menu_page: descendants = top_menu_page.get_descendants().live().in_menu() tree = treeutils.grow_tree(descendants) return '\n'.join(traverse(treeutils.transform(tree))) return ''
def test_create_tree(self): root = get_default_site().root_page tree = self.fixtures.tree_of_pages() treeutils.create_tree(root, tree) self.assertEqual(treeutils.grow_tree(root.get_descendants()), tree)
def test_single_node(self): tree = treeutils.grow_tree([Node('p1', 0)], slug_getter) self.assertEqual(tree, ['p1'])
def test_empty_list(self): tree = treeutils.grow_tree([], slug_getter) self.assertEqual(tree, [])
def test_grow_tree_callback(self): dfs_tree = self.fixtures.dfs_tree_of_nodes() tree = treeutils.grow_tree(dfs_tree, lambda node: node.slug) self.assertEqual(tree, self.fixtures.tree_of_slugs())
def test_grow_tree(self): tree = treeutils.grow_tree(self.fixtures.dfs_tree_of_nodes()) self.assertEqual(tree, self.fixtures.tree_of_nodes())