commit 569e7a74aea5ad26058790714a8e5f22ff29a8d8
parent b8811c6c970b805c82ecd9864df5d1978b9a40c0
Author: St John Karp <contact@stjo.hn>
Date: Sat, 21 Aug 2021 12:44:09 -0400
Refactor shell scripts to use strictly POSIX-compatible utils
The shell scripts were using GNU-specific extensions to find, xargs,
etc. I have refactored them to use strictly POSIX-only features.
This should allow Squeeze to be a lot more portable, e.g. if you
want to run it on one of the BSDs or a system with Suckless sbase.
Diffstat:
5 files changed, 81 insertions(+), 20 deletions(-)
diff --git a/generate_html.sh b/generate_html.sh
@@ -1,8 +1,16 @@
#!/usr/bin/env sh
-echo "$1"
+# Convert a Markdown file to HTML using a site's template.
-swipl --traditional --quiet -l parse_entry.pl -g "consult('$2/site.pl'), generate_entry('$2/source/$1')." |
+# Usage: generate_html.sh SITE_PATH MARKDOWN_FILE
+#
+# MARKDOWN_FILE is expected to be found at SITE_PATH/source/MARKDOWN_FILE.
+# The resulting HTML will be saved to SITE_PATH/output/HTML_FILE, where
+# HTML_FILE is the same filename as MARKDOWN_FILE but with a .html extension.
+
+echo "$2"
+
+swipl --traditional --quiet -l parse_entry.pl -g "consult('$1/site.pl'), generate_entry('$1/source/$2')." |
# Unwrap block-level elements that have erroneously been wrapped in <p> tags.
sed "s|<p><details|<details|g" |
sed "s|</summary></p>|</summary>|g" |
@@ -11,4 +19,4 @@ swipl --traditional --quiet -l parse_entry.pl -g "consult('$2/site.pl'), generat
sed "s|</figure></p>|</figure>|g" |
# Smarten punctuation.
smartypants \
- > "$2/output/${1%%.md}.html"
+ > "$1/output/${2%%.md}.html"
diff --git a/generate_html_list.sh b/generate_html_list.sh
@@ -0,0 +1,22 @@
+#!/usr/bin/env sh
+
+# Loop through a list of Markdown files and generate the corresponding HTML,
+# launching a background job for each process.
+
+# Usage: generate_html_list SITE_PATH MARKDOWN_FILE...
+
+# The site path will be the first argument.
+# Save its value and shift it down so we can take the rest together.
+SITE_PATH="$1"
+shift
+
+# Get all the remaining arguments. These will be the file names.
+# Remove the start of the path.
+ARGS="$(echo "$@" | sed "s|$SITE_PATH/source/||g")"
+
+for arg in $ARGS; do
+ ./generate_html.sh "$SITE_PATH" $arg &
+done
+
+# Wait until all jobs have completed.
+wait
diff --git a/generate_markdown.sh b/generate_markdown.sh
@@ -1,6 +1,15 @@
#!/usr/bin/env sh
-echo "$1"
+SITE_PATH="$1"
+shift
-swipl --traditional --quiet -l parse_entry.pl -g "consult('$2/site.pl'), parse_entry('$2/output/$1')." \
- > "$2/source/${1%%.html}.md"
+ARGS="$(echo "$@" | sed "s|$SITE_PATH/output/||g")"
+
+for arg in $ARGS; do
+ echo "$arg"
+
+ swipl --traditional --quiet -l parse_entry.pl -g "consult('$SITE_PATH/site.pl'), parse_entry('$SITE_PATH/output/$arg')." \
+ > "$SITE_PATH/source/${arg%%.html}.md" &
+done
+
+wait
diff --git a/squeeze.sh b/squeeze.sh
@@ -1,9 +1,13 @@
#!/usr/bin/env sh
-export SITE_PATH=$1
+# Generate a static website.
-export OUTPUT_PATH="$SITE_PATH/output"
-export SOURCE_PATH="$SITE_PATH/source"
+# Usage: squeeze.sh SITE_PATH
+
+SITE_PATH=$1
+
+OUTPUT_PATH="$SITE_PATH/output"
+SOURCE_PATH="$SITE_PATH/source"
# Copy everything that's not Markdown.
# This will also create the folder structure for the destination Markdown files.
@@ -12,19 +16,20 @@ rsync --archive --delete --verbose \
"$SOURCE_PATH/" "$OUTPUT_PATH/"
# Parse and create all the HTML files.
-find "$SOURCE_PATH" -type f -name "*.md" -printf "%P\0" |
- xargs --null --max-procs 99 -I % sh generate_html.sh "%" "$SITE_PATH"
+find "$SOURCE_PATH" -type f -name "*.md" \
+ -exec ./generate_html_list.sh "$SITE_PATH" {} +
# Generate the RSS feed.
mkdir -p "$OUTPUT_PATH/feeds"
# Grep the date of each article.
-grep --recursive --include "*.html" "id=\"article-date\"" "$OUTPUT_PATH" |
+find "$OUTPUT_PATH" -type f -name "*.html" \
+ -exec grep "id=\"article-date\"" {} + |
# Sort articles by date (skipping the first field).
- sort +1 |
+ sort -k 2 |
# Get the last (i.e. most recent) posts for the RSS feed.
tail -5 |
# Reformat to just the file names.
- cut --fields 1 --delimiter : |
+ cut -f 1 -d : |
# Parse the articles and generate the RSS.
swipl --traditional --quiet -l generate_rss.pl -g "consult('$SITE_PATH/site.pl'), generate_rss." \
> "$OUTPUT_PATH/feeds/rss.xml"
diff --git a/unsqueeze.sh b/unsqueeze.sh
@@ -1,5 +1,9 @@
#!/usr/bin/env sh
+# Ungenerate a static website.
+
+# Usage: unsqueeze.sh SITE_PATH
+
export SITE_PATH=$1
export OUTPUT_PATH="$SITE_PATH/output"
@@ -13,11 +17,24 @@ rsync --archive --delete --verbose \
"$OUTPUT_PATH/" "$SOURCE_PATH/"
# Parse and create all the Markdown files.
-find "$OUTPUT_PATH" -type f -name "*.html" -printf "%P\0" |
- xargs --null --max-procs 99 -I % sh generate_markdown.sh "%" "$SITE_PATH"
+find "$OUTPUT_PATH" -type f -name "*.html" \
+ -exec ./generate_markdown.sh "$SITE_PATH" {} +
# Unsmarten the punctuation.
-find "$SOURCE_PATH" -type f -name "*.md" \
- -exec sed -i "s/ / /g" {} + \
- -exec sed -E -i "s/('|‘|’|’|‘)/'/g" {} + \
- -exec sed -E -i "s/(“|”|”|“|")/\"/g" {} +
+MARKDOWN_FILES="$(find "$SOURCE_PATH" -type f -name "*.md")"
+for markdown_file in $MARKDOWN_FILES; do
+ sed "s/ / /g" "$markdown_file" |
+ # Replace single quotes.
+ sed "s/'/'/g" |
+ sed "s/‘/'/g" |
+ sed "s/’/'/g" |
+ sed "s/’/'/g" |
+ sed "s/‘/'/g" |
+ # Replace double quotes.
+ sed "s/“/\"/g" |
+ sed "s/”/\"/g" |
+ sed "s/”/\"/g" |
+ sed "s/“/\"/g" |
+ sed "s/"/\"/g" \
+ > "$markdown_file"
+done