From acff01f32b2e240e4029ca4ad5fe6df717be35d5 Mon Sep 17 00:00:00 2001 From: redtide Date: Wed, 27 Dec 2023 22:50:45 +0100 Subject: [PATCH] scripts update --- scripts/generators/atom_feed.py | 1 - scripts/hooks/fs.py | 2 +- scripts/hooks/new_post.py | 39 +++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 scripts/hooks/new_post.py diff --git a/scripts/generators/atom_feed.py b/scripts/generators/atom_feed.py index 5dd18c84e..4ced831c9 100644 --- a/scripts/generators/atom_feed.py +++ b/scripts/generators/atom_feed.py @@ -21,7 +21,6 @@ for f in sorted(files, key=lambda x: x, reverse=True): with open(f) as file: post = frontmatter.load(file) - try: post["date"] = datetime.strptime(post["date"], "%Y-%m-%dT%H:%M:%S%z").isoformat() except KeyError: diff --git a/scripts/hooks/fs.py b/scripts/hooks/fs.py index 4e5bd0c00..28befa708 100644 --- a/scripts/hooks/fs.py +++ b/scripts/hooks/fs.py @@ -1,4 +1,4 @@ -import jinja2, os, json, yaml +import json, os, yaml def path_exists(path): if isinstance(path, str): diff --git a/scripts/hooks/new_post.py b/scripts/hooks/new_post.py new file mode 100644 index 000000000..082a9e4c2 --- /dev/null +++ b/scripts/hooks/new_post.py @@ -0,0 +1,39 @@ +import argparse, re +from datetime import datetime, timezone + +""" + Creates a new blog post to edit manually or with content if specified. +""" +def new_post(title, author, content=None): + dt = datetime.now(timezone.utc).replace(microsecond=0) + date = dt.strftime("%Y-%m-%d") + path = "docs/news/posts" + name = title.lower() + name = name.replace(' ', '-') + name = re.sub("[^0-9a-z\-\.-]*", '', name) + path = "{}/{}-{}.md".format(path, date, name) + date = dt.strftime("%Y-%m-%dT%T%z") # without comma in tz + post = "\ +---\n\ +title: \"{}\"\n\ +author: \"{}\"\n\ +date: \"{}\"\n\ +---\n".format(title, author, date) + if content is not None: + post += content + + with open(path, "w") as file: + file.write(post) + + print("File created at \"{}\".".format(path)) + return path + +def main(): + parser = argparse.ArgumentParser(description="creates a new blog post to edit manually or with content if specified.") + parser.add_argument("title", type=str, help="post title") + parser.add_argument("author", type=str, help="post author") + parser.add_argument('-c', '--content', help="post content (optional)", type=str) + args = parser.parse_args() + +if __name__ == "__main__": + main()