2024-02-16 16:13:07 +00:00
|
|
|
import jinja2
|
2024-02-17 22:43:19 +00:00
|
|
|
from mkdocs.structure.nav import Navigation
|
2024-02-21 11:40:55 +00:00
|
|
|
from urllib.parse import urlparse
|
2024-02-16 16:13:07 +00: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
|
|
|
|
|
|
|
|
raise ValueError("Unable to find page for '%s'", slug)
|
|
|
|
|
|
|
|
|
2024-02-16 17:12:00 +00:00
|
|
|
def get_notes_from_nav(nav: Navigation):
|
|
|
|
notes_section = next(item for item in nav if item.is_section and item.title == "Notes")
|
|
|
|
|
|
|
|
notes = [item for item in nav.pages if item.is_page and notes_section in item.ancestors and item.file.src_uri != "notes/index.md"]
|
|
|
|
|
|
|
|
return notes_section, notes
|
|
|
|
|
|
|
|
|
2024-02-16 16:13:07 +00:00
|
|
|
@jinja2.pass_context
|
|
|
|
def get_notes(context):
|
2024-02-16 17:12:00 +00:00
|
|
|
notes_section, notes = get_notes_from_nav(context["nav"])
|
2024-02-16 16:13:07 +00:00
|
|
|
|
|
|
|
return sorted(notes, key=lambda p: p.meta["git_creation_date_localized_raw_iso_date"], reverse=True)
|
|
|
|
|
2024-02-21 11:40:55 +00:00
|
|
|
def get_domain(url):
|
|
|
|
return urlparse(url).netloc
|
2024-02-16 16:13:07 +00:00
|
|
|
|
2024-02-15 18:07:13 +00:00
|
|
|
def on_env(env, config, files):
|
|
|
|
env.tests["startswith"] = str.startswith
|
2024-02-16 16:13:07 +00:00
|
|
|
env.globals["get_page"] = get_page
|
|
|
|
env.globals["get_notes"] = get_notes
|
2024-02-21 11:40:55 +00:00
|
|
|
env.filters["domain"] = get_domain
|