Skip to main content

scripts-fix-md036

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

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

Simple wrapper for MD036 emphasis-to-heading fixes for testing.""" from pathlib import Path import re

def fix_md036(content): """Convert emphasis used as headings to proper headings.""" lines = content.split('\n') result = [] fixed = 0

emphasis_pattern = re.compile(r'^\s*(\*\*|__)(.*?)\1\s*$|^\s*(\*|_)(.*?)\3\s*$')

for i, line in enumerate(lines):
match = emphasis_pattern.match(line)

if match:
# Extract text from emphasis
text = (match.group(2) or match.group(4)).strip()

# Simple heuristic: if it's short and looks like a heading, convert it
if text and len(text) < 60:
# Check if next line is blank (heading indicator)
next_line = lines[i + 1] if i + 1 < len(lines) else ''
prev_line = lines[i - 1] if i > 0 else ''

if next_line.strip() == '' or prev_line.strip() == '':
# Convert to h2 heading
result.append(f"## {text}")
fixed += 1
continue

result.append(line)

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_md036(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} 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} in {md_file}")
total += count
print(f"\nTotal: {total} fixed")