Mercurial > public > pelican-blog
view tools/new_post.py @ 22:a893739e9587
Updates for SSL
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 21 Jul 2018 17:47:55 -0500 |
parents | 75a003a548c4 |
children |
line wrap: on
line source
"""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()