From 6abb1aa3fa78038003bb5ee1df9e5e83b069b609 Mon Sep 17 00:00:00 2001 From: Andreas Stephanides Date: Thu, 2 Feb 2017 08:34:48 +0100 Subject: [PATCH 1/2] config --- config.cfg | 2 ++ cont.py | 21 +++++++++++---------- 2 files changed, 13 insertions(+), 10 deletions(-) create mode 100644 config.cfg diff --git a/config.cfg b/config.cfg new file mode 100644 index 0000000..b9b5b66 --- /dev/null +++ b/config.cfg @@ -0,0 +1,2 @@ +pages_root: '/mnt/data' +pages_reload: True \ No newline at end of file diff --git a/cont.py b/cont.py index 3fd0da4..ae29c58 100644 --- a/cont.py +++ b/cont.py @@ -3,22 +3,23 @@ from flask import Flask, render_template, send_from_directory from flask_flatpages import FlatPages, pygments_style_defs from flask_frozen import Freezer from config import Config -from os import listdir import os -from os.path import isfile, join -DEBUG = True -FLATPAGES_AUTO_RELOAD = DEBUG -FLATPAGES_EXTENSION = '.md' -FLATPAGES_ROOT = '/mnt/data' -POST_DIR = 'posts' import re +from os.path import isfile, join + +cfg = Config(file('config.cfg')) + +FLATPAGES_AUTO_RELOAD = cfg.pages_reload +FLATPAGES_EXTENSION = '.md' +FLATPAGES_ROOT = cfg.pages_root + app = Flask(__name__) flatpages = FlatPages(app) freezer = Freezer(app) app.config.from_object(__name__) def list_dir(mypath): - return [f for f in listdir(mypath) if isfile(join(mypath, f)) and re.match('.*\.md.*',f) is None] + return [f for f in os.listdir(mypath) if isfile(join(mypath, f)) and re.match('.*\.md.*',f) is None] def get_sub_pages(path, page): ppath=page.path @@ -46,11 +47,11 @@ def get_flatpage(path): else: page=flatpages.get(path) return (is_index, path, page) -@app.route('/') + @app.route('//') def post(name='index'): is_index, path, page = get_flatpage(name) - + app.logger.debug(name) if is_index == True and not page is None: path2 = '{}/{}'.format(FLATPAGES_ROOT,path) ld=list_dir(path2) From 70cad33944b8543b3cffe55e1dc1e4c036896e78 Mon Sep 17 00:00:00 2001 From: Andreas Stephanides Date: Thu, 2 Feb 2017 09:08:03 +0100 Subject: [PATCH 2/2] nicer robust code --- config.cfg | 3 ++- cont.py | 44 +++++++++++++++++++++++++------------------- templates/post.html | 2 +- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/config.cfg b/config.cfg index b9b5b66..bdd7a41 100644 --- a/config.cfg +++ b/config.cfg @@ -1,2 +1,3 @@ pages_root: '/mnt/data' -pages_reload: True \ No newline at end of file +pages_reload: True +static_root: 'static' \ No newline at end of file diff --git a/cont.py b/cont.py index ae29c58..f76e0ad 100644 --- a/cont.py +++ b/cont.py @@ -5,7 +5,7 @@ from flask_frozen import Freezer from config import Config import os import re -from os.path import isfile, join +from os.path import isfile, abspath cfg = Config(file('config.cfg')) @@ -19,7 +19,7 @@ freezer = Freezer(app) app.config.from_object(__name__) def list_dir(mypath): - return [f for f in os.listdir(mypath) if isfile(join(mypath, f)) and re.match('.*\.md.*',f) is None] + return [f for f in os.listdir(mypath) if isfile(os.path.join(mypath, f)) and re.match('.*\.md.*',f) is None] def get_sub_pages(path, page): ppath=page.path @@ -30,9 +30,6 @@ def get_sub_pages(path, page): def get_sub_ipages(path, page): ppath=page.path ps=[p for p in flatpages if p.path.startswith(path) and not ( re.match('.*index',p.path) is None) and ( re.match(ppath,p.path) is None)] - -# for p in ps: -# p.path='/'.join(p.path.split('/')[0:-1]) return ps def get_flatpage(path): @@ -40,7 +37,7 @@ def get_flatpage(path): if path.split('/')[-1]=='index': is_index=True path='/'.join(path.split('/')[0:-1]) - pathi = '{}/{}'.format(path, 'index') + pathi = u'{}/{}'.format(path, 'index') page = flatpages.get(pathi) if not page is None: is_index=True @@ -48,12 +45,30 @@ def get_flatpage(path): page=flatpages.get(path) return (is_index, path, page) +def pjoin (rt,pth): + return u'{}/{}'.format(rt,pth) + + +def misskey(a,key): + if not a.has_key(key): + return True + return a[key] is None + +def page_defaults(page, is_index, path): + if misskey(page.meta,"title"): + page.meta["title"]=path.split('/')[-1] + if misskey(page.meta, "template"): + page.meta["template"]='post.html' + return page + + @app.route('//') def post(name='index'): is_index, path, page = get_flatpage(name) - app.logger.debug(name) + + path2 = pjoin(FLATPAGES_ROOT,path) + if is_index == True and not page is None: - path2 = '{}/{}'.format(FLATPAGES_ROOT,path) ld=list_dir(path2) sp=get_sub_pages(path,page) spi=get_sub_ipages(path,page) @@ -62,18 +77,9 @@ def post(name='index'): sp=[] spi=[] - if page is not None and (not page.meta.has_key("title") or (page.meta["title"] is None)): - print path - page.meta["title"]=path.split('/')[-1] - - if page is not None and page.meta.has_key("template"): - t=page["template"] - else: - if page is not None: - page.meta["template"]='post.html' - if not page is None: - return render_template(page["template"], ld=ld, post=page, sp=sp, spi=spi) + page = page_defaults(page,is_index,path) + return render_template(page["template"], ld=ld, post=page, sp=sp, spi=spi, path=path) if os.path.exists('{}/{}'.format(FLATPAGES_ROOT,path)): return send_from_directory(FLATPAGES_ROOT,path) diff --git a/templates/post.html b/templates/post.html index ab0829d..4771ab7 100644 --- a/templates/post.html +++ b/templates/post.html @@ -31,7 +31,7 @@ Files: