pnbp

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit d2e7cde8bb0c9b36d7be0eb5aacb6cc71b4863b3
parent e0ab5c33aa54b3428815606aaa7b51d6d578880d
Author: Paul Longtine <paullongtine@gmail.com>
Date:   Wed May 21 12:47:37 2014

Added includes, and buildSite()

Diffstat:
 .gitignore           |  3 +++
 compile.py           | 51 ++++++++++++++++++++++++++++++++++++++++-----------
 includes/header.html |  3 +++
 mod/blog.py          |  8 ++++----
 pages.json           |  4 ++--
 templates/post.html  |  4 ++--
 templates/std.html   |  7 +++++--
 7 files changed, 59 insertions(+), 21 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -15,3 +15,6 @@ emacs-config.el #python hooblab *.pyc + +#Don't want to upload the site, do we? +site/ diff --git a/compile.py b/compile.py @@ -5,14 +5,7 @@ ' Paul Longtine - paullongtine@gmail.com ' ''' -import mod -import json - -pages = open("pages.json") - -pagedata = json.load(pages) - -pages.close() +import os, shutil, mod, json def main(): print("Going through pages...") @@ -27,15 +20,22 @@ def main(): print("page '{}' using template '{}'...".format(name,v['template'])) site[name] = runMod(template,v['pagemod']) - - print site + + buildSite(site) # Adds in variables defined in pages.json # # t = raw template, var = "pagevar" variables in pages.json (<pagename> -> "pagevar") def generateTemplate(t,var): for search,replace in var.items(): - t = t.replace("%"+search,replace) + if search[0] == ":": + try: + inc = open(replace).read() + t = t.replace("%"+search+"%",inc) + except: + print("Can't open file '{}'".format(replace)) + else: + t = t.replace("%"+search+"%",replace) return t @@ -49,6 +49,35 @@ def runMod(t,var): return subpage +# Builds the site off of a filestructure dictionary. + +def buildSite(site): + try: + shutil.rmtree("./site/") + except: + print("No directory site/, ignoring") + + os.mkdir("./site/") + for page, subpages in site.items(): + if page == "index": + currentDir = "./site" + else: + currentDir = "./site/"+page + os.mkdir(currentDir) + + open(currentDir+"/index.html", "w").write(subpages['default']) + + for subdir, data in subpages.items(): + if subdir != "default": + os.mkdir(currentDir+"/"+subdir) + for page, content in data.items(): + os.mkdir(currentDir+"/"+subdir+"/"+page) + open(currentDir+"/"+subdir+"/"+page+"/index.html","w").write(content) + + if __name__ == "__main__": + pages = open("pages.json") + pagedata = json.load(pages) + pages.close() main() print("Finished.") diff --git a/includes/header.html b/includes/header.html @@ -0,0 +1,3 @@ +<div> + <h1>This is in fact not a blog.</h1> +</div> diff --git a/mod/blog.py b/mod/blog.py @@ -13,22 +13,22 @@ def getPages(template,settings,name): if 0 == 0 or int(data['post']) >= 0: a = generatePost(i,temp) + a - pages['default'] = template.replace("%"+name,a) + pages['default'] = template.replace("%"+name+"%",a) # Generates individual pages referenced by title pages['post'] = {} for i in data: post = generatePost(i,temp) - pages['post'][slug(i['title'])] = template.replace("%"+name,post) + pages['post'][slug(i['title'])] = template.replace("%"+name+"%",post) return pages def generatePost(data, post): for name,x in data.items(): if name == 'title': - post = post.replace("%titlelink",slug(x)) + post = post.replace("%titlelink%",slug(x)) - post = post.replace("%"+name, x) + post = post.replace("%"+name+"%", x) return post diff --git a/pages.json b/pages.json @@ -1,4 +1,4 @@ { -"index":{"template":"templates/std.html","pagevar":{"title":"This is a test page"},"pagemod":{"content":{"mod":"blog","settings":{"defaultPostCount":"0","data":"./data/index.json"}}}}, -"test":{"template":"templates/std.html","pagevar":{"title":"This is THE test page"},"pagemod":{"content":{"mod":"blog","settings":{"defaultPostCount":"1","data":"./data/test.json"}}}} +"index":{"template":"templates/std.html","pagevar":{":header":"./includes/header.html","title":"This is a test page"},"pagemod":{"content":{"mod":"blog","settings":{"defaultPostCount":"0","data":"./data/index.json"}}}}, +"test":{"template":"templates/std.html","pagevar":{":header":"./includes/header.html","title":"This is THE test page"},"pagemod":{"content":{"mod":"blog","settings":{"defaultPostCount":"1","data":"./data/test.json"}}}} } \ No newline at end of file diff --git a/templates/post.html b/templates/post.html @@ -1,4 +1,4 @@ <div> - <a href="?title=%titlelink"><h1>%title</h1></a> - <p>%content</p> + <a href="./post/%titlelink%"><h1>%title%</h1></a> + <p>%content%</p> </div> diff --git a/templates/std.html b/templates/std.html @@ -1,8 +1,11 @@ <html> <head> - <title>%title</title> + <title>%title%</title> </head> <body> - %content + %:header% + <div> + %content% + </div> </body> </html>