From c5cf7e970d7792042e65aa6081f3dd826f1ce5d1 Mon Sep 17 00:00:00 2001 From: Govindas Date: Sat, 22 Jan 2022 16:06:25 +0200 Subject: [PATCH] Functions --- generate.py | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/generate.py b/generate.py index dee34dd..987a0ef 100644 --- a/generate.py +++ b/generate.py @@ -17,7 +17,6 @@ except FileExistsError: for d in dirs: rmtree(os.path.join(root, d)) -#generate navigation bar def generateNavigationBar(lines): global navbar navbar = E @@ -25,29 +24,37 @@ def generateNavigationBar(lines): split = line.strip().split(";", 1) navbar = navbar.li(HTML("" + split[0] + "")) +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.") -#generate pages for file in os.listdir("./"): if file.endswith(".page"): with open(file, 'r') as page: - pagetitle = os.path.basename(file).replace(".page", "") - doc = Document(pagetitle, lang="en") - for line in page.readlines(): - doc.p(line.strip()) - - #creates ./website-output/pagetitle/index.html file if it is not homepage - foldername = "" - if pagetitle != homepage: - foldername = "/" + pagetitle.replace(" ", "-").lower() - os.mkdir("./website-output" + foldername) - print(foldername) - with open("./website-output" + foldername + "/index.html", 'w') as newpage: - if 'navbar' in globals(): - newpage.write(str(E.ul(navbar)) + str(doc)) - else: - newpage.write(str(doc)) + generateLines(os.path.basename(file), page.readlines())