-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblogpost.py
More file actions
47 lines (37 loc) · 949 Bytes
/
blogpost.py
File metadata and controls
47 lines (37 loc) · 949 Bytes
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
#!/usr/bin/python3
import datetime
import pathlib
import os
fdir = pathlib.Path(__file__).parent.absolute()
#fdir = os.path.abspath(os.path.split(__file__)[0])
if not fdir.samefile(os.getcwd()):
os.chdir(fdir)
title = input('Title: ')
if not title:
exit('No title specified. Exiting')
ftitle = title.strip().replace(' ', '-')
tz = datetime.timezone(datetime.timedelta(hours=2))
now = datetime.datetime.now()
now = now.replace(tzinfo=tz)
shor = now.strftime("%Y-%m-%d")
long = now.strftime(
"%Y-%m-%d %H:%M:%S %z"
)
fn = pathlib.Path(f'_posts/{shor}-{ftitle}.md')
template = """
---
layout: post
date: {now:%Y-%m-%d %H:%M:%S %z}
title: {title}
---
""".strip()
temp = template.format(now=now, title=title)
fn.write_text(temp)
print(fn)
if not os.environ.get('EDITOR'):
os.environ['EDITOR'] = 'nano'
os.system(f'$EDITOR {fn}')
posttext = fn.read_text()
if posttext == temp:
fn.unlink()
exit('post was not written')