-
Notifications
You must be signed in to change notification settings - Fork 27
/
jekyll-post-template.py
executable file
·101 lines (81 loc) · 2.4 KB
/
jekyll-post-template.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import getopt
import time
def show_usage():
print 'A simple script to generate jekyll post template\n'
print '\tUsage: ', sys.argv[0], '[options]\n'
print '\t-h|--help show this help'
print '\t-v|--verbose verbose mode'
print '\t-t|--title specify the title of your blog, e.g \'My blog title\''
print '\t-c|--categories specify the categories of your blog'
print '\t-o|--output specify output file name, will write to a default file name if not specify, - stand for stdout\n'
sys.exit(0)
def error_action():
"""TODO: Docstring for error_action.
:returns: TODO
"""
print 'Error happened\n'
sys.exit(0)
d = {}
d['-t'] = ''
d['--title'] = ''
d['-c'] = ''
d['--categories'] = ''
d['-v'] = 0
d['--verbose'] = 0
d['-o'] = ''
d['--output'] = ''
verbose = False
title = ''
categories = ''
def main():
try:
# Short option syntax: "hv:"
# Long option syntax: "help" or "verbose="
opts, args = getopt.getopt(sys.argv[1:], "hv:t:c:o:", ['help', 'title=', 'categories=', 'verbose=', 'output='])
except getopt.GetoptError, err:
# Print debug info
show_usage()
for option, argument in opts:
if option in ("-h", "--help"):
show_usage()
return
elif option in d.keys():
d[option] = argument
else:
show_usage()
if int(d.get('-v')) == 1 or int(d.get('--verbose')) == 1:
verbose = True
else:
verbose = False
if len(d.get('-t')) > 0:
title = d.get('-t')
if len(d.get('--title')) > 0:
title = d.get('--title')
if len(d.get('-c')) > 0:
categories = d.get('-c')
if len(d.get('--categories')) > 0:
categories = d.get('--categories')
if not 'title' in locals() or not 'categories' in locals():
show_usage()
content = '---\nlayout: post\ntitle: {0}\ndate: {1}\ncategories: {2}\n---\n'.format(title, \
time.strftime('%F %T %z', time.gmtime()), categories)
filename = time.strftime('%F', time.localtime()) + '-' + title.lower().replace(' ', '-') + '.markdown'
if len(d.get('--output')) > 0 or len(d.get('-o')) > 0:
if d.get('--output') == '-' or d.get('-o') == '-':
print filename
sys.stdout.write(content)
return
else:
if len(d.get('--output')) > 0:
filename = d.get('--output')
if len(d.get('-o')) > 0:
filename = d.get('-o')
f = open(filename, 'w+')
f.write(content)
f.flush()
f.close()
if __name__ == "__main__":
main()