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
|
#/bin/python3
import os
import subprocess
import sys
from config.config import *
from utils import utils
from generators import generators
def generate(args):
"""Wrapper for generate_blog(fresh=False)."""
# pylint: disable=unused-argument
exit(generators.generate_blog(fresh=False))
def regenerate(args):
"""Wrapper for generate_blog(fresh=True)."""
# pylint: disable=unused-argument
exit(generators.generate_blog(fresh=True))
def edit_post_with_editor(path):
"""Launch text editor to edit post at a given path.
Text editor is $VISUAL, then if empty, $EDITOR, then if still empty,
vi.
"""
if "VISUAL" in os.environ:
editor = os.environ["VISUAL"]
elif "EDITOR" in os.environ:
editor = os.environ["EDITOR"]
else:
editor = "vi"
subprocess.call([editor, path])
def new_post(title):
"""Create a new post with metadata pre-filled.
The path to the new post is printed to stdout.
Returns
-------
0
On success.
"""
date = utils.current_datetime()
filename_date = date.strftime("%Y-%m-%d")
iso_date = date.isoformat()
display_date = "%s %d, %d" % (date.strftime("%B"), date.day, date.year)
title_sanitized = utils.sanitize(title)
filename = "%s-%s.md" % (filename_date, title_sanitized)
fullpath = os.path.join(POSTSDIR, filename)
if not os.path.isdir(POSTSDIR):
if os.path.exists(POSTSDIR):
os.remove(POSTSDIR)
os.mkdir(POSTSDIR, mode=0o755)
if os.path.exists(fullpath):
sys.stderr.write("%serror: '%s' already exists, please pick a different title%s\n" %
(RED, fullpath, RESET))
return 1
with open(fullpath, 'w', encoding='utf-8') as newpost:
newpost.write("---\n")
newpost.write('title: "%s"\n' % title)
newpost.write("date: %s\n" % iso_date)
newpost.write("date_display: %s\n" % display_date)
newpost.write("---\n\n")
sys.stderr.write("New post created in:\n")
print(fullpath)
edit_post_with_editor(fullpath)
return 0
def new_post_cli(args):
"""CLI wrapper around new_post."""
new_post(args.title)
|