nicer robust code
This commit is contained in:
@@ -1,2 +1,3 @@
|
|||||||
pages_root: '/mnt/data'
|
pages_root: '/mnt/data'
|
||||||
pages_reload: True
|
pages_reload: True
|
||||||
|
static_root: 'static'
|
||||||
44
cont.py
44
cont.py
@@ -5,7 +5,7 @@ from flask_frozen import Freezer
|
|||||||
from config import Config
|
from config import Config
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
from os.path import isfile, join
|
from os.path import isfile, abspath
|
||||||
|
|
||||||
cfg = Config(file('config.cfg'))
|
cfg = Config(file('config.cfg'))
|
||||||
|
|
||||||
@@ -19,7 +19,7 @@ freezer = Freezer(app)
|
|||||||
app.config.from_object(__name__)
|
app.config.from_object(__name__)
|
||||||
|
|
||||||
def list_dir(mypath):
|
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):
|
def get_sub_pages(path, page):
|
||||||
ppath=page.path
|
ppath=page.path
|
||||||
@@ -30,9 +30,6 @@ def get_sub_pages(path, page):
|
|||||||
def get_sub_ipages(path, page):
|
def get_sub_ipages(path, page):
|
||||||
ppath=page.path
|
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)]
|
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
|
return ps
|
||||||
|
|
||||||
def get_flatpage(path):
|
def get_flatpage(path):
|
||||||
@@ -40,7 +37,7 @@ def get_flatpage(path):
|
|||||||
if path.split('/')[-1]=='index':
|
if path.split('/')[-1]=='index':
|
||||||
is_index=True
|
is_index=True
|
||||||
path='/'.join(path.split('/')[0:-1])
|
path='/'.join(path.split('/')[0:-1])
|
||||||
pathi = '{}/{}'.format(path, 'index')
|
pathi = u'{}/{}'.format(path, 'index')
|
||||||
page = flatpages.get(pathi)
|
page = flatpages.get(pathi)
|
||||||
if not page is None:
|
if not page is None:
|
||||||
is_index=True
|
is_index=True
|
||||||
@@ -48,12 +45,30 @@ def get_flatpage(path):
|
|||||||
page=flatpages.get(path)
|
page=flatpages.get(path)
|
||||||
return (is_index, path, page)
|
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('/<path:name>/')
|
@app.route('/<path:name>/')
|
||||||
def post(name='index'):
|
def post(name='index'):
|
||||||
is_index, path, page = get_flatpage(name)
|
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:
|
if is_index == True and not page is None:
|
||||||
path2 = '{}/{}'.format(FLATPAGES_ROOT,path)
|
|
||||||
ld=list_dir(path2)
|
ld=list_dir(path2)
|
||||||
sp=get_sub_pages(path,page)
|
sp=get_sub_pages(path,page)
|
||||||
spi=get_sub_ipages(path,page)
|
spi=get_sub_ipages(path,page)
|
||||||
@@ -62,18 +77,9 @@ def post(name='index'):
|
|||||||
sp=[]
|
sp=[]
|
||||||
spi=[]
|
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:
|
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)):
|
if os.path.exists('{}/{}'.format(FLATPAGES_ROOT,path)):
|
||||||
return send_from_directory(FLATPAGES_ROOT,path)
|
return send_from_directory(FLATPAGES_ROOT,path)
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ Files:
|
|||||||
<ul>
|
<ul>
|
||||||
{% for d in ld %}
|
{% for d in ld %}
|
||||||
<li>
|
<li>
|
||||||
<a href="{{d}}">{{d}} </a>
|
<a href="/{{path}}/{{d}}">{{d}} </a>
|
||||||
</li>
|
</li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
|
|||||||
Reference in New Issue
Block a user