From e4202d920a03ef663174e793fcd1cfc838bbd6dc Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Tue, 3 Nov 2026 20:46:07 -0800 Subject: [PATCH 1/4] Fix: Catch query blocks with no prefix (just whitespace before #+BEGIN_QUERY) --- clean_task_lists.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/clean_task_lists.py b/clean_task_lists.py index eb243cb..8797bcd 100755 --- a/clean_task_lists.py +++ b/clean_task_lists.py @@ -16,9 +16,11 @@ def remove_task_list_queries(content: str) -> Tuple[str, int]: 1. - query-table:: + collapsed:: (no dash) + #+BEGIN_QUERY 2. collapsed:: (no dash) + - query-table:: + #+BEGIN_QUERY 3. - collapsed:: + #+BEGIN_QUERY - 4. - #+BEGIN_QUERY (direct, no prefix) + 4. - #+BEGIN_QUERY (direct with dash) + 5. #+BEGIN_QUERY (no dash, no prefix - just whitespace) """ - pattern = r'(?:- query-table::\s*(?:true|false)\s*\n\s+collapsed::\s*true\s*\n\s*#\+BEGIN_QUERY|collapsed::\s*true\s*\n\s*- query-table::\s*(?:true|false)\s*\n\s*#\+BEGIN_QUERY|- collapsed::\s*true\s*\n\s*#\+BEGIN_QUERY|- #\+BEGIN_QUERY)[\s\S]*?:title\s*"[✅✓]?\s*(?:Task|ToDo)\s*List"[\s\S]*?#\+END_QUERY' + # Pattern handles all 5 formats above + pattern = r'(?:- query-table::\s*(?:true|false)\s*\n\s+collapsed::\s*true\s*\n\s*#\+BEGIN_QUERY|collapsed::\s*true\s*\n\s*- query-table::\s*(?:true|false)\s*\n\s*#\+BEGIN_QUERY|- collapsed::\s*true\s*\n\s*#\+BEGIN_QUERY|- #\+BEGIN_QUERY|\s*#\+BEGIN_QUERY)[\s\S]*?:title\s*"[✅✓]?\s*(?:Task|ToDo)\s*List"[\s\S]*?#\+END_QUERY' matches = re.findall(pattern, content) count = len(matches) -- 2.49.1 From 53547303a825d43118a2e62fecc1fb3523695cc1 Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Wed, 4 Nov 2026 07:41:25 -0800 Subject: [PATCH 2/4] Simplify pattern to catch all Task/ToDo List query blocks - Use single pattern matching #+BEGIN_QUERY to #+END_QUERY - Check for Task/ToDo List title within the block - Catches all formats without enumerating prefixes --- clean_task_lists.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/clean_task_lists.py b/clean_task_lists.py index 8797bcd..7355798 100755 --- a/clean_task_lists.py +++ b/clean_task_lists.py @@ -12,15 +12,12 @@ from typing import Tuple def remove_task_list_queries(content: str) -> Tuple[str, int]: """ Remove all Task List query blocks from Logseq content. - Handles multiple formats: - 1. - query-table:: + collapsed:: (no dash) + #+BEGIN_QUERY - 2. collapsed:: (no dash) + - query-table:: + #+BEGIN_QUERY - 3. - collapsed:: + #+BEGIN_QUERY - 4. - #+BEGIN_QUERY (direct with dash) - 5. #+BEGIN_QUERY (no dash, no prefix - just whitespace) + Simple approach: match from #+BEGIN_QUERY to #+END_QUERY if it contains Task/ToDo List title. + This catches all formats regardless of prefix lines. """ - # Pattern handles all 5 formats above - pattern = r'(?:- query-table::\s*(?:true|false)\s*\n\s+collapsed::\s*true\s*\n\s*#\+BEGIN_QUERY|collapsed::\s*true\s*\n\s*- query-table::\s*(?:true|false)\s*\n\s*#\+BEGIN_QUERY|- collapsed::\s*true\s*\n\s*#\+BEGIN_QUERY|- #\+BEGIN_QUERY|\s*#\+BEGIN_QUERY)[\s\S]*?:title\s*"[✅✓]?\s*(?:Task|ToDo)\s*List"[\s\S]*?#\+END_QUERY' + # 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' matches = re.findall(pattern, content) count = len(matches) -- 2.49.1 From a5a1c01553d6416c1287746bb7eaedc4140b354e Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Wed, 4 Nov 2026 14:41:02 -0800 Subject: [PATCH 3/4] 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__': -- 2.49.1 From cc7989067ca8c89a9865efb46213641dd4014a15 Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Thu, 5 Nov 2026 07:38:53 -0800 Subject: [PATCH 4/4] Fix: Use working regex patterns with str.replace for reliable query block removal - Replace pattern.sub() with str.replace() for more reliable matching - Add newline preservation to maintain proper spacing after removal - Clean up trailing whitespace on all lines - Normalize file ending with single trailing newline - Handle all three formats: query-table prefix, collapsed prefix, and standalone This version has been tested and successfully removes all Task List query blocks while preserving non-Task-List queries and maintaining proper file formatting. --- clean_task_lists.py | 70 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 62 insertions(+), 8 deletions(-) diff --git a/clean_task_lists.py b/clean_task_lists.py index 945b4cb..a05a58b 100755 --- a/clean_task_lists.py +++ b/clean_task_lists.py @@ -8,15 +8,69 @@ from typing import Tuple def remove_task_list_queries(content: str) -> Tuple[str, int]: - """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|$)' + """Remove all Task List query blocks from Logseq content. - matches = re.findall(pattern, content) - count = len(matches) - cleaned = re.sub(pattern, '', content) + Handles formats like: + - - query-table:: true/false + query-sort-by:: page + #+BEGIN_QUERY + {:title "✅ Task List" ...} + #+END_QUERY + + - - collapsed:: true/false + #+BEGIN_QUERY + {:title "✅ Task List" ...} + #+END_QUERY + + - #+BEGIN_QUERY + {:title "✅ Task List" ...} + #+END_QUERY + + Only removes blocks with "Task" or "ToDo" AND "List" in the title. + Other query blocks are preserved. + """ + count = 0 + cleaned = content + + # Pattern 1: With "query-table::" prefix + p1 = re.compile( + r'-\s+query-table::\s+(?:true|false)\s*\n' + r'(?:\s+\w+::\s+\S+\s*\n)*' + r'(?:\s*#?\+BEGIN_QUERY\s*\n)?' + r'[\s\S]*?#?\+END_QUERY\s*\n?', + re.MULTILINE + ) + + # Pattern 2: With "collapsed::" prefix (no query-table) + p2 = re.compile( + r'-\s+collapsed::\s+(?:true|false)\s*\n' + r'(?:\s+\w+::\s+\S+\s*\n)*' + r'(?:\s*#?\+BEGIN_QUERY\s*\n)?' + r'[\s\S]*?#?\+END_QUERY\s*\n?', + re.MULTILINE + ) + + # Pattern 3: No prefix, just BEGIN_QUERY + p3 = re.compile( + r'(?:^|\n)\s*#?\+BEGIN_QUERY\s*\n' + r'[\s\S]*?#?\+END_QUERY\s*\n?', + re.MULTILINE + ) + + for pattern in [p1, p2, p3]: + matches = pattern.findall(cleaned) + for match in matches: + if ':title' in match and ('Task' in match or 'ToDo' in match) and 'List' in match: + # Replace with a single newline to preserve spacing + cleaned = cleaned.replace(match, '\n') + count += 1 + + # Clean up extra blank lines (3+ newlines become 2) + cleaned = re.sub(r'\n{3,}', '\n\n', cleaned) + # Clean up any leading/trailing whitespace on lines + cleaned = '\n'.join(line.rstrip() for line in cleaned.split('\n')) + # Remove leading/trailing whitespace from entire content + cleaned = cleaned.strip() + '\n' return cleaned, count -- 2.49.1