Fix query block pattern to handle both formats with/without query-table line; add batch processing script
This commit is contained in:
parent
c6bed839e8
commit
e8527ded34
2 changed files with 120 additions and 9 deletions
112
batch_cleanup_journals.sh
Executable file
112
batch_cleanup_journals.sh
Executable file
|
|
@ -0,0 +1,112 @@
|
||||||
|
#!/bin/bash
|
||||||
|
# Batch cleanup script for Logseq journal Task List query blocks
|
||||||
|
# Automatically excludes the most recent journal file to avoid disrupting active work
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
CLEANUP_SCRIPT="$SCRIPT_DIR/clean_task_lists.py"
|
||||||
|
|
||||||
|
# Default options
|
||||||
|
DRY_RUN=false
|
||||||
|
INCLUDE_MOST_RECENT=false
|
||||||
|
|
||||||
|
# Parse arguments
|
||||||
|
while [[ $# -gt 0 ]]; do
|
||||||
|
case $1 in
|
||||||
|
--dry-run)
|
||||||
|
DRY_RUN=true
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
--include-most-recent)
|
||||||
|
INCLUDE_MOST_RECENT=true
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
-h|--help)
|
||||||
|
echo "Usage: $0 [OPTIONS] <journals_directory>"
|
||||||
|
echo ""
|
||||||
|
echo "Options:"
|
||||||
|
echo " --dry-run Show what would be changed without modifying files"
|
||||||
|
echo " --include-most-recent Include the most recent file (normally excluded)"
|
||||||
|
echo " -h, --help Show this help message"
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
if [[ -z "$JOURNALS_DIR" ]]; then
|
||||||
|
JOURNALS_DIR="$1"
|
||||||
|
fi
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
# Check if journals directory was provided
|
||||||
|
if [[ -z "$JOURNALS_DIR" ]]; then
|
||||||
|
echo "Error: Please provide the journals directory path"
|
||||||
|
echo "Usage: $0 [--dry-run] <journals_directory>"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check if cleanup script exists
|
||||||
|
if [[ ! -f "$CLEANUP_SCRIPT" ]]; then
|
||||||
|
echo "Error: Cleanup script not found at $CLEANUP_SCRIPT"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Find all markdown files, sorted by name (which should be chronological for date-named files)
|
||||||
|
ALL_FILES=$(find "$JOURNALS_DIR" -name '*.md' -type f | sort)
|
||||||
|
|
||||||
|
if [[ -z "$ALL_FILES" ]]; then
|
||||||
|
echo "No markdown files found in $JOURNALS_DIR"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Count total files
|
||||||
|
TOTAL_FILES=$(echo "$ALL_FILES" | wc -l)
|
||||||
|
echo "Found $TOTAL_FILES journal files"
|
||||||
|
|
||||||
|
# Determine which file to exclude (most recent - last in sorted list)
|
||||||
|
MOST_RECENT_FILE=$(echo "$ALL_FILES" | tail -n 1)
|
||||||
|
echo "Most recent file: $(basename "$MOST_RECENT_FILE")"
|
||||||
|
|
||||||
|
if [[ "$INCLUDE_MOST_RECENT" == "true" ]]; then
|
||||||
|
echo "Including most recent file (--include-most-recent flag set)"
|
||||||
|
FILES_TO_PROCESS="$ALL_FILES"
|
||||||
|
else
|
||||||
|
echo "Excluding most recent file from processing"
|
||||||
|
# Remove the last line (most recent)
|
||||||
|
FILES_TO_PROCESS=$(echo "$ALL_FILES" | head -n -1)
|
||||||
|
PROCESSED_COUNT=$(echo "$FILES_TO_PROCESS" | wc -l)
|
||||||
|
echo "Processing $PROCESSED_COUNT files (excluding most recent)"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Build command
|
||||||
|
CMD="python3 $CLEANUP_SCRIPT"
|
||||||
|
if [[ "$DRY_RUN" == "true" ]]; then
|
||||||
|
CMD="$CMD --dry-run"
|
||||||
|
fi
|
||||||
|
CMD="$CMD -r"
|
||||||
|
|
||||||
|
# Process files
|
||||||
|
echo ""
|
||||||
|
echo "========================================="
|
||||||
|
echo "Starting cleanup..."
|
||||||
|
echo "========================================="
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
# Process each file individually to get per-file output
|
||||||
|
echo "$FILES_TO_PROCESS" | while read -r file; do
|
||||||
|
if [[ -n "$file" ]]; then
|
||||||
|
python3 "$CLEANUP_SCRIPT" "$file" $( [[ "$DRY_RUN" == "true" ]] && echo "--dry-run" )
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
# Summary
|
||||||
|
echo ""
|
||||||
|
echo "========================================="
|
||||||
|
echo "Cleanup complete!"
|
||||||
|
echo "========================================="
|
||||||
|
if [[ "$DRY_RUN" == "true" ]]; then
|
||||||
|
echo "This was a DRY RUN - no files were modified"
|
||||||
|
fi
|
||||||
|
echo "Excluded most recent file: $(basename "$MOST_RECENT_FILE")"
|
||||||
17
clean_task_lists.py
Normal file → Executable file
17
clean_task_lists.py
Normal file → Executable file
|
|
@ -27,19 +27,18 @@ def remove_task_list_queries(content: str) -> Tuple[str, int]:
|
||||||
Returns:
|
Returns:
|
||||||
Tuple of (cleaned content, number of blocks removed)
|
Tuple of (cleaned content, number of blocks removed)
|
||||||
"""
|
"""
|
||||||
# Pattern to match query-table lines and #+BEGIN_QUERY ... #+END_QUERY blocks
|
# Pattern to match Task List query blocks. Handles two formats:
|
||||||
# that contain Task List or ToDo List. Handles:
|
# 1. With query-table:: line: "- query-table:: true" + "collapsed:: true" + "#+BEGIN_QUERY"
|
||||||
# - query-table:: true/false
|
# 2. Without query-table:: line: "- collapsed:: true" + "#+BEGIN_QUERY"
|
||||||
# - Optional collapsed::, query-sort-* lines
|
# Both formats end with "#+END_QUERY" and contain :title "✅ Task List" or :title "✅ ToDo List"
|
||||||
# - Multi-line or single-line #+BEGIN_QUERY ... #+END_QUERY
|
pattern = r'(?:- query-table::\s*(?:true|false)\s*\n\s+collapsed::\s*true\s*\n\s*#\+BEGIN_QUERY[\s\S]*?:title\s*"[✅✓]?\s*(?:Task|ToDo)\s*List"[\s\S]*?#\+END_QUERY|- collapsed::\s*true\s*\n\s*#\+BEGIN_QUERY[\s\S]*?:title\s*"[✅✓]?\s*(?:Task|ToDo)\s*List"[\s\S]*?#\+END_QUERY)'
|
||||||
pattern = r'query-table::\s*(?:true|false).*?#\+BEGIN_QUERY.*?:title\s*"[✅✓]?\s*(?:Task|ToDo)\s*List".*?#\+END_QUERY'
|
|
||||||
|
|
||||||
# Find all matches first to count them
|
# Find all matches first to count them
|
||||||
matches = re.findall(pattern, content, re.DOTALL)
|
matches = re.findall(pattern, content)
|
||||||
count = len(matches)
|
count = len(matches)
|
||||||
|
|
||||||
# Remove all Task List query blocks
|
# Remove all Task List query blocks
|
||||||
cleaned = re.sub(pattern, '', content, flags=re.DOTALL)
|
cleaned = re.sub(pattern, '', content)
|
||||||
|
|
||||||
return cleaned, count
|
return cleaned, count
|
||||||
|
|
||||||
|
|
@ -149,4 +148,4 @@ def main():
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue