Fix Logseq Task List query block pattern #1
2 changed files with 229 additions and 0 deletions
150
ensure_newline_ending.py
Executable file
150
ensure_newline_ending.py
Executable file
|
|
@ -0,0 +1,150 @@
|
|||
#!/usr/bin/env python3
|
||||
"""
|
||||
Logseq File Ending Normalizer
|
||||
|
||||
This script ensures all Logseq notes files end with a single newline character.
|
||||
Files that don't end with a newline or end with multiple newlines will be fixed.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Tuple
|
||||
|
||||
|
||||
def ensure_single_trailing_newline(content: str) -> Tuple[str, int]:
|
||||
"""
|
||||
Ensure content ends with exactly one newline.
|
||||
|
||||
Args:
|
||||
content: The file content
|
||||
|
||||
Returns:
|
||||
Tuple of (normalized content, action taken)
|
||||
action: 0 = no change, 1 = added newline, 2 = removed extra newlines
|
||||
"""
|
||||
if not content:
|
||||
return content, 0
|
||||
|
||||
# Strip all trailing newlines
|
||||
stripped = content.rstrip('\n')
|
||||
|
||||
# Add exactly one newline
|
||||
normalized = stripped + '\n'
|
||||
|
||||
# Determine what action was taken
|
||||
if content == normalized:
|
||||
return content, 0 # No change needed
|
||||
elif not content.endswith('\n'):
|
||||
return normalized, 1 # Added missing newline
|
||||
else:
|
||||
return normalized, 2 # Removed extra newlines
|
||||
|
||||
|
||||
def process_file(file_path: Path, dry_run: bool = False) -> bool:
|
||||
"""
|
||||
Process a single file to ensure proper ending.
|
||||
|
||||
Args:
|
||||
file_path: Path to the 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
|
||||
|
||||
normalized, action = ensure_single_trailing_newline(content)
|
||||
|
||||
if action == 0:
|
||||
print(f" ✓ OK (already ends with single newline) {file_path.name}")
|
||||
return False
|
||||
|
||||
if dry_run:
|
||||
action_desc = {1: 'add newline', 2: 'remove extra newlines'}
|
||||
print(f" WOULD {action_desc[action]} {file_path.name}")
|
||||
return True
|
||||
|
||||
# Write normalized content back
|
||||
try:
|
||||
file_path.write_text(normalized, encoding='utf-8')
|
||||
action_desc = {1: 'Added newline to', 2: 'Removed extra newlines from'}
|
||||
print(f" ✓ {action_desc[action]} {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='Ensure all Logseq files end with a single newline'
|
||||
)
|
||||
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 = []
|
||||
|
||||
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
|
||||
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()
|
||||
79
run_full_cleanup.sh
Executable file
79
run_full_cleanup.sh
Executable file
|
|
@ -0,0 +1,79 @@
|
|||
#!/bin/bash
|
||||
#
|
||||
# Logseq Full Cleanup Runner
|
||||
#
|
||||
# Runs all cleanup scripts in the correct order to normalize Logseq journal files.
|
||||
#
|
||||
# Order of operations:
|
||||
# 1. Remove Task List query blocks (largest changes first)
|
||||
# 2. Remove empty bullet lines (cleanup remnants)
|
||||
# 3. Ensure single trailing newline (final normalization)
|
||||
#
|
||||
# Usage:
|
||||
# ./run_full_cleanup.sh [--dry-run] <path>
|
||||
#
|
||||
# Examples:
|
||||
# ./run_full_cleanup.sh --dry-run ~/notes_test/journals/
|
||||
# ./run_full_cleanup.sh ~/notes_test/journals/2026_10_23.md
|
||||
# ./run_full_cleanup.sh -r ~/notes_test/journals/
|
||||
#
|
||||
|
||||
set -e
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
DRY_RUN=""
|
||||
RECURSIVE=""
|
||||
TARGET=""
|
||||
|
||||
# Parse arguments
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case $1 in
|
||||
--dry-run)
|
||||
DRY_RUN="--dry-run"
|
||||
shift
|
||||
;;
|
||||
-r)
|
||||
RECURSIVE="-r"
|
||||
shift
|
||||
;;
|
||||
*)
|
||||
TARGET="$1"
|
||||
shift
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [ -z "$TARGET" ]; then
|
||||
echo "Error: No target path specified"
|
||||
echo "Usage: $0 [--dry-run] [-r] <path>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "=========================================="
|
||||
echo "Logseq Full Cleanup"
|
||||
echo "=========================================="
|
||||
echo "Target: $TARGET"
|
||||
[ -n "$DRY_RUN" ] && echo "Mode: DRY RUN (no changes will be made)"
|
||||
echo ""
|
||||
|
||||
# Step 1: Remove Task List query blocks
|
||||
echo "Step 1/3: Removing Task List query blocks..."
|
||||
echo "-------------------------------------------"
|
||||
python3 "$SCRIPT_DIR/clean_task_lists.py" $DRY_RUN $RECURSIVE "$TARGET"
|
||||
echo ""
|
||||
|
||||
# Step 2: Remove empty bullet lines
|
||||
echo "Step 2/3: Removing empty bullet lines..."
|
||||
echo "-------------------------------------------"
|
||||
python3 "$SCRIPT_DIR/clean_empty_bullets.py" $DRY_RUN $RECURSIVE "$TARGET"
|
||||
echo ""
|
||||
|
||||
# Step 3: Ensure single trailing newline
|
||||
echo "Step 3/3: Ensuring single trailing newline..."
|
||||
echo "-------------------------------------------"
|
||||
python3 "$SCRIPT_DIR/ensure_newline_ending.py" $DRY_RUN $RECURSIVE "$TARGET"
|
||||
echo ""
|
||||
|
||||
echo "=========================================="
|
||||
echo "Cleanup complete!"
|
||||
echo "=========================================="
|
||||
Loading…
Add table
Add a link
Reference in a new issue