Only change edited pages, W.I.P
This commit is contained in:
parent
e8d39ccb9e
commit
08f12551c5
1
gsitegen/About.page
Normal file
1
gsitegen/About.page
Normal file
@ -0,0 +1 @@
|
||||
About page
|
9
gsitegen/Home.page
Normal file
9
gsitegen/Home.page
Normal file
@ -0,0 +1,9 @@
|
||||
# Welcome!
|
||||
This is the homepage of our website....
|
||||
>
|
||||
<b>HTML Hello</b>
|
||||
Another markdown line
|
||||
<a href=/test>**Another markdown line 2**</a>
|
||||
>
|
||||
<p><i>**HTML Italics test 1**
|
||||
<p><i>[Test link](/about)
|
@ -3,6 +3,7 @@ from html5tagger import Document, E, HTML
|
||||
from shutil import rmtree
|
||||
import markdown
|
||||
import html
|
||||
from pathlib import Path
|
||||
|
||||
def generateNavigationBar(lines):
|
||||
global navbar
|
||||
@ -17,9 +18,6 @@ def generateNavigationBar(lines):
|
||||
#parse markdown
|
||||
if not rawhtml:
|
||||
navbar = navbar.li(HTML(parseMarkdown(navbar, line)))
|
||||
print(str(navbar))
|
||||
#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
|
||||
@ -45,6 +43,7 @@ def parseRawHTML(doc, line, htmlstring, rawhtml, id, maxlines):
|
||||
return rawhtml, doc, htmlstring
|
||||
|
||||
def parseMarkdown(doc, line):
|
||||
#do not allow HTML in markdown
|
||||
line = html.escape(line.strip())
|
||||
return markdown.markdown(line)
|
||||
|
||||
@ -65,42 +64,68 @@ def generateLines(title, lines):
|
||||
generatePage(title, doc)
|
||||
|
||||
def generatePage(title, doc):
|
||||
#creates ./website-output/pagetitle/index.html file if it is not homepage
|
||||
foldername = ""
|
||||
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)
|
||||
|
||||
global outputexists
|
||||
if not outputexists:
|
||||
try:
|
||||
os.mkdir("./website-output")
|
||||
outputexists = True
|
||||
except FileExistsError:
|
||||
print("Output directory already exists, let's clean it!")
|
||||
outputexists = True
|
||||
def writePages(homepage):
|
||||
global pages
|
||||
global titles
|
||||
|
||||
#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))
|
||||
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))
|
||||
|
||||
#not using else to be sure
|
||||
if outputexists:
|
||||
if title != homepage:
|
||||
foldername = "/" + title.replace(" ", "-").lower()
|
||||
os.mkdir("./website-output" + foldername)
|
||||
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()
|
||||
|
||||
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))
|
||||
#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])
|
||||
|
||||
def main():
|
||||
#if homepage is at Home.page, set homepage to "Home"
|
||||
global outputexists
|
||||
outputexists = False
|
||||
global homepage
|
||||
homepage = "Home"
|
||||
|
||||
if os.path.exists("./navbar"):
|
||||
@ -109,13 +134,16 @@ def main():
|
||||
else:
|
||||
print("No 'navbar' file found, there will be no navigation bar.")
|
||||
|
||||
#count total amount of pages first
|
||||
pagescount = 0
|
||||
for file in os.listdir("./"):
|
||||
if file.endswith(".page"):
|
||||
pagescount += 1
|
||||
with open(file, 'r') as page:
|
||||
generateLines(os.path.basename(file), page.readlines())
|
||||
pagescount += 1
|
||||
print("Generated " + str(pagescount) + " pages")
|
||||
print("Found " + str(pagescount) + " pages")
|
||||
|
||||
writePages(homepage)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
main()
|
||||
|
2
gsitegen/navbar
Normal file
2
gsitegen/navbar
Normal file
@ -0,0 +1,2 @@
|
||||
Home;/
|
||||
About;/about
|
Loading…
Reference in New Issue
Block a user