gallery blueimp
This commit is contained in:
100
flatpages.py
100
flatpages.py
@@ -1,12 +1,25 @@
|
||||
from flask_flatpages import FlatPages, pygments_style_defs
|
||||
import flask_flatpages
|
||||
from flask import url_for
|
||||
import re
|
||||
from utils import path_depth,pjoin2
|
||||
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):
|
||||
"""
|
||||
@@ -14,23 +27,26 @@ class FlatPagesIndex(FlatPages):
|
||||
a/b/index -> [a/index a/b/index]
|
||||
"""
|
||||
elements = path.split('/')
|
||||
elements2 = ['index']
|
||||
for i in range(1,len(elements)):
|
||||
elements2.append(pjoin2(elements[0:i])+u'/index')
|
||||
return elements2
|
||||
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)
|
||||
|
||||
@staticmethod
|
||||
def breadcrumbs(page):
|
||||
"Parse a path or the path of a page into breadcrumbs"
|
||||
if type(page) is flask_flatpages.Page:
|
||||
return FlatPagesIndex.get_breadcrumb_paths(page.path)
|
||||
else:
|
||||
return FlatPagesIndex.get_breadcrumb_paths(page)
|
||||
|
||||
def get_pages(self, paths):
|
||||
if paths is None:
|
||||
@@ -50,17 +66,17 @@ class FlatPagesIndex(FlatPages):
|
||||
def get_sub_ipages(self, path):
|
||||
cc=path_depth(path)
|
||||
ppath="index" if path=="" else pjoin2([path,"index"])
|
||||
ps=[p for p in self
|
||||
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
|
||||
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'
|
||||
is_index = path=='' or path.split('/')[-1]=='index'
|
||||
if path == 'index' or path=='':
|
||||
is_index=True
|
||||
path=''
|
||||
@@ -77,18 +93,54 @@ class FlatPagesIndex(FlatPages):
|
||||
page=FlatPages.get(self,path)
|
||||
return (is_index, path, page)
|
||||
|
||||
|
||||
|
||||
def load_lists_for_templates(self, path, is_index):
|
||||
path2 = pjoin(FLATPAGES_ROOT,path)
|
||||
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)
|
||||
il=list_dir_img(path2)
|
||||
sp=list(get_sub_pages(path))
|
||||
spi=list(get_sub_ipages(path))
|
||||
sp=self.get_sub_pages(path)
|
||||
spi=self.get_sub_ipages(path)
|
||||
else:
|
||||
ld=[]
|
||||
sp=[]
|
||||
spi=[]
|
||||
il=[]
|
||||
bc=get_bc(path)
|
||||
return (ld, il, sp, spi,bc)
|
||||
|
||||
fl=FileLists(list_files=ld, list_images=il, sub_pages=sp, sub_index_pages=spi, breadcrumbs=bc)
|
||||
return fl
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user