Created
May 30, 2022 15:14
-
-
Save adamchainz/5683986d2945cb01b079248df8c25e57 to your computer and use it in GitHub Desktop.
Curly quote fixer script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
def main(argv=None): | |
parser = argparse.ArgumentParser( | |
description="Replace curly quotes with straight versions." | |
) | |
parser.add_argument( | |
"file", | |
nargs="+", | |
type=argparse.FileType("r+", encoding="utf-8"), | |
) | |
args = parser.parse_args(argv) | |
exit_code = 0 | |
for file in args.file: | |
if fix_file(file): | |
exit_code = 1 | |
file.close() | |
return exit_code | |
translate_quotes = str.maketrans( | |
{ | |
"“": '"', | |
"”": '"', | |
"‘": "'", | |
"’": "'", | |
} | |
) | |
def fix_file(file): | |
orig_text = file.read() | |
new_text = orig_text.translate(translate_quotes) | |
changed = new_text != orig_text | |
if changed: | |
print(f"Fixing {file.name}") | |
file.seek(0) | |
file.write(new_text) | |
file.truncate() | |
return changed | |
if __name__ == "__main__": | |
raise SystemExit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment