aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar kosayoda <[email protected]>2021-03-24 14:28:45 +0800
committerGravatar kosayoda <[email protected]>2021-03-24 14:28:45 +0800
commit19191bb1f45d14522a8de00b70feae2fc624e58a (patch)
tree31828820433a2bcc063822c9350e524546be9b23
parentImprove variable and key names for page utils. (diff)
Simplify pathlib code and specify file encoding.
-rw-r--r--pydis_site/apps/content/utils.py18
-rw-r--r--pydis_site/apps/content/views/page_category.py5
2 files changed, 11 insertions, 12 deletions
diff --git a/pydis_site/apps/content/utils.py b/pydis_site/apps/content/utils.py
index 8cbcad91..0faf722c 100644
--- a/pydis_site/apps/content/utils.py
+++ b/pydis_site/apps/content/utils.py
@@ -8,19 +8,19 @@ from markdown2 import markdown
def get_category(path: Path) -> Dict[str, str]:
"""Load category information by name from _info.yml."""
- if not path.exists() or not path.is_dir():
+ if not path.is_dir():
raise Http404("Category not found.")
- return yaml.safe_load(path.joinpath("_info.yml").read_text())
+ return yaml.safe_load(path.joinpath("_info.yml").read_text(encoding="utf-8"))
def get_categories(path: Path) -> Dict[str, Dict]:
"""Get information for all categories."""
categories = {}
- for name in path.iterdir():
- if name.is_dir():
- categories[name.name] = get_category(path.joinpath(name.name))
+ for item in path.iterdir():
+ if item.is_dir():
+ categories[item.name] = get_category(item)
return categories
@@ -29,8 +29,8 @@ def get_category_pages(path: Path) -> Dict[str, Dict]:
"""Get all page names and their metadata at a category path."""
pages = {}
- for item in path.iterdir():
- if item.is_file() and item.name.endswith(".md"):
+ for item in path.glob("*.md"):
+ if item.is_file():
md = markdown(item.read_text(), extras=["metadata"])
pages[item.stem] = md.metadata
@@ -39,11 +39,11 @@ def get_category_pages(path: Path) -> Dict[str, Dict]:
def get_page(path: Path) -> Dict[str, Union[str, Dict]]:
"""Get one specific page."""
- if not path.exists() or not path.is_file():
+ if not path.is_file():
raise Http404("Page not found.")
html = markdown(
- path.read_text(),
+ path.read_text(encoding="utf-8"),
extras=[
"metadata",
"fenced-code-blocks",
diff --git a/pydis_site/apps/content/views/page_category.py b/pydis_site/apps/content/views/page_category.py
index 0bef82d0..91aed7f0 100644
--- a/pydis_site/apps/content/views/page_category.py
+++ b/pydis_site/apps/content/views/page_category.py
@@ -50,12 +50,11 @@ class PageOrCategoryView(TemplateView):
else:
raise Http404
- breadcrumb_items = [
+ context["breadcrumb_items"] = [
{
"name": utils.get_category(settings.PAGES_PATH / location)["name"],
"path": str(location)
- } for location in self.location.parents
+ } for location in reversed(self.location.parents)
]
- context["breadcrumb_items"] = reversed(breadcrumb_items)
return context