147 lines
5.1 KiB
Python
147 lines
5.1 KiB
Python
from flask_flatpages import FlatPages, pygments_style_defs
|
|
import flask_flatpages
|
|
from flask import url_for
|
|
import re
|
|
from utils import path_depth,pjoin2,pjoin,list_dir, page_to_link, file_to_link
|
|
from functools import partial
|
|
|
|
from collections import namedtuple
|
|
|
|
FileLists=namedtuple("FileLists", ["list_files", "list_images", "sub_pages", "sub_index_pages", "breadcrumbs"])
|
|
|
|
|
|
|
|
# List all non *.md files within the directory
|
|
list_dir_md=partial(list_dir, not_ext=["md","~"])
|
|
|
|
# List all jpg images within the directory
|
|
list_dir_img=partial(list_dir, ext=["jpg", "jpeg","JPG"])
|
|
|
|
|
|
class FlatPagesIndex(FlatPages):
|
|
FLATPAGES_ROOT="."
|
|
@staticmethod
|
|
def get_breadcrumb_paths(path):
|
|
"""
|
|
breadcrumbs decompose the path
|
|
a/b/index -> [a/index a/b/index]
|
|
"""
|
|
elements = path.split('/')
|
|
print "i: %s" % elements
|
|
|
|
elements2 = ['index']
|
|
if len(elements)>2:
|
|
for i in range(1,len(elements)):
|
|
print "i: %s" % elements[0:i]
|
|
elements2.append(pjoin2(elements[0:i])+u'/index')
|
|
return elements2
|
|
|
|
|
|
def breadcrumbs(self, page):
|
|
"Parse a path or the path of a page into breadcrumbs"
|
|
if type(page) is flask_flatpages.Page:
|
|
return self.get_pages( FlatPagesIndex.get_breadcrumb_paths(page.path))
|
|
else:
|
|
return self.get_pages(FlatPagesIndex.get_breadcrumb_paths(page))
|
|
|
|
@staticmethod
|
|
def get_paths(pages):
|
|
return (p.path for p in pages)
|
|
|
|
def get_pages(self, paths):
|
|
if paths is None:
|
|
raise AttributeError("paths for get_pages can't be None")
|
|
return (self.get(p) for p in paths)
|
|
|
|
def get_sub_pages(self, path):
|
|
def is_subpage(path, root):
|
|
return (path.startswith(root) and # is a subpage
|
|
( re.match('.*index',path) is None) and # is no index
|
|
path_depth(path)<path_depth(root)+2
|
|
)
|
|
return (p for p in self
|
|
if is_subpage(str(p.path),path))
|
|
|
|
# List all index subpages of the current page i.e. all pages in subdirectories
|
|
def get_sub_ipages(self, path):
|
|
cc=path_depth(path)
|
|
ppath="index" if path=="" else pjoin2([path,"index"])
|
|
ps=(p for p in self
|
|
if (p.path.startswith(path) and # subpages of this page
|
|
not ( re.match('.*index',p.path) is None) and # only index
|
|
( ppath != p.path) and # not identical page
|
|
len(p.path.split('/'))<=cc+2) ) # only one level
|
|
return ps
|
|
|
|
|
|
# load a page from a path information
|
|
def get_flatpage(self,path):
|
|
is_index = path=='' or path.split('/')[-1]=='index'
|
|
if path == 'index' or path=='':
|
|
is_index=True
|
|
path=''
|
|
pathi='index'
|
|
else:
|
|
if path.split('/')[-1]=='index': # last element is "index" ?
|
|
is_index=True
|
|
path='/'.join(path.split('/')[0:-1]) # remove index from path
|
|
pathi = u'{}/{}'.format(path, 'index') # try to add index to the path
|
|
page = FlatPages.get(self,pathi) # try to load index page
|
|
if not page is None: # if successful it is a index
|
|
is_index=True
|
|
else:
|
|
page=FlatPages.get(self,path)
|
|
return (is_index, path, page)
|
|
|
|
|
|
|
|
def load_linklists_for_templates(self,page_path, rel_path, is_index=True, cfg={}, page_url_for=partial(url_for,'intern.postjson')):
|
|
abspath = pjoin(FlatPagesIndex.FLATPAGES_ROOT,rel_path)
|
|
# if not is_index:
|
|
dirpath= pjoin(FlatPagesIndex.FLATPAGES_ROOT,"/".join(page_path.split('/')[:-1]))
|
|
|
|
bc = page_to_link( self.breadcrumbs(page_path), page_url_for)
|
|
il=[]
|
|
if (cfg.get("has_img",False) ):
|
|
il=file_to_link(list_dir_img(dirpath), page_url_for)
|
|
if is_index == True:
|
|
ld,sp,spi=(file_to_link(list_dir_md(abspath),page_url_for),
|
|
page_to_link(self.get_sub_pages(rel_path),page_url_for),
|
|
page_to_link(self.get_sub_ipages(rel_path),page_url_for))
|
|
else:
|
|
ld,sp,spi=([],[],[])
|
|
|
|
fl=FileLists(list_files=ld, list_images=il, sub_pages=sp, sub_index_pages=spi, breadcrumbs=bc)
|
|
return fl
|
|
|
|
|
|
|
|
def load_lists_for_templates(self,path,is_index=True, cfg={}):
|
|
path2 = pjoin(FlatPagesIndex.FLATPAGES_ROOT,path)
|
|
if not is_index:
|
|
path="/".join(path2.split('/')[:-1])
|
|
|
|
# List all subpages of the current page
|
|
# get_sub_pages = partial(FlatPagesIndex.get_sub_pages, self)
|
|
# get_sub_ipages = partial(FlatPagesIndex.get_sub_ipages, self)
|
|
bc= self.breadcrumbs(path)
|
|
il=[]
|
|
if (cfg.get("has_img",False) ):
|
|
print "path2: %s" % path2
|
|
il=list_dir_img(mpath)
|
|
if is_index == True:
|
|
ld=list_dir_md(path2)
|
|
sp=self.get_sub_pages(path)
|
|
spi=self.get_sub_ipages(path)
|
|
else:
|
|
ld=[]
|
|
sp=[]
|
|
spi=[]
|
|
|
|
fl=FileLists(list_files=ld, list_images=il, sub_pages=sp, sub_index_pages=spi, breadcrumbs=bc)
|
|
return fl
|
|
|
|
|
|
|
|
|