From b9796ab096676633ab78c290f50a1af0f4b87d6c Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Tue, 10 Dec 2024 11:46:29 -0800 Subject: [PATCH] When editing weeknotes with the website script, create the page from the Hugo archetype and process it This implements what the Makefile did, but with a more intuitive interface. Remove the weeknotes target from the Makefile. --- Makefile | 8 ---- archetypes/weeknotes.md | 2 +- scripts/erynwells_me/commands.py | 18 +++++++++ scripts/erynwells_me/weeknotes.py | 65 ++++++++++++++++++++++++------- 4 files changed, 71 insertions(+), 22 deletions(-) create mode 100644 scripts/erynwells_me/commands.py diff --git a/Makefile b/Makefile index 413dece..72bf627 100644 --- a/Makefile +++ b/Makefile @@ -38,13 +38,5 @@ endif nethack-commit: $(NETHACK_LOGFILE_DATA_FILE) if ! git diff --quiet $<; then git commit -m "Update Nethack logfile for $(HOSTNAME)" -- $<; fi -weeknotes: YEAR=$(shell date '+%Y') -weeknotes: WEEK_NUMBER=$(shell date '+%V') -weeknotes: UPCOMING_SUNDAY=$(shell scripts/weeknotes show --date) -weeknotes: PAGE_PATH=$(shell scripts/weeknotes show) -weeknotes: $(PAGE_PATH) - hugo new -k weeknotes --clock "$(UPCOMING_SUNDAY)" -c "$(CONTENT_PATH)" "$(PAGE_PATH)" - sed -I -e "s/%%WEEK_NUMBER%%/$(WEEK_NUMBER)/" "$(PAGE_PATH)" - clean: rm -rf "$(BUILD_DIR)/" diff --git a/archetypes/weeknotes.md b/archetypes/weeknotes.md index 93bbb34..9fa6e6b 100644 --- a/archetypes/weeknotes.md +++ b/archetypes/weeknotes.md @@ -1,5 +1,5 @@ --- -title: "Weeknotes for {{ time.Now.Format "2006" }}W%%WEEK_NUMBER%%" +title: "{{ time.Now.Format "2006" }}W%%WEEK_NUMBER%%" slug: weeknotes-{{ time.Now.Format "2006" }}w%%WEEK_NUMBER%% date: {{ .Date }} categories: weeknotes diff --git a/scripts/erynwells_me/commands.py b/scripts/erynwells_me/commands.py new file mode 100644 index 0000000..1ef7ba0 --- /dev/null +++ b/scripts/erynwells_me/commands.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3.12 +# Eryn Wells + +import os +import subprocess +import sys + + +def edit_file(path, editor=None): + editor = editor or os.environ.get('VISUAL') or os.environ.get('EDITOR') or 'vi' + subprocess.run( + f'{editor} "{path}"', + stdin=sys.stdin, + stdout=sys.stdout, + stderr=sys.stderr, + env=os.environ, + shell=True, + ) diff --git a/scripts/erynwells_me/weeknotes.py b/scripts/erynwells_me/weeknotes.py index aeaecbd..a014dd4 100644 --- a/scripts/erynwells_me/weeknotes.py +++ b/scripts/erynwells_me/weeknotes.py @@ -5,10 +5,8 @@ import argparse import datetime import os import subprocess -import sys from typing import Optional -from . import blog -from .dates import next_sunday_noon +from . import blog, commands, dates, paths from .scripting import Command @@ -23,7 +21,7 @@ class WeeknotesCommand(Command): year = datetime.datetime.now().year week_number_str = str(week_number) else: - next_sunday = next_sunday_noon() + next_sunday = dates.next_sunday_noon() year = next_sunday.year week_number_str = next_sunday.strftime('%V') @@ -70,19 +68,60 @@ class WeeknotesCommand(Command): def handle_edit_command(self, args: argparse.Namespace): path = WeeknotesCommand.weeknotes_path(week_number=args.week) - subprocess.run( - f'{args.editor} "{path}"', - stdin=sys.stdin, - stdout=sys.stdout, - stderr=sys.stderr, - env=os.environ, - shell=True, - ) + + if not os.path.isfile(path): + if not self._create_weeknotes_page(week_number=args.week): + return -1 + + relpath = os.path.relpath(path, paths.content_path()) + print(f'Editing {relpath}') + + commands.edit_file(path, editor=args.editor) def handle_show_command(self, args: argparse.Namespace): if args.date: if args.week: raise NotImplementedError('Cannot print date with specified week number') - print(next_sunday_noon().isoformat()) + print(dates.next_sunday_noon().isoformat()) else: print(WeeknotesCommand.weeknotes_path(week_number=args.week)) + + def _create_weeknotes_page(self, week_number): + next_sunday_noon = dates.next_sunday_noon() + + page_path = WeeknotesCommand.weeknotes_path(week_number=week_number) + + try: + print("Creating weeknotes page...", end='') + subprocess.run( + [ + 'hugo', 'new', '-k', 'weeknotes', + '--clock', next_sunday_noon.isoformat(), + '--contentDir', paths.content_path(), + page_path, + ], + env=os.environ + ).check_returncode() + except subprocess.CalledProcessError: + print('Failed') + return False + else: + print('OK') + + try: + print("Processing weeknotes page...", end='') + week_number = next_sunday_noon.strftime('%V') + subprocess.run( + f'sed -I -e "s/%%WEEK_NUMBER%%/{week_number}/" "{page_path}"', + env=os.environ, + shell=True + ).check_returncode() + except subprocess.CalledProcessError: + print("Failed") + return False + else: + print('OK') + + return True + +