bgneal@11: """A script to help create new blog posts by asking the user some questions and bgneal@11: then creating a new .rst file accordingly. bgneal@11: bgneal@11: """ bgneal@11: import datetime bgneal@11: import os bgneal@11: import pwd bgneal@11: import re bgneal@11: import subprocess bgneal@11: bgneal@11: bgneal@11: POST = """\ bgneal@11: {title} bgneal@11: {title_under} bgneal@11: bgneal@11: :date: {date} bgneal@11: :tags: {tags} bgneal@11: :slug: {slug} bgneal@11: :author: {author} bgneal@11: bgneal@11: """ bgneal@11: bgneal@11: bgneal@11: def slugify(s): bgneal@11: """Return a slug from the string s.""" bgneal@11: bgneal@11: slug = s.lower() bgneal@11: bgneal@11: # convert ellipses to spaces bgneal@11: slug = re.sub(r'\.{2,}', ' ', slug) bgneal@11: bgneal@11: # flatten everything non alpha or . into a single - bgneal@11: slug = re.sub(r'[^0-9a-zA-Z\.]+', '-', slug) bgneal@11: bgneal@11: # trim off leading/trailing - bgneal@11: slug = re.sub(r'^-+|-+$', '', slug) bgneal@11: return slug bgneal@11: bgneal@11: bgneal@11: def main(): bgneal@11: bgneal@11: title = raw_input("Blog title: ") bgneal@11: title_under = '#' * len(title) bgneal@11: bgneal@11: # Usually takes me about an hour to write a blog post: bgneal@11: anticipated_date = datetime.datetime.now() + datetime.timedelta(hours=1) bgneal@11: default_date = anticipated_date.strftime('%Y-%m-%d %H:%M') bgneal@11: date_str = raw_input("Publish date [{}]: ".format(default_date)) bgneal@11: date_str = date_str if date_str else default_date bgneal@11: bgneal@11: tags = raw_input("Tags (comma separated): ") bgneal@11: bgneal@11: default_slug = slugify(title) bgneal@11: slug = raw_input("Slug [{}]: ".format(default_slug)) bgneal@11: slug = slug if slug else default_slug bgneal@11: bgneal@11: # Author; yes I could just hardcode my name here... ;) bgneal@11: user = os.environ.get('USER', '') bgneal@11: if user: bgneal@11: user = pwd.getpwnam(user)[4].split(',')[0] bgneal@11: bgneal@11: default_author = user bgneal@11: author = raw_input('Author [{}]: '.format(default_author)) bgneal@11: author = author if author else default_author bgneal@11: bgneal@11: post = POST.format(title=title, title_under=title_under, date=date_str, bgneal@11: tags=tags, slug=slug, author=author) bgneal@11: bgneal@11: # Create a file for the new post. bgneal@11: # This is all specific to my convention of put posts under a category bgneal@11: # directory and having the first 3 characters in the filename be a number bgneal@11: # (000-999). bgneal@11: category = raw_input('Category [Coding]: ') bgneal@11: category = category if category else 'Coding' bgneal@11: bgneal@11: post_dir = os.path.join('content', category) bgneal@11: posts = sorted(os.listdir(post_dir)) bgneal@11: if posts: bgneal@11: last_post_num = int(posts[-1][:3]) bgneal@11: num = last_post_num + 1 bgneal@11: else: bgneal@11: num = 0 bgneal@11: bgneal@11: new_name = '{num:03}-{slug}.rst'.format(num=num, slug=slug) bgneal@11: new_path = os.path.join('content', category, new_name) bgneal@11: bgneal@11: # Write file: bgneal@11: with open(new_path, 'w') as fp: bgneal@11: fp.write(post) bgneal@11: bgneal@11: # Launch editor if set bgneal@11: editor = os.environ.get('VISUAL', os.environ.get('EDITOR')) bgneal@11: if editor: bgneal@11: subprocess.call([editor, new_path]) bgneal@11: bgneal@11: bgneal@11: if __name__ == '__main__': bgneal@11: main()