commit 6e337f971ca5908ae02487cc382b3e06bd550d9a
parent 64a60a25019d0ee71b1fe597f47d607eeb8f53b9
Author: St John Karp <contact@stjo.hn>
Date: Sun, 21 Jun 2020 00:17:26 -0500
Refactor shell scripts to remove conditionals/loops
Removed the conditionals and loops in favor of reprocessing all
the articles every time. I sped up that process by using xargs
in parallel. This has the advantage that if you alter your template,
you don't need to delete all your HTML files to force Squeeze to
regenerate them.
Diffstat:
M | squeeze.sh | | | 47 | +++++++++++++++++------------------------------ |
M | unsqueeze.sh | | | 48 | +++++++++++++++++++----------------------------- |
2 files changed, 36 insertions(+), 59 deletions(-)
diff --git a/squeeze.sh b/squeeze.sh
@@ -1,53 +1,40 @@
#!/usr/bin/env sh
-SITE_PATH=$1
+export SITE_PATH=$1
-OUTPUT_PATH="$SITE_PATH/output"
-SOURCE_PATH="$SITE_PATH/source"
+export OUTPUT_PATH="$SITE_PATH/output"
+export SOURCE_PATH="$SITE_PATH/source"
-# Copy everything that's not Markdown or HTML.
+# Copy everything that's not Markdown.
# This will also create the folder structure for the destination Markdown files.
-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 "$OUTPUT_PATH" -type f -name "*.html" |
- while read -r file; do
- OLD_PATH=$(echo "$file" |
- sed "s|^$OUTPUT_PATH|$SOURCE_PATH|" |
- sed 's|.html$|.md|')
- [ ! -f "$OLD_PATH" ] && rm "$file"
- done
+rsync --archive --delete --verbose \
+ --exclude "*.md" --exclude "feeds" \
+ "$SOURCE_PATH/" "$OUTPUT_PATH/"
# Parse and create all the HTML files.
-find "$SOURCE_PATH" -type f -name "*.md" |
- while read -r file; do
- NEW_PATH=$(echo "$file" |
- sed "s|^$SOURCE_PATH|$OUTPUT_PATH|" |
- sed 's|.md$|.html|')
- # Only process files whose destination doesn't exist, or which has been recently changed.
- [ ! -f "$NEW_PATH" ] || find "$file" -mtime -7 | grep -q . &&
- echo "$file" &&
- swipl --traditional --quiet -l parse_entry.pl -g "consult('$SITE_PATH/site.pl'), generate_entry('$file')." |
- # Smarten punctuation.
- smartypants \
- > "$NEW_PATH"
- done
+find "$SOURCE_PATH" -type f -name "*.md" -print0 |
+ sed "s|$SOURCE_PATH/||g" |
+ sed "s|\.md||g" |
+ xargs --null --max-procs 99 -I % sh -c "echo \"%\" &&
+ swipl --traditional --quiet -l parse_entry.pl -g \"consult('$SITE_PATH/site.pl'), generate_entry('$SOURCE_PATH/%.md').\" |
+ smartypants \
+ > \"$OUTPUT_PATH/%.html\""
# Generate the RSS feed.
mkdir -p "$OUTPUT_PATH/feeds"
# Grep the date of each article.
-ARTICLES=$(grep --recursive --include=\*.md "^Date: " "$SOURCE_PATH" |
+ARTICLES=$(grep --recursive --include "*.md" "^Date: " "$SOURCE_PATH" |
# Sort articles by date (skipping the first field).
sort +1 |
# 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 --fields 1 --delimiter : |
# Convert paths so we operate on the generated HTML, not the unformatted Markdown.
sed "s|^$SOURCE_PATH|$OUTPUT_PATH|" |
sed 's|.md$|.html|' |
# Glue the file names together to be passed to Prolog.
- paste --serial --delimiters=',' - |
+ paste --serial --delimiters ',' - |
sed "s|,|','|g")
BUILD_DATE=$(date +"%Y-%m-%d %T")
# Parse the articles and generate the RSS.
diff --git a/unsqueeze.sh b/unsqueeze.sh
@@ -1,37 +1,27 @@
#!/usr/bin/env sh
-SITE_PATH=$1
+export SITE_PATH=$1
-OUTPUT_PATH="$SITE_PATH/output"
-SOURCE_PATH="$SITE_PATH/source"
+export OUTPUT_PATH="$SITE_PATH/output"
+export SOURCE_PATH="$SITE_PATH/source"
-# Copy everything that's not Markdown or HTML.
+# Copy everything that's not 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" "$OUTPUT_PATH/" "$SOURCE_PATH/"
+rsync --archive --delete --verbose \
+ --exclude "*.html" --exclude "feeds" \
+ "$OUTPUT_PATH/" "$SOURCE_PATH/"
-# Delete any Markdown files for which the output was removed.
-find "$SOURCE_PATH" -type f -name "*.md" |
- while read -r file; do
- OLD_PATH=$(echo "$file" |
- sed "s|^$SOURCE_PATH|$OUTPUT_PATH|" |
- sed 's|.md$|.html|')
- [ ! -f "$OLD_PATH" ] && rm "$file"
- done
-
-# Parse and create all the markdown files.
-find "$OUTPUT_PATH" -type f -name "*.html" |
- while read -r file; do
- NEW_PATH=$(echo "$file" |
- 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.
- sed "s| | |g" |
- sed "s|‘|'|g" |
- sed "s|’|'|g" |
- sed "s|“|\"|g" |
- sed "s|”|\"|g" \
- > "$NEW_PATH"
- done
+# Parse and create all the Markdown files.
+find "$OUTPUT_PATH" -type f -name "*.html" -print0 |
+ sed "s|$OUTPUT_PATH/||g" |
+ sed "s|\.html||g" |
+ xargs --null --max-procs 99 -I % sh -c "echo \"%\" &&
+ swipl --traditional --quiet -l parse_entry.pl -g \"consult('$SITE_PATH/site.pl'), parse_entry('$OUTPUT_PATH/%.html').\" \
+ > \"$SOURCE_PATH/%.md\""
+# 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" {} +