(argv: Sequence[str] | None = None)
| 48 | |
| 49 | |
| 50 | def main(argv: Sequence[str] | None = None) -> int: |
| 51 | parser = argparse.ArgumentParser() |
| 52 | parser.add_argument("filenames", nargs="*") |
| 53 | args = parser.parse_args(argv) |
| 54 | |
| 55 | contents: dict[str, str] = {} |
| 56 | for filename in args.filenames: |
| 57 | with open(filename, encoding="UTF-8") as f: |
| 58 | contents[filename] = f.read() |
| 59 | |
| 60 | formatted = format_code_blocks( |
| 61 | [textwrap.dedent(match["code"]) for src in contents.values() for match in MD_RE.finditer(src)] |
| 62 | ) |
| 63 | |
| 64 | def _md_match(match: Match[str]) -> str: |
| 65 | code = textwrap.indent(formatted[textwrap.dedent(match["code"])], match["indent"]) |
| 66 | return f"{match['before']}{code}{match['after']}" |
| 67 | |
| 68 | for filename, src in contents.items(): |
| 69 | new_contents = MD_RE.sub(_md_match, src) |
| 70 | if src != new_contents: |
| 71 | print(f"{filename}: Rewriting...") |
| 72 | with open(filename, "w", encoding="UTF-8") as f: |
| 73 | f.write(new_contents) |
| 74 | return 0 |
| 75 | |
| 76 | |
| 77 | if __name__ == "__main__": |
no test coverage detected