Fix: Handle all query block formats including truncated files without END_QUERY
This commit is contained in:
parent
53547303a8
commit
a5a1c01553
1 changed files with 12 additions and 17 deletions
|
|
@ -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__':
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue