Skip to content

Instantly share code, notes, and snippets.

@adamchainz
Created May 30, 2022 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamchainz/5683986d2945cb01b079248df8c25e57 to your computer and use it in GitHub Desktop.
Save adamchainz/5683986d2945cb01b079248df8c25e57 to your computer and use it in GitHub Desktop.
Curly quote fixer script
#!/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