Start using html5tagger better

This commit is contained in:
Govindas 2022-01-22 11:26:09 +02:00
parent acf1a5a097
commit c28b3167a1
1 changed files with 21 additions and 20 deletions

View File

@ -1,5 +1,5 @@
import os import os
from html5tagger import Document from html5tagger import Document, E, HTML
from shutil import rmtree from shutil import rmtree
#if homepage is at Home.page, set homepage to "Home" #if homepage is at Home.page, set homepage to "Home"
@ -22,31 +22,32 @@ navbarfound = False
if os.path.exists("./navbar"): if os.path.exists("./navbar"):
with open("./navbar", 'r') as navbarfile: with open("./navbar", 'r') as navbarfile:
navbarfound = True navbarfound = True
navbar = "" navbar = E
for line in navbarfile.readlines(): for line in navbarfile.readlines():
split = line.strip().split(";", 1) split = line.strip().split(";", 1)
navbar = navbar + "<li><a href='" + split[1] + "'>" + split[0] + "</a></li>" navbar = navbar.li(HTML("<a href='" + split[1] + "'>" + split[0]))
else:
#checking false instead of else to be more sure
if navbarfound == False:
print("No 'navbar' file found, there will be no navigation bar.") print("No 'navbar' file found, there will be no navigation bar.")
#generate pages #generate pages
for file in os.listdir("./"): for file in os.listdir("./"):
if file.endswith(".page"): if file.endswith(".page"):
with open(file, 'r') as page: with open(file, 'r') as page:
pagetitle = os.path.basename(file).replace(".page", "") pagetitle = os.path.basename(file).replace(".page", "")
doc = Document(pagetitle, lang="en") doc = Document(pagetitle, lang="en")
for line in page.readlines(): for line in page.readlines():
doc.p(line.strip()) doc.p(line.strip())
#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 pagetitle == homepage: foldername = ""
foldername = "" if pagetitle =! homepage:
else: foldername = "/" + pagetitle.replace(" ", "-").lower()
foldername = "/" + pagetitle.replace(" ", "-").lower() os.mkdir("./website-output" + foldername)
os.mkdir("./website-output" + foldername)
with open("./website-output" + foldername + "/index.html", 'w') as newpage: with open("./website-output" + foldername + "/index.html", 'w') as newpage:
if navbarfound: if navbarfound:
newpage.write("<ul>" + navbar + "</ul>" + str(doc)) newpage.write(str(E.ul(navbar)))
else: else:
newpage.write(str(doc)) newpage.write(str(doc))