From 19191bb1f45d14522a8de00b70feae2fc624e58a Mon Sep 17 00:00:00 2001 From: kosayoda Date: Wed, 24 Mar 2021 14:28:45 +0800 Subject: Simplify pathlib code and specify file encoding. --- pydis_site/apps/content/utils.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'pydis_site/apps/content/utils.py') 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", -- cgit v1.2.3