-
Notifications
You must be signed in to change notification settings - Fork 63
/
create-post.py
53 lines (40 loc) · 1.33 KB
/
create-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
#!/usr/bin/env python
# coding:utf-8
__author__ = 'coderzh'
import os
import re
import subprocess
from datetime import datetime
import sys
# replace to vim or others if your like
# EDITOR = ['MarkdownPad2.exe']
EDITOR = ['code']
if __name__ == '__main__':
if len(sys.argv) > 1:
post_name = sys.argv[1]
else:
post_name = input("Post'title: ")
post_path = 'post/{year}/{date_format}-{post_name}.md'.format(
year=datetime.now().year,
date_format=datetime.now().strftime('%Y-%m-%d'),
post_name=post_name
)
subprocess.call(['hugo', 'new', post_path])
# replace template value
post_rel_path = os.path.join('content', post_path)
with open(post_rel_path, 'r') as f:
content = f.read()
url = '/{date_format}/{post_name}'.format(
date_format=datetime.now().strftime('%Y/%m/%d'),
post_name=post_name
)
replace_patterns =[
(re.compile(r'title:(.*)'), 'title: "%s"' % post_name),
(re.compile(r'url:(.*)'), 'url: "%s/"' % url),
(re.compile(r'\n---'), r'\n---'),
]
for regex, replace_with in replace_patterns:
content = regex.sub(replace_with, content)
with open(post_rel_path, 'w') as f:
f.write(content)
subprocess.Popen(EDITOR + [post_rel_path])