From 09ca6183a4e568427eaa393f8bcb154968eb80a1 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Wed, 3 Jan 2024 20:00:00 +0000 Subject: [PATCH] Add `create-daily` task --- run | 44 ++++++++++++++++++++++++++++++++++++++++++++ stub.md | 11 +++++++++++ 2 files changed, 55 insertions(+) create mode 100644 stub.md diff --git a/run b/run index af34d776..e173beca 100755 --- a/run +++ b/run @@ -7,6 +7,50 @@ function clean { rm -fr output_*/ source/build/ } +# Create a new daily email. +function create-daily { + local date="${1}" + local title="${2}" + + if [ "${date}" == "next" ]; then + next_date=$(ls -1 source/_daily_emails | tail -n 1 | tr -d '.md' | xargs -I {} date +%Y-%m-%d -d '{} +1 day') + else + next_date="${date}" + fi + + filepath="source/_daily_emails/${next_date}.md" + + shift 1 + + # Generate the title and slug. + title="${*}" + slug=$(echo "${title}" | \ + tr '[:upper:]' '[:lower:]' | \ + sed 's/[^a-z0-9]/-/g' | \ + sed 's/\-\-+/-/g' | \ + sed 's/^\-//;s/\-$//') + + # Create the file. + cp -f --no-clobber stub.md "${filepath}" + + date=$(date -d "${next_date}" +%Y-%m-%d) + day=$(date -d "${next_date}" +%d) + month=$(date -d "${next_date}" +%m) + year=$(date -d "${next_date}" +%Y) + + # Replace the placeholders. + sed -i "s/{{ date }}/${date}/" "${filepath}" + sed -i "s/{{ title }}/${title}/" "${filepath}" + sed -i "s#{{ permalink }}#archive/${year}/${month}/${day}/${slug}#" "${filepath}" + + # Create a commit with the appropriate date in the message + git add "${filepath}" + git commit --quiet -m "Add daily email for ${date} + +${title}" + + echo "${filepath}" +} function generate { local args=() diff --git a/stub.md b/stub.md new file mode 100644 index 00000000..a63225b7 --- /dev/null +++ b/stub.md @@ -0,0 +1,11 @@ +--- +title: >- + {{ title }} +date: {{ date }} +permalink: {{ permalink }} +snippet: | + TODO +# tags: +# - a +# - b +---