80 lines
2.5 KiB
Python
80 lines
2.5 KiB
Python
import argparse
|
|
import locale
|
|
from pathlib import Path
|
|
from datetime import datetime, timedelta
|
|
|
|
MONDAY = 0 # 0 = Monday, 1=Tuesday, 2=Wednesday...
|
|
|
|
|
|
def get_next_weekday(d, weekday):
|
|
days_ahead = weekday - d.weekday()
|
|
if days_ahead <= 0: # Target day already happened this week
|
|
days_ahead += 7
|
|
return d + timedelta(days_ahead)
|
|
|
|
|
|
def set_publication_date_for_humans(text: str, publication_date: datetime) -> str:
|
|
locale.setlocale(locale.LC_TIME, "fr_FR")
|
|
date = publication_date.strftime("%d %B %Y")
|
|
return text.replace("{DATE}", date)
|
|
|
|
|
|
def set_publication_date_for_pelican(text: str, publication_date: datetime) -> str:
|
|
date_digits = publication_date.isoformat()[0:10]
|
|
return text.replace("{DATE_DIGITS}", date_digits)
|
|
|
|
|
|
def set_publication_date_for_seo(text: str, publication_date: datetime) -> str:
|
|
locale.setlocale(locale.LC_TIME, "en_US")
|
|
date_utc = publication_date.strftime("%a %b %d %Y")
|
|
return text.replace("{DATE_UTC}", date_utc)
|
|
|
|
|
|
# Find the number of the letter to create
|
|
destination_path = Path(f"./content/newsletter")
|
|
p = destination_path.glob("craft-letter-*.md")
|
|
files = [x for x in p if x.is_file()]
|
|
letter_number = len(files) + 1
|
|
|
|
# Load the newsletter template
|
|
template = Path("./template/newsletter.md")
|
|
content = template.read_text()
|
|
|
|
|
|
new_content = content.replace("{LETTER_NUMBER}", str(letter_number))
|
|
|
|
today = datetime.today()
|
|
next_monday = get_next_weekday(today, MONDAY)
|
|
|
|
new_content = set_publication_date_for_humans(new_content, next_monday)
|
|
|
|
new_content = set_publication_date_for_pelican(new_content, next_monday)
|
|
|
|
new_content = set_publication_date_for_seo(new_content, next_monday)
|
|
|
|
# Create the new file
|
|
destination = destination_path / f"craft-letter-{letter_number}.md"
|
|
destination.write_text(new_content)
|
|
|
|
print(f"Created letter #{letter_number}: {destination}")
|
|
|
|
# Load the homepage template
|
|
template = Path("./template/index.md")
|
|
content = template.read_text()
|
|
|
|
new_content = set_publication_date_for_pelican(content, next_monday)
|
|
|
|
new_content = set_publication_date_for_seo(new_content, next_monday)
|
|
|
|
link = f"[lettre n°{letter_number}]({{filename}}/newsletter/craft-letter-{letter_number}.md)"
|
|
new_content = new_content.replace("{LINK}", link)
|
|
|
|
for i in reversed(range(letter_number)):
|
|
link = f"* [Lettre n°{i + 1}]({{filename}}/newsletter/craft-letter-{i + 1}.md)\n"
|
|
new_content += link
|
|
|
|
# Update the index page
|
|
destination = Path("./content/pages/index.md")
|
|
destination.write_text(new_content)
|
|
|
|
print(f"Updated index page: {destination}")
|