def get_indexer(app=None): from bookish import search app = app or flask.current_app config = app.config sables = config.get("SEARCHABLES", search.Searchables()) cls = config.get("INDEXER_CLASS", search.WhooshIndexer) main_dir = config["INDEX_DIR"] static_dir = config.get("STATIC_INDEX_DIR") return cls(main_dir, sables, logger=app.logger, staticdir=static_dir)
def preview_wiki(): request = flask.request config = flask.current_app.config pages = get_wikipages() indexer = get_indexer() searcher = indexer.searcher() autosave_seconds = config.get("AUTOSAVE_SECONDS", 10) autosave = ( config.get("AUTOSAVE", True) and request.form.get("autosave") != "false" ) assert "path" in request.form path = request.form["path"] assert path source = request.form.get("source", "") scrollTop = int(request.form.get("scrollTop") or "0") lastmod = 0 if pages.exists(path): lastmod = pages.last_modified(path) last_autosave = 0 if autosave: session = flask.session userid = get_request_userid() if "last_autosave" in session: last_autosave = session.get("last_autosave") else: last_autosave = datetime.datetime.utcnow() cp = Checkpoints(userid, pages.store, pages.cachestore) now = datetime.datetime.utcnow() if now - last_autosave >= datetime.timedelta(seconds=autosave_seconds): cp.autosave(path, source) last_autosave = session["last_autosave"] = now def rel(p): if p.startswith("http:") or p.startswith("https:"): return p return paths.join(path, p) extras = {"rel": rel} html = pages.preview(path, source, searcher=searcher, extras=extras, templatename="/templates/preview.jinja2") return flask.jsonify(html=html, last_modified=lastmod, last_autosave=last_autosave, scrollTop=scrollTop)
def search_page(): request = flask.request config = flask.current_app.config indexer = get_indexer() searcher = indexer.searcher() qobj = searcher.query() cat_order = config.get("CATEGORIES", "").split() shortcuts = list(config.get("SHORTCUTS", ())) shortcuts.extend(config.get("EXTRA_SHORTCUTS", ())) qstring = request.args.get("q", "") # startpos = request.args.get("startpos", "") # endpos = request.args.get("endpos", "") category = request.args.get("category", None) templatepath = request.args.get("template", config["SEARCH_TEMPLATE"]) r = qobj.results(qstring, cat_order, category=category, shortcuts=shortcuts) return flask.render_template(templatepath, **r)
def save_wiki(): request = flask.request config = flask.current_app.config maxnum = config.get("CHECKPOINT_MAX", 10) pages = get_wikipages() path = request.form["path"] source = request.form["source"] # encoding = request.form.get("encoding", "utf8") userid = get_request_userid() cp = Checkpoints(userid, pages.store, pages.cachestore, maxnum) cp.save_checkpoint(path, source, encoding="utf8") return source
def page_not_found(e): path = flask.request.path config = flask.current_app.config pages = get_wikipages() store = pages.store editable = False isdir = store.exists(path) and store.is_dir(path) if config.get("EDITABLE") and not isdir: editable = pages.is_wiki(path) content = flask.render_template('/templates/404.jinja2', path=path, editable=editable, rel=null_rel, num=404) return content, 404
def get_wikipages(app=None): from bookish import wikipages app = app or flask.current_app config = app.config store = get_store(app) cls = config.get("PAGES_CLASS", wikipages.WikiPages) return cls( store=store, env=app.jinja_env, cachedir=config.get("CACHE_DIR"), page_template=config.get("DEFAULT_TEMPLATE", "/templates/page.jinja2"), index_page_name=config.get("INDEX_PAGE_NAME", "_index"), wiki_ext=config.get("WIKI_EXT", ".txt"), default_language=config.get("DEFAULT_LANGAUGE", "en-us"), logger=app.logger, )