diff --git a/gsitegen/About.page b/gsitegen/About.page new file mode 100644 index 0000000..e718280 --- /dev/null +++ b/gsitegen/About.page @@ -0,0 +1 @@ +About page diff --git a/gsitegen/Home.page b/gsitegen/Home.page new file mode 100644 index 0000000..a40856f --- /dev/null +++ b/gsitegen/Home.page @@ -0,0 +1,9 @@ +# Welcome! +This is the homepage of our website.... +> + HTML Hello +Another markdown line +**Another markdown line 2** +> +

**HTML Italics test 1** +

[Test link](/about) diff --git a/gsitegen/generate.py b/gsitegen/generate.py index a48534d..f4367dd 100644 --- a/gsitegen/generate.py +++ b/gsitegen/generate.py @@ -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("" + title + "")) 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() diff --git a/gsitegen/navbar b/gsitegen/navbar new file mode 100644 index 0000000..e1de320 --- /dev/null +++ b/gsitegen/navbar @@ -0,0 +1,2 @@ +Home;/ +About;/about