-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_entry.py
executable file
·48 lines (38 loc) · 1.34 KB
/
generate_entry.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
#!/usr/bin/python3
import sys, os, re
from datetime import datetime
TEMPLATE = """
Title: {title}
Date: {year}-{month:02d}-{day:02d} {hour}:{minute:02d}
Category: Dross
Tags:
Description: META
Status: Draft
<section markdown="1">
## Subtitle
</section>
"""
def generate_entry(title):
today = datetime.today()
slug = re.sub('[^a-zA-Z0-9\-]', '', title.lower().strip().replace(' ', '-'))
# Will attempt to make the year's directory. Does not throw an error if it exists
os.makedirs("content/{}".format(today.year), exist_ok=True)
new_article = "content/{}/{}.md".format(today.year, slug)
if os.path.exists(new_article):
print("Error. Article already exists. Not overwriting.")
return
t = TEMPLATE.strip().format(title=title,
year=today.year,
month=today.month,
day=today.day,
hour=today.hour,
minute=today.minute,
slug=slug)
with open(new_article, 'w') as w:
w.write(t)
print("Article ready for editing: " + new_article)
if __name__ == '__main__':
if len(sys.argv) > 1:
generate_entry(sys.argv[1])
else:
print("Need an article title to generate a new entry. It's the only argument.")