From 0c1186ef0694d12cc654288f09cf09343324c5ee Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Thu, 25 Jan 2024 11:25:52 +0000 Subject: [PATCH] docs: Tidy up breadcrumb generation Signed-off-by: Michael Tremer --- src/backend/wiki.py | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/backend/wiki.py b/src/backend/wiki.py index 03368e80..7ef21401 100644 --- a/src/backend/wiki.py +++ b/src/backend/wiki.py @@ -58,6 +58,15 @@ class Wiki(misc.Object): # Normalise links return os.path.normpath(path) + def _make_url(self, path): + """ + Composes the URL out of the path + """ + # Remove any leading slashes (if present) + path = path.removeprefix("/") + + return os.path.join("/docs", path) + def page_exists(self, path): page = self.get_page(path) @@ -140,15 +149,28 @@ class Wiki(misc.Object): # Just creates a blank last version of the page self.create_page(page, author=author, content=None, **kwargs) - def make_breadcrumbs(self, url): - # Split and strip all empty elements (double slashes) - parts = list(e for e in url.split("/") if e) - + def make_breadcrumbs(self, path): ret = [] - for part in ("/".join(parts[:i]) for i in range(1, len(parts))): - ret.append(("/docs/%s" % part, self.get_page_title(part, os.path.basename(part)))) - return ret + while path: + # Cut off everything after the last slash + path, _, _ = path.rpartition("/") + + # Do not include the root + if not path: + break + + # Find the page + page = self.get_page(path) + + # Append the URL and title to the output + ret.append(( + page.url if page else self._make_url(path), + page.title if page else os.path.basename(path), + )) + + # Return the breadcrumbs in order + return reversed(ret) def search(self, query, account=None, limit=None): res = self._get_pages(""" @@ -413,7 +435,7 @@ class Page(misc.Object): @property def url(self): - return "/docs%s" % self.page + return self.backend.wiki._make_url(self.page) @property def full_url(self): -- 2.39.2