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
|
#!/usr/bin/env python3
|
||||||
"""
|
"""Logseq Task List Query Block Remover"""
|
||||||
Logseq Task List Query Block Remover
|
|
||||||
"""
|
|
||||||
|
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
@ -10,14 +8,11 @@ from typing import Tuple
|
||||||
|
|
||||||
|
|
||||||
def remove_task_list_queries(content: str) -> Tuple[str, int]:
|
def remove_task_list_queries(content: str) -> Tuple[str, int]:
|
||||||
"""
|
"""Remove all Task List query blocks from Logseq content."""
|
||||||
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
|
||||||
Simple approach: match from #+BEGIN_QUERY to #+END_QUERY if it contains Task/ToDo List title.
|
# Handle both proper (# prefix) and malformed (no #) BEGIN_QUERY markers
|
||||||
This catches all formats regardless of prefix lines.
|
# Make END_QUERY optional to handle truncated files
|
||||||
"""
|
pattern = r'#?\+BEGIN_QUERY[\s\S]*?:title\s+"[^"]*(?:Task|ToDo)[^"]*List[^"]*"' r'[\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)
|
matches = re.findall(pattern, content)
|
||||||
count = len(matches)
|
count = len(matches)
|
||||||
|
|
@ -45,7 +40,7 @@ def process_file(file_path: Path, dry_run: bool = False) -> bool:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
file_path.write_text(cleaned, encoding='utf-8')
|
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
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f" ERROR writing {file_path}: {e}")
|
print(f" ERROR writing {file_path}: {e}")
|
||||||
|
|
@ -84,12 +79,12 @@ def main():
|
||||||
if dry_run:
|
if dry_run:
|
||||||
print("DRY RUN\n")
|
print("DRY RUN\n")
|
||||||
|
|
||||||
modified_count = 0
|
modified = 0
|
||||||
for file_path in sorted(files):
|
for f in sorted(files):
|
||||||
if process_file(file_path, dry_run):
|
if process_file(f, dry_run):
|
||||||
modified_count += 1
|
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__':
|
if __name__ == '__main__':
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue