Skip to content

Instantly share code, notes, and snippets.

@mikeyakymenko
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save mikeyakymenko/9238542 to your computer and use it in GitHub Desktop.

Select an option

Save mikeyakymenko/9238542 to your computer and use it in GitHub Desktop.
simple generate file for blog post, for FlaskFlatpages
#!/usr/bin/env python
# coding: utf-8
# usage
# clone file
# change content_path variable
# chmod +x blogadd
# mv blogadd /usr/local/bin/blogadd
import os
import sys
import datetime
from slugify import slugify
datenow = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
time = datetime.datetime.now().strftime('%H:%M:%S')
content_path = '/Users/michael/macgera/src/content/blog/'
base_current_dir = datetime.datetime.now().strftime('%Y/%m/')
def main(postname=None, tags=None, date=None):
os.chdir(content_path)
postname = raw_input('Топик: ')
base_file = raw_input('Файл: ')
tags = raw_input('Тэги: ')
date = raw_input('дата: ')
if date:
current_date = '{} {}'.format(date, time)
pre_dir = date.split('-')
current_dir = '{}/{}/'.format(date.split('-')[0], date.split('-')[1])
else:
current_date = datenow
current_dir = base_current_dir
if not os.path.exists(current_dir):
os.makedirs(current_dir)
os.chdir(current_dir)
if base_file:
filename = slugify(base_file, to_lower=True)
else:
filename = slugify(postname, to_lower=True)
fileready = filename + '.md'
new_file_template = 'title: %s\ndate: %s\ntags: [%s]\n\n' % (postname, current_date, tags)
new_file = open(fileready, 'a')
new_file.write(new_file_template)
new_file.close()
open_command = 'open -a iA\ Writer %s' % (content_path + current_dir + fileready)
os.system(open_command)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment