def wp_update(config_agent, tpl_render, req_path): view_settings = get_view_settings(config_agent, simple = True) static_files = static_file.get_global_static_files(**view_settings) folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") local_full_path = mdutils.req_path_to_local_full_path(req_path, folder_pages_full_path) title = "Editing %s" % req_path create_new = False if local_full_path.endswith("/"): msg = "not allow to edit path/to/folder, using path/to/folder/index instead" raise web.BadRequest(message = msg) # os.path.exists(local_full_path) if os.path.isfile(local_full_path): buf = commons.shutils.cat(local_full_path) else: # not os.path.exists(local_full_path) create_new = True buf = "" return tpl_render.update(config_agent = config_agent, static_files = static_files, req_path = req_path, create_new = create_new, title = title, content = buf, **view_settings)
def wp_stat(config_agent, tpl_render, req_path): view_settings = get_view_settings(config_agent, simple = True) static_files = static_file.get_global_static_files(**view_settings) folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") local_full_path = mdutils.req_path_to_local_full_path(req_path = req_path, folder_pages_full_path = folder_pages_full_path) static_file_prefix = static_file.get_static_file_prefix_by_local_full_path(config_agent = config_agent, local_full_path = local_full_path, req_path = req_path) title = "Stat" cmd = "find . -type f -name '*.md' -or -name '*.markdown' | wc -l " page_count = commons.shutils.run(cmd, cwd = folder_pages_full_path) or 0 cmd = "find . -type d | wc -l " folder_count = commons.shutils.run(cmd, cwd = folder_pages_full_path) or 0 buf = _stat_tpl % (int(page_count), int(folder_count)) content = mdutils.md2html(config_agent = config_agent, req_path = req_path, text = buf, static_file_prefix = static_file_prefix, **view_settings) return tpl_render.canvas(config = config_agent.config, static_files = static_files, button_path = "", req_path = req_path, title = title, content = content, **view_settings)
def wp_update(config_agent, tpl_render, req_path): view_settings = get_view_settings(config_agent, simple=True) static_files = static_file.get_global_static_files(**view_settings) folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") local_full_path = mdutils.req_path_to_local_full_path( req_path, folder_pages_full_path) title = "Editing %s" % req_path create_new = False if local_full_path.endswith("/"): msg = "not allow to edit path/to/folder, using path/to/folder/index instead" raise web.BadRequest(message=msg) # os.path.exists(local_full_path) if os.path.isfile(local_full_path): buf = commons.shutils.cat(local_full_path) else: # not os.path.exists(local_full_path) create_new = True buf = "" return tpl_render.update(config_agent=config_agent, static_files=static_files, req_path=req_path, create_new=create_new, title=title, content=buf, **view_settings)
def wp_source(config_agent, tpl_render, req_path): folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") local_full_path = mdutils.req_path_to_local_full_path(req_path, folder_pages_full_path) if os.path.isdir(local_full_path): a = os.path.join(local_full_path, "index.md") b = os.path.join(local_full_path, "index.markdown") if os.path.exists(a): web.header("Content-Type", "text/plain; charset=UTF-8") return commons.shutils.cat(a) elif os.path.exists(b): web.header("Content-Type", "text/plain; charset=UTF-8") return commons.shutils.cat(b) else: web.header("Content-Type", "text/plain; charset=UTF-8") return "folder doesn't providers source code in Markdown" elif os.path.isfile(local_full_path): web.header("Content-Type", "text/plain; charset=UTF-8") return commons.shutils.cat(local_full_path) raise web.NotFound()
def wp_stat(config_agent, tpl_render, req_path): view_settings = get_view_settings(config_agent, simple=True) static_files = static_file.get_global_static_files(**view_settings) folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") local_full_path = mdutils.req_path_to_local_full_path( req_path=req_path, folder_pages_full_path=folder_pages_full_path) static_file_prefix = static_file.get_static_file_prefix_by_local_full_path( config_agent=config_agent, local_full_path=local_full_path, req_path=req_path) title = "Stat" cmd = "find . -type f -name '*.md' -or -name '*.markdown' | wc -l " page_count = commons.shutils.run(cmd, cwd=folder_pages_full_path) or 0 cmd = "find . -type d | wc -l " folder_count = commons.shutils.run(cmd, cwd=folder_pages_full_path) or 0 buf = _stat_tpl % (int(page_count), int(folder_count)) content = mdutils.md2html(config_agent=config_agent, req_path=req_path, text=buf, static_file_prefix=static_file_prefix, **view_settings) return tpl_render.canvas(config=config_agent.config, static_files=static_files, button_path="", req_path=req_path, title=title, content=content, **view_settings)
def wp_rename_post(config_agent, tpl_render, req_path, new_path): folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") old_path = req_path old_full_path = mdutils.req_path_to_local_full_path( old_path, folder_pages_full_path) new_full_path = mdutils.req_path_to_local_full_path( new_path, folder_pages_full_path) if old_full_path == new_full_path: return web.seeother("/%s" % new_path) elif not os.path.exists(old_full_path): msg = "old %s doesn't exists" % old_full_path raise web.BadRequest(msg) msg = """<pre> allow file -> new_file folder -> new_folder not allow file -> new_folder folder -> new_file </pre>""" if os.path.isfile(old_full_path): if new_full_path.endswith("/"): raise web.BadRequest(msg) elif os.path.isdir(old_full_path): if not new_full_path.endswith("/"): raise web.BadRequest(msg) parent = os.path.dirname(new_full_path) if not os.path.exists(parent): os.makedirs(parent) shutil.move(old_full_path, new_full_path) cache.update_all_pages_list_cache(folder_pages_full_path) cache.update_recent_change_cache(folder_pages_full_path) if os.path.isfile(new_full_path): return web.seeother("/%s" % new_path) elif os.path.isdir(new_full_path): return web.seeother("/%s/" % new_path) else: raise web.BadRequest()
def generate_feed(config_agent, req_path, tpl_render): folder_pages_full_path = config_agent.config.get("paths", "pages_path") cache_file_full_path = os.path.join(folder_pages_full_path, ".zw_all_pages_list_cache") buf = cache.get_all_pages_list_from_cache(config_agent) md_list = buf.split() author = config_agent.config.get("main", "maintainer_email") or "Anonymous" e_author = atom.Element(name="author") child = atom.Element(name="name", text=author) e_author.append_children(child) ts = os.stat(cache_file_full_path).st_ctime updated = atom.generate_updated(ts) ts_as_id = "timestamp:" + commons.strutils.md5(updated) feed = atom.Feed(author=e_author, id=ts_as_id, updated=updated, title="Testing Feed Output") for md_file_name in md_list[:100]: req_path = commons.strutils.rstrips(md_file_name, ".md") req_path = commons.strutils.rstrips(req_path, ".markdown") local_full_path = mdutils.req_path_to_local_full_path( req_path, folder_pages_full_path) raw_text = commons.shutils.cat(local_full_path) page_title = mdutils.get_title_by_file_path_in_md( folder_pages_full_path, req_path) static_file_prefix = static_file.get_static_file_prefix_by_local_full_path( config_agent=config_agent, local_full_path=local_full_path, req_path=req_path) view_settings = page.get_view_settings(config_agent) page_content = mdutils.md2html(config_agent=config_agent, req_path=req_path, text=raw_text, static_file_prefix=static_file_prefix, **view_settings) text = cgi.escape(commons.strutils.safestr(page_content)) e_content = atom.Element(name="content", text=text, type="html") if not page_title: continue hash_title_as_id = "md5:" + commons.strutils.md5(page_title) updated = atom.generate_updated(os.stat(local_full_path).st_ctime) entry = atom.Entry(id=hash_title_as_id, title=page_title, updated=updated, content=e_content) feed.append_children(entry) buf = str(feed) return buf
def wp_rename_post(config_agent, tpl_render, req_path, new_path): folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") old_path = req_path old_full_path = mdutils.req_path_to_local_full_path(old_path, folder_pages_full_path) new_full_path = mdutils.req_path_to_local_full_path(new_path, folder_pages_full_path) if old_full_path == new_full_path: return web.seeother("/%s" % new_path) elif not os.path.exists(old_full_path): msg = "old %s doesn't exists" % old_full_path raise web.BadRequest(msg) msg = """<pre> allow file -> new_file folder -> new_folder not allow file -> new_folder folder -> new_file </pre>""" if os.path.isfile(old_full_path): if new_full_path.endswith("/"): raise web.BadRequest(msg) elif os.path.isdir(old_full_path): if not new_full_path.endswith("/"): raise web.BadRequest(msg) parent = os.path.dirname(new_full_path) if not os.path.exists(parent): os.makedirs(parent) shutil.move(old_full_path, new_full_path) cache.update_all_pages_list_cache(folder_pages_full_path) cache.update_recent_change_cache(folder_pages_full_path) if os.path.isfile(new_full_path): return web.seeother("/%s" % new_path) elif os.path.isdir(new_full_path): return web.seeother("/%s/" % new_path) else: raise web.BadRequest()
def wp_delete(config_agent, tpl_render, req_path): folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") local_full_path = mdutils.req_path_to_local_full_path(req_path, folder_pages_full_path) delete_page_file_by_full_path(local_full_path) cache.update_recent_change_cache(config_agent) cache.update_all_pages_list_cache(config_agent) web.seeother("/") return
def generate_feed(config_agent, req_path, tpl_render): folder_pages_full_path = config_agent.config.get("paths", "pages_path") cache_file_full_path = os.path.join(folder_pages_full_path, ".zw_all_pages_list_cache") buf = cache.get_all_pages_list_from_cache(config_agent) md_list = buf.split() author = config_agent.config.get("main", "maintainer_email") or "Anonymous" e_author = atom.Element(name="author") child = atom.Element(name="name", text=author) e_author.append_children(child) ts = os.stat(cache_file_full_path).st_ctime updated = atom.generate_updated(ts) ts_as_id = "timestamp:" + commons.strutils.md5(updated) feed = atom.Feed(author=e_author, id=ts_as_id, updated=updated, title="Testing Feed Output") for md_file_name in md_list[:100]: req_path = commons.strutils.rstrips(md_file_name, ".md") req_path = commons.strutils.rstrips(req_path, ".markdown") local_full_path = mdutils.req_path_to_local_full_path(req_path, folder_pages_full_path) raw_text = commons.shutils.cat(local_full_path) page_title = mdutils.get_title_by_file_path_in_md(folder_pages_full_path, req_path) static_file_prefix = static_file.get_static_file_prefix_by_local_full_path( config_agent = config_agent, local_full_path = local_full_path, req_path = req_path) view_settings = page.get_view_settings(config_agent) page_content = mdutils.md2html(config_agent = config_agent, req_path = req_path, text = raw_text, static_file_prefix = static_file_prefix, **view_settings) text = cgi.escape(commons.strutils.safestr(page_content)) e_content = atom.Element(name="content", text=text, type="html") if not page_title: continue hash_title_as_id = "md5:" + commons.strutils.md5(page_title) updated = atom.generate_updated(os.stat(local_full_path).st_ctime) entry = atom.Entry(id=hash_title_as_id, title=page_title, updated=updated, content=e_content) feed.append_children(entry) buf = str(feed) return buf
def wp_source(config_agent, tpl_render, req_path): folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") local_full_path = mdutils.req_path_to_local_full_path(req_path, folder_pages_full_path) if os.path.isfile(local_full_path): web.header("Content-Type", "text/plain; charset=UTF-8") buf = commons.shutils.cat(local_full_path) return buf elif os.path.isdir(local_full_path): msg = "folder doesn't providers source in Markdown, using file instead" raise web.BadRequest(msg) else: raise web.NotFound()
def update_page_by_req_path(req_path, content): full_path = mdutils.req_path_to_local_full_path(req_path, folder_pages_full_path) if full_path.endswith((".md", ".markdown")): folder_parent = os.path.dirname(full_path) if not os.path.exists(folder_parent): os.makedirs(folder_parent) if not os.path.isdir(full_path): web.utils.safewrite(full_path, content.replace("\r\n", "\n")) else: idx_dot_md_full_path = os.path.join(full_path, "index.md") web.utils.safewrite(idx_dot_md_full_path, content.replace("\r\n", "\n"))
def wp_source(config_agent, tpl_render, req_path): folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") local_full_path = mdutils.req_path_to_local_full_path( req_path, folder_pages_full_path) if os.path.isfile(local_full_path): web.header("Content-Type", "text/plain; charset=UTF-8") buf = commons.shutils.cat(local_full_path) return buf elif os.path.isdir(local_full_path): msg = "folder doesn't providers source in Markdown, using file instead" raise web.BadRequest(msg) else: raise web.NotFound()
def wp_get_recent_changes_from_cache(config_agent, tpl_render, req_path, limit, offset): view_settings = get_view_settings(config_agent) static_files = static_file.get_global_static_files(**view_settings) folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") local_full_path = mdutils.req_path_to_local_full_path( req_path, folder_pages_full_path) static_file_prefix = static_file.get_static_file_prefix_by_local_full_path( config_agent=config_agent, local_full_path=local_full_path, req_path=req_path) buf = cache.get_recent_changes_from_cache(config_agent) all_lines = buf.split() total_lines = len(all_lines) title = "Recent Changes (%d/%d)" % (offset, total_lines / limit) start = offset * limit end = start + limit lines = all_lines[start:end] buf = mdutils.sequence_to_unorder_list( folder_pages_full_path=folder_pages_full_path, seq=lines, **view_settings) content = mdutils.md2html(config_agent=config_agent, req_path=req_path, text=buf, static_file_prefix=static_file_prefix, **view_settings) pg = paginator.Paginator() pg.total = total_lines pg.current_offset = offset pg.limit = limit pg.url = "/~recent" return tpl_render.canvas(config=config_agent.config, static_files=static_files, button_path=title, req_path=req_path, title=title, content=content, paginator=pg, **view_settings)
def wp_rename(config_agent, tpl_render, req_path): view_settings = get_view_settings(config_agent) static_files = static_file.get_global_static_files(**view_settings) folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") local_full_path = mdutils.req_path_to_local_full_path( req_path=req_path, folder_pages_full_path=folder_pages_full_path ) if not os.path.exists(local_full_path): raise web.NotFound() title = "Rename %s" % req_path old_path = req_path return tpl_render.rename( config_agent=config_agent, static_files=static_files, title=title, old_path=old_path, **view_settings )
def wp_search(config_agent, tpl_render, req_path): view_settings = get_view_settings(config_agent) folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") local_full_path = mdutils.req_path_to_local_full_path( req_path, folder_pages_full_path) static_file_prefix = static_file.get_static_file_prefix_by_local_full_path( config_agent=config_agent, local_full_path=local_full_path, req_path=req_path) keywords = web.input().get("k") keywords = web.utils.safestr(keywords) title = "Search %s" % keywords if keywords: limit = config_agent.config.getint("pagination", "search_page_limit") lines = search.search_by_filename_and_file_content(keywords, limit=limit) if lines: buf = mdutils.sequence_to_unorder_list( folder_pages_full_path=folder_pages_full_path, seq=lines, **view_settings) else: buf = None else: buf = None if buf: content = mdutils.md2html(config_agent=config_agent, req_path=req_path, text=buf, static_file_prefix=static_file_prefix, **view_settings) else: content = "matched not found" static_files = static_file.get_global_static_files(**view_settings) return tpl_render.search(config_agent=config_agent, static_files=static_files, title=title, keywords=keywords, content=content)
def wp_update_post(config_agent, req_path, new_content): path_info = web.ctx.environ["PATH_INFO"] folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") local_full_path = mdutils.req_path_to_local_full_path(req_path, folder_pages_full_path) folder_parent = os.path.dirname(local_full_path) if not os.path.exists(folder_parent): os.makedirs(folder_parent) content = new_content.replace("\r\n", "\n") with open(local_full_path, "w") as f: f.write(content) cache.update_recent_change_cache(folder_pages_full_path) return web.seeother(path_info)
def wp_get_recent_changes_from_cache(config_agent, tpl_render, req_path, limit, offset): view_settings = get_view_settings(config_agent) static_files = static_file.get_global_static_files(**view_settings) folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") local_full_path = mdutils.req_path_to_local_full_path(req_path, folder_pages_full_path) static_file_prefix = static_file.get_static_file_prefix_by_local_full_path( config_agent = config_agent, local_full_path = local_full_path, req_path = req_path) buf = cache.get_recent_changes_from_cache(config_agent) all_lines = buf.split() total_lines = len(all_lines) title = "Recent Changes (%d/%d)" % (offset, total_lines / limit) start = offset * limit end = start + limit lines = all_lines[start : end] buf = mdutils.sequence_to_unorder_list(lines, **view_settings) content = mdutils.md2html(config_agent = config_agent, req_path = req_path, text = buf, static_file_prefix = static_file_prefix, **view_settings) pg = paginator.Paginator() pg.total = total_lines pg.current_offset = offset pg.limit = limit pg.url = "/~recent" return tpl_render.canvas(config = config_agent.config, static_files = static_files, button_path = title, req_path = req_path, title = title, content = content, paginator = pg, **view_settings)
def wp_update_post(config_agent, req_path, new_content): path_info = web.ctx.environ["PATH_INFO"] folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") local_full_path = mdutils.req_path_to_local_full_path( req_path, folder_pages_full_path) folder_parent = os.path.dirname(local_full_path) if not os.path.exists(folder_parent): os.makedirs(folder_parent) content = new_content.replace("\r\n", "\n") with open(local_full_path, "w") as f: f.write(content) cache.update_recent_change_cache(folder_pages_full_path) return web.seeother(path_info)
def wp_delete(config_agent, tpl_render, req_path): folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") local_full_path = mdutils.req_path_to_local_full_path(req_path, folder_pages_full_path) path_info = web.ctx.environ["PATH_INFO"] if os.path.isfile(local_full_path): redirect_to = os.path.dirname(path_info) delete_page_file_by_full_path(local_full_path) elif os.path.isdir(local_full_path): redirect_to = os.path.dirname(commons.strutils.rstrip(path_info, "/")) delete_page_file_by_full_path(local_full_path) else: raise web.NotFound() cache.update_recent_change_cache(folder_pages_full_path) cache.update_all_pages_list_cache(folder_pages_full_path) return web.seeother(redirect_to)
def wp_search(config_agent, tpl_render, req_path): view_settings = get_view_settings(config_agent) folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") local_full_path = mdutils.req_path_to_local_full_path(req_path, folder_pages_full_path) static_file_prefix = static_file.get_static_file_prefix_by_local_full_path( config_agent = config_agent, local_full_path = local_full_path, req_path = req_path) keywords = web.input().get("k") keywords = web.utils.safestr(keywords) title = "Search %s" % keywords if keywords: limit = config_agent.config.getint("pagination", "search_page_limit") lines = search.search_by_filename_and_file_content(keywords, limit = limit) if lines: buf = mdutils.sequence_to_unorder_list(seq = lines, **view_settings) else: buf = None else: buf = None if buf: content = mdutils.md2html(config_agent = config_agent, req_path = req_path, text = buf, static_file_prefix = static_file_prefix, **view_settings) else: content = "matched not found" static_files = static_file.get_global_static_files(**view_settings) return tpl_render.search(config_agent = config_agent, static_files = static_files, title = title, keywords = keywords, content = content)
def wp_delete(config_agent, tpl_render, req_path): folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") local_full_path = mdutils.req_path_to_local_full_path( req_path, folder_pages_full_path) path_info = web.ctx.environ["PATH_INFO"] if os.path.isfile(local_full_path): redirect_to = os.path.dirname(path_info) delete_page_file_by_full_path(local_full_path) elif os.path.isdir(local_full_path): redirect_to = os.path.dirname(commons.strutils.rstrip(path_info, "/")) delete_page_file_by_full_path(local_full_path) else: raise web.NotFound() cache.update_recent_change_cache(folder_pages_full_path) cache.update_all_pages_list_cache(folder_pages_full_path) return web.seeother(redirect_to)
def wp_create(config_agent, req_path, path, content): folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") local_full_path = mdutils.req_path_to_local_full_path(path, folder_pages_full_path) if path.endswith("/"): file_full_path = os.path.join(local_full_path, "index.md") else: file_full_path = local_full_path parent_path = os.path.dirname(file_full_path) if not os.path.exists(parent_path): os.makedirs(parent_path) with open(file_full_path, "w'") as f: f.write(content) cache.update_recent_change_cache(folder_pages_full_path) cache.update_all_pages_list_cache(folder_pages_full_path = folder_pages_full_path) web.seeother(path)
def wp_edit(config_agent, tpl_render, req_path): view_settings = get_view_settings(config_agent) view_settings["auto_toc"] = False view_settings["highlight_code"] = False view_settings["reader_mode"] = False static_files = static_file.get_global_static_files(**view_settings) full_path = mdutils.req_path_to_local_full_path(req_path, folder_pages_full_path) if config_agent.config.get("frontend", "button_mode_path"): buf = mdutils.text_path_to_button_path("/%s" % req_path) title = mdutils.md2html(config_agent=config_agent, req_path=req_path, text=buf, **view_settings) else: title = req_path create_new = False if os.path.isfile(full_path): content = commons.shutils.cat(full_path) elif os.path.isdir(full_path): content = get_dot_idx_content_by_full_path(full_path) elif not os.path.exists(full_path): create_new = True content = "" else: raise Exception("invalid path '%s'" % req_path) return tpl_render.editor( config_agent=config_agent, static_files=static_files, req_path=req_path, create_new=create_new, title=title, content=content, **view_settings )
def wp_create(config_agent, req_path, path, content): folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") local_full_path = mdutils.req_path_to_local_full_path( path, folder_pages_full_path) if path.endswith("/"): file_full_path = os.path.join(local_full_path, "index.md") else: file_full_path = local_full_path parent_path = os.path.dirname(file_full_path) if not os.path.exists(parent_path): os.makedirs(parent_path) with open(file_full_path, "w'") as f: f.write(content) cache.update_recent_change_cache(folder_pages_full_path) cache.update_all_pages_list_cache( folder_pages_full_path=folder_pages_full_path) web.seeother(path)
def wp_read(config_agent, tpl_render, req_path): view_settings = get_view_settings(config_agent) folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") local_full_path = mdutils.req_path_to_local_full_path(req_path, folder_pages_full_path) HOME_PAGE = "" if req_path != HOME_PAGE and view_settings["button_mode_path"]: buf = mdutils.text_path_to_button_path("/%s" % req_path) button_path = mdutils.md2html(config_agent=config_agent, req_path=req_path, text=buf) else: button_path = None view_settings["show_quick_links"] = False if os.path.isfile(local_full_path): content = commons.shutils.cat(local_full_path) content = commons.strutils.strip_bom(content) elif os.path.isdir(local_full_path): # try /path/to/folder/index content = get_dot_idx_content_by_full_path(local_full_path) or "" if not content: # try /path/to/folder/* content = get_page_file_list_by_req_path(folder_pages_full_path=folder_pages_full_path, req_path=req_path) if content: content = mdutils.sequence_to_unorder_list(content.split("\n"), **view_settings) else: # /index does not exists if req_path == HOME_PAGE: return web.seeother("~all") else: return web.seeother("/%s?action=edit" % req_path) title = mdutils.get_title_from_md(full_path=local_full_path) content = mdutils.md2html(config_agent=config_agent, req_path=req_path, text=content) static_files = get_the_same_folders_cssjs_files( req_path=req_path, local_full_path=local_full_path, folder_pages_full_path=folder_pages_full_path ) if not static_files: static_files = static_file.get_global_static_files(**view_settings) + "\n" buf = tpl_render.canvas( config=config_agent.config, static_files=static_files, button_path=button_path, req_path=req_path, title=title, content=content, **view_settings ) # try: # buf = tpl_render.canvas(config = config_agent.config, # static_files = static_files, button_path = button_path, content = content, # **view_settings) # except TypeError, ex: # logging.error(str(ex)) # buf = "Rendering template for '%s' failed. \n" % req_path +\ # "try following command override old templtes files and fix it: \n\n" +\ # " zwadmin.py upgrade <full path of instance>" + "\n" # logging.error(buf) return buf
def wp_read(config_agent, tpl_render, req_path): view_settings = get_view_settings(config_agent) folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") local_full_path = mdutils.req_path_to_local_full_path( req_path, folder_pages_full_path) static_file_prefix = static_file.get_static_file_prefix_by_local_full_path( config_agent=config_agent, local_full_path=local_full_path, req_path=req_path) path_info = web.ctx.environ["PATH_INFO"] HOME_PAGE = "" if req_path != HOME_PAGE and view_settings["button_mode_path"]: buf = mdutils.text_path_to_button_path("/%s" % req_path) button_path = mdutils.md2html(config_agent=config_agent, req_path=req_path, text=buf, static_file_prefix=static_file_prefix, **view_settings) else: button_path = None view_settings["show_quick_links"] = False title = "" if os.path.isfile(local_full_path): # os.path.exists(local_full_path) buf = commons.shutils.cat(local_full_path) buf = commons.strutils.strip_bom(buf) title = mdutils.get_title_by_file_path_in_md( folder_pages_full_path=folder_pages_full_path, file_path_suffix=local_full_path) elif os.path.isdir(local_full_path): # os.path.exists(local_full_path) if req_path == HOME_PAGE: a = os.path.join(local_full_path, "index.md") b = os.path.join(local_full_path, "index.markdown") if os.path.exists(a) or os.path.exists(b): fixed_req_path = os.path.join(path_info, "index") return web.seeother(fixed_req_path) else: fixed_req_path = os.path.join(path_info, "~all") return web.seeother(fixed_req_path) else: # listdir /path/to/folder/* buf = shell.get_page_file_list_by_req_path( folder_pages_full_path=folder_pages_full_path, req_path=req_path) if buf: buf = mdutils.sequence_to_unorder_list( folder_pages_full_path=folder_pages_full_path, seq=buf.split("\n"), **view_settings) title = req_path else: buf = "folder `%s` exists, but there is no files" % path_info else: # not os.path.exists(local_full_path) readonly = config_agent.config.get("main", "readonly") if readonly: raise web.Forbidden() else: if path_info.endswith("/"): fixed_req_path = path_info + "index?action=update" else: fixed_req_path = path_info + "?action=update" return web.seeother(fixed_req_path) content = mdutils.md2html(config_agent=config_agent, req_path=req_path, text=buf, static_file_prefix=static_file_prefix, **view_settings) static_files = get_the_same_folders_cssjs_files( req_path=req_path, local_full_path=local_full_path, folder_pages_full_path=folder_pages_full_path) if not static_files: static_files = static_file.get_global_static_files( **view_settings) + "\n" buf = tpl_render.canvas(config=config_agent.config, static_files=static_files, button_path=button_path, req_path=req_path, title=title, content=content, **view_settings) return buf
def wp_read(config_agent, tpl_render, req_path): view_settings = get_view_settings(config_agent) folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") local_full_path = mdutils.req_path_to_local_full_path(req_path, folder_pages_full_path) static_file_prefix = static_file.get_static_file_prefix_by_local_full_path(config_agent = config_agent, local_full_path = local_full_path, req_path = req_path) path_info = web.ctx.environ["PATH_INFO"] HOME_PAGE = "" if req_path != HOME_PAGE and view_settings["button_mode_path"]: buf = mdutils.text_path_to_button_path("/%s" % req_path) button_path = mdutils.md2html(config_agent = config_agent, req_path = req_path, text = buf, static_file_prefix = static_file_prefix, **view_settings) else: button_path = None view_settings["show_quick_links"] = False if os.path.isfile(local_full_path): # os.path.exists(local_full_path) buf = commons.shutils.cat(local_full_path) buf = commons.strutils.strip_bom(buf) elif os.path.isdir(local_full_path): # os.path.exists(local_full_path) if req_path == HOME_PAGE: a = os.path.join(local_full_path, "index.md") b = os.path.join(local_full_path, "index.markdown") if os.path.exists(a) or os.path.exists(b): fixed_req_path = os.path.join(path_info, "index") return web.seeother(fixed_req_path) else: fixed_req_path = os.path.join(path_info, "~all") return web.seeother(fixed_req_path) else: # listdir /path/to/folder/* buf = shell.get_page_file_list_by_req_path(folder_pages_full_path = folder_pages_full_path, req_path = req_path) if buf: buf = mdutils.sequence_to_unorder_list(buf.split("\n"), **view_settings) else: buf = "folder `%s` exists, but there is no files" % path_info else: # not os.path.exists(local_full_path) if path_info.endswith("/"): fixed_req_path = path_info + "index?action=update" else: fixed_req_path = path_info + "?action=update" return web.seeother(fixed_req_path) title = mdutils.get_title_from_md(local_full_path = local_full_path) content = mdutils.md2html(config_agent = config_agent, req_path = req_path, text = buf, static_file_prefix = static_file_prefix, **view_settings) static_files = get_the_same_folders_cssjs_files(req_path = req_path, local_full_path = local_full_path, folder_pages_full_path = folder_pages_full_path) if not static_files: static_files = static_file.get_global_static_files(**view_settings) + "\n" buf = tpl_render.canvas(config = config_agent.config, static_files = static_files, button_path = button_path, req_path = req_path, title = title, content = content, **view_settings) return buf
def POST(self, req_path): req_path = cgi.escape(req_path) inputs = web.input() action = inputs.get("action") if (not action) or (action not in ("edit", "rename")): raise web.BadRequest() content = inputs.get("content") content = web.utils.safestr(content) folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") # NOTICE: if req_path == `users/`, full_path will be `/path/to/users/`, # its parent will be `/path/to/users`. full_path = mdutils.req_path_to_local_full_path(req_path, folder_pages_full_path) parent = os.path.dirname(full_path) if not os.path.exists(parent): os.makedirs(parent) if action == "edit": page.update_page_by_req_path(req_path = req_path, content = content) web.seeother("/%s" % req_path) return elif action == "rename": new_path = inputs.get("new_path") if not new_path: raise web.BadRequest() old_full_path = mdutils.req_path_to_local_full_path(req_path, folder_pages_full_path) if os.path.isfile(old_full_path): new_full_path = mdutils.req_path_to_local_full_path(new_path) elif os.path.isdir(old_full_path): folder_pages_full_path = config_agent.get_full_path("paths", "pages_path") new_full_path = os.path.join(folder_pages_full_path, new_path) else: raise Exception("un-expected path '%s'" % req_path) if os.path.exists(new_full_path): err_info = "WARNING: The page %s already exists" % new_full_path return tpl_render.rename(req_path, err_info, static_files = static_file.g_global_static_files) parent = os.path.dirname(new_full_path) if not os.path.exists(parent): os.makedirs(parent) shutil.move(old_full_path, new_full_path) cache.update_all_pages_list_cache() cache.update_recent_change_cache() if os.path.isfile(new_full_path): web.seeother("/%s" % new_path) return elif os.path.isdir(new_full_path): web.seeother("/%s/" % new_path) return else: raise Exception("un-expected path '%s'" % new_path) url = os.path.join("/", req_path) web.redirect(url) return