def __call__(self, filename): #>>>>> find file pan = None # marker for file not found for d in self.dirs: pan = butil.join(d, filename) if butil.fileExists(pan): break pan = None #//for if pan==None: msg = "File %s not found in any of %r" % (filename, self.dirs) raise IOError(msg) #>>>>> read file fh = open(pan, 'r') s = fh.read() fh.close() #>>>>> need to convert to unicode? toUnicode = False for ch in s: if ord(ch)>127: #prvars("s ch") toUnicode = True break #//for #prvars("toUnicode") if not toUnicode: return s return unicode(s, "utf_8")
def articleExists(d, an): """ @param d::str = a full path to a directory @param an::str = a filename within that directory, but without the ".md" extension @return::bool = whether the article (an) exists """ pan = butil.join(d, an + ".md") return butil.fileExists(pan)
def siteInfo(siteName): """ Display information about a site. """ import wiki tem = jinjaEnv.get_template("generic_10_2.html") dirPan = wiki.getDirPan(siteName, "") realPan = os.path.realpath(dirPan) realPanInfo = "" if realPan != dirPan: realPanInfo = form( "<p>Canonical path is <code>{realPan}" "</code> .</p>\n", realPan=realPan) #prvars("dirPan realPan realPanInfo") fns, dirs = butil.getFilesDirs(dirPan) if butil.fileExists(butil.join(dirPan, "home.md")): homePageInfo = form( "View <a href='/{siteName}/w/home'>" "<i class='fa fa-home'></i> home page</a>.", siteName=siteName) else: homePageInfo = form("""There is no home page. <a href='/{siteName}/wikiedit/home'> <i class='fa fa-plus'></i> Create one</a>.""", siteName=siteName) contents = """\ <h1>Information about site <i class='fa fa-bank'></i> {siteName}</h1> <p><b>{siteName}</b> is stored in directory <code>{siteRoot}</code> .</p> {realPanInfo} <p>There are {numPages} pages in the root folder, and {numSubDirs} sub-folders. <a href='/{siteName}/w/'><i class='fa fa-folder'></i> View root folder</a>. </p> <p>{homePageInfo}</p> """.format( siteName=siteName, siteRoot=dirPan, realPanInfo=realPanInfo, numPages=len(fns), numSubDirs=len(dirs), homePageInfo=homePageInfo, ) h = tem.render( siteName=siteName, title="Information about " + siteName, nav2=wiki.locationSitePath(siteName, ""), wikiText=contents, ) return h
def getDirPan(siteName, pathName): """ return the pathname for a directory @param siteName::str = the site name @param pathName::str = the pathname within the site @return::str = the full pathname to the directory (which may or may not exist). If the site doesn't exist, returns "". """ if not siteName: return "" for stub in config.SITE_STUBS: _, dirs = butil.getFilesDirs(stub) if siteName in dirs: return butil.join(stub, siteName, pathName) #//for return ""
def getArticlePan2(stub, siteName, pathName): """ return the pathname for an article, given the stub of the directory hierarchy to get it from. @param stub::str = the leftmost part of the pathname, to just before the siteName, e.g.: "/home/someuser/siteboxdata/sites" @param siteName::str = the site name @param pathName::str = the pathname within the site @return::str = the full pathname to the article (which may or may not exist). If the site doesn't exist, returns "". """ pathNameParts = pathName.split("/") #prvars("pathNameParts") pnLastPart = pathNameParts[-1] normLP = normArticleName(pnLastPart) pathName2 = "/".join(pathNameParts[:-1] + [normLP]) #prvars("normLP pathName2") useDir = butil.join(stub, siteName, "/".join(pathNameParts[:-1])) if articleExists(useDir, normLP): return butil.join(useDir, normLP + ".md") # article doesn't exist under the normalised name, look elsewhere: articleNames = getArticleFilesWithoutExt(useDir) for an in articleNames: nan = normArticleName(an) #prvars("an nan") if nan == normLP: return butil.join(useDir, an + ".md") #//for # couldn't find it elsewhere, use the normalised name return butil.join(useDir, normLP + ".md") pn = butil.join(stub, siteName, pathName2 + ".md") prvars("pn") return pn
def siteInfo(siteName): """ Display information about a site. """ import wiki tem = jinjaEnv.get_template("generic_10_2.html") dirPan = wiki.getDirPan(siteName, "") fns, dirs = butil.getFilesDirs(dirPan) if butil.fileExists(butil.join(dirPan, "home.md")): homePageInfo = form("View <a href='/{siteName}/w/home'>" "<i class='fa fa-home'></i> home page</a>.", siteName = siteName) else: homePageInfo = form("""There is no home page. <a href='/{siteName}/wikiedit/home'> <i class='fa fa-plus'></i> Create one</a>.""", siteName = siteName) contents = """\ <h1>Information about site <i class='fa fa-bank'></i> {siteName}</h1> <p><b>{siteName}</b> is stored in directory <code>{siteRoot}</code> .</p> <p>There are {numPages} pages in the root folder, and {numSubDirs} sub-folders. <a href='/{siteName}/w/'><i class='fa fa-folder'></i> View root folder</a>. </p> <p>{homePageInfo}</p> """.format( siteName = siteName, siteRoot = dirPan, numPages = len(fns), numSubDirs = len(dirs), homePageInfo = homePageInfo, ) h = tem.render( siteName = siteName, title = "Information about " + siteName, nav2 = wiki.locationSitePath(siteName, ""), wikiText = contents, ) return h
# config.py = configuration data import os.path from ulib import butil # --------------------------------------------------------------------- # port we are running on PORT = 7331 # A list of directories which might be followed by sites. # New sites are always created using the last one on this list. SITE_STUBS = [butil.join(os.path.dirname(__file__), "../data"), butil.join("~/siteboxdata/sites")] # --------------------------------------------------------------------- # end
def getIndex(siteName, pathName): """ get an index of a directory. @param siteName::str @param pathName::str @return::(str,str) =title,html """ def isArticle(fn): """ is a filename an article? """ return (fn[-3:]==".md" and not fn[:1]=="~") if pathName[-1:] == "/": uPath = pathName[:-1] else: uPath = pathName dirPan = getDirPan(siteName, uPath) #prvars() if not os.path.isdir(dirPan): h = "<p>Directory {} does not exist.</p>\n".format(pathName) return h fns, dirs = butil.getFilesDirs(dirPan) dirs = [d for d in dirs if d[:1] != "."] arts = sorted([fn[:-3] for fn in fns if isArticle(fn)]) nonArticles = sorted([fn for fn in fns if not isArticle(fn)]) dirs = sorted(dirs) h = ("<h1><i class='fa fa-list'></i> Index of articles in " " /{}</h1>\n").format(pathName) items = [] nonArticleItems = [] if arts: for fn in arts: text = getTitle(butil.join(dirPan, fn+".md")) if text==fn: text = "" else: text = " - " + text if fn=="home": item = form("<a href='{fn}'>" "<span class='home-icon'><i class='fa fa-home'></i></span>" " {fn}</a>{text}", fn = fn, text = text) else: item = form("<a href='{fn}'>" "<i class='fa fa-file-text-o'></i> {fn}</a>{text}", fn = fn, text = text) items.append(item) #//for h += bsColumns(items, 3) if nonArticles: for fn in nonArticles: nonArticleItems.append(form("<a href='{fn}'>" "<i class='fa fa-file-o'></i> " "{fn}</a>", fn = fn)) #//for h += "<h3>Other files</h3>\n" + bsColumns(nonArticleItems, 3) if dirs: dirItems = [] for d in dirs: dirItems.append(("<a href='{d}/'><i class='fa fa-folder'></i> " "{text}</a>").format( d = d, text = d, )) #//for h += "<h3>Folders</h3>\n" + bsColumns(dirItems, 3) title = "Index of {}".format(pathName) return title, h
app = Flask(__name__) #app.config["SECRET_KEY"] = "don't tell anyone" # not using app.config["SESSION_COOKIE_NAME"] = "session_%d" % (config.PORT,) from ulib import debugdec, butil, termcolours from ulib.debugdec import printargs, prvars #--------------------------------------------------------------------- # jinja2 environment import jinja2 from jinja2 import Template jinjaEnv = jinja2.Environment() thisDir = os.path.dirname(os.path.realpath(__file__)) templateDir = butil.join(thisDir, "templates") jinjaEnv.loader = jinja2.FileSystemLoader(templateDir) #--------------------------------------------------------------------- # login manager def helpPage(): p = request.path[1:] r = p.split('/')[0] if r=="": r = "main" return r jinjaEnv.globals['helpPage'] = helpPage def highlightPageIfCurrent(testUrl): """ If the current page starts with (testUrl), highlight it
# config.py = configuration data import os.path from ulib import butil #--------------------------------------------------------------------- # port we are running on PORT=7331 # A list of directories which might be followed by sites. # New sites are always created using the last one on this list. SITE_STUBS = [ butil.join(os.path.dirname(__file__), "../data"), butil.join("~/siteboxdata/sites"), ] #--------------------------------------------------------------------- #end
def __init__(self, *directories): self.dirs = [butil.join(d) for d in directories]
#app.config["SECRET_KEY"] = "don't tell anyone" # not using app.config["SESSION_COOKIE_NAME"] = "session_%d" % (config.PORT, ) app.config["WERKZEUG_DEBUG_PIN"] = "off" from ulib import debugdec, butil, termcolours from ulib.debugdec import printargs, prvars #--------------------------------------------------------------------- # jinja2 environment import jinja2 from jinja2 import Template jinjaEnv = jinja2.Environment() thisDir = os.path.dirname(os.path.realpath(__file__)) templateDir = butil.join(thisDir, "templates") jinjaEnv.loader = jinja2.FileSystemLoader(templateDir) #--------------------------------------------------------------------- # login manager def helpPage(): p = request.path[1:] r = p.split('/')[0] if r == "": r = "main" return r jinjaEnv.globals['helpPage'] = helpPage
def getIndex(siteName, pathName): """ get an index of a directory. @param siteName::str @param pathName::str @return::(str,str) =title,html """ def isArticle(fn): """ is a filename an article? """ return (fn[-3:] == ".md" and not fn[:1] == "~") if pathName[-1:] == "/": uPath = pathName[:-1] else: uPath = pathName dirPan = getDirPan(siteName, uPath) #prvars() if not os.path.isdir(dirPan): h = "<p>Directory {} does not exist.</p>\n".format(pathName) return h fns, dirs = butil.getFilesDirs(dirPan) dirs = [d for d in dirs if d[:1] != "."] arts = sorted([fn[:-3] for fn in fns if isArticle(fn)]) nonArticles = sorted([fn for fn in fns if not isArticle(fn)]) dirs = sorted(dirs) h = ("<h1><i class='fa fa-list'></i> Index of articles in " " /{}</h1>\n").format(pathName) items = [] nonArticleItems = [] if arts: for fn in arts: text = getTitle(butil.join(dirPan, fn + ".md")) if text == fn: text = "" else: text = " - " + text if fn == "home": item = form( "<a href='{fn}'>" "<span class='home-icon'><i class='fa fa-home'></i></span>" " {fn}</a>{text}", fn=fn, text=text) else: item = form( "<a href='{fn}'>" "<i class='fa fa-file-text-o'></i> {fn}</a>{text}", fn=fn, text=text) items.append(item) #//for h += bsColumns(items, 3) if nonArticles: for fn in nonArticles: hf = form( "<a href='{fn}'>" "<i class='fa fa-file-o'></i> " "{fn}</a>", fn=fn) if hasImageExtension(fn): hf += form( "<br>\n<a href='{fn}'>" "<img class='index_image' src='{fn}'>" "</a>", fn=fn) nonArticleItems.append(hf) #//for h += "<h3>Other files</h3>\n" + bsColumns(nonArticleItems, 3) if dirs: dirItems = [] for d in dirs: dirItems.append(("<a href='{d}/'><i class='fa fa-folder'></i> " "{text}</a>").format( d=d, text=d, )) #//for h += "<h3>Folders</h3>\n" + bsColumns(dirItems, 3) title = "Index of {}".format(pathName) return title, h