def edit(filename): """ Possible actions: [post rename, edit markdown] """ if request.method == 'GET': location = get_location(filename) if location: with open(os.path.join(blog_dir, location, filename), 'r', 'utf-8') as f: markdown = f.read() if location == 'publish': action = 'Save and Publish' else: action = 'Save' edit_commits = hg_edit_commits(filename) try: past = edit_commits[1]['node'] except IndexError: past = None return render_template('edit_post.html', filename=filename, markdown=markdown, action=action, past=past) else: flash(MSG_POST_NOT_FOUND.format(filename[:-3])) return redirect(url_for('posts')) elif request.method == 'POST': new_filename = request.form['filename'] if not new_filename.endswith('.md'): new_filename += '.md' markdown = request.form['markdown'] action = request.form['action'] valid_filename = is_valid_filename(filename) if not valid_filename: flash(MSG_POST_FILENAME_INVALID) elif not is_valid_post(markdown, valid_filename['published']): flash(MSG_POST_CONTENT_INVALID) else: location = get_location(filename) if not (new_filename == filename) and get_location(new_filename): flash(MSG_POST_EXISTS) else: with open(os.path.join(blog_dir, location, filename), 'w', 'utf-8') as f: f.write(markdown) if new_filename == filename: hg_commit(os.path.join(blog_dir, location, filename), 'edited post {}'.format(filename)) else: hg_rename( os.path.join(blog_dir, location, filename), os.path.join(blog_dir, location, new_filename) ) flash(MSG_POST_SAVED.format(new_filename[:-3])) if 'Publish' in action: bake_blog() return redirect(url_for('posts')) return render_template('edit_post.html', filename=filename, markdown=markdown, action=action)
def move(filename): location = get_location(filename) if location: new_location = get_location(filename, True) if new_location == 'drafts': dest = 'drafts' flash(MSG_POST_DRAFTED.format(filename[:-3])) else: dest = 'publish' flash(MSG_POST_PUBLISHED.format(filename[:-3])) hg_move(os.path.join(blog_dir, location, filename), os.path.join(blog_dir, new_location, filename), dest) bake_blog() else: flash(MSG_POST_NOT_FOUND.format(filename[:-3])) return redirect(url_for('posts'))
def remove(filename): location = get_location(filename) if location: hg_remove(os.path.join(blog_dir, location, filename)) flash(MSG_POST_REMOVED.format(filename[:-3])) if location == 'publish': bake_blog() else: flash(MSG_POST_NOT_FOUND.format(filename[:-3])) return redirect(url_for('posts'))
def hg_remove(source): face = ui.ui() repo = hg.repository(face, blog_dir) location = get_location(os.path.basename(source)) commands.remove(face, repo, source) commands.commit(face, repo, source, user=g.blog['AUTHOR'], message='deleted post {}'.format(os.path.basename(source))) if not os.path.exists(os.path.dirname(source)): os.mkdir(os.path.dirname(source))
def edit_revision(filename, revision): location = get_location(filename) if location == 'publish': action = 'Save and Publish' else: action = 'Save' # We will only show 'edit' commits done to the user edit_commits = hg_edit_commits(filename) for i, commit in enumerate(edit_commits): if commit['node'] == revision: try: past = edit_commits[i+1]['node'] except IndexError: past = None try: future = edit_commits[i-1]['node'] except IndexError: future = None if i == 0: future = None # Track the location of the post file moved = None for i, commit in enumerate(hg_commits(filename)): if commit['move']: moved = commit['move'].split()[1][1:-1] if commit['node'] == revision: if commit['move']: moved = commit['move'].split()[0] break # hg_revision takes paths relative to the repo root # thanks to ronny@#mercurial if moved: relpath = moved else: relpath = os.path.join(get_location(filename), filename) markdown = hg_revision(relpath, revision).decode('utf-8') return render_template('edit_post.html', filename=filename, markdown=markdown, action=action, past=past, future=future)
def new(): """ Actions are sent as forms, and redirects are made by `referer` """ if request.method == 'GET': filename, markdown = default_post() return render_template('new_post.html', filename=filename, markdown=markdown) elif request.method == 'POST': filename = request.form['filename'] if not filename.endswith('.md'): filename += '.md' markdown = request.form['markdown'] action = request.form['action'] valid_filename = is_valid_filename(filename) if not valid_filename: flash(MSG_POST_FILENAME_INVALID) elif get_location(filename): flash(MSG_POST_EXISTS) elif not is_valid_post(markdown, valid_filename['published']): flash(MSG_POST_CONTENT_INVALID) else: if 'Draft' in action: dest = 'drafts' flash(MSG_POST_SAVED.format(filename[:-3])) elif 'Publish' in action: dest = 'publish' flash(MSG_POST_PUBLISHED.format(filename[:-3])) with open(os.path.join(blog_dir, dest, filename), 'w', 'utf-8') as f: f.write(markdown) if 'Publish' in action: bake_blog() source = os.path.join(blog_dir, get_location(filename)) hg_add(source) hg_commit(source, 'new post {} in {}'.format(filename, dest)) return redirect(url_for('posts')) return render_template(request.form['referer'], filename=filename, markdown=markdown)
def hg_commits(filename): face = ui.ui() repo = hg.repository(face, blog_dir) location = get_location(filename) face.pushbuffer() commands.log(face, repo, os.path.join(blog_dir, location, filename), date='', rev=[], follow=True, template="node:{node}\ndesc:{desc}\nmove:{file_copies}\n\n") output = face.popbuffer() commit = [] commits = [] for line in output.splitlines(): if line: commit.append(line.split(':')) else: commits.append(dict(commit)) commit = [] return commits