diff options
Diffstat (limited to 'reformat_pages.sh')
| -rwxr-xr-x | reformat_pages.sh | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/reformat_pages.sh b/reformat_pages.sh new file mode 100755 index 0000000..68f52df --- /dev/null +++ b/reformat_pages.sh @@ -0,0 +1,118 @@ +#!/usr/bin/env bash +# vim: set sw=4 sts=4 et ft=sh : +# -*- mode:bash; -*- +# ------------------------------------------------------- +# Copyright (C) 2025 by Anoduck, The Anonymous Duck +# ------------------------------------------------------- +# https://anoduck.mit-license.org +# ------------------------------------------------------- +ACTION=$1 +DIRPATH=$2 + +function get_find { + if [[ -e $(which fd) ]]; then + FCMD=$(which fd) + else + FCMD=$(which fdfind) + fi +} + +get_total () { + get_find + TOTAL=$("$FCMD" -t f -e md --search-path "$DIRPATH" | wc -l) + if [[ -z "$TOTAL" ]]; then + echo "No files found" + exit 1 + fi + echo "Found $TOTAL files" +} + +modify () { + get_find + echo "Using $FCMD" + MDFIND=$("$FCMD" -t f -e md --search-path "$DIRPATH") + get_total + + c=0 + + for i in $MDFIND; do + fname=$(basename "$i") + if [[ "$fname" != "_index.md" ]]; then + echo "File is $i" + echo "Basename is $fname" + slug="${fname%.*}" + dpath="$(dirname "$i")" + backpath="$dpath/$slug.bak" + echo "Slug should be $slug" + cp "$i" "$backpath" + awk --assign uri="$slug" '1;/title:/{print "slug: " uri}' "$backpath" | tee "$i" > /dev/null + ((c++)) + echo "Fixed: $c out of $TOTAL" + fi + done +} + +reset () { + get_find + MDFIND=$("$FCMD" -t f -e md --search-path "$DIRPATH") + BAKFIND=$("$FCMD" -t f -e bak --search-path "$DIRPATH") + BTOTAL=$("$FCMD" -t f -e bak --search-path "$DIRPATH" | wc -l) + get_total + echo "Found $TOTAL backup files, and $TOTAL original files" + if [[ "$BTOTAL" -gt "$TOTAL" ]]; then + echo "There are more backup files than original files" + fi + c=0 + for j in $BAKFIND; do + echo "Backup file is $j" + fname=$(basename "$j") + if [[ "$fname" != "_index.md" ]]; then + echo "Basename is $fname" + slug="${fname%.*}" + dpath="$(dirname "$j")" + mdpath="$dpath/$slug.md" + echo "Slug should be $slug" + cp "$j" "$mdpath" + rm "$j" + ((c++)) + echo "Reset: $c out of $BTOTAL" + fi + done +} + +clean () { + get_find + BAKFIND=$("$FCMD" -t f -e bak --search-path "$DIRPATH") + BTOTAL=$("$FCMD" -t f -e bak --search-path "$DIRPATH" | wc -l) + echo "Found $BTOTAL backup files" + c=0 + for q in $BAKFIND; do + echo "Backup file is $q" + rm "$q" + ((c++)) + echo "Deleted: $c out of $BTOTAL" + done +} + +case "$ACTION" in + frontmatter) + echo "performing frontmatter modification on $DIRPATH" + modify + ;; + reset) + echo "performing reset on $DIRPATH" + reset + ;; + clean) + echo "performing backup file cleanup on $DIRPATH" + clean + ;; + full) + echo "Performing frontmatter alteration AND cleanup on $DIRPATH" + modify + clean + ;; + *) + echo "Usage: $0 [frontmatter | clean | full | reset] [path]" + ;; +esac |
