-
Notifications
You must be signed in to change notification settings - Fork 2
/
new.py
78 lines (58 loc) · 1.8 KB
/
new.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
#!/usr/bin/env python
from __future__ import print_function
import sys
import os
from datetime import datetime
import re
import subprocess
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
def get_valid_filename(s):
s = str(s).strip().replace(' ', '_')
return re.sub(r'(?u)[^-\w.]', '', s)
valid_types = ['posts', 'buildlog', 'projects', 'products']
new_type = sys.argv[1].lower()
title = sys.argv[2]
title_path = get_valid_filename(title)
year = datetime.now().year
month = datetime.now().month
if new_type not in valid_types:
if (new_type + 's') in valid_types:
new_type = (new_type + 's')
else:
print('{} is invalid type! Use one of {}'.format(new_type, ', '.join(valid_types)))
sys.exit(1)
path = '{}/{}/{}/{}/index.md'.format(new_type, year, month, title_path)
author = ""
p = subprocess.Popen('git config user.name', stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
output = output.decode("utf-8")
res = p.wait()
if res:
print(err)
sys.exit(res)
author = output.strip()
cmd = 'hugo new {}'.format(path)
print(cmd)
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
output = output.decode("utf-8")
res = p.wait()
print(output)
if res:
print('Error running hugo new!')
sys.exit(res)
full_path = output.replace(' created\n', '')
with open(full_path, 'r+') as f:
content = f.read()
f.truncate()
f.seek(0)
content = content.replace('$$TITLE$$', title).replace("# author:", "author: {}".format(author))
f.write(content)
f.flush()
print("""
New doc created @ {}
Static resources may be included in the same directory
and referenced as a relative path.
""".format(full_path))