#!/bin/sh # Static Blogging System: A blog generation script # 2021 Bryce Vandegrift # This program is licensed under the GPL V3 # Report bugs to: bryce@brycevandegrift.xyz name="Bryce Vandegrift" website="https://brycevandegrift.xyz" website_dir="/home/bryce/Documents/website" # Absolute path to website directory (replace with $(pwd) for local dir use) blog_dir="$website_dir/blog" # Blog directory relative to website directory blogindex="$blog_dir/index.html" rssindex="$website_dir/rss.xml" # RSS index relative to website directory draft_dir="$website_dir/.drafts" # Draft directory relative to website directory html_css_path="//brycevandegrift.xyz/index.css" # Path to css stylesheet html_icon_path="//brycevandegrift.xyz/p/icon.ico" # Path to web icon default() { [ -z "$1" ] && help || echo "ERROR: $1 is not a valid command!" && exit 1 } listDrafts() { echo "Printing drafts" case "$(ls "$draft_dir" | wc -l)" in 0) echo "There are no drafts." && exit 1 ;; *) ls -rc "$draft_dir" | nl read -rp "Pick a draft to $1: " number ;; esac choice="$(ls -rc "$draft_dir" | nl | grep -w $number | awk '{print $2}')" } new() { read -rp "Enter file name: " name echo $name | grep -q "\"" && echo "Double quotes are not allowed in file names." && exit 1 name="$(echo $name | tr -d '[:punct:]' | tr ' ' '-' | tr '[:upper:]' '[:lower:]')" ( [ -f "$draft_dir/$name.md" ] || [ -f "$blog_dir/$name.html" ] ) && echo "A draft or post already exists under that name!" && exit 1 $EDITOR "$draft_dir/$name.md" } edit() { listDrafts edit "$@" $EDITOR "$draft_dir/$choice" } publish() { listDrafts publish "$@" dir_name=$(echo $choice | sed 's/.md$//g') date=$(date '+%a, %d %b %Y %H:%M:%S %z') url="$website/blog/$dir_name/$dir_name.html" contents=$(pandoc -f markdown -t html "$draft_dir/$choice") title=$(echo "$contents" | grep "$" | sed 's/<\/h1>$//g' | sed 's/^//g') tmp_dir=$(mktemp -d) mkdir $blog_dir/$dir_name printf "\n\n\n\n\n\n%s\n\n\n%s\n\n" "$html_icon_path" "$html_css_path" "$title" "$contents" > $blog_dir/$dir_name/$dir_name.html printf "\n\n%s\n%s\n%s\n%s\n\n%s\n\n\n" "$title" "$url" "$url" "$date" "$contents" > $tmp_dir/rss.xml printf "\n
  • %s - %s
  • \n" "$(date '+%b %d, %Y')" "$url" "$title" > $tmp_dir/preview.html sed -i "//r $tmp_dir/rss.xml" $rssindex sed -i "//r $tmp_dir/preview.html" $blogindex sed -i "//r $tmp_dir/preview.html" $website_dir/index.html rm -f $draft_dir/$choice } delete() { listDrafts delete "$@" rm "$draft_dir/$choice" } help() { cat <<-EOF Usage: sbs [OPERATION] ... Operations: sbs new sbs edit sbs publish sbs delete sbs help sbs version Read README.md for more details. EOF } version() { echo "sbs: Static Blogging System V1.0.0" } [ -z "$EDITOR" ] && EDITOR="vi" case "$1" in new) shift; new "$@" ;; edit) shift; edit "$@" ;; publish) shift; publish "$@" ;; delete|del) shift; delete "$@" ;; help|-h) help ;; version|-v) version ;; *) default "$@" ;; esac exit 0