commit 680e736a2ed17eac80cda9503bbf3b1162ad6476
parent 65b238f6a3701d476de4b5dfe9bc4f5f6772006d
Author: Paul Longtine <paullongtine@gmail.com>
Date: Mon, 1 Sep 2014 21:18:50 -0400
second refactorbeam
Diffstat:
18 files changed, 330 insertions(+), 340 deletions(-)
diff --git a/example/includes/header.html b/example/includes/header.html
@@ -1,7 +1,7 @@
<div>
<h1>This is in fact not a blog. %page%</h1>
{:
-for i,x in sorted(pagedata.items(),reverse=True):
+for i,x in sorted(getConf().items(),reverse=True):
l = i
if page == "":page = "index"
if str(i) == page: c = "current"
diff --git a/src/core/__init__.py b/src/core/__init__.py
@@ -1,7 +1,48 @@
-#Dynamically imports all files in this directory
-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
+'''
+' pnbp - pnbp is not a blogging platform
+' __init__.py
+' Paul Longtine - paullongtine@gmail.com
+'
+' For documentation, please visit http://static.nanner.co/pnbp
+'''
+
+import os, sys, yaml
+
+import core.helper.cmd
+import core.builder
+
+def init(arg):
+ bd = cli(arg)
+
+ #Try to get the config
+ try:
+ pages = file("pages.yml")
+
+ except:
+ print("Can't open file 'pages.yml'")
+ sys.exit()
+
+ pagedata = yaml.load(pages)
+
+ core.builder.build(pagedata,bd)
+
+#CLI Interface function
+#args = list of command line arguementsn
+def cli(args):
+ bd = "site/"
+ for i in args:
+ if i[0] != "-" and args.index(i) != 0:
+ bd = i
+
+ elif i == "-d":
+ os.chdir(args.pop(args.index(i)+1))
+
+ elif i == "--help":
+ core.helper.cmd.phelp()
+ elif i == "--init" or i == "-i":
+ core.helper.cmd.init()
+
+ elif 0 != args.index(i):
+ print("Unknown option: {}".format(i))
+
+ return bd
diff --git a/src/core/build.py b/src/core/build.py
@@ -1,96 +0,0 @@
-'''
-' pnbp - pnbp is not a blogging platform
-' build.py
-' Paul Longtine - paullongtine@gmail.com
-'
-' For documentation, please visit http://static.nanner.co/pnbp
-'''
-import os, shutil, module
-
-# Builds the site off of a filestructure dictionary.
-#site = dict of site directory tree/pages, loc = root of site
-def write(site,loc):
- try:
- shutil.rmtree(loc)
-
- except:
- print("No directory {}, ignoring".format(loc))
-
- os.mkdir(loc)
- for page, subpages in site.items():
- currentDir = handleDirectory(page,loc)
-
- subpageLoop(subpages,currentDir)
-
- if loc[-1] != "/":
- loc = loc + "/"
- try:
- for i in os.listdir("data/static/"):
- try:
- shutil.copytree("data/static/"+i,loc+i)
-
- except:
- shutil.copy2("data/static/"+i,loc+i)
- except:
- print("No directory data/static, ignoring")
-
-# Handles directories
-#p = name of page, l = location
-def handleDirectory(p,l):
- if p == "index":
- if l[-1] == "/":
- r = l[0:-1]
-
- else:
- r = l
-
- else:
- if l[-1] == "/":
- r = l+p
-
- else:
- r = l+"/"+p
-
- try:
- os.mkdir(r)
-
- except:
- pass
-
- return r
-
-#Recursive loop through all subpages
-#d = dict of all subpages, cd = Current directory
-def subpageLoop(d,cur):
- for k, v in d.iteritems():
- if isinstance(v, dict):
- subpageLoop(v,cur + "/" + k)
- else:
- f = "index.html"
-
- if k == "default":
- k = ""
-
- elif k[0:4] == "php:":
- f = "{}.php".format(k[4:])
- k = ""
-
- else:
- k = k + "/"
-
- try:
- file("{}/{}{}".format(cur,k,f), "w").write(v)
-
- except:
- try:
- os.mkdir("{}".format(cur))
-
- except:
- pass
-
- try:
- os.mkdir("{}/{}".format(cur,k))
- except:
- pass
-
- file("{}/{}{}".format(cur,k,f), "w").write(v)
diff --git a/src/core/builder.py b/src/core/builder.py
@@ -0,0 +1,129 @@
+'''
+' pnbp - pnbp is not a blogging platform
+' builder.py
+' Paul Longtine - paullongtine@gmail.com
+'
+' For documentation, please visit http://static.nanner.co/pnbp
+'''
+import os, shutil
+
+import core.template
+import core.module
+
+#Builds the site off of a filestructure dictionary.
+#site = dict of site directory tree/pages, loc = root of site
+def makeSite(site,loc):
+ try:
+ shutil.rmtree(loc)
+
+ except:
+ print("No directory {}, ignoring".format(loc))
+
+ os.mkdir(loc)
+ for page, subpages in site.items():
+ currentDir = handleDirectory(page,loc)
+
+ subpageLoop(subpages,currentDir)
+
+ if loc[-1] != "/":
+ loc = loc + "/"
+ try:
+ for i in os.listdir("data/static/"):
+ try:
+ shutil.copytree("data/static/"+i,loc+i)
+
+ except:
+ shutil.copy2("data/static/"+i,loc+i)
+ except:
+ print("No directory data/static, ignoring")
+
+# Handles directories
+#p = name of page, l = location
+def handleDirectory(p,l):
+ if p == "index":
+ if l[-1] == "/":
+ r = l[0:-1]
+
+ else:
+ r = l
+
+ else:
+ if l[-1] == "/":
+ r = l+p
+
+ else:
+ r = l+"/"+p
+
+ try:
+ os.mkdir(r)
+
+ except:
+ pass
+
+ return r
+
+#Recursive loop through all subpages
+#d = dict of all subpages, cd = Current directory
+def subpageLoop(d,cur):
+ for k, v in d.iteritems():
+ if isinstance(v, dict):
+ subpageLoop(v,cur + "/" + k)
+ else:
+ f = "index.html"
+
+ if k == "default":
+ k = ""
+
+ elif k[0:4] == "php:":
+ f = "{}.php".format(k[4:])
+ k = ""
+
+ else:
+ k = k + "/"
+
+ try:
+ file("{}/{}{}".format(cur,k,f), "w").write(v)
+
+ except:
+ try:
+ os.mkdir("{}".format(cur))
+
+ except:
+ pass
+
+ try:
+ os.mkdir("{}/{}".format(cur,k))
+ except:
+ pass
+
+ file("{}/{}{}".format(cur,k,f), "w").write(v)
+
+def build(pd,directory):
+ site = {}
+ for name,v in pd.items():
+ #Read the template
+ if 'template' in v:
+ try:
+ temp = file(v['template']).read()
+
+ except:
+ print("{}: Can't open file '{}'".format(name,v['template']))
+ sys.exit()
+
+ else:
+ temp = ""
+
+ #Check if pagevar is defined, skip the variable replacement step
+ if 'pagevar' in v:
+ temp = core.template.generate(temp,v['pagevar'],name)
+
+ else:
+ temp = core.template.run(temp,name)
+
+ print("Running modules for page: '"+name+"'")
+
+ site[name] = core.module.run(temp,v,name)
+
+ print("Built page: '"+ name +"'\n")
+
+ makeSite(site,directory)
diff --git a/src/core/functions.py b/src/core/functions.py
@@ -1,40 +0,0 @@
-'''
-' pnbp - pnbp is not a blogging platform
-' functions.py
-' Paul Longtine - paullongtine@gmail.com
-'
-' For documentation, please visit http://static.nanner.co/pnbp
-'''
-# Functions file, used for inline scripts
-import time
-
-
-#Return the current date in the format sepecified in config
-def now(config):
- return time.strftime(config)
-
-#Return an HTML list. example format: {'root':"<ul class=\"special\">%li%</ul>",'li':"<li class=\"list\">%content%</li>"}
-def list(things,formats={}):
- formats['root'] = formats.get("root","<ul>%li%</ul>")
- formats['li'] = formats.get("li","<li>%content%</li>")
- li = ""
- for thing in things:
- li = li + formats['li'].replace("%content%",thing)
-
- return formats['root'].replace("%li%",li)
-
-# slug(string -> "hi's") -> his- removes all "unwanted" characters and creates a URL-friendly slug
-def slug(string):
- invalidChars = [
- "<",">","#","%","{","}",
- "|","\\","^","[","]","`",
- "'",";","/","?",":","@",
- "&","+",",","."
- ]
- for x in invalidChars:
- string = string.replace(x, "")
-
- string = string.replace(" ","_")
- return string.lower()
-
-#
diff --git a/src/core/helper/__init__.py b/src/core/helper/__init__.py
diff --git a/src/core/helper/cmd.py b/src/core/helper/cmd.py
@@ -0,0 +1,24 @@
+'''
+' pnbp - pnbp is not a blogging platform
+' cmd.py
+' Paul Longtine - paullongtine@gmail.com
+'
+' For documentation, please visit http://static.nanner.co/pnbp
+'''
+
+basicConfig = "index:\n template: \"template.html\"\n pagevar:\n title: \"I'm basic\"\n pagemod:\n page:\n mod: \"page\""
+basicTemplate = "<html>\n <body>\n <h1>%title%</h1>\n </body>\n</html>"
+
+def init():
+ file("pages.yml","w").write(basicConfig)
+ file("template.html","w").write(basicTemplate)
+
+def phelp():
+ print(
+ "Usage: build [OPTION(s)]... [DIR]...\n"
+ "Build site in DIR using configuration in pwd\n"
+ "\n"
+ " -d DIR Use configuration in DIR, when not specified DIR is 'site/'\n"
+ " -i, --init Make a new site using the bare minimium config and build it in DIR\n"
+ " --help Display this help and exit\n"
+ )
diff --git a/src/core/helper/functions.py b/src/core/helper/functions.py
@@ -0,0 +1,42 @@
+'''
+' pnbp - pnbp is not a blogging platform
+' functions.py
+' Paul Longtine - paullongtine@gmail.com
+'
+' For documentation, please visit http://static.nanner.co/pnbp
+'''
+# Functions file, used for inline scripts
+import time, yaml
+
+
+#Return the current date in the format sepecified in config
+def now(config):
+ return time.strftime(config)
+
+#Return an HTML list. example format: {'root':"<ul class=\"special\">%li%</ul>",'li':"<li class=\"list\">%content%</li>"}
+def list(things,formats={}):
+ formats['root'] = formats.get("root","<ul>%li%</ul>")
+ formats['li'] = formats.get("li","<li>%content%</li>")
+ li = ""
+ for thing in things:
+ li = li + formats['li'].replace("%content%",thing)
+
+ return formats['root'].replace("%li%",li)
+
+# slug(string -> "hi's") -> his- removes all "unwanted" characters and creates a URL-friendly slug
+def slug(string):
+ invalidChars = [
+ "<",">","#","%","{","}",
+ "|","\\","^","[","]","`",
+ "'",";","/","?",":","@",
+ "&","+",",","."
+ ]
+ for x in invalidChars:
+ string = string.replace(x, "")
+
+ string = string.replace(" ","_")
+ return string.lower()
+
+#Returns config
+def getConf():
+ return yaml.load(file("pages.yml"))
diff --git a/src/core/initbasic.py b/src/core/initbasic.py
@@ -1,15 +0,0 @@
-'''
-' pnbp - pnbp is not a blogging platform
-' initbasic.py
-' Paul Longtine - paullongtine@gmail.com
-'
-' For documentation, please visit http://static.nanner.co/pnbp
-'''
-basicConfig = "index:\n template: \"template.html\"\n pagevar:\n title: \"I'm basic\"\n pagemod:\n page:\n mod: \"page\""
-basicTemplate = "<html>\n <body>\n <h1>%title%</h1>\n </body>\n</html>"
-
-def init():
- file("pages.yml","w").write(basicConfig)
- file("template.html","w").write(basicTemplate)
-
-
diff --git a/src/core/modtool.py b/src/core/modtool.py
@@ -1,83 +0,0 @@
-'''
-' pnbp - pnbp is not a blogging platform
-' modtool.py
-' Paul Longtine - paullongtine@gmail.com
-'
-' For documentation, please visit http://static.nanner.co/pnbp
-'''
-
-import sys, module, template
-
-# Built-in module, generates page as subpage
-def modPage(t,var,data,name,page):
- if 'settings' in data:
- try:
- if 'template' in data['settings']:
- temp = file(data['settings']['template']).read()
-
- except:
- print("Error occured at {} using module page".format(page))
- print("Cannot open file {}".format(data['settings']['template']))
- sys.exit()
-
- else:
- temp = t
-
- if 'pagevar' in var:
- if 'settings' in data:
- if 'pagevar' in data['settings']:
- var['pagevar'].update(data['settings']['pagevar'])
-
- temp = template.generate(temp,var['pagevar'],name)
-
- else:
- temp = template.run(template,name)
-
- if not 'settings' == data:
- t = {'default':temp}
-
- else:
- if 'location' in meta:
- t = {data['settings']['location']:{'default':temp}}
-
- return t
-
-# Gets subpages from module specified in data
-def getSubpages(t,var,data,name,page):
- returns = {}
- if not "settings" in data:
- data['settings'] = {}
-
- try:
- returns = getattr(module, data['mod']).getPages(t, data['settings'], name, page)
-
- except Exception,e:
- print("Error occured at {} using module {}:".format(page,data['mod']))
- if type(e) == KeyError:
- print("Missing attribute {}".format(e))
- sys.exit()
-
- else:
- print(e)
-
- return returns
-
-# Runs modules defined in pages.json
-#
-# t = raw template, var = "pagemod" variables in pages.json (<pagename> -> "pagemod")
-def runMod(t,var,page):
- subpage = {}
- for name, meta in var['pagemod'].items():
- if meta['mod'] == "page":
- subpage.update(
- modPage(t,var,meta,name,page)
- )
-
- else:
- subpage.update(
- getSubpages(t,var,meta,name,page)
- )
-
- return subpage
-
-
diff --git a/src/core/module.py b/src/core/module.py
@@ -0,0 +1,81 @@
+'''
+' pnbp - pnbp is not a blogging platform
+' module.py
+' Paul Longtine - paullongtine@gmail.com
+'
+' For documentation, please visit http://static.nanner.co/pnbp
+'''
+
+import sys, modules, template
+
+# Built-in module, generates page as subpage
+def modPage(t,var,data,name,page):
+ if 'settings' in data:
+ try:
+ if 'template' in data['settings']:
+ temp = file(data['settings']['template']).read()
+
+ except:
+ print("Error occured at {} using module page".format(page))
+ print("Cannot open file {}".format(data['settings']['template']))
+ sys.exit()
+
+ else:
+ temp = t
+
+ if 'pagevar' in var:
+ if 'settings' in data:
+ if 'pagevar' in data['settings']:
+ var['pagevar'].update(data['settings']['pagevar'])
+
+ temp = template.generate(temp,var['pagevar'],name)
+
+ else:
+ temp = template.run(template,name)
+
+ if not 'settings' == data:
+ t = {'default':temp}
+
+ else:
+ if 'location' in meta:
+ t = {data['settings']['location']:{'default':temp}}
+
+ return t
+
+# Gets subpages from module specified in data
+def getSubpages(t,var,data,name,page):
+ returns = {}
+ if not "settings" in data:
+ data['settings'] = {}
+
+ try:
+ returns = getattr(modules, data['mod']).getPages(t, data['settings'], name, page)
+
+ except Exception,e:
+ print("Error occured at {} using module {}:".format(page,data['mod']))
+ if type(e) == KeyError:
+ print("Missing attribute {}".format(e))
+ sys.exit()
+
+ else:
+ print(e)
+
+ return returns
+
+# Runs modules defined in pages.json
+#
+# t = raw template, var = "pagemod" variables in pages.json (<pagename> -> "pagemod")
+def run(t,var,page):
+ subpage = {}
+ for name, meta in var['pagemod'].items():
+ if meta['mod'] == "page":
+ subpage.update(
+ modPage(t,var,meta,name,page)
+ )
+
+ else:
+ subpage.update(
+ getSubpages(t,var,meta,name,page)
+ )
+
+ return subpage
diff --git a/src/core/module/__init__.py b/src/core/module/__init__.py
@@ -1,7 +0,0 @@
-#Dynamically imports all files in this directory
-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
diff --git a/src/core/__init__.py b/src/core/modules/__init__.py
diff --git a/src/core/module/admin.py b/src/core/modules/admin.py
diff --git a/src/core/module/blog.py b/src/core/modules/blog.py
diff --git a/src/core/template.py b/src/core/template.py
@@ -7,10 +7,7 @@
'''
import re, json, yaml
-from functions import *
-
-try: pagedata = yaml.load(file("pages.yml"))
-except: pass
+from helper.functions import *
# Adds in variables defined in pages.json
#
diff --git a/src/init.py b/src/init.py
@@ -1,12 +1,13 @@
#!/usr/bin/python
'''
' pnbp - pnbp is not a blogging platform
-' main.py
+' init.py
' Paul Longtine - paullongtine@gmail.com
'
' For documentation, please visit http://static.nanner.co/pnbp
'''
-import sys, main
+import sys
+import core
from time import time
if __name__ == "__main__":
@@ -14,7 +15,7 @@ if __name__ == "__main__":
start = time()
#Try to build the site
- main.build(sys.argv)
+ core.init(sys.argv)
#Print the time it took to build the site
print("Finished in {} ms.".format((time()-start)*1000))
diff --git a/src/main.py b/src/main.py
@@ -1,84 +0,0 @@
-'''
-' pnbp - pnbp is not a blogging platform
-' main.py
-' Paul Longtine - paullongtine@gmail.com
-'
-' For documentation, please visit http://static.nanner.co/pnbp
-'''
-
-import os, sys, yaml
-
-#Helper imports
-import core
-
-def build(arg):
- bd = cli(arg)
-
- #Try to get the config
- try: pages = file("pages.yml")
- except:
- print("Can't open file 'pages.yml'")
- sys.exit()
-
- pagedata = yaml.load(pages)
-
- site = {}
- #Loops through defined "sites"
- for name,v in pagedata.items():
- #Read the template
- if 'template' in v:
- try: temp = file(v['template']).read()
- except:
- print("{}: Can't open file '{}'".format(name,v['template']))
- sys.exit()
- else:
- temp = ""
-
- #Check if pagevar is defined, skip the variable replacement step
- if 'pagevar' in v:
- temp = core.template.generate(temp,v['pagevar'],name)
-
- else:
- temp = core.template.run(temp,name)
-
- print("Running modules for page: '"+name+"'")
-
- site[name] = core.modtool.runMod(temp,v,name)
-
- print("Built page: '"+ name +"'\n")
-
- core.build.write(site,bd)
-
-#CLI Interface function
-#args = list of command line arguementsn
-def cli(args):
- bd = "site/"
- for i in args:
- if i[0] != "-" and args.index(i) != 0:
- bd = i
-
- elif i == "-d":
- try:
- os.chdir(args.pop(args.index(i)+1))
- except:
- pass
-
- elif i == "--help":
- print("Usage: build [OPTION(s)]... [DIR]...\n"
- "Build site in DIR using configuration in pwd\n"
- "\n"
- " -d DIR Use configuration in DIR, when not specified DIR is 'site/'\n"
- " -i, --init Make a new site using the bare minimium config and build it in DIR\n"
- " --help Display this help and exit\n")
-
- sys.exit()
-
- elif i == "--init" or i == "-i":
- core.initbasic.init()
-
- elif 0 != args.index(i):
- print("Unknown option: {}".format(i))
-
- return bd
-
-