From 78817f44671aa7848babebd033601d381efc1653 Mon Sep 17 00:00:00 2001 From: Govindas Date: Thu, 27 Jan 2022 12:11:33 +0200 Subject: [PATCH] More pathlib usage & code cleanup --- .gitignore | 1 + gsitegen/generate.py | 14 ++++++++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index ae249c0..1301020 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ website-output __pycache__ coverage.xml +/gsitegen/*.page diff --git a/gsitegen/generate.py b/gsitegen/generate.py index c6e13c7..d46c64e 100644 --- a/gsitegen/generate.py +++ b/gsitegen/generate.py @@ -5,6 +5,7 @@ import markdown import html from pathlib import Path +#TODO re-think navigation bar. Think of way to add option to display navigation bar entry differently if you're currently on that page. def generateNavigationBar(lines): global navbar navbar = E @@ -108,14 +109,16 @@ def writePages(homepage): os.mkdir("./website-output" + foldername) except FileExistsError: pass - filepath = Path("./website-output" + foldername + "/index.html") - if filepath.exists(): - with open("./website-output" + foldername + "/index.html", 'r') as newpage: + filepath = "./website-output" + foldername + "/index.html" + + if Path(filepath).exists(): + with open(filepath, 'r') as newpage: if newpage.read() == page: print("Page not changed: " + titles[id]) continue - with open("./website-output" + foldername + "/index.html", 'w') as newpage: + + with open(filepath, 'w') as newpage: newpage.write(page) print("Written changed page: " + titles[id]) @@ -124,13 +127,12 @@ def main(): #if homepage is at Home.page, set homepage to "Home" homepage = "Home" - if os.path.exists("./navbar"): + if Path("./navbar").exists(): with open("./navbar", 'r') as navbarfile: generateNavigationBar(navbarfile.readlines()) else: print("No 'navbar' file found, there will be no navigation bar.") - #count total amount of pages first pagescount = 0 for file in os.listdir("./"): if file.endswith(".page"):