More flexible resources implementation

This commit is contained in:
Govindas 2022-02-01 18:36:30 +02:00
parent 150a81ffd0
commit 5362f65c21
2 changed files with 28 additions and 17 deletions

View File

@ -1,2 +1,2 @@
Home;/ [Home](/)
About;/about [About](/about)

View File

@ -1,5 +1,5 @@
from html5tagger import Document, E, HTML from html5tagger import Document, E, HTML
from shutil import rmtree import shutil
import markdown import markdown
import html import html
from pathlib import Path from pathlib import Path
@ -115,9 +115,12 @@ def writePages(homepage):
#creates ./website-output/pagetitle/index.html file if it is not homepage #creates ./website-output/pagetitle/index.html file if it is not homepage
if titles[id] != 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) dirpath.mkdir(parents=True, exist_ok=True)
filepath = dirpath.joinpath("index.html") filepath = dirpath.joinpath("index.html")
@ -135,19 +138,27 @@ def writePages(homepage):
#resources #resources
respath = Path(__file__).parent.joinpath("resources") respath = Path(__file__).parent.joinpath("resources")
if respath.exists(): if respath.exists():
for item in respath.iterdir():
if item.is_dir():
dir = item.name.replace(" ", "-").lower()
#loop all files within page's directory #shutil.copytree copies timestamp too
for file in item.iterdir(): shutil.copytree(respath, outputpath, dirs_exist_ok=True)
dest = Path(__file__).parent.joinpath("website-output" + "/" + dir + "/" + file.name)
#TODO write only if file has changed #check if folders are named correctly
dest.write_bytes(file.read_bytes()) for folder in outputpath.iterdir():
else: if folder.is_dir():
dest = Path(__file__).parent.joinpath("website-output" + "/" + item.name)
#TODO write only if file has changed #rename folder name format from "About Page" to "about-page"
dest.write_bytes(item.read_bytes()) 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(): def main():
#if homepage is at Home.page, set homepage to "Home" #if homepage is at Home.page, set homepage to "Home"