import glob import os import sys from datetime import datetime import frontmatter articles = glob.glob("_posts/*.md") articles.sort(reverse=True) titles = [] if len(articles) == 0: print("No articles found") sys.exit(1) for article in articles: fm = frontmatter.load(article) titles.append(fm.get("title")) print("\nAvailable articles:\n") for index, title in enumerate(titles): print(f"\t[{index + 1}] {title}") print("") article = "" selection = "" index = -1 while index < 0 or index > len(articles): try: index = int(selection) - 1 if index == -1: sys.exit(0) article = articles[index] except (ValueError, IndexError): selection = input("Enter a number (0 to exit): ") fm = frontmatter.load(article) file_date = datetime.fromisoformat(fm.metadata["date"]).strftime("%Y-%m-%d") del fm.metadata["date"] with open( article.replace(f"_posts/{file_date}-", "_drafts/"), mode="w", encoding="utf8", ) as file: file.write(frontmatter.dumps(fm)) try: os.remove(article) except PermissionError: print(f"Permission denied to delete '{article}'.") except Exception as e: print(f"An error occurred: {e}")