diff options
Diffstat (limited to 'pydis_site/apps/content/views')
| -rw-r--r-- | pydis_site/apps/content/views/page_category.py | 72 |
1 files changed, 52 insertions, 20 deletions
diff --git a/pydis_site/apps/content/views/page_category.py b/pydis_site/apps/content/views/page_category.py index eec4e7e5..4031fde2 100644 --- a/pydis_site/apps/content/views/page_category.py +++ b/pydis_site/apps/content/views/page_category.py @@ -1,6 +1,7 @@ import typing as t from pathlib import Path +import frontmatter from django.conf import settings from django.http import Http404 from django.views.generic import TemplateView @@ -14,16 +15,22 @@ class PageOrCategoryView(TemplateView): def dispatch(self, request: t.Any, *args, **kwargs) -> t.Any: """Conform URL path location to the filesystem path.""" self.location = Path(kwargs.get("location", "")) - self.full_location = settings.PAGES_PATH / self.location + + # URL location on the filesystem + self.full_location = settings.CONTENT_PAGES_PATH / self.location + + # Possible places to find page content information + self.category_path = self.full_location + self.page_path = self.full_location.with_suffix(".md") return super().dispatch(request, *args, **kwargs) def get_template_names(self) -> t.List[str]: """Checks if the view uses the page template or listing template.""" - if self.full_location.is_dir(): - template_name = "content/listing.html" - elif self.full_location.with_suffix(".md").is_file(): + if self.page_path.is_file(): template_name = "content/page.html" + elif self.category_path.is_dir(): + template_name = "content/listing.html" else: raise Http404 @@ -33,29 +40,54 @@ class PageOrCategoryView(TemplateView): """Assign proper context variables based on what resource user requests.""" context = super().get_context_data(**kwargs) - if self.full_location.is_dir(): - context["categories"] = utils.get_categories(self.full_location) - context["pages"] = utils.get_category_pages(self.full_location) - - category = utils.get_category(self.full_location) - context["page_title"] = category["name"] - context["page_description"] = category["description"] - - context["path"] = f"{self.location}/" # Add trailing slash here to simplify template - elif self.full_location.with_suffix(".md").is_file(): - page, metadata = utils.get_page(self.full_location.with_suffix(".md")) - context["page"] = page - context["page_title"] = metadata["title"] - context["page_description"] = metadata["description"] - context["relevant_links"] = metadata.get("relevant_links", {}) + if self.page_path.is_file(): + context.update(self._get_page_context(self.page_path)) + elif self.category_path.is_dir(): + context.update(self._get_category_context(self.category_path)) + context["path"] = f"{self.location}/" # Add trailing slash to simplify template else: raise Http404 + # Add subarticle information for dropdown menu if the page is also a category + if self.page_path.is_file() and self.category_path.is_dir(): + context["subarticles"] = [] + for entry in self.category_path.iterdir(): + entry_info = {"path": entry.stem} + if entry.suffix == ".md": + entry_info["name"] = frontmatter.load(entry).metadata["title"] + elif entry.is_dir(): + entry_info["name"] = utils.get_category(entry)["title"] + else: + continue + context["subarticles"].append(entry_info) + context["breadcrumb_items"] = [ { - "name": utils.get_category(settings.PAGES_PATH / location)["name"], + "name": utils.get_category(settings.CONTENT_PAGES_PATH / location)["title"], "path": str(location) } for location in reversed(self.location.parents) ] return context + + @staticmethod + def _get_page_context(path: Path) -> t.Dict[str, t.Any]: + page, metadata = utils.get_page(path) + return { + "page": page, + "page_title": metadata["title"], + "page_description": metadata["description"], + "relevant_links": metadata.get("relevant_links", {}), + "toc": metadata.get("toc") + } + + @staticmethod + def _get_category_context(path: Path) -> t.Dict[str, t.Any]: + category = utils.get_category(path) + return { + "categories": utils.get_categories(path), + "pages": utils.get_category_pages(path), + "page_title": category["title"], + "page_description": category["description"], + "icon": category.get("icon"), + } |