SiteGenerator/gsitegen/generate.py

150 lines
4.1 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
2022-01-24 10:09:04 +00:00
import markdown
import html
2022-01-26 10:03:25 +00:00
from pathlib import Path
2022-01-21 10:16:26 +00:00
2022-01-22 09:54:31 +00:00
def generateNavigationBar(lines):
global navbar
navbar = E
2022-01-25 14:05:07 +00:00
rawhtml = False
htmlstring = ""
2022-01-24 13:20:08 +00:00
for id, line in enumerate(lines):
2022-01-24 10:35:29 +00:00
2022-01-25 14:05:07 +00:00
#parse raw HTML
rawhtml, navbar, htmlstring = parseRawHTML(navbar, line, htmlstring, rawhtml, id, len(lines))
#parse markdown
if not rawhtml:
navbar = navbar.li(HTML(parseMarkdown(navbar, line)))
2022-01-22 09:54:31 +00:00
2022-01-24 13:20:08 +00:00
def parseRawHTML(doc, line, htmlstring, rawhtml, id, maxlines):
#raw html start
if line.strip() == ">":
rawhtml = True
htmlstring = ""
#parse indented raw html
elif rawhtml:
if line.startswith(" "):
#experimental markdown inside HTML support
htmlstring = htmlstring + html.unescape(markdown.markdown(html.escape(line.strip())))
2022-01-24 13:20:08 +00:00
#if indented html was the last line, this is needed for it to not be ignored
2022-01-24 14:06:16 +00:00
#since this is the end of the file, we will not set rawhtml to False.
2022-01-24 13:20:08 +00:00
if maxlines - id == 1:
doc.div(HTML(htmlstring))
else:
rawhtml = False
2022-01-24 13:20:08 +00:00
doc.div(HTML(htmlstring))
htmlstring = ""
return rawhtml, doc, htmlstring
def parseMarkdown(doc, line):
2022-01-26 10:03:25 +00:00
#do not allow HTML in markdown
line = html.escape(line.strip())
return markdown.markdown(line)
2022-01-22 14:06:25 +00:00
def generateLines(title, lines):
title = title.replace(".page", "")
doc = Document(title, lang="en")
2022-01-24 10:09:04 +00:00
rawhtml = False
htmlstring = ""
2022-01-24 13:20:08 +00:00
for id, line in enumerate(lines):
2022-01-24 10:09:04 +00:00
#parse raw HTML
2022-01-24 13:20:08 +00:00
rawhtml, doc, htmlstring = parseRawHTML(doc, line, htmlstring, rawhtml, id, len(lines))
2022-01-24 10:09:04 +00:00
#parse markdown
if not rawhtml:
doc = doc(HTML(parseMarkdown(doc, line)))
2022-01-24 10:09:04 +00:00
generatePage(title, doc)
2022-01-22 14:06:25 +00:00
def generatePage(title, doc):
2022-01-26 10:03:25 +00:00
global pages
global titles
global pagescount
if 'pages' not in globals():
pages = []
if 'titles' not in globals():
titles = []
if 'navbar' in globals():
pages.append(str(E.ul(navbar)) + str(doc))
titles.append(title)
else:
pages.append(str(doc))
titles.append(title)
def writePages(homepage):
global pages
global titles
try:
os.mkdir("./website-output")
except FileExistsError:
pass
print("Output directory already exists, let's clean it!")
#TODO only delete files that aren't present in newest site generation
#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))
for id, page in enumerate(pages):
foldername = ""
#creates ./website-output/pagetitle/index.html file if it is not homepage
if titles[id] != homepage:
foldername = "/" + titles[id].replace(" ", "-").lower()
#doing this here, because homepage directory is already created above
try:
os.mkdir("./website-output" + foldername)
except FileExistsError:
pass
changed = False
filepath = Path("./website-output" + foldername + "/index.html")
if filepath.exists():
with open("./website-output" + foldername + "/index.html", 'r') as newpage:
if newpage.read() != page:
changed = True
else:
changed = True
if changed:
with open("./website-output" + foldername + "/index.html", 'w') as newpage:
newpage.write(page)
print("Written changed page: " + titles[id])
else:
print("Page not changed: " + titles[id])
2022-01-24 16:49:02 +00:00
def main():
#if homepage is at Home.page, set homepage to "Home"
homepage = "Home"
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.")
2022-01-26 10:03:25 +00:00
#count total amount of pages first
2022-01-24 16:49:02 +00:00
pagescount = 0
for file in os.listdir("./"):
if file.endswith(".page"):
2022-01-26 10:03:25 +00:00
pagescount += 1
2022-01-24 16:49:02 +00:00
with open(file, 'r') as page:
generateLines(os.path.basename(file), page.readlines())
2022-01-26 10:03:25 +00:00
print("Found " + str(pagescount) + " pages")
writePages(homepage)
2022-01-24 16:49:02 +00:00
if __name__ == "__main__":
2022-01-26 10:03:25 +00:00
main()