pnbp

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

commit bcfc664caf431a87303e6c86447af3081c882a7f
parent 0dda7fb33d42a2d7db12f82094f97dd76df72ee2
Author: Paul Longtine <paullongtine@gmail.com>
Date:   Thu May 22 10:30:25 2014

dates are now working properly

Diffstat:
 compile.py           | 14 ++++++++++++--
 data/index.json      |  2 +-
 data/test.json       |  2 +-
 includes/header.html |  4 ++--
 mod/__init__.py      |  7 +------
 mod/blog.py          | 39 +++++++++++++++++++++++++++++++++++----
 templates/post.html  |  1 +
 7 files changed, 53 insertions(+), 16 deletions(-)

diff --git a/compile.py b/compile.py @@ -17,7 +17,7 @@ def main(): except: print("Can't open file '{}'".format(v['template'])) - template = generateTemplate(template,v['pagevar']) + template = generateTemplate(template,v['pagevar'],name) site[name] = runMod(template,v['pagemod'],name) @@ -26,14 +26,24 @@ def main(): # Adds in variables defined in pages.json # # t = raw template, var = "pagevar" variables in pages.json (<pagename> -> "pagevar") -def generateTemplate(t,var): +def generateTemplate(t,var,page): + if page == "index": + page = "" + + t = t.replace("%page%",page) for search,replace in var.items(): if search[0] == ":": try: inc = open(replace).read() + inc = inc.replace("%page%", page) + for subsearch,subreplace in var.items(): + inc = inc.replace("%"+subsearch+"%",subreplace) + t = t.replace("%"+search+"%",inc) + except: print("Can't open file '{}'".format(replace)) + else: t = t.replace("%"+search+"%",replace) diff --git a/data/index.json b/data/index.json @@ -1 +1 @@ -[{"post":"0","title":"I don't know what I'm doing","content":"Hello! How're you tuhday?"},{"post":"1","title":"I might have an idea of what I'm doing.","content":"Uh. hi."}]- \ No newline at end of file +[{"post":"0","date":"1997-10-3","title":"I don't know what I'm doing","content":"Hello! How're you tuhday?"},{"post":"1","date":"1998-9-13","title":"I might have an idea of what I'm doing.","content":"Uh. hi."}]+ \ No newline at end of file diff --git a/data/test.json b/data/test.json @@ -1 +1 @@ -[{"post":"0","title":"I don't know what I'm doing","content":"Hello! How're you tuhday?"},{"post":"1","title":"I might have an idea of what I'm doing.","content":"Uh. hi."}]- \ No newline at end of file +[{"post":"0","date":"1997-10-3","title":"I don't know what I'm doing","content":"Hello! How're you tuhday?"},{"post":"1","date":"1998-9-13","title":"I might have an idea of what I'm doing.","content":"Uh. hi."}]+ \ No newline at end of file diff --git a/includes/header.html b/includes/header.html @@ -1,5 +1,5 @@ <div> - <h1>This is in fact not a blog.</h1> + <h1>This is in fact not a blog. %page%</h1> <a href="/">Home</a>&nbsp;<a href="/test/">Test</a><br/> - <a href="./all">all</a> + <a href="/%page%/all">all</a> </div> diff --git a/mod/__init__.py b/mod/__init__.py @@ -1,7 +1,2 @@ #Imports all mods from mod/ -import os -for module in os.listdir(os.path.dirname(__file__)): - if module == '__init__.py' or module[-3:] != '.py': - continue - __import__(module[:-3], locals(), globals()) -del module +import blog diff --git a/mod/blog.py b/mod/blog.py @@ -1,4 +1,4 @@ -import json +import json, time def getPages(template,settings,name,page): pages = {} @@ -44,19 +44,50 @@ def generatePost(data, post, page): page = page + "/" post = post.replace("%titlelink%","/"+page+"post/"+slug(x)) + post = post.replace("%"+name+"%", x) - post = post.replace("%"+name+"%", x) + elif name == 'date': + config = getConfig("%date:",post) + if config == "none": + post = post.replace("%date:none%",x) + else: + post = post.replace( + "%date:"+config+"%", + time.strftime(config.replace("&","%"),time.strptime(x,"%Y-%m-%d"))) + + + else: + post = post.replace("%"+name+"%", x) return post # Helper functions -# slug("hi's") -> his- removes all "unwanted" characters and creates a URL-friendly slug +# slug(string -> "hi's") -> his- removes all "unwanted" characters and creates a URL-friendly slug def slug(string): - invalidChars = ["<",">","#","%","{","}","|","\\","^","[","]","`","'",";","/","?",":","@","&","+",",","."] + invalidChars = [ + "<",">","#","%","{","}", + "|","\\","^","[","]","`", + "'",";","/","?",":","@", + "&","+",",","." + ] for x in invalidChars: string = string.replace(x, "") string = string.replace(" ","_") return string.lower() + +# getConfig(string -> index, string -> data) -> gets "config" data ex. (%blah:<config>%) +def getConfig(index,data): + retVal = "" + try: + pointer = data.index(index)+len(index) + except: + retVal = "-1" + + while data[pointer] != "%" and retVal != "-1": + retVal = retVal + data[pointer] + pointer += 1 + + return retVal diff --git a/templates/post.html b/templates/post.html @@ -1,4 +1,5 @@ <div> <a href="%titlelink%"><h1>%title%</h1></a> + <p>%date:&B, &d &Y%</p> <p>%content%</p> </div>