63 lines
1.2 KiB
Python
63 lines
1.2 KiB
Python
|
import glob
|
||
|
import os
|
||
|
import sys
|
||
|
from datetime import UTC, datetime
|
||
|
|
||
|
import frontmatter
|
||
|
|
||
|
articles = glob.glob("_drafts/*.md")
|
||
|
articles.sort()
|
||
|
titles = []
|
||
|
|
||
|
if len(articles) == 0:
|
||
|
print("No drafts found")
|
||
|
sys.exit(1)
|
||
|
|
||
|
for article in articles:
|
||
|
fm = frontmatter.load(article)
|
||
|
titles.append(fm.get("title"))
|
||
|
|
||
|
print("\nAvailable drafts:\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): ")
|
||
|
|
||
|
now = datetime.now(UTC)
|
||
|
file_date = now.strftime("%F")
|
||
|
frontmatter_date = now.strftime("%F %H:%m %z")
|
||
|
|
||
|
fm = frontmatter.load(article)
|
||
|
fm.metadata["date"] = frontmatter_date
|
||
|
|
||
|
with open(
|
||
|
article.replace("_drafts/", f"_posts/{file_date}-"),
|
||
|
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}")
|