diff --git a/Makefile b/Makefile deleted file mode 100644 index c29e001..0000000 --- a/Makefile +++ /dev/null @@ -1,44 +0,0 @@ -# Variables -HUGO = hugo -DATE = $(shell date +%Y-%m-%dT%H:%M:%S%z) -YEAR = $(shell date +%Y) -TITLE = -FILE_NAME = $(shell echo $(TITLE) | tr '[:upper:]' '[:lower:]' | tr ' ' '-') -DRAFTS_DIR = content/posts/drafts -POSTS_DIR = content/posts/$(YEAR) - -.PHONY: new serve publish - -new: - @echo "Enter post title: "; \ - read TITLE; \ - FILE_NAME=$$(echo "$$TITLE" | tr '[:upper:]' '[:lower:]' | tr -s ' ' '-' | sed "s/[^a-zA-Z0-9-]//g"); \ - FILE_PATH="$(DRAFTS_DIR)/$$FILE_NAME.md"; \ - echo -e "---\ntitle: \"$$TITLE\"\ndescription: \"\"\nauthor: Mansoor\ndate: $(DATE)\nlastmod: $(DATE)\ndraft: true\nurl: /blog/$$FILE_NAME\nimages: []\n---\n\n" > "$$FILE_PATH"; \ - echo "Draft post created at $$FILE_PATH" - -serve: - $(HUGO) server -D - -publish: - @echo "Select a draft to publish:"; \ - FILES=$$(ls $(DRAFTS_DIR)/*.md); \ - if [ -z "$$FILES" ]; then \ - echo "No drafts available."; \ - exit 1; \ - fi; \ - IFS=$$'\n'; \ - select FILENAME in $$FILES; do \ - TEST=$$FILENAME; \ - if [ -z "$$TEST" ]; then \ - echo "Invalid selection."; \ - else \ - break; \ - fi; \ - done; \ - clear; \ - FILE_BASE=$$(basename $$FILENAME .md); \ - mkdir -p $(POSTS_DIR); \ - mv $$FILENAME $(POSTS_DIR)/$$FILE_BASE.md; \ - sed -i'' -e 's/draft: true/draft: false/' $(POSTS_DIR)/$$FILE_BASE.md; \ - echo "Draft $$FILE_BASE.md published in $(POSTS_DIR)/." diff --git a/content/posts/draft.md b/content/posts/draft.md deleted file mode 100644 index 7b76872..0000000 --- a/content/posts/draft.md +++ /dev/null @@ -1,11 +0,0 @@ ---- -title: "" -description: "" -author: Mansoor -date: 2022-08-03T22:32:37-04:00 -lastmod: 2022-08-03T22:32:37-04:00 -draft: true -url: blog/title -images: [] ---- - diff --git a/posts.py b/posts.py new file mode 100755 index 0000000..04f91b4 --- /dev/null +++ b/posts.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python3 + +import os +import shutil +from datetime import datetime +import argparse + + +HUGO_DRAFTS_DIR = "content/posts/drafts" +HUGO_POSTS_DIR = "content/posts" + +def create_new_post(title: str): + date_prefix = datetime.now().strftime("%Y-%m-%d") + file_name = f"{date_prefix}-{title.lower().replace(' ', '-')}.md" + file_path = os.path.join(HUGO_DRAFTS_DIR, file_name) + + front_matter = f"""--- +title: "{title}" +description: "" +author: Mansoor +date: {datetime.now().isoformat()} +lastmod: {datetime.now().isoformat()} +draft: true +url: /blog/{date_prefix}-{title.lower().replace(' ', '-')} +images: [] +--- + +""" + + os.makedirs(os.path.dirname(file_path), exist_ok=True) + with open(file_path, 'w') as f: + f.write(front_matter) + + print(f"Draft post created at {file_path}") + +def list_drafts(): + return [f for f in os.listdir(HUGO_DRAFTS_DIR) if f.endswith('.md')] + +def publish_post(file_name: str): + year = file_name.split('-')[0] + new_file_name = '-'.join(file_name.split('-')[1:]) + new_path = os.path.join(HUGO_POSTS_DIR, year, new_file_name) + + os.makedirs(os.path.dirname(new_path), exist_ok=True) + shutil.move(os.path.join(HUGO_DRAFTS_DIR, file_name), new_path) + + # Update draft status + with open(new_path, 'r+') as f: + content = f.read() + content = content.replace('draft: true', 'draft: false') + f.seek(0) + f.write(content) + f.truncate() + + print(f"Draft published as {new_path}") + +def main(): + parser = argparse.ArgumentParser(description='Manage Hugo blog posts.') + subparsers = parser.add_subparsers(dest='command', required=True) + + # Subparser for 'new' command + parser_new = subparsers.add_parser('new', help='Create a new post') + parser_new.add_argument('title', type=str, help='Title of the post') + + # Subparser for 'publish' command + parser_publish = subparsers.add_parser('publish', help='Publish a draft post') + parser_publish.add_argument('draft_number', type=int, nargs='?', default=0, help='Draft number to publish (optional)') + + args = parser.parse_args() + + if args.command == 'new': + create_new_post(args.title) + elif args.command == 'publish': + if args.draft_number > 0: + drafts = list_drafts() + draft_number = args.draft_number - 1 # Adjust for zero-based index + if 0 <= draft_number < len(drafts): + publish_post(drafts[draft_number]) + else: + print("Invalid draft number.") + else: + drafts = list_drafts() + for i, draft in enumerate(drafts, 1): + print(f"{i}) {draft}") + draft_number = int(input("Select a draft to publish: ")) - 1 + if 0 <= draft_number < len(drafts): + publish_post(drafts[draft_number]) + else: + print("Invalid selection.") + +if __name__ == "__main__": + main()