scripts-fix-md031-fenced-code-blank-lines
#!/usr/bin/env python3 """
title: "Fix Md031 Fenced Code Blank Lines" component_type: script version: "1.0.0" audience: contributor status: stable summary: "CODITECT Markdown Quality System Copyright © 2025 AZ1.AI INC - All Rights Reserved" keywords: ['blank', 'code', 'fenced', 'fix', 'lines'] tokens: ~500 created: 2025-12-22 updated: 2025-12-22 script_name: "fix-md031-fenced-code-blank-lines.py" language: python executable: true usage: "python3 scripts/fix-md031-fenced-code-blank-lines.py [options]" python_version: "3.10+" dependencies: [] modifies_files: false network_access: false requires_auth: false
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. """
"""MD031 Fix: Fenced code blocks should be surrounded by blank lines""" import re from pathlib import Path
def fix_md031(content: str): """Add blank lines around fenced code blocks.""" lines = content.split('\n') result = [] in_code = False fixed = 0 i = 0
while i < len(lines):
line = lines[i]
if re.match(r'^```', line):
if not in_code: # Opening fence
# Add blank line before if previous line has content
if result and result[-1].strip():
result.append('')
fixed += 1
result.append(line)
in_code = True
else: # Closing fence
result.append(line)
# Add blank line after if next line has content
if i + 1 < len(lines) and lines[i + 1].strip():
result.append('')
fixed += 1
in_code = False
else:
result.append(line)
i += 1
return '\n'.join(result), fixed
def process_file(file_path: Path, dry_run=False): with open(file_path, 'r', encoding='utf-8') as f: content = f.read() fixed_content, count = fix_md031(content) if count > 0 and not dry_run: with open(file_path, 'w', encoding='utf-8') as f: f.write(fixed_content) return count
if name == 'main': import argparse parser = argparse.ArgumentParser(description='Fix MD031: Fenced code blocks blank lines') 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} code blocks 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} code blocks in {md_file}")
total += count
print(f"\nTotal: {total} code block boundaries fixed")