erick.is/_publish.py

65 lines
1.2 KiB
Python
Raw Normal View History

import glob
import os
import sys
from datetime import UTC, datetime
import _utils
articles = glob.glob("_drafts/*.gmi")
print(articles)
articles.sort()
titles = []
if len(articles) == 0:
print("No drafts found")
sys.exit(1)
for article in articles:
fm = _utils.load(article)
titles.append(fm.metadata["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 = _utils.load(article)
fm.metadata["date"] = frontmatter_date
with open(
article.replace("_drafts/", f"_posts/{file_date}-"),
mode="w",
encoding="utf8",
) as file:
file.write(_utils.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}")