cmd/nosmartquotes.py

62 lines
1.4 KiB
Python
Raw Normal View History

2021-08-17 07:42:00 +00:00
'''
no smart quotes
===============
Replace smart quotes and smart apostrophes with regular ASCII values.
Just say no to smart quotes!
'''
import argparse
2021-10-05 00:21:14 +00:00
import os
2021-08-17 07:42:00 +00:00
import sys
from voussoirkit import betterhelp
2021-10-05 00:21:14 +00:00
from voussoirkit import pathclass
2021-08-17 07:42:00 +00:00
from voussoirkit import spinal
from voussoirkit import vlogging
log = vlogging.getLogger(__name__, 'nosmartquotes')
2021-10-05 00:21:14 +00:00
THIS_FILE = os.path.abspath(__file__)
2021-08-17 07:42:00 +00:00
def replace_smartquotes(text):
text = text.replace('', '"')
text = text.replace('', '"')
text = text.replace('', "'")
text = text.replace('', "'")
return text
def nosmartquotes_argparse(args):
files = spinal.walk(
glob_filenames=args.filename_glob,
2021-10-05 00:21:14 +00:00
exclude_filenames={THIS_FILE},
2021-08-17 07:42:00 +00:00
recurse=args.recurse,
)
for file in files:
2021-10-05 00:21:14 +00:00
text = file.read('r', encoding='utf-8')
2021-08-17 07:42:00 +00:00
original_text = text
text = replace_smartquotes(text)
if text == original_text:
continue
2021-10-05 00:21:14 +00:00
file.write('w', text, encoding='utf-8')
2021-08-17 07:42:00 +00:00
print(file.absolute_path)
return 0
@vlogging.main_decorator
def main(argv):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('filename_glob')
parser.add_argument('--recurse', action='store_true')
parser.set_defaults(func=nosmartquotes_argparse)
return betterhelp.single_main(argv, parser, __doc__)
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))