From 4c2d57de12f71d293e76347eb56010b4c7add917 Mon Sep 17 00:00:00 2001 From: Galuh Sahid Date: Wed, 27 Dec 2023 22:04:30 +0100 Subject: [PATCH] Add script --- scripts/publish.sh | 20 ++++++++++---- scripts/publish_old.sh | 40 --------------------------- scripts/publish_old_works.sh | 53 ------------------------------------ 3 files changed, 15 insertions(+), 98 deletions(-) delete mode 100755 scripts/publish_old.sh delete mode 100755 scripts/publish_old_works.sh diff --git a/scripts/publish.sh b/scripts/publish.sh index 1dbc54f..4846d7f 100755 --- a/scripts/publish.sh +++ b/scripts/publish.sh @@ -1,12 +1,18 @@ #!/bin/bash +# Check if both SOURCE_DIR and DEST_DIR are provided as arguments +if [ "$#" -ne 2 ]; then + echo "Usage: $0 " + exit 1 +fi + # Debugging: Print commands as they are executed set -x -# Define source and destination directories -SOURCE_DIR=~/obsidian -DEST_DIR=/home/ac1dburn/obsidian-site/content -IMAGE_DIR=$DEST_DIR/images +# 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" @@ -26,8 +32,12 @@ find -L "$SOURCE_DIR" -type f -name "*.md" -exec grep -q "publish: true" {} \; - # 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" + # Copy the Markdown file - cp "$markdown_file" "$dest_path" + cp -p "$markdown_file" "$dest_path" echo "Copied $markdown_file to $dest_path" >> "$LOG_FILE" # Extract and copy referenced images diff --git a/scripts/publish_old.sh b/scripts/publish_old.sh deleted file mode 100755 index 9926ab3..0000000 --- a/scripts/publish_old.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/bash - -# Debugging: Print commands as they are executed -set -x - -# Define source and destination directories -SOURCE_DIR=~/obsidian -DEST_DIR=/home/ac1dburn/obsidian-site/content - -# Check if source directory exists -if [ ! -d "$SOURCE_DIR" ]; then - echo "Source directory $SOURCE_DIR does not exist!" - exit 1 -fi - -# Check if destination directory exists, if not create it -if [ ! -d "$DEST_DIR" ]; then - mkdir -p "$DEST_DIR" -fi - -# Find markdown files where publish: true exists in the YAML front matter -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/}" - - # Extract directory path - dir_path=$(dirname "$relative_path") - - # Create the corresponding directory structure in the destination directory - mkdir -p "$DEST_DIR/$dir_path" - - # Copy markdown file to the destination directory - cp "$markdown_file" "$DEST_DIR/$relative_path" - - # Debugging: Log the operations being performed - echo "Copied $markdown_file to $DEST_DIR/$relative_path" -done - -# Debugging: Indicate completion -echo "Script execution completed." diff --git a/scripts/publish_old_works.sh b/scripts/publish_old_works.sh deleted file mode 100755 index 841ac49..0000000 --- a/scripts/publish_old_works.sh +++ /dev/null @@ -1,53 +0,0 @@ -#!/bin/bash - -# Debugging: Print commands as they are executed -set -x - -# Define source and destination directories -SOURCE_DIR=~/obsidian -DEST_DIR=/home/ac1dburn/obsidian-site/content -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" - - # Copy the Markdown file - cp "$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 - image_path="$SOURCE_DIR/$image_ref" - - # Debugging: Print image_path - echo "Checking image_path: $image_path" >> "$LOG_FILE" - - if [ -f "$image_path" ]; then - cp "$image_path" "$IMAGE_DIR/" - echo "Copied image $image_ref to $IMAGE_DIR/" >> "$LOG_FILE" - else - echo "Image reference $image_ref in $markdown_file does not exist." >> "$LOG_FILE" - fi - done -done - -# Print the log file -cat "$LOG_FILE" - -# Debugging: Indicate completion -echo "Script execution completed."