Skip to content

Instantly share code, notes, and snippets.

@junmakii
Created May 10, 2015 14:55
Show Gist options
  • Select an option

  • Save junmakii/cd5b879109843496aa1b to your computer and use it in GitHub Desktop.

Select an option

Save junmakii/cd5b879109843496aa1b to your computer and use it in GitHub Desktop.
#!/usr/bin/python
#-*- coding: utf-8 -*-
from __future__ import print_function
import os, sys, yaml, re, uuid
import datetime, argparse
import jinja2
class AsiaTokyoTimezone(datetime.tzinfo):
def utcoffset(self, dt):
return datetime.timedelta(hours=9)
def dst(self, dt):
return datetime.timedelta(hours=9)
def tzname(self, dt):
return 'Asia/Tokyo'
TZ = AsiaTokyoTimezone()
s ='''---
title: {{ page.title }}
summary: {{ page.summary or "null" }}
category: {{ page.category or "null" }}
tags: {{ page.tags or "[]" }}
date: {{ page.current_time }}
updated: {{ page.current_time }}
lang: {{ page.lang or site.lang or "en" }}
image: {{ page.image or "null" }}
layout: {{ page.layout or "default" }}
published: {{ page.published or "true" }}
permalink: {{ page.published or "null" }}
author: {{ site.author.name or "Anonymous" }}
uuid: {{ page.uuid }}
---'''
BASE_DIR = os.path.abspath(os.getcwd())
DEFAULT_DIR = os.path.join(BASE_DIR)
DEFAULT_CONF_FILE = os.path.join(BASE_DIR, '_config.yml')
def parse_args(*args):
parser = argparse.ArgumentParser(description='')
parser.add_argument('title', action='store')
parser.add_argument('-f', '--template', action='store')
parser.add_argument('-t', '--timestamp', default=False, action='store_true')
parser.add_argument('-c', '--config', default=DEFAULT_CONF_FILE, action='store')
parser.add_argument('-o', '--destination', default=DEFAULT_DIR, action='store')
args = parser.parse_args(args)
return args
def write(dest, data):
if not os.path.exists(dest):
with open(dest, 'w') as fp:
fp.write(data.encode('utf8'))
else:
print('ERROR: File already exists.', file=sys.stderr)
def main(*args):
args = parse_args(*args)
timestamp = args.timestamp
time_now = datetime.datetime.now(tz=TZ)
time_date = time_now.strftime('%Y-%m-%d')
title_lower = args.title.lower()
title = re.sub(r' ', '-', title_lower)
if timestamp:
file_name = time_date + '-' + title + '.md'
else:
file_name = title + '.md'
#dst_dir = os.path.dirname(os.path.abspath(__file__))
dest = os.path.join(args.destination, file_name)
config_context = {}
config_context.update(yaml.load(open(DEFAULT_CONF_FILE, 'r').read()))
if args.template:
with open(args.template, 'r') as fp:
template_data = fp.read()
else:
template_data = s
template_context = {}
template_context.update({'page': {'title': args.title,
'current_time': time_now,
'uuid': str(uuid.uuid4())}})
template_context.update({'site': config_context})
template_result = jinja2.Template(template_data).render(template_context)
print(dest)
write(dest, template_result)
return 0
if __name__ == '__main__':
sys.exit(main(*sys.argv[1:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment