|
| 1 | +import os |
| 2 | +import re |
| 3 | + |
| 4 | +# Define a dictionary of common typos and their corrections |
| 5 | +typos = { |
| 6 | + 'teh': 'the', |
| 7 | + 'recieve': 'receive', |
| 8 | + 'adn': 'and', |
| 9 | + 'occured': 'occurred', |
| 10 | + 'seperate': 'separate', |
| 11 | + 'definately': 'definitely', |
| 12 | + 'goverment': 'government', |
| 13 | + # Add more typos and corrections as needed |
| 14 | +} |
| 15 | + |
| 16 | +def correct_typos_in_file(file_path): |
| 17 | + with open(file_path, 'r') as file: |
| 18 | + content = file.read() |
| 19 | + |
| 20 | + for typo, correction in typos.items(): |
| 21 | + content = re.sub(r'\b' + typo + r'\b', correction, content) |
| 22 | + |
| 23 | + if '<!-- Updated README links and corrected typos -->' not in content: |
| 24 | + content += "\n<!-- Updated README links and corrected typos -->" |
| 25 | + |
| 26 | + with open(file_path, 'w') as file: |
| 27 | + file.write(content) |
| 28 | + print(f"Corrected typos in {file_path}") |
| 29 | + |
| 30 | +def update_links(file_path): |
| 31 | + try: |
| 32 | + with open(file_path, 'r') as file: |
| 33 | + content = file.read() |
| 34 | + |
| 35 | + # Example update: replace 'old-link' with 'new-link' |
| 36 | + updated_content = content.replace('old-link', 'new-link') |
| 37 | + |
| 38 | + with open(file_path, 'w') as file: |
| 39 | + file.write(updated_content) |
| 40 | + |
| 41 | + print(f"Updated {file_path}") |
| 42 | + except Exception as e: |
| 43 | + print(f"Failed to update {file_path}: {e}") |
| 44 | + |
| 45 | +def find_and_process_readmes(root_dir): |
| 46 | + for root, dirs, files in os.walk(root_dir): |
| 47 | + for file in files: |
| 48 | + if file.lower() == 'readme.md': |
| 49 | + file_path = os.path.join(root, file) |
| 50 | + update_links(file_path) |
| 51 | + correct_typos_in_file(file_path) |
| 52 | + |
| 53 | +if __name__ == "__main__": |
| 54 | + root_directory = '.' # Set this to your repo's root directory if necessary |
| 55 | + find_and_process_readmes(root_directory) |
0 commit comments