# HG changeset patch # User Brian Neal # Date 1391368035 21600 # Node ID 75a003a548c496e2ef24a3c9010d1bebb3d94d6c # Parent 6c03ca07a16d67618db722886605d9b0e2051b44 Added a script to create a new post by answering questions. diff -r 6c03ca07a16d -r 75a003a548c4 fabfile.py --- a/fabfile.py Sun Feb 02 11:32:13 2014 -0600 +++ b/fabfile.py Sun Feb 02 13:07:15 2014 -0600 @@ -58,3 +58,8 @@ local_dir=DEPLOY_PATH.rstrip('/') + '/', delete=True ) + +def new_post(): + """Create a new post by answering questions interactively""" + local('python tools/new_post.py') + diff -r 6c03ca07a16d -r 75a003a548c4 tools/new_post.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/new_post.py Sun Feb 02 13:07:15 2014 -0600 @@ -0,0 +1,98 @@ +"""A script to help create new blog posts by asking the user some questions and +then creating a new .rst file accordingly. + +""" +import datetime +import os +import pwd +import re +import subprocess + + +POST = """\ +{title} +{title_under} + +:date: {date} +:tags: {tags} +:slug: {slug} +:author: {author} + +""" + + +def slugify(s): + """Return a slug from the string s.""" + + slug = s.lower() + + # convert ellipses to spaces + slug = re.sub(r'\.{2,}', ' ', slug) + + # flatten everything non alpha or . into a single - + slug = re.sub(r'[^0-9a-zA-Z\.]+', '-', slug) + + # trim off leading/trailing - + slug = re.sub(r'^-+|-+$', '', slug) + return slug + + +def main(): + + title = raw_input("Blog title: ") + title_under = '#' * len(title) + + # Usually takes me about an hour to write a blog post: + anticipated_date = datetime.datetime.now() + datetime.timedelta(hours=1) + default_date = anticipated_date.strftime('%Y-%m-%d %H:%M') + date_str = raw_input("Publish date [{}]: ".format(default_date)) + date_str = date_str if date_str else default_date + + tags = raw_input("Tags (comma separated): ") + + default_slug = slugify(title) + slug = raw_input("Slug [{}]: ".format(default_slug)) + slug = slug if slug else default_slug + + # Author; yes I could just hardcode my name here... ;) + user = os.environ.get('USER', '') + if user: + user = pwd.getpwnam(user)[4].split(',')[0] + + default_author = user + author = raw_input('Author [{}]: '.format(default_author)) + author = author if author else default_author + + post = POST.format(title=title, title_under=title_under, date=date_str, + tags=tags, slug=slug, author=author) + + # Create a file for the new post. + # This is all specific to my convention of put posts under a category + # directory and having the first 3 characters in the filename be a number + # (000-999). + category = raw_input('Category [Coding]: ') + category = category if category else 'Coding' + + post_dir = os.path.join('content', category) + posts = sorted(os.listdir(post_dir)) + if posts: + last_post_num = int(posts[-1][:3]) + num = last_post_num + 1 + else: + num = 0 + + new_name = '{num:03}-{slug}.rst'.format(num=num, slug=slug) + new_path = os.path.join('content', category, new_name) + + # Write file: + with open(new_path, 'w') as fp: + fp.write(post) + + # Launch editor if set + editor = os.environ.get('VISUAL', os.environ.get('EDITOR')) + if editor: + subprocess.call([editor, new_path]) + + +if __name__ == '__main__': + main()