Fix Logseq Task List query block pattern #1
1 changed files with 154 additions and 0 deletions
154
clean_empty_bullets.py
Normal file
154
clean_empty_bullets.py
Normal file
|
|
@ -0,0 +1,154 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Logseq Empty Bullet Line Remover
|
||||||
|
|
||||||
|
This script removes empty bullet lines from Logseq notes - lines that contain
|
||||||
|
only whitespace and/or a single dash (-). These often remain after removing
|
||||||
|
query blocks or cleaning up other content.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import List, Tuple
|
||||||
|
|
||||||
|
|
||||||
|
def remove_empty_bullets(content: str) -> Tuple[str, int]:
|
||||||
|
"""
|
||||||
|
Remove empty bullet lines from Logseq content.
|
||||||
|
|
||||||
|
Empty bullet lines are lines that contain only:
|
||||||
|
- Whitespace
|
||||||
|
- A single dash/hyphen (-)
|
||||||
|
- Optional trailing whitespace
|
||||||
|
|
||||||
|
Args:
|
||||||
|
content: The Logseq markdown content
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Tuple of (cleaned content, number of lines removed)
|
||||||
|
"""
|
||||||
|
lines = content.split('\n')
|
||||||
|
cleaned_lines = []
|
||||||
|
count = 0
|
||||||
|
|
||||||
|
for line in lines:
|
||||||
|
# Match lines that are only whitespace and/or a single dash
|
||||||
|
# Pattern: optional whitespace, optional single dash, optional whitespace, end of line
|
||||||
|
if re.match(r'^\s*-?\s*$', line):
|
||||||
|
count += 1
|
||||||
|
# Skip this line (don't add to cleaned_lines)
|
||||||
|
continue
|
||||||
|
cleaned_lines.append(line)
|
||||||
|
|
||||||
|
cleaned = '\n'.join(cleaned_lines)
|
||||||
|
return cleaned, count
|
||||||
|
|
||||||
|
|
||||||
|
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:
|
||||||
|
content = file_path.read_text(encoding='utf-8')
|
||||||
|
except Exception as e:
|
||||||
|
print(f" ERROR reading {file_path}: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
cleaned, count = remove_empty_bullets(content)
|
||||||
|
|
||||||
|
if count == 0:
|
||||||
|
print(f" No empty bullets found in {file_path.name}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
if dry_run:
|
||||||
|
print(f" WOULD remove {count} empty bullet line(s) from {file_path.name}")
|
||||||
|
return True
|
||||||
|
|
||||||
|
# Write cleaned content back
|
||||||
|
try:
|
||||||
|
file_path.write_text(cleaned, encoding='utf-8')
|
||||||
|
print(f" ✓ Removed {count} empty bullet line(s) from {file_path.name}")
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
print(f" ERROR writing {file_path}: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""Main entry point for the script."""
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description='Remove empty bullet lines 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 without modifying files'
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--recursive', '-r',
|
||||||
|
action='store_true',
|
||||||
|
help='Process directories recursively'
|
||||||
|
)
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
target = args.path
|
||||||
|
dry_run = args.dry_run
|
||||||
|
|
||||||
|
# Collect files to process
|
||||||
|
files: List[Path] = []
|
||||||
|
|
||||||
|
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)
|
||||||
|
elif target.is_dir():
|
||||||
|
if args.recursive:
|
||||||
|
files.extend(target.rglob('*.md'))
|
||||||
|
files.extend(target.rglob('*.markdown'))
|
||||||
|
else:
|
||||||
|
files.extend(target.glob('*.md'))
|
||||||
|
files.extend(target.glob('*.markdown'))
|
||||||
|
else:
|
||||||
|
print(f"Error: {target} is not a valid file or directory")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
if not files:
|
||||||
|
print("No markdown files found to process")
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
print(f"Found {len(files)} markdown file(s)")
|
||||||
|
if dry_run:
|
||||||
|
print("DRY RUN - No files will be modified\n")
|
||||||
|
|
||||||
|
# Process each file
|
||||||
|
modified_count = 0
|
||||||
|
total_removed = 0
|
||||||
|
for file_path in sorted(files):
|
||||||
|
if process_file(file_path, dry_run):
|
||||||
|
modified_count += 1
|
||||||
|
|
||||||
|
# Summary
|
||||||
|
print(f"\n{'Would modify' if dry_run else 'Modified'} {modified_count} of {len(files)} file(s)")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue