def html_request(fpath): fpath = urllib.parse.unquote(fpath) fpath = legal_path(fpath, 'blog') if not fpath: abort(404) full_fpath = full_path(fpath, 'blog', app.config) full_fname = full_fpath + '.html' with open(full_fname, 'r', encoding='utf-8') as f: return f.read()
def blog_view(fpath, md_type): fpath = legal_path(fpath, md_type) if not fpath: abort(404) y, m, d, title = parse_path(fpath, md_type) return render_template('blog_view.html', fpath=fpath, md_type=md_type, title=title)
def data_request(fpath, md_type): fpath = urllib.parse.unquote(fpath) fpath = legal_path(fpath, md_type) if not fpath: abort(404) full_fpath = full_path(fpath, md_type, app.config) full_fname = full_fpath + '.md' with open(full_fname, 'r', encoding='utf-8') as f: return f.read()
def edit(fpath, md_type): fpath = legal_path(fpath, md_type) if not fpath: abort(404) y, m, d, title = parse_path(fpath, md_type) date = "{}-{}-{}".format(y, m, d) return render_template("editormd.html", md_path=fpath, date=date, title=title, md_type=md_type)
def delete_draft(fpath): fpath = legal_path(fpath, md_type='draft') if not fpath: abort(404) try: full_fpath = full_path(fpath, 'draft', app.config) os.remove(full_fpath + '.md') del_item(app.config['DRAFTS_INDEX'], fpath) flash('已删除') except Exception as e: flash('删除失败!请重试') return redirect(url_for("list_drafts")) return redirect(url_for("list_drafts"))
def save(): date = request.form['date'] title = request.form.get('title', 'untitled') if title == '': title = 'untitled' fpath = legal_path('{}-{}'.format(date, title), md_type='draft') if not fpath: abort(404) md = request.form['md'] full_fpath = full_path(fpath, 'draft', app.config) try: with open(full_fpath + '.md', 'w', encoding='utf-8') as mdf: mdf.write(md) add_item(app.config['DRAFTS_INDEX'], fpath) except: return '保存失败!' return '保存成功!'
def delete_blog(fpath): fpath = legal_path(fpath, md_type='blog') if not fpath: abort(404) full_fpath = full_path(fpath, 'blog', app.config) save_dir, title = os.path.split(full_fpath) try: os.remove(full_fpath + '.md') del_item(app.config['BLOGS_INDEX'], fpath) flist = os.listdir(save_dir) flash('删除成功!') for f in flist: if os.path.splitext(f)[1] == '.md': return redirect(url_for("list_blog")) os.removedirs(save_dir) except: flash('删除失败!') return redirect(url_for("list_blog")) return redirect(url_for("list_blog"))