]> git.ipfire.org Git - ipfire.org.git/commitdiff
docs: Tidy up breadcrumb generation
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 25 Jan 2024 11:25:52 +0000 (11:25 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 25 Jan 2024 11:25:52 +0000 (11:25 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/backend/wiki.py

index 03368e8057e8f2521df2e78117571876d4580508..7ef214016da9e78f56bfc18702af9792349c6178 100644 (file)
@@ -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):