This commit is contained in:
Andreas Stephanides
2017-02-01 22:06:25 +01:00
commit 21fb667cf4
2 changed files with 89 additions and 0 deletions

87
cont.py Normal file
View File

@@ -0,0 +1,87 @@
import sys
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 = 'contents'
POST_DIR = 'posts'
import re
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]
def get_sub_pages(path, page):
ppath=page.path
cc=len(path.split('/'))
return [p for p in flatpages if p.path.startswith(path) and ( re.match('.*index',p.path) is None) and len(p.path.split('/'))<cc+2 ]
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):
is_index=False
if path.split('/')[-1]=='index':
is_index=True
path='/'.join(path.split('/')[0:-1])
pathi = '{}/{}'.format(path, 'index')
page = flatpages.get(pathi)
if not page is None:
is_index=True
else:
page=flatpages.get(path)
return (is_index, path, page)
@app.route('/fet_daten/<path:name>/')
def post(name):
is_index, path, page = get_flatpage(name)
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)
else:
ld=[]
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)
if os.path.exists('{}/{}'.format(FLATPAGES_ROOT,path)):
return send_from_directory(FLATPAGES_ROOT,path)
else:
return send_from_directory('static',path)
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "build":
freezer.freeze()
else:
app.run(host='0.0.0.0', debug=True)