Example #1
0
File: main.py Project: rui/ZWiki
def get_dot_idx_content_by_fullpath(fullpath):
    dot_idx_fullpath = osp.join(fullpath, ".index.md")
    return zsh_util.cat(dot_idx_fullpath)
Example #2
0
File: main.py Project: rui/ZWiki
    def GET(self, req_path):
        req_path = cgi.escape(req_path)
        inputs = web.input()
        action = inputs.get("action", "read")

        if action and action not in ("edit", "read", "rename", "delete"):
            raise web.BadRequest()

        fullpath = get_page_file_or_dir_fullpath_by_req_path(req_path)

        if conf.use_button_mode_path:
            buf = zmarkdown_utils.convert_text_path_to_button_path("/%s" % req_path)
            title = zmarkdown_utils.markdown(buf)
        else:
            title = req_path

        if osp.isfile(fullpath):
            work_fullpath = osp.dirname(fullpath)
            static_file_prefix = osp.join("/static/pages", osp.dirname(req_path))
        elif osp.isdir(fullpath):
            work_fullpath = fullpath
            static_file_prefix = osp.join("/static/pages", req_path)            

        if action == "read":
            if osp.isfile(fullpath):
                content = zsh_util.cat(fullpath)
            elif osp.isdir(fullpath):
                dot_idx_content = get_dot_idx_content_by_fullpath(fullpath)
                page_file_list_content = get_page_file_list_content_by_fullpath(fullpath)
                content = ""

                if dot_idx_content:
                    content = dot_idx_content
                if page_file_list_content:
                    content = "%s\n\n----\n%s" % (content, page_file_list_content)
            else:
                web.seeother("/%s?action=edit" % req_path)
                return
            
            content = zmarkdown_utils.markdown(text=content,
                                               work_fullpath=work_fullpath,
                                               static_file_prefix=static_file_prefix)

            static_files = DEFAULT_GLOBAL_STATIC_FILES
            static_files = "%s\n    %s" % (static_files, get_the_same_folders_cssjs_files(req_path))

            return t_render.canvas(req_path=req_path, title=title, content=content, static_files=static_files)
        elif action == "edit":
            if osp.isfile(fullpath):
                content = zsh_util.cat(fullpath)
            elif osp.isdir(fullpath):
                content = get_dot_idx_content_by_fullpath(fullpath)
            elif not osp.exists(fullpath):
                content = ""
            else:
                raise Exception("unknow path")

            static_files = DEFAULT_GLOBAL_STATIC_FILES
            filepath = osp.join("/static", "css", "pagedown.css")
            static_files = _append_static_file(static_files, filepath, file_type="css", add_newline=True)                        
            filepath = osp.join("/static", "js", "editor.js")
            static_files = _append_static_file(static_files, filepath, file_type="js", add_newline=True)

            return t_render.editor(title, content, static_files=static_files)
        elif action == "rename":
            if not osp.exists(fullpath):
                raise web.NotFound()

            return t_render.rename(req_path, static_files=DEFAULT_GLOBAL_STATIC_FILES)
        elif action == "delete":
            delete_page_file_by_fullpath(fullpath)

            web.seeother("/")
            return

        raise web.BadRequest()