Skip to main content

scripts-fix-md009-trailing-spaces

#!/usr/bin/env python3 """

CODITECT Markdown Quality System Copyright © 2025 AZ1.AI INC - All Rights Reserved

This software is proprietary and confidential. Unauthorized copying, distribution, or use is strictly prohibited.

CODITECT owns all intellectual property rights to this implementation. """

"""MD009 Fix: Trailing spaces - Remove trailing whitespace from lines""" import sys from pathlib import Path

def fix_md009(content: str): """Remove trailing spaces from all lines.""" lines = [line.rstrip() for line in content.split('\n')] original_lines = content.split('\n') count = sum(1 for i, line in enumerate(original_lines) if line != line.rstrip()) return '\n'.join(lines), count

def process_file(file_path: Path, dry_run=False): with open(file_path, 'r', encoding='utf-8') as f: content = f.read() fixed, count = fix_md009(content) if count > 0 and not dry_run: with open(file_path, 'w', encoding='utf-8') as f: f.write(fixed) return count

if name == 'main': import argparse parser = argparse.ArgumentParser(description='Fix MD009: Trailing spaces') parser.add_argument('paths', nargs='*', default=['.']) parser.add_argument('--dry-run', action='store_true') args = parser.parse_args()

total = 0
for path_str in args.paths:
path = Path(path_str)
if path.is_file() and path.suffix == '.md':
# Process single file
count = process_file(path, args.dry_run)
if count:
print(f"{'[DRY RUN] ' if args.dry_run else ''}Fixed {count} lines in {path}")
total += count
elif path.is_dir():
# Process directory
for md_file in path.rglob('*.md'):
count = process_file(md_file, args.dry_run)
if count:
print(f"{'[DRY RUN] ' if args.dry_run else ''}Fixed {count} lines in {md_file}")
total += count
print(f"\nTotal: {total} trailing spaces removed")