Add hugo helpers

This commit is contained in:
Andrew Stryker
2026-03-24 21:41:09 -07:00
parent 80ea5b1c2c
commit 04aa189f8a
2 changed files with 119 additions and 0 deletions

72
hugo-new.sh Executable file
View File

@@ -0,0 +1,72 @@
#!/bin/bash
# Usage: ./hugo-new.sh [post|rmarkdown] "Awesome Post Title"
# Ensure Hugo is installed
if ! command -v hugo &> /dev/null; then
echo "Error: Hugo is not installed."
exit 1
fi
# Check arguments
if [ $# -lt 2 ]; then
echo "Usage: $0 [post|rmarkdown] \"Post Title\""
exit 1
fi
# Assign input variables
KIND="$1" # post or rmarkdown
TITLE="$2"
# Validate post type
if [[ "$KIND" != "post" && "$KIND" != "rmarkdown" ]]; then
echo "Error: Type must be 'post' or 'rmarkdown'."
exit 1
fi
# Generate ISO date prefix (YYYY-MM-DD)
DATE=$(date +"%Y-%m-%d")
# Convert title to a URL-friendly slug (lowercase, dashes)
SLUG=$(echo "$TITLE" | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
# Construct post directory
POST_DIR="content/posts/${DATE}-${SLUG}"
# Determine the expected file extension
EXT="md"
if [[ "$KIND" == "rmarkdown" ]]; then
EXT="Rmd"
fi
# Ensure the directory exists and create a page bundle
mkdir -p "$POST_DIR"
# Generate the new post with the selected archetype
hugo new --kind "$KIND" "posts/${DATE}-${SLUG}/index.${EXT}"
HUGO_EXIT_CODE=$?
# Validate Hugo execution
if [[ $HUGO_EXIT_CODE -ne 0 ]]; then
echo "Error: Hugo command failed."
exit 1
fi
# Check if the expected file was created
if [[ ! -f "$POST_DIR/index.${EXT}" ]]; then
echo "Error: Expected file was not created: $POST_DIR/index.${EXT}"
exit 1
fi
# Add .gitignore for rmarkdown posts to exclude generated output
if [[ "$KIND" == "rmarkdown" ]]; then
cat > "$POST_DIR/.gitignore" <<'EOF'
index.md
figure/
libs/
EOF
echo "Created .gitignore for generated output"
fi
# Confirm success
echo "New $KIND created at: $POST_DIR/index.${EXT}"

47
hugo.sh Normal file
View File

@@ -0,0 +1,47 @@
#!/bin/bash
set -e
# Usage: ./hugo-new.sh "<title>" [md|rmd]
TITLE="$2"
EXTENSION="${1:-rmd}" # default to Rmd if not specified
# Validate extension
if [[ "$EXTENSION" != "md" && "$EXTENSION" != "rmd" ]]; then
echo "⚠️ Usage: ./hugo-new.sh \"Post Title\" [md|rmd]"
exit 1
fi
DATE=$(date '+%Y-%m-%d')
SLUG=$(echo "$TITLE" | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
POST_DIR="content/posts/${DATE}-${SLUG}"
# Create new post using Hugo archetype
if [ "$EXTENSION" == "rmd" ]; then
POST_FILE="index.Rmd"
else
POST_DIR="content/posts/${DATE}-${SLUG}"
EXTENSION="md"
fi
hugo new "posts/${DATE}-${SLUG}/index.${EXTENSION}"
# Change to post directory
cd "$POST_DIR"
# Set up .gitignore and renv (for R Markdown)
if [ "$EXTENSION" == "rmd" ]; then
# Ignore generated Markdown file
echo "index.md" > .gitignore
# Set up minimal renv environment from base snapshot
cp ../../../../scripts/base-renv.lock ./renv.lock
Rscript -e '
if (!require("renv")) install.packages("renv");
renv::init(bare = TRUE);
renv::restore();
'
fi
echo "✅ Created post at ${POST_DIR}/index.${EXTENSION}"