More code cleanup

This commit is contained in:
Govindas 2022-01-26 12:36:59 +02:00
parent 2d6058d451
commit 253851b871
2 changed files with 12 additions and 13 deletions

View File

@ -1,5 +1,5 @@
# Welcome! # Welcome!
This is the homepage of our website.... This is the homepage of our website.....
> >
<b>HTML Hello</b> <b>HTML Hello</b>
Another markdown line Another markdown line

View File

@ -20,6 +20,7 @@ def generateNavigationBar(lines):
navbar = navbar.li(HTML(parseMarkdown(navbar, line))) navbar = navbar.li(HTML(parseMarkdown(navbar, line)))
def parseRawHTML(doc, line, htmlstring, rawhtml, id, maxlines): def parseRawHTML(doc, line, htmlstring, rawhtml, id, maxlines):
#raw html start #raw html start
if line.strip() == ">": if line.strip() == ">":
rawhtml = True rawhtml = True
@ -43,6 +44,7 @@ def parseRawHTML(doc, line, htmlstring, rawhtml, id, maxlines):
return rawhtml, doc, htmlstring return rawhtml, doc, htmlstring
def parseMarkdown(doc, line): def parseMarkdown(doc, line):
#do not allow HTML in markdown #do not allow HTML in markdown
line = html.escape(line.strip()) line = html.escape(line.strip())
return markdown.markdown(line) return markdown.markdown(line)
@ -74,10 +76,9 @@ def generatePage(title, doc):
if 'navbar' in globals(): if 'navbar' in globals():
pages.append(str(E.ul(navbar)) + str(doc)) pages.append(str(E.ul(navbar)) + str(doc))
titles.append(title)
else: else:
pages.append(str(doc)) pages.append(str(doc))
titles.append(title) titles.append(title)
def writePages(homepage): def writePages(homepage):
global pages global pages
@ -97,6 +98,7 @@ def writePages(homepage):
for id, page in enumerate(pages): for id, page in enumerate(pages):
foldername = "" foldername = ""
#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 titles[id] != homepage: if titles[id] != homepage:
foldername = "/" + titles[id].replace(" ", "-").lower() foldername = "/" + titles[id].replace(" ", "-").lower()
@ -106,23 +108,19 @@ def writePages(homepage):
os.mkdir("./website-output" + foldername) os.mkdir("./website-output" + foldername)
except FileExistsError: except FileExistsError:
pass pass
changed = True
filepath = Path("./website-output" + foldername + "/index.html") filepath = Path("./website-output" + foldername + "/index.html")
if filepath.exists(): if filepath.exists():
with open("./website-output" + foldername + "/index.html", 'r') as newpage: with open("./website-output" + foldername + "/index.html", 'r') as newpage:
if newpage.read() == page: if newpage.read() == page:
changed = False print("Page not changed: " + titles[id])
continue
if changed: with open("./website-output" + foldername + "/index.html", 'w') as newpage:
with open("./website-output" + foldername + "/index.html", 'w') as newpage: newpage.write(page)
newpage.write(page) print("Written changed page: " + titles[id])
print("Written changed page: " + titles[id])
else:
print("Page not changed: " + titles[id])
def main(): def main():
#if homepage is at Home.page, set homepage to "Home" #if homepage is at Home.page, set homepage to "Home"
homepage = "Home" homepage = "Home"
@ -141,6 +139,7 @@ def main():
generateLines(os.path.basename(file), page.readlines()) generateLines(os.path.basename(file), page.readlines())
print("Found " + str(pagescount) + " pages") print("Found " + str(pagescount) + " pages")
#write all pages to files
writePages(homepage) writePages(homepage)
if __name__ == "__main__": if __name__ == "__main__":