paritybit.ca

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 4afb6680869099013789317bf355a1e4754f0f26
parent 68b058e0f578a5320b846aeb911694572dd8de7f
Author: Jake Bauer <jbauer@paritybit.ca>
Date:   Thu, 11 Jun 2020 15:20:13 -0400

Refactor publish script

Diffstat:
Mpublish | 363+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
1 file changed, 217 insertions(+), 146 deletions(-)

diff --git a/publish b/publish @@ -1,10 +1,10 @@ -#!/bin/bash -# -# publish.sh +#!/bin/sh + +# publish # -# Copyright (C) 2019 Jake Bauer +# Copyright (C) 2019,2020 Jake Bauer # -# A utility to publish posts/guides/projects to paritybit.ca. +# A utility to publish blog posts to paritybit.ca. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -19,163 +19,234 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see \<https://www.gnu.org/licenses/\>. -mainFeed="public/feeds/sitewide-feed.xml" -destination="paritybit.ca:uploads/" - -date=$(date +"%a, %d %b %Y %T %z") -miniDate=$(date +"%F") -filename=$(basename -s .md -- "$1") -pageTitle=$(head -n 1 "$1" | cut -d' ' -f2-) -entry="" - -baseURL="https://www.paritybit.ca/blog" -basePage="blog" -categoryPage="html/blog.html" -article="html/blog/$filename.html" - -if [ "$1" = "" ]; then - echo "Missing file to publish." - exit 2 -fi - -if [ ! -f "$1" ]; then - echo "File $1 not found." - exit 2 -fi +# POSIX Shell "Strict Mode" +set -o errexit +set -o nounset +IFS=$(printf '\n\t') if [ ! -x "$(command -v rsync)" ]; then echo "rsync is needed but was not found." - exit 3 + exit 1 fi - if [ ! -x "$(command -v ed)" ]; then echo "ed is needed but was not found." - exit 3 + exit 1 +fi +if [ "$1" = "" ]; then + echo "No arguments given; nothing to do." + exit 1 fi +# Define colours +CLEAR="\033[0m" +RED="\033[0;31m" +GREEN="\033[0;32m" +YELLOW="\033[0;33m" -# Populate the [DATE] fields of the page -sed -i "s/\[DATE\]/$miniDate/g" "$1" -./compile "$1" +# The RSS feed to edit +mainFeed="public/feeds/sitewide-feed.xml" +# Where to upload files +remote="paritybit.ca:uploads/" +# The main base page on the site +baseURL="https://www.paritybit.ca/blog" +# Name of the base page +basePage="blog" +# The path to the compiled base page +categoryPage="html/blog.html" +# Set to false to disable auto uploading files +autoUpload="false" +# Set to false to disable auto posting statuses on Mastodon +autoPost="false" + +# Prints the message passed as the 1st argument in red. +print_error_msg() +{ + printf "$RED[ee] %s\n$CLEAR" "$1" +} + +# Prints the message passed as the 1st argument in green. +print_success_msg() +{ + printf "$GREEN[ok] %s\n$CLEAR" "$1" +} + +# Prints the message passed as the 1st argument in yellow. +print_header_msg() +{ + printf "$YELLOW[hh] %s\n$CLEAR" "$1" +} + +# Prints the message passed as the 1st argument with default colours. +print_info_msg() +{ + printf "$CLEAR[ii] %s\n" "$1" +} + +# Thanks to: https://stackoverflow.com/a/27875395/12865517 +# First argument is the yes/no question to be asked +confirm() +{ + printf "%s [y/N]? " "$1" + old_stty_cfg=$(stty -g) + stty raw -echo + answer=$( while ! head -c 1 | grep -i '[ny]' ;do true ;done ) + stty "$old_stty_cfg" + if echo "$answer" | grep -iq "^y" ;then + return 1 + fi + return 0 +} + +# Populate the [DATE] fields of the blog post +populate_date() +{ + print_info_msg "Populating [DATE] fields" + sed -i -e "s/\[DATE\]/$shortDate/g" "$file" + ./compile "$file" +} # Update the main blog list page -awk -v pt="$pageTitle" -v bp="$basePage" -v f="$filename" -v d="$miniDate" \ - '{print} /^<ul>/ && !n {print " <li>"d" <a href=\""bp"/"f"\">"pt"</a></li>"; n++}' \ -pages/"$basePage".md > tmp.md && mv tmp.md pages/"$basePage".md +update_base_page() +{ + print_info_msg "Updating base page" + awk -v pt="$pageTitle" -v bp="$basePage" -v f="$fileName" -v d="$shortDate" \ + '{print} /^<ul>/ && !n {print " <li>"d" <a href=\""bp"/"f"\">"pt"</a></li>"; n++}' \ + pages/"$basePage".md > tmp.md && mv tmp.md pages/"$basePage".md + ./compile pages/$basePage.md +} -./compile pages/$basePage.md # Update the home page to show the new published article and remove oldest entry -sed -i "/<\/div>/a $miniDate \<a class=\"feed-item\" href=\"$basePage\/$filename\"\>$pageTitle\<\/a\>\n" pages/home.md -ed -s pages/home.md <<< $'8,$g/### What is a Parity Bit/-2,-1d\nw' - -./compile -s pages/home.md - - -tput setaf 3; tput bold; tput smul -echo "### PUBLISHING: $filename" -tput sgr0 -printf " Creating RSS <item>... " - -# Opening tag -entry="${entry}<item>" - -# Set <title> -entry="${entry} - <title>$pageTitle</title>" - -# Set <link> and <guid> -entry="${entry} - <link>$baseURL/$filename</link>" -entry="${entry} - <guid>$baseURL/$filename</guid>" - -# Set pubDate using date command -entry="${entry} - <pubDate>$date</pubDate>" - -pageContent=$(cat build/blog/"$filename"-content.html) -entry="${entry} -<description><![CDATA[$pageContent]]></description>" - -# Close the entry -entry="${entry} -</item>" - -tput setaf 2; tput bold -echo "[DONE]" -tput sgr0 - - -# Update the RSS feed -printf " Updating RSS feed... " - -mv "$mainFeed" "$mainFeed.bak" -head -n 8 "$mainFeed.bak" > "$mainFeed" -echo "$entry" >> "$mainFeed" -tail -n +9 "$mainFeed.bak" >> "$mainFeed" - -tput setaf 2; tput bold -echo "[DONE]" -tput sgr0 - - -# Update the sitemap -printf " Updating sitemap.xml " -baseURLClean=$(sed -e 's/\//\\\//g' <<< "$baseURL") -sed -i "/$baseURLClean</a \ \ \ \ \ \ \ \<url\>\<loc\>$baseURLClean\/$filename\<\/loc\>\<\/url\>" public/sitemap.xml - -tput setaf 2; tput bold -echo "[DONE]" -tput sgr0 - +update_home_page() +{ + print_info_msg "Updating home page" + sed -i -e "/<\/div>/a\\ +$shortDate \<a class=\"feed-item\" href=\"$basePage\/$fileName\"\>$pageTitle\<\/a\>\n" \ + pages/home.md + echo \$'8,$g/### What is a Parity Bit/-2,-1d\nw' | ed -s pages/home.md + ./compile -s pages/home.md +} + +create_rss_entry() +{ + print_info_msg "Creating RSS entry" + + # Opening tag + rssEntry="${rssEntry}<item>" + + # Set <title> + rssEntry="${rssEntry} + <title>$pageTitle</title>" + + # Set <link> and <guid> + rssEntry="${rssEntry} + <link>$baseURL/$fileName</link>" + rssEntry="${rssEntry} + <guid>$baseURL/$fileName</guid>" + + # Set pubDate + rssEntry="${rssEntry} + <pubDate>$date</pubDate>" + + # Fill out content + pageContent=$(cat build/blog/"$fileName"-content.html) + rssEntry="${rssEntry} + <description><![CDATA[$pageContent]]></description>" + + # Close the entry + rssEntry="${rssEntry} + </item>" +} + +update_rss_feed() +{ + print_info_msg "Updating RSS feed" + mv "$mainFeed" "$mainFeed.bak" + head -n 8 "$mainFeed.bak" > "$mainFeed" + echo "$rssEntry" >> "$mainFeed" + tail -n +9 "$mainFeed.bak" >> "$mainFeed" +} + +update_sitemap() +{ + print_info_msg "Updating sitemap" + echo "$baseURL" > /tmp/base-url.txt + baseURLClean=$(sed -i -e 's/\//\\\//g' /tmp/base-url.txt) + sed -i -e "/$baseURLClean</a\\ + <url\>\<loc\>$baseURLClean\/$fileName\<\/loc\>\<\/url\>" \ + public/sitemap.xml +} # Upload the new feed plus the new article and updated pages to the website -printf " Uploading pages and feed... " - -cd public && rsync -rR html/home.html sitemap.xml feeds/sitewide-feed.xml \ -"$categoryPage" "$article" "$destination" && cd .. - -tput setaf 2; tput bold -echo "[DONE]" -tput sgr0 - +auto_upload() +{ + if [ "$autoUpload" != "true" ]; then + return 0 + fi + print_info_msg "Auto-uploading files" + cd public && rsync -rR html/home.html sitemap.xml feeds/sitewide-feed.xml \ + "$categoryPage" "$article" "$remote" && cd .. +} # Post a status update on Mastodon linking to the new blog post -printf " Posting about it on Mastodon... " -accessToken="<ACCESS_TOKEN>" -status="$(cat << END -New Blog Post: $(head -n 1 $1 | cut -d" " -f 2-) - -$baseURL/$filename - -#100DaysToOffload -END -)" -uniqueId="$(md5sum <<< $status | awk '{print $1}')" - -resp="$(curl -s -S -i -X POST \ - -H "Idempotency-Key: $uniqueId" \ - -H "Authorization: Bearer $accessToken" \ - -F status="$status" \ - -F visibility=public \ - https://social.paritybit.ca/api/v1/statuses)" - -httpCode="$(echo "$resp" | grep HTTP/ | awk '{print $2}')" - -if [ "$httpCode" = "200" ]; then - tput setaf 2; tput bold - echo "Status updated successfully." - tput sgr0 -else - tput setaf 1; tput bold - echo "Status failed to update, post manually later:" - tput sgr0 - echo "$resp" -fi - -tput bold -echo "### PUBLISHED: $filename | Visit: $baseURL/$filename" -tput sgr0 +auto_post() +{ + if [ "$autoPost" != "true" ]; then + return 0 + fi + + print_info_msg "Posting about it on Mastodon" + accessToken="" + meta="$(head -n3 "$file" | tail -n1 \ + | cut -d'#' -f2 | sed -e 's/^[[:space:]]*//')" + printf "New Blog Post: %s\n\n%s%s\n\n%s\n\n#100DaysToOffload" \ + "$pageTitle" "$baseURL" "$fileName" "$meta" > /tmp/status.txt + uniqueId="$(md5sum /tmp/status.txt | awk '{print $1}')" + + resp="$(curl -s -S -i -X POST \ + -H "Idempotency-Key: $uniqueId" \ + -H "Authorization: Bearer $accessToken" \ + -F status="$(cat /tmp/status.txt)" \ + -F visibility=public \ + https://social.paritybit.ca/api/v1/statuses)" + httpCode="$(echo "$resp" | grep HTTP/ | awk '{print $2}')" + + if [ "$httpCode" = "200" ]; then + print_success_msg "Successfully posted status update" + else + print_error_msg "ERROR: Failed to post status update" + echo "$resp" + fi +} + +for file in "$@"; do + if [ ! -f "$file" ]; then + print_error_msg "ERROR: File $file not found or is not a file." + continue + fi + + date=$(date +"%a, %d %b %Y %T %z") + shortDate=$(date +"%F") + fileName=$(basename -s .md -- "$file") + pageTitle=$(head -n 1 "$file" | cut -d' ' -f2-) + article="html/blog/$fileName.html" + rssEntry="" + + print_header_msg "PUBLISHING: $file" + confirmed=$(confirm "Are you sure you wish to publish this post?") + [ ! "$confirmed" ] && continue + + populate_date + update_base_page + update_home_page + create_rss_entry + update_rss_feed + update_sitemap + auto_upload + auto_post + + print_success_msg "PUBLISHED: $file" + print_success_msg "Visit: $baseURL/$fileName.html" + +done -exit 0