blog-devlog.sh (1723B)
1 #!/bin/sh 2 3 # Helper scripts for my blog and devlog 4 5 blog () { 6 # Get dir, make if doesn't exist 7 dir="$HOME/Programs/websites/personal-website/blog/$(date +%Y/%B | awk '{print tolower($0)}')" 8 [ -d "$dir" ] || mkdir -p "$dir" 9 # If cd argument given just echo the dir and exit (a function in shell rc picks this up and does the actual cd) 10 [ "$1" = "cd" ] && echo "$dir" && exit 11 12 # Get date in proper format 13 month_num="$(date "+%m")" 14 year="$(date "+%Y")" 15 date="$(date "+%d")" 16 # Prompt for filename and title 17 read -p "Enter filename: " filename 18 read -p "Enter title: " title 19 if ! echo "$filename" | grep -q ".md$"; then 20 filename="${filename}.md" 21 fi 22 # Add header to document and open 23 fullpath="${dir}/${filename}" 24 echo "+++" >> "$fullpath" 25 echo "title = \"${title}\"" >> "$fullpath" 26 echo "datePublished = ${year}-${month_num}-${date}" >> "$fullpath" 27 echo "template = \"blog-page.html\"" >> "$fullpath" 28 echo "+++" >> "$fullpath" 29 "${VISUAL:-${EDITOR:-vi}}" "$fullpath" 30 } 31 32 devlog () { 33 # Get date and form filename 34 month_num="$(date "+%m")" 35 month_name="$(date "+%B")" 36 year="$(date "+%Y")" 37 date="$(date "+%d")" 38 filename="$HOME/Programs/websites/personal-website/devlog/${year}-${month_num}-${month_name}.md" 39 40 # If the file exists, just open it 41 if [ -f "$filename" ]; then 42 "${VISUAL:-${EDITOR:-vi}}" "$filename" 43 # Otherwise make and add header 44 else 45 echo "+++" >> "$filename" 46 echo "title = \"${month_name} ${year} Devlog\"" >> "$filename" 47 echo "datePublished = ${year}-${month_num}-${date}" >> "$filename" 48 echo "template = \"blog-page.html\"" >> "$filename" 49 echo "+++" >> "$filename" 50 "${VISUAL:-${EDITOR:-vi}}" "$filename" 51 fi 52 } 53 54 case "$1" in 55 b*) 56 blog "$2" 57 ;; 58 d*) 59 devlog 60 ;; 61 esac