2023-09-09 10:56:04 +01:00
|
|
|
import jinja2
|
2023-09-14 22:02:40 +01:00
|
|
|
from mkdocs.utils.templates import url_filter
|
2023-09-09 10:56:04 +01:00
|
|
|
|
2023-09-08 22:10:52 +01:00
|
|
|
def is_list_page(page) -> bool:
|
|
|
|
if not page.parent:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return page.parent.children[0] == page
|
|
|
|
|
2023-09-08 22:32:51 +01:00
|
|
|
def children(page):
|
|
|
|
if not is_list_page(page):
|
|
|
|
return []
|
2023-09-15 18:11:06 +01:00
|
|
|
|
|
|
|
page_children = []
|
|
|
|
|
|
|
|
for child in page.parent.children[1:]:
|
|
|
|
if child.is_page:
|
|
|
|
page_children.append(child)
|
|
|
|
|
|
|
|
# If the child is a section, use its index page as the child
|
|
|
|
if child.is_section:
|
|
|
|
page_children.append(child.children[0])
|
|
|
|
|
|
|
|
return page_children
|
2023-09-08 22:32:51 +01:00
|
|
|
|
2023-09-15 18:19:06 +01:00
|
|
|
def get_parent_page(page):
|
|
|
|
if is_list_page(page) and len(page.parent.children) == 1:
|
2023-09-15 21:11:44 +01:00
|
|
|
parent = page.parent.parent
|
|
|
|
else:
|
|
|
|
parent = page.parent
|
2023-09-15 18:19:06 +01:00
|
|
|
|
2023-09-15 21:11:44 +01:00
|
|
|
if parent is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return parent.children[0]
|
2023-09-15 18:19:06 +01:00
|
|
|
|
|
|
|
|
2023-09-09 10:56:04 +01:00
|
|
|
@jinja2.pass_context
|
|
|
|
def get_page(context, slug):
|
|
|
|
nav = context["nav"]
|
|
|
|
for page in nav.pages:
|
|
|
|
if page.file.src_uri == slug:
|
|
|
|
return page
|
|
|
|
return None
|
|
|
|
|
2023-09-14 22:02:40 +01:00
|
|
|
@jinja2.pass_context
|
|
|
|
def get_page_url(context, slug):
|
|
|
|
page = get_page(context, slug)
|
|
|
|
|
|
|
|
if page is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return url_filter(context, page.url)
|
|
|
|
|
2023-09-08 22:10:52 +01:00
|
|
|
def on_env(env, config, files):
|
|
|
|
env.filters["is_list_page"] = is_list_page
|
2023-09-08 22:32:51 +01:00
|
|
|
env.filters["children"] = children
|
2023-09-09 10:56:04 +01:00
|
|
|
env.globals["get_page"] = get_page
|
2023-09-14 22:02:40 +01:00
|
|
|
env.globals["get_page_url"] = get_page_url
|
2023-09-15 18:19:06 +01:00
|
|
|
env.globals["get_parent_page"] = get_parent_page
|