upgrade1
This commit is contained in:
100
__init__.py
100
__init__.py
@@ -1,5 +1,5 @@
|
|||||||
import sys
|
import sys
|
||||||
from flask import Flask, render_template, send_from_directory
|
from flask import Flask, render_template, send_from_directory,jsonify
|
||||||
from flask_flatpages import FlatPages, pygments_style_defs
|
from flask_flatpages import FlatPages, pygments_style_defs
|
||||||
from flask_frozen import Freezer
|
from flask_frozen import Freezer
|
||||||
from config import Config
|
from config import Config
|
||||||
@@ -11,68 +11,84 @@ package_directory = os.path.dirname(os.path.abspath(__file__))
|
|||||||
#cfg = Config(file('./config.cfg'))
|
#cfg = Config(file('./config.cfg'))
|
||||||
cfg = Config(file(os.path.join(package_directory, 'config.cfg')))
|
cfg = Config(file(os.path.join(package_directory, 'config.cfg')))
|
||||||
|
|
||||||
|
# Loading constants from config file
|
||||||
FLATPAGES_AUTO_RELOAD = cfg.pages_reload
|
FLATPAGES_AUTO_RELOAD = cfg.pages_reload
|
||||||
FLATPAGES_EXTENSION = '.md'
|
FLATPAGES_EXTENSION = '.md'
|
||||||
FLATPAGES_ROOT = abspath(cfg.pages_root)
|
FLATPAGES_ROOT = abspath(cfg.pages_root)
|
||||||
FLATPAGES_FP_ROOT = abspath(cfg.pages_root)
|
FLATPAGES_FP_ROOT = abspath(cfg.pages_root)
|
||||||
|
|
||||||
|
# Initialize application
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
flatpages = FlatPages(app)
|
flatpages = FlatPages(app)
|
||||||
flatpages.get('index')
|
flatpages.get('index')
|
||||||
freezer = Freezer(app)
|
freezer = Freezer(app)
|
||||||
app.config.from_object(__name__)
|
app.config.from_object(__name__)
|
||||||
|
|
||||||
def list_dir(mypath):
|
# List all non *.md files within the directory
|
||||||
return [f for f in os.listdir(mypath) if isfile(os.path.join(mypath, f)) and re.match('.*\.md.*',f) is None]
|
def list_dir_md(mypath):
|
||||||
|
return [f for f in os.listdir(mypath) if isfile(os.path.join(mypath, f)) and re.match('.*\%s.*' % FLATPAGES_EXTENSION,f) is None]
|
||||||
|
|
||||||
def list_img(mypath):
|
# List all jpg images within the directory
|
||||||
|
def list_dir_img(mypath):
|
||||||
return [f for f in os.listdir(mypath) if isfile(os.path.join(mypath, f)) and re.match('.*\.jpg',f) is not None]
|
return [f for f in os.listdir(mypath) if isfile(os.path.join(mypath, f)) and re.match('.*\.jpg',f) is not None]
|
||||||
|
|
||||||
|
# List all subpages of the current page
|
||||||
def get_sub_pages(path, page):
|
def get_sub_pages(path, page):
|
||||||
ppath=page.path
|
ppath=page.path
|
||||||
cc=len(path.split('/'))
|
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 ]
|
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) ]
|
||||||
|
|
||||||
|
# List all index subpages of the current page i.e. all pages in subdirectories
|
||||||
def get_sub_ipages(path, page):
|
def get_sub_ipages(path, page):
|
||||||
ppath=page.path
|
ppath=page.path
|
||||||
cc=len(path.split('/'))
|
cc=len(path.split('/'))
|
||||||
if path=="":
|
if path=="":
|
||||||
cc=0
|
cc=0
|
||||||
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) and len(p.path.split('/'))<=cc+2 ]
|
ps=[p for p in flatpages
|
||||||
|
if (p.path.startswith(path) and # subpages of this page
|
||||||
|
not ( re.match('.*index',p.path) is None) and # only index
|
||||||
|
( re.match(ppath,p.path) is None) and # not identical page
|
||||||
|
len(p.path.split('/'))<=cc+2) ] # only one level
|
||||||
return ps
|
return ps
|
||||||
|
|
||||||
|
# breadcrumbs decompose the path
|
||||||
|
# a/b/index -> [a/index a/b/index]
|
||||||
def get_bc(path, page):
|
def get_bc(path, page):
|
||||||
ppath=page.path
|
# ppath = page.path
|
||||||
elements=path.split('/')#[0:-1]
|
elements = path.split('/')
|
||||||
elements2=['index']
|
elements2 = ['index']
|
||||||
for i in range(0,len(elements)):
|
for i in range(1,len(elements)):
|
||||||
elements2.append(pjoin2(elements[0:i])+u'/index')
|
elements2.append(pjoin2(elements[0:i])+u'/index')
|
||||||
ps=[p for p in flatpages if p.path in elements2 ]
|
# ps=[p for p in flatpages if p.path in elements2 ]
|
||||||
|
ps=[flatpages.get(p) for p in elements2]
|
||||||
|
# ps.append(['index'])
|
||||||
return ps
|
return ps
|
||||||
|
|
||||||
|
|
||||||
|
# load a page from a path information
|
||||||
def get_flatpage(path):
|
def get_flatpage(path):
|
||||||
is_index=False
|
is_index=False
|
||||||
if path == 'index' or path=='':
|
# root index
|
||||||
|
if path == 'index' or path=='':
|
||||||
is_index=True
|
is_index=True
|
||||||
path=''
|
path=''
|
||||||
pathi='index'
|
pathi='index'
|
||||||
else:
|
else:
|
||||||
if path.split('/')[-1]=='index':
|
if path.split('/')[-1]=='index': # last element is "index" ?
|
||||||
is_index=True
|
is_index=True
|
||||||
path='/'.join(path.split('/')[0:-1])
|
path='/'.join(path.split('/')[0:-1]) # remove index from path
|
||||||
pathi = u'{}/{}'.format(path, 'index')
|
pathi = u'{}/{}'.format(path, 'index') # try to add index to the path
|
||||||
page = flatpages.get(pathi)
|
page = flatpages.get(pathi) # try to load index page
|
||||||
if not page is None:
|
if not page is None: # if successful it is a index
|
||||||
is_index=True
|
is_index=True
|
||||||
else:
|
else:
|
||||||
page=flatpages.get(path)
|
page=flatpages.get(path)
|
||||||
return (is_index, path, page)
|
return (is_index, path, page)
|
||||||
|
|
||||||
|
|
||||||
def pjoin (rt,pth):
|
def pjoin (rt,pth):
|
||||||
return u'{}/{}'.format(rt,pth)
|
return u'{}/{}'.format(rt,pth)
|
||||||
|
|
||||||
@@ -88,19 +104,19 @@ def page_defaults(page, is_index, path):
|
|||||||
if misskey(page.meta,"title"):
|
if misskey(page.meta,"title"):
|
||||||
page.meta["title"]=path.split('/')[-1]
|
page.meta["title"]=path.split('/')[-1]
|
||||||
if misskey(page.meta, "template"):
|
if misskey(page.meta, "template"):
|
||||||
page.meta["template"]='post.html'
|
page.meta["template"]='page.html'
|
||||||
return page
|
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.info('render'+name)
|
app.logger.info('render '+name)
|
||||||
path2 = pjoin(FLATPAGES_ROOT,path)
|
path2 = pjoin(FLATPAGES_ROOT,path)
|
||||||
|
|
||||||
if is_index == True and not page is None:
|
if is_index == True and not page is None:
|
||||||
ld=list_dir(path2)
|
ld=list_dir_md(path2)
|
||||||
il=list_img(path2)
|
il=list_dir_img(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)
|
||||||
else:
|
else:
|
||||||
@@ -118,3 +134,35 @@ def post(name='index'):
|
|||||||
else:
|
else:
|
||||||
return send_from_directory('static',path)
|
return send_from_directory('static',path)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/<path:name>.json/')
|
||||||
|
def postjson(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_md(path2)
|
||||||
|
il=list_dir_img(path2)
|
||||||
|
sp=get_sub_pages(path,page)
|
||||||
|
spi=get_sub_ipages(path,page)
|
||||||
|
else:
|
||||||
|
ld=[]
|
||||||
|
sp=[]
|
||||||
|
spi=[]
|
||||||
|
il=[]
|
||||||
|
if not page is None:
|
||||||
|
page_defaults(page,is_index,path)
|
||||||
|
app.logger.info("Render Template"+page["template"] +"for "+path)
|
||||||
|
p=page.meta
|
||||||
|
p["text"]=page.html
|
||||||
|
return jsonify(page=p)
|
||||||
|
# return render_template(page.meta["template"], ld=ld, post=page, sp=sp, spi=spi, il=il, 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)
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
pages_root: '/mnt/data'
|
pages_root: '/home/andreas/www/tmp/int1/data'
|
||||||
pages_reload: True
|
pages_reload: True
|
||||||
static_root: 'static'
|
static_root: 'static'
|
||||||
44
templates/page.html
Normal file
44
templates/page.html
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
{% extends "layout.html" %}
|
||||||
|
{% block content %}
|
||||||
|
|
||||||
|
<h1>{{post.title}}</h1>
|
||||||
|
{{post.date}}
|
||||||
|
{% if spi|length > 0 %}
|
||||||
|
{{ post.html|safe }}
|
||||||
|
<hr/>
|
||||||
|
<b id="up_head"> Unterseiten: </b>
|
||||||
|
<ul class="nav flex-column flex-sm-row " labeledby="up_head">
|
||||||
|
{% for d in spi %}
|
||||||
|
<li class="nav-item">
|
||||||
|
<a href="{{url_for('post',name=d['path'])}}" class="nav-link">
|
||||||
|
<h6> {{d.title}} <small class="text-muted">{{'/'.join(d.path.split('/')[-2:-1])}} </small>
|
||||||
|
</h6>
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
{% endif %}
|
||||||
|
{% if sp|length > 0 %}
|
||||||
|
|
||||||
|
<ul class="nav flex-column flex-sm-row" labeledby="inf_head">
|
||||||
|
{% for d in sp %}
|
||||||
|
<li class="nav-item">
|
||||||
|
<a href="{{url_for('post',name=d['path'])}}" class="nav-link text-info">
|
||||||
|
<h6> {{d.title}} <small class="text-muted">{{d.path.split('/')[-1]}} </small>
|
||||||
|
</h6>
|
||||||
|
</a>
|
||||||
|
|
||||||
|
</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
{% endif %}
|
||||||
|
<hr/>
|
||||||
|
<b id="inf_head">Files: </b>
|
||||||
|
<ul>
|
||||||
|
{% for d in ld %}
|
||||||
|
<li>
|
||||||
|
<a href="/{{pth}}/{{d}}">{{d}} </a>
|
||||||
|
</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
{% endblock %}
|
||||||
Reference in New Issue
Block a user