scripts-fix-md059-table-cell-count
#!/usr/bin/env python3 """
title: "Fix Md059 Table Cell Count" component_type: script version: "1.0.0" audience: contributor status: stable summary: "CODITECT Markdown Quality System Copyright © 2025 AZ1.AI INC - All Rights Reserved" from pat..." keywords: ['cell', 'count', 'fix', 'md059', 'table'] tokens: ~500 created: 2025-12-22 updated: 2025-12-22 script_name: "fix-md059-table-cell-count.py" language: python executable: true usage: "python3 scripts/fix-md059-table-cell-count.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""" from pathlib import Path import re
def fix_md059_table_cell_count(content): """Fix tables where rows don't have the correct number of cells by padding with empty cells.""" lines = content.split('\n') result = [] fixed = 0 i = 0
while i < len(lines):
line = lines[i]
# Check if this is a table row (contains |)
if '|' in line and line.strip():
# Find the table block
table_lines = []
# Collect all consecutive table lines
while i < len(lines) and '|' in lines[i] and lines[i].strip():
table_lines.append(lines[i])
i += 1
if len(table_lines) >= 2: # Valid table needs header + separator
# Count columns from header row
header = table_lines[0]
expected_cols = header.count('|') - 1
# Fix rows that don't match column count
for idx, tline in enumerate(table_lines):
current_cols = tline.count('|') - 1
if current_cols != expected_cols:
cells = tline.split('|')
if current_cols < expected_cols:
# Add empty cells
needed = expected_cols - current_cols
for _ in range(needed):
cells.insert(-1, ' ')
else:
# Remove extra cells (keep first expected_cols + 2 for start/end pipes)
cells = cells[:expected_cols + 2]
table_lines[idx] = '|'.join(cells)
fixed += 1
result.extend(table_lines)
else:
result.extend(table_lines)
else:
result.append(line)
i += 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_md059_table_cell_count(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")