All checks were successful
Auto Changelog & Release / release (push) Successful in 14s
- The `|| true` ensures the script continues even if the increment operation fails. This prevents the script from halting if `CONVERTED` is unset.
100 lines
3.3 KiB
Bash
Executable File
100 lines
3.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#=== HELP START ===
|
|
# convert-md-tree.sh – Recursive Markdown → HTML conversion (Pandoc)
|
|
#
|
|
# Usage:
|
|
# ./convert-md-tree.sh [options] <source_dir> <dest_dir>
|
|
#
|
|
# Options:
|
|
# -n, --dry-run Show what would be done without converting
|
|
# -p, --pandoc-args <string> Extra arguments passed to pandoc (e.g. "-t html5")
|
|
# -h, --help Show this help message
|
|
#
|
|
# Arguments:
|
|
# <source_dir> Root directory containing .md files
|
|
# <dest_dir> Destination directory for the HTML output
|
|
#
|
|
# Behaviour:
|
|
# • Preserves the directory structure in the destination.
|
|
# • Creates required sub-directories automatically.
|
|
# • Emits clear errors for missing programs or invalid parameters.
|
|
#=== HELP END ===
|
|
set -euo pipefail
|
|
|
|
# ───────────────────────────────────────────────
|
|
# Defaults
|
|
DRY_RUN=false
|
|
PANDOC_ARGS=""
|
|
# ───────────────────────────────────────────────
|
|
|
|
# 0 Help function -------------------------------------------------------------
|
|
show_help() {
|
|
sed -n '/^#=== HELP START ===/,/^#=== HELP END ===/ {
|
|
/^#=== HELP START ===/d
|
|
/^#=== HELP END ===/d
|
|
s/^#//
|
|
p
|
|
}' "$0"
|
|
}
|
|
|
|
# 1 Parse options -------------------------------------------------------------
|
|
POSITIONAL=()
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
-h|--help) show_help; exit 0 ;;
|
|
-n|--dry-run) DRY_RUN=true; shift ;;
|
|
-p|--pandoc-args)
|
|
[[ $# -lt 2 ]] && { echo "❌ Option $1 requires an argument"; exit 1; }
|
|
PANDOC_ARGS="$2"; shift 2 ;;
|
|
--) shift; break ;;
|
|
-*)
|
|
echo "❌ Unknown option: $1"; exit 1 ;;
|
|
*) POSITIONAL+=("$1"); shift ;;
|
|
esac
|
|
done
|
|
set -- "${POSITIONAL[@]}"
|
|
|
|
[[ $# -lt 2 ]] && { echo "❌ Missing <source_dir> and/or <dest_dir>"; echo; show_help; exit 1; }
|
|
|
|
SRC="$1"
|
|
DEST="$2"
|
|
|
|
# 2 Validate arguments --------------------------------------------------------
|
|
[[ ! -d "$SRC" ]] && { echo "❌ Source directory does not exist: $SRC"; exit 1; }
|
|
|
|
mkdir -p "$DEST" || { echo "❌ Cannot create/access dest dir: $DEST"; exit 1; }
|
|
|
|
# Remove trailing slash for path manipulation
|
|
SRC="${SRC%/}"
|
|
|
|
# 3 Check required tools ------------------------------------------------------
|
|
need() { command -v "$1" >/dev/null || { echo "❌ $1 is missing"; exit 1; }; }
|
|
need pandoc; need find; need mkdir
|
|
|
|
# 4 Process files -------------------------------------------------------------
|
|
echo "🔍 Scanning for .md files in $SRC …"
|
|
COUNT_TOTAL=$(find "$SRC" -type f -name '*.md' | wc -l | tr -d ' ')
|
|
[[ "$COUNT_TOTAL" -eq 0 ]] && { echo "ℹ️ No Markdown files found – nothing to do."; exit 0; }
|
|
echo "📄 Found $COUNT_TOTAL Markdown file(s)."
|
|
|
|
CONVERTED=0
|
|
while IFS= read -r -d '' FILE; do
|
|
REL_PATH="${FILE#$SRC/}"
|
|
OUT_FILE="$DEST/${REL_PATH%.md}.html"
|
|
mkdir -p "$(dirname "$OUT_FILE")"
|
|
|
|
if $DRY_RUN; then
|
|
echo "🧪 Would convert: $FILE -> $OUT_FILE"
|
|
else
|
|
pandoc $PANDOC_ARGS "$FILE" -s -o "$OUT_FILE"
|
|
echo "✅ Converted: $FILE -> $OUT_FILE"
|
|
fi
|
|
((CONVERTED++)) || true
|
|
done < <(find "$SRC" -type f -name '*.md' -print0)
|
|
|
|
if $DRY_RUN; then
|
|
echo "🧪 Dry-run complete – $CONVERTED file(s) would be processed."
|
|
else
|
|
echo "🏁 Finished – $CONVERTED file(s) converted."
|
|
fi
|