-
Notifications
You must be signed in to change notification settings - Fork 0
/
jpost
executable file
·163 lines (136 loc) · 5.57 KB
/
jpost
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/python
"""
jpost - this is a simple script that will create and initialize a new
Jekyll post file. The default is a markdown file (.md) and the yaml options
listed below will be part of the initialization.
jpost [-w] [-d date] [title]
title the title of the post wrapped in quotes, e.g. "post title here"
-c the category in which this post should happen. Options are notes, longform or reading.
-d the default date is today's date or this option can be used to specify
a date. The date must be specified in the format YYYY-MM-DD.
-n the name for the post file instead of using the automatically generated one.
-t the tags that should be added to the yaml portion of the file separated
by spaces and the whole thing wrapped in quotes.
-w this optional flag will signal the file to be opened for writing in vi.
yaml options
---
layout: notes-post
category: [category]
title: [title]
location: [location]
date: 2016-05-05
---
"""
import argparse
import datetime
import subprocess
import os.path
import sys
import requests as rq
import json
DEFAULT_EXT = 'md'
def main():
# parse the arguments
parser = argparse.ArgumentParser(description='Create a new Jekyll post template')
parser.add_argument('title', help='the title of the post wrapped in quotes.')
parser.add_argument('-d', '--date', help='specify the post date in the format YYYY-MM-DD, otherwise today is the default date.')
parser.add_argument('-n', '--name', help='specify the name of the post file instead of the automatically generated one. It is a best practice for the words to be separated by hyphens. Also note that the given name will be prepended with the date so as to conform to Jekyll naming requirements.')
parser.add_argument('-t', '--tags', help='specify a set of tags for the post separated by spaces with the entire set wrapped in quotes (e.g. "tag1 tag2 tag3").')
parser.add_argument('-c', '--category', help='specify a category. notes, longform, or reading.')
parser.add_argument('-l', '--location', help='specify a location. Otherwise a location will be read from your IP address.')
parser.add_argument('-w', '--write', action="store_true",
help='signals if this file should be opened for writing after creation')
args = parser.parse_args()
title = args.title
# exchange spaces for dashes
# Use title if a name isn't specified
# ---------------------------------------
dash_title = ''
if args.name:
dash_title = args.name.replace(' ','-')
else:
dash_title = title.replace(' ','-')
# clean up any special characters from the dash-title
# ---------------------------------------
temp_title = '';
for char in dash_title:
if char.isalnum() or char == '-':
temp_title += char
dash_title = temp_title
# Adding a date to the post
# Perhaps the most annoying bit of Jekyll
# ---------------------------------------
date = None
if args.date:
date = args.date
else:
# use today's date
today = datetime.datetime.now()
year = str(today.year)
month = str(today.month)
if len(month) == 1:
month = '0' + month
#month = today.month > 9 ? str(today.month) : '0' + str(today.month)
day = str(today.day)
if len(day) == 1:
day = '0' + day
#day = today.day > 9 ? str(today.day) : '0' + str(today.day)
date = "-".join([year,month,day])
filename = date + '-' + dash_title + '.' + DEFAULT_EXT
# With the right category, move it to the correct location
# --------------------------------------------------------
if args.category is None:
args.category = "notes"
if args.category == "notes":
filename = "notes/_posts/" + filename
if args.category == "longform":
filename = "longform/_posts/" + filename
if args.category == "reading":
filename = "reading/_posts/" + filename
# check if the desired file already exists
if os.path.exists(filename):
# file already exists, abort the program
print filename + ' already exists.'
print 'Jekyll post NOT created.'
sys.exit(1)
# collect any tags included in the args
# [TODO there is a bug here]
# -------------------------------------------
tags = []
if args.tags:
tags = args.tags.split(' ')
# Add location
# I like to add a location to my posts as "city, state"
# ---------------------------------------------------------
z = rq.get("http://ipinfo.io")
x = json.loads(z.text)
location = x['city'] + ", " + x['region']
# type
category = args.category
# try writing yaml to the post file
try:
f = open(filename, 'w')
# write yaml to file
try:
f.write('---\n')
f.write('layout: notes-post\n')
f.write('category: ' + category + '\n')
f.write('title: ' + title + '\n')
f.write('location: ' + '\"' + location + '\"' + '\n')
f.write('date: ' + date + '\n')
if tags:
f.write('tags:\n')
for tag in tags:
f.write('- ' + tag + '\n')
f.write('---\n\n\n')
finally:
f.close()
except IOError:
print 'Issue writing to Jekyll post file.'
# if write flag is on, then try to open file with vi
if args.write:
subprocess.call(['subl', filename])
# print confirmation statement
print 'New Jekyll post "' + title + '" has been created -- ' + filename
if __name__ == '__main__':
main()