1
0
Fork 0

The basic program is now working to create posts

This commit is contained in:
Luca Beltrame 2021-01-03 13:01:27 +01:00
parent 864124f7d2
commit c28dd21e62
Signed by: einar
GPG key ID: 4707F46E9EC72DEC

View file

@ -8,6 +8,7 @@ from pathlib import Path
from typing import List from typing import List
import frontmatter import frontmatter
from git import Repo
import sarge import sarge
from slugify import slugify from slugify import slugify
@ -23,16 +24,26 @@ def new_post_title(title: str) -> str:
return post_title return post_title
def commit_initial_post(post_title, post_filename):
repo = Repo("./")
repo.index.add([str(post_filename)])
template = f"New post: {post_title}"
repo.index.commit(template)
def create_post(post_title: str, tags: List[str] = None, def create_post(post_title: str, tags: List[str] = None,
categories: List[str] = None, comments: bool = True) -> bool: categories: List[str] = None,
cmd = ["hugo", "new", "-k", "post"] comments: bool = True,
draft: bool = True) -> Path or None:
cmd = ["hugo", "new", "-k", "posts"]
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))
pid = sarge.run(cmd) pid = sarge.run(cmd)
if pid.returncode != 0: if pid.returncode != 0:
return False return
header = frontmatter.load(path) header = frontmatter.load(path)
@ -41,12 +52,15 @@ def create_post(post_title: str, tags: List[str] = None,
if tags is not None: if tags is not None:
header["tags"] = tags header["tags"] = tags
header["draft"] = draft
header["comments"] = comments header["comments"] = comments
header["title"] = post_title
with path.open("w") as handle: frontmatter.dump(header, path)
frontmatter.dump(header, handle)
return True print(f"{path} created.")
return path
def main(): def main():
@ -59,12 +73,19 @@ def main():
parser.add_argument("--disable_comments", action="store_false", parser.add_argument("--disable_comments", action="store_false",
help="Disable comments for the post") help="Disable comments for the post")
parser.add_argument("--commit", action="store_true", parser.add_argument("--commit", action="store_true",
help="Commit and push to the remote 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("title", help="Title of the new post") parser.add_argument("title", help="Title of the new post")
pass options = parser.parse_args()
post_path = create_post(options.title, options.tags,
options.categories,
comments=options.disable_comments)
if post_path is not None and options.commit:
commit_initial_post(options.title, post_path)
if __name__ == "__main__": if __name__ == "__main__":