provisioning/scripts/fix-closing-fences.py
2026-01-14 02:59:52 +00:00

80 lines
2.4 KiB
Python

#!/usr/bin/env python3
"""Fix malformed closing fences - remove language specifiers from closing ```."""
import re
import sys
from pathlib import Path
def fix_closing_fences(content):
"""Fix malformed closing code fences: ```text -> ```"""
# Match closing fence lines that have language specifiers
# Pattern: line starts with ``` followed by word characters (language)
# This must be on a line by itself (possibly with whitespace)
pattern = r'^```\w+\s*$'
lines = content.split('\n')
fixed_lines = []
for line in lines:
# If this line is a closing fence with language specifier, remove it
if re.match(pattern, line):
fixed_lines.append('```')
else:
fixed_lines.append(line)
return '\n'.join(fixed_lines)
def fix_file(filepath):
"""Fix all malformed closing fences in a single file."""
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
fixed_content = fix_closing_fences(content)
with open(filepath, 'w', encoding='utf-8') as f:
f.write(fixed_content)
return True
except Exception as e:
print(f"Error processing {filepath}: {e}", file=sys.stderr)
return False
def main():
"""Fix malformed closing fences in all AI documentation files."""
docs_root = Path('provisioning/docs/src')
# All AI files
ai_files = [
'ai/ai-agents.md',
'ai/ai-assisted-forms.md',
'ai/architecture.md',
'ai/config-generation.md',
'ai/configuration.md',
'ai/cost-management.md',
'ai/mcp-integration.md',
'ai/natural-language-config.md',
'ai/rag-system.md',
'ai/README.md',
'ai/security-policies.md',
'ai/troubleshooting-with-ai.md',
]
success_count = 0
for filepath_rel in ai_files:
filepath = docs_root / filepath_rel
if filepath.exists():
if fix_file(filepath):
print(f"✓ Fixed {filepath_rel}")
success_count += 1
else:
print(f"✗ Failed to fix {filepath_rel}")
else:
print(f"⚠ File not found: {filepath_rel}")
print(f"\n✓ Fixed malformed closing fences in {success_count}/{len(ai_files)} files")
return 0 if success_count == len(ai_files) else 1
if __name__ == '__main__':
sys.exit(main())