#!/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 and set file extension case "$KIND" in post) EXT="md" ;; rmarkdown) EXT="Rmd" ;; *) echo "Error: Type must be 'post' or 'rmarkdown'." exit 1 ;; esac # 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 ' ' '-') POST_SLUG="posts/${DATE}-${SLUG}" POST_SLUG_EXT="${POST_SLUG}/index.${EXT}" # Construct post directory POST_DIR="content/${POST_SLUG}" # Generate the new post with the selected archetype hugo new --kind "$KIND" "${POST_SLUG_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 # place work in a new branch git checkout -b "${POST_SLUG}" || \ echo "Failed to create brach ${POST_SLUG}" # 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}"