pnbp

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

commit 83cc682c40674c57cc6827d039b45c5a6ff0f29d
parent 3d4e98659436f0300103160205c5bddb98e68722
Author: Paul Longtine <paullongtine@gmail.com>
Date:   Sun Aug 31 01:01:47 2014

added support for an admin page

Diffstat:
 src/buildsite.py         |  10 +-
 src/core.py              | 208 +-----------------------------------------------
 src/module/.admin.py.swp | Bin 0 -> 20480 bytes
 src/module/admin.py      | 160 +++++++++++++++++++++++++++++++++++++-
 4 files changed, 174 insertions(+), 204 deletions(-)

diff --git a/src/buildsite.py b/src/buildsite.py @@ -59,14 +59,20 @@ def subpageLoop(d,currentDir): if isinstance(v, dict): subpageLoop(v,currentDir + "/" + 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("{}/{}index.html".format(currentDir,k), "w").write(v) + file("{}/{}{}".format(currentDir,k,f), "w").write(v) except: try: @@ -80,4 +86,4 @@ def subpageLoop(d,currentDir): except: pass - file("{}/{}index.html".format(currentDir,k), "w").write(v) + file("{}/{}{}".format(currentDir,k,f), "w").write(v) diff --git a/src/core.py b/src/core.py @@ -142,7 +142,7 @@ def getSubpages(t,var,data,name,page): else: print(e) - + return returns # Runs modules defined in pages.json @@ -180,210 +180,14 @@ def build(arg): #Loops through defined "sites" for name,v in pagedata.items(): #Read the template - try: template = file(v['template']).read() - except: - print("{}: Can't open file '{}'".format(name,v['template'])) - sys.exit() - - #Check if pagevar is defined, skip the variable replacement step - if 'pagevar' in v: - template = generateTemplate(template,v['pagevar'],name) - - else: - template = runInlineScript(template,name) - - print("Running modules for page: '"+name+"'") - site[name] = runMod(template,v,name) - print("Built page: '"+ name +"'\n") - - buildSite(site,bd) -''' -' pnbp - pnbp is not a blogging platform -' core.py -' Paul Longtine - paullongtine@gmail.com -' -' For documentation, please visit http://static.nanner.co/pnbp -''' -#Core imports -import os, sys, json, yaml, re - -#Helper imports -import module -from buildsite import * -from functions import * -from initbasic import * - -#Global variables - -pages = "" -pagedata = {} - -#CLI Interface function -#args = list of command line arguementsn -def cli(args): - bd = "site/" - if len(args) > 1: - 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 0 != args.index(i): - print("Unknown option: {}".format(i)) - - if "--init" in args or "-i" in args: - init() - - return bd - -# Adds in variables defined in pages.json -# -# t = raw template, var = "pagevar" variables in pages.json (<pagename> -> "pagevar") -def generateTemplate(t,var,page): - if page == "index": - page = "" - - t = t.replace("%page%",page) - t = runInlineScript(t,page) - - for search,replace in var.items(): - if search[0] == ":": - try: - t.index("%"+search+"%") - exists = True - + if 'template' in v: + try: template = file(v['template']).read() except: - exists = False - - if exists: - inc = file(replace).read() - inc = generateTemplate(inc,var,page) - print("Building include: '"+search+"'") - t = t.replace("%"+search+"%",inc) - - else: - t = t.replace("%"+search+"%",replace) - - return t - -#Takes all code blocks in templates ("{:print("Hi"):}") and executes it, and replaces the block with the "returns" variable -def runInlineScript(template,page): - for script in re.findall("{:(.*?):}",template, re.DOTALL): - returns = "" - exec(script) - template = template.replace("{:"+script+":}",returns) - - return template - -# Built-in module, generates page as subpage -def genPage(t,var,data,name,page): - if 'settings' in data: - try: - if 'template' in data['settings']: - template = 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: - template = t - - if 'pagevar' in var: - if 'settings' in data: - if 'pagevar' in data['settings']: - var['pagevar'].update(data['settings']['pagevar']) - - template = generateTemplate(template,var['pagevar'],name) - - else: - template = runInlineScript(template,name) - - if not 'settings' == data: - t = {'default':template} - - else: - if 'location' in meta: - t = {data['settings']['location']:{'default':template}} - - 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() - + print("{}: Can't open file '{}'".format(name,v['template'])) + 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( - getSubpages(t,var,meta,name,page) - ) - - elif meta['mod'] == "page": - subpage.update( - genPage(t,var,meta,name,page) - ) + template = "" - return subpage - -def build(arg): - global pages, pagedata - - 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 - try: template = file(v['template']).read() - except: - print("{}: Can't open file '{}'".format(name,v['template'])) - sys.exit() - #Check if pagevar is defined, skip the variable replacement step if 'pagevar' in v: template = generateTemplate(template,v['pagevar'],name) diff --git a/src/module/.admin.py.swp b/src/module/.admin.py.swp Binary files differ diff --git a/src/module/admin.py b/src/module/admin.py @@ -0,0 +1,160 @@ +import yaml + +def getPages(template,settings,name,page): + blogdb = getBlogDB() + index = """ +<!DOCTYPE html> +<html> + <head> + <title>Admin Page</title> + <style> +html { + background-color:#EFEFEF; + border-top:5px solid #FF9311; +} +.container { + margin-left:50px; +} + </style> + </head> + <body> + <a href="/admin" class="nav">Home</a> +<?php + +$databases = [%db%]; + +foreach ($databases as $db) { + $data = json_decode(file_get_contents($db),TRUE); + echo "<h1>".$db."</h1>"; + echo "<div class='container'>"; + echo "<a href=\\"edit.php?location=".$db."\\">New Post</a><br/>"; + foreach ($data as $val) { + echo "<a href=\\"edit.php?location=".$db."&post=".$val["post"]."\\">".$val["title"]."</a><br/>"; + } + echo "</div>"; +} +?> + </body> +</html> + """ + + edit = """ +<!DOCTYPE html> +<html> +<head> + <title>Admin</title> + <style> +html { + background-color:#EFEFEF; + border-top:5px solid #FF9311; +} +#wrapper { + width:800px; + margin:0 auto; +} +#wrapper label { + font-family:Consolas, Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono, monospace, serif; +} +#post { + width:100%; + height:1000px; +} +#submit { + background-color:#EFEFEF; +} +#submit:hover { + box-shadow:0px 0px 3px black inset; +} +#submit:active { + background-color:#AAEEFF; +} +.input { + outline:none; + padding:5px; + border:1px solid #DEDEDE; + border-radius:5px; + box-shadow:0px 0px 3px #DEDEDE inset; +} + </style> +</head> +<body> + <div id="wrapper"> + <a href="/admin" class="nav">Home</a> +<?php + +if (isset($_GET['location']) && isset($_GET['post'])) { + $data = json_decode(file_get_contents($_GET['location']), TRUE); + $out = ""; + foreach ($data[$_GET['post']] as $key => $val) { + if ($key !== "content") { + $out = $out . $key . "=" . $val . ", "; + } + } + $_SESSION['vars'] = $out; + $_SESSION['post'] = $data[$_GET['post']]['content']; +} else { + $_SESSION['post'] = ""; + $_SESSION['vars'] = ""; + $_GET['location'] = ""; +} +?> + <form id="update" action="./post.php" method="post" onsubmit="return validate();"> + <label>Variables:</label><input id="vars" name="vars" type="text" class="input" value="<?php echo $_SESSION['vars']; ?>"/><input type="submit" id="submit" class="input"/><br /> + <label>Location :</label><input id="loc" name="loc" type="text" class="input" value="<?php echo $_GET['location']; ?>"/><br /> + <textarea id="post" name="post" form="update" class="input"><?php echo $_SESSION['post']; ?></textarea> + </form> + </div> +</body> +</html> +""" + + post = """ +<!DOCTYPE html> +<html> + <head> + <title>Admin</title> + <style> +html { + background-color:#EFEFEF; + border-top:5px solid #FF9311; +} +.example { + width:700px; + margin:0 auto; +} + </style> + </head> + <body> + <a href="/admin" class="nav">Home</a> +<?php +$posts = json_decode(file_get_contents($_POST['loc']),TRUE); +parse_str(str_replace(",", "&", $_POST['vars']), $data); + +echo "<div class=\\"example\\">"; + +$posts[$data["post"]] = array_merge($data,array("content" => $_POST["post"])); +$fp = fopen($_POST['loc'], 'w'); +fwrite($fp, json_encode($posts)); +fclose($fp); +$output = json_decode(file_get_contents($_POST['loc']),TRUE); +echo $output[$data["post"]]["content"]; +echo "</div>"; +echo "<hr/>"; +echo "<plaintext>". shell_exec("build %destination% -d %root%"); +?> +""" + + return {"php:index":index.replace("%db%",blogdb[:-1]),"php:edit":edit,"php:post":post.replace("%root%",settings['root']).replace("%destination%",settings['dest'])} + +def getBlogDB(): + dbs = "" + + data = yaml.load(file("pages.yml").read()) + + for k,v in data.items(): + for m,md in v['pagemod'].items(): + if md['mod'] == "blog": + dbs = dbs + "\""+md['settings']['data']+"\"," + + return dbs +