Improve navigation with breadcrumbs

This commit is contained in:
L. Karkkainen 2023-01-27 04:29:15 +00:00
parent e328d4406b
commit 41da8bbd61
2 changed files with 20 additions and 5 deletions

View File

@ -15,11 +15,12 @@ from sanic.response.types import HTTPResponse
class DirectoryHandler: class DirectoryHandler:
def __init__( def __init__(
self, directory: Path, autoindex: bool, index_name: str self, directory: Path, autoindex: bool, index_name: str, url: str
) -> None: ) -> None:
self.directory = directory self.directory = directory
self.autoindex = autoindex self.autoindex = autoindex
self.index_name = index_name self.index_name = index_name
self.url = url
def handle(self): def handle(self):
index_file = self.directory / self.index_name index_file = self.directory / self.index_name
@ -30,7 +31,7 @@ class DirectoryHandler:
return file(index_file) return file(index_file)
def index(self): def index(self):
page = AutoIndex(self._iter_files()) page = AutoIndex(self._iter_files(), self.url)
return html(page.render()) return html(page.render())
def _prepare_file(self, path: Path) -> Dict[str, Union[int, str]]: def _prepare_file(self, path: Path) -> Dict[str, Union[int, str]]:
@ -61,7 +62,7 @@ class DirectoryHandler:
) -> Optional[Coroutine[Any, Any, HTTPResponse]]: ) -> Optional[Coroutine[Any, Any, HTTPResponse]]:
if exception.autoindex or exception.index_name: if exception.autoindex or exception.index_name:
maybe_response = DirectoryHandler( maybe_response = DirectoryHandler(
exception.location, exception.autoindex, exception.index_name exception.location, exception.autoindex, exception.index_name, request.path
).handle() ).handle()
if maybe_response: if maybe_response:
return maybe_response return maybe_response

View File

@ -16,6 +16,11 @@ class FileInfo(TypedDict):
class AutoIndex(BasePage): class AutoIndex(BasePage):
EXTRA_STYLE = dedent( EXTRA_STYLE = dedent(
""" """
#breadcrumbs .path-0 a::before { content: "🏠"; }
a { text-decoration: none; }
a:hover { text-decoration: underline; }
#breadcrumbs span { display: inline-block; }
#breadcrumbs span:has(> a:hover) * { color: #ff0d61; text-shadow: 0 0 0.3rem; }
table.autoindex tr { display: flex; } table.autoindex tr { display: flex; }
table.autoindex td { margin: 0 0.5rem; } table.autoindex td { margin: 0 0.5rem; }
table.autoindex td:first-child { flex: 1; } table.autoindex td:first-child { flex: 1; }
@ -25,9 +30,10 @@ class AutoIndex(BasePage):
) )
TITLE = "📁 File browser" TITLE = "📁 File browser"
def __init__(self, files: Iterable[FileInfo]) -> None: def __init__(self, files: Iterable[FileInfo], url: str) -> None:
super().__init__() super().__init__()
self.files = files self.files = files
self.url = url
def _body(self) -> None: def _body(self) -> None:
with self.doc.main: with self.doc.main:
@ -35,7 +41,15 @@ class AutoIndex(BasePage):
self._file_table(self.files) self._file_table(self.files)
def _headline(self): def _headline(self):
self.doc.h1(self.TITLE) # Implement a heading with the current path, combined with breadcrumb links
with self.doc.h1(id="breadcrumbs"):
p = self.url.split("/")[:-1]
for i in reversed(range(len(p))):
self.doc.span(class_=f"path-{i}").__enter__()
for i, part in enumerate(p):
path = "/".join(p[: i + 1]) + "/"
self.doc.a(f"{part}/", href=path)
self.doc.__exit__(None, None, None)
def _file_table(self, files: Iterable[FileInfo]): def _file_table(self, files: Iterable[FileInfo]):
with self.doc.table(class_="autoindex"): with self.doc.table(class_="autoindex"):