SiteGenerator/generate.py

64 lines
1.8 KiB
Python
Raw Normal View History

2022-01-21 10:16:26 +00:00
import os
2022-01-22 09:26:09 +00:00
from html5tagger import Document, E, HTML
2022-01-21 10:16:26 +00:00
from shutil import rmtree
#if homepage is at Home.page, set homepage to "Home"
homepage = "Home"
try:
2022-01-21 11:09:43 +00:00
os.mkdir("./website-output")
2022-01-21 10:16:26 +00:00
except FileExistsError:
2022-01-21 11:09:43 +00:00
print("Output directory already exists, let's clean it!")
2022-01-21 10:16:26 +00:00
2022-01-21 11:09:43 +00:00
#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))
2022-01-21 10:16:26 +00:00
2022-01-22 09:54:31 +00:00
def generateNavigationBar(lines):
global navbar
navbar = E
for line in lines:
2022-01-22 14:19:47 +00:00
title, link = line.strip().split(";", 1)
navbar = navbar.li(HTML("<a href='" + link + "'>" + title + "</a>"))
2022-01-22 09:54:31 +00:00
2022-01-22 14:06:25 +00:00
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))
2022-01-21 10:16:26 +00:00
if os.path.exists("./navbar"):
2022-01-21 11:09:43 +00:00
with open("./navbar", 'r') as navbarfile:
2022-01-22 09:54:31 +00:00
generateNavigationBar(navbarfile.readlines())
else:
2022-01-21 11:09:43 +00:00
print("No 'navbar' file found, there will be no navigation bar.")
2022-01-21 10:16:26 +00:00
2022-01-22 14:19:47 +00:00
pagescount = 0
2022-01-21 10:16:26 +00:00
for file in os.listdir("./"):
2022-01-21 11:09:43 +00:00
if file.endswith(".page"):
2022-01-22 09:26:09 +00:00
with open(file, 'r') as page:
2022-01-22 14:06:25 +00:00
generateLines(os.path.basename(file), page.readlines())
2022-01-22 14:19:47 +00:00
pagescount=pagescount+1
print("Generated " + str(pagescount) + " pages")