From 20e883cddf2c45af84472bd0da54b7c00fc0e3a4 Mon Sep 17 00:00:00 2001 From: Luca Beltrame Date: Sun, 24 May 2015 23:58:27 +0200 Subject: [PATCH] Draft support --- jekyll_new_post.py | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/jekyll_new_post.py b/jekyll_new_post.py index 2aa4130..a9ba992 100755 --- a/jekyll_new_post.py +++ b/jekyll_new_post.py @@ -35,11 +35,17 @@ def hash_file(filename: Path) -> str: return file_hash.hexdigest() -def perform_commit(filepath: Path, title: str=None) -> None: +def perform_commit(filepath: Path, title: str=None, + draft: bool=False) -> None: repo = Repo("./") # FIXME repo.index.add(str(filepath)) - message = "New post: {}".format(title) + + if not draft: + message = "New post: {}".format(title) + else: + message = "New draft: {}".format(title) + message = fill(message, width=78) repo.commit(message) @@ -47,16 +53,21 @@ def perform_commit(filepath: Path, title: str=None) -> None: def create_new_post(title: str, categories: list=None, tags: list=None, - post_metadata: dict=None, comments: bool=True) -> Path: + post_metadata: dict=None, comments: bool=True, + draft: bool=False) -> Path: title_slug = slugify(title) current_time = datetime.datetime.now(pytz.timezone("Europe/Rome")) date_slug = current_time.date().isoformat() - filename = "{0}-{1}.markdown".format(date_slug, title_slug) formatted_date = current_time.strftime("%Y-%m-%d %H:%M:%S%z") - metadata = dict(title=title, comments=comments, - date=formatted_date) + metadata = dict(title=title, comments=comments, layout="page") + + if not draft: + metadata["date"] = formatted_date + filename = "{0}-{1}.markdown".format(date_slug, title_slug) + else: + filename = "{0}.markdown".format(title_slug) if categories is not None: metadata["categories"] = categories @@ -70,9 +81,12 @@ def create_new_post(title: str, categories: list=None, tags: list=None, for key, value in post_metadata.items(): metadata[key] = value - final_path = Path("_posts") / filename + if not draft: + final_path = Path("_posts") / filename + else: + final_path = Path("_drafts") / filename - with tempfile.NamedTemporaryFile() as temp: + with tempfile.NamedTemporaryFile(mode="wt") as temp: temp_path = Path(temp.name) @@ -82,9 +96,10 @@ def create_new_post(title: str, categories: list=None, tags: list=None, temp.flush() pre_edit_hash = hash_file(temp_path) + print("Save and close the editor after writing the post.") subprocess.check_call("/usr/bin/kate {}".format(temp.name), shell=True) - post_edit_hash = hash_file(final_path) + post_edit_hash = hash_file(temp_path) if pre_edit_hash == post_edit_hash: print("No post content. Aborting.") @@ -106,6 +121,8 @@ def main(): help="Disable comments for the post") parser.add_argument("--commit", action="store_true", help="Commit and push to the remote repository") + parser.add_argument("--draft", action="store_true", + help="Create the post as draft") parser.add_argument("title", help="Title of the new post") options = parser.parse_args() @@ -114,16 +131,18 @@ def main(): raise FileNotFoundError("Jekyll root not found.") path = create_new_post(options.title, options.categories, options.tags, - comments=options.disable_comments) + comments=options.disable_comments, + draft=options.draft) if path is None: exit(1) - print("Created new post {}".format(path.name)) + print("Created new post {}.".format(path.name)) if options.commit: print("Committing to upstream repository...") - perform_commit(path, options.title) + perform_commit(path, options.title, options.draft) + print("Done.")