commit 1970382862a87156d520bf1d0f4844cf1236fc41
parent 78c5475b805b496d32d9094c249276fdb01d1f61
Author: St John Karp <contact@stjo.hn>
Date: Tue, 16 Jun 2020 10:14:27 -0500
Refactor shell scripts
Refactored the shell scripts for better readability.
Diffstat:
2 files changed, 22 insertions(+), 22 deletions(-)
diff --git a/squeeze.sh b/squeeze.sh
@@ -1,19 +1,19 @@
-#!/bin/bash
-
-OUTPUT_DIR=output
-SOURCE_DIR=source
+#!/usr/bin/env bash
SITE_PATH=$1
+OUTPUT_PATH="$SITE_PATH/output"
+SOURCE_PATH="$SITE_PATH/source"
+
# Copy everything that's not Markdown or HTML.
# This will also create the folder structure for the destination Markdown files.
-rsync --archive --delete --verbose --exclude "*.md" --exclude "*.html" --exclude "feeds" "$SITE_PATH/$SOURCE_DIR/" "$SITE_PATH/$OUTPUT_DIR/"
+rsync --archive --delete --verbose --exclude "*.md" --exclude "*.html" --exclude "feeds" "$SOURCE_PATH/" "$OUTPUT_PATH/"
# Delete any HTML files for which the source was removed.
-find "$SITE_PATH/$OUTPUT_DIR" -type f -name "*.html" |
+find "$OUTPUT_PATH" -type f -name "*.html" |
while read -r file; do
OLD_PATH=$(echo "$file" |
- sed "s|^$SITE_PATH/$OUTPUT_DIR|$SITE_PATH/$SOURCE_DIR|" |
+ sed "s|^$OUTPUT_PATH|$SOURCE_PATH|" |
sed 's|.html$|.md|')
if [ ! -f "$OLD_PATH" ]; then
rm "$file"
@@ -21,10 +21,10 @@ find "$SITE_PATH/$OUTPUT_DIR" -type f -name "*.html" |
done
# Parse and create all the HTML files.
-find "$SITE_PATH/$SOURCE_DIR" -type f -name "*.md" |
+find "$SOURCE_PATH" -type f -name "*.md" |
while read -r file; do
NEW_PATH=$(echo "$file" |
- sed "s|^$SITE_PATH/$SOURCE_DIR|$SITE_PATH/$OUTPUT_DIR|" |
+ sed "s|^$SOURCE_PATH|$OUTPUT_PATH|" |
sed 's|.md$|.html|')
# Only process files whose destination doesn't exist, or which has been recently changed.
if [ ! -f "$NEW_PATH" ] || [[ $(find "$file" -mtime -7) ]]; then
@@ -38,9 +38,9 @@ find "$SITE_PATH/$SOURCE_DIR" -type f -name "*.md" |
done
# Generate the RSS feed.
-mkdir -p "$SITE_PATH/$OUTPUT_DIR/feeds"
+mkdir -p "$OUTPUT_PATH/feeds"
# Grep the date of each article.
-ARTICLES=$(grep --recursive --include=\*.md "^Date: " "$SITE_PATH/$SOURCE_DIR" |
+ARTICLES=$(grep --recursive --include=\*.md "^Date: " "$SOURCE_PATH" |
# Reformat the output so the date comes first, then the file name.
sed --quiet --regexp-extended 's/^([^:]+):(.+)$/\2\t\1/p' |
# Sort articles by date.
@@ -50,7 +50,7 @@ ARTICLES=$(grep --recursive --include=\*.md "^Date: " "$SITE_PATH/$SOURCE_DIR" |
# Get the last (i.e. most recent) posts for the RSS feed.
tail -5 |
# Convert paths so we operate on the generated HTML, not the unformatted Markdown.
- sed "s|^$SITE_PATH/$SOURCE_DIR|$SITE_PATH/$OUTPUT_DIR|" |
+ sed "s|^$SOURCE_PATH|$OUTPUT_PATH|" |
sed 's|.md$|.html|' |
# Glue the file names together to be passed to Prolog.
paste --serial --delimiters=',' - |
@@ -58,4 +58,4 @@ ARTICLES=$(grep --recursive --include=\*.md "^Date: " "$SITE_PATH/$SOURCE_DIR" |
BUILD_DATE=$(date +"%Y-%m-%d %T")
# Parse the articles and generate the RSS.
swipl --traditional --quiet -l generate_rss.pl -g "consult('$SITE_PATH/site.pl'), generate_rss(\"$BUILD_DATE\", ['$ARTICLES'])." \
- > "$SITE_PATH/$OUTPUT_DIR/feeds/rss.xml"
+ > "$OUTPUT_PATH/feeds/rss.xml"
diff --git a/unsqueeze.sh b/unsqueeze.sh
@@ -1,20 +1,20 @@
-#!/bin/bash
-
-OUTPUT_DIR=output
-SOURCE_DIR=source
+#!/usr/bin/env bash
SITE_PATH=$1
+OUTPUT_PATH="$SITE_PATH/output"
+SOURCE_PATH="$SITE_PATH/source"
+
# Copy everything that's not Markdown or HTML.
# Excludes the RSS folder, which we create ourselves upon generation.
# This will also create the folder structure for the destination Markdown files.
-rsync --archive --delete --verbose --exclude "*.html" --exclude "*.md" --exclude "feeds" "$SITE_PATH/$OUTPUT_DIR/" "$SITE_PATH/$SOURCE_DIR/"
+rsync --archive --delete --verbose --exclude "*.html" --exclude "*.md" --exclude "feeds" "$OUTPUT_PATH/" "$SOURCE_PATH/"
# Delete any Markdown files for which the output was removed.
-find "$SITE_PATH/$SOURCE_DIR" -type f -name "*.md" |
+find "$SOURCE_PATH" -type f -name "*.md" |
while read -r file; do
OLD_PATH=$(echo "$file" |
- sed "s|^$SITE_PATH/$SOURCE_DIR|$SITE_PATH/$OUTPUT_DIR|" |
+ sed "s|^$SOURCE_PATH|$OUTPUT_PATH|" |
sed 's|.md$|.html|')
if [ ! -f "$OLD_PATH" ]; then
rm "$file"
@@ -22,10 +22,10 @@ find "$SITE_PATH/$SOURCE_DIR" -type f -name "*.md" |
done
# Parse and create all the markdown files.
-find "$SITE_PATH/$OUTPUT_DIR" -type f -name "*.html" |
+find "$OUTPUT_PATH" -type f -name "*.html" |
while read -r file; do
NEW_PATH=$(echo "$file" |
- sed "s|^$SITE_PATH/$OUTPUT_DIR|$SITE_PATH/$SOURCE_DIR|" |
+ sed "s|^$OUTPUT_PATH|$SOURCE_PATH|" |
sed 's|.html$|.md|')
swipl --traditional --quiet -l parse_entry.pl -g "consult('$SITE_PATH/site.pl'), parse_entry('$file')." |
# Unsmarten the punctuation.