From 802f4c3eceebdca90dd56003b0ec445951400746 Mon Sep 17 00:00:00 2001 From: Pascal Le Merrer Date: Sat, 10 Jan 2026 16:44:30 +0100 Subject: [PATCH] Add templates and generation script --- scripts/create_newsletter.py | 72 ++++++++++++++++++++++++++++++++++++ template/index.md | 53 ++++++++++++++++++++++++++ template/newsletter.md | 8 ++-- 3 files changed, 128 insertions(+), 5 deletions(-) create mode 100644 scripts/create_newsletter.py create mode 100644 template/index.md diff --git a/scripts/create_newsletter.py b/scripts/create_newsletter.py new file mode 100644 index 0000000..c5837b6 --- /dev/null +++ b/scripts/create_newsletter.py @@ -0,0 +1,72 @@ +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) + + +# Parse the command line arguments +parser = argparse.ArgumentParser() +parser.add_argument("-n", "--number", required=True, type=int, help="Newsletter number") +args = parser.parse_args() + +# Load the newsletter template +template = Path("../template/newsletter.md") +content = template.read_text() + + +new_content = content.replace("{LETTER_NUMBER}", str(args.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 = Path(f"../content/newsletter/craft-letter-{args.number}.md") +destination.write_text(new_content) + +# 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) + +for i in reversed(range(args.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) diff --git a/template/index.md b/template/index.md new file mode 100644 index 0000000..4bb29e1 --- /dev/null +++ b/template/index.md @@ -0,0 +1,53 @@ +Title: Accueil +Date: {DATE_DIGITS} 09:00 +URL: +save_as: index.html +Category: Home + + + + + +La Craft Letter est une newsletter hebdomadaire dans laquelle je partage des articles +issues de ma veille technologique. Vous y trouverez des articles relatifs au développement logiciel d'une façon générale, qu'il soit front-end, back-end ou autre. Mais aussi des articles consacrés à l'architecture logicielle, la méthodologie, les outils, des projets open source, des conférences... + +Pour savoir qui je suis, ou pourquoi j'écris cette lettre, je vous invite à vous lire l'édito du [premier numéro]({filename}/newsletter/craft-letter-1.md). + + + + +
+ +--- + +# Archives + diff --git a/template/newsletter.md b/template/newsletter.md index 721b199..8ef4f05 100644 --- a/template/newsletter.md +++ b/template/newsletter.md @@ -1,5 +1,5 @@ Title: Lettre n°{LETTER_NUMBER} - {DATE} -Date: {DATETIME} +Date: {DATE_DIGITS} 09:00 Category: Newsletter - - - + \ No newline at end of file