provisioning/scripts/add-code-block-languages.py

107 lines
3.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""Add language specifiers to opening code fences only.
Never touch closing fences - closing fences MUST remain as just ```
"""
import re
import sys
from pathlib import Path
def add_code_block_languages(content):
"""Add language specifier to opening code fences without one (MD040).
Logic:
- Find ``` at start of line followed by newline (no language)
- Add 'text' as default language
- NEVER modify closing fences
This is done carefully:
1. Split into lines
2. Track whether we're inside a code block
3. For opening fences without language: add 'text'
4. Leave closing fences untouched
"""
lines = content.split('\n')
fixed_lines = []
in_code_block = False
for line in lines:
# Check if this is a code fence line
if line.startswith('```'):
# Extract what's after the backticks
fence_content = line[3:]
if not in_code_block:
# This is an OPENING fence
if not fence_content.strip():
# Opening fence with no language - add 'text'
fixed_lines.append('```text')
in_code_block = True
else:
# Already has a language specifier
fixed_lines.append(line)
in_code_block = True
else:
# This is a CLOSING fence
# MUST remain as just ``` - never add language
fixed_lines.append('```')
in_code_block = False
else:
fixed_lines.append(line)
return '\n'.join(fixed_lines)
def fix_file(filepath):
"""Fix code block languages in a single file."""
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
fixed_content = add_code_block_languages(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():
"""Add language specifiers to 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✓ Added code block languages to {success_count}/{len(ai_files)} files")
return 0 if success_count == len(ai_files) else 1
if __name__ == '__main__':
sys.exit(main())