-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdify.sh
executable file
·44 lines (34 loc) · 1.17 KB
/
mdify.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/bash
# Usage: ./mdify.sh <directory> <output_file> <extensions>
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <directory> <output_file> <extensions>"
echo "Example: $0 ./src output.md cpp,hpp"
exit 1
fi
DIRECTORY=$1
OUTPUT_FILE=$2
EXTENSIONS=$3
# Check if the specified directory exists
if [ ! -d "$DIRECTORY" ]; then
echo "The specified directory does not exist."
exit 1
fi
# Prepare the output file, clearing it if it already exists
> "$OUTPUT_FILE"
# Convert comma-separated extensions to find format
IFS=',' read -r -a EXTENSIONS_ARRAY <<< "$EXTENSIONS"
# Build the find command with the specified extensions
find_command="find \"$DIRECTORY\" \( -false"
for ext in "${EXTENSIONS_ARRAY[@]}"; do
find_command+=" -o -name \"*.$ext\""
done
find_command+=" \) -print0"
# Execute the find command and process the files
eval $find_command | while IFS= read -r -d $'\0' file; do
echo "**\`$file\`:**" >> "$OUTPUT_FILE"
echo '```'${file##*.} >> "$OUTPUT_FILE"
cat "$file" >> "$OUTPUT_FILE"
echo '```' >> "$OUTPUT_FILE"
echo >> "$OUTPUT_FILE"
done
echo "The contents of all specified files have been written to $OUTPUT_FILE in Markdown format."