Skip to main content

scripts-fix-md046-code-block-style

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

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

MD046: Code block style (prefer fenced over indented)

import re from pathlib import Path

def fix_md046(content): lines = content.split('\n') result = [] fixed = 0 in_indented_block = False block_lines = []

for i, line in enumerate(lines):
is_indented = line.startswith(' ') and line.strip()

if is_indented and not in_indented_block:
in_indented_block = True
block_lines = [line[4:]] # Remove indent
elif is_indented and in_indented_block:
block_lines.append(line[4:])
elif in_indented_block:
# End of block - convert to fenced
result.append('```')
result.extend(block_lines)
result.append('```')
result.append(line)
fixed += 1
in_indented_block = False
block_lines = []
else:
result.append(line)

# Handle block at end of file
if in_indented_block:
result.append('```')
result.extend(block_lines)
result.append('```')
fixed += 1

return '\n'.join(result), fixed

def process_file(file_path, dry_run=False): with open(file_path, 'r', encoding='utf-8') as f: content = f.read() fixed_content, count = fix_md046(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() 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':
count = process_file(path, args.dry_run)
if count:
print(f"{'[DRY RUN] ' if args.dry_run else ''}Fixed {count} issues in {path}")
total += count
elif path.is_dir():
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} issues in {md_file}")
total += count
print(f"\nTotal: {total} issues fixed")