|
| 1 | +""" |
| 2 | +Quick and dirty script to create the "rules.md" file from components |
| 3 | +of the extensions files themselves. |
| 4 | +""" |
| 5 | + |
| 6 | +import os |
| 7 | +import shutil |
| 8 | +import tempfile |
| 9 | + |
| 10 | +script_path = os.path.dirname(os.path.realpath(__file__)) |
| 11 | +extensions_path = os.path.join(script_path, "src", "plugins") |
| 12 | +extension_files = os.listdir(extensions_path) |
| 13 | +extension_files.sort() |
| 14 | + |
| 15 | +with tempfile.NamedTemporaryFile() as temp_output: |
| 16 | + temporary_file_name = temp_output.name |
| 17 | +with open(temporary_file_name, "wt", encoding="utf-8") as destination_file: |
| 18 | + destination_file.write("# Rules\n") |
| 19 | + |
| 20 | + for next_extension_file in extension_files: |
| 21 | + with open( |
| 22 | + os.path.join(extensions_path, next_extension_file), "r", encoding="utf-8" |
| 23 | + ) as readme_file: |
| 24 | + file_contents = readme_file.read() |
| 25 | + |
| 26 | + first_prefix = "# " |
| 27 | + assert file_contents.startswith(first_prefix) |
| 28 | + dd = file_contents.index("\n") |
| 29 | + extension_title_text = file_contents[len(first_prefix) : dd] |
| 30 | + |
| 31 | + end_of_first_part = file_contents.index("\n## Reasoning") |
| 32 | + replacement_text = ( |
| 33 | + file_contents[dd:end_of_first_part] |
| 34 | + .replace( |
| 35 | + "## Summary", |
| 36 | + "<!--- pyml disable-next-line no-duplicate-heading-->\n### Summary", |
| 37 | + ) |
| 38 | + .replace( |
| 39 | + "## Deprecation", |
| 40 | + "<!--- pyml disable-next-line no-duplicate-heading-->\n### Deprecation", |
| 41 | + ) |
| 42 | + .replace("./rule_", "./plugins/rule_") |
| 43 | + ) |
| 44 | + while replacement_text.startswith("\n"): |
| 45 | + replacement_text = replacement_text[1:] |
| 46 | + |
| 47 | + destination_file.write( |
| 48 | + f"""\n## {extension_title_text} |
| 49 | +
|
| 50 | +[Full Documentation](./plugins/{next_extension_file}) |
| 51 | +
|
| 52 | +{replacement_text}""" |
| 53 | + ) |
| 54 | +print(temporary_file_name) |
| 55 | + |
| 56 | +output_path = os.path.join(script_path, "src", "rules.md") |
| 57 | +shutil.copyfile(temporary_file_name, output_path) |
| 58 | + |
| 59 | +# rule_md033/ |
0 commit comments