From c9a4ce0de8c0a79a3986a992f8d31e6425917710 Mon Sep 17 00:00:00 2001 From: Nicolas Bossard Date: Mon, 7 Apr 2025 22:15:28 +0200 Subject: [PATCH 1/3] fix: support of quotes in tasks --- README.md | 4 +++- mtt_taskwarrior_to_md.sh | 12 +++++++----- tests/Makefile | 3 +++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 5a4d6ef..4f8a8c0 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,9 @@ Create a file named "on-modify.obsidian-sync" in folder "~/.task/hooks" with con #!/bin/bash read -r OLD read -r NEW -~/folder-you-cloned/obsidian-taskwarrior-sync/mtt_taskwarrior_to_md.sh --task "$NEW" +# escape double quotes +NEW_ESCAPED=$(echo "$NEW" | sed 's/"/\\"/g') +~/folder-you-cloned/obsidian-taskwarrior-sync/mtt_md_import.sh --task $NEW_ESCAPED echo "$NEW" ``` diff --git a/mtt_taskwarrior_to_md.sh b/mtt_taskwarrior_to_md.sh index 7d44d55..583de51 100755 --- a/mtt_taskwarrior_to_md.sh +++ b/mtt_taskwarrior_to_md.sh @@ -36,7 +36,7 @@ show_help() { echo " --debug Display debug information including sed commands" echo echo "Sample call:" - echo "mtt_taskwarrior_to_md --task \"{\"id\":0,\"description\":\"feed the cat\",\"end\":\"20250328T213759Z\",\"entry\"::\"20250328T102249Z\",\"modified\":\"20250328T213759Z\",\"project\":\"paymetrics\",\"status\":\"completed\",\"uuid\":\"eb48e204-e8be-416b-857d-8154edbbd7ad\",\"annotations\":[{\"entry\":\"20250328T213742Z\",\"description\":\"Source: \/Users\/nbossard\/PilotageDistri\/business-server\/documentation\/Agenda\/2025-03-28.md\"}],\"tags\":[\"Nicolas\"],\"urgency\":4.4}\"" + echo "mtt_md_import --task \"{\\\"id\":0,\\\"description\\\":\\\"feed the cat\\\",\\\"end\\\":\\\"20250328T213759Z\\\",\\\"entry\\\"::\\\"20250328T102249Z\\\",\\\"modified\\\":\\\"20250328T213759Z\\\",\\\"project\\\":\\\"paymetrics\\\",\\\"status\\\":\\\"completed\\\",\\\"uuid\\\":\\\"eb48e204-e8be-416b-857d-8154edbbd7ad\\\",\\\"annotations\\\":[{\\\"entry\\\":\\\"20250328T213742Z\\\",\\\"description\\\":\\\"Source: \\\/Users\\\/nbossard\\\/PilotageDistri\\\/business-server\\\/documentation\\\/Agenda\\\/2025-03-28.md\\\"}],\\\"tags\\\":[\\\"Nicolas\\\"],\\\"urgency\\\":4.4}\"" exit 0 } @@ -66,7 +66,7 @@ convert_priority() { # Parse command line arguments -task_json="" +param_task_json="" debug=false while [[ "$#" -gt 0 ]]; do case $1 in @@ -75,7 +75,7 @@ while [[ "$#" -gt 0 ]]; do --task) shift if [[ -n "$1" ]]; then - task_json="$1" + param_task_json="$1" else echo "mtt - Error: --task requires JSON data" show_help @@ -86,12 +86,14 @@ while [[ "$#" -gt 0 ]]; do shift done -if [ -z "$task_json" ]; then +if [ -z "$param_task_json" ]; then echo "mtt - Error: --task parameter is required" show_help exit $EXIT_MISSING_ARGS fi +# unescape double quotes +task_json=$(echo "${param_task_json}" | sed 's/\\"/"/g') echo "mtt - task to be imported is : $task_json" echo "mtt - removing escaping" task_json=$(echo "$task_json" | sed 's/\\"/\"/g') @@ -166,7 +168,7 @@ updated_task_line+="$formatted_tags" escaped_task_line=$(printf '%s\n' "$updated_task_line" | sed 's/[|&]/\\&/g') # Use a different delimiter for sed (| instead of /) to avoid issues with slashes -sed_command="sed -i.bak -E 's|^- \[ \].*\[id:: $uuid\].*$|${escaped_task_line}|' \"$source_file\"" +sed_command="sed -i.bak -E \"s|^- \[ \].*\[id:: $uuid\].*$|${escaped_task_line}|\" \"$source_file\"" if [ "$debug" = true ]; then echo "mtt - Debug: Executing sed command:" echo "mtt - $sed_command" diff --git a/tests/Makefile b/tests/Makefile index 039ccd3..e8c7944 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -7,6 +7,7 @@ test: $(MAKE) -C test_taskwarrior_to_md_3_with_dependency $@ $(MAKE) -C test_taskwarrior_to_md_4_deleted $@ $(MAKE) -C test_taskwarrior_to_md_5_with_priority $@ + $(MAKE) -C test_taskwarrior_to_md_6_desc_with_quote $@ $(MAKE) -C test_add_uuids_1 $@ $(MAKE) -C test_add_uuids_2_multiple_tasks $@ $(MAKE) -C test_add_uuids_3_multiple_files $@ @@ -28,3 +29,5 @@ clean: $(MAKE) -C test_taskwarrior_to_md_3_with_dependency $@ $(MAKE) -C test_taskwarrior_to_md_4_deleted $@ $(MAKE) -C test_taskwarrior_to_md_5_with_priority $@ + $(MAKE) -C test_taskwarrior_to_md_6_desc_with_quote $@ + From 11cd404193f04fb0fa5cf8caf86b57128b375c20 Mon Sep 17 00:00:00 2001 From: Nicolas Bossard Date: Tue, 8 Apr 2025 11:12:14 +0200 Subject: [PATCH 2/3] test: adding test5 of quotes --- tests/test5_import_desc_with_quote/Makefile | 20 +++++++++++++++++++ tests/test5_import_desc_with_quote/README.md | 5 +++++ .../existing_markdown_file.md | 5 +++++ .../expected_markdown_file.md | 5 +++++ .../task_to_be_imported.json | 1 + 5 files changed, 36 insertions(+) create mode 100644 tests/test5_import_desc_with_quote/Makefile create mode 100644 tests/test5_import_desc_with_quote/README.md create mode 100644 tests/test5_import_desc_with_quote/existing_markdown_file.md create mode 100644 tests/test5_import_desc_with_quote/expected_markdown_file.md create mode 100644 tests/test5_import_desc_with_quote/task_to_be_imported.json diff --git a/tests/test5_import_desc_with_quote/Makefile b/tests/test5_import_desc_with_quote/Makefile new file mode 100644 index 0000000..9457896 --- /dev/null +++ b/tests/test5_import_desc_with_quote/Makefile @@ -0,0 +1,20 @@ +modified_markdown_file.md: existing_markdown_file.md \ + Makefile \ + task_to_be_imported.json + cp ./existing_markdown_file.md modified_markdown_file.md + ../../mtt_md_import.sh --task '$(shell cat task_to_be_imported.json)' + +clean: + rm -f modified_markdown_file.md + +test: modified_markdown_file.md + $(call .cecho,"Comparing files with cmp and diff") + @if cmp -s expected_markdown_file.md modified_markdown_file.md; then \ + echo "✅ TEST PASSED : Files are identical"; \ + else \ + echo "❌ Files are different:"; \ + DIFF_OUTPUT=$$(diff -u expected_markdown_file.md modified_markdown_file.md); \ + echo "$$DIFF_OUTPUT"; \ + exit 1; \ + fi + diff --git a/tests/test5_import_desc_with_quote/README.md b/tests/test5_import_desc_with_quote/README.md new file mode 100644 index 0000000..f602c23 --- /dev/null +++ b/tests/test5_import_desc_with_quote/README.md @@ -0,0 +1,5 @@ +# README + +This is test of a task modification in taskwarrior being synced back to original file. + +**But the description contains a quote, which should be supported.** diff --git a/tests/test5_import_desc_with_quote/existing_markdown_file.md b/tests/test5_import_desc_with_quote/existing_markdown_file.md new file mode 100644 index 0000000..3350e4e --- /dev/null +++ b/tests/test5_import_desc_with_quote/existing_markdown_file.md @@ -0,0 +1,5 @@ +# bla bla + +Lorem ipsum dolor sit amet, consectetur adipisicing elit. + +- [ ] feed the cat l'obèse [id:: 16a1d4bb-fec6-416b-be70-6df438a6f44e] diff --git a/tests/test5_import_desc_with_quote/expected_markdown_file.md b/tests/test5_import_desc_with_quote/expected_markdown_file.md new file mode 100644 index 0000000..0d9a8a6 --- /dev/null +++ b/tests/test5_import_desc_with_quote/expected_markdown_file.md @@ -0,0 +1,5 @@ +# bla bla + +Lorem ipsum dolor sit amet, consectetur adipisicing elit. + +- [x] feed the cat l 'obèse [id:: 16a1d4bb-fec6-416b-be70-6df438a6f44e] diff --git a/tests/test5_import_desc_with_quote/task_to_be_imported.json b/tests/test5_import_desc_with_quote/task_to_be_imported.json new file mode 100644 index 0000000..e1fadf4 --- /dev/null +++ b/tests/test5_import_desc_with_quote/task_to_be_imported.json @@ -0,0 +1 @@ +{"description":"feed the cat l'obèse","entry":"20250406T090430Z","modified":"20250406T091641Z","status":"deleted","uuid":"16a1d4bb-fec6-416b-be70-6df438a6f44e","annotations":[{"entry":"20250406T090430Z","description":"Source: ./modified_markdown_file.md"}]} From c0510327c4fdac705e1ee07c8bcfbeeade15fd87 Mon Sep 17 00:00:00 2001 From: Nicolas Bossard Date: Wed, 9 Apr 2025 13:21:18 +0200 Subject: [PATCH 3/3] feat: playing with file pattern --- mtt_md_add_uuids.sh | 6 +++--- mtt_sync.sh | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mtt_md_add_uuids.sh b/mtt_md_add_uuids.sh index b99eb78..f75e86d 100755 --- a/mtt_md_add_uuids.sh +++ b/mtt_md_add_uuids.sh @@ -88,8 +88,8 @@ done # First pass: build mapping of short IDs to UUIDs echo "First pass: Building mapping of short IDs to UUIDs..." echo "Searching for short IDs with pattern: \\[id:: [a-z0-9]{6}\\]" -debug_echo "Running: rg --no-heading --line-number --with-filename \"\\[id:: [a-z0-9]{6}\\]\" $file_pattern" -rg --no-heading --line-number --with-filename "\\[id:: [a-z0-9]{6}\\]" $file_pattern | while IFS=: read -r file line_number line; do +debug_echo "Running: rg --no-heading --line-number --with-filename \"\\[id:: [a-z0-9]{6}\\]\" --glob $file_pattern" +rg --no-heading --line-number --with-filename "\\[id:: [a-z0-9]{6}\\]" --glob $file_pattern | while IFS=: read -r file line_number line; do echo "Found line with short ID: $line" # Extract the short ID using regex if [[ $line =~ \[id::\ ([a-z0-9]{6})\] ]]; then @@ -128,7 +128,7 @@ fi rm -f /tmp/id_mappings.tmp # Fourth pass: Add UUIDs to tasks without any ID -rg --no-heading --line-number --with-filename "^- \\[ \\] " $file_pattern | while IFS=: read -r file line_number line; do +rg --no-heading --line-number --with-filename "^- \\[ \\] " --glob $file_pattern | while IFS=: read -r file line_number line; do echo "......................................" echo "scanning file $file" echo "scanning line $line" diff --git a/mtt_sync.sh b/mtt_sync.sh index 3338672..9aa817a 100755 --- a/mtt_sync.sh +++ b/mtt_sync.sh @@ -67,7 +67,7 @@ fi # Build command arguments for mtt_md_add_uuids.sh (only needs mask) uuid_args=() -[ -n "$file_pattern" ] && uuid_args+=(--mask "$file_pattern") +[ -n "$file_pattern" ] && uuid_args+=(--mask "\"$file_pattern\"") # Build command arguments for mtt_md_to_taskwarrior.sh (needs both mask and project) export_args=()