Skip to content

Commit

Permalink
.scripts/gen-post: make it POSIX shell compatible, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagokokada committed Aug 20, 2024
1 parent fd64ce3 commit a0d421c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
23 changes: 10 additions & 13 deletions .scripts/gen-post.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
#!/usr/bin/env bash
#!/bin/sh

set -euo pipefail
set -eu

# Expect to be set as environment variables
readonly DATE TITLE
readonly DATE SLUG TITLE

SCRIPT_DIR="$(dirname -- "${BASH_SOURCE[0]}")"
readonly SCRIPT_DIR

SLUG="$(cd "$SCRIPT_DIR" && ../blog -slugify "$TITLE")"
readonly SLUG

for i in $(seq -f "%02g" 99); do
i=1
while [ "$i" -ne 100 ]; do
n="$(printf '%.2d' "$i")"
# Match either normal files or hidden post files
if compgen -G "$DATE/$i*.md" &>/dev/null || \
compgen -G "$DATE/.$i*.md" &>/dev/null; then
if ls "$DATE/$n"*".md" >/dev/null 2>&1 || \
ls "$DATE/.$n"*".md" >/dev/null 2>&1; then
i=$(( i + 1 ))
continue
fi

file="$DATE/$i-$SLUG.md"
file="$DATE/$n-$SLUG.md"
echo "Creating file: $file"
echo "# ${TITLE}" > "$file"
exit 0
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ day:
mkdir -p '$(DATE)'

TITLE = $(error TITLE is not defined)
SLUG = $(shell ./blog -slugify "$(TITLE)")
.PHONY: post
post: blog day
@echo $(TITLE) >/dev/null # this is to force an error if TITLE is unset
DATE=$(DATE) ./.scripts/gen-post.sh
DATE=$(DATE) SLUG=$(SLUG) ./.scripts/gen-post.sh

FILE = $(error FILE is not defined)
.PHONY: draft
Expand Down

0 comments on commit a0d421c

Please sign in to comment.