diff --git a/bin/blog.sh b/bin/blog.sh index 00ae949..b72b20e 100755 --- a/bin/blog.sh +++ b/bin/blog.sh @@ -11,26 +11,30 @@ DRAFTS_DIR="drafts" CONTENT_DIR="content" BLOG_DIR="$CONTENT_DIR/blog" OUTPUT_DIR="_output" -mkdir -p "$OUTPUT_DIR" + +command -v "rsync" >/dev/null 2>&1 || { echo >&2 "!!! rsync it's not installed. Aborting."; exit 1; } build_pages() { + local OUTPUT_FILE rsync -r --exclude="*.html" "$CONTENT_DIR/" "$OUTPUT_DIR/" find "$CONTENT_DIR" -type f -name "*.html" | while read -r FILE; do - local OUTPUT_FILE="$(printf "$FILE" | sed "s/$CONTENT_DIR/$OUTPUT_DIR/")" - if [ ! -f "$OUTPUT_FILE" ]; then - printf '\n' > "$OUTPUT_FILE" - printf '\n' >> "$OUTPUT_FILE" - sed "s!!&$SITE_TITLE!" "templates/_head.html" >> "$OUTPUT_FILE" - printf '<body>\n' >> "$OUTPUT_FILE" - cat "templates/_nav.html" "$FILE" "templates/_footer.html" >> "$OUTPUT_FILE" - printf '</body>\n' >> "$OUTPUT_FILE" - printf '</html>\n' >> "$OUTPUT_FILE" - fi -done + OUTPUT_FILE="$(printf %s "$FILE" | sed "s/$CONTENT_DIR/$OUTPUT_DIR/")" + if [ ! -f "$OUTPUT_FILE" ]; then + printf '<!DOCTYPE html>\n' > "$OUTPUT_FILE" + printf '<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">\n' >> "$OUTPUT_FILE" + sed "s!<title>!&$SITE_TITLE!" "templates/_head.html" >> "$OUTPUT_FILE" + printf '<body>\n' >> "$OUTPUT_FILE" + cat "templates/_nav.html" "$FILE" "templates/_footer.html" >> "$OUTPUT_FILE" + printf '</body>\n' >> "$OUTPUT_FILE" + printf '</html>\n' >> "$OUTPUT_FILE" + fi + done } build_feed() { - local BLOG_INDEX="$BLOG_DIR/index.html" - local OUTPUT_FILE="$OUTPUT_DIR/index.xml" + local BLOG_INDEX + BLOG_INDEX="$BLOG_DIR/index.html" + local OUTPUT_FILE + OUTPUT_FILE="$OUTPUT_DIR/index.xml" printf '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n' > "$OUTPUT_FILE" printf '<?xml-stylesheet href="style.xsl" type="text/xsl"?>\n' >> "$OUTPUT_FILE" printf '<rss version="2.0">\n' >> "$OUTPUT_FILE" @@ -39,11 +43,15 @@ build_feed() { printf '<link>%s</link>\n' "$DOMAIN" >> "$OUTPUT_FILE" printf '<description>%s</description>\n' "$DESCRIPTION" >> "$OUTPUT_FILE" printf '<copyright>%s</copyright>\n' "$COPYRIGHT" >> "$OUTPUT_FILE" - grep -Eo "/blog/[a-zA-Z0-9./?=_%:-]*.html" "$BLOG_INDEX" | while read POST_URL; do - local POST_FILE="$CONTENT_DIR$POST_URL" - local POST_TITLE="$(grep "class=\"title\"" "$POST_FILE" | head -n 1 | sed 's!^[^>]*>!!' | sed 's!<.*$!!')" - local POST_DATE="$(date -d "$(grep "class=\"date\"" "$POST_FILE" | head -n 1 | sed 's!^[^>]*>!!' | sed 's!<.*$!!')" '+%a, %d %b %Y %H:%M:%S %Z')" - local POST_CONTENT="$(tail -n +6 "$POST_FILE" | head -n -1 | sed 's/^\t//g')" + grep -Eo "/blog/[a-zA-Z0-9./?=_%:-]*.html" "$BLOG_INDEX" | while read -r POST_URL; do + local POST_FILE + POST_FILE="$CONTENT_DIR$POST_URL" + local POST_TITLE + POST_TITLE="$(grep "class=\"title\"" "$POST_FILE" | head -n 1 | sed 's!^[^>]*>!!' | sed 's!<.*$!!')" + local POST_DATE + POST_DATE="$(date -d "$(grep "class=\"date\"" "$POST_FILE" | head -n 1 | sed 's!^[^>]*>!!' | sed 's!<.*$!!')" '+%a, %d %b %Y %H:%M:%S %Z')" + local POST_CONTENT + POST_CONTENT="$(tail -n +6 "$POST_FILE" | head -n -1 | sed 's/^\t//g')" printf '<item><author>%s</author>' "$AUTHOR" >> "$OUTPUT_FILE" printf '<pubDate>%s</pubDate>' "$POST_DATE" >> "$OUTPUT_FILE" printf '<title>%s' "$POST_TITLE" >> "$OUTPUT_FILE" @@ -58,30 +66,38 @@ done printf '\n' >> "$OUTPUT_FILE" } draft_post() { - local DRAFT_FILE="$DRAFTS_DIR/$1" mkdir -p "$DRAFTS_DIR" - cat "$TEMPLATES_DIR/_blog_post.html" > "$DRAFT_FILE" - nvim "$DRAFT_FILE" - read -p 'Post this draft? (Y/n) ' CHOICE + local DRAFT_FILE + DRAFT_FILE="$DRAFTS_DIR/$(basename "${1%.*}").html" + cp "$TEMPLATES_DIR/_blog_post.html" "$DRAFT_FILE" + "$EDITOR" "$DRAFT_FILE" + read -r -p 'Post this draft? (Y/n) ' CHOICE case "$CHOICE" in [nN]*) exit 0 ;; [yY]*|*) post_draft "$DRAFT_FILE" ;; esac } edit_draft() { - local DRAFT_FILE="$DRAFTS_DIR/$1" + local DRAFT_FILE + DRAFT_FILE="$DRAFTS_DIR/$(basename "${1%.*}").html" "$EDITOR" "$DRAFT_FILE" - read -p 'Post this draft? (Y/n) ' CHOICE + read -r -p 'Post this draft? (Y/n) ' CHOICE case "$CHOICE" in [nN]*) exit 0 ;; - [yY]*|*) post_draft "$(basename "${DRAFT_FILE%.*}.html")" ;; + [yY]*|*) post_draft "$DRAFT_FILE" ;; esac } post_draft() { - local DRAFT_FILE="$DRAFTS_DIR/$1" - local POST_TITLE="$(grep "class=\"title\"" "$DRAFT_FILE" | head -n 1 | sed 's!^[^>]*>!!' | sed 's!<.*$!!')" - local POST_DATE="$(date -d "$(grep "class=\"date\"" "$DRAFT_FILE" | head -n 1 | sed 's!^[^>]*>!!' | sed 's!<.*$!!')" '+%a, %d %b %Y %H:%M:%S %Z')" - local INDEX_LINE="

$POST_DATE
$POST_TITLE

" + local DRAFT_FILE + DRAFT_FILE="$DRAFTS_DIR/$(basename "${1%.*}").html" + local POST_URL + POST_URL="/$(basename "$BLOG_DIR")/$(basename "$DRAFT_FILE")" + local POST_TITLE + POST_TITLE="$(grep "class=\"title\"" "$DRAFT_FILE" | head -n 1 | sed 's!^[^>]*>!!' | sed 's!<.*$!!')" + local POST_DATE + POST_DATE="$(grep "class=\"date\"" "$DRAFT_FILE" | head -n 1 | sed 's!^[^>]*>!!' | sed 's!<.*$!!')" + local INDEX_LINE + INDEX_LINE="

$POST_DATE
$POST_TITLE

" sed -i "s##&\n$INDEX_LINE#" "$BLOG_DIR/index.html" mv "$DRAFT_FILE" "$BLOG_DIR" } @@ -94,7 +110,7 @@ drafts_selection() { PS3="Select a draft to post: " select FILE in "${FILENAMES[@]}"; do if [[ -n "$FILE" ]]; then - printf '%s' "$(basename "${FILE%.*}.html")" + printf '%s' "$(basename "$FILE")" break else printf 'Invalid selection.\n' >&2 @@ -106,7 +122,7 @@ drafts_dir_check() { printf 'Error: No drafts directory found\n' >&2 exit 1 fi - if ! find "$DRAFTS_DIR" -type f | read; then + if ! find "$DRAFTS_DIR" -type f | read -r; then printf 'Error: No files found in %s, removing empty directory.\n' "$DRAFTS_DIR" >&2 rmdir "$DRAFTS_DIR" exit 1 @@ -115,15 +131,16 @@ drafts_dir_check() { case "$1" in build) + mkdir -p "$OUTPUT_DIR" build_pages build_feed ;; draft) - read -p "Enter blog post file name: " INPUT + read -r -p "Enter blog post file name: " INPUT DRAFT_FILE="${INPUT%.*}.html" - if find "$DRAFTS_DIR" -name "$DRAFT_FILE" | read; then + if find "$DRAFTS_DIR" -name "$DRAFT_FILE" | read -r; then printf 'Error: Draft already exists with that filename.\n' - read -p 'Edit the file? (Y/n) ' CHOICE + read -r -p 'Edit the file? (Y/n) ' CHOICE case "$CHOICE" in [nN]*) exit 0 ;; [yY]*|*) edit_draft "$DRAFT_FILE" ;;