-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_article.py
More file actions
executable file
·70 lines (54 loc) · 1.8 KB
/
create_article.py
File metadata and controls
executable file
·70 lines (54 loc) · 1.8 KB
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
#!/usr/bin/env python3
from argparse import ArgumentParser
import datetime
from itertools import repeat
import os, sys
# parse the command line arguments
parser = ArgumentParser()
parser.add_argument('-title', help='the title of the article')
parser.add_argument('-description', help='a short description of the article')
args = parser.parse_args()
print(f'Creating article "{args.title}": {args.description}')
today = datetime.date.today()
timestamp = '{y}{m:02d}{d:02d}'.format(
y = today.year,
m = today.month,
d = today.day,
)
badChars = ('/', '\\', '?', '%', '*', ':', '|', '"', '<', '>', '.')
t = str.maketrans(dict(zip((badChars), repeat(None))))
slug = '{d}_{t}'.format(
d = timestamp,
t = args.title.lower().translate(t).replace(' ', '_'),
)
# create the directory
os.makedirs(f'articles/{slug}', exist_ok=True)
fpath = f'articles/{slug}/{slug}.md'
# create the article
heading = f'# {timestamp} {args.title}'
with open(fpath, 'w') as ostream:
print(heading, file=ostream)
print(f'wrote heading to {fpath}: "{heading}"')
# add a readme entry that links to the article
# - create the markdown for the readme
entry = f'''
### [{timestamp} {args.title}]({fpath})
> _{args.description}_
'''
# - read in the whole readme
with open('README.md', 'r') as istream:
header, articles = istream.read().split('---\n', 1)
with open('README.md', 'w') as ostream:
print('---\n'.join([header, entry+articles]), file=ostream, end='')
print(f'\x1b[93mchecking out to branch: {slug}\x1b[0m')
os.system(f'git checkout -b {slug}')
os.system(f'git add {fpath} README.md')
os.system(f'git diff --cached')
os.system('git status')
choice = input('looks good? [y/n]: ')
if choice == 'y':
os.system(f'git commit -m "add article: {args.title}"')
os.system('git push')
else:
print('aborting')
sys.exit(1)