Soleris_Roman/regrouper_md.sh

57 lines
No EOL
1.9 KiB
Bash

#!/bin/bash
# === CONFIGURATION ===
OUTPUT="compilation.md"
# === ARGUMENTS ===
if [[ $# -eq 0 ]]; then
TARGET_DIR="."
echo "Aucun dossier → dossier courant : $PWD"
elif [[ $# -eq 1 ]]; then
TARGET_DIR="$1"
if [[ ! -d "$TARGET_DIR" ]]; then
echo "Erreur : dossier '$TARGET_DIR' introuvable." >&2
exit 1
fi
else
echo "Usage: $0 [dossier]" >&2
exit 1
fi
# Chemin absolu
TARGET_DIR="$(cd "$TARGET_DIR" && pwd)"
OUTPUT_PATH="$TARGET_DIR/$OUTPUT"
# Vider le fichier
> "$OUTPUT_PATH"
echo "Regroupement dans : $TARGET_DIR"
echo "Sortie → $OUTPUT_PATH"
echo "--------------------------------------------------"
# === BOUCLE SUR SOUS-DOSSIERS (tri alphabétique) ===
while IFS= read -r -d '' subdir; do
rel_subdir="${subdir#$TARGET_DIR/}"
chapter_title=$(basename "$subdir")
chapter_title=$(echo "$chapter_title" | sed 's/[-_]/ /g')
# Majuscule première lettre (macOS compatible)
chapter_title="$(echo "$chapter_title" | awk '{print toupper(substr($0,1,1)) substr($0,2)}')"
echo "Chapitre : $chapter_title ($rel_subdir)"
printf "\n# %s\n\n" "$chapter_title" >> "$OUTPUT_PATH"
# === BOUCLE SUR FICHIERS .MD DU SOUS-DOSSIER ===
while IFS= read -r -d '' file; do
[[ "$file" == "$OUTPUT_PATH" ]] && continue
rel_file="${file#$subdir/}"
echo " → Traitement : $rel_file"
# Ajout du contenu entier du fichier (sans titre individuel)
cat "$file" >> "$OUTPUT_PATH"
printf "\n" >> "$OUTPUT_PATH" # Séparateur simple entre fichiers
done < <(find "$subdir" -maxdepth 1 -type f -name "*.md" ! -name "$OUTPUT" -not -empty -print0 | sort -z)
printf "\n---\n\n" >> "$OUTPUT_PATH" # Séparateur entre chapitres (optionnel, ligne horizontale)
done < <(find "$TARGET_DIR" -mindepth 1 -maxdepth 1 -type d -print0 | sort -z)
echo "--------------------------------------------------"
echo "TERMINE : $OUTPUT_PATH"