def publish(): date = request.form['date'] title = request.form.get('title', 'untitled') if title == '': title = 'untitled' y, m, d = date.split('-')[0:3] fpath = construct_fpath(title, y, m, d, md_type='blog') if not fpath: abort(404) full_fpath = full_path(fpath, 'blog', app.config) save_dir = os.path.dirname(full_fpath) if not os.path.exists(save_dir): os.makedirs(save_dir) draft_path = construct_fpath(title, y, m, d, md_type='draft') md = request.form['md'] html = request.form['html'] try: with open(full_fpath + '.md', 'w', encoding='utf-8') as mdf: mdf.write(md) with open(full_fpath + '.html', 'w', encoding='utf-8') as htmlf: htmlf.write(html) add_item(app.config['BLOGS_INDEX'], fpath) full_draft_path = full_path(draft_path, 'draft', app.config) if os.path.exists(full_draft_path + '.md'): os.remove(full_draft_path + '.md') del_item(app.config['DRAFTS_INDEX'], draft_path) flash('发表成功!') except: flash('发表失败!') return url_for("list_blog") return url_for("list_blog")
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 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 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"))