@@ -76,42 +76,37 @@ trap cleanup EXIT
7676extract_blocks () {
7777 local current_file=" "
7878 local in_block=false
79- local block_content =" "
79+ local temp_file =" "
8080
8181 echo " $MARKDOWN " | while IFS= read -r line; do
8282 if [[ " $line " =~ ^[[:space:]]* \`\`\` ([a-zA-Z0-9\+\- ]+)[[:space:]]+ (.+) ]]; then
8383 current_file=" ${BASH_REMATCH[2]} "
8484 in_block=true
85- block_content=" "
86- continue
87- fi
88-
89- if [[ " $line " =~ ^[[:space:]]* \`\`\` ([a-zA-Z0-9\+\- ]+)[[:space:]]* $ ]]; then
90- in_block=false
91- current_file=" "
85+ temp_file=" $TEMP_DIR /$current_file "
86+ mkdir -p " $( dirname " $temp_file " ) "
87+ : > " $temp_file " # Create empty file
88+ if [ " $VERBOSE " = true ]; then
89+ echo " Starting code block for file: $current_file "
90+ fi
9291 continue
9392 fi
9493
9594 if [[ " $line " =~ ^[[:space:]]* \`\`\` [[:space:]]* $ ]]; then
9695 if [ -n " $current_file " ] && [ " $in_block " = true ]; then
9796 echo " $current_file " >> " $FILES_FILE "
98- echo " $block_content " > " $TEMP_DIR /$current_file "
9997 current_file_formatted=$( echo " $current_file " | sed ' s/^\.\///' )
10098 if [ " $VERBOSE " = true ]; then
101- echo " Found code block for file: $current_file_formatted "
99+ echo " Completed code block for file: $current_file_formatted "
102100 fi
103101 fi
104102 in_block=false
105103 current_file=" "
104+ temp_file=" "
106105 continue
107106 fi
108107
109- if [ " $in_block " = true ] && [ -n " $current_file " ]; then
110- if [ -z " $block_content " ]; then
111- block_content=" $line "
112- else
113- block_content=" $block_content " $' \n ' " $line "
114- fi
108+ if [ " $in_block " = true ] && [ -n " $current_file " ] && [ -n " $temp_file " ]; then
109+ echo " $line " >> " $temp_file "
115110 fi
116111 done
117112}
@@ -141,6 +136,9 @@ apply_block() {
141136 return
142137 fi
143138
139+ # Normalize file path
140+ file=$( realpath -m " $file " 2> /dev/null || echo " $file " )
141+
144142 if [ ! -f " $file " ]; then
145143 if [ " $CREATE_MISSING " = true ]; then
146144 if [ " $DRY_RUN " = false ]; then
@@ -200,6 +198,7 @@ apply_block() {
200198 if [ " $BACKUP " = true ]; then
201199 local backup_file=" $BACKUP_DIR /$( basename " $file " ) .$( date +%Y%m%d%H%M%S) .bak"
202200 if [ " $DRY_RUN " = false ]; then
201+ mkdir -p " $( dirname " $backup_file " ) "
203202 cp " $file " " $backup_file "
204203 if [ " $VERBOSE " = true ]; then
205204 echo " Created backup: $backup_file "
0 commit comments