Update pattern to catch all query block formats including direct - #+BEGIN_QUERY
This commit is contained in:
parent
e8527ded34
commit
fa6cc6cbe7
1 changed files with 14 additions and 68 deletions
|
|
@ -1,59 +1,33 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""
|
"""
|
||||||
Logseq Task List Query Block Remover
|
Logseq Task List Query Block Remover
|
||||||
|
|
||||||
This script removes the "Task List" query blocks from Logseq journal pages.
|
|
||||||
Most days have two such blocks:
|
|
||||||
1. Work Task List (under work page reference)
|
|
||||||
2. Personal Task List (non-work tasks)
|
|
||||||
|
|
||||||
The query blocks are marked with #+BEGIN_QUERY ... #+END_QUERY and contain
|
|
||||||
"title "✅ Task List"" or "title "✅ ToDo List"" in their configuration.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import List, Tuple
|
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.
|
||||||
|
Handles multiple formats:
|
||||||
Args:
|
1. - query-table:: + collapsed:: (no dash) + #+BEGIN_QUERY
|
||||||
content: The Logseq markdown content
|
2. collapsed:: (no dash) + - query-table:: + #+BEGIN_QUERY
|
||||||
|
3. - collapsed:: + #+BEGIN_QUERY
|
||||||
Returns:
|
4. - #+BEGIN_QUERY (direct, no prefix)
|
||||||
Tuple of (cleaned content, number of blocks removed)
|
|
||||||
"""
|
"""
|
||||||
# Pattern to match Task List query blocks. Handles two formats:
|
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'
|
||||||
# 1. With query-table:: line: "- query-table:: true" + "collapsed:: true" + "#+BEGIN_QUERY"
|
|
||||||
# 2. Without query-table:: line: "- collapsed:: true" + "#+BEGIN_QUERY"
|
|
||||||
# Both formats end with "#+END_QUERY" and contain :title "✅ Task List" or :title "✅ ToDo List"
|
|
||||||
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)'
|
|
||||||
|
|
||||||
# Find all matches first to count them
|
|
||||||
matches = re.findall(pattern, content)
|
matches = re.findall(pattern, content)
|
||||||
count = len(matches)
|
count = len(matches)
|
||||||
|
|
||||||
# Remove all Task List query blocks
|
|
||||||
cleaned = re.sub(pattern, '', content)
|
cleaned = re.sub(pattern, '', content)
|
||||||
|
|
||||||
return cleaned, count
|
return cleaned, count
|
||||||
|
|
||||||
|
|
||||||
def process_file(file_path: Path, dry_run: bool = False) -> bool:
|
def process_file(file_path: Path, dry_run: bool = False) -> bool:
|
||||||
"""
|
|
||||||
Process a single Logseq file.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
file_path: Path to the Logseq file
|
|
||||||
dry_run: If True, only report what would be changed
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
True if file was/would be modified, False otherwise
|
|
||||||
"""
|
|
||||||
try:
|
try:
|
||||||
content = file_path.read_text(encoding='utf-8')
|
content = file_path.read_text(encoding='utf-8')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
@ -70,7 +44,6 @@ def process_file(file_path: Path, dry_run: bool = False) -> bool:
|
||||||
print(f" WOULD remove {count} Task List block(s) from {file_path.name}")
|
print(f" WOULD remove {count} Task List block(s) from {file_path.name}")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# Write cleaned content back
|
|
||||||
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}")
|
||||||
|
|
@ -81,69 +54,42 @@ def process_file(file_path: Path, dry_run: bool = False) -> bool:
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Main entry point for the script."""
|
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(description='Remove Task List query blocks from Logseq notes')
|
||||||
description='Remove Task List query blocks from Logseq notes'
|
parser.add_argument('path', type=Path, help='File or directory to process')
|
||||||
)
|
parser.add_argument('--dry-run', action='store_true', help='Show what would be changed')
|
||||||
parser.add_argument(
|
parser.add_argument('--recursive', '-r', action='store_true', help='Process recursively')
|
||||||
'path',
|
|
||||||
type=Path,
|
|
||||||
help='File or directory to process'
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'--dry-run',
|
|
||||||
action='store_true',
|
|
||||||
help='Show what would be changed without modifying files'
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'--recursive', '-r',
|
|
||||||
action='store_true',
|
|
||||||
help='Process directories recursively'
|
|
||||||
)
|
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
target = args.path
|
target = args.path
|
||||||
dry_run = args.dry_run
|
dry_run = args.dry_run
|
||||||
|
|
||||||
# Collect files to process
|
|
||||||
files = []
|
files = []
|
||||||
|
|
||||||
if target.is_file():
|
if target.is_file():
|
||||||
if target.suffix.lower() not in ['.md', '.markdown']:
|
|
||||||
print(f"Warning: {target.name} doesn't look like a markdown file")
|
|
||||||
response = input("Process anyway? [y/N]: ")
|
|
||||||
if response.lower() != 'y':
|
|
||||||
sys.exit(0)
|
|
||||||
files.append(target)
|
files.append(target)
|
||||||
elif target.is_dir():
|
elif target.is_dir():
|
||||||
if args.recursive:
|
if args.recursive:
|
||||||
files.extend(target.rglob('*.md'))
|
files.extend(target.rglob('*.md'))
|
||||||
files.extend(target.rglob('*.markdown'))
|
|
||||||
else:
|
else:
|
||||||
files.extend(target.glob('*.md'))
|
files.extend(target.glob('*.md'))
|
||||||
files.extend(target.glob('*.markdown'))
|
|
||||||
else:
|
else:
|
||||||
print(f"Error: {target} is not a valid file or directory")
|
print(f"Error: {target} is not valid")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if not files:
|
if not files:
|
||||||
print("No markdown files found to process")
|
print("No markdown files found")
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
print(f"Found {len(files)} markdown file(s)")
|
print(f"Found {len(files)} markdown file(s)")
|
||||||
if dry_run:
|
if dry_run:
|
||||||
print("DRY RUN - No files will be modified\n")
|
print("DRY RUN\n")
|
||||||
|
|
||||||
# Process each file
|
|
||||||
modified_count = 0
|
modified_count = 0
|
||||||
for file_path in sorted(files):
|
for file_path in sorted(files):
|
||||||
if process_file(file_path, dry_run):
|
if process_file(file_path, dry_run):
|
||||||
modified_count += 1
|
modified_count += 1
|
||||||
|
|
||||||
# Summary
|
|
||||||
print(f"\n{'Would modify' if dry_run else 'Modified'} {modified_count} of {len(files)} file(s)")
|
print(f"\n{'Would modify' if dry_run else 'Modified'} {modified_count} of {len(files)} file(s)")
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue