From a5a1c01553d6416c1287746bb7eaedc4140b354e Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Wed, 4 Nov 2026 14:41:02 -0800 Subject: [PATCH] Fix: Handle all query block formats including truncated files without END_QUERY --- clean_task_lists.py | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/clean_task_lists.py b/clean_task_lists.py index 7355798..945b4cb 100755 --- a/clean_task_lists.py +++ b/clean_task_lists.py @@ -1,7 +1,5 @@ #!/usr/bin/env python3 -""" -Logseq Task List Query Block Remover -""" +"""Logseq Task List Query Block Remover""" import re import sys @@ -10,14 +8,11 @@ from typing import Tuple def remove_task_list_queries(content: str) -> Tuple[str, int]: - """ - Remove all Task List query blocks from Logseq content. - Simple approach: match from #+BEGIN_QUERY to #+END_QUERY if it contains Task/ToDo List title. - This catches all formats regardless of prefix lines. - """ - # Simple pattern: match #+BEGIN_QUERY ... #+END_QUERY blocks that contain Task/ToDo List title - # This catches all formats without needing to enumerate all possible prefixes - pattern = r'#+BEGIN_QUERY[\s\S]*?:title\s*"[✅✓]?\s*(?:Task|ToDo)\s*List"[\s\S]*?#\+END_QUERY' + """Remove all Task List query blocks from Logseq content.""" + # Match #+BEGIN_QUERY or +BEGIN_QUERY to #+END_QUERY (or EOF) blocks containing Task/ToDo List title + # Handle both proper (# prefix) and malformed (no #) BEGIN_QUERY markers + # Make END_QUERY optional to handle truncated files + pattern = r'#?\+BEGIN_QUERY[\s\S]*?:title\s+"[^"]*(?:Task|ToDo)[^"]*List[^"]*"' r'[\s\S]*?(?:#?\+END_QUERY|$)' matches = re.findall(pattern, content) count = len(matches) @@ -45,7 +40,7 @@ def process_file(file_path: Path, dry_run: bool = False) -> bool: try: file_path.write_text(cleaned, encoding='utf-8') - print(f" ✓ Removed {count} Task List block(s) from {file_path.name}") + print(f" Removed {count} Task List block(s) from {file_path.name}") return True except Exception as e: print(f" ERROR writing {file_path}: {e}") @@ -84,12 +79,12 @@ def main(): if dry_run: print("DRY RUN\n") - modified_count = 0 - for file_path in sorted(files): - if process_file(file_path, dry_run): - modified_count += 1 + modified = 0 + for f in sorted(files): + if process_file(f, dry_run): + modified += 1 - print(f"\n{'Would modify' if dry_run else 'Modified'} {modified_count} of {len(files)} file(s)") + print(f"\nWould modify {modified} of {len(files)} file(s)" if dry_run else f"\nModified {modified} of {len(files)} file(s)") if __name__ == '__main__':