From 624e87d15ccfbe4ba2410dcaab0b4fb494b664e5 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Sat, 27 Mar 2021 12:42:07 +0800 Subject: Improve content page and listing metadata keys. Make category `title` consistent with page `title`. Simplify `icon` key, since there is no benefit to defining the icon class and the icon separately. Allow overriding the `icon` for category entries just like page entries. --- pydis_site/apps/content/views/page_category.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'pydis_site/apps/content/views') diff --git a/pydis_site/apps/content/views/page_category.py b/pydis_site/apps/content/views/page_category.py index eec4e7e5..723c5ad0 100644 --- a/pydis_site/apps/content/views/page_category.py +++ b/pydis_site/apps/content/views/page_category.py @@ -38,8 +38,9 @@ class PageOrCategoryView(TemplateView): context["pages"] = utils.get_category_pages(self.full_location) category = utils.get_category(self.full_location) - context["page_title"] = category["name"] + context["page_title"] = category["title"] context["page_description"] = category["description"] + context["icon"] = category.get("icon") context["path"] = f"{self.location}/" # Add trailing slash here to simplify template elif self.full_location.with_suffix(".md").is_file(): @@ -53,7 +54,7 @@ class PageOrCategoryView(TemplateView): context["breadcrumb_items"] = [ { - "name": utils.get_category(settings.PAGES_PATH / location)["name"], + "name": utils.get_category(settings.PAGES_PATH / location)["title"], "path": str(location) } for location in reversed(self.location.parents) ] -- cgit v1.2.3 From 3d75841b2e7a8ca219d77314c1533827e8213b8e Mon Sep 17 00:00:00 2001 From: kosayoda Date: Mon, 29 Mar 2021 15:23:36 +0800 Subject: Allow displaying a page at a category path. This is useful for our /contributing pages, which have a main contributing page at /contributing but project pages under the same path, /contributing/bot etc. --- .../pydis-guides/how-to-contribute-a-page.md | 20 ++++++++ pydis_site/apps/content/views/page_category.py | 57 ++++++++++++++-------- 2 files changed, 58 insertions(+), 19 deletions(-) (limited to 'pydis_site/apps/content/views') diff --git a/pydis_site/apps/content/resources/guides/pydis-guides/how-to-contribute-a-page.md b/pydis_site/apps/content/resources/guides/pydis-guides/how-to-contribute-a-page.md index 51486bfe..303a2c0f 100644 --- a/pydis_site/apps/content/resources/guides/pydis-guides/how-to-contribute-a-page.md +++ b/pydis_site/apps/content/resources/guides/pydis-guides/how-to-contribute-a-page.md @@ -21,8 +21,10 @@ As website changes require staff approval, discussing the page content beforehan ## Creating the Page All pages are located in the `site` repo, at the path `pydis_site/apps/content/resources/`. This is the root folder, which corresponds to the URL `www.pythondiscord.com/pages/`. + For example, the file `pydis_site/apps/content/resources/hello-world.md` will result in a page available at `www.pythondiscord.com/pages/hello-world`. +#### Page Categories Nested folders represent page categories on the website. Each folder under the root folder must include a `_info.yml` file with the following: ```yml @@ -33,6 +35,24 @@ icon: fas fa-folder # Optional All the markdown files in this folder will then be under this category. +#### Having the Category also be a Page +In order to make categories a page, place a page inside the category folder **with the same name as the category folder**. + +```plaintext +guides +├── contributing +│   ├── _info.yml +│   ├── contributing.md +│   └── bot.md +└── _info.yml +``` + +In the above example, `www.pythondiscord.com/guides/` will list `Contributing` as a category entry with information from `contributing/_info.yml`. + +However, `www.pythondiscord.com/guides/contributing` will render `contributing.md` rather than show the category contents, so *it is the article's responsibility to link to any subpages under the article*. + +Therefore, `www.pythondiscord.com/guides/contributing/bot` will then render `bot.md`, with backlinks to `contributing.md`. + ## Writing the Page Files representing pages are in `.md` (Markdown) format, with all-lowercase filenames and spaces replaced with `-` characters. diff --git a/pydis_site/apps/content/views/page_category.py b/pydis_site/apps/content/views/page_category.py index 723c5ad0..711d6a56 100644 --- a/pydis_site/apps/content/views/page_category.py +++ b/pydis_site/apps/content/views/page_category.py @@ -14,16 +14,23 @@ 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", "")) + + # URL location on the filesystem self.full_location = settings.PAGES_PATH / self.location + # Possible places to find page content information + self.category_page_path = self.full_location.joinpath(self.location.stem).with_suffix(".md") + 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.category_page_path.is_file() or 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,22 +40,13 @@ 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["title"] - context["page_description"] = category["description"] - context["icon"] = category.get("icon") - - 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.category_page_path.is_file(): + context.update(self._get_page_context(self.category_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 + elif self.page_path.is_file(): + context.update(self._get_page_context(self.page_path)) else: raise Http404 @@ -60,3 +58,24 @@ class PageOrCategoryView(TemplateView): ] 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", {}), + } + + @staticmethod + def _get_category_context(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"), + } -- cgit v1.2.3 From 69f33290b8bd12b95bb6c620a9a1422a5d11b798 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Mon, 29 Mar 2021 17:51:10 +0800 Subject: Allow adding a table of contents to a page. --- .../pydis-guides/how-to-contribute-a-page.md | 2 ++ pydis_site/apps/content/utils.py | 12 +++++++--- pydis_site/apps/content/views/page_category.py | 1 + pydis_site/static/css/content/page.css | 19 +++++++++++++++ pydis_site/templates/content/page.html | 28 +++++++++++++++------- 5 files changed, 50 insertions(+), 12 deletions(-) (limited to 'pydis_site/apps/content/views') diff --git a/pydis_site/apps/content/resources/guides/pydis-guides/how-to-contribute-a-page.md b/pydis_site/apps/content/resources/guides/pydis-guides/how-to-contribute-a-page.md index 67666428..c2d9d975 100644 --- a/pydis_site/apps/content/resources/guides/pydis-guides/how-to-contribute-a-page.md +++ b/pydis_site/apps/content/resources/guides/pydis-guides/how-to-contribute-a-page.md @@ -5,6 +5,7 @@ icon: fas fa-info relevant_links: Contributing to Site: https://pythondiscord.com/pages/contributing/site/ Using Git: https://pythondiscord.com/pages/contributing/working-with-git/ +toc: 4 --- Pages, which include guides, articles, and other static content, are stored in markdown files in the `site` repository on Github. @@ -80,6 +81,7 @@ Pages, which include guides, articles, and other static content,... ### Optional Fields - **icon:** Icon for the category entry for the page. Default: `fab fa-python` - **relevant_links:** A YAML dictionary containing `text:link` pairs. See the example above. +- **toc:** A number representing the smallest heading tag to show in the table of contents. ## Extended Markdown diff --git a/pydis_site/apps/content/utils.py b/pydis_site/apps/content/utils.py index 726c991f..d6886ce2 100644 --- a/pydis_site/apps/content/utils.py +++ b/pydis_site/apps/content/utils.py @@ -44,14 +44,20 @@ def get_page(path: Path) -> Tuple[str, Dict]: raise Http404("Page not found.") metadata, content = frontmatter.parse(path.read_text(encoding="utf-8")) - html = markdown.markdown( - content, + toc_depth = metadata.get("toc", 1) + + md = markdown.Markdown( extensions=[ "extra", # Empty string for marker to disable text searching for [TOC] # By using a metadata key instead, we save time on long markdown documents - TocExtension(title="Table of Contents:", permalink=True, marker="") + TocExtension(permalink=True, marker="", toc_depth=toc_depth) ] ) + html = md.convert(content) + + # Don't set the TOC if the metadata does not specify one + if "toc" in metadata: + metadata["toc"] = md.toc return str(html), metadata diff --git a/pydis_site/apps/content/views/page_category.py b/pydis_site/apps/content/views/page_category.py index 711d6a56..a995d2a1 100644 --- a/pydis_site/apps/content/views/page_category.py +++ b/pydis_site/apps/content/views/page_category.py @@ -67,6 +67,7 @@ class PageOrCategoryView(TemplateView): "page_title": metadata["title"], "page_description": metadata["description"], "relevant_links": metadata.get("relevant_links", {}), + "toc": metadata.get("toc") } @staticmethod diff --git a/pydis_site/static/css/content/page.css b/pydis_site/static/css/content/page.css index 97b297b2..3ac41d1b 100644 --- a/pydis_site/static/css/content/page.css +++ b/pydis_site/static/css/content/page.css @@ -49,3 +49,22 @@ img + em { /* Style */ font-size: .875em; } + +/* + * Remove extra padding on the left of TOC items + */ +ul.menu-list.toc { + margin-left: 0; +} + +/* + * Remove bullets set by the markdown extension, since bulma adds vertical + * lines to represent nesting + */ +.toc li { + list-style-type: none; +} +/* ..but we still want bullets on the top
    items */ +.toc > ul > li { + list-style-type: disc; +} diff --git a/pydis_site/templates/content/page.html b/pydis_site/templates/content/page.html index 06d74208..45aa8221 100644 --- a/pydis_site/templates/content/page.html +++ b/pydis_site/templates/content/page.html @@ -9,20 +9,30 @@ {% endblock %} {% block page_content %} - {% if relevant_links|length > 0 %} + {% if relevant_links or toc %}
    {{ page|safe }}
    -
    - - -
    + {% if toc %} +
    + + +
    + {% endif %} + {% if relevant_links %} +
    + + +
    + {% endif %}
    {% else %} -- cgit v1.2.3 From 4208626262d6cf790da1c0e00765b375fa427aa7 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Thu, 1 Apr 2021 16:29:29 +0800 Subject: Place category pages in the same directory as categories. --- .../resources/guides/pydis-guides/how-to-contribute-a-page.md | 6 +++--- pydis_site/apps/content/tests/helpers.py | 4 ++-- pydis_site/apps/content/tests/test_views.py | 1 - pydis_site/apps/content/utils.py | 3 ++- pydis_site/apps/content/views/page_category.py | 11 ++++------- 5 files changed, 11 insertions(+), 14 deletions(-) (limited to 'pydis_site/apps/content/views') diff --git a/pydis_site/apps/content/resources/guides/pydis-guides/how-to-contribute-a-page.md b/pydis_site/apps/content/resources/guides/pydis-guides/how-to-contribute-a-page.md index c2d9d975..51f1097d 100644 --- a/pydis_site/apps/content/resources/guides/pydis-guides/how-to-contribute-a-page.md +++ b/pydis_site/apps/content/resources/guides/pydis-guides/how-to-contribute-a-page.md @@ -36,14 +36,14 @@ icon: fas fa-folder # Optional All the markdown files in this folder will then be under this category. -#### Having the Category also be a Page -In order to make categories a page, place a page inside the category folder **with the same name as the category folder**. +#### Having the Category Also Be a Page +In order to make categories a page, just create a page **with the same name as the category folder** in the category's parent directory. ```plaintext guides +├── contributing.md ├── contributing │   ├── _info.yml -│   ├── contributing.md │   └── bot.md └── _info.yml ``` diff --git a/pydis_site/apps/content/tests/helpers.py b/pydis_site/apps/content/tests/helpers.py index 59cd3bd6..202dee42 100644 --- a/pydis_site/apps/content/tests/helpers.py +++ b/pydis_site/apps/content/tests/helpers.py @@ -50,9 +50,9 @@ class MockPagesTestCase(TestCase): ├── root.md ├── root_without_metadata.md ├── not_a_page.md + ├── tmp.md ├── tmp |   ├── _info.yml - |   ├── tmp.md |   └── category |    ├── _info.yml |      └── subcategory_without_info @@ -86,6 +86,6 @@ class MockPagesTestCase(TestCase): # for testing purposes. # See: https://jmcgeheeiv.github.io/pyfakefs/release/usage.html#os-temporary-directories self.fs.create_file("tmp/_info.yml", contents=CATEGORY_INFO) - self.fs.create_file("tmp/tmp.md", contents=MARKDOWN_WITH_METADATA) + self.fs.create_file("tmp.md", contents=MARKDOWN_WITH_METADATA) self.fs.create_file("tmp/category/_info.yml", contents=MARKDOWN_WITH_METADATA) self.fs.create_dir("tmp/category/subcategory_without_info") diff --git a/pydis_site/apps/content/tests/test_views.py b/pydis_site/apps/content/tests/test_views.py index 81c4012b..ab266b29 100644 --- a/pydis_site/apps/content/tests/test_views.py +++ b/pydis_site/apps/content/tests/test_views.py @@ -29,7 +29,6 @@ def patch_dispatch_attributes(view: PageOrCategoryView, location: str) -> None: view.full_location = view.location # Possible places to find page content information - view.category_page_path = view.full_location.joinpath(view.location.stem).with_suffix(".md") view.category_path = view.full_location view.page_path = view.full_location.with_suffix(".md") diff --git a/pydis_site/apps/content/utils.py b/pydis_site/apps/content/utils.py index d6886ce2..d3f270ff 100644 --- a/pydis_site/apps/content/utils.py +++ b/pydis_site/apps/content/utils.py @@ -32,7 +32,8 @@ def get_category_pages(path: Path) -> Dict[str, Dict]: pages = {} for item in path.glob("*.md"): - if item.is_file(): + # Only list page if there is no category with the same name + if item.is_file() and not item.with_suffix("").is_dir(): pages[item.stem] = frontmatter.load(item).metadata return pages diff --git a/pydis_site/apps/content/views/page_category.py b/pydis_site/apps/content/views/page_category.py index a995d2a1..8783e33f 100644 --- a/pydis_site/apps/content/views/page_category.py +++ b/pydis_site/apps/content/views/page_category.py @@ -19,7 +19,6 @@ class PageOrCategoryView(TemplateView): self.full_location = settings.PAGES_PATH / self.location # Possible places to find page content information - self.category_page_path = self.full_location.joinpath(self.location.stem).with_suffix(".md") self.category_path = self.full_location self.page_path = self.full_location.with_suffix(".md") @@ -27,7 +26,7 @@ class PageOrCategoryView(TemplateView): def get_template_names(self) -> t.List[str]: """Checks if the view uses the page template or listing template.""" - if self.category_page_path.is_file() or self.page_path.is_file(): + if self.page_path.is_file(): template_name = "content/page.html" elif self.category_path.is_dir(): template_name = "content/listing.html" @@ -40,13 +39,11 @@ class PageOrCategoryView(TemplateView): """Assign proper context variables based on what resource user requests.""" context = super().get_context_data(**kwargs) - if self.category_page_path.is_file(): - context.update(self._get_page_context(self.category_page_path)) + 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 - elif self.page_path.is_file(): - context.update(self._get_page_context(self.page_path)) else: raise Http404 @@ -71,7 +68,7 @@ class PageOrCategoryView(TemplateView): } @staticmethod - def _get_category_context(path) -> t.Dict[str, t.Any]: + def _get_category_context(path: Path) -> t.Dict[str, t.Any]: category = utils.get_category(path) return { "categories": utils.get_categories(path), -- cgit v1.2.3 From bbb6fc242a4eb10551a8549c400f3db88658fce1 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Thu, 1 Apr 2021 19:11:28 +0800 Subject: Add a dropdown menu listing direct children. This only shows when the page is also a category, since regular pages have no children and regular categories already list their children. --- pydis_site/apps/content/templatetags/__init__.py | 0 .../apps/content/templatetags/str_methods.py | 21 ++++++++++++++++ pydis_site/apps/content/tests/test_templatetags.py | 12 ++++++++++ pydis_site/apps/content/tests/test_views.py | 3 ++- pydis_site/apps/content/views/page_category.py | 7 ++++++ pydis_site/templates/content/base.html | 5 ++++ pydis_site/templates/content/dropdown.html | 28 ++++++++++++++++++++++ 7 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 pydis_site/apps/content/templatetags/__init__.py create mode 100644 pydis_site/apps/content/templatetags/str_methods.py create mode 100644 pydis_site/apps/content/tests/test_templatetags.py create mode 100644 pydis_site/templates/content/dropdown.html (limited to 'pydis_site/apps/content/views') diff --git a/pydis_site/apps/content/templatetags/__init__.py b/pydis_site/apps/content/templatetags/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pydis_site/apps/content/templatetags/str_methods.py b/pydis_site/apps/content/templatetags/str_methods.py new file mode 100644 index 00000000..91db3e72 --- /dev/null +++ b/pydis_site/apps/content/templatetags/str_methods.py @@ -0,0 +1,21 @@ +from django import template +from django.template.defaultfilters import stringfilter + +register = template.Library() + + +@register.filter(is_safe=True) +@stringfilter +def replace_hyphens(value: str, replacement: str) -> str: + """ + Simple filter to replace hyphens with the specified replacement string. + + Usage: + + ```django + {% for name_with_hyphens in name_list %} + {{ name_with_hyphens|replace_hyphen:" " }} + {% endfor %} + ``` + """ + return value.replace("-", replacement) diff --git a/pydis_site/apps/content/tests/test_templatetags.py b/pydis_site/apps/content/tests/test_templatetags.py new file mode 100644 index 00000000..1147bd88 --- /dev/null +++ b/pydis_site/apps/content/tests/test_templatetags.py @@ -0,0 +1,12 @@ +from django.test import TestCase + +from pydis_site.apps.content.templatetags.str_methods import replace_hyphens + + +class TestTemplateTags(TestCase): + """Tests for the custom template tags in the content app.""" + + def test_replace_hyphens(self): + self.assertEquals(replace_hyphens("word-with-hyphens", " "), "word with hyphens") + self.assertEquals(replace_hyphens("---", ""), "") + self.assertEquals(replace_hyphens("hi----", "A"), "hiAAAA") diff --git a/pydis_site/apps/content/tests/test_views.py b/pydis_site/apps/content/tests/test_views.py index ab266b29..cd0d0bf7 100644 --- a/pydis_site/apps/content/tests/test_views.py +++ b/pydis_site/apps/content/tests/test_views.py @@ -160,7 +160,8 @@ class PageOrCategoryViewTests(MockPagesTestCase, SimpleTestCase, TestCase): "page": PARSED_HTML, "page_title": PARSED_METADATA["title"], "page_description": PARSED_METADATA["description"], - "relevant_links": PARSED_METADATA["relevant_links"] + "relevant_links": PARSED_METADATA["relevant_links"], + "subarticles": ["category"] } for key, expected_value in expected_page_context.items(): with self.subTest(): diff --git a/pydis_site/apps/content/views/page_category.py b/pydis_site/apps/content/views/page_category.py index 8783e33f..bb2c07cc 100644 --- a/pydis_site/apps/content/views/page_category.py +++ b/pydis_site/apps/content/views/page_category.py @@ -47,6 +47,13 @@ class PageOrCategoryView(TemplateView): 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"] = [ + path.stem for path in self.category_path.iterdir() + if path.suffix != ".yml" + ] + context["breadcrumb_items"] = [ { "name": utils.get_category(settings.PAGES_PATH / location)["title"], diff --git a/pydis_site/templates/content/base.html b/pydis_site/templates/content/base.html index 19eec5d4..21895479 100644 --- a/pydis_site/templates/content/base.html +++ b/pydis_site/templates/content/base.html @@ -14,6 +14,7 @@ diff --git a/pydis_site/templates/content/dropdown.html b/pydis_site/templates/content/dropdown.html new file mode 100644 index 00000000..711be113 --- /dev/null +++ b/pydis_site/templates/content/dropdown.html @@ -0,0 +1,28 @@ +{% load str_methods %} + + + + -- cgit v1.2.3 From 4039266c5083545b498b5ef751a98961aa5ba7e1 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Thu, 1 Apr 2021 20:32:24 +0800 Subject: Fix overriden PAGES_PATH in settings.py. This was causing mysterious 404s because while migrating a feature to dewikification, the same variable would be placed in the settings file. --- pydis_site/apps/content/tests/test_views.py | 2 +- pydis_site/apps/content/views/page_category.py | 4 ++-- pydis_site/apps/events/tests/test_views.py | 4 ++-- pydis_site/apps/events/views/page.py | 7 ++++--- pydis_site/settings.py | 9 +++++---- 5 files changed, 14 insertions(+), 12 deletions(-) (limited to 'pydis_site/apps/content/views') diff --git a/pydis_site/apps/content/tests/test_views.py b/pydis_site/apps/content/tests/test_views.py index cd0d0bf7..36d771a1 100644 --- a/pydis_site/apps/content/tests/test_views.py +++ b/pydis_site/apps/content/tests/test_views.py @@ -33,7 +33,7 @@ def patch_dispatch_attributes(view: PageOrCategoryView, location: str) -> None: view.page_path = view.full_location.with_suffix(".md") -@override_settings(PAGES_PATH=BASE_PATH) +@override_settings(CONTENT_PAGES_PATH=BASE_PATH) class PageOrCategoryViewTests(MockPagesTestCase, SimpleTestCase, TestCase): """Tests for the PageOrCategoryView class.""" diff --git a/pydis_site/apps/content/views/page_category.py b/pydis_site/apps/content/views/page_category.py index bb2c07cc..b31814f7 100644 --- a/pydis_site/apps/content/views/page_category.py +++ b/pydis_site/apps/content/views/page_category.py @@ -16,7 +16,7 @@ class PageOrCategoryView(TemplateView): self.location = Path(kwargs.get("location", "")) # URL location on the filesystem - self.full_location = settings.PAGES_PATH / self.location + self.full_location = settings.CONTENT_PAGES_PATH / self.location # Possible places to find page content information self.category_path = self.full_location @@ -56,7 +56,7 @@ class PageOrCategoryView(TemplateView): context["breadcrumb_items"] = [ { - "name": utils.get_category(settings.PAGES_PATH / location)["title"], + "name": utils.get_category(settings.CONTENT_PAGES_PATH / location)["title"], "path": str(location) } for location in reversed(self.location.parents) ] diff --git a/pydis_site/apps/events/tests/test_views.py b/pydis_site/apps/events/tests/test_views.py index 0db0ef9a..23c9e596 100644 --- a/pydis_site/apps/events/tests/test_views.py +++ b/pydis_site/apps/events/tests/test_views.py @@ -17,7 +17,7 @@ class IndexTests(TestCase): class PageTests(TestCase): - @override_settings(PAGES_PATH=PAGES_PATH) + @override_settings(EVENTS_PAGES_PATH=PAGES_PATH) def test_valid_event_page_reponse_200(self): """Should return response code 200 when visiting valid event page.""" pages = ( @@ -29,7 +29,7 @@ class PageTests(TestCase): resp = self.client.get(page) self.assertEqual(resp.status_code, 200) - @override_settings(PAGES_PATH=PAGES_PATH) + @override_settings(EVENTS_PAGES_PATH=PAGES_PATH) def test_invalid_event_page_404(self): """Should return response code 404 when visiting invalid event page.""" pages = ( diff --git a/pydis_site/apps/events/views/page.py b/pydis_site/apps/events/views/page.py index f4c37aeb..eab2f462 100644 --- a/pydis_site/apps/events/views/page.py +++ b/pydis_site/apps/events/views/page.py @@ -11,15 +11,16 @@ class PageView(TemplateView): def get_template_names(self) -> List[str]: """Get specific template names.""" path: str = self.kwargs['path'] - page_path = settings.PAGES_PATH / path + page_path = settings.EVENTS_PAGES_PATH / path if page_path.is_dir(): page_path = page_path / "_index.html" path = f"{path}/_index.html" else: - page_path = settings.PAGES_PATH / f"{path}.html" + page_path = settings.EVENTS_PAGES_PATH / f"{path}.html" path = f"{path}.html" if not page_path.exists(): raise Http404 + print(f"events/{settings.EVENTS_PAGES_PATH.name}/{path}") - return [f"events/{settings.PAGES_PATH.name}/{path}"] + return [f"events/{settings.EVENTS_PAGES_PATH.name}/{path}"] diff --git a/pydis_site/settings.py b/pydis_site/settings.py index 3abf556a..e7bf2a79 100644 --- a/pydis_site/settings.py +++ b/pydis_site/settings.py @@ -119,9 +119,6 @@ MIDDLEWARE = [ ] ROOT_URLCONF = 'pydis_site.urls' -# Path for events pages -PAGES_PATH = Path(BASE_DIR, "pydis_site", "templates", "events", "pages") - TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', @@ -287,4 +284,8 @@ SITE_REPOSITORY_OWNER = "python-discord" SITE_REPOSITORY_NAME = "site" SITE_REPOSITORY_BRANCH = "master" -PAGES_PATH = Path(BASE_DIR, "pydis_site", "apps", "content", "resources") +# Path for events pages +EVENTS_PAGES_PATH = Path(BASE_DIR, "pydis_site", "templates", "events", "pages") + +# Path for content pages +CONTENT_PAGES_PATH = Path(BASE_DIR, "pydis_site", "apps", "content", "resources") -- cgit v1.2.3 From bb8a57a00835ffae8382f4360e0f55888bbe03b0 Mon Sep 17 00:00:00 2001 From: kosayoda Date: Fri, 2 Apr 2021 17:47:42 +0800 Subject: Use metadata titles in Sub-Articles dropdown. This allows us to keep filenames (thus URLs) as concise as possible, while having a more descriptive entry in the Sub-Articles dropdown for category pages. --- pydis_site/apps/content/templatetags/__init__.py | 0 pydis_site/apps/content/templatetags/str_methods.py | 21 --------------------- pydis_site/apps/content/tests/helpers.py | 2 +- pydis_site/apps/content/tests/test_templatetags.py | 12 ------------ pydis_site/apps/content/tests/test_views.py | 2 +- pydis_site/apps/content/views/page_category.py | 15 +++++++++++---- pydis_site/templates/content/dropdown.html | 6 ++---- 7 files changed, 15 insertions(+), 43 deletions(-) delete mode 100644 pydis_site/apps/content/templatetags/__init__.py delete mode 100644 pydis_site/apps/content/templatetags/str_methods.py delete mode 100644 pydis_site/apps/content/tests/test_templatetags.py (limited to 'pydis_site/apps/content/views') diff --git a/pydis_site/apps/content/templatetags/__init__.py b/pydis_site/apps/content/templatetags/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/pydis_site/apps/content/templatetags/str_methods.py b/pydis_site/apps/content/templatetags/str_methods.py deleted file mode 100644 index 91db3e72..00000000 --- a/pydis_site/apps/content/templatetags/str_methods.py +++ /dev/null @@ -1,21 +0,0 @@ -from django import template -from django.template.defaultfilters import stringfilter - -register = template.Library() - - -@register.filter(is_safe=True) -@stringfilter -def replace_hyphens(value: str, replacement: str) -> str: - """ - Simple filter to replace hyphens with the specified replacement string. - - Usage: - - ```django - {% for name_with_hyphens in name_list %} - {{ name_with_hyphens|replace_hyphen:" " }} - {% endfor %} - ``` - """ - return value.replace("-", replacement) diff --git a/pydis_site/apps/content/tests/helpers.py b/pydis_site/apps/content/tests/helpers.py index 202dee42..29140375 100644 --- a/pydis_site/apps/content/tests/helpers.py +++ b/pydis_site/apps/content/tests/helpers.py @@ -87,5 +87,5 @@ class MockPagesTestCase(TestCase): # See: https://jmcgeheeiv.github.io/pyfakefs/release/usage.html#os-temporary-directories self.fs.create_file("tmp/_info.yml", contents=CATEGORY_INFO) self.fs.create_file("tmp.md", contents=MARKDOWN_WITH_METADATA) - self.fs.create_file("tmp/category/_info.yml", contents=MARKDOWN_WITH_METADATA) + self.fs.create_file("tmp/category/_info.yml", contents=CATEGORY_INFO) self.fs.create_dir("tmp/category/subcategory_without_info") diff --git a/pydis_site/apps/content/tests/test_templatetags.py b/pydis_site/apps/content/tests/test_templatetags.py deleted file mode 100644 index 1147bd88..00000000 --- a/pydis_site/apps/content/tests/test_templatetags.py +++ /dev/null @@ -1,12 +0,0 @@ -from django.test import TestCase - -from pydis_site.apps.content.templatetags.str_methods import replace_hyphens - - -class TestTemplateTags(TestCase): - """Tests for the custom template tags in the content app.""" - - def test_replace_hyphens(self): - self.assertEquals(replace_hyphens("word-with-hyphens", " "), "word with hyphens") - self.assertEquals(replace_hyphens("---", ""), "") - self.assertEquals(replace_hyphens("hi----", "A"), "hiAAAA") diff --git a/pydis_site/apps/content/tests/test_views.py b/pydis_site/apps/content/tests/test_views.py index 36d771a1..74d38f78 100644 --- a/pydis_site/apps/content/tests/test_views.py +++ b/pydis_site/apps/content/tests/test_views.py @@ -161,7 +161,7 @@ class PageOrCategoryViewTests(MockPagesTestCase, SimpleTestCase, TestCase): "page_title": PARSED_METADATA["title"], "page_description": PARSED_METADATA["description"], "relevant_links": PARSED_METADATA["relevant_links"], - "subarticles": ["category"] + "subarticles": [{"path": "category", "name": "Category Name"}] } for key, expected_value in expected_page_context.items(): with self.subTest(): diff --git a/pydis_site/apps/content/views/page_category.py b/pydis_site/apps/content/views/page_category.py index b31814f7..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 @@ -49,10 +50,16 @@ class PageOrCategoryView(TemplateView): # 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"] = [ - path.stem for path in self.category_path.iterdir() - if path.suffix != ".yml" - ] + 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"] = [ { diff --git a/pydis_site/templates/content/dropdown.html b/pydis_site/templates/content/dropdown.html index 25a68e88..c9491f3a 100644 --- a/pydis_site/templates/content/dropdown.html +++ b/pydis_site/templates/content/dropdown.html @@ -1,5 +1,3 @@ -{% load str_methods %} -