Skip to main content

scripts-fix-md056-table-column-count

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

CODITECT Markdown Quality System Copyright © 2025 AZ1.AI INC - All Rights Reserved""" from pathlib import Path import re

def fix_md056_table_column_count(content): """Fix tables with inconsistent column counts 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 = []
start_idx = i

# 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 at minimum
# Determine max column count
col_counts = []
for tline in table_lines:
# Count pipes, subtract 1 for column count (pipes are delimiters)
cols = len([c for c in tline if c == '|']) - 1
col_counts.append(cols)

max_cols = max(col_counts)

# Fix rows with fewer columns
for idx, tline in enumerate(table_lines):
current_cols = col_counts[idx]
if current_cols < max_cols:
# Add empty cells to match max columns
cells = tline.split('|')

# Add empty cells at the end
needed = max_cols - current_cols
for _ in range(needed):
cells.insert(-1, ' ') # Insert before the last empty element

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_md056_table_column_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")