-
Notifications
You must be signed in to change notification settings - Fork 7
/
create_post.py
executable file
·108 lines (90 loc) · 2.63 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
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
#! /usr/bin/env python3
# A CLI to create a blog post template
import argparse
from datetime import datetime
from pathlib import Path
import sys
from textwrap import dedent
CATS = {
"news": "News",
"event": "Events",
"report": "Reports",
"release": "Releases",
"blog": "Blog",
}
def main(args=None):
parser = argparse.ArgumentParser(description="Create a blog post")
parser.add_argument("title", help="The title of the post")
parser.add_argument(
"-c",
"--category",
type=str,
required=True,
choices=list(CATS),
help="The category of the blog post",
)
parser.add_argument(
"-t",
"--tags",
type=str,
default="",
help="Comma delimited list of tags",
)
parser.add_argument(
"-s",
"--start",
type=str,
default="",
help="The start date(time) for the event",
)
args = parser.parse_args(args)
post_category = CATS[args.category]
post_date = datetime.now().strftime("%Y-%m-%d")
if args.category == "event" and not args.start:
print(f"error: Events require a start date e.g.: -s/--start {post_date}")
sys.exit(1)
content = dedent(
f"""\
---
blogpost: true
category: {post_category}
tags: {args.tags}
date: {post_date}
---
# {args.title}
Now you can fill in your content here.
Write in [MyST Markdown](https://myst-parser.readthedocs.io/en/latest/syntax/syntax.html).
"""
)
# create file name from date and title (max 20 chars)
post_title = args.title.lower().replace(" ", "-")
post_title = post_title[:20]
post_path = (
Path(__file__).parent
/ "docs"
/ "news"
/ "posts"
/ f"{post_date}-{post_title}.md"
)
post_rel_path = post_path.relative_to(Path(__file__).parent / "docs")
if post_path.exists():
raise ValueError(f"File already exists: {post_path}")
post_path.write_text(content, encoding="utf-8")
print(f"Created post: {post_path}")
if args.category == "event":
events_path = Path(__file__).parent / "docs" / "events.yaml"
with events_path.open("a", encoding="utf8") as handle:
handle.write("\n")
handle.write(
dedent(
f"""\
- name: {args.title}
start: {args.start}
location: unknown
announce: /{post_rel_path}
"""
)
)
print(f"Updated events file: {events_path}")
if __name__ == "__main__":
main()