1
0
Fork 0

Allow editing posts

This commit is contained in:
Luca Beltrame 2021-01-03 14:52:19 +01:00
parent c28dd21e62
commit 6d91bb8d72
Signed by: einar
GPG key ID: 4707F46E9EC72DEC

View file

@ -5,8 +5,10 @@
import argparse import argparse
from datetime import datetime from datetime import datetime
from pathlib import Path from pathlib import Path
from subprocess import call
from typing import List from typing import List
from atomicwrites import atomic_write
import frontmatter import frontmatter
from git import Repo from git import Repo
import sarge import sarge
@ -24,18 +26,31 @@ def new_post_title(title: str) -> str:
return post_title return post_title
def commit_initial_post(post_title, post_filename): def commit_initial_post(post_title: str, post_filename: str):
repo = Repo("./") repo = Repo("./")
repo.index.add([str(post_filename)]) repo.index.add([str(post_filename)])
template = f"New post: {post_title}" template = f"New post: {post_title}"
repo.index.commit(template) repo.index.commit(template)
def edit_post(post_path):
with post_path.open() as handle:
with atomic_write(str(post_path), overwrite=True) as writer:
writer.write(handle.read())
writer.write("\n\n")
writer.flush()
print("Save and close the editor when done.")
call(["kate", "--new", str(writer.name), "--tempfile", "-l",
"22"])
writer.flush()
def create_post(post_title: str, tags: List[str] = None, def create_post(post_title: str, tags: List[str] = None,
categories: List[str] = None, categories: List[str] = None,
comments: bool = True, comments: bool = True,
draft: bool = True) -> Path or None: draft: bool = True) -> Path or None:
cmd = ["hugo", "new", "-k", "posts"] cmd = ["hugo", "new", "-k", "posts", "--quiet"]
title = new_post_title(post_title) title = new_post_title(post_title)
path = Path(POST_PATH) / title path = Path(POST_PATH) / title
cmd.append(str(path)) cmd.append(str(path))
@ -76,6 +91,8 @@ def main():
help="Commit to the repository") help="Commit to the repository")
parser.add_argument("--draft", action="store_true", parser.add_argument("--draft", action="store_true",
help="Create the post as draft") help="Create the post as draft")
parser.add_argument("--edit", action="store_true",
help="Edit the post after creation")
parser.add_argument("title", help="Title of the new post") parser.add_argument("title", help="Title of the new post")
options = parser.parse_args() options = parser.parse_args()
@ -84,8 +101,13 @@ def main():
options.categories, options.categories,
comments=options.disable_comments) comments=options.disable_comments)
if post_path is not None and options.commit: if post_path is not None:
commit_initial_post(options.title, post_path)
if options.edit:
edit_post(post_path)
if options.commit:
commit_initial_post(options.title, post_path)
if __name__ == "__main__": if __name__ == "__main__":