Refactor Logseq scripts to their own directory
This commit is contained in:
parent
3b95671fb8
commit
a0b42920d9
6 changed files with 0 additions and 0 deletions
|
|
@ -1,145 +0,0 @@
|
|||
#!/usr/bin/env python3
|
||||
"""Logseq Task List Query Block Remover"""
|
||||
|
||||
import re
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Tuple
|
||||
|
||||
|
||||
def remove_task_list_queries(content: str) -> Tuple[str, int]:
|
||||
"""Remove all Task List query blocks from Logseq 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
|
||||
|
||||
|
||||
def process_file(file_path: Path, dry_run: bool = False) -> bool:
|
||||
try:
|
||||
content = file_path.read_text(encoding='utf-8')
|
||||
except Exception as e:
|
||||
print(f" ERROR reading {file_path}: {e}")
|
||||
return False
|
||||
|
||||
cleaned, count = remove_task_list_queries(content)
|
||||
|
||||
if count == 0:
|
||||
print(f" No Task List blocks found in {file_path.name}")
|
||||
return False
|
||||
|
||||
if dry_run:
|
||||
print(f" WOULD remove {count} Task List block(s) from {file_path.name}")
|
||||
return True
|
||||
|
||||
try:
|
||||
file_path.write_text(cleaned, encoding='utf-8')
|
||||
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}")
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(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('--recursive', '-r', action='store_true', help='Process recursively')
|
||||
|
||||
args = parser.parse_args()
|
||||
target = args.path
|
||||
dry_run = args.dry_run
|
||||
|
||||
files = []
|
||||
if target.is_file():
|
||||
files.append(target)
|
||||
elif target.is_dir():
|
||||
if args.recursive:
|
||||
files.extend(target.rglob('*.md'))
|
||||
else:
|
||||
files.extend(target.glob('*.md'))
|
||||
else:
|
||||
print(f"Error: {target} is not valid")
|
||||
sys.exit(1)
|
||||
|
||||
if not files:
|
||||
print("No markdown files found")
|
||||
sys.exit(0)
|
||||
|
||||
print(f"Found {len(files)} markdown file(s)")
|
||||
if dry_run:
|
||||
print("DRY RUN\n")
|
||||
|
||||
modified = 0
|
||||
for f in sorted(files):
|
||||
if process_file(f, dry_run):
|
||||
modified += 1
|
||||
|
||||
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__':
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue