commit f053c16fce2d300e1af8ae5aec0744c81e8a08f5
parent 40d07c71e553d657fca8bc3d25324df1da8d5702
Author: Jake Bauer <jbauer@paritybit.ca>
Date: Mon, 30 Dec 2019 01:29:42 -0500
Create script for automatic publishing
Diffstat:
A | publish | | | 216 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 216 insertions(+), 0 deletions(-)
diff --git a/publish b/publish
@@ -0,0 +1,216 @@
+#!/bin/bash
+#
+# publish.sh
+#
+# Copyright (C) 2019 Jake Bauer
+#
+# A utility to publish posts/guides/projects 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
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see \<https://www.gnu.org/licenses/\>.
+
+# $1 = flag which determines which action to take
+# $2 = file to act upon
+
+mainFeed="public/feeds/sitewide-feed.xml"
+blogFeed="public/feeds/blog-feed.xml"
+projectsFeed="public/feeds/projects-feed.xml"
+guidesFeed="public/feeds/guides-feed.xml"
+descriptionFile="/tmp/publish-description.txt"
+destination="paritybit.ca:uploads/"
+
+if [ "$1" = "-b" ]; then
+ publishBlog=1
+ baseURL="https://www.paritybit.ca/blog"
+ basePage="blog"
+ categoryFeed=${blogFeed}
+ newString="New Blog Post:"
+elif [ "$1" = "-p" ]; then
+ publishProject=1
+ baseURL="https://www.paritybit.ca/projects"
+ basePage="projects"
+ categoryFeed=${projectsFeed}
+ newString="New Project:"
+elif [ "$1" = "-g" ]; then
+ publishGuide=1
+ baseURL="https://www.paritybit.ca/guides"
+ basePage="guides"
+ categoryFeed=${guidesFeed}
+ newString="New Guide:"
+else
+ cat << EOF
+Usage: publish <-b|-p|-g> <page_to_publish>
+Options:
+ -b — Publish a blog post
+ -p — Publish a project
+ -g — Publish a guide
+Examples:
+ publish -b pages/blog/sample-post.md
+ publish -p pages/projects/sample-project.md
+ publish -g pages/guides/sample-guide.md"
+EOF
+ exit 1
+fi
+
+if [ ! -f "$2" ]; then
+ echo "File $2 not found."
+ exit 2
+fi
+
+if [ ! -x "$(command -v rsync)" ]; then
+ echo "rsync is needed but was not found."
+ exit 3
+fi
+
+if [ ! -x "$(command -v ed)" ]; then
+ echo "ed is needed but was not found."
+ exit 3
+fi
+
+date=$(date +"%a, %d %b %Y %T %z")
+miniDate=$(date +"%F")
+filename=$(basename -s .md -- "$2")
+pageTitle=$(head -n 1 "$2" | cut -d' ' -f2-)
+entry=""
+
+# Make sure that there is an up-to-date compiled version of the page
+./compile "$2"
+
+# Update the relevant category page
+if [ $publishBlog ]; then
+ sed -i "/<ul>/a \ \ \ \ \<li\>$miniDate \<a href=\"$basePage\/$filename\"\>$pageTitle\<\/a\>\<\/li\>" pages/$basePage.md
+
+elif [ $publishProject ]; then
+ # The meta text of the .md becomes the description on the projects.html page
+ metaText=$(sed '3!d' pages/"$basePage"/"$filename".md | cut -d# -f 2)
+ # Remove leading whitespace, surrounding quotes, forward slash characters
+ metaTextClean=$(echo -e "${metaText}" | sed -e 's/^[[:space:]]*//')
+ metaTextClean=$(sed -e 's/\//\\\//' <<< "$metaTextClean")
+ metaTextClean=$(sed -e 's/^"//' -e 's/"$//' <<< "$metaTextClean")
+ sed -i "/<ul>/a \ \ \ \ \<li\>\<a href=\"$basePage\/$filename\"\>$pageTitle\<\/a\>: $metaTextClean\<\/li\>" pages/$basePage.md
+
+else
+ sed -i "/<ul>/a \ \ \ \ \<li\>\<a href=\"$basePage\/$filename\"\>$pageTitle\<\/a\>\<\/li\>" pages/$basePage.md
+fi
+./compile pages/$basePage.md
+
+# Update the home page to show the new published article and remove oldest entry
+sed -i "/<\/div>/a $miniDate \<a href=\"$basePage\/$filename\"\>$newString $pageTitle\<\/a\>\n" pages/home.md
+ed -s pages/home.md <<< $'3,$g/### What is a Parity Bit/-2,-1d\nw'
+./compile 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>
+if [ $publishBlog ]; then
+ entry="${entry}
+ <title>$newString $pageTitle</title>"
+elif [ $publishProject ]; then
+ entry="${entry}
+ <title>$newString $pageTitle</title>"
+elif [ $publishGuide ]; then
+ entry="${entry}
+ <title>$newString $pageTitle</title>"
+else
+ echo "Something went very wrong... (<title>)"
+ exit -1
+fi
+
+# 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>"
+
+# Get content of \<page\>-content.html and make description
+if [ $publishBlog ]; then
+ pageContent=$(cat build/blog/"$filename"-content.html)
+ entry="${entry}
+ <description><![CDATA[$pageContent]]></description>"
+elif [ $publishProject ] || [ $publishGuide ]; then
+ echo "REPLACE THIS TEXT WITH A DESCRIPTION FOR THE PROJECT/GUIDE." \
+ > $descriptionFile
+ /usr/bin/editor $descriptionFile
+ description=$(cat $descriptionFile)
+ entry="${entry}
+ <description><![CDATA[$description]]></description>"
+else
+ echo "Something went very wrong... (<description>)"
+ exit -1
+fi
+
+# Close the entry
+entry="${entry}
+</item>"
+
+tput setaf 2; tput bold
+echo "[DONE]"
+tput sgr0
+
+# Update the main feed and the relevant category feed
+printf " Updating feed files... "
+
+mv "$mainFeed" "$mainFeed.bak"
+head -n 8 "$mainFeed.bak" > "$mainFeed"
+echo "$entry" >> "$mainFeed"
+tail -n +9 "$mainFeed.bak" >> "$mainFeed"
+
+mv "$categoryFeed" "$categoryFeed.bak"
+head -n 8 "$categoryFeed.bak" > "$categoryFeed"
+echo "$entry" >> "$categoryFeed"
+tail -n +9 "$categoryFeed.bak" >> "$categoryFeed"
+
+tput setaf 2; tput bold
+echo "[DONE]"
+tput sgr0
+
+# Upload the new feeds plus the new article and updated pages to the website
+printf " Uploading pages and feeds... "
+
+if [ $publishBlog ]; then
+ categoryPage="html/blog.html"
+ article="html/blog/$filename.html"
+ feed="feeds/blog-feed.xml"
+elif [ $publishProject ]; then
+ categoryPage="html/projects.html"
+ article="html/projects/$filename.html"
+ feed="feeds/projects-feed.xml"
+elif [ $publishGuide ]; then
+ categoryPage="html/guides.html"
+ article="html/guides/$filename.html"
+ feed="feeds/guides-feed.xml"
+else
+ echo "Something went very wrong... (uploading files)"
+ exit -1
+fi
+
+echo "cd public && rsync --progress -rR html/home.html feeds/sitewide-feed.xml $feed $categoryPage $article $destination && cd .."
+
+tput setaf 2; tput bold
+echo "[DONE]"
+tput sgr0
+
+tput bold
+echo "### PUBLISHED: $filename | Visit: $baseURL/$filename"
+tput sgr0
+
+exit 0