- Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.py
30 lines (27 loc) · 1.11 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
defdefine_env(env):
"Hook function"
@env.macro
defbuild_toc(nav, page):
"""
Build a nested list of page links from a navigation section index, to be inserted into the index page itself
"""
assertpage.is_index, (
"`build_toc` macro is only available for navigation index pages"
)
parent_section=page.parent
links= [make_link(item) foriteminparent_section.childrenifitem!=page]
html=f"<ul>{''.join(links)}</ul>"
returnhtml
defmake_link(item):
"""
Create the html list links for a nav item
`item` may be a Section or a Page
"""
ifitem.is_page:
# Note that we prepend / so the URL extracted from the navigation is relative to the
# root, and not to the location of this index page. This means we can deal with nested
# nav links from any point in the document structure
returnf"<li><a href=/{item.url}>{item.title}</a></li>"
else:
items= [make_link(sub_item) forsub_iteminitem.children]
returnf"<li>{item.title}<ul>{''.join(items)}</ul></li>"