|
| 1 | +import os |
| 2 | +import re |
| 3 | +import math |
| 4 | + |
| 5 | +# Function to find .yaml files recursively |
| 6 | +def find_yaml_files(root_dir): |
| 7 | + yaml_files = [] |
| 8 | + for dirpath, _, filenames in os.walk(root_dir): |
| 9 | + for filename in filenames: |
| 10 | + if filename.endswith('.yaml') and re.match(r'(\d+\.)+\d+\.yaml', filename): |
| 11 | + yaml_files.append(os.path.join(dirpath, filename)) |
| 12 | + return yaml_files |
| 13 | + |
| 14 | +# Function to update README.md with an HTML table (5 columns, no .yaml extension) |
| 15 | +def update_readme(yaml_files): |
| 16 | + readme_file = 'README.md' |
| 17 | + try: |
| 18 | + with open(readme_file, 'r', encoding='utf-8') as file: |
| 19 | + readme_content = file.read() |
| 20 | + |
| 21 | + # Remove .yaml extension and sort filenames |
| 22 | + yaml_filenames = sorted(set(os.path.splitext(os.path.basename(f))[0] for f in yaml_files)) |
| 23 | + |
| 24 | + # Create a table with 5 columns |
| 25 | + table_rows = "" |
| 26 | + num_files = len(yaml_filenames) |
| 27 | + num_columns = 5 |
| 28 | + num_rows = math.ceil(num_files / num_columns) |
| 29 | + |
| 30 | + for i in range(num_rows): |
| 31 | + row_files = yaml_filenames[i * num_columns:(i + 1) * num_columns] |
| 32 | + table_rows += "<tr>" + "".join(f"<td>{file}</td>" for file in row_files) + "</tr>\n" |
| 33 | + |
| 34 | + table_html = f"""<h2 align="center">Available Templates</h2> |
| 35 | +<table border="1" cellpadding="5" cellspacing="0" align="center"> |
| 36 | + {table_rows} |
| 37 | +</table> |
| 38 | +</center> |
| 39 | +""" |
| 40 | + |
| 41 | + if "<h2 align=\"center\">Available Templates</h2>" in readme_content: |
| 42 | + h2_index = readme_content.index("<h2 align=\"center\">Available Templates</h2>") |
| 43 | + |
| 44 | + readme_content = readme_content[:h2_index] |
| 45 | + |
| 46 | + readme_content += f'{table_html}' |
| 47 | + with open(readme_file, 'w', encoding='utf-8') as file: |
| 48 | + file.write(readme_content) |
| 49 | + |
| 50 | + print("README.md updated successfully.") |
| 51 | + |
| 52 | + except FileNotFoundError: |
| 53 | + print(f"{readme_file} not found.") |
| 54 | + except Exception as e: |
| 55 | + print(f"An error occurred: {e}") |
| 56 | + |
| 57 | +if __name__ == '__main__': |
| 58 | + root_dir = '../../' |
| 59 | + yaml_files = find_yaml_files(root_dir) |
| 60 | + if yaml_files: |
| 61 | + update_readme(yaml_files) |
| 62 | + else: |
| 63 | + print("No matching YAML files found.") |
0 commit comments