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.
This commit is contained in:
Eryn Wells 2024-12-10 11:46:29 -08:00
parent 8ec88e6c80
commit b9796ab096
4 changed files with 71 additions and 22 deletions

View file

@ -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)/"

View file

@ -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

View file

@ -0,0 +1,18 @@
#!/usr/bin/env python3.12
# Eryn Wells <eryn@erynwells.me>
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,
)

View file

@ -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