From 5362f65c21d7a87b64185d1947dbd9c10a7e0ef2 Mon Sep 17 00:00:00 2001 From: Govindas Date: Tue, 1 Feb 2022 18:36:30 +0200 Subject: [PATCH] More flexible resources implementation --- example/navbar | 4 ++-- gsitegen/generate.py | 41 ++++++++++++++++++++++++++--------------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/example/navbar b/example/navbar index e1de320..5498e0e 100644 --- a/example/navbar +++ b/example/navbar @@ -1,2 +1,2 @@ -Home;/ -About;/about +[Home](/) +[About](/about) diff --git a/gsitegen/generate.py b/gsitegen/generate.py index 65a8a73..7cc4853 100644 --- a/gsitegen/generate.py +++ b/gsitegen/generate.py @@ -1,5 +1,5 @@ from html5tagger import Document, E, HTML -from shutil import rmtree +import shutil import markdown import html from pathlib import Path @@ -115,9 +115,12 @@ def writePages(homepage): #creates ./website-output/pagetitle/index.html file if it is not homepage if titles[id] != homepage: - foldername = "/" + titles[id].replace(" ", "-").lower() + foldername = titles[id].replace(" ", "-").lower() + + outputpath = Path(__file__).parent.joinpath("website-output") + + dirpath = outputpath.joinpath(foldername) - dirpath = Path(__file__).parent.joinpath("website-output" + foldername) dirpath.mkdir(parents=True, exist_ok=True) filepath = dirpath.joinpath("index.html") @@ -135,19 +138,27 @@ def writePages(homepage): #resources respath = Path(__file__).parent.joinpath("resources") if respath.exists(): - for item in respath.iterdir(): - if item.is_dir(): - dir = item.name.replace(" ", "-").lower() - #loop all files within page's directory - for file in item.iterdir(): - dest = Path(__file__).parent.joinpath("website-output" + "/" + dir + "/" + file.name) - #TODO write only if file has changed - dest.write_bytes(file.read_bytes()) - else: - dest = Path(__file__).parent.joinpath("website-output" + "/" + item.name) - #TODO write only if file has changed - dest.write_bytes(item.read_bytes()) + #shutil.copytree copies timestamp too + shutil.copytree(respath, outputpath, dirs_exist_ok=True) + + #check if folders are named correctly + for folder in outputpath.iterdir(): + if folder.is_dir(): + + #rename folder name format from "About Page" to "about-page" + bettername = folder.name.replace(" ", "-").lower() + + if folder.name != bettername: + newpath = outputpath.joinpath(bettername) + + #example: resources folder is "About", page got auto-created earlier as "about" (from About.page), let's copy "About" resources to "about" and delete "About" + if newpath.exists(): + shutil.copytree(folder, newpath, dirs_exist_ok=True) + shutil.rmtree(folder) + else: + shutil.move(str(folder), newpath) + def main(): #if homepage is at Home.page, set homepage to "Home"