77 lines
2.6 KiB
Bash
Executable File
77 lines
2.6 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Check if both SOURCE_DIR and DEST_DIR are provided as arguments
|
|
if [ "$#" -ne 2 ]; then
|
|
echo "Usage: $0 <SOURCE_DIR> <DEST_DIR>"
|
|
exit 1
|
|
fi
|
|
|
|
# Capture SOURCE_DIR and DEST_DIR from arguments
|
|
SOURCE_DIR="$1"
|
|
DEST_DIR="$2"
|
|
IMAGE_DIR="$DEST_DIR/images"
|
|
|
|
# Ensure destination directories exist
|
|
mkdir -p "$DEST_DIR"
|
|
mkdir -p "$IMAGE_DIR"
|
|
|
|
# Temporary log for copied files
|
|
LOG_FILE=/tmp/copied_files.log
|
|
|
|
# Clear the log file
|
|
> "$LOG_FILE"
|
|
|
|
# Find Markdown files with "publish: true" and process them
|
|
find -L "$SOURCE_DIR" -type f -name "*.md" -exec grep -q "publish: true" {} \; -exec grep -l "publish: true" {} + | while read -r markdown_file; do
|
|
# Extract relative path from the source directory
|
|
relative_path="${markdown_file#$SOURCE_DIR/}"
|
|
|
|
# Destination path
|
|
dest_path="$DEST_DIR/$relative_path"
|
|
|
|
# Ensure the directory exists before copying the Markdown file
|
|
dest_dir=$(dirname "$dest_path")
|
|
mkdir -p "$dest_dir"
|
|
|
|
# Check if the destination file already exists
|
|
if [ -f "$dest_path" ]; then
|
|
echo "Skipped $markdown_file as it already exists in $dest_path" >> "$LOG_FILE"
|
|
else
|
|
# Copy the Markdown file
|
|
cp -p "$markdown_file" "$dest_path"
|
|
echo "Copied $markdown_file to $dest_path" >> "$LOG_FILE"
|
|
|
|
# Extract and copy referenced images
|
|
grep -o '\!\[\[.*\]\]' "$markdown_file" | sed 's/\!\[\[\(.*\)\]\]/\1/' | while IFS= read -r image_ref; do
|
|
# Check if image_ref is a relative path
|
|
if [[ ! "$image_ref" =~ ^/ ]]; then
|
|
image_ref="$SOURCE_DIR/$image_ref"
|
|
fi
|
|
|
|
# Try to find the image recursively within SOURCE_DIR
|
|
image_path=$(find -L "$SOURCE_DIR" -type f -name "$(basename "$image_ref")" -print -quit)
|
|
|
|
# Debugging: Print image_path
|
|
echo "Checking image_path: $image_path" >> "$LOG_FILE"
|
|
|
|
if [ -f "$image_path" ]; then
|
|
# Check if the image already exists in IMAGE_DIR
|
|
if [ ! -f "$IMAGE_DIR/$(basename "$image_ref")" ]; then
|
|
cp "$image_path" "$IMAGE_DIR/"
|
|
echo "Copied image $image_ref to $IMAGE_DIR/" >> "$LOG_FILE"
|
|
else
|
|
echo "Skipped copying image $image_ref as it already exists in $IMAGE_DIR/" >> "$LOG_FILE"
|
|
fi
|
|
else
|
|
echo "Image reference $image_ref in $markdown_file does not exist." >> "$LOG_FILE"
|
|
fi
|
|
done
|
|
fi
|
|
done
|
|
|
|
# Print the log file
|
|
cat "$LOG_FILE"
|
|
|
|
# Debugging: Indicate completion
|
|
echo "Script execution completed."
|