Setup Python project template.
This commit is contained in:
0
gsitegen/__init__.py
Normal file
0
gsitegen/__init__.py
Normal file
115
gsitegen/generate.py
Normal file
115
gsitegen/generate.py
Normal file
@@ -0,0 +1,115 @@
|
||||
import os
|
||||
from html5tagger import Document, E, HTML
|
||||
from shutil import rmtree
|
||||
import markdown
|
||||
import html
|
||||
|
||||
|
||||
|
||||
def generateNavigationBar(lines):
|
||||
global navbar
|
||||
navbar = E
|
||||
rawhtml = False
|
||||
htmlstring = ""
|
||||
for id, line in enumerate(lines):
|
||||
|
||||
#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)))
|
||||
#title, link = line.strip().split(";", 1)
|
||||
#navbar = navbar.li(HTML("<a href='" + link + "'>" + title + "</a>"))
|
||||
|
||||
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())))
|
||||
|
||||
#if indented html was the last line, this is needed for it to not be ignored
|
||||
#since this is the end of the file, we will not set rawhtml to False.
|
||||
if maxlines - id == 1:
|
||||
doc.div(HTML(htmlstring))
|
||||
else:
|
||||
rawhtml = False
|
||||
doc.div(HTML(htmlstring))
|
||||
htmlstring = ""
|
||||
return rawhtml, doc, htmlstring
|
||||
|
||||
def parseMarkdown(doc, line):
|
||||
line = html.escape(line.strip())
|
||||
return doc(HTML(markdown.markdown(line)))
|
||||
|
||||
def generateLines(title, lines):
|
||||
title = title.replace(".page", "")
|
||||
doc = Document(title, lang="en")
|
||||
rawhtml = False
|
||||
htmlstring = ""
|
||||
for id, line in enumerate(lines):
|
||||
|
||||
#parse raw HTML
|
||||
rawhtml, doc, htmlstring = parseRawHTML(doc, line, htmlstring, rawhtml, id, len(lines))
|
||||
|
||||
#parse markdown
|
||||
if not rawhtml:
|
||||
doc = parseMarkdown(doc, line)
|
||||
|
||||
generatePage(title, doc)
|
||||
|
||||
def generatePage(title, doc):
|
||||
#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)) + str(doc))
|
||||
else:
|
||||
newpage.write(str(doc))
|
||||
|
||||
|
||||
def main():
|
||||
#if homepage is at Home.page, set homepage to "Home"
|
||||
global homepage
|
||||
homepage = "Home"
|
||||
|
||||
try:
|
||||
os.mkdir("./website-output")
|
||||
except FileExistsError:
|
||||
print("Output directory already exists, let's clean it!")
|
||||
|
||||
#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))
|
||||
|
||||
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.")
|
||||
|
||||
pagescount = 0
|
||||
for file in os.listdir("./"):
|
||||
if file.endswith(".page"):
|
||||
with open(file, 'r') as page:
|
||||
generateLines(os.path.basename(file), page.readlines())
|
||||
pagescount += 1
|
||||
print("Generated " + str(pagescount) + " pages")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user