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;/
About;/about
[Home](/)
[About](/about)

View File

@ -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())
#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:
dest = Path(__file__).parent.joinpath("website-output" + "/" + item.name)
#TODO write only if file has changed
dest.write_bytes(item.read_bytes())
shutil.move(str(folder), newpath)
def main():
#if homepage is at Home.page, set homepage to "Home"