annotate tools/new_post.py @ 20:137650a0be4e

Add requirements.txt.
author Brian Neal <bgneal@gmail.com>
date Sat, 27 Aug 2016 13:04:58 -0500
parents 75a003a548c4
children
rev   line source
bgneal@11 1 """A script to help create new blog posts by asking the user some questions and
bgneal@11 2 then creating a new .rst file accordingly.
bgneal@11 3
bgneal@11 4 """
bgneal@11 5 import datetime
bgneal@11 6 import os
bgneal@11 7 import pwd
bgneal@11 8 import re
bgneal@11 9 import subprocess
bgneal@11 10
bgneal@11 11
bgneal@11 12 POST = """\
bgneal@11 13 {title}
bgneal@11 14 {title_under}
bgneal@11 15
bgneal@11 16 :date: {date}
bgneal@11 17 :tags: {tags}
bgneal@11 18 :slug: {slug}
bgneal@11 19 :author: {author}
bgneal@11 20
bgneal@11 21 """
bgneal@11 22
bgneal@11 23
bgneal@11 24 def slugify(s):
bgneal@11 25 """Return a slug from the string s."""
bgneal@11 26
bgneal@11 27 slug = s.lower()
bgneal@11 28
bgneal@11 29 # convert ellipses to spaces
bgneal@11 30 slug = re.sub(r'\.{2,}', ' ', slug)
bgneal@11 31
bgneal@11 32 # flatten everything non alpha or . into a single -
bgneal@11 33 slug = re.sub(r'[^0-9a-zA-Z\.]+', '-', slug)
bgneal@11 34
bgneal@11 35 # trim off leading/trailing -
bgneal@11 36 slug = re.sub(r'^-+|-+$', '', slug)
bgneal@11 37 return slug
bgneal@11 38
bgneal@11 39
bgneal@11 40 def main():
bgneal@11 41
bgneal@11 42 title = raw_input("Blog title: ")
bgneal@11 43 title_under = '#' * len(title)
bgneal@11 44
bgneal@11 45 # Usually takes me about an hour to write a blog post:
bgneal@11 46 anticipated_date = datetime.datetime.now() + datetime.timedelta(hours=1)
bgneal@11 47 default_date = anticipated_date.strftime('%Y-%m-%d %H:%M')
bgneal@11 48 date_str = raw_input("Publish date [{}]: ".format(default_date))
bgneal@11 49 date_str = date_str if date_str else default_date
bgneal@11 50
bgneal@11 51 tags = raw_input("Tags (comma separated): ")
bgneal@11 52
bgneal@11 53 default_slug = slugify(title)
bgneal@11 54 slug = raw_input("Slug [{}]: ".format(default_slug))
bgneal@11 55 slug = slug if slug else default_slug
bgneal@11 56
bgneal@11 57 # Author; yes I could just hardcode my name here... ;)
bgneal@11 58 user = os.environ.get('USER', '')
bgneal@11 59 if user:
bgneal@11 60 user = pwd.getpwnam(user)[4].split(',')[0]
bgneal@11 61
bgneal@11 62 default_author = user
bgneal@11 63 author = raw_input('Author [{}]: '.format(default_author))
bgneal@11 64 author = author if author else default_author
bgneal@11 65
bgneal@11 66 post = POST.format(title=title, title_under=title_under, date=date_str,
bgneal@11 67 tags=tags, slug=slug, author=author)
bgneal@11 68
bgneal@11 69 # Create a file for the new post.
bgneal@11 70 # This is all specific to my convention of put posts under a category
bgneal@11 71 # directory and having the first 3 characters in the filename be a number
bgneal@11 72 # (000-999).
bgneal@11 73 category = raw_input('Category [Coding]: ')
bgneal@11 74 category = category if category else 'Coding'
bgneal@11 75
bgneal@11 76 post_dir = os.path.join('content', category)
bgneal@11 77 posts = sorted(os.listdir(post_dir))
bgneal@11 78 if posts:
bgneal@11 79 last_post_num = int(posts[-1][:3])
bgneal@11 80 num = last_post_num + 1
bgneal@11 81 else:
bgneal@11 82 num = 0
bgneal@11 83
bgneal@11 84 new_name = '{num:03}-{slug}.rst'.format(num=num, slug=slug)
bgneal@11 85 new_path = os.path.join('content', category, new_name)
bgneal@11 86
bgneal@11 87 # Write file:
bgneal@11 88 with open(new_path, 'w') as fp:
bgneal@11 89 fp.write(post)
bgneal@11 90
bgneal@11 91 # Launch editor if set
bgneal@11 92 editor = os.environ.get('VISUAL', os.environ.get('EDITOR'))
bgneal@11 93 if editor:
bgneal@11 94 subprocess.call([editor, new_path])
bgneal@11 95
bgneal@11 96
bgneal@11 97 if __name__ == '__main__':
bgneal@11 98 main()