#!/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) -> 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 with path.open("w") as handle: frontmatter.dump(header, handle) return True def main(): pass if __name__ == "__main__": main()