78 lines
1.9 KiB
Python
78 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Append table of contents directive to RST files that don't have one.
|
|
|
|
This script:
|
|
1. Finds all .rst files in content/ directory
|
|
2. Checks if they already have .. contents::
|
|
3. Appends proper whitespace and .. contents:: to files without it
|
|
4. Reports which files were modified
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
def has_toc(content):
|
|
"""Check if file already has a table of contents directive."""
|
|
return '.. contents::' in content
|
|
|
|
|
|
def append_toc(filepath):
|
|
"""Append TOC directive to file with proper whitespace."""
|
|
with open(filepath, 'r', encoding='utf-8') as f:
|
|
content = f.read()
|
|
|
|
# Check if already has TOC
|
|
if has_toc(content):
|
|
return False
|
|
|
|
# Ensure file ends with newline
|
|
if not content.endswith('\n'):
|
|
content += '\n'
|
|
|
|
# Add blank line and TOC directive
|
|
content += '\n.. contents::\n'
|
|
|
|
# Write back to file
|
|
with open(filepath, 'w', encoding='utf-8') as f:
|
|
f.write(content)
|
|
|
|
return True
|
|
|
|
|
|
def main():
|
|
content_dir = Path('content')
|
|
|
|
if not content_dir.exists():
|
|
print("Error: content/ directory not found")
|
|
sys.exit(1)
|
|
|
|
# Find all RST files
|
|
rst_files = sorted(content_dir.glob('*.rst'))
|
|
|
|
modified_files = []
|
|
skipped_files = []
|
|
|
|
for filepath in rst_files:
|
|
if append_toc(filepath):
|
|
modified_files.append(filepath.name)
|
|
print(f"✓ Added TOC to: {filepath.name}")
|
|
else:
|
|
skipped_files.append(filepath.name)
|
|
|
|
# Summary
|
|
print(f"\n{'='*60}")
|
|
print(f"Modified: {len(modified_files)} files")
|
|
print(f"Skipped (already has TOC): {len(skipped_files)} files")
|
|
print(f"{'='*60}")
|
|
|
|
if modified_files:
|
|
print("\nModified files:")
|
|
for filename in modified_files:
|
|
print(f" - {filename}")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|