示例#1
0
文件: views.py 项目: limeburst/yak
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)
示例#2
0
文件: __init__.py 项目: limeburst/yak
 def process_IN_CREATE(self, event):
     if is_valid_filename(event.name) or event.name == 'publish':
         with open(os.path.join(self.blog_dir, 'yak.log'), 'w') as f:
             try:
                 bake(self.blog_dir)
             except ValueError:
                 f.write("You don't have anything to publish!")
             except:
                 f.write(traceback.format_exc())
                 f.write("\nBlog baking failed!")
             else:
                 f.write(strftime("%a, %d %b %Y %H:%M:%S +0000",
                     gmtime()))
                 f.write("\nBlog baking success!")
         if event.name == 'publish':
             try:
                 os.remove(event.pathname)
             except:
                 os.rmdir(event.pathname)
示例#3
0
文件: views.py 项目: limeburst/yak
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)
示例#4
0
文件: utils.py 项目: limeburst/yak
def medialist():
    medialist = []
    for filename in os.listdir(os.path.join(blog_dir, 'publish')):
        if not is_valid_filename(filename):
            medialist.append(filename)
    return medialist
示例#5
0
文件: utils.py 项目: limeburst/yak
def postlist(post_dir):
    postlist = []
    for filename in os.listdir(post_dir):
        if is_valid_filename(filename):
            postlist.append(filename)
    return sorted(postlist)