From f3600fccc4a02324b5cc86549e644720097c61df Mon Sep 17 00:00:00 2001
From: neodarz <neodarz@neodarz.net>
Date: Sun, 26 May 2019 16:38:13 +0200
Subject: Move new_post to external file

---
 cli/cli.py | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

(limited to 'cli')

diff --git a/cli/cli.py b/cli/cli.py
index 4c9733b1..51b9925c 100644
--- a/cli/cli.py
+++ b/cli/cli.py
@@ -3,6 +3,12 @@
 import os
 import subprocess
 
+import sys
+
+from config.config import *
+
+from utils import utils
+
 from generators import generators
 
 
@@ -32,3 +38,42 @@ def edit_post_with_editor(path):
     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
-- 
cgit v1.2.1