#!/usr/bin/env python3 # SPDX-FileCopyrightText: 2021 Luca Beltrame # SPDX-License-Identifier: BSD-3-Clause import argparse from datetime import datetime from pathlib import Path from typing import List import frontmatter import sarge from slugify import slugify POST_PATH = "content/post" def new_post_title(title: str) -> str: suffix = "md" current_date = datetime.now().date().isoformat() slugified_title = slugify(title) post_title = f"{current_date}-{slugified_title}.{suffix}" return post_title def create_post(post_title: str, tags: List[str] = None, categories: List[str] = None, comments: bool = True) -> bool: cmd = ["hugo", "new", "-k", "post"] title = new_post_title(post_title) path = Path(POST_PATH) / title cmd.append(str(path)) pid = sarge.run(cmd) if pid.returncode != 0: return False header = frontmatter.load(path) if categories is not None: header["categories"] = categories if tags is not None: header["tags"] = tags header["comments"] = comments with path.open("w") as handle: frontmatter.dump(header, handle) return True def main(): parser = argparse.ArgumentParser() parser.add_argument("-t", "--tags", nargs="+", help="Post tags (space separated)") parser.add_argument("-c", "--categories", nargs="+", help="Post categories (space separated)") parser.add_argument("--disable_comments", action="store_false", 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") pass if __name__ == "__main__": main()