-
Notifications
You must be signed in to change notification settings - Fork 0
/
new-post.py
executable file
·79 lines (57 loc) · 1.85 KB
/
new-post.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
#!/bin/python3
from datetime import datetime
from sys import argv
from os import rename, path, listdir
DRAFT_PATH = 'draft/'
POST_PATH = 'public/_posts/'
def post_filename(name):
assert name.find('_') == -1
return name.lower().replace(' ', '_') + '.md'
def create_draft(name):
filepath = DRAFT_PATH + post_filename(name)
if path.exists(filepath):
print('Filename already exists, still proceed? y/N')
yn = input().lower()
if yn != 'y' and yn != 'yes':
exit(3)
else:
rename(filepath, filepath + '.bak')
with open(filepath, 'w+') as f:
f.write('# {}\n\nLorem ipsum'.format(name))
def publish(name):
draft_path = DRAFT_PATH + post_filename(name)
if not path.exists(draft_path):
print('Draft `{}` does not exist'.format(name))
exit(4)
for item in listdir(POST_PATH):
if item.find(post_filename(name)) != -1:
print('Post `{}` already published as {}, still proceed? y/N'.format(name, item))
yn = input().lower()
if yn != 'y' and yn != 'yes':
exit(4)
else:
rename(POST_PATH + item, POST_PATH + item + '.old')
now = datetime.now()
filename = POST_PATH + now.strftime('%Y_%m_%d_') + post_filename(name)
rename(draft_path, filename)
with open(filename, 'r+') as f:
content = f.read()
f.seek(0, 0)
f.write('<!-- {} -->\n{}'.format(now.strftime('%Y/%m/%d %H:%M:%S'), content))
USAGE = 'Usage: {} (draft|publish) <post-name>'.format(argv[0])
if len(argv) != 3:
print(USAGE)
exit(1)
cmd = argv[1]
name = argv[2]
if name.find('_') != -1:
print("Name cannot contain '_' symbol")
exit(2)
if cmd == 'draft':
create_draft(name)
elif cmd == 'publish':
publish(name)
else:
print('Unknown command')
print(USAGE)
exit(1)