103 lines
2.9 KiB
Python
103 lines
2.9 KiB
Python
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
|
|
import os
|
|
import re
|
|
from os.path import isfile, abspath
|
|
|
|
cfg = Config(file('config.cfg'))
|
|
|
|
FLATPAGES_AUTO_RELOAD = cfg.pages_reload
|
|
FLATPAGES_EXTENSION = '.md'
|
|
FLATPAGES_ROOT = abspath(cfg.pages_root)
|
|
FLATPAGES_FP_ROOT = abspath(cfg.pages_root)
|
|
|
|
app = Flask(__name__)
|
|
flatpages = FlatPages(app)
|
|
flatpages.get('index')
|
|
freezer = Freezer(app)
|
|
app.config.from_object(__name__)
|
|
|
|
def list_dir(mypath):
|
|
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
|
|
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)]
|
|
return ps
|
|
|
|
|
|
def get_bc(path, page):
|
|
ppath=page.path
|
|
elements=path.split('/')#[0:-1]
|
|
elements2=['index']
|
|
for i in range(0,len(elements)):
|
|
elements2.append(pjoin2(elements[0:i])+u'/index')
|
|
ps=[p for p in flatpages if p.path in elements2 ]
|
|
return ps
|
|
|
|
|
|
|
|
def get_flatpage(path):
|
|
is_index=False
|
|
if not ( re.match('.*index',path) is None):
|
|
is_index=True
|
|
pathi = path
|
|
path='/'.join(path.split('/')[0:-1])
|
|
page = flatpages.get(pathi)
|
|
else:
|
|
page=flatpages.get(path)
|
|
return (is_index, path, page)
|
|
|
|
def pjoin (rt,pth):
|
|
return u'{}/{}'.format(rt,pth)
|
|
|
|
def pjoin2 (pth):
|
|
return u'/'.join(pth) or u''
|
|
|
|
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>/')
|
|
def post(name='index'):
|
|
is_index, path, page = get_flatpage(name)
|
|
app.logger.info('render'+name)
|
|
path2 = pjoin(FLATPAGES_ROOT,path)
|
|
|
|
if is_index == True and not page is None:
|
|
ld=list_dir(path2)
|
|
sp=get_sub_pages(path,page)
|
|
spi=get_sub_ipages(path,page)
|
|
else:
|
|
ld=[]
|
|
sp=[]
|
|
spi=[]
|
|
|
|
if not page is None:
|
|
page_defaults(page,is_index,path)
|
|
app.logger.info("Render Template"+page["template"] +"for "+path)
|
|
return render_template(page.meta["template"], ld=ld, post=page, sp=sp, spi=spi, pth=path, pagebreadcrumbs=get_bc(path,page))
|
|
|
|
if os.path.exists(u'{}/{}'.format(FLATPAGES_ROOT,path)):
|
|
return send_from_directory(FLATPAGES_ROOT,path)
|
|
else:
|
|
return send_from_directory('static',path)
|
|
|