Add hugo helpers
This commit is contained in:
Executable
+72
@@ -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}"
|
||||
Reference in New Issue
Block a user