SiteGenerator/generate.py
2022-01-22 16:19:47 +02:00

64 lines
1.8 KiB
Python

import os
from html5tagger import Document, E, HTML
from shutil import rmtree
#if homepage is at Home.page, set homepage to "Home"
homepage = "Home"
try:
os.mkdir("./website-output")
except FileExistsError:
print("Output directory already exists, let's clean it!")
#deleting contents of folder without deleting the folder, to increase compatibility with various systems
for root, dirs, files in os.walk('./website-output'):
for f in files:
os.unlink(os.path.join(root, f))
for d in dirs:
rmtree(os.path.join(root, d))
def generateNavigationBar(lines):
global navbar
navbar = E
for line in lines:
title, link = line.strip().split(";", 1)
navbar = navbar.li(HTML("<a href='" + link + "'>" + title + "</a>"))
def generateLines(title, lines):
global document
title = title.replace(".page", "")
document = Document(title, lang="en")
for line in lines:
document.p(line.strip())
generatePage(title)
def generatePage(title):
#creates ./website-output/pagetitle/index.html file if it is not homepage
foldername = ""
if title != homepage:
foldername = "/" + title.replace(" ", "-").lower()
os.mkdir("./website-output" + foldername)
with open("./website-output" + foldername + "/index.html", 'w') as newpage:
if 'navbar' in globals():
newpage.write(str(E.ul(navbar).join(document)))
else:
newpage.write(str(document))
if os.path.exists("./navbar"):
with open("./navbar", 'r') as navbarfile:
generateNavigationBar(navbarfile.readlines())
else:
print("No 'navbar' file found, there will be no navigation bar.")
pagescount = 0
for file in os.listdir("./"):
if file.endswith(".page"):
with open(file, 'r') as page:
generateLines(os.path.basename(file), page.readlines())
pagescount=pagescount+1
print("Generated " + str(pagescount) + " pages")